Side Channel

  • home
  • resources
Home

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Books
  • Feed aggregator

Recent blog posts

  • Ubuntu mirrors up and improved!
  • Google-jstemplate: Iterate object with unknown properties
  • nginx https proxypass for php apps
  • sugarcrm & memcache: Doing it Wrong
  • subversion and https in Ubuntu Karmic
  • Drupal: Views block delta converted to md5 hash
  • Ubuntu Server install requires PAE
  • Installing Drupal from command line
  • Drupal: Handling form field weight through CCK
  • minify javascript using Google Closure Compiler
more

angch's location

angch twitter

  • angch: Heading to Brunei. Business class for a change.
more

javascript

helmi's picture

minify javascript using Google Closure Compiler

helmi — Wed, 11/11/2009 - 12:26

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 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 &quot;java&quot; 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(&quot;\n&quot;, $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.');
        }
    }
}

  • javascript
  • helmi's blog
  • Add new comment
helmi's picture

asynchronous javascripts load

helmi — Fri, 20/03/2009 - 11:53


if you want
better user experience
more revenue
reduced operating expenses
the strategy is clear
Even Faster Web Sites

- from slides Even Faster Web Sites

  • javascript
  • performance
  • helmi's blog
  • Add new comment
  • Read more
helmi's picture

IE innerHTML - "Unknown runtime error"

helmi — Fri, 19/09/2008 - 12:16

"Unknown runtime error" -- only in IE.

  • ie
  • javascript
  • helmi's blog
  • 1 comment
  • Read more
Syndicate content


Creative Commons License

  • home
  • resources

Standard Disclaimer.