JS_Squeeze
JS_Squeeze compresses and obfuscates Javascript documents for your web site to increase your sites performance and protect your intellectual property.
Features:
- Concatenates multiple Javascript documents into a single document to reduce HTTP requests.
- Compresses and obfuscates script content by removing unrequired white space and comments to reduce file size (using Dean Edward’s ‘Packer‘ class, ported to PHP by Nicholas Martin).
- Compresses the HTTP transfer via GZIP if available on the server.
- Optimizes client caching to reduce HTTP requests. One ‘squeezed’ script can contain all Javascript functions for an entire site and does not require refreshing.
- Caches parser output on the server to reduce server processing time.
License
You are free to download, modify and distribute this script for personal or commercial purposes under a modified MIT License provided that all original copyright and attribution information is distributed with the software.
Installation
- Place your sites Javascript documents in a single folder on a PHP server (typically ‘/public_html/js’).
- Copy javascript.php, class.JSSQueeze.php and class.JavaScriptPacker.php into your scripts folder.
- Edit the class.JSSqueeze.php file and set $debug_secret for secure debugging (only required for debug mode).
- Link the script to your HTML documents header with the following declaration:
<script type="text/javascript" src="js/javascript.php"></script>
Usage
JS_Squeeze will concatenate and pack all Javascript documents in it’s own folder. Documents are parsed in alphanumerical order. It is important to load scripts in the correct order as some scripts are dependent on others and the dependencies need to be parsed first. This can be achieved by prepending your script file names with a number.
For example, rename your jQuery or similar library script to ‘00.jquery-1.3.2.js‘, plugins to ‘10.jquery.pluginname.js‘ and your ‘$(document).ready()‘ or page related scripts can be upwards of ‘20.myscript.js’.
Debug
You can enable debug information on the scripts output which will prevent scripts from being obfuscated and print additional debug information in Javascript comments.
This allows you to debug your scripts with human readable data before publishing obfuscated data. For this reason, you can use the ‘developer’ versions (ie. Not minified or packed) of your 3rd party scripts for design-time debugging, but the production output will still be compressed and obfuscated.
To enable debugging, simply append ‘?debug=secret‘ to the script url (where secret is the value you set for $debug_secret) as follows:
<script type="text/javascript" src="js/javascript.php?debug=password"></script>
The script creates ‘.cache_[hash key].js.tmp‘ files containing the cached output of the parser for each unique browser version that requests a script. If required, these can be cleared with a URL request ‘…/javascript.php?flushCache=true‘. All cache files are set to expire and be deleted automatically after ‘$cache_expiry‘ seconds since their last access time. Changes to any Javascript file will be reflected immediately in the parser output due to smart hashing of the script files.
Features in development
- Document priority tags to remove the need for files to be alphanumerically named
