lib/jammit/compressor.rb in jammit-0.1.3 vs lib/jammit/compressor.rb in jammit-0.2.0

- old
+ new

@@ -1,9 +1,10 @@ module Jammit - # Uses the YUI Compressor to compress JavaScript and CSS. (Which means that - # Java must be installed.) Also knows how to create a concatenated JST file. + # Uses the YUI Compressor or Closure Compiler to compress JavaScript. + # Always uses YUI to compress CSS (Which means that Java must be installed.) + # Also knows how to create a concatenated JST file. # If "embed_images" is turned on, creates "mhtml" and "datauri" versions of # all stylesheets, with all enabled images inlined into the css. class Compressor # Mapping from extension to mime-type of all embeddable images. @@ -27,28 +28,38 @@ # JST file constants. JST_START = "(function(){window.JST = window.JST || {};" JST_END = "})();" + DEFAULT_OPTIONS = { + :yui => {:munge => true}, + :closure => {} + } + # Creating a compressor initializes the internal YUI Compressor from - # the "yui-compressor" gem. + # the "yui-compressor" gem, or the internal Closure Compiler from the + # "closure-compiler" gem. def initialize - @yui_js = YUI::JavaScriptCompressor.new(:munge => true) - @yui_css = YUI::CssCompressor.new + @css_compressor = YUI::CssCompressor.new + flavor = Jammit.javascript_compressor + js_options = DEFAULT_OPTIONS[flavor].merge(Jammit.compressor_options) + @js_compressor = flavor == :closure ? + Closure::Compiler.new(js_options) : + YUI::JavaScriptCompressor.new(js_options) end # Concatenate together a list of JavaScript paths, and pass them through the # YUI Compressor (with munging enabled). def compress_js(paths) - @yui_js.compress(concatenate(paths)) + @js_compressor.compress(concatenate(paths)) end # Concatenate and compress a list of CSS stylesheets. When compressing a # :datauri or :mhtml variant, post-processes the result to embed # referenced images. def compress_css(paths, variant=nil, asset_url=nil) - return @yui_css.compress(concatenate(paths)) if variant.nil? - compressed_css = @yui_css.compress(concatenate_and_tag_images(paths)) + return @css_compressor.compress(concatenate(paths)) if variant.nil? + compressed_css = @css_compressor.compress(concatenate_and_tag_images(paths)) return with_data_uris(compressed_css) if variant == :datauri return with_mhtml(compressed_css, asset_url) if variant == :mhtml raise PackageNotFound, "\"#{variant}\" is not a valid stylesheet variant" end