Class: AutoprefixerRails::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/autoprefixer-rails/processor.rb

Overview

Ruby to JS wrapper for Autoprefixer processor instance

Instance Method Summary collapse

Constructor Details

#initialize(params = { }) ⇒ Processor

Returns a new instance of Processor.



11
12
13
# File 'lib/autoprefixer-rails/processor.rb', line 11

def initialize(params = { })
  @params = params || { }
end

Instance Method Details

#infoObject

Return, which browsers and prefixes will be used



54
55
56
# File 'lib/autoprefixer-rails/processor.rb', line 54

def info
  runtime.eval("autoprefixer(#{ js_params }).info()")
end

#parse_config(config) ⇒ Object

Parse Browserslist config



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/autoprefixer-rails/processor.rb', line 59

def parse_config(config)
  sections = { 'defaults' => [] }
  current  = 'defaults'
  config.gsub(/#[^\n]*/, '')
        .split(/\n/)
        .map(&:strip)
        .reject(&:empty?)
        .each do |line|
          if line =~ IS_SECTION
            current = line.match(IS_SECTION)[1].strip
            sections[current] ||= []
          else
            sections[current] << line
          end
        end
  sections
end

#process(css, opts = { }) ⇒ Object

Process ‘css` and return result.

Options can be:

  • ‘from` with input CSS file name. Will be used in error messages.

  • ‘to` with output CSS file name.

  • ‘map` with true to generate new source map or with previous map.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/autoprefixer-rails/processor.rb', line 21

def process(css, opts = { })
  opts = convert_options(opts)

  apply_wrapper =
    "(function(opts, pluginOpts) {" +
    "return eval(process.apply(this, opts, pluginOpts));" +
    "})"

  pluginOpts = params_with_browsers(opts[:from]).merge(opts)
  processOpts = {
    from: pluginOpts.delete(:from),
    to:   pluginOpts.delete(:to),
    map:  pluginOpts.delete(:map)
  }

  begin
    result = runtime.call(apply_wrapper, [css, processOpts, pluginOpts])
  rescue ExecJS::ProgramError => e
    contry_error = 'BrowserslistError: ' +
      'Country statistics is not supported ' +
      'in client-side build of Browserslist'
    if e.message == contry_error
      raise 'Country statistics is not supported in AutoprefixerRails. ' +
            'Use Autoprefixer with webpack or other Node.js builder.'
    else
      raise e
    end
  end

  Result.new(result['css'], result['map'], result['warnings'])
end