class Middleman::Extensions::MinifyJavascript::Rack
Rack
middleware to look for JS and compress it
Constants
- INLINE_JS_REGEX
Public Class Methods
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 39 def initialize(app, options={}) @app = app @ignore = options.fetch(:ignore) @inline = options.fetch(:inline) @compressor = options.fetch(:compressor) @compressor = @compressor.to_proc if @compressor.respond_to? :to_proc @compressor = @compressor.call if @compressor.is_a? Proc @content_types = options[:content_types] @inline_content_types = options[:inline_content_types] end
Public Instance Methods
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 54 def call(env) status, headers, response = @app.call(env) type = headers['Content-Type'].try(:slice, /^[^;]*/) @path = env['PATH_INFO'] minified = if @inline && minifiable_inline?(type) minify_inline(::Middleman::Util.extract_response_text(response)) elsif minifiable?(type) && !ignore?(@path) minify(::Middleman::Util.extract_response_text(response)) end if minified headers['Content-Length'] = minified.bytesize.to_s response = [minified] end [status, headers, response] end
Rack
interface @param [Rack::Environmemt] env @return [Array]
Private Instance Methods
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 79 def ignore?(path) @ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) } end
Whether the path should be ignored @param [String] path @return [Boolean]
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 87 def minifiable?(content_type) @content_types.include?(content_type) end
Whether this type of content can be minified @param [String, nil] content_type @return [Boolean]
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 95 def minifiable_inline?(content_type) @inline_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]
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 103 def minify(content) @compressor.compress(content) rescue ExecJS::ProgramError => e warn "WARNING: Couldn't compress JavaScript in #{@path}: #{e.message}" content end
Minify the content @param [String] content @return [String]
Source
# File lib/middleman-core/extensions/minify_javascript.rb, line 114 def minify_inline(content) content.gsub(INLINE_JS_REGEX) do |match| first = $1 inline_content = $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.include?('type=') || first.include?('text/javascript') first + minify(inline_content) + last else match end end end
Detect and minify inline content @param [String] content @return [String]