Minifiers

Several minifiers are available. Minification can take several seconds so it is recommended to set up a cache directory.

Google Closure Compiler service

This is the best choice for most users. The Closure Compiler service is hosted by Google and is accessible via HTTP. The default configuration gives the best results.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->enableJavaScript();
$configurator->javascript->setMinifier('ClosureCompilerService');

Google Closure Compiler application

Alternatively, the Google Closure Compiler application can be used. This requires PHP to be able to use exec() to execute the relevant executables. Like the Google Closure Compiler service, configuration is automatic.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->enableJavaScript();

// Using the Java application
$configurator->javascript->setMinifier(
    'ClosureCompilerApplication',
    'java -jar /usr/local/bin/compiler.jar'
);

// You can use npx or a native executable
$configurator->javascript->setMinifier(
    'ClosureCompilerApplication',
    'npx google-closure-compiler'
);

MatthiasMullie\Minify

Minify is a JavaScript minifier written in PHP. Its minification is not as extensive as Google's Closure Compiler but it is fast and does not use any external service. In order to use this minifier you must have Minify already installed.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->enableJavaScript();
$configurator->javascript->setMinifier('MatthiasMullieMinify');

Meta-minifiers

Noop

As the name implies, the no-op minifier will preserve the original source as-is. This is the default setting.

FirstAvailable

The FirstAvailable strategy allows multiple minifiers to be set. They are executed in order and the result of the first successful minification is returned.

In the following example, we set up the ClosureCompilerService minifier to handle minification. If it fails, the MatthiasMullieMinify minifier will be executed. If it fails too, the Noop (no-op) minifier is executed as a fail-safe and the original source is returned.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->enableJavaScript();

$minifier = $configurator->javascript->setMinifier('FirstAvailable');
$minifier->add('ClosureCompilerService');
$minifier->add('MatthiasMullieMinify');
$minifier->add('Noop');