Git Product home page Git Product logo

yuicompressor's Introduction

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.

yuicompressor's People

Contributors

mjackson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yuicompressor's Issues

RuntimeError: Compression failed: Broken pipe

yuicompressor appears to be dying on an EC2 m1.small instance running under nanoc. It fails at a different place each run and never seems to successfully complete.

Ubuntu 10.04.3 LTS (lucid)
Ruby 1.8.7.249-2ubuntu0.1 (from distribution packages)
Rubygems 1.6.2 (from source)
bundler 1.1.3
yuicompressor 1.2.0

This may be related to the java memory error in yui-compressor.

$ bundle exec rake compile

[snip]

Captain! We’ve been hit!

=== MESSAGE:

RuntimeError: Compression failed: Broken pipe

=== COMPILATION STACK:

  (empty)

=== BACKTRACE:

  0. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/yuicompressor-1.2.0/lib/yuicompressor/shell.rb:65:in `compress'
  1. /usr/lib/ruby/1.8/open3.rb:86:in `popen3'
  2. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/yuicompressor-1.2.0/lib/yuicompressor/shell.rb:56:in `compress'
  3. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/filters/yui_compressor.rb:18:in `run'
  4. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/result_data/item_rep.rb:344:in `filter'
  5. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/item_rep_proxy.rb:47:in `filter'
  6. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/rule_context.rb:54:in `filter'
  7. ./Rules:64:in `load'
  8. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/rule.rb:68:in `instance_eval'
  9. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/rule.rb:68:in `apply_to'
  10. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/compiler.rb:381:in `compile_rep'
  11. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/compiler.rb:337:in `compile_reps'
  12. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/compiler.rb:330:in `loop'
  13. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/compiler.rb:330:in `compile_reps'
  14. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/compilation/compiler.rb:85:in `run'
  15. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/base/source_data/site.rb:57:in `compile'
  16. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/commands/compile.rb:57:in `run'
  17. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/command_runner.rb:14:in `call'
  18. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/error_handler.rb:42:in `handle_while'
  19. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/error_handler.rb:23:in `handle_while'
  20. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/command_runner.rb:13:in `call'
  21. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/cri-2.2.1/lib/cri/command_dsl.rb:175
  22. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/cri-2.2.1/lib/cri/command.rb:291:in `call'
  23. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/cri-2.2.1/lib/cri/command.rb:291:in `run_this'
  24. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/cri-2.2.1/lib/cri/command.rb:244:in `run'
  25. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/cri-2.2.1/lib/cri/command.rb:257:in `run'
  26. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli.rb:44:in `run'
  27. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/error_handler.rb:42:in `handle_while'
  28. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli/error_handler.rb:23:in `handle_while'
  29. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/lib/nanoc/cli.rb:41:in `run'
  30. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/gems/nanoc-3.3.2/bin/nanoc:19
  31. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/bin/nanoc:19:in `load'
  32. /srv/opscode-corpsite/vendor/bundle/ruby/1.8/bin/nanoc:19

=== VERSION INFORMATION:

nanoc 3.3.2 © 2007-2012 Denis Defreyne.
Running ruby 1.8.7 (2010-01-10) on x86_64-linux with RubyGems 1.6.2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.