How to minify and combine Javascript and CSS files with Coldfusion

Akitogo Team, 12 Apr 2019

The headline is a bit misleading, we should have written "...for your CFML projects". On the fly compression on the server doesn't make much sense. It leads to concurrency issues so better minify and combine your files upfront.

If you do a quick research on Google you wont find many projects supporting this. The only project which is up to date is Coldbox Elixir which uses Node and Webpack. It does more than simple compression and minification but installs a full Node server including many dependencies into your project. Node is just used for starting and processing compression but will not be deployed with your web project. We used 2.0 for a while but had some difficulties starting with 3.0. So finally we decided to develop something more native CF. That's how we started:

Commandbox minify

After you have installed Commandbox you simply write  box install commandbox-minfy . This will install a Commandbox module written in Coldfusion handling minification and compression tasks. It uses two libraries for that:

  • CSS minification and compression is done with a CFML port of YUIcompressor including a few fixes
  • JS minification and compression is done by a Java lib of Google Closure Compiler

If you want to see how it all works check out the source code on Github

How to use

box minifycss "path/to/css/files/*"  or  box mcss "path/to/css/files/*"  for compressing css files from a location

box minifjs "path/to/js/files/*"  or  box mjs "path/to/js/files/*"  for compressing js files from a location

Call this (if you want to use it with Coldbox) box minify from your project root directory. It will scan for all  Theme.cfc  and  ModuleConfig.cfc  files and sees if any contains  this.minify = { "nameItAsYouLike" : {}, "nameItAsYouLike2" : {} } Make sure that  this.minify  is a valid json.

Check out the Forgebox page for more information.

Thanks

Ben Nadel for writing an introduction on Google Closure Compiler.

Brad Wood for giving help and feedback on writing Commandbox modules