Related

SF Project Page

SF Downloads

Mozilla SpiderMonkey

jscompact - A command-line utility to compact and obfuscate javascript code.

jscompact is free software licensed under the GNU GPL. It is released "as-is" and without any support from the author. Patches, improvements and bug reports may be submitted via the Sourceforge Project page.

jscompact is a text-mode program designed to compact javascript code and thereby speed download times. It does this primarily by removing whitespace, and by changing variable names within functions to single character values, eg 'mybiglongvariablename' becomes 'a'. For sites with large external javascript libraries, this can be a big win, especially for users with a slow connection. The utility is handy for compacting (and to a degree obfuscating) javascript immediately prior to releasing it into production. No specific size reduction claims are made. Your mileage will vary based on the input code.

jscompact uses libjs (spidermonkey) from the mozilla project, and thus fully understands the javascript and is able to do a better job than compactors built using regular expressions or similar kludges. This program is written in C, and should compile/run on any platform that mozilla will compile on. However, at this time jscompact will not build on Windows without some patches to the libjs source code. So it is recommended to use it on a unix-like OS.

jscompact cannot process javascript embedded within an HTML file. It expects to be given javascript as input, and spits javascript back out as output.

This example page contains a simplistic and probably buggy calculator that references a compacted javascript file. For comparison, you can view the javascript files below.

Usage:

  ./jscompact [options]

Options:

 -h       --help              display this usage help
 -p       --pretty            pretty print code without comments
 -a       --argvarsoff        disable auto-shortening of function arguments
 -l       --localvarsoff      disable auto-shortening of local scope variables
 -n       --newlines          keep newlines when compacting
 -c       --checkonly         Syntax check only.  Do not print script
 -i       --infile=file       Input file.  Uses STDIN if absent
 -o       --outfile=file      Output file.  Used STDOUT if absent