Railsify!

PackR

== PackR

PackR is a Ruby port of Dean Edwards' MIT-licensed JavaScript compressor, Packer:

  # Packer version 3.0 (final) - copyright 2004-2007, Dean Edwards
  # http://www.opensource.org/licenses/mit-license
  http://dean.edwards.name/packer/

This version is based on Packer 3.0. You may find that the results it produces
are not strictly identical to those of the JavaScript version, but the difference is
just a question of how the base-62 word list is ordered -- your scripts
will work just like those produced with the online version. The level of
compression achieved is identical between the two versions.

=== Usage

Usage is dead simple. Within your Rails app, you can compress pieces of code
like this:

  # Create a new instance of Packr first
  packr = Packr.new
  compressed = packr.pack(script)
  
  # Pass options to control the type of compression
  compressed = packr.pack(script, :shrink_vars => true)
  compressed = packr.pack(script, :base62 => true)
  compressed = packr.pack(script, :shrink_vars => true, :base62 => true)

If you want to compress a particular file, you can do this:

  packr = Packr.new
  packr.pack_file(path, ... options as above ...)

Be careful with that - it will overwrite the contents of the file with
the packed version. Be a good kid and use version control in case something
goes wrong and you lose all your source code!

=== Automated packing

PackR also comes with a rake task to let you batch-pack all your scripts.
To use it, store any files you want to serve in packed form in the directory
<tt>lib/javascripts</tt>. (The idea is that you won't be serving the source files to
the public, so why keep them in the public folder? Also, it keeps the source
files and packed copies nicely separated.) Then run:

  rake packr:pack_libs

and PackR will put packed copies of all the scripts from <tt>lib/javascripts</tt> in
<tt>public/javascripts</tt>. Again, be careful as this will overwrite any pre-existing
files in your public directory.

It is not recommended to run this as part of your deployment process, as
you will need to verfiy that your JavaScript works when packed. PackR works
using regular expressions -- not a true JavaScript interpreter -- and cannot
fix missing semicolons for you.

Also, DO NOT use PackR to compress files on-the-fly in response to HTTP
requests. The packing process can be quite processor-intensive and it will
make you app very slow when serving script files.

=== License

Copyright (c) 2007 Dean Edwards, James Coglan

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Last updated: October 17, 2007 01:00