middleman/middleman-more/lib/middleman-more/extensions/minify_javascript.rb

111 lines
3.3 KiB
Ruby
Raw Normal View History

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-27 00:57:37 +02:00
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
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
2012-04-27 00:57:37 +02:00
# Setup Rack middlware to minify JS
use Rack, :compressor => chosen_compressor,
:ignore => ignore,
:inline => inline
end
end
2012-04-27 00:57:37 +02:00
alias :included :registered
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
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)
2012-04-27 00:57:37 +02:00
path = env["PATH_INFO"]
2012-04-27 00:57:37 +02:00
begin
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
uncompressed_source = extract_response_text(response)
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
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-27 00:57:37 +02:00
first << minified_js << last
else
match
end
end
2012-04-27 00:57:37 +02:00
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)
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
2012-04-27 00:57:37 +02:00
[status, headers, response]
end
2012-04-27 00:57:37 +02:00
private
2012-04-27 00:57:37 +02:00
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
2012-04-27 00:57:37 +02:00
end