7.2. Minifier

7.2. Minifier

Minifier allows you to minify javascript sourcecode into a compact distributable form. It is a PHP5 port of JSMin by Douglas Crockford. “ JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files. It typically reduces filesize by half, resulting in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate self-documentation.

[Note] Note

The performance of this minifier is not very good compared to the C version; however, it works fairly well for small scripts and cached implementations.

Example 7.2. Minify a javascript string

<?php
 
// Object style
$minifier = new Zym_Js_Minifier();
$js = $minifier->process('function() { alert(); }');
 
// Static
$js = Zym_Js_Minifier::minify('function() {alert();}');

Example 7.3. Minify and return code from javascript file

<?php
$js = Zym_Js_Minifier::minifyFromFile('dojo.js');

Example 7.4. Minify a javascript file

<?php
Zym_Js_Minifier::minifyFile('dojo.js', 'dojo.minified.js');