autoprefixer-rails 8.6.5 → 9.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,14 @@
1
- require 'pathname'
2
- require 'execjs'
3
- require 'json'
1
+ require "pathname"
2
+ require "execjs"
3
+ require "json"
4
4
 
5
5
  IS_SECTION = /^\s*\[(.+)\]\s*$/
6
6
 
7
7
  module AutoprefixerRails
8
8
  # Ruby to JS wrapper for Autoprefixer processor instance
9
9
  class Processor
10
-
11
- def initialize(params = { })
12
- @params = params || { }
10
+ def initialize(params = {})
11
+ @params = params || {}
13
12
  end
14
13
 
15
14
  # Process `css` and return result.
@@ -18,85 +17,79 @@ module AutoprefixerRails
18
17
  # * `from` with input CSS file name. Will be used in error messages.
19
18
  # * `to` with output CSS file name.
20
19
  # * `map` with true to generate new source map or with previous map.
21
- def process(css, opts = { })
20
+ def process(css, opts = {})
22
21
  opts = convert_options(opts)
23
22
 
24
23
  apply_wrapper =
25
- "(function(opts, pluginOpts) {" +
26
- "return eval(process.apply(this, opts, pluginOpts));" +
24
+ "(function(opts, pluginOpts) {" \
25
+ "return eval(process.apply(this, opts, pluginOpts));" \
27
26
  "})"
28
27
 
