Skip to content

Commit

Permalink
Temporary revert Processor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 16, 2020
1 parent abd48f3 commit d2bfb50
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions lib/autoprefixer-rails/processor.rb
Expand Up @@ -7,6 +7,19 @@
IS_SECTION = /^\s*\[(.+)\]\s*$/.freeze

module AutoprefixerRails
def self.show_deprecation_message!
return unless defined?(ActiveSupport::Deprecation)

return if defined?(@deprecation_shown)

ActiveSupport::Deprecation.warn(
"autoprefixer-rails was deprected. Migration guide:\n" \
"https://github.com/ai/autoprefixer-rails/wiki/Deprecated"
)

@deprecation_shown = true
end

# Ruby to JS wrapper for Autoprefixer processor instance
class Processor
def initialize(params = {})
Expand All @@ -20,8 +33,15 @@ def initialize(params = {})
# * `to` with output CSS file name.
# * `map` with true to generate new source map or with previous map.
def process(css, opts = {})
AutoprefixerRails.show_deprecation_message!

opts = convert_options(opts)

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

plugin_opts = params_with_browsers(opts[:from]).merge(opts)
process_opts = {
from: plugin_opts.delete(:from),
Expand All @@ -30,10 +50,10 @@ def process(css, opts = {})
}

begin
result = runtime.call("autoprefixer.process", css, process_opts, plugin_opts)
result = runtime.call(apply_wrapper, [css, process_opts, plugin_opts])
rescue ExecJS::ProgramError => e
contry_error = "BrowserslistError: " \
"Country statistics are not supported " \
"Country statistics is not supported " \
"in client-side build of Browserslist"
if e.message == contry_error
raise "Country statistics is not supported in AutoprefixerRails. " \
Expand All @@ -48,7 +68,7 @@ def process(css, opts = {})

# Return, which browsers and prefixes will be used
def info
runtime.call("autoprefixer.info", params_with_browsers)
runtime.eval("autoprefixer(#{js_params}).info()")
end

# Parse Browserslist config
Expand Down Expand Up @@ -93,6 +113,13 @@ def params_with_browsers(from = nil)
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 = {}
Expand Down Expand Up @@ -149,10 +176,34 @@ def runtime
end
end

# Cache autoprefixer.js content
def read_js
@read_js ||= begin
root = Pathname(File.dirname(__FILE__))
path = root.join("../../vendor/autoprefixer.js")
path.read
end
end

# Return processor JS with some extra methods
def build_js
root = Pathname(File.dirname(__FILE__))
path = root.join("../../vendor/autoprefixer.js")
path.read
"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

0 comments on commit d2bfb50

Please sign in to comment.