condenser-rails 2.0.0.rc2 → 2.0.0.rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85d5ac4cb95e71d372827c8eda9c52db58726c6cad9c90813b4ae8a519e21b2b
4
- data.tar.gz: 6801341c6b6043c0bb19fd222a9ff943a1e2dd398625e6977689385992e61bcb
3
+ metadata.gz: 886aa5a1636dd9dd8cf4a53e5239132702152a31fb32e9b3d63a1fbdb4c819e8
4
+ data.tar.gz: d60870ca9e12d8f4f2bbed07461d0acccaf0627697be58cb4e277617fb4c70ba
5
5
  SHA512:
6
- metadata.gz: 42a1e35f891b27d5e651fabe7cb1bf3f4db0952567207c0287c40f27146b42fa6804792d250ce624ba39da8bb218114ee47aa05a997c3215efd6fdea28ed54df
7
- data.tar.gz: 9388b86b8aa8722374af79cbe685dacfe8f47d817eb3dad79dad172a5e367f651486a4d1751dbcd84a5d8964859562e70cd4cb4a205cbf6f9a09dc1ba11252fa
6
+ metadata.gz: 4ae246c2a9e4965784eb580820b9fb589aac50b43eb600022aa2abd111f28a708d3214fbc62b6dcba50431dccbd1f998e456fe083c86add67c35ebfb26ac2fae
7
+ data.tar.gz: '0965d8b3e80ef9a1b50db2ce1795535d7e97f30a759cd8033d6536e98f5286d6a43f28bd6410ab110320067c6b719c72865e135316a8e88067e7d4ef6f166574'
data/README.md CHANGED
@@ -28,31 +28,24 @@ default. Rolldown runs in Node, so Node must be installed wherever assets are
28
28
  compiled. The `rolldown` npm package is installed into the npm path
29
29
  (`config.assets.npm_path`, `Rails.root` by default) the first time it's needed.
30
30
 
