Class: Middleman::Extensions::MinifyCss::Rack

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

Overview

Rack middleware to look for CSS and compress it

Constant Summary collapse

INLINE_CSS_REGEX =
/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

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

Returns a new instance of Rack.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/middleman-core/extensions/minify_css.rb', line 49

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:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/middleman-core/extensions/minify_css.rb', line 64

def call(env)
  status, headers, response = @app.call(env)

  content_type = headers['Content-Type'].try(:slice, /^[^;]*/)
  path = env['PATH_INFO']

  minified = if @inline && minifiable_inline?(content_type)
    minify_inline(::Middleman::Util.extract_response_text(response))
  elsif minifiable?(content_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