Merge pull request #392 from bhollis/minify

Disable inline minification by default, allow it to be turned on with an option
This commit is contained in:
Thomas Reynolds 2012-04-26 10:52:48 -07:00
commit 1e271448fa
4 changed files with 28 additions and 12 deletions

View file

@ -12,6 +12,14 @@ Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state|
end end
end end
Given /^"([^\"]*)" feature is "enabled" with "([^\"]*)"$/ do |feature, options_str|
@initialize_commands ||= []
options = eval("{#{options_str}}")
@initialize_commands << lambda { activate(feature.to_sym, options) }
end
Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value| Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value|
@initialize_commands ||= [] @initialize_commands ||= []
@initialize_commands << lambda { set(variable.to_sym, value) } @initialize_commands << lambda { set(variable.to_sym, value) }

View file

@ -78,7 +78,7 @@ Feature: Minify Javascript
""" """
Scenario: Rendering inline js with the feature enabled Scenario: Rendering inline js with the feature enabled
Given "minify_javascript" feature is "enabled" Given "minify_javascript" feature is "enabled" with ":inline => true"
And the Server is running at "minify-js-app" And the Server is running at "minify-js-app"
When I go to "/inline-js.html" When I go to "/inline-js.html"
Then I should see: Then I should see:
@ -115,7 +115,7 @@ Feature: Minify Javascript
Then I should see "8" lines Then I should see "8" lines
Scenario: Rendering inline js (coffeescript) with the feature enabled Scenario: Rendering inline js (coffeescript) with the feature enabled
Given "minify_javascript" feature is "enabled" Given "minify_javascript" feature is "enabled" with ":inline => true"
And the Server is running at "minify-js-app" And the Server is running at "minify-js-app"
When I go to "/inline-coffeescript.html" When I go to "/inline-coffeescript.html"
Then I should see "6" lines Then I should see "6" lines

View file

@ -12,6 +12,7 @@ module Middleman::Extensions
app.set :css_compressor, false app.set :css_compressor, false
ignore = Array(options[:ignore]) << /\.min\./ ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
app.after_configuration do app.after_configuration do
unless respond_to?(:css_compressor) && css_compressor unless respond_to?(:css_compressor) && css_compressor
@ -19,16 +20,18 @@ module Middleman::Extensions
set :css_compressor, ::Rainpress set :css_compressor, ::Rainpress
end end
# Setup Rack to watch for inline JS # Setup Rack middleware to minify CSS
use InlineCSSRack, :compressor => css_compressor, :ignore => ignore use MinifyCSSRack, :compressor => css_compressor,
:ignore => ignore,
:inline => inline
end end
end end
alias :included :registered alias :included :registered
end end
end end
# Rack middleware to look for JS in HTML and compress it # Rack middleware to look for CSS and compress it
class InlineCSSRack class MinifyCSSRack
# Init # Init
# @param [Class] app # @param [Class] app
@ -37,6 +40,7 @@ module Middleman::Extensions
@app = app @app = app
@compressor = options[:compressor] @compressor = options[:compressor]
@ignore = options[:ignore] @ignore = options[:ignore]
@inline = options[:inline]
end end
# Rack interface # Rack interface
@ -47,7 +51,7 @@ module Middleman::Extensions
path = env["PATH_INFO"] path = env["PATH_INFO"]
if path.end_with?('.html') || path.end_with?('.php') if (path.end_with?('.html') || path.end_with?('.php')) && @inline
uncompressed_source = extract_response_text(response) uncompressed_source = extract_response_text(response)
minified = uncompressed_source.gsub(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match| minified = uncompressed_source.gsub(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match|

View file

@ -12,6 +12,7 @@ module Middleman::Extensions
app.set :js_compressor, false app.set :js_compressor, false
ignore = Array(options[:ignore]) << /\.min\./ ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
# Once config is parsed # Once config is parsed
app.after_configuration do app.after_configuration do
@ -20,15 +21,17 @@ module Middleman::Extensions
set :js_compressor, ::Uglifier.new set :js_compressor, ::Uglifier.new
end end
# Setup Rack to watch for inline JS # Setup Rack middlware to minify JS
use InlineJavascriptRack, :compressor => js_compressor, :ignore => ignore use MinifyJavascriptRack, :compressor => js_compressor,
:ignore => ignore,
:inline => inline
end end
end end
alias :included :registered alias :included :registered
end end
# Rack middleware to look for JS in HTML and compress it # Rack middleware to look for JS and compress it
class InlineJavascriptRack class MinifyJavascriptRack
# Init # Init
# @param [Class] app # @param [Class] app
@ -37,6 +40,7 @@ module Middleman::Extensions
@app = app @app = app
@compressor = options[:compressor] @compressor = options[:compressor]
@ignore = options[:ignore] @ignore = options[:ignore]
@inline = options[:inline]
end end
# Rack interface # Rack interface
@ -48,7 +52,7 @@ module Middleman::Extensions
path = env["PATH_INFO"] path = env["PATH_INFO"]
begin begin
if path.end_with?('.html') || path.end_with?('.php') if (path.end_with?('.html') || path.end_with?('.php')) && @inline
uncompressed_source = extract_response_text(response) 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|