31
- To configure Rolldown, replace the default exporter with a configured
32
- `Condenser::RolldownProcessor` instance. Registering the class, as in earlier
33
- versions, no longer works. Pass `minify:` yourself, since it defaults to
34
- `false`. `module_types:` loads imports by file extension, e.g.
35
- `import logo from 'logo.png'` as a data URL:
31
+ To configure Rolldown, pass its options with `config.assets.js_bundler` (see
32
+ [`config.assets.js_bundler`](#initializer-options)). `minify:` is set for you
33
+ by `config.assets.js_minifier`. `module_types:` loads imports by file
34
+ extension, e.g. `import logo from 'logo.png'` as a data URL:
36
35
 
37
36
  ``` ruby
38
- config.assets.configure do |app, env|
39
- env.unregister_exporter('application/javascript', Condenser::RolldownProcessor)
40
- env.register_exporter('application/javascript', Condenser::RolldownProcessor.new(env.npm_path,
41
- minify: !Rails.env.local?,
42
- aliases: { 'jquery' => app.root.join('vendor/assets/javascripts/jquery.js').to_s },
43
- module_types: { png: :dataurl, ttf: :base64 }
44
- ))
45
- end
37
+ config.assets.js_bundler = { rolldown: {
38
+ aliases: { 'jquery' => Rails.root.join('vendor/assets/javascripts/jquery.js').to_s },
39
+ module_types: { png: :dataurl, ttf: :base64 }
40
+ } }
46
41
  ```
47
42
 
48
- Rollup is deprecated, and will be removed in Condenser 3.
49
- To use it instead of Rolldown:
43
+ Rollup is deprecated, and will be removed in Condenser 3. To use it instead of
44
+ Rolldown, which also minifies by default, pick another minifier:
50
45
 
51
46
  ``` ruby
52
- config.assets.configure do |app, env|
53
- env.unregister_exporter('application/javascript')
54
- env.register_exporter('application/javascript', Condenser::RollupProcessor.new(env.npm_path))
55
- end
47
+ config.assets.js_bundler = :rollup
48
+ config.assets.js_minifier = :terser
56
49
  ```
57
50
 
58
51
  ### Tailwind CSS
@@ -192,7 +185,9 @@ The JS minifier to use to minify javascript sources. Valid options are
192
185
  `:rolldown` (the default, Rolldown minifies its own output), `:terser`, your own
193
186
  minifier (a `Condenser::Processor` instance), and `false` or `nil` to disable
194
187
  minification. `:terser` runs in Node, is npm-installed into the npm path when
195
- first used, and turns Rolldown's minifier off.
188
+ first used, and turns Rolldown's minifier off. A `js_minifier` that names the
189
+ `js_bundler`, like the default `:rolldown`, has the bundler minify its own
190
+ output.
196
191
 
197
192
  **`config.assets.css_minifier`**
198
193
 
@@ -203,6 +198,21 @@ instance), and `false` or `nil` to disable minification.
203
198
 
204
199
  Assets are only minified outside the `development` and `test` environments.
205
200
 
201
+ **`config.assets.js_bundler`**
202
+
203
+ The exporter that bundles JavaScript. Valid options are `:rolldown` (the
204
+ default), `:rollup` (deprecated), either one with options, and your own exporter
205
+ (a `Condenser::Processor` instance). Options are passed to the exporter, e.g.
206
+ `keep_names:`, `aliases:`, `platform:` or `module_types:` for Rolldown:
207
+
208
+ ``` ruby
209
+ config.assets.js_bundler = { rolldown: { keep_names: true } }
210
+ ```
211
+
212
+ `minify:` is set by `config.assets.js_minifier` and can't be set here; an
213
+ exporter instance minifies (or not) by itself. It isn't used with a custom
214
+ `config.assets.pipeline`.
215
+
206
216
  **`config.assets.sourcemap`**
207
217
 
208
218
  Sets condenser's `sourcemap` setting, whether a source map is written for each
@@ -292,7 +302,10 @@ HTTPS.
292
302
  - JavaScript is bundled and minified by Rolldown by default. `:uglify` is
293
303
  removed; `:terser` still works, and turns Rolldown's minifier off.
294
304
  - CSS is minified by Lightning CSS by default.
295
- - A custom Rolldown exporter must be an instance:
305
+ - Configure Rolldown with `config.assets.js_bundler`, e.g.
306
+ `{ rolldown: { aliases: { … } } }`, instead of registering
307
+ `Condenser::RolldownProcessor` in a `configure` block. A custom exporter
308
+ registered yourself must be an instance:
296
309
  `Condenser::RolldownProcessor.new(env.npm_path, minify: !Rails.env.local?, …)`.
297
310
  - Processors registered in `config.assets.pipeline` or `config.assets.configure`,
298
311
  and minifiers set with `config.assets.js_minifier` or `css_minifier`, must be
@@ -43,7 +43,7 @@ module Condenser::Rails
43
43
 
44
44
  def manifest
45
45
  if app
46
- Condenser::Manifest.new(environment, output, app.config.assets.manifest)
46
+ Condenser::Manifest.new(environment, dir: output, filename: app.config.assets.manifest)
47
47
  else
48
48
  super
49
49
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Condenser
4
4
  module Rails
5
- VERSION = "2.0.0.rc2"
5
+ VERSION = "2.0.0.rc3"
6
6
  end
7
7
  end
@@ -76,6 +76,9 @@ class Condenser::Railtie < ::Rails::Railtie
76
76
  # Used outside development and test; set to nil or false to disable
77
77
  config.assets.js_minifier = :rolldown
78
78
  config.assets.css_minifier = :lightning_css
79
+ # :rolldown, :rollup, a name with options like { rolldown: { keep_names: true } },
80
+ # or an exporter instance
81
+ config.assets.js_bundler = :rolldown
79
82
  # Source maps: false, true, :linked or :headers, in every environment
80
83
  config.assets.sourcemap = false
81
84
  # Directories, relative to Rails.root or absolute, whose files can be
@@ -114,14 +117,23 @@ class Condenser::Railtie < ::Rails::Railtie
114
117
  Condenser::Rails::Task.new(app)
115
118
  end
116
119
 
117
- # Minifier names that don't camelize to their class name
118
- MINIFIERS = {
119
- lightning_css: 'Condenser::LightningCSSMinifier'
120
- }.freeze
120
+ # A name ignoring case and underscores, e.g. "lightningcss" for :lightning_css
121
+ def normalize_name(name)
122
+ name.to_s.delete('_').downcase
123
+ end
124
+
125
+ # The Condenser class for a name and suffix, ignoring case and underscores,
126
+ # e.g. Condenser::LightningCSSMinifier for :lightning_css and "Minifier"
127
+ def condenser_class(name, suffix)
128
+ target = "#{normalize_name(name)}#{suffix}"
129
+ constant = Condenser.constants.find { |c| c.to_s.casecmp?(target) }
130
+ Condenser.const_get(constant) if constant
131
+ end
121
132
 
122
133
  def resolve_minifier(value, env)
123
134
  if value.is_a?(Symbol) || value.is_a?(String)
124
- klass = (MINIFIERS[value.to_sym] || "Condenser::#{value.to_s.camelize}Minifier").constantize
135
+ klass = condenser_class(value, 'Minifier')
136
+ raise NameError, "no Condenser minifier named #{value.inspect}" unless klass
125
137
  # Node minifiers (e.g. Terser) install their package into the npm path
126
138
  klass <= Condenser::NodeProcessor ? klass.new(env.npm_path) : klass.new
127
139
  else
@@ -129,6 +141,39 @@ class Condenser::Railtie < ::Rails::Railtie
129
141
  end
130
142
  end
131
143
 
144
+ # Returns the bundler's name, class and options; an instance's name comes
145
+ # from its class, e.g. "rolldown" for a Condenser::RolldownProcessor
146
+ def parse_bundler(value)
147
+ name, options = case value
148
+ when Symbol, String
149
+ [value, {}]
150
+ when Hash
151
+ value.size == 1 ? value.first : nil
152
+ when Condenser::Processor
153
+ return [value.class.name.to_s.demodulize.delete_suffix('Processor').underscore, nil, nil]
154
+ end
155
+
156
+ klass = condenser_class(name, 'Processor') if name.is_a?(Symbol) || name.is_a?(String)
157
+ unless klass.is_a?(Class) && klass <= Condenser::Processor && options.is_a?(Hash)
158
+ raise ArgumentError, "js_bundler must be :rolldown, :rollup, { name => options } or a Condenser::Processor, got #{value.inspect}"
159
+ end
160
+ options = options.symbolize_keys
161
+ if options.key?(:minify)
162
+ raise ArgumentError, "js_bundler can't set minify; use config.assets.js_minifier"
163
+ end
164
+
165
+ [name.to_s, klass, options]
166
+ end
167
+
168
+ # minify - Passed to the bundler unless nil
169
+ def resolve_bundler(value, env, minify:)
170
+ _, klass, options = parse_bundler(value)
171
+ return value if klass.nil?
172
+
173
+ options = options.merge(minify: minify) unless minify.nil?
174
+ klass.new(env.npm_path, **options)
175
+ end
176
+
132
177
  def resolve_writer(value)
133
178
  if value.is_a?(Symbol) || value.is_a?(String)
134
179
  "Condenser::#{value.to_s.camelize}Writer".constantize.new
@@ -157,8 +202,14 @@ class Condenser::Railtie < ::Rails::Railtie
157
202
  # Only minify outside development and test
158
203
  minify = !::Rails.env.local?
159
204
  js_minifier = config.assets.js_minifier
160
- # Rolldown minifies its own output unless another JS minifier is chosen
161
- rolldown_minifies = minify && js_minifier.to_s == 'rolldown'
205
+ js_bundler = config.assets.js_bundler
206
+ bundler_name, = parse_bundler(js_bundler)
207
+ # The bundler minifies its own output when the js_minifier names it; an
208
+ # exporter instance minifies (or not) by itself
209
+ bundler_minifies = normalize_name(js_minifier) == normalize_name(bundler_name)
210
+ if (js_minifier.is_a?(Symbol) || js_minifier.is_a?(String)) && !bundler_minifies && !condenser_class(js_minifier, 'Minifier')
211
+ raise ArgumentError, "js_minifier #{js_minifier.inspect} isn't a minifier or the js_bundler; set js_minifier to :terser or nil"
212
+ end
162
213
 
163
214
  env.register_transformer 'text/scss', 'text/css', Condenser::ScssTransformer.new
164
215
  env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer.new
@@ -168,10 +219,10 @@ class Condenser::Railtie < ::Rails::Railtie
168
219
  sources = ['app', *config.assets.tailwind_sources].map { |path| File.expand_path(path, app.root) }
169
220
  env.register_preprocessor 'text/css', Condenser::TailwindProcessor.new(env.npm_path, sources: sources, minify: false)
170
221
  end
171
- env.register_exporter 'application/javascript', Condenser::RolldownProcessor.new(env.npm_path, minify: rolldown_minifies)
222
+ env.register_exporter 'application/javascript', resolve_bundler(js_bundler, env, minify: bundler_minifies ? minify : nil)
172
223
 
173
224
  if minify
174
- env.register_minifier 'application/javascript', resolve_minifier(js_minifier, env) if js_minifier && !rolldown_minifies
225
+ env.register_minifier 'application/javascript', resolve_minifier(js_minifier, env) if js_minifier && !bundler_minifies
175
226
  env.register_minifier 'text/css', resolve_minifier(config.assets.css_minifier, env) if config.assets.css_minifier
176
227
  end
177
228
 
@@ -193,7 +244,7 @@ class Condenser::Railtie < ::Rails::Railtie
193
244
  config = app.config
194
245
 
195
246
  path = File.join(config.paths['public'].first, config.assets.prefix)
196
- Condenser::Manifest.new(app.assets, path, config.assets.manifest)
247
+ Condenser::Manifest.new(app.assets, dir: path, filename: config.assets.manifest)
197
248
  end
198
249
 
199
250
  config.after_initialize do |app|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: condenser-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc2
4
+ version: 2.0.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Bracy
@@ -15,7 +15,7 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 2.0.0.rc2
18
+ version: 2.0.0.rc3
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3'
@@ -25,7 +25,7 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 2.0.0.rc2
28
+ version: 2.0.0.rc3
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '3'