2013-04-20 23:29:51 +02:00
|
|
|
# Minify Javascript Extension
|
|
|
|
class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
|
|
|
|
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'
|
2014-10-13 00:44:16 +02:00
|
|
|
option :content_types, %w(application/javascript), 'Content types of resources that contain JS'
|
|
|
|
option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline JS'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
app.config.define_setting :js_compressor, nil, 'Set the JS compressor to use. Deprecated in favor of the :compressor option when activating :minify_js'
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
def after_configuration
|
|
|
|
chosen_compressor = app.config[:js_compressor] || options[:compressor] || begin
|
|
|
|
require 'uglifier'
|
|
|
|
::Uglifier.new
|
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
# Setup Rack middleware to minify JS
|
2014-04-29 19:44:24 +02:00
|
|
|
app.use Rack, compressor: chosen_compressor,
|
|
|
|
ignore: Array(options[:ignore]) + [/\.min\./],
|
2014-10-13 00:44:16 +02:00
|
|
|
inline: options[:inline],
|
|
|
|
content_types: options[:content_types],
|
|
|
|
inline_content_types: options[:inline_content_types]
|
2013-04-20 23:29:51 +02:00
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
# Rack middleware to look for JS and compress it
|
|
|
|
class Rack
|
2014-10-13 00:44:16 +02:00
|
|
|
INLINE_JS_REGEX = /(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m
|
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
# Init
|
|
|
|
# @param [Class] app
|
|
|
|
# @param [Hash] options
|
|
|
|
def initialize(app, options={})
|
|
|
|
@app = app
|
|
|
|
@compressor = options[:compressor]
|
|
|
|
@ignore = options[:ignore]
|
|
|
|
@inline = options[:inline]
|
2014-10-13 00:44:16 +02:00
|
|
|
@content_types = options[:content_types]
|
|
|
|
@inline_content_types = options[:inline_content_types]
|
2013-04-20 23:29:51 +02:00
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2013-04-20 23:29:51 +02:00
|
|
|
# Rack interface
|
|
|
|
# @param [Rack::Environmemt] env
|
|
|
|
# @return [Array]
|
|
|
|
def call(env)
|
|
|
|
status, headers, response = @app.call(env)
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
type = headers['Content-Type'].try(:slice, /^[^;]*/)
|
2015-05-19 16:41:07 +02:00
|
|
|
@path = env['PATH_INFO']
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
minified = if @inline && minifiable_inline?(type)
|
|
|
|
minify_inline(::Middleman::Util.extract_response_text(response))
|
2015-05-19 16:41:07 +02:00
|
|
|
elsif minifiable?(type) && !ignore?(@path)
|
2014-10-13 00:44:16 +02:00
|
|
|
minify(::Middleman::Util.extract_response_text(response))
|
|
|
|
end
|
2013-04-20 23:29:51 +02:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
if minified
|
|
|
|
headers['Content-Length'] = ::Rack::Utils.bytesize(minified).to_s
|
|
|
|
response = [minified]
|
2012-04-08 05:00:24 +02:00
|
|
|
end
|
2013-04-20 23:29:51 +02:00
|
|
|
|
|
|
|
[status, headers, response]
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2014-04-29 01:02:18 +02:00
|
|
|
private
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
# Whether the path should be ignored
|
|
|
|
# @param [String] path
|
|
|
|
# @return [Boolean]
|
|
|
|
def ignore?(path)
|
|
|
|
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) }
|
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
# Whether this type of content can be minified
|
|
|
|
# @param [String, nil] content_type
|
|
|
|
# @return [Boolean]
|
|
|
|
def minifiable?(content_type)
|
|
|
|
@content_types.include?(content_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Whether this type of content contains inline content that can be minified
|
|
|
|
# @param [String, nil] content_type
|
|
|
|
# @return [Boolean]
|
|
|
|
def minifiable_inline?(content_type)
|
|
|
|
@inline_content_types.include?(content_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Minify the content
|
|
|
|
# @param [String] content
|
|
|
|
# @return [String]
|
|
|
|
def minify(content)
|
|
|
|
@compressor.compress(content)
|
|
|
|
rescue ExecJS::ProgramError => e
|
2015-05-19 16:41:07 +02:00
|
|
|
warn "WARNING: Couldn't compress JavaScript in #{@path}: #{e.message}"
|
2014-10-13 00:44:16 +02:00
|
|
|
content
|
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2014-10-13 00:44:16 +02:00
|
|
|
# Detect and minify inline content
|
|
|
|
# @param [String] content
|
|
|
|
# @return [String]
|
|
|
|
def minify_inline(content)
|
|
|
|
content.gsub(INLINE_JS_REGEX) do |match|
|
2015-05-16 22:21:12 +02:00
|
|
|
first = $1
|
|
|
|
inline_content = $2
|
|
|
|
last = $3
|
2014-10-13 00:44:16 +02:00
|
|
|
|
|
|
|
# Only compress script tags that contain JavaScript (as opposed to
|
|
|
|
# something like jQuery templates, identified with a "text/html" type).
|
|
|
|
if first.include?('<script>') || first.include?('text/javascript')
|
|
|
|
first + minify(inline_content) + last
|
2013-06-01 02:46:12 +02:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
2012-04-27 06:55:07 +02:00
|
|
|
end
|