condenser-rails 2.0.0.rc1 → 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: af5ea6490579f3e2d0ad960b7b105ead8d9e65841d5c3554275f77593d9dea40
4
- data.tar.gz: 6b3be923f9411e60fa19c16e4e9087210d8251247066e7b560dc8c6f8406ea58
3
+ metadata.gz: 886aa5a1636dd9dd8cf4a53e5239132702152a31fb32e9b3d63a1fbdb4c819e8
4
+ data.tar.gz: d60870ca9e12d8f4f2bbed07461d0acccaf0627697be58cb4e277617fb4c70ba
5
5
  SHA512:
6
- metadata.gz: c5b51b307cff02a363c48855bc66741e31b942d8c8ca70a756a1409637d12e6b4a9883fdcc459141d31bba40995cd84bda2932eb321aac8f395ccb1da329ba8d
7
- data.tar.gz: 8a6c9793fc11a839dd02898ad2823b57ef051fa3eeb031efdd7bec105d368a145f35466b1d2eed826e811e4da31fc04a51c0f5955891d0cc7ea941b89f0d1edd
6
+ metadata.gz: 4ae246c2a9e4965784eb580820b9fb589aac50b43eb600022aa2abd111f28a708d3214fbc62b6dcba50431dccbd1f998e456fe083c86add67c35ebfb26ac2fae
7
+ data.tar.gz: '0965d8b3e80ef9a1b50db2ce1795535d7e97f30a759cd8033d6536e98f5286d6a43f28bd6410ab110320067c6b719c72865e135316a8e88067e7d4ef6f166574'
data/README.md CHANGED
@@ -28,28 +28,78 @@ 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`:
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:
35
35
 
36
36
  ``` ruby
37
- config.assets.configure do |app, env|
38
- env.unregister_exporter('application/javascript', Condenser::RolldownProcessor)
39
- env.register_exporter('application/javascript', Condenser::RolldownProcessor.new(env.npm_path,
40
- minify: !Rails.env.local?,
41
- aliases: { 'jquery' => app.root.join('vendor/assets/javascripts/jquery.js').to_s }
42
- ))
43
- 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
+ } }
44
41
  ```
45
42
 
46
- Rollup is deprecated, and will be removed in Condenser 3.
47
- 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:
48
45
 
49
46
  ``` ruby
50
- config.assets.configure do |app, env|
51
- env.unregister_exporter('application/javascript')
52
- env.register_exporter('application/javascript', Condenser::RollupProcessor)
47
+ config.assets.js_bundler = :rollup
48
+ config.assets.js_minifier = :terser
49
+ ```
50
+
51
+ ### Tailwind CSS
52
+
53
+ To compile stylesheets with [Tailwind CSS](https://tailwindcss.com) v4, turn it
54
+ on:
55
+
56
+ ``` ruby
57
+ # config/application.rb
58
+ config.assets.tailwind = true
59
+ ```
60
+
61
+ ``` css
62
+ /* app/assets/stylesheets/application.css */
63
+ @import "tailwindcss";
64
+ ```
65
+
66
+ Stylesheets that `@import "tailwindcss"` or `@reference` another one are
67
+ compiled by `Condenser::TailwindProcessor`; others are left as they are. Like
68
+ Rolldown, it runs in Node, and `tailwindcss`, `@tailwindcss/node` and
69
+ `@tailwindcss/oxide` are installed into the npm path when the asset
70
+ environment is built, if they aren't there.
71
+
72
+ Tailwind scans `app/` for class names: the views, components, helpers and
73
+ JavaScript. Add other directories with `config.assets.tailwind_sources`,
74
+ relative to `Rails.root` or absolute, such as templates in `lib/` or in a gem
75
+ or engine:
76
+
77
+ ``` ruby
78
+ config.assets.tailwind_sources = [
79
+ 'lib/components',
80
+ File.join(Gem.loaded_specs['some_engine'].full_gem_path, 'app/views')
81
+ ]
82
+ ```
83
+
84
+ Each has to be an existing directory. `app/` and the `tailwind_sources` are
85
+ watched in development, so editing a template's classes rebuilds the CSS.
86
+ `source(...)`, `source(none)` and `@source` in the stylesheet work as in
87
+ Tailwind; `@source` paths outside `app/` and the `tailwind_sources` need to be
88
+ added to `config.assets.watch` to rebuild a running server's CSS.
89
+
90
+ Tailwind doesn't minify its output itself: the CSS minifier
91
+ (`config.assets.css_minifier`, Lightning CSS by default) minifies the whole
92
+ stylesheet outside development and test.
93
+
94
+ The processor is only added to the default pipeline. With a custom
95
+ `config.assets.pipeline`, register it yourself:
96
+
97
+ ``` ruby
98
+ config.assets.pipeline do |env|
99
+ # ...
100
+ env.register_preprocessor 'text/css', Condenser::TailwindProcessor.new(env.npm_path,
101
+ sources: [Rails.root.join('app'), Rails.root.join('lib/components')]
102
+ )
53
103
  end
