Class: Middleman::Rack::MinifyJavascript

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman/features/minify_javascript/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ MinifyJavascript

Returns a new instance of MinifyJavascript.



11
12
13
# File 'lib/middleman/features/minify_javascript/rack.rb', line 11

def initialize(app, options={})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/middleman/features/minify_javascript/rack.rb', line 15

def call(env)
  status, headers, response = @app.call(env)
    
  if env["PATH_INFO"].match(/\.js$/)
    compressor = ::YUI::JavaScriptCompressor.new(:munge => true)

    uncompressed_source = response.is_a?(::Rack::File) ? File.read(response.path) : response
    response = compressor.compress(uncompressed_source)
    headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
  end
    
  [status, headers, response]
end