Class: AutoprefixerRails::Processor

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

Overview

Ruby to JS wrapper for Autoprefixer processor instance

Constant Summary collapse

SUPPORTED_RUNTIMES =
[ExecJS::Runtimes::Node, ExecJS::Runtimes::MiniRacer]

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Processor

Returns a new instance of Processor.



14
15
16
# File 'lib/autoprefixer-rails/processor.rb', line 14

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

Instance Method Details

#infoObject

Return, which browsers and prefixes will be used



52
53
54
# File 'lib/autoprefixer-rails/processor.rb', line 52

def info
  runtime.call("autoprefixer.info", params_with_browsers)
end

#parse_config(config) ⇒ Object

Parse Browserslist config



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

def parse_config(config)
  sections = { "defaults" => [] }
  current  = "defaults"
  config.gsub(/#[^\n]*/, "")
        .split(/\n/)
        .map(&:strip)
        .reject(&:empty?)
        .each do |line|
    if IS_SECTION =~ line
      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.



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
# File 'lib/autoprefixer-rails/processor.rb', line 24

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

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

  begin
    result = runtime.call("autoprefixer.process", css, process_opts, plugin_opts)
  rescue ExecJS::ProgramError => e
    contry_error = "BrowserslistError: " \
      "Country statistics are 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