Automate JavaScript compression with YUI Compressor and /packer/

Posted 2007-12-17 in JavaScript by Johann.

/packer/ output

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

  1. Create the following folders:
    • js for the compressed JavaScript.
    • js-in for the input files.
    • packer for /packer/.
    • yui for YUI Compressor.
  2. Download YUI Compressor.
  3. Extract yuicompressor-2.2.5.jar to the yui directory.
  4. Download Rhino.
  5. Extract js.jar to packer.
  6. Download packer.js (originally written by David McNab) to packer.

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
    This ensures that your files are compressed in the right order.

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,

#2 2009-05-17 by Johann

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

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation