Minor minify_javascript cleanup
This commit is contained in:
parent
2292c32773
commit
8bf0124241
|
@ -1,112 +1,111 @@
|
||||||
# Extension namespace
|
# Extension namespace
|
||||||
module Middleman::Extensions
|
module Middleman
|
||||||
|
module Extensions
|
||||||
|
|
||||||
# Minify Javascript Extension
|
# Minify Javascript Extension
|
||||||
module MinifyJavascript
|
module MinifyJavascript
|
||||||
|
|
||||||
# Setup extension
|
# Setup extension
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
# Once registered
|
# Once registered
|
||||||
def registered(app, options={})
|
def registered(app, options={})
|
||||||
app.set :js_compressor, false
|
app.set :js_compressor, false
|
||||||
|
|
||||||
ignore = Array(options[:ignore]) << /\.min\./
|
ignore = Array(options[:ignore]) << /\.min\./
|
||||||
inline = options[:inline] || false
|
inline = options[:inline] || false
|
||||||
|
|
||||||
# Once config is parsed
|
# Once config is parsed
|
||||||
app.after_configuration do
|
app.after_configuration do
|
||||||
chosen_compressor = js_compressor || options[:compressor] || begin
|
chosen_compressor = js_compressor || options[:compressor] || begin
|
||||||
require 'uglifier'
|
require 'uglifier'
|
||||||
::Uglifier.new
|
::Uglifier.new
|
||||||
end
|
|
||||||
|
|
||||||
# Setup Rack middlware to minify JS
|
|
||||||
use MinifyJavascriptRack, :compressor => chosen_compressor,
|
|
||||||
:ignore => ignore,
|
|
||||||
:inline => inline
|
|
||||||
end
|
|
||||||
end
|
|
||||||
alias :included :registered
|
|
||||||
end
|
|
||||||
|
|
||||||
# Rack middleware to look for JS and compress it
|
|
||||||
class MinifyJavascriptRack
|
|
||||||
|
|
||||||
# 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 = 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
|
end
|
||||||
|
|
||||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
# Setup Rack middlware to minify JS
|
||||||
response = [minified]
|
use Rack, :compressor => chosen_compressor,
|
||||||
elsif path.end_with?('.js') && @ignore.none? {|ignore| path =~ ignore }
|
:ignore => ignore,
|
||||||
uncompressed_source = extract_response_text(response)
|
:inline => inline
|
||||||
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
|
end
|
||||||
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
|
alias :included :registered
|
||||||
end
|
|
||||||
|
|
||||||
[status, headers, response]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
# 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
|
||||||
|
|
||||||
def extract_response_text(response)
|
# Rack interface
|
||||||
case(response)
|
# @param [Rack::Environmemt] env
|
||||||
when String
|
# @return [Array]
|
||||||
response
|
def call(env)
|
||||||
when Array
|
status, headers, response = @app.call(env)
|
||||||
response.join
|
|
||||||
when Rack::Response
|
path = env["PATH_INFO"]
|
||||||
response.body.join
|
|
||||||
when Rack::File
|
begin
|
||||||
File.read(response.path)
|
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
||||||
else
|
uncompressed_source = extract_response_text(response)
|
||||||
response.to_s
|
|
||||||
|
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| 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
|
||||||
|
rescue ExecJS::ProgramError => e
|
||||||
|
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
|
||||||
|
end
|
||||||
|
|
||||||
|
[status, headers, response]
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def extract_response_text(response)
|
||||||
|
case(response)
|
||||||
|
when String
|
||||||
|
response
|
||||||
|
when Array
|
||||||
|
response.join
|
||||||
|
when ::Rack::Response
|
||||||
|
response.body.join
|
||||||
|
when ::Rack::File
|
||||||
|
File.read(response.path)
|
||||||
|
else
|
||||||
|
response.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
# Register extension
|
|
||||||
# register :minify_javascript, MinifyJavascript
|
|
||||||
end
|
|
Loading…
Reference in a new issue