Only warn on JS minification errors, and allow for custom minification ignores

This commit is contained in:
Ben Hollis 2012-04-23 21:39:00 -07:00
parent 83178031d2
commit 82cc836ed9
2 changed files with 39 additions and 29 deletions

View file

@ -8,9 +8,11 @@ module Middleman::Extensions
class << self class << self
# Once registered # Once registered
def registered(app) def registered(app, options={})
app.set :css_compressor, false app.set :css_compressor, false
ignore = Array(options[:ignore]) << /\.min\./
app.after_configuration do app.after_configuration do
unless respond_to?(:css_compressor) && css_compressor unless respond_to?(:css_compressor) && css_compressor
require "middleman-more/extensions/minify_css/rainpress" require "middleman-more/extensions/minify_css/rainpress"
@ -18,7 +20,7 @@ module Middleman::Extensions
end end
# Setup Rack to watch for inline JS # Setup Rack to watch for inline JS
use InlineCSSRack, :compressor => css_compressor use InlineCSSRack, :compressor => css_compressor, :ignore => ignore
end end
end end
alias :included :registered alias :included :registered
@ -34,6 +36,7 @@ module Middleman::Extensions
def initialize(app, options={}) def initialize(app, options={})
@app = app @app = app
@compressor = options[:compressor] @compressor = options[:compressor]
@ignore = options[:ignore]
end end
# Rack interface # Rack interface
@ -59,7 +62,7 @@ module Middleman::Extensions
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified] response = [minified]
elsif path.end_with?('.css') && path !~ /\.min\./ elsif path.end_with?('.css') && @ignore.none? {|ignore| path =~ ignore }
uncompressed_source = extract_response_text(response) uncompressed_source = extract_response_text(response)
minified_css = @compressor.compress(uncompressed_source) minified_css = @compressor.compress(uncompressed_source)

View file

@ -8,9 +8,11 @@ module Middleman::Extensions
class << self class << self
# Once registered # Once registered
def registered(app) def registered(app, options={})
app.set :js_compressor, false app.set :js_compressor, false
ignore = Array(options[:ignore]) << /\.min\./
# Once config is parsed # Once config is parsed
app.after_configuration do app.after_configuration do
unless respond_to?(:js_compressor) && js_compressor unless respond_to?(:js_compressor) && js_compressor
@ -19,7 +21,7 @@ module Middleman::Extensions
end end
# Setup Rack to watch for inline JS # Setup Rack to watch for inline JS
use InlineJavascriptRack, :compressor => js_compressor use InlineJavascriptRack, :compressor => js_compressor, :ignore => ignore
end end
end end
alias :included :registered alias :included :registered
@ -34,6 +36,7 @@ module Middleman::Extensions
def initialize(app, options={}) def initialize(app, options={})
@app = app @app = app
@compressor = options[:compressor] @compressor = options[:compressor]
@ignore = options[:ignore]
end end
# Rack interface # Rack interface
@ -44,6 +47,7 @@ module Middleman::Extensions
path = env["PATH_INFO"] path = env["PATH_INFO"]
begin
if path.end_with?('.html') || path.end_with?('.php') if path.end_with?('.html') || path.end_with?('.php')
uncompressed_source = extract_response_text(response) uncompressed_source = extract_response_text(response)
@ -66,13 +70,16 @@ module Middleman::Extensions
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified] response = [minified]
elsif path.end_with?('.js') && path !~ /\.min\./ elsif path.end_with?('.js') && @ignore.none? {|ignore| path =~ ignore }
uncompressed_source = extract_response_text(response) uncompressed_source = extract_response_text(response)
minified_js = @compressor.compress(uncompressed_source) minified_js = @compressor.compress(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s
response = [minified_js] response = [minified_js]
end end
rescue ExecJS::ProgramError => e
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
end
[status, headers, response] [status, headers, response]
end end