CFBuilder Contest: Squeezer

This post is more than 2 years old.

Welcome to the first entry in the ColdFusion Builder Contest. We didn't get a lot of entries for this contest, but every entry is really darn cool, so I'm very excited to be able to share the first of the entries with you today. Our first entry is Squeezer and comes to us from Guust Nieuwenhuis.

Squeezer is an extension that provides support for optimization/compression/minification of JavaScript and CSS files. One of the things I learned in Peter Farrell's client side optimization class is that these techniques are not considered to be as effective as they once were. (And please forgive me if I got that detail wrong!) The point Peter made was that the thought was that the benefit you gained was not outweighed by the possible difficulty you have in debugging. That being said, I do think people still have a need for tools like this - perhaps as part of a deployment script (as suggested by Lance Staples, one of my coworkers). If you do need this type of tool though, Squeezer makes it as easy as "right click, Squeeze", which is about as easy as you can get it.

After installing Squeezer, you will have a new right click menu on JavaScript and CSS files. This is achieved by using filters in the IDE configuration file. CFBuilder extensions can be set to only work on specific files/folders/names. While you can check for this on the code side, it is much better to simply filter out from even showing up to the user. One big bug here - the filter will allow you to select multiple files. If you do this, things will appear to work but the compression never fires. As far as I know, you can't specify that a menu will only show up on one selected item. In fact, if you begin your file selection on a valid file, you can select additional files that do not match the filter. This is not a bug in his code, but rather a limitation of CFBuilder. (I'll go file a bug report on this later today.) What I'd probably recommend is a) writing your extension to correctly notice N files instead of just one and b) double checking to ensure the extension matches what you want. By the way - you will probably see me repeat this advice over all the contest entries. It's something I know that my own extensions need to do better as well.

Once installed, you have the option of running Google's Closure Compiler or Yahoo's Compressor. Both of these can be used on JS files. While the Yahoo tool focuses on compression, Google's tool actually tries to rewrite your JavaScript for optimization. The Google Closure Compiler never worked for me, though. It changed my files into: com.google.javascript.jscomp.Compiler@faf3284. It goes without saying - backup before you test this. I had much better luck though with the Yahoo version.

As you can see, you have many options for how to process the files. To be honest, I'm not sure what some of these are, but I love the fact that you have the option to Save As. I mainly just took the defaults though and let it overwrite the file as is.

The results - again when it worked right and when I used it files that were not already minimized - were respectable. A good 50% cut on some files.

As to the code - first and foremost - he remembered the cardinal rule of CFBuilder extensions - disable debugging:

<cffunction name="onRequestStart" returnRequest="boolean" output="false"> <cfsetting showdebugoutput="false" enablecfoutputonly="false" /> </cffunction>

I'm not quite sure what returnRequest is meant to be, but the important thing is he turned off debugging. Do not forget this.

Another interesting aspect is how he handles storing the data. One issue you run into with CFBuilder extensions is how to properly keep state over multiple requests. You can kinda use the Session scope at times but it is a bit wonky. He gets around this by simply using the Application scope. He stores his information in the Application scope under a numeric ID. He then passes that value over the URL from step to step so he can get it again later. This is a technique I kind of like and I may "innovate" into my own extensions.

Another nice feature - he makes use of JavaLoader, Mark Mandel's open source project that lets you make use of Java libraries without having to modify your class path or restart ColdFusion. Luckily this feature will get rolled into ColdFusion 10. (Because Adobe has said they will be integrating all of Mark's open source projects into ColdFusion. Yes, that's a joke. Kinda.)

All in all - a very handy little extension. You can grab it yourself via the download link below. (Edit: I've removed the download as it is now available at http://squeezer.riaforge.org/.) Thank you, Guust! (Quick reminder - don't forget that you should not extract the zip. CFBuilder will do it as part of the installation.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Sam Farmer posted on 5/4/2010 at 8:44 PM

Sounds very cool and useful. Certainly more useful than the directory watcher via event gateways version I once worked on and never got to really work!

Can it compress multiple files into one file? Is that even a good idea?

Comment 2 by Guust Nieuwenhuis posted on 5/4/2010 at 8:57 PM

Thanks for the contest and the review Ray!

@Sam, it can't handle merging multiple files. But that could be a feature for the next version :D

Comment 3 by Raymond Camden posted on 5/4/2010 at 9:25 PM

As an FYI, Guust's project is now on RIAForge:

http://squeezer.riaforge.org

Comment 4 by Ryan Vikander posted on 5/4/2010 at 10:47 PM

I installed the extension and when I right click on a js file it pops up a window with nothing inside. Am I missing something?

Comment 5 by Raymond Camden posted on 5/4/2010 at 10:49 PM

I'll let Guust debug with you. Sounds like maybe an error someplace though.

Comment 6 by Guust Nieuwenhuis posted on 5/5/2010 at 11:49 AM

@Ryan: I'm looking into it...

Comment 7 by Guust Nieuwenhuis posted on 5/5/2010 at 1:11 PM

I've put version 1.0.1 on RiaForge (http://squeezer.riaforge.org)

@ryan: your issue should be fixed in v1.0.1
@ray: can you update the attached file?

Comment 8 by Guust Nieuwenhuis posted on 5/5/2010 at 2:00 PM

Ah, forgot to say that I've also fixed the issue reported by Ray that compiling with the Closure Compiler didn't work.

Comment 9 by Raymond Camden posted on 5/5/2010 at 3:12 PM

Removed the attachment.

Comment 10 by Edy Ionescu posted on 5/5/2010 at 11:09 PM

While the extension works as described behind the scenes, the response is a bit unexpected http://i41.tinypic.com/35d3....
I installed the extension both from archive file and from folder. The behavior is the same. Also, I'm very curious to see if the 'refreshproject' command actually works in this case as my previous attempts had only left me kinda furious.

Slightly off-topic: is there a way to bind keyboard shortcuts to the action tags specified in ide_config.xml? This would be very useful on a frequently called action.

Congratulations Guust for your second RIAForge entry this year! Keep'em coming!

Greetings from Romania

Comment 11 by Guust Nieuwenhuis posted on 5/5/2010 at 11:40 PM

@Edy: The unexpected response is something I've noticed as well this afternoon at work (Windows XP, Firefox as default browser), it's an issue I don't have at home (Mac, Safari as default browser) but I'll try to fix this as fast as possible. The 'refreshproject' behaves a bit strange... I did got it to work at some point but I don't think is does in the latest version, but I didn't change the code. Some more investigation is needed.