mjackson / yuicompressor Goto Github PK
View Code? Open in Web Editor NEWA YUI JavaScript and CSS compressor wrapper for Ruby and JRuby
A YUI JavaScript and CSS compressor wrapper for Ruby and JRuby
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.
How to limit (in terms of memory usage) the parallel compression process threads !
Hi,
I use latest version of Jammit which uses yui compressor gem version 0.9.1.
When I compress a CSS file with media queries, those queries don't work.
This is a known issue of "old" versions of the YUI compressor. The solution would be to use a more recent version.
This is more detailed there: http://stackoverflow.com/questions/4144706/is-there-a-version-of-yui-compressor-that-deals-correctly-with-media-queries
Cheers,
Damien :)
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.