Minify & merge assets

The plugin embeds a Compressor object to merge and minify JS and CSS contents. The result files are stored on a the "temporary assets directory" (a configuration variable) and can be used "as-is" in HTML contents.

Please see the note below for an information (and a warning) about the minification processes implemented by the plugin.

Merging assets

Creation of an \AssetsManager\Compressor\Compressor object:

 1 $compressor = new AssetsManager\Compressor\Compressor(
 2     // the files to treat
 3     $js_stack,
 4     // the result filename - null will cause automatic generation
 5     null,
 6     // the result file path
 7     __DIR__."/../tmp",
 8     // the file type - will be guessed if null
 9     "js"
10 );
11 $compressor->process();

We can ask the compressor in which file it has generate its merge:

1 $minified_filename = $compressor->getDestinationRealPath();
2 // => /absolute/path/to/tmp/d1811861a0e25d97d8285e32cade0ff6_merge.js

We can get a ready-to-use file path of the generated file defining the web root:

1 $compressor->setWebRootPath( $loader->getDocumentRoot() );
2 $minified_webpath = $compressor->getDestinationWebPath();
3 // => tmp/d1811861a0e25d97d8285e32cade0ff6_merge.js

Minifying assets

Ask the minifier to process our files stack:

1 $compressor
2     ->setAdapterAction("minify")
3     ->process();

Ask the compressor in which file it has generate minify our files:

1 $minified_filename = $compressor->getDestinationRealPath();
2 // => /absolute/path/to/tmp/d1811861a0e25d97d8285e32cade0ff6_minify.js

We can get a ready-to-use file path of the generated file defining the web root:

1 $compressor->setWebRootPath( $loader->getDocumentRoot() );
2 $minified_webpath = $compressor->getDestinationWebPath();
3 // => tmp/d1811861a0e25d97d8285e32cade0ff6_minify.js

Note

Please be aware that the minification process defined in the plugin is "simple" and "custom". It does NOT use any external tool to optimize assets contents. For a better rendering, you MAY use an external tool.

 Found a typo on this page ? You can fork and edit it!