2009-11-30 14:32:02 -05:00
|
|
|
begin
|
|
|
|
require "yui/compressor"
|
|
|
|
rescue LoadError
|
|
|
|
puts "YUI-Compressor not available. Install it with: gem install yui-compressor"
|
|
|
|
end
|
|
|
|
|
2010-01-20 18:22:40 -05:00
|
|
|
class Middleman::Rack::MinifyJavascript
|
|
|
|
def initialize(app, options={})
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2010-03-02 20:38:08 -05:00
|
|
|
status, headers, response = @app.call(env)
|
|
|
|
|
2010-03-03 00:50:33 -05:00
|
|
|
if env["PATH_INFO"].match(/\.js$/)
|
2010-01-20 18:22:40 -05:00
|
|
|
compressor = ::YUI::JavaScriptCompressor.new(:munge => true)
|
|
|
|
|
2010-03-02 20:38:08 -05:00
|
|
|
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
|
2009-11-30 14:32:02 -05:00
|
|
|
end
|
2010-01-20 18:22:40 -05:00
|
|
|
|
2010-03-02 20:38:08 -05:00
|
|
|
[status, headers, response]
|
2009-11-30 14:32:02 -05:00
|
|
|
end
|
2010-01-20 18:22:40 -05:00
|
|
|
end
|