#5 discuss
Sam Stephenson

Integrate the YUI Compressor

Reported by Sam Stephenson | February 11th, 2009 @ 10:19 PM | in 1.1.0

Sprockets should offer a feature to pass the resulting concatenation through the YUI Compressor.

YUI Compressor is distributed as an 830 KB .jar and requires Java >= 1.4 to run. Shelling out to it from Sprockets is easy; the difficult problem, as I see it, is figuring out how to a) bundle the compressor with Sprockets or b) make use of existing installations of the compressor with minimal configuration.

Comments and changes to this ticket

  • ronin-15560 (at lighthouseapp)

    ronin-15560 (at lighthouseapp) February 11th, 2009 @ 10:41 PM

    I wonder if we can just download the YUI compressor with an extra, optional step and install it into the sprockets lib folder.

    As it requires Java it might not run everywhere (This is a sentence I long wanted to use).

  • Ryan Baumann

    Ryan Baumann February 19th, 2009 @ 11:28 PM

    How about something like the attached? This makes a path to the YUI jar a Secretary option (so you don't have to distribute the jar, and you get all the baked-in Secretary config goodness), and makes the main calls to secretary use "processed" instead of "concatenation" which is really just a naming thing - if the YUI jar is set, it will try to minify the concatenation, if not it will just give the concatenation. For the minify method there may be a cleaner way than a popen that just assumes "java" is on the path.

  • Andrew Dupont

    Andrew Dupont February 21st, 2009 @ 03:59 AM

    • State changed from “new” to “open”

    Ryan, that approach seems OK to me. I'd also suggest that the path to the YUI can be set by environment variable — though a command-line argument would take precedence.

  • Ryan Baumann

    Ryan Baumann February 22nd, 2009 @ 02:33 AM

    Here's a slightly improved version of the patch. The YUI path can now be set by an environment variable, and Concatenation.save_to has been pulled out into Secretary (would also affect sprockets-rails in a few places). The minify method has been made a little bit more robust. A stub conditional test (using the YUI path environment variable) for minification has been dropped in.

  • Sam Stephenson

    Sam Stephenson February 22nd, 2009 @ 08:01 PM

    Thanks Ryan. Good call on moving Concatenation#save_to into Secretary. I'm not so keen on the name Secretary#processed.

  • Sam Stephenson

    Sam Stephenson February 22nd, 2009 @ 08:17 PM

    • State changed from “open” to “discuss”
  • Sam Stephenson

    Sam Stephenson February 22nd, 2009 @ 08:23 PM

    • Milestone set to 1.1.0
  • Thomas Bukowski

    Thomas Bukowski March 18th, 2009 @ 03:25 AM

    As an alternative, could Sprockets use a Ruby javascript compressor? Say, something like Douglas Crockford's: http://www.crockford.com/javascr...

  • Logan Raarup

    Logan Raarup April 20th, 2009 @ 12:43 PM

    • Tag changed from feature to compression, feature, suggestion

    I agree with Thomas Bukowski that YUI should not be the only option. I think Ryan's patch would be a good solution, as long as it does not only apply to YUI. It could be a configuration option that allows you to pass the output file to any shell command - this broadens the usage of the option beyond compression.

      :post_process_command => 'java -jar /path/to/yui -o #{file} #{file}'

    An alternative approach could be to provide various compressors as plugins in the form of gems (sprockets-compressor-yui, sprockets-compressor-jsmin etc.) - each of these gems would contain the compressor script/binary and provide configuration options in the Secretary class to enable the compressor.

    I have used something similar to the latter approach in a Rails plugin that enables compression through YUI by overriding the save_to method (a temporary solution until Sprockets supports compression).

  • ronin-15560 (at lighthouseapp)

    ronin-15560 (at lighthouseapp) April 20th, 2009 @ 01:08 PM

    I like the idea of gem-based plugins, which would remove the hassle of configuring. Install the plugin, and you've a command line option available for compression. We'd need a "API" in Sprockets for registering a plugin command-line option, and we can just use Ruby to alias/override methods in the plugins.

  • Samuel Lebeau

    Samuel Lebeau October 23rd, 2009 @ 06:42 PM

    I'm not sure it's worth it to support several minifiers.
    As Sam already wrapped up YUI Compressor as a gem, I decided to try integrate it and it works like a charm.

    The patch consists in two commits: http://github.com/samleb/sprockets/tree/minification

  • Samuel Lebeau

    Samuel Lebeau November 13th, 2009 @ 04:08 PM

    Thanks to @jwheare I fixed support for minification in nph-sprockets.cgi.
    The third commit is available on my minification branch here : http://github.com/samleb/sprockets/commit/fc8fdff18e0d86969da451772...

    I forgot to notice that you can use either :minify => true which uses ruby-yui-compressor gem default options, or give an explicit hash of options like :minify => { :munge => true }.

    What do you guys think about integrating this ?

  • jwheare

    jwheare November 13th, 2009 @ 04:39 PM

    +1 Samuel's patch. This would be great to have in the main gem.

  • Loduis Madariaga

    Loduis Madariaga February 15th, 2011 @ 10:14 PM

    • Importance changed from “” to “High”

    I've been working on a solution that uses the google closure compiler.
    Attached file path

  • Johannes

    Johannes July 27th, 2011 @ 12:59 PM

    +1 on integrating such patch into main. I'd love that feature.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

JavaScript dependency management and concatenation
<a href="http://getsprockets.org/">http://getsprockets.org/</a>