From d2bfb502fcace5214bd10b363eb4da90e26e08b1 Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Tue, 15 Sep 2020 20:24:11 -0400 Subject: [PATCH] Temporary revert Processor changes --- lib/autoprefixer-rails/processor.rb | 63 ++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/lib/autoprefixer-rails/processor.rb b/lib/autoprefixer-rails/processor.rb index 6fc26fc..079daaa 100644 --- a/lib/autoprefixer-rails/processor.rb +++ b/lib/autoprefixer-rails/processor.rb @@ -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 = {}) @@ -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), @@ -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. " \ @@ -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 @@ -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 = {} @@ -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