#!/usr/bin/env ruby require 'rubygems' require 'oyster' require File.expand_path('../../lib/packr', __FILE__) spec = Oyster.spec do name "packr -- JavaScript code compressor based on Dean Edwards' Packer" synopsis <<-EOS packr [OPTIONS] INPUT_FILES -o OUTPUT_FILE cat INPUT_FILES | packr [OPTIONS] > OUTPUT_FILE EOS description <<-EOS Packr is a program for compressing JavaScript programs. 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. When invoked from the command line, it concatenates all the code in INPUT_FILES (or from standard input) and compresses the code using the given options, printing the result to standard output. If the --output option is used, the result is written to the given file along with a source map. EOS flag :minify, :default => true, :desc => 'Remove unnecessary whitespace, comments and other tokens' flag :'shrink-vars', :default => true, :desc => 'Shrink local variable names inside functions' flag :private, :default => false, :desc => 'Obfuscate private identifiers, i.e. names beginning with a single underscore' flag :base62, :default => false, :desc => 'Encode the program using base 62' array :protect, :default => [], :desc => 'List of variable names to protect from compression when using --shrink-vars' string :header, :desc => 'Optional string to prepend to the output, e.g. for copyright comments' string :output, :desc => <<-EOS Path to which to write compressed output; output will be written to stdout unless this is passed. If this option is used with a list of INPUT_FILES then a source map will also be written to OUTPUT_FILE.map. EOS notes <<-EOS 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. EOS author <<-EOS Original JavaScript version by Dean Edwards, Ruby port by James Coglan EOS copyright <<-EOS Copyright (c) 2004-2012 Dean Edwards, James Coglan. This program is free software, distributed under the MIT license. EOS end begin opts = spec.parse compression_options = { :minify => opts[:minify], :shrink_vars => opts[:'shrink-vars'], :private => opts[:private], :protect => opts[:protect], :base62 => opts[:base62], :header => opts[:header] } inputs = opts[:unclaimed] output = opts[:output] if inputs.empty? result = Packr.pack($stdin.read, compression_options) if output FileUtils.mkdir_p(File.expand_path('..', output)) File.open(output, 'w') { |f| f.write(result) } else $stdout.puts(result) end else result = Packr.bundle(inputs, output, compression_options) $stdout.puts(result) unless output end rescue Oyster::HelpRendered end