Minify_JS_ClosureCompiler::minify PHP Method

minify() public static method

Minify JavaScript code via HTTP request to a Closure Compiler API
public static minify ( string $js, array $options = [] ) : string
$js string input code
$options array Options passed to __construct(). @see __construct
return string
    public static function minify($js, array $options = array())
    {
        $obj = new self($options);
        return $obj->min($js);
    }

Usage Example

function test_Minify_JS_ClosureCompiler()
{
    global $thisDir;
    $src = "\n(function (window, undefined){\n    function addOne(input) {\n        return 1 + input;\n    }\n    window.addOne = addOne;\n    window.undefined = undefined;\n})(window);\n    ";
    $minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
    $minOutput = Minify_JS_ClosureCompiler::minify($src);
    if (false !== strpos($minOutput, 'Error(22): Too many compiles')) {
        echo "!NOTE: Too many recent calls to Closure Compiler API to test.\n";
        return;
    }
    $passed = assertTrue($minExpected == $minOutput, 'Minify_JS_ClosureCompiler : Overall');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Output: " . countBytes($minOutput) . " bytes\n\n{$minOutput}\n\n";
        echo "---Expected: " . countBytes($minExpected) . " bytes\n\n{$minExpected}\n\n";
        echo "---Source: " . countBytes($src) . " bytes\n\n{$src}\n\n\n";
    }
    $src = "function blah({ return 'blah';} ";
    $exc = null;
    try {
        $minOutput = Minify_JS_ClosureCompiler::minify($src);
    } catch (Exception $e) {
        $exc = $e;
    }
    $passed = assertTrue($exc instanceof Minify_JS_ClosureCompiler_Exception, 'Minify_JS_ClosureCompiler : Throws Minify_JS_ClosureCompiler_Exception');
    if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
        echo "\n---Message: " . var_export($exc->getMessage(), 1) . "\n\n\n";
    }
}
All Usage Examples Of Minify_JS_ClosureCompiler::minify