Skip to content

JavaScript Compression

1 min read

As I was re-rolling my JS aggregation patch for Drupal, I stumbled upon an interesting solution to an age old problem I’ve encountered numerous times.

When I write various JavaScript files for websites, I often like to serve them out compressed—not only to save on bandwidth but also to speed up the loading time for users. To accomplish the task of compressing JavaScript files, I usually resort to using tools like Dojo ShrinkSafe and Dean Edward’s packer(the same compression that jQuery uses as well).

The basis of these compressors is to remove unneeded whitespace, optional characters, and in the case of packer, simplify variable names and function names for maximum compression.

The downside of this, is that your JavaScript must be well-formed, otherwise you’ll get all sorts of weird errors when you try to used the compressed version.

While I have had great success with these tools, I often find myself encountering an error or two I just can’t resolve, so I end up sticking with the non-compressed JavaScript file. And while working on the patch for Drupal, I was running into the same issue. Why didn’t some of Drupal’s JavaScript files want to compress nicely?

Well, it turns out the solution is simple and involves adding in an optional semi-colon. Here is the example:

var foo = function bar() {
  // do something here
};

See it? That last semi-colon there is optional and any JavaScript will run fine without. But as soon as you go and try and compress your JavaScript file, you’ll be seeing some funky errors about a missing “;”.

The reason for this is that during the compression, all of the JavaScript is merged onto one line. As such, variables need a closing semi-colon to signify the end of their declaration.

Adding these back in to all of Drupal’s JavaScript and re-running the compressor, everything started working :-)

Hope this saves you some head banging time, enjoy!

codeDrupal

Related Posts

MothersClick Acquired by Lifetime Networks

MothersClick.com [http://www.mothersclick.com/] and the rest of the ParentsClick Network [http://www.parentsclick.com/]have been acquired by Lifetime Entertainment [http://www.marketwatch.com/news/story/l/story.aspx?guid=%7B764B126C-2071-4605-8222-7FA1E481B0EE%7D] . I’ve been working on this site and with Dietrich (CEO & founder) for over 2

news — 5 min read

Leo talks about the new TWiT.tv site

At the end of last week’s TWiT episode [http://www.twit.tv/62], Leo and company talk about the new TWiT.tv site [https://tedserbinski.com/drupal/twit-tv/]. A clip of that can be listened to below. Also last week on the Lullabot podcast [http://www.lullabot.com/podcast]

code — 1 min read

My first 15 min of fame!

A few weeks back, Leo Laporte [http://en.wikipedia.org/wiki/Leo_Laporte] approached Lullabot [http://www.lullabot.com/] about developing the new TWiT website [http://www.twit.tv/]. Well we gladly accepted, I mean, this is Leo! So I was put in charge of developing the new site, and

code — 1 min read