/usr/lib/ruby/vendor_ruby/autoprefixer-rails/processor.rb is in ruby-autoprefixer-rails 6.3.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | require 'pathname'
require 'execjs'
require 'json'
module AutoprefixerRails
# Ruby to JS wrapper for Autoprefixer processor instance
class Processor
def initialize(params = { })
@params = params || { }
end
# 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.
def process(css, opts = { })
opts = convert_options(opts)
apply_wrapper =
"(function(opts) {" +
"return eval(process.apply(this, opts));" +
"})"
params = params_with_browsers(opts[:from]).merge(opts)
result = runtime.call(apply_wrapper, [css, params])
Result.new(result['css'], result['map'], result['warnings'])
end
# Return, which browsers and prefixes will be used
def info
runtime.eval("autoprefixer(#{ js_params }).info()")
end
# Parse Browserslist config
def parse_config(config)
config.gsub(/#[^\n]*/, '')
.split(/\n/)
.map(&:strip)
.reject(&:empty?)
end
private
def params_with_browsers(from = nil)
unless from
if defined? Rails and Rails.respond_to?(:root) and Rails.root
from = Rails.root.join('app/assets/stylesheets').to_s
else
from = '.'
end
end
params = @params
if not params.has_key?(:browsers) and from
config = find_config(from)
if config
params = params.dup
params[:browsers] = parse_config(config)
end
end
params
end
# Convert params to JS string and add browsers from Browserslist config
def js_params
'{ ' +
params_with_browsers.map { |k, v| "#{k}: #{v.inspect}"}.join(', ') +
' }'
end
# Convert ruby_options to jsOptions
def convert_options(opts)
converted = { }
opts.each_pair do |name, value|
if name =~ /_/
name = name.to_s.gsub(/_\w/) { |i| i.gsub('_', '').upcase }.to_sym
end
value = convert_options(value) if value.is_a? Hash
converted[name] = value
end
converted
end
# Try to find Browserslist config
def find_config(file)
path = Pathname(file).expand_path.dirname
while path.parent != path
config = path.join('browserslist')
return config.read if config.exist? and not config.directory?
path = path.parent
end
nil
end
# Lazy load for JS library
def runtime
@runtime ||= begin
if ExecJS.eval('typeof(Array.prototype.map)') != 'function'
raise "Current ExecJS runtime does't support ES5. " +
"Please install node.js."
end
ExecJS.compile(build_js)
end
end
# Cache autoprefixer.js content
def read_js
@@js ||= Pathname(__FILE__).join("/usr/share/ruby-autoprefixer-rails/vendor/autoprefixer.js").read
end
# Return processor JS with some extra methods
def build_js
'var global = this;' + read_js + process_proxy
end
# Return JS code for process method proxy
def process_proxy
<<-JS
var processor;
var process = function() {
var result = autoprefixer.process.apply(autoprefixer, arguments);
var warns = result.warnings().map(function (i) {
delete i.plugin;
return i.toString();
});
var map = result.map ? result.map.toString() : null;
return { css: result.css, map: map, warnings: warns };
};
JS
end
end
end
|