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
# MinifyJavascript compresses JS
Middleman::Extensions.register(:minify_javascript) do
require "middleman-more/extensions/minify_javascript"
Middleman::Extensions::MinifyJavascript
end
Middleman::Extensions::MinifyJavascript.register
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.

View file

@ -1,34 +1,25 @@
# Extension namespace
module Middleman
module Extensions
# 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'
# Minify Javascript Extension
module MinifyJavascript
def initialize(app, options_hash={}, &block)
super
# Setup extension
class << self
# Once registered
def registered(app, options={})
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
ignore = Array(options[:ignore]) << /\.min\./
inline = options[:inline] || false
# Once config is parsed
app.after_configuration do
chosen_compressor = js_compressor || options[:compressor] || begin
def after_configuration
chosen_compressor = app.config[:js_compressor] || options[:compressor] || begin
require 'uglifier'
::Uglifier.new
end
# Setup Rack middlware to minify JS
use Rack, :compressor => chosen_compressor,
:ignore => ignore,
:inline => inline
end
end
alias :included :registered
# Setup Rack middleware to minify CSS
app.use Rack, :compressor => chosen_compressor,
:ignore => options[:ignore] + [/\.min\./],
:inline => options[:inline]
end
# Rack middleware to look for JS and compress it
@ -89,6 +80,4 @@ module Middleman
[status, headers, response]
end
end
end
end
end