ted serbinski – a blog about drupal, macs, productivity, health, and bmws

a blog about drupal, macs, productivity, health, and bmws

JavaScript Compression

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!

4 comments

 
Anonymous (not verified) wrote 1 year 34 weeks ago

Blumin useful tip Ted, thanks!

Now, whats 7 + 10? ermmm

 
S Jain (not verified) wrote 1 year 34 weeks ago

I recently came across a better solution for compressing CSS and JS but it only seems to be working on CSS for now.

The mod_gzip module for Apache was last updated in 2002. Back then, Netscape 4 had problems with gziped CSS files, i guess Javascript files too; so by deafult (at least on WHM/Cpanel configs) CSS and Javascript are not compressed by mod_gzip module.

I enabled them in my apache config files and now CSS files are compressed automatically. My CSS was compressed nearly 80%. Even after combining all the CSS in 1 file as in Drupal 5.

But I couldn’t get it to work for JS.

You can test the Gzip compression by inputing the URL here http://www.gidnetwork.com/tools/gzip-test.php … You can even test CSS o JS files.

 
ted wrote 1 year 33 weeks ago

Yes, GZIP compression is the next logical step. Right now we aggregate everything into one file with some basic compression—but adding in gzipping will definitely take us to the next, and final level. Look for patches soon (will have a post later this week/next detailing more plans in this area).

 
Carro Brasilia (not verified) wrote 20 weeks 3 days ago

Take a look at JavaScript Compressor. This is a powerful php compression class.

Add your comment

The content of this field is kept private and will not be shown publicly.
  • You can use Textile markup to format text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <pre>

More information about formatting options

Subscribe to updates

don't worry, spam free!

Recent comments