minify javascript using Google Closure Compiler

Optimize javascript using minify
"It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers."
Example of interesting urls:
* http://dev.bigproject/min/?f=static/map.js
* http://dev.bigproject/min/?b=static&f=map.js,taskpane.js,OpenLayers-2.8.js
* http://dev.bigproject/min/?g=js
Few challenges during setup
* 400 Bad Request -- check your path in config.php, especially $min_documentRoot
* watch for symbolic link for js/css files - you may need to update config $min_symlinks
In Minify Cookbook, it say YUICompressor = "Best Javascript Compression", but from my random test, Google Closure Compiler is better.
I created a php code for Minify Google Closure (80% copy paste from existing YUICompressor php code). To setup
function googleJs($js) {
require_once 'lib/Minify/GoogleClosure.php';
Minify_GoogleClosure::$jarFile = dirname(__FILE__) . '/lib/compiler.jar';
Minify_GoogleClosure::$tempDir = '/tmp';
return Minify_GoogleClosure::minifyJs($js);
}
$min_serveOptions['minifiers']['application/x-javascript'] = 'googleJs';
Since have problem attach the source code, so below is the code.
<?php
/**
* Class Minify_GoogleClosure
* @package Minify
*/
/**
* Compress Javascript/CSS using the GoogleClosure
*
* You must set $jarFile and $tempDir before calling the minify functions.
* Also, depending on your shell's environment, you may need to specify
* the full path to java in $javaExecutable or use putenv() to setup the
* Java environment.
*
*
* @package Minify
*/
class Minify_GoogleClosure {
/**
* Filepath of the Google Closure jar file. This must be set before
* calling minifyJs() or minifyCss().
*
* @var string
*/
public static $jarFile = null;
/**
* Writable temp directory. This must be set before calling minifyJs()
* or minifyCss().
*
* @var string
*/
public static $tempDir = null;
/**
* Filepath of "java" executable (may be needed if not in shell's PATH)
*
* @var string
*/
public static $javaExecutable = 'java';
/**
* Minify a Javascript string
*
* @param string $js
*
* @param array $options (verbose is ignored)
*
* @return string
*/
public static function minifyJs($js, $options = array())
{
return self::_minify('js', $js, $options);
}
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options (verbose is ignored)
*
* @return string
*/
public static function minifyCss($css, $options = array())
{
return self::_minify('css', $css, $options);
}
private static function _minify($type, $content, $options)
{
self::_prepare();
if (! ($tmpFile = tempnam(self::$tempDir, 'googlec_'))) {
throw new Exception('Minify_GoogleClosure : could not create temp file.');
}
file_put_contents($tmpFile, $content);
exec(self::_getCmd($options, $type, $tmpFile), $output);
unlink($tmpFile);
return implode("\n", $output);
}
private static function _getCmd($userOptions, $type, $tmpFile)
{
$cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile);
return $cmd . ' --js ' . escapeshellarg($tmpFile);
}
private static function _prepare()
{
if (! is_file(self::$jarFile)
|| ! is_dir(self::$tempDir)
|| ! is_writable(self::$tempDir)
) {
throw new Exception('Minify_GoogleClosure : $jarFile and $tempDir must be set.');
}
}
}
- Tags:

