Packing Prototype

Posted in Prototype

In the last month or two we’ve had a number of tickets filed on the Rails Trac asking that Prototype adopt a more rigid syntax. Doing so would make it much easier to compress Prototype with tools like JSMin and Packer.

Opinions vary on this issue within the core team, but we all seem to agree that:

  1. We don’t want to distribute an official compressed version of Prototype.
  2. We don’t want to support a compressed version of Prototype. I’d rather eat glass than have to reconcile line numbers in a ticket someone filed against the compressed version.
  3. Some of the syntax requirements of compressors really harsh our mellow — for instance, Sam is not a fan of semicolons in one‐line functions, whereas I really don’t want to wrap one‐line if statements in braces. We don’t intentionally write malformed JavaScript, but as long as it can be parsed by a browser then it’s fine by us.
  4. Though there is a time and place for minification, we generally prefer other ways of reducing page weight: mod_gzip and proper caching, for instance.

Thus: any tickets created on this subject will be summarily closed.

But we’re not naive — we know people will compress Prototype whether we endorse it or not. Steve Kallestad distributes compressed versions of Prototype and Scriptaculous, managing to reduce Prototype’s file size from 64K to 25K. That’s compelling.

So if you want a compressed version of Prototype without having to put all those damned semicolons in by hand, use Dojo ShrinkSafe first. It uses Rhino, Mozilla’s JavaScript engine for Java, to parse and compress JavaScript safely, then outputs a strange‐looking but syntax‐abiding JavaScript file.

Here’s a quick workflow to compress whatever version of Prototype you please:

  1. Grab the most recent stable version of Prototype, the bleeding‐edge version in source control, or whichever older version you prefer.
  2. ShrinkSafe it. You can do this on the Dojo website, from the command line, with a GUI version for OS X, or in TextMate with my JavaScript Tools Bundle. Some versions let you choose whether to strip newlines; for others you’ll have to do it manually.
  3. Run the resulting file through this online version of Packer. The JavaScript version on Dean Edwards’s site will not work — it chokes on a few properties that the PHP version does not. (Or you can experiment with some of the offline versions — .NET, PHP5, Perl — but I haven’t tested these myself.)
  4. Be safe: verify that your newly‐compressed version of Prototype passes all unit tests. (No whining! If you’re old enough to compress then you’re old enough to run unit tests, son.)

Following these easily‐automable steps allowed me to reduce version 1.5.0 of Prototype from 71K to 31K. So this is great news for all: those people who care so deeply about JS compression can integrate this into their build process, whereas those of us who contribute to Prototype can lazily and selfishly flout whatever syntax rules are not mandated by JavaScript interpreters.

Go nuts!

Comments

  1. Pingback