Class: Middleman::Extensions::MinifyJavascript::Rack
- Inherits:
-
Object
- Object
- Middleman::Extensions::MinifyJavascript::Rack
- Extended by:
- Memoist
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/extensions/minify_javascript.rb
Overview
Rack middleware to look for JS and compress it
Constant Summary collapse
- INLINE_JS_REGEX =
/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m
Constants included from Contracts
Instance Method Summary collapse
-
#call(env) ⇒ Array
Rack interface.
-
#initialize(app, options = {}) ⇒ Rack
constructor
A new instance of Rack.
Methods included from Contracts
Constructor Details
#initialize(app, options = {}) ⇒ Rack
Returns a new instance of Rack.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/middleman-core/extensions/minify_javascript.rb', line 39 def initialize(app, ={}) @app = app @ignore = .fetch(:ignore) @inline = .fetch(:inline) @compressor = .fetch(:compressor) @compressor = @compressor.to_proc if @compressor.respond_to? :to_proc @compressor = @compressor.call if @compressor.is_a? Proc @content_types = [:content_types] @inline_content_types = [:inline_content_types] end |
Instance Method Details
#call(env) ⇒ Array
Rack interface
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 |