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:
- Type
java -jar(type a space after) and then drag theyuicompressor.x.x.x.jarfile 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. - Type another space and drag the file you want to compress into the window.
- Another space then type
-oto specify that you want to write the compressed output into a new file. - 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.
- 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.
Comments
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!
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!
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/
Very cool Mike, I was looking for an online version and could never find one. Thanks!
Mike thanks for the link of online version and Chris, thanks.
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?
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.
@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.
Hi all,
I recently tried to use the YUI Compressor and came accross this online version. http://yui.2clics.net/
Thanks
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!