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,34 +47,38 @@ module Middleman::Extensions
path = env["PATH_INFO"] path = env["PATH_INFO"]
if path.end_with?('.html') || path.end_with?('.php') begin
uncompressed_source = extract_response_text(response) if path.end_with?('.html') || path.end_with?('.php')
uncompressed_source = extract_response_text(response)
minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match| minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
first = $1 first = $1
javascript = $2 javascript = $2
last = $3 last = $3
# Only compress script tags that contain JavaScript (as opposed # Only compress script tags that contain JavaScript (as opposed
# to something like jQuery templates, identified with a "text/html" # to something like jQuery templates, identified with a "text/html"
# type. # type.
if first =~ /<script>/ || first.include?('text/javascript') if first =~ /<script>/ || first.include?('text/javascript')
minified_js = @compressor.compress(javascript) minified_js = @compressor.compress(javascript)
first << minified_js << last first << minified_js << last
else else
match match
end
end end
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
elsif path.end_with?('.js') && @ignore.none? {|ignore| path =~ ignore }
uncompressed_source = extract_response_text(response)
minified_js = @compressor.compress(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s
response = [minified_js]
end end
rescue ExecJS::ProgramError => e
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
response = [minified]
elsif path.end_with?('.js') && path !~ /\.min\./
uncompressed_source = extract_response_text(response)
minified_js = @compressor.compress(uncompressed_source)
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s
response = [minified_js]
end end
[status, headers, response] [status, headers, response]