Examples jcmin – A command line JS and CSS minimizer built on top of Google closure and YUI Compressor

G’day Fellas,

So today I am going to show you how to use jcmin A util that I wrote for myself. Lets consider what problems I faced before I decided to write this.

Most of the things you can do yourself if you are sticking with either of Google Closure or YUI compressor. I wrote this util so that I can switch between these beautiful compilers without any brain work. Both these compilers have their own positives and negatives. But if you operate these compilers at basic levels then there is not much difference between the two, and jcmin operate at that level.

The problems that I faced are listed below

  • What if I have a directory which is full of JS files and then subdirectories with more JS files. How do I minimize all those at once.
  • How do I minimize JS and CSS under a Directory with one command.
  • What If java is not available? off course I could have used online compressor but compressing 10s of files one by one is such a pain, and manageability becomes nightmare at least the way my project is setup. You could be smarter and use other tools
  • and so on…

Soluton: jcmin

I know that you may not agree using a wrapper but Its Ok. I am sharing what I’ve got so bad reviews or no acceptance doesn’t bother me. My motto is “Anything for a better world” It could be a crap solution but it works for me 🙂

 Advertisement

 

Lets explore this util with Problem solution scenarios

Problem 1: Minimizing javascript and CSS files under a directory and its subdirectories and create equivalent directory structure with minimized JS and CSS files

Below is the directory structure I will be working on

File sizes are shown below

 

As you see that my CSS folder has few CSS files and Demo directory has few javascript files and total size of all files is little more than 92KB

Now lets do a jcmin magic

I run the following command from my windows command prompt

So the process what successful but what does resulting c:\doutput folder looks like is shown below

 

So the result is an equivalent directory structure and .min added to all files and the files has been minimized. The overall size now is just over 56 KB.

The above is using Google Closure compiler now lets use YUI compressor. The results are shown below

 

As you see that I’ve used -dnam switch which means that program should not add “.min” to the output file names. As you can also see that the difference between sizes are not that great because this util work at safe levels. Only difference I can see is that YUI is a bit faster than Google Closure. I could be wrong but that the results of my tests.

 Advertisement

 

Problem 2: I’ve uninstalled JRE and also I would like to skip all files that has words forms and main in their names

Ok so let me tell you a good thing about jcmin. It ships with IKVM which is a java runtime written in .NET thus even if  JRE is not available this util will switch to fallback JRE which is IKVM. Read more about IKVM here

http://www.ikvm.net/

Ok so when I run above commands I see no difference they execute the way above screenshot tell you. But lets now explore how to exclude certain files not to be compressed and shouldn’t end up in output at all.

Going with above file structure which is from c:\demo directory the command below will exclude _ajforms.js and main.css from being compressed.

 

One thing I would like you to be aware of is that if Google Closure or YUI compressor throws any error you will see it right before your eyes. Errors are not suppressed at all below is an example. I intentionally removed closing bracket in one of the ruleset in my screen.css file below is what I get when I try to minimize that file.

YUI is very friendly so it will not raise any issues as such many times. But Google Closure is very sensitive and below is output to show how it complains

 

Depending on what sort of feedback I get I’ll add more to this post later. And off course I will add more features to this tool if I get good response.

Cheers

Jas Chahal

Download jcmin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.