condenser-rails 2.0.0.rc1 → 2.0.0.rc2

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