1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

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
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|
@initialize_commands ||= []
@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
Given "minify_javascript" feature is "enabled"
Given "minify_javascript" feature is "enabled" with ":inline => true"
And the Server is running at "minify-js-app"
When I go to "/inline-js.html"
Then I should see:
@ -115,7 +115,7 @@ Feature: Minify Javascript
Then I should see "8" lines
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"
When I go to "/inline-coffeescript.html"
Then I should see "6" lines

View file

@ -12,6 +12,7 @@ module Middleman::Extensions
app.set :css_compressor, false
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
app.after_configuration do
unless respond_to?(:css_compressor) && css_compressor
@ -19,16 +20,18 @@ module Middleman::Extensions
set :css_compressor, ::Rainpress
end
# Setup Rack to watch for inline JS
use InlineCSSRack, :compressor => css_compressor, :ignore => ignore
# Setup Rack middleware to minify CSS
use MinifyCSSRack, :compressor => css_compressor,
:ignore => ignore,
:inline => inline
end
end
alias :included :registered
end
end
# Rack middleware to look for JS in HTML and compress it
class InlineCSSRack
# Rack middleware to look for CSS and compress it
class MinifyCSSRack
# Init
# @param [Class] app
@ -37,6 +40,7 @@ module Middleman::Extensions
@app = app
@compressor = options[:compressor]
@ignore = options[:ignore]
@inline = options[:inline]
end
# Rack interface
@ -47,7 +51,7 @@ module Middleman::Extensions
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)
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
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
# Once config is parsed
app.after_configuration do
@ -20,15 +21,17 @@ module Middleman::Extensions
set :js_compressor, ::Uglifier.new
end
# Setup Rack to watch for inline JS
use InlineJavascriptRack, :compressor => js_compressor, :ignore => ignore
# Setup Rack middlware to minify JS
use MinifyJavascriptRack, :compressor => js_compressor,
:ignore => ignore,
:inline => inline
end
end
alias :included :registered
end
# Rack middleware to look for JS in HTML and compress it
class InlineJavascriptRack
# Rack middleware to look for JS and compress it
class MinifyJavascriptRack
# Init
# @param [Class] app
@ -37,6 +40,7 @@ module Middleman::Extensions
@app = app
@compressor = options[:compressor]
@ignore = options[:ignore]
@inline = options[:inline]
end
# Rack interface
@ -48,7 +52,7 @@ module Middleman::Extensions
path = env["PATH_INFO"]
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)
minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|