Side Channel

  • home
  • resources
Home › Blogs

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

helmi's blog

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
  • Add new comment
helmi's picture

Enhance performance by make use of browser parallel download

helmi — Tue, 04/08/2009 - 11:37

Enhance performance by make use of browser parallel download. Below is from a ticket for one of Byte Craft project:

Need more subdomains

To optimize browser parallel download of map tile images by add subdomains [ http://developer.yahoo.com/performance/rules.html "at least two but no more than four hostnames"]

Most browser have [ http://stevesouders.com/ua/report.php?v=top 60 max connections]

Example subdomains/[ http://trac.openlayers.org/wiki/OpenLayersOptimization multiple URLS] :
* t1.map.localhost
* t2.map.localhost
* t3.map.localhost
* t4.map.localhost

  • Add new comment
  • Read more
helmi's picture

Render road labels, MapGuide vs Mapnik

helmi — Thu, 11/06/2009 - 14:15

MapGuide Mapnik
  • Both software configured to tile 256x256
  • MapGuide bad at rendering road name.
  • gis
  • mapguide
  • mapnik
  • Add new comment
  • Read more
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
  • 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
  • 1 comment
  • Read more
helmi's picture

VMware Causing Keyboard Issues in Ubuntu

helmi — Fri, 15/08/2008 - 16:15

Previously to solve, I Log Out and then Log In again. But you better give this a try

  • open terminal
  • type: setxkbmap
  • put your cursor over the VMWare title bar, but not in the client window. Then Press Ctrl-g Ctrl-Alt. (This grabs and un-grabs the keyboard).

http://nthrbldyblg.blogspot.com/2008/06/vmware-and-fubar-keyboard-effect...

  • ubuntu
  • vmware
  • Add new comment
helmi's picture

Web 2.0 elements online generator

helmi — Fri, 25/07/2008 - 17:55

  • http://www.tabsgenerator.com/
  • http://www.reflectionmaker.com/
  • http://www.stripegenerator.com/
  • http://www.tartanmaker.com/
  • http://www.web20badges.com/

Web 2.0 Design Guide

So, try this online service before spent time using Gimp

  • web2.0
  • Add new comment
Syndicate content


Creative Commons License

  • home
  • resources

Standard Disclaimer.