I created this script in the middle of the last project - Blogger Post Calendar. I wanted to speed up the loading of the external javascript file - so I created a script that would automatically compress javascript code. And perl was the obvious choice as its language.
The code can be downloaded from the JavaScript Source Compressor page.
Features
- Removes Comments - both
//...
and/* ... */
style comments. - Removes Whitespaces - newlines, spaces and tabs.
- Replaces all varable names with a shorter version(a,b,...,z,aa,ab,...). This feature is optional
perl SourceCompressor.pl --help
SourceCompressor is a perl script that can be used to compress the Javascript source file to a much smaller file. Replaces large variable names with smaller, generic names and removes unwanted white spaces.
Usage
perl compressor.pl <js_file.js> [options] Example : perl compressor.pl settings.js -display -verbose -change-name Command Line Options The first argument MUST be the file name of the JavaScript file. -verbose Show details of what is going on. -readable Use \\n instead of ';' as the command seperator. -change-name Rename the variables to a shorter version. Enabled by default. -remove-vars Remove the 'var' keyword - this is asking for trouble. -print Outputs the compressed data instead of writing it to a file.
To Do
- BUG: Changes the text if the variable name appears in the coments/strings/other places.
- The variables appearing more times must have shorter number of chars.
- Change the variables that don't use the 'var' decleration method. - Is this possible?
- Implement Unremoveable comments - needed for copyright statements.
- Change the names of the functions/classes too?