YUICompressor

YUICompressor is a Ruby module that may be used to create compressed versions of JavaScript and CSS code quickly and easily using the Yahoo User Interface (YUI) library compressor. The module is essentially a wrapper around the YUI Compressor (a Java library) that supports two different modes of operation: shell and native.

In shell mode the YUI Compressor library executes in a separate process. Code is piped into and out of this process using the system shell. This approach yields good performance and is the default for MRI and other Ruby versions that are not able to execute Java code.

In native mode the compressor is invoked in the same process as Ruby. This is only possible when using YUICompressor on JRuby. With this approach, compression speeds dramatically improve because the system does not incur the overhead of invoking a separate Java process for each compression.

In any case YUICompressor will automatically detect the Ruby platform it is running on and choose the mode that will offer the best performance.

The inspiration for this project is Sam Stephenson’s yui-compressor gem. The goal of YUICompressor is to provide a more fluent interface, greater flexibility, and better performance than its predecessor.

Installation


Via RubyGems:

$ gem install yuicompressor

From a local copy:

$ git clone git://github.com/mjijackson/yuicompressor.git
$ cd yuicompressor
$ rake package && rake install

Usage


The easiest way to compress JavaScript code using YUICompressor is to pass an IO or a string containing your code to YUICompressor.compress_js. Similarly, CSS code may similarly be compressed using YUICompressor.compress_css. Both methods may take a second parameter that is a Hash of options to use during compression. When compressing CSS, the following option is available:

:line_break     The maximum number of characters that may appear in a
                single line of compressed code. Defaults to no maximum
                length. If set to 0 each line will be the minimum length
                possible.
:charset        Sests the carset in which to read the input file. Passed
                through to the YUICompressor Java library. Default: utf8

When compressing JavaScript, these additional options are available:

:munge                  Should be +true+ if the compressor should shorten
                        local variable names when possible. Defaults to
                        +false+.
:preserve_semicolons    Should be +true+ if the compressor should preserve
                        all semicolons in the code. Defaults to +false+.
:optimize               Should be +true+ if the compressor should enable all
                        micro optimizations. Defaults to +true+.

When running in shell mode (i.e. not on JRuby) a :java option may be used to specify the path to the Java binary. The default value is “java”.

Examples


JavaScript compression:

require 'yuicompressor'
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
YUICompressor.compress_js(js)
 => "(function(){var abc={};abc.key=\"value\"})();"

JavaScript compression with shortened local variable names (i.e. munging):

require 'yuicompressor'
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
YUICompressor.compress_js(js, :munge => true)
 => "(function(){var a={};a.key=\"value\"})();"

CSS compression:

require 'yuicompressor'
css = <<'CODE'
div.main {
  background-position: 0;
}
h1#title {
  color: #FFFFFF;
}
CODE
YUICompressor.compress_css(css)
 => "div.main{background-position:0 0}h1#title{color:#fff}"

CSS compression with every statement on a separate line:

require 'yuicompressor'
css = <<'CODE'
div.main {
  background-position: 0;
}
h1#title {
  color: #FFFFFF;
}
CODE
YUICompressor.compress_css(css, :line_break => 0)
 => "div.main{background-position:0 0}\nh1#title{color:#fff}"

Including in another module:

require 'yuicompressor'

module MyJavaScriptCompressor
  include YUICompressor

  def compress(code)
    compress_js(code)
  end
end

License


Copyright 2010 Michael Jackson

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.

Credits


The YUI Compressor library is copyright Yahoo! Inc. The jar file is included in the source distribution under the terms of the BSD license.

Also, thanks to Charles Nutter (@headius) for helping me understand how to convert Ruby streams to Java and back when doing the JRuby module.