Permalink
Browse files

Merge pull request #376 from bhollis/minify

Only warn on JS minification errors, and allow for custom minification ignores
  • Loading branch information...
2 parents 1d583ca + 82cc836 commit dc9c3c5e77e39814206d92c9f94c875be01403f1 @tdreyno tdreyno committed Apr 24, 2012
@@ -8,17 +8,19 @@ module MinifyCss
class << self
# Once registered
- def registered(app)
+ def registered(app, options={})
app.set :css_compressor, false
+ ignore = Array(options[:ignore]) << /\.min\./
+
app.after_configuration do
unless respond_to?(:css_compressor) && css_compressor
require "middleman-more/extensions/minify_css/rainpress"
set :css_compressor, ::Rainpress
end
# Setup Rack to watch for inline JS
- use InlineCSSRack, :compressor => css_compressor
+ use InlineCSSRack, :compressor => css_compressor, :ignore => ignore
end
end
alias :included :registered
@@ -34,6 +36,7 @@ class InlineCSSRack
def initialize(app, options={})
@app = app
@compressor = options[:compressor]
+ @ignore = options[:ignore]
end
# Rack interface
@@ -59,7 +62,7 @@ def call(env)
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
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)
minified_css = @compressor.compress(uncompressed_source)
@@ -8,9 +8,11 @@ module MinifyJavascript
class << self
# Once registered
- def registered(app)
+ def registered(app, options={})
app.set :js_compressor, false
+ ignore = Array(options[:ignore]) << /\.min\./
+
# Once config is parsed
app.after_configuration do
unless respond_to?(:js_compressor) && js_compressor
@@ -19,7 +21,7 @@ def registered(app)
end
# Setup Rack to watch for inline JS
- use InlineJavascriptRack, :compressor => js_compressor
+ use InlineJavascriptRack, :compressor => js_compressor, :ignore => ignore
end
end
alias :included :registered
@@ -34,6 +36,7 @@ class InlineJavascriptRack
def initialize(app, options={})
@app = app
@compressor = options[:compressor]
+ @ignore = options[:ignore]
end
# Rack interface
@@ -44,35 +47,39 @@ def call(env)
path = env["PATH_INFO"]
- if path.end_with?('.html') || path.end_with?('.php')
- uncompressed_source = extract_response_text(response)
+ begin
+ 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|
- first = $1
- javascript = $2
- last = $3
+ minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
+ first = $1
+ javascript = $2
+ last = $3
- # Only compress script tags that contain JavaScript (as opposed
- # to something like jQuery templates, identified with a "text/html"
- # type.
- if first =~ /<script>/ || first.include?('text/javascript')
- minified_js = @compressor.compress(javascript)
+ # Only compress script tags that contain JavaScript (as opposed
+ # to something like jQuery templates, identified with a "text/html"
+ # type.
+ if first =~ /<script>/ || first.include?('text/javascript')
+ minified_js = @compressor.compress(javascript)
- first << minified_js << last
- else
- match
+ first << minified_js << last
+ else
+ match
+ end
end
- end
- headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
- 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).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
+ headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s
+ response = [minified_js]
+ end
+ rescue ExecJS::ProgramError => e
+ warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
+ end
[status, headers, response]
end

0 comments on commit dc9c3c5

Please sign in to comment.