No commit activity in last 3 years
No release in over 3 years
Ruby version of Dean Edwards' JavaScript compressor
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0.9.5
 Project Readme

Packr is a Ruby implementation of Dean Edwards’ JavaScript compressor, Packer. It can remove comments and whitespace, compress variable names, obfuscate _private identifiers, and base-62 encode your programs. It can also generate source maps, which lets your browser trace log messages and errors back to the original source, even when running minified code.

Usage¶ ↑

Packr provides both a command-line interface and a Ruby API. To call it from the command line (use packr --help to see available options):

packr my_script.js > my_script.min.js

To call from within a Ruby program:

require 'packr'

code = File.read('my_script.js')
compressed = Packr.pack(code)
File.open('my_script.min.js', 'w') { |f| f.write(compressed) }

This method takes a number of options to control compression, for example:

compressed = Packr.pack(code, :shrink_vars => true, :base62 => true)

The full list of available options is:

  • :minify – set to false to prevent any compression; this is useful if you just want to use Packr.bundle to concatenate files and generate a source map

  • :shrink_vars – set to true to compress local variable names

  • :private – set to true to obfuscate ‘private’ identifiers, i.e. names beginning with a single underscore

  • :base62 – encode the program using base 62

  • :protect – an array of variable names to protect from compression

  • :header – an optional string to prepend to the output, e.g. for copyright comments

Here’s an example using all of these:

compressed = Packr.pack(code,
  :shrink_vars => true,
  :private     => true,
  :protect     => %w[$super self],
  :header      => '/* Copyright 2012 some guy */'
)

Bundling and source maps¶ ↑

Packr also provides an API for bundling several files together and generating a single output file and source map. For example, given these two files:

# example_a.js

1. // When the minified code is loaded into a browser, you should see the call to
2. // console.log() attributed to example_a.js:4
3. 
4. console.log('Hello from file A');

and

# example_b.js

1. var display = function(message) {
2.   alert(message + ' from file B');
3. };
4. 
5. display('Ahoy there');

it can generate a combined output file:

# example-min.js

/* Copyright 2012 */
console.log('Hello from file A');var display=function(a){alert(a+' from file B')};display('Ahoy there');
//@ sourceMappingURL=example-min.js.map

and a source map:

# example-min.js.map

{
  "version": 3,
  "file": "example-min.js",
  "sourceRoot": "",
  "sources": ["../example_a.js", "../example_b.js"],
  "names": ["message"],
  "mappings": ";AAGA,QAAQ,KAAK,MAAM,KAAK,KAAK,ICH7B,IAAI,QAAU,SAASA,GACrB,MAAMA,IAAY,KAAK,KAAK,KAG9B,SAAS,KAAK;"
}

To do this, you use the bundle method, passing an array of input files and a single output file, and the compression options you want to use.

Packr.bundle %w[example_a.js example_b.js], 'min/example-min.js',
  :shrink_vars => true,
  :header      => '/* Copyright 2012 */'

On the command line, this looks like:

packr example_a.js example_b.js -o min/example-min.js -h '/* Copyright 2012 */' --shrink-vars

Notes¶ ↑

This program is not a JavaScript parser, and rewrites your files using regular expressions. Be sure to include semicolons and braces everywhere they are required so that your program will work correctly when packed down to a single line.

By far the most efficient way to serve JavaScript over the web is to use Packr with the --shrink-vars flag, combined with gzip compression.

If you really cannot serve gzip files, use the --base62 option to further compress your code. This mode is at its best when compressing large files with many repeated tokens.

The --private option can be used to stop other programs calling private methods in your code by renaming anything beginning with a single underscore. Beware that you should not use this if the generated file contains ‘private’ methods that need to be accessible by other files. Also know that all the files that access any particular private method must be compressed together so they all get the same rewritten name for the private method.

License¶ ↑

(The MIT License)

Copyright © 2004-2012 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.