How to use the YUI compressor

Saturday, December 13, 2008

Compressing your javascript and CSS is a easy way to save bandwidth and make a site’s download and performance feel a bit snappier. There’s lots of tools to allow you to do this, but from my research it seems that Yahoo!‘s YUI compressor is the overall best.  Sure there’s others out there like JSmin and Dean Edward’s packer, but YUI offers a higher compression ratio, it also works with CSS files, and it’ won’t obfuscate your code.

Compressors work by removing all comments and whitespace (line breaks), and in some cases they will actually analyze your code and shrink variable names.  Using compression can result in up to a 60% decrease in file size!  So lets get started in learning how to use the YUI compressor!

First, it’s probably a good idea to read about YUI, so check out the video introducing it at http://developer.yahoo.com/yui/compressor/

Now, lets go and download the latest version: http://www.julienlecomte.net/yuicompressor/ - also since this runs on Java, make sure you have Java installed and updated.

Open up the folder that was downloaded and grab the file in the “build” folder named yuicompressor-x.x.x.jar and you can delete everything else since we won’t need it just to use the compressor.

For this example I’m going to compress jQuery and jQuery UI, first go download them and be sure to get the uncompressed version of each, since we can’t really further compress an already compressed file.  Just open these two files, select all of jQuery UI and copy/paste that to the end of the jQuery file.  The resulting uncompressed file is 436k and over 13,000 lines of code!

If you’re using a Mac, you’ll need to open up the terminal (found by navigating to /Applications/Utilities/Terminal.app), and if you’re using Windows you’ll need to open the command line (Start > Run > type “cmd” and hit enter).

Now in the Terminal/Command Line here’s the syntax for basic compressing with YUI:

java -jar /path/to/yuicompressor.jar path/to/original.js -o path/to/output.js

Instead of typing all these file paths, luckily we can just drag files onto the terminal/Command line and it will write the path out for us.  So here’s what you need to do:

  1. Type java -jar (type a space after) and then drag the yuicompressor.x.x.x.jar file into the window - this tells it that we want to run java code from a .jar file, and then we give it the file to run.
  2. Type another space and drag the file you want to compress into the window.
  3. Another space then type -o to specify that you want to write the compressed output into a new file.
  4. Now you need the path to the output destination file that does not yet exist. So what I usually do is drag the same file into the window, and then use the arrow keys to go back a few characters and rename the file.
  5. Just hit enter and that should do it!

As you can see, we went from 436k down to 264k - which is a 60.5% decrease! Also, it now has no comments and it’s all on a single line of code which makes it next to impossible to read, so be sure to keep a backup.  This is especially true if you are compressing a script that you wrote, if you don’t keep a backup you will forever loose your comments and formatting.

Posted by Chris Barr on 12/13 at 11:52 PM
Filed under General, Web, Code, Javascript
Comments
1
Adrian Rodriguez commented on Dec 15, 2008 at 10:26 pm

First of all, how did you get the background color of your terminal changed? Second of all, I just learned how to access the terminal today, third of all, I don’t get much of this post, but I guarantee that if you post more stuff about php, you’ll most likely get me to read some more. Although you don’t post often. Peace Bro!

2
Chris Barr commented on Dec 16, 2008 at 08:06 am

Open Terminal > Preferences > Settings and there’s lots of color and transparency options there.

What didn’t you understand here? I guess some command line stuff can be confusing if you haven’t messed with it before.

Next time you work on a site, use this and give it a shot. Remember it works with CSS files as well!

3
Mike commented on Jan 20, 2009 at 01:34 pm

If, like me, you don’t feel comfortable in terminal, you can use one of several online interfaces for the YUI Compressor.

Here’s one: http://refresh-sf.com/yui/

4
Chris Barr commented on Jan 20, 2009 at 05:57 pm

Very cool Mike, I was looking for an online version and could never find one.  Thanks!

5
jQuery Howto commented on Feb 18, 2009 at 06:03 am

Mike thanks for the link of online version and Chris, thanks.

6
.(JavaScript must be enabled to view this email address) commented on Jul 16, 2009 at 03:15 am

This is a joke right? I mean it has been 20 YEARS since I had to run programs like this.

Yahoo cannot produce a front end to run this app.? That’s embarrassing. What kind of programmers have they got?

7
.(JavaScript must be enabled to view this email address) commented on Jul 20, 2009 at 02:24 pm

Paul, there’s *plenty* of front ends to this tool. Search the web a little, or just look at these comments.

Additionally, a text-based backend is very sensible. I’ve got my web server to compress the JS on the fly, gzip it and cache it, then serve. This lets me roll out new JS with ease, and not worry about tracking multiple JS files in my DVCS.

8
.(JavaScript must be enabled to view this email address) commented on Jul 29, 2009 at 03:35 am

@Paul

Surely it you who are joking, this is designed to be run as an automated task as part of the build process.

It’s embarrassing that you think everyone needs a GUI interface.

9
Ian Crowther commented on Sep 16, 2009 at 09:43 am

Hi all,

I recently tried to use the YUI Compressor and came accross this online version.  http://yui.2clics.net/

Thanks

10
.(JavaScript must be enabled to view this email address) commented on Jul 14, 2010 at 05:35 pm

Thanks so much for this, Chris!  I’m a beginner writing a shell script to automatically minify our JS and CSS every time we do a sync and push live, and nowhere on the YUI Compressor website does it say that we only need to extract the .jar file.  Your post helped a lot!

Post a Comment