Class: Sprockets::YUICompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/yui_compressor.rb

Overview

Public: YUI compressor.

To accept the default options

environment.register_bundle_processor 'application/javascript',
  Sprockets::YUICompressor

Or to pass options to the YUI::JavaScriptCompressor class.

environment.register_bundle_processor 'application/javascript',
  Sprockets::YUICompressor.new(munge: true)

Constant Summary collapse

VERSION =
'1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ YUICompressor



30
31
32
33
34
35
36
37
38
# File 'lib/sprockets/yui_compressor.rb', line 30

def initialize(options = {})
  @options = options
  @cache_key = [
    self.class.name,
    ::YUI::Compressor::VERSION,
    VERSION,
    options
  ].freeze
end

Class Method Details

.call(input) ⇒ Object



26
27
28
# File 'lib/sprockets/yui_compressor.rb', line 26

def self.call(input)
  instance.call(input)
end

.instanceObject

Public: Return singleton instance with default options.

Returns YUICompressor object.



22
23
24
# File 'lib/sprockets/yui_compressor.rb', line 22

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sprockets/yui_compressor.rb', line 40

def call(input)
  data = input[:data]

  case input[:content_type]
  when 'application/javascript'
    key = @cache_key + [input[:content_type], input[:data]]
    input[:cache].fetch(key) do
      ::YUI::JavaScriptCompressor.new(@options).compress(data)
    end
  when 'text/css'
    key = @cache_key + [input[:content_type], input[:data]]
    input[:cache].fetch(key) do
      ::YUI::CssCompressor.new(@options).compress(data)
    end
  else
    data
  end
end