| 1 |
|
| 2 |
|
| 3 |
require "pathname" |
| 4 |
require "execjs" |
| 5 |
require "json" |
| 6 |
|
| 7 |
module AutoprefixerRails |
| 8 |
IS_SECTION = /^\s*\[(.+)\]\s*$/.freeze |
| 9 |
|
| 10 |
|
| 11 |
class Processor |
| 12 |
SUPPORTED_RUNTIMES = [ExecJS::Runtimes::Node, ExecJS::Runtimes::MiniRacer] |
| 13 |
|
| 14 |
def initialize(params = {}) |
| 15 |
@params = params || {} |
| 16 |
end |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
def process(css, opts = {}) |
| 25 |
opts = convert_options(opts) |
| 26 |
|
| 27 |
plugin_opts = params_with_browsers(opts[:from]).merge(opts) |
| 28 |
process_opts = { |
| 29 |
from: plugin_opts.delete(:from), |
| 30 |
to: plugin_opts.delete(:to), |
| 31 |
map: plugin_opts.delete(:map) |
| 32 |
} |
| 33 |
|
| 34 |
begin |
| 35 |
result = runtime.call("autoprefixer.process", css, process_opts, plugin_opts) |
| 36 |
rescue ExecJS::ProgramError => e |
| 37 |
contry_error = "BrowserslistError: " \ |
| 38 |
"Country statistics are not supported " \ |
| 39 |
"in client-side build of Browserslist" |
| 40 |
if e.message == contry_error |
| 41 |
raise "Country statistics is not supported in AutoprefixerRails. " \ |
| 42 |
"Use Autoprefixer with webpack or other Node.js builder." |
| 43 |
else |
| 44 |
raise e |
| 45 |
end |
| 46 |
end |
| 47 |
|
| 48 |
Result.new(result["css"], result["map"], result["warnings"]) |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
def info |
| 53 |
runtime.call("autoprefixer.info", params_with_browsers) |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
def parse_config(config) |
| 58 |
sections = { "defaults" => [] } |
| 59 |
current = "defaults" |
| 60 |
config.gsub(/#[^\n]*/, "") |
| 61 |
.split(/\n/) |
| 62 |
.map(&:strip) |
| 63 |
.reject(&:empty?) |
| 64 |
.each do |line| |
| 65 |
if IS_SECTION =~ line |
| 66 |
current = line.match(IS_SECTION)[1].strip |
| 67 |
sections[current] ||= [] |
| 68 |
else |
| 69 |
sections[current] << line |
| 70 |
end |
| 71 |
end |
| 72 |
sections |
| 73 |
end |
| 74 |
|
| 75 |
private |
| 76 |
|
| 77 |
def params_with_browsers(from = nil) |
| 78 |
from ||= if defined?(Rails) && Rails.respond_to?(:root) && Rails.root |
| 79 |
Rails.root.join("app/assets/stylesheets").to_s |
| 80 |
else |
| 81 |
"." |
| 82 |
end |
| 83 |
|
| 84 |
params = @params |
| 85 |
if !params.key?(:browsers) && !params.key?(:overrideBrowserslist) && from |
| 86 |
file = find_config(from) |
| 87 |
if file |
| 88 |
env = params[:env].to_s || "development" |
| 89 |
config = parse_config(file) |
| 90 |
params = params.dup |
| 91 |
params[:overrideBrowserslist] = (config[env] || config["defaults"]) |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
params |
| 96 |
end |
| 97 |
|
| 98 |
|
| 99 |
def convert_options(opts) |
| 100 |
converted = {} |
| 101 |
|
| 102 |
opts.each_pair do |name, value| |
| 103 |
if /_/ =~ name |
| 104 |
name = name.to_s.gsub(/_\w/) { |i| i.delete("_").upcase }.to_sym |
| 105 |
end |
| 106 |
value = convert_options(value) if value.is_a? Hash |
| 107 |
converted[name] = value |
| 108 |
end |
| 109 |
|
| 110 |
converted |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
def find_config(file) |
| 115 |
path = Pathname(file).expand_path |
| 116 |
|
| 117 |
while path.parent != path |
| 118 |
config1 = path.join("browserslist") |
| 119 |
return config1.read if config1.exist? && !config1.directory? |
| 120 |
|
| 121 |
config2 = path.join(".browserslistrc") |
| 122 |
return config2.read if config2.exist? && !config1.directory? |
| 123 |
|
| 124 |
path = path.parent |
| 125 |
end |
| 126 |
|
| 127 |
nil |
| 128 |
end |
| 129 |
|
| 130 |
|
| 131 |
def runtime |
| 132 |
@runtime ||= begin |
| 133 |
ExecJS.compile(build_js) |
| 134 |
rescue ExecJS::RuntimeError |
| 135 |
|
| 136 |
|
| 137 |
case ExecJS.runtime |
| 138 |
when ExecJS::Runtimes::Node |
| 139 |
node_command = ExecJS.runtime.send(:binary) rescue "Unknown" |
| 140 |
|
| 141 |
raise <<~MSG |
| 142 |
Your nodejs binary failed to load autoprefixer script file, |
| 143 |
please check if you're running a supported version (10, 12, 14+) |
| 144 |
|
| 145 |
ENV["PATH"] = #{ENV["PATH"]} |
| 146 |
binary = #{node_command} |
| 147 |
MSG |
| 148 |
when *SUPPORTED_RUNTIMES |
| 149 |
raise |
| 150 |
else |
| 151 |
raise <<~MSG |
| 152 |
Your ExecJS runtime #{ExecJS.runtime.name} isn't supported by autoprefixer-rails, |
| 153 |
please switch to #{SUPPORTED_RUNTIMES.map(&:name).join(' or ')} |
| 154 |
MSG |
| 155 |
end |
| 156 |
end |
| 157 |
end |
| 158 |
|
| 159 |
def build_js |
| 160 |
root = Pathname(File.dirname(__FILE__)) |
| 161 |
path = root.join("../../vendor/autoprefixer.js") |
| 162 |
path.read |
| 163 |
end |
| 164 |
end |
| 165 |
end |