54
104
  ```
55
105
 
@@ -119,23 +169,92 @@ const foo = <%= YAML.load_file(Rails.root.join('config/foo.yml')).to_json %>;
119
169
  config.assets.watch << 'data'
120
170
  ```
121
171
 
172
+ **`config.assets.tailwind`**
173
+
174
+ Compiles stylesheets that `@import "tailwindcss"` with Tailwind. Defaults to
175
+ `false`. See [Tailwind CSS](#tailwind-css).
176
+
177
+ **`config.assets.tailwind_sources`**
178
+
179
+ More directories for Tailwind to scan for class names, besides `app/`,
180
+ relative to `Rails.root` or absolute. Defaults to `[]`.
181
+
122
182
  **`config.assets.js_minifier`**
123
183
 
124
184
  The JS minifier to use to minify javascript sources. Valid options are
125
185
  `:rolldown` (the default, Rolldown minifies its own output), `:terser`, your own
126
- minifier, and `false` or `nil` to disable minification. `:terser` runs in Node,
127
- is npm-installed into the npm path when first used, and turns Rolldown's
128
- minifier off.
186
+ minifier (a `Condenser::Processor` instance), and `false` or `nil` to disable
187
+ minification. `:terser` runs in Node, is npm-installed into the npm path when
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.
129
191
 
130
192
  **`config.assets.css_minifier`**
131
193
 
132
194
  The CSS minifier to use to minify css sources. Valid options are
133
195
  `:lightning_css` (the default, which needs the `lightningcss` gem), `:sass`
134
- (which needs the `sass-embedded` gem), your own minifier, and `false` or `nil`
135
- to disable minification.
196
+ (which needs the `sass-embedded` gem), your own minifier (a `Condenser::Processor`
197
+ instance), and `false` or `nil` to disable minification.
136
198
 
137
199
  Assets are only minified outside the `development` and `test` environments.
138
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
+
216
+ **`config.assets.sourcemap`**
217
+
218
+ Sets condenser's `sourcemap` setting, whether a source map is written for each
219
+ JavaScript bundle and how it's linked:
220
+
221
+ | Value | Map | `//# sourceMappingURL` comment | `SourceMap` header (development server) |
222
+ |---|---|---|---|
223
+ | `false` (default) | no | no | no |
224
+ | `true` | yes | yes | yes |
225
+ | `:linked` | yes | yes | no |
226
+ | `:headers` | yes | no | yes |
227
+
228
+ It applies in every environment, so set it per environment in
229
+ `config/environments/*.rb`. Development builds aren't minified, so maps there
230
+ are optional.
231
+
232
+ ``` ruby
233
+ # config/environments/development.rb
234
+ config.assets.sourcemap = true
235
+
236
+ # config/environments/production.rb
237
+ config.assets.sourcemap = :headers
238
+ ```
239
+
240
+ In production, precompiled assets are usually served by nginx or a CDN, not
241
+ condenser's server, so nothing sends the `SourceMap` header. With `:headers`
242
+ the map is on disk next to the bundle, with no link to it unless your web
243
+ server adds the header. That's the setup for error trackers such as Sentry:
244
+ upload the maps yourself (e.g. with `sentry-cli sourcemaps upload`). There's no
245
+ separate hidden mode. Use `true` or `:linked` for browsers and error trackers
246
+ to fetch the map from your server.
247
+
248
+ `rake assets:precompile` writes the map next to the bundle, as
249
+ `application-<etag>.js.map`, and adds it to the manifest's `sourcemap` field;
250
+ `assets:clean` keeps it and `assets:clobber` removes it. The development server
251
+ serves it at the bundle's URL plus `.map`. The map covers Rolldown's minifying
252
+ and a `:terser` `js_minifier`. The setting is part of the asset cache key and
253
+ etag, so changing it rebuilds the bundles.
254
+
255
+ `config.assets.sourcemap` applies to any pipeline, including a custom
256
+ `config.assets.pipeline`, since it's set on the environment.
257
+
139
258
  **`config.assets.compressors`**
140
259
 
141
260
  A list of compressors to compress the output with. Each file will be written along
@@ -155,7 +274,7 @@ for asset compiling.
155
274
  ``` ruby
156
275
  config.assets.configure do |app, env|
157
276
  require 'my_processor'
158
- env.register_preprocessor 'application/javascript', MyProcessor
277
+ env.register_preprocessor 'application/javascript', MyProcessor.new
159
278
  end
160
279
  ```
161
280
 
@@ -183,8 +302,16 @@ HTTPS.
183
302
  - JavaScript is bundled and minified by Rolldown by default. `:uglify` is
184
303
  removed; `:terser` still works, and turns Rolldown's minifier off.
185
304
  - CSS is minified by Lightning CSS by default.
186
- - 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:
187
309
  `Condenser::RolldownProcessor.new(env.npm_path, minify: !Rails.env.local?, …)`.
310
+ - Processors registered in `config.assets.pipeline` or `config.assets.configure`,
311
+ and minifiers set with `config.assets.js_minifier` or `css_minifier`, must be
312
+ instances of a `Condenser::Processor` subclass, e.g.
313
+ `env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer.new`.
314
+ Registering a class raises an `ArgumentError`.
188
315
  - A bare `// depends_on` path that matches nothing now raises
189
316
  `Condenser::FileNotFound` (globs don't). Files relative to the app root need
190
317
  a `!`, e.g. `// depends_on !config/foo.yml`. `config/` is watched by default
@@ -195,6 +322,9 @@ HTTPS.
195
322
  its `exports` (`sass` or `style` condition), `sass` or `style` field.
196
323
  - A configured `config.assets.npm_path` is now respected instead of being
197
324
  reset to `Rails.root`.
325
+ - Tailwind CSS v4 is built in: set `config.assets.tailwind = true`, and
326
+ `config.assets.tailwind_sources` for templates outside `app/`, instead of
327
+ building Tailwind's CSS separately.
198
328
  - Clear `tmp/cache/assets` and `public/assets` after upgrading.
199
329
 
200
330
  ## License
@@ -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.rc1"
5
+ VERSION = "2.0.0.rc3"
6
6
  end
7
7
  end
@@ -76,9 +76,18 @@ 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
82
+ # Source maps: false, true, :linked or :headers, in every environment
83
+ config.assets.sourcemap = false
79
84
  # Directories, relative to Rails.root or absolute, whose files can be
80
85
  # `depends_on`'d and rebuild assets when they change
81
86
  config.assets.watch = ['config']
87
+ # Compile stylesheets that `@import "tailwindcss"` with Tailwind, scanning
88
+ # app/ and the tailwind_sources, relative to Rails.root or absolute
89
+ config.assets.tailwind = false
90
+ config.assets.tailwind_sources = []
82
91
 
83
92
  config.assets.configure do |app, env|
84
93
  config.assets.path.each { |path| env.append_path(path) }
@@ -108,19 +117,63 @@ class Condenser::Railtie < ::Rails::Railtie
108
117
  Condenser::Rails::Task.new(app)
109
118
  end
110
119
 
111
- # Minifier names that don't camelize to their class name
112
- MINIFIERS = {
113
- lightning_css: 'Condenser::LightningCSSMinifier'
114
- }.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
115
132
 
116
- def resolve_minifier(value)
133
+ def resolve_minifier(value, env)
117
134
  if value.is_a?(Symbol) || value.is_a?(String)
118
- (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
137
+ # Node minifiers (e.g. Terser) install their package into the npm path
138
+ klass <= Condenser::NodeProcessor ? klass.new(env.npm_path) : klass.new
119
139
  else
120
140
  value
121
141
  end
122
142
  end
123
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
+
124
177
  def resolve_writer(value)
125
178
  if value.is_a?(Symbol) || value.is_a?(String)
126
179
  "Condenser::#{value.to_s.camelize}Writer".constantize.new
@@ -141,7 +194,7 @@ class Condenser::Railtie < ::Rails::Railtie
141
194
 
142
195
  # Set the npm path up front so exporters and custom pipelines can use it
143
196
  npm_path = config.assets.npm_path if config.assets.npm_path && File.directory?(config.assets.npm_path)
144
- env = Condenser.new(pipeline: false, logger: ::Rails.logger, base: app.root.to_s, npm_path: npm_path, listen: listen)
197
+ env = Condenser.new(pipeline: false, logger: ::Rails.logger, base: app.root.to_s, npm_path: npm_path, listen: listen, sourcemap: config.assets.sourcemap || false)
145
198
 
146
199
  if config.assets._pipeline
147
200
  config.assets._pipeline.call(env)
@@ -149,16 +202,28 @@ class Condenser::Railtie < ::Rails::Railtie
149
202
  # Only minify outside development and test
150
203
  minify = !::Rails.env.local?
151
204
  js_minifier = config.assets.js_minifier
152
- # Rolldown minifies its own output unless another JS minifier is chosen
153
- 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
154
213
 
155
214
  env.register_transformer 'text/scss', 'text/css', Condenser::ScssTransformer.new
156
- env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer
157
- env.register_exporter 'application/javascript', Condenser::RolldownProcessor.new(env.npm_path, minify: rolldown_minifies)
215
+ env.register_preprocessor 'application/javascript', Condenser::JSAnalyzer.new
216
+ if config.assets.tailwind
217
+ # The CSS minifier minifies Tailwind's output with the rest of the
218
+ # stylesheet, so Tailwind doesn't. The processor watches the sources.
219
+ sources = ['app', *config.assets.tailwind_sources].map { |path| File.expand_path(path, app.root) }
220
+ env.register_preprocessor 'text/css', Condenser::TailwindProcessor.new(env.npm_path, sources: sources, minify: false)
221
+ end
222
+ env.register_exporter 'application/javascript', resolve_bundler(js_bundler, env, minify: bundler_minifies ? minify : nil)
158
223
 
159
224
  if minify
160
- env.register_minifier 'application/javascript', resolve_minifier(js_minifier) if js_minifier && !rolldown_minifies
161
- env.register_minifier 'text/css', resolve_minifier(config.assets.css_minifier) if config.assets.css_minifier
225
+ env.register_minifier 'application/javascript', resolve_minifier(js_minifier, env) if js_minifier && !bundler_minifies
226
+ env.register_minifier 'text/css', resolve_minifier(config.assets.css_minifier, env) if config.assets.css_minifier
162
227
  end
163
228
 
164
229
  env.register_writer Condenser::FileWriter.new
@@ -179,7 +244,7 @@ class Condenser::Railtie < ::Rails::Railtie
179
244
  config = app.config
180
245
 
181
246
  path = File.join(config.paths['public'].first, config.assets.prefix)
182
- Condenser::Manifest.new(app.assets, path, config.assets.manifest)
247
+ Condenser::Manifest.new(app.assets, dir: path, filename: config.assets.manifest)
183
248
  end
184
249
 
185
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.rc1
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.rc1
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.rc1
28
+ version: 2.0.0.rc3
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '3'