Class: Middleman::Extensions::MinifyJavascript::Rack

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/extensions/minify_javascript.rb

Overview

Rack middleware to look for JS and compress it

Constant Summary

INLINE_JS_REGEX =
/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options = {}) ⇒ Rack

Returns a new instance of Rack



39
40
41
42
43
44
45
46
47
48
49
# File 'middleman-core/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

Instance Method Details

#call(env) ⇒ Array

Rack interface

Parameters:

  • env (Rack::Environmemt)

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'middleman-core/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