MitrahSoft is an Adobe offcial partner MitrahSoft is Lucee offcial partner & core contributing developer

Minify JS And CSS Files Using ColdFusion And YUI Library

All web applications has CSS and JS files. But, now a days most of the developers wants to include only minified JS and CSS files. Because, normal JS and CSS files having unnecessary characters in source codes. These unnecessary characters usually include white space characters, new line characters, comments, and sometimes block delimiters. These are very important only for code readability, but not needed for execution process. These unnecessary characters increase file size and it will be affect performance too. For this reason, the minification (also minimisation or minimization) is very important.

We have lot of JS and CSS minification online convertor and have lot of JAVA libraries available in internet. But, best one is, YUI Library ( Yahoo userInterface Library ). Because, it is very lightweight core library and also it is a free open source Library.

Minified JS & CSS files using YUI library

The YUI - Yahoo userInterface Library Compressor is JavaScript minifier designed to be 100% safe and yield a higher compression ratio than most other tools.The YUI Compressor is also able to compress CSS files by using a port of Isaac Schlueter's regular-expression-based CSS minifier.

Prerequisites
  1. YUI Compressor requires 1.5 and above Java version.
  2. Version of YUI compressor library 2.4.8
  3. Download 2.4.8 version from this link ( https://github.com/yui/yuicompressor/releases )
Application.cfc

In this file, First need to include YUI jar file. In the below sample code, I have paste my downloaded YUI jar file inside of the lib folder.

So, using this.settings I have included my jar file into my application. And As usaual, I have created an object for 2 CFC files. Inside of those files, I wrote a code for minified process.

YUICompressor.cfc

In this file, I wrote two functions such as jsCompressor(), cssCompressor(). In the cssCompressor() function, I wrote CSS file minification process code and in the another function wrote a JS file minification process code.

In side of this function, I created an object for ErrorReporter.cfc file. This is a required core file. So, Please don't forgot to include this CFC file on the particular directory. I just read all the JS and CSS files from /assets/js & /assets/css directory and then created some wonderful Java objects like StringBuilder,StringReader,StringWriter. These all are used for minified the CSS & JS files.

For compressing JS files need to call the JavaScriptCompressor, com.yahoo.platform.yui.compressor.JavaScriptCompressor For compressing CSS files need to call the CssCompressor com.yahoo.platform.yui.compressor.CssCompressor