29
- pluginOpts = params_with_browsers(opts[:from]).merge(opts)
30
- processOpts = {
31
- from: pluginOpts.delete(:from),
32
- to: pluginOpts.delete(:to),
33
- map: pluginOpts.delete(:map)
28
+ plugin_opts = params_with_browsers(opts[:from]).merge(opts)
29
+ process_opts = {
30
+ from: plugin_opts.delete(:from),
31
+ to: plugin_opts.delete(:to),
32
+ map: plugin_opts.delete(:map),
34
33
  }
35
34
 
36
35
  begin
37
- result = runtime.call(apply_wrapper, [css, processOpts, pluginOpts])
36
+ result = runtime.call(apply_wrapper, [css, process_opts, plugin_opts])
38
37
  rescue ExecJS::ProgramError => e
39
- contry_error = 'BrowserslistError: ' +
40
- 'Country statistics is not supported ' +
41
- 'in client-side build of Browserslist'
38
+ contry_error = "BrowserslistError: " \
39
+ "Country statistics is not supported " \
40
+ "in client-side build of Browserslist"
42
41
  if e.message == contry_error
43
- raise 'Country statistics is not supported in AutoprefixerRails. ' +
44
- 'Use Autoprefixer with webpack or other Node.js builder.'
42
+ raise "Country statistics is not supported in AutoprefixerRails. " \
43
+ "Use Autoprefixer with webpack or other Node.js builder."
45
44
  else
46
45
  raise e
47
46
  end
48
47
  end
49
48
 
50
- Result.new(result['css'], result['map'], result['warnings'])
49
+ Result.new(result["css"], result["map"], result["warnings"])
51
50
  end
52
51
 
53
52
  # Return, which browsers and prefixes will be used
54
53
  def info
55
- runtime.eval("autoprefixer(#{ js_params }).info()")
54
+ runtime.eval("autoprefixer(#{js_params}).info()")
56
55
  end
57
56
 
58
57
  # Parse Browserslist config
59
58
  def parse_config(config)
60
- sections = { 'defaults' => [] }
61
- current = 'defaults'
62
- config.gsub(/#[^\n]*/, '')
63
- .split(/\n/)
64
- .map(&:strip)
65
- .reject(&:empty?)
66
- .each do |line|
67
- if line =~ IS_SECTION
68
- current = line.match(IS_SECTION)[1].strip
69
- sections[current] ||= []
70
- else
71
- sections[current] << line
72
- end
73
- end
59
+ sections = {"defaults" => []}
60
+ current = "defaults"
61
+ config.gsub(/#[^\n]*/, "")
62
+ .split(/\n/)
63
+ .map(&:strip)
64
+ .reject(&:empty?)
65
+ .each do |line|
66
+ if IS_SECTION =~ line
67
+ current = line.match(IS_SECTION)[1].strip
68
+ sections[current] ||= []
69
+ else
70
+ sections[current] << line
71
+ end
72
+ end
74
73
  sections
75
74
  end
76
75
 
77
76
  private
78
77
 
79
78
  def params_with_browsers(from = nil)
80
- unless from
81
- if defined? Rails and Rails.respond_to?(:root) and Rails.root
82
- from = Rails.root.join('app/assets/stylesheets').to_s
83
- else
84
- from = '.'
85
- end
79
+ from ||= if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
80
+ Rails.root.join("app/assets/stylesheets").to_s
81
+ else
82
+ "."
86
83
  end
87
84
 
88
85
  params = @params
89
- if not params.has_key?(:browsers) and from
86
+ if !params.key?(:browsers) && !params.key?(:overrideBrowserslist) && from
90
87
  file = find_config(from)
91
88
  if file
92
- env = params[:env].to_s || 'development'
89
+ env = params[:env].to_s || "development"
93
90
  config = parse_config(file)
94
91
  params = params.dup
95
- if config[env]
96
- params[:browsers] = config[env]
97
- else
98
- params[:browsers] = config['defaults']
99
- end
92
+ params[:overrideBrowserslist] = (config[env] || config["defaults"])
100
93
  end
101
94
  end
102
95
 
@@ -105,18 +98,18 @@ module AutoprefixerRails
105
98
 
106
99
  # Convert params to JS string and add browsers from Browserslist config
107
100
  def js_params
108
- '{ ' +
109
- params_with_browsers.map { |k, v| "#{k}: #{v.inspect}"}.join(', ') +
110
- ' }'
101
+ "{ " +
102
+ params_with_browsers.map { |k, v| "#{k}: #{v.inspect}"}.join(", ") +
103
+ " }"
111
104
  end
112
105
 
113
106
  # Convert ruby_options to jsOptions
114
107
  def convert_options(opts)
115
- converted = { }
108
+ converted = {}
116
109
 
117
110
  opts.each_pair do |name, value|
118
- if name =~ /_/
119
- name = name.to_s.gsub(/_\w/) { |i| i.gsub('_', '').upcase }.to_sym
111
+ if /_/ =~ name
112
+ name = name.to_s.gsub(/_\w/) { |i| i.delete("_").upcase }.to_sym
120
113
  end
121
114
  value = convert_options(value) if value.is_a? Hash
122
115
  converted[name] = value
@@ -130,11 +123,11 @@ module AutoprefixerRails
130
123
  path = Pathname(file).expand_path
131
124
 
132
125
  while path.parent != path
133
- config1 = path.join('browserslist')
134
- return config1.read if config1.exist? and not config1.directory?
126
+ config1 = path.join("browserslist")
127
+ return config1.read if config1.exist? && !config1.directory?
135
128
 
136
- config2 = path.join('.browserslistrc')
137
- return config2.read if config2.exist? and not config1.directory?
129
+ config2 = path.join(".browserslistrc")
130
+ return config2.read if config2.exist? && !config1.directory?
138
131
 
139
132
  path = path.parent
140
133
  end
@@ -145,9 +138,23 @@ module AutoprefixerRails
145
138
  # Lazy load for JS library
146
139
  def runtime
147
140
  @runtime ||= begin
148
- if ExecJS.eval('typeof(Array.prototype.map)') != 'function'
149
- raise "Current ExecJS runtime does't support ES5. " +
150
- "Please install node.js."
141
+ if ExecJS.eval("typeof Uint8Array") != "function"
142
+ if ExecJS.runtime.name.start_with?("therubyracer")
143
+ raise "ExecJS::RubyRacerRuntime is not supported. " \
144
+ "Please replace therubyracer with mini_racer " \
145
+ "in your Gemfile or use Node.js as ExecJS runtime."
146
+ else
147
+ raise "#{ExecJS.runtime.name} runtime does’t support ES6. " \
148
+ "Please update or replace your current ExecJS runtime."
149
+ end
150
+ end
151
+
152
+ if ExecJS.runtime == ExecJS::Runtimes::Node
153
+ version = ExecJS.runtime.eval("process.version")
154
+ first = version.match(/^v(\d+)/)[1].to_i
155
+ if first < 6
156
+ raise "Autoprefixer doesn’t support Node #{version}. Update it."
157
+ end
151
158
  end
152
159
 
153
160
  ExecJS.compile(build_js)
@@ -159,90 +166,13 @@ module AutoprefixerRails
159
166
  @@js ||= begin
160
167
  root = Pathname(File.dirname(__FILE__))
161
168
  path = root.join("../../vendor/autoprefixer.js")
162
- path.read.gsub(/Object.setPrototypeOf\(chalk[^)]+\)/, '')
169
+ path.read
163
170
  end
164
171
  end
165
172
 
166
- def polyfills
167
- <<-JS
168
- if (typeof Uint8Array === "undefined")
169
- global.Uint8Array = Array;
170
- if (typeof ArrayBuffer === "undefined")
171
- global.ArrayBuffer = Array;
172
- if (typeof Set === "undefined") {
173
- global.Set = function (values) { this.values = values }
174
- global.Set.prototype = {
175
- has: function (i) { return this.values.indexOf(i) !== -1 }
176
- }
177
- }
178
- if (typeof Map === "undefined") {
179
- global.Map = function () { this.data = { } }
180
- global.Map.prototype = {
181
- set: function (k, v) { this.data[k] = v },
182
- get: function (k) { return this.data[k] },
183
- has: function (k) {
184
- return Object.keys(this.data).indexOf(k) !== -1
185
- },
186
- }
187
- }
188
- Math.log2 = Math.log2 ||
189
- function(x) { return Math.log(x) * Math.LOG2E; };
190
- Math.sign = Math.sign ||
191
- function(x) {
192
- x = +x;
193
- if (x === 0 || isNaN(x)) return Number(x);
194
- return x > 0 ? 1 : -1;
195
- };
196
- Array.prototype.fill = Array.prototype.fill ||
197
- function(value) {
198
- var O = Object(this);
199
- var len = O.length >>> 0;
200
- var start = arguments[1];
201
- var relativeStart = start >> 0;
202
- var k = relativeStart < 0 ?
203
- Math.max(len + relativeStart, 0) :
204
- Math.min(relativeStart, len);
205
- var end = arguments[2];
206
- var relativeEnd = end === undefined ?
207
- len : end >> 0;
208
- var final = relativeEnd < 0 ?
209
- Math.max(len + relativeEnd, 0) :
210
- Math.min(relativeEnd, len);
211
- while (k < final) {
212
- O[k] = value;
213
- k++;
214
- }
215
- return O;
216
- };
217
- if (!Object.assign) {
218
- Object.assign = function(target, firstSource) {
219
- var to = Object(target);
220
- for (var i = 1; i < arguments.length; i++) {
221
- var nextSource = arguments[i];
222
- if (nextSource === undefined || nextSource === null) continue;
223
- var keysArray = Object.keys(Object(nextSource));
224
- for (var n = 0, len = keysArray.length; n < len; n++) {
225
- var nextKey = keysArray[n];
226
- var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
227
- if (desc !== undefined && desc.enumerable) {
228
- to[nextKey] = nextSource[nextKey];
229
- }
230
- }
231
- }
232
- return to;
233
- }
234
- }
235
- if (!Array.isView) {
236
- Array.isView = function () {
237
- return false
238
- }
239
- }
240
- JS
241
- end
242
-
243
173
  # Return processor JS with some extra methods
244
174
  def build_js
245
- 'var global = this;' + polyfills + read_js + process_proxy
175
+ "var global = this;" + read_js + process_proxy
246
176
  end
247
177
 
248
178
  # Return JS code for process method proxy
@@ -1,20 +1,20 @@
1
- require 'yaml'
1
+ require "yaml"
2
2
 
3
3
  begin
4
4
  module AutoprefixedRails
5
5
  class Railtie < ::Rails::Railtie
6
6
  rake_tasks do |app|
7
- require 'rake/autoprefixer_tasks'
8
- Rake::AutoprefixerTasks.new( config ) if defined? app.assets
7
+ require "rake/autoprefixer_tasks"
8
+ Rake::AutoprefixerTasks.new(config) if defined? app.assets
9
9
  end
10
10
 
11
- if config.respond_to?(:assets) and not config.assets.nil?
11
+ if config.respond_to?(:assets) && !config.assets.nil?
12
12
  config.assets.configure do |env|
13
13
  AutoprefixerRails.install(env, config)
14
14
  end
15
15
  else
16
16
  initializer :setup_autoprefixer, group: :all do |app|
17
- if defined? app.assets and not app.assets.nil?
17
+ if defined?(app.assets) && !app.assets.nil?
18
18
  AutoprefixerRails.install(app.assets, config)
19
19
  end
20
20
  end
@@ -25,7 +25,7 @@ begin
25
25
  params = {}
26
26
 
27
27
  roots.each do |root|
28
- file = File.join(root, 'config/autoprefixer.yml')
28
+ file = File.join(root, "config/autoprefixer.yml")
29
29
 
30
30
  if File.exist?(file)
31
31
  parsed = ::YAML.load_file(file)
@@ -1,4 +1,4 @@
1
- require 'pathname'
1
+ require "pathname"
2
2
 
3
3
  module AutoprefixerRails
4
4
  # Register autoprefixer postprocessor in Sprockets and fix common issues
@@ -16,11 +16,11 @@ module AutoprefixerRails
16
16
 
17
17
  # Add prefixes to `css`
18
18
  def self.run(filename, css)
19
- output = filename.chomp(File.extname(filename)) + '.css'
19
+ output = filename.chomp(File.extname(filename)) + ".css"
20
20
  result = @processor.process(css, from: filename, to: output)
21
21
 
22
22
  result.warnings.each do |warning|
23
- $stderr.puts "autoprefixer: #{ warning }"
23
+ warn "autoprefixer: #{warning}"
24
24
  end
25
25
 
26
26
  result.css
@@ -29,10 +29,10 @@ module AutoprefixerRails
29
29
  # Register postprocessor in Sprockets depend on issues with other gems
30
30
  def self.install(env)
31
31
  if ::Sprockets::VERSION.to_f < 4
32
- env.register_postprocessor('text/css',
32
+ env.register_postprocessor("text/css",
33
33
  ::AutoprefixerRails::Sprockets)
34
34
  else
35
- env.register_bundle_processor('text/css',
35
+ env.register_bundle_processor("text/css",
36
36
  ::AutoprefixerRails::Sprockets)
37
37
  end
38
38
  end
@@ -40,18 +40,18 @@ module AutoprefixerRails
40
40
  # Register postprocessor in Sprockets depend on issues with other gems
41
41
  def self.uninstall(env)
42
42
  if ::Sprockets::VERSION.to_f < 4
43
- env.unregister_postprocessor('text/css',
43
+ env.unregister_postprocessor("text/css",
44
44
  ::AutoprefixerRails::Sprockets)
45
45
  else
46
- env.unregister_bundle_processor('text/css',
46
+ env.unregister_bundle_processor("text/css",
47
47
  ::AutoprefixerRails::Sprockets)
48
48
  end
49
49
  end
50
50
 
51
51
  # Sprockets 2 API new and render
52
- def initialize(filename, &block)
52
+ def initialize(filename)
53
53
  @filename = filename
54
- @source = block.call
54
+ @source = yield
55
55
  end
56
56
 
57
57
  # Sprockets 2 API new and render
@@ -1,3 +1,3 @@
1
1
  module AutoprefixerRails
2
- VERSION = '8.6.5'.freeze unless defined? AutoprefixerRails::VERSION
2
+ VERSION = "9.7.0".freeze unless defined? AutoprefixerRails::VERSION
3
3
  end
@@ -1,6 +1,6 @@
1
- require 'rake'
2
- require 'rake/tasklib'
3
- require 'autoprefixer-rails'
1
+ require "rake"
2
+ require "rake/tasklib"
3
+ require "autoprefixer-rails"
4
4
 
5
5
  module Rake
6
6
  # Define task to inspect Autoprefixer browsers, properties and values.
@@ -18,7 +18,7 @@ module Rake
18
18
 
19
19
  def define
20
20
  namespace :autoprefixer do
21
- desc 'Show selected browsers and prefixed CSS properties and values'
21
+ desc "Show selected browsers and prefixed CSS properties and values"
22
22
  task :info do
23
23
  puts @processor.info
24
24
  end
data/spec/app/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require File.expand_path('../config/application', __FILE__)
1
+ require File.expand_path("../config/application", __FILE__)
2
2
  App::Application.load_tasks
@@ -1,6 +1,6 @@
1
1
  class CssController < ApplicationController
2
2
  def test
3
- file = params[:exact_file] || params[:file] + '.css'
3
+ file = params[:exact_file] || params[:file] + ".css"
4
4
  render plain: Rails.application.assets[file].to_s.gsub(/;(\s})/, '\1')
5
5
  end
6
6
  end
data/spec/app/config.ru CHANGED
@@ -1,2 +1,2 @@
1
- require ::File.expand_path('../config/environment', __FILE__)
1
+ require ::File.expand_path("../config/environment", __FILE__)
2
2
  run App::Application
@@ -1,14 +1,16 @@
1
- require File.expand_path('../boot', __FILE__)
1
+ require File.expand_path("../boot", __FILE__)
2
2
 
3
- require 'action_controller/railtie'
4
- require 'sprockets/railtie'
3
+ require "action_controller/railtie"
4
+ require "sprockets/railtie"
5
5
 
6
6
  if defined?(Bundler)
7
- Bundler.require(*Rails.groups(assets: %w(development test)))
7
+ Bundler.require(*Rails.groups(assets: %w[development test]))
8
8
  end
9
9
 
10
10
  module App
11
11
  class Application < Rails::Application
12
12
  config.assets.enabled = true
13
+ config.sass.line_comments = false
14
+ config.sass.inline_source_maps = true
13
15
  end
14
16
  end