2011-12-31 23:28:17 +01:00
|
|
|
# Extension namespace
|
2012-04-27 00:57:37 +02:00
|
|
|
module Middleman
|
|
|
|
module Extensions
|
2011-12-31 23:28:17 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Minify Javascript Extension
|
|
|
|
module MinifyJavascript
|
2011-12-31 23:28:17 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Setup extension
|
|
|
|
class << self
|
2011-12-31 23:28:17 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Once registered
|
|
|
|
def registered(app, options={})
|
|
|
|
app.set :js_compressor, false
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
ignore = Array(options[:ignore]) << /\.min\./
|
|
|
|
inline = options[:inline] || false
|
2012-04-24 06:39:00 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Once config is parsed
|
|
|
|
app.after_configuration do
|
|
|
|
chosen_compressor = js_compressor || options[:compressor] || begin
|
|
|
|
require 'uglifier'
|
|
|
|
::Uglifier.new
|
|
|
|
end
|
2011-12-11 05:28:37 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Setup Rack middlware to minify JS
|
|
|
|
use Rack, :compressor => chosen_compressor,
|
|
|
|
:ignore => ignore,
|
|
|
|
:inline => inline
|
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
2012-04-27 00:57:37 +02:00
|
|
|
alias :included :registered
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Rack middleware to look for JS and compress it
|
|
|
|
class Rack
|
2011-12-31 23:28:17 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Init
|
|
|
|
# @param [Class] app
|
|
|
|
# @param [Hash] options
|
|
|
|
def initialize(app, options={})
|
|
|
|
@app = app
|
|
|
|
@compressor = options[:compressor]
|
|
|
|
@ignore = options[:ignore]
|
|
|
|
@inline = options[:inline]
|
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# Rack interface
|
|
|
|
# @param [Rack::Environmemt] env
|
|
|
|
# @return [Array]
|
|
|
|
def call(env)
|
|
|
|
status, headers, response = @app.call(env)
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
path = env["PATH_INFO"]
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
begin
|
|
|
|
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
2012-04-27 18:20:38 +02:00
|
|
|
uncompressed_source = ::Middleman::Util.extract_response_text(response)
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
|
|
|
|
first = $1
|
|
|
|
javascript = $2
|
|
|
|
last = $3
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
# 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)
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
first << minified_js << last
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
2012-04-24 06:39:00 +02:00
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
|
|
|
response = [minified]
|
2012-06-17 05:23:33 +02:00
|
|
|
elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
|
2012-04-27 18:20:38 +02:00
|
|
|
uncompressed_source = ::Middleman::Util.extract_response_text(response)
|
2012-04-27 00:57:37 +02:00
|
|
|
minified_js = @compressor.compress(uncompressed_source)
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
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
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2012-04-27 00:57:37 +02:00
|
|
|
[status, headers, response]
|
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
|
|
|
end
|
2012-04-27 06:55:07 +02:00
|
|
|
end
|