Class: Middleman::Autoprefixer::Extension::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-autoprefixer/extension.rb

Overview

Rack middleware to look for CSS and apply prefixes.

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Init

Parameters:

  • app (Class)
  • options (Hash) (defaults to: {})


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/middleman-autoprefixer/extension.rb', line 34

def initialize(app, options = {})
  @app = app
  @middleman_app = options.fetch(:middleman_app)
  @config = options.fetch(:config)
  @inline = @config[:inline]
  @ignore = @config[:ignore]

  @processor = ::AutoprefixerRails::Processor.new({
    browsers: @config[:browsers] && Array(@config[:browsers]),
    add:      @config[:add],
    remove:   @config[:remove],
    grid:     @config[:grid],
    supports: @config[:supports],
    flexbox:  @config[:flexbox]
  })
end

Instance Method Details

#call(env) ⇒ Array

Rack interface

Parameters:

  • env (Rack::Environmemt)

Returns:

  • (Array)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/middleman-autoprefixer/extension.rb', line 54

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

  type = headers.fetch('Content-Type', 'application/octet-stream').split(';').first
  path = env['PATH_INFO']

  prefixed = process(response, type, path)

  if prefixed.is_a?(String)
    headers['Content-Length'] = prefixed.bytesize.to_s
    response = [prefixed]
  end

  [status, headers, response]
end