Class: HtmlMockup::Release::Processors::Yuicompressor

Inherits:
Base
  • Object
show all
Defined in:
lib/html_mockup/release/processors/yuicompressor.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from HtmlMockup::Release::Processors::Base

Instance Method Details

#call(release, options = {}) ⇒ Object

Compresses all JS and CSS files, it will keep all lines before

/* -------------------------------------------------------------------------------- */

(80 dashes)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/html_mockup/release/processors/yuicompressor.rb', line 16

def call(release, options={})
  options = {
    :match => ["**/*.{css,js}"],
    :skip =>  [/javascripts\/vendor\/.*\.js\Z/, /_doc\/.*/],
    :delimiter => Regexp.escape("/* -------------------------------------------------------------------------------- */")
  }.update(options)
  
  compressor_options = {:line_break => 80}
  css_compressor = YUI::CssCompressor.new(compressor_options) 
  js_compressor = YUI::JavaScriptCompressor.new(compressor_options)
  
  release.log self,  "Minifying #{options[:match].inspect}"
  
  # Add version numbers and minify the files
  release.get_files(options[:match], options[:skip]).each do |f|
    type = f[/\.([^.]+)\Z/,1]
    
    data = File.read(f);
    File.open(f,"w") do |fh| 
      
      # Extract header and store for later use
      header = data[/\A(.+?)\n#{options[:delimiter]}\s*\n/m,1]
      minified = [header]

      # Actual minification
      release.debug self,  "Minifying #{f}"
      case type
      when "css"
        minified << css_compressor.compress(data)
      when "js"
        minified << js_compressor.compress(data)
      else
        release.warn self, "Error minifying: encountered unknown type \"#{type}\""
        minified << data
      end

      fh.write minified.join("\n")
    end
  end
  
end