Compress JavaScripts in Window Explorer

If you worked with JavaScript libraries before, you’ll probably have noticed that most libraries contain a compress file as well. One is called my.library.js while the compressed version is usually called my.library.min.js. This is not a must but when you have a frequently visited site you should consider to compress your JavaScript files. To make it easier and quicker to do this, I’ve added a small shell extension with which you can compress your JavaScript files with pretty much no effort.

Installing YUI Compressor

Yahoo has created an often and well proved compressor which we are going to use. Download the latest version from this page: http://yuilibrary.com/download/yuicompressor/. If you extract the file, you’ll see a bunch of directories and files, but only the one in the build directory is actually required. Move the yuicompressor-x.y.z.jar into a directory of your choice. I used C:\Program Files\YuiCompressor as I thought it’s kind of an application and belongs in the program files, but feel free to use any other directory.

Creating Batch File to call Yui Compressor

We are going to create a small little wrapper which will call the YUI compressor. For this, create a new file called compress.bat in the same directory. Mine looks like this:

In this file, add the following content:

1
java -jar "%~dp0\yuicompressor-2.4.7.jar" %1 -o %~n1.min%~x1

Make sure you are using the correct version (2.4.7 at the time of writing). As you can also see, the YUI Compressor uses Java. If it’s not already installed, install either the Java JRE or Java SDK http://java.com/en/download/index.jsp.

Adding Shell Extension

We need some new registry entries to call our wrapper directly from the Windows explorer. Hit “Windows + R” and enter “regedit” to open the registry editor. Please note: Changes in the registry editor have to be done very carefully! Doing the wrong changes in the registry can break your Windows installation!

In the left pane, select HKEY_CLASSES_ROOT\.js and look for the value in the right pane. The value we need is JSFile in the following screenshot:

In HKEY_CLASSES_ROOT we have to find the value we just found. In that folder you should find another key called “shell”.

  1. Right click on “shell”
  2. Select “New – Key”
  3. Enter “compress” and confirm it
  4. Select the newly created key
  5. Open the default value on the right and enter “Compress”
  6. Right click on the new folder “compress” in the right panel and select “New – Key”
  7. Enter “Command” and enter the following value: “”C:\Program Files\YuiCompressor\compress.bat” “%1″”

Adjust the path in the last step according to the directory where you installed the YUI compressor.
If you’re done, you should have the following structure:

When you now go to your Windows explorer and right click a file with the extension .js you’ll see a new entry called “Compress”. Click on it and you’ll find a new, compressed, JavaScript in the same directory:

No more excuses not to compress your JavaScript files!




2 Comments

A registry file won’t always work. If the default value of “.JS” is different on your system, it would override other settings, you’ve made. I’m not eager to answer all those support requests.. Just follow the instructions, it works on Win7 x64 too and doesn’t take much time..

Leave a Reply

Your email address will not be published. Required fields are marked *