Automate JavaScript compression with YUI Compressor and /packer/
Posted 2007-12-17 in JavaScript by Johann.
JavaScript compression should be one of the finishing touches to your website, like optimizing your images with OptiPNG and other automated optimizations. Shrinking the size of your JavaScript code will make your site load faster and your visitors happier.
In this blog entry, I will show you how to automatically compress your various JavaScript source files into one single compressed file. The tools I will use is the YUI Compressor and Dean Edward’s /packer/.
Prerequisites
- Create the following folders:
js
for the compressed JavaScript.js-in
for the input files.packer
for /packer/.yui
for YUI Compressor.
- Download YUI Compressor.
- Extract
yuicompressor-2.2.5.jar
to theyui
directory. - Download Rhino.
- Extract
js.jar
topacker
. - Download
packer.js
(originally written by David McNab) topacker
.
Create the batch file
Copy the following code to a file called pack.bat
and save it to your js
folder.
type ..\js-in\* > jb.js java -jar ..\yui\yuicompressor-2.2.5.jar -o jb-yui.js jb.js cd ..\packer java -jar js.jar packer.js ..\js\jb-yui.js ..\js\jb.js cd ..\js del jb-yui.js
(Replace jb
with the name of your site or something similar.)
Tips
- If you use multiple input files, you should name them like this:
10-jquery-1.2.1.js
20-jquery.inc-5.js
30-menu.js
2 comments
#1 2009-05-16 by Shigeru Takehara
Hi Jhoann,
I was trying to use the latest packer.js and visited their site. There is a download page, but there is not download for the latest version. Also there is no download for packer 2.0.1 javascript version.
Would you tell me how you get it?
Thanks,
Shigeru,
I found the link to the Rhino version here.
Today, I'm no longer using /packer/ though. Instead, I only use YUI and let lighttpd compress the file with gzip.
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/