Monday, August 15th, 2005

Dojo Compressor Toolkit

Category: Dojo, Utility

Yeah, yeah. So you have written your own script which takes JavaScript and munges it to compress it. How do you grok the JavaScript? Some cheeky regex’s that are very brittle?

This is where Dojo’s Compressor differs from our little hacks:

So how does this all work? And why should anyone choose this tool over the other ones that are available. The answer to both of those questions is in the design of the the Dojo compressor. Instead of brittle regular expressions, the Dojo compressor is based on Rhino, a JavaScript engine from the Mozilla project.

Being based on a real parse stream, the Dojo compressor can get a better idea for the context of a token (variable name, etc.) than the regular-expression based tools. This allows us to achieve the over-riding goal of a compressor that would be acceptable to the Dojo project: it must never mangle a public API.

A simple:

java -jar custom_rhino.jar -c infile.js > outfile.js 2>&1

will take:

function MyClass(){
    this.foo = function(argument1, argument2){
        var addedArgs = parseInt(argument1)+parseInt(argument2);
        return addedArgs;
    }

    var anonymousInnerFunction = function(){
        // do stuff here!
    }
}

function MyFunc(){
    // this is a top-level function
}



// we've got multiple lines of whitespace here

and will generate:

function MyClass(){
this.foo=function(_1,_2){
var _3=parseInt(_1)+parseInt(_2);
return _3;
};
var _4=function(){
};
}
function MyFunc(){
}

Check out the Dojo Compressor

Posted by Dion Almaer at 1:34 am
4 Comments

++---
2.7 rating from 6 votes

4 Comments »

Comments feed

Thank you for the post but PLEASE consider changing your stylesheet! Reading the grey on grey code examples is ridiculously hard.

Comment by Mat Seeley — August 16, 2005

Does anyone know the command line argument for stripping newlines like the online ShrinkSafe form? When run -h I got (not even the -c is listed, somebody might want to update the usage page)
$# java -jar custom_rhino.jar -h
Didn’t understand “-h”.
Valid arguments are:
-w
-version 100|110|120|130|140|150
-opt [-1|0-9]
-f script-filename
-e script-source

Comment by Son Nguyen — January 30, 2006

After running custom_rhino.jar and if you have Linux system, simple do:

tr -d ‘\n’ fileCompressWithOutNewLine.js

regards.
Abelardo

Comment by Abelardo Perez. — October 27, 2006

The display of my before post, is not correct, is not accepting some signs, please check the man pages for tr for correct use.

Comment by Abelardo Perez. — October 27, 2006

Leave a comment

You must be logged in to post a comment.