update minify js extension

This commit is contained in:
Thomas Reynolds 2013-04-20 14:29:51 -07:00
parent 11a3507528
commit 1c4e6cb835
2 changed files with 72 additions and 85 deletions

View file

@ -59,10 +59,8 @@ module Middleman
Middleman::Extensions::MinifyCss.register Middleman::Extensions::MinifyCss.register
# MinifyJavascript compresses JS # MinifyJavascript compresses JS
Middleman::Extensions.register(:minify_javascript) do require "middleman-more/extensions/minify_javascript"
require "middleman-more/extensions/minify_javascript" Middleman::Extensions::MinifyJavascript.register
Middleman::Extensions::MinifyJavascript
end
# RelativeAssets allow any asset path in dynamic templates to be either # RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL. # relative to the root of the project or use an absolute URL.

View file

@ -1,94 +1,83 @@
# Extension namespace # Minify Javascript Extension
module Middleman class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
module Extensions option :compressor, nil, 'Set the JS compressor to use.'
option :inline, false, 'Whether to minify JS inline within HTML files'
option :ignore, [], 'Patterns to avoid minifying'
# Minify Javascript Extension def initialize(app, options_hash={}, &block)
module MinifyJavascript super
# Setup extension app.config.define_setting :js_compressor, nil, 'Set the JS compressor to use. Deprecated in favor of the :compressor option when activating :minify_js'
class << self end
# Once registered def after_configuration
def registered(app, options={}) chosen_compressor = app.config[:js_compressor] || options[:compressor] || begin
app.config.define_setting :js_compressor, nil, 'Set the JS compressor to use. Deprecated in favor of the :compressor option when activating :minify_js' require 'uglifier'
::Uglifier.new
end
ignore = Array(options[:ignore]) << /\.min\./ # Setup Rack middleware to minify CSS
inline = options[:inline] || false app.use Rack, :compressor => chosen_compressor,
:ignore => options[:ignore] + [/\.min\./],
:inline => options[:inline]
end
# Once config is parsed # Rack middleware to look for JS and compress it
app.after_configuration do class Rack
chosen_compressor = js_compressor || options[:compressor] || begin
require 'uglifier' # Init
::Uglifier.new # @param [Class] app
# @param [Hash] options
def initialize(app, options={})
@app = app
@compressor = options[:compressor]
@ignore = options[:ignore]
@inline = options[:inline]
end
# Rack interface
# @param [Rack::Environmemt] env
# @return [Array]
def call(env)
status, headers, response = @app.call(env)
path = env["PATH_INFO"]
begin
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
uncompressed_source = ::Middleman::Util.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
# 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
end end
# Setup Rack middlware to minify JS
use Rack, :compressor => chosen_compressor,
:ignore => ignore,
:inline => inline
end
end
alias :included :registered
end
# Rack middleware to look for JS and compress it
class Rack
# Init
# @param [Class] app
# @param [Hash] options
def initialize(app, options={})
@app = app
@compressor = options[:compressor]
@ignore = options[:ignore]
@inline = options[:inline]
end
# Rack interface
# @param [Rack::Environmemt] env
# @return [Array]
def call(env)
status, headers, response = @app.call(env)
path = env["PATH_INFO"]
begin
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
uncompressed_source = ::Middleman::Util.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
# 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
end
end
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
uncompressed_source = ::Middleman::Util.extract_response_text(response)
minified_js = @compressor.compress(uncompressed_source)
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 end
[status, headers, response] headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
response = [minified]
elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
uncompressed_source = ::Middleman::Util.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
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
end end
[status, headers, response]
end end
end end
end end