shakapacker 9.3.0 → 9.3.2
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 +4 -4
- data/.claude/commands/update-changelog.md +232 -0
- data/.github/actionlint-matcher.json +17 -0
- data/.github/workflows/dummy.yml +9 -0
- data/.github/workflows/generator.yml +13 -0
- data/.github/workflows/node.yml +83 -0
- data/.github/workflows/ruby.yml +11 -0
- data/.github/workflows/test-bundlers.yml +10 -0
- data/CHANGELOG.md +24 -9
- data/CLAUDE.md +6 -10
- data/CONTRIBUTING.md +44 -0
- data/Gemfile.lock +1 -1
- data/README.md +74 -44
- data/docs/api-reference.md +519 -0
- data/docs/cdn_setup.md +3 -3
- data/docs/common-upgrades.md +6 -7
- data/docs/configuration.md +14 -8
- data/docs/css-modules-export-mode.md +40 -6
- data/docs/deployment.md +3 -3
- data/docs/early_hints_manual_api.md +1 -1
- data/docs/feature_testing.md +3 -3
- data/docs/optional-peer-dependencies.md +2 -2
- data/docs/precompile_hook.md +15 -15
- data/docs/react.md +1 -1
- data/docs/releasing.md +7 -7
- data/docs/rspack_migration_guide.md +8 -14
- data/docs/transpiler-migration.md +12 -9
- data/docs/troubleshooting.md +3 -3
- data/docs/using_swc_loader.md +13 -10
- data/docs/v6_upgrade.md +2 -2
- data/docs/v9_upgrade.md +78 -3
- data/eslint.config.fast.js +120 -8
- data/eslint.config.js +50 -31
- data/lib/install/config/shakapacker.yml +14 -1
- data/lib/shakapacker/bundler_switcher.rb +83 -18
- data/lib/shakapacker/configuration.rb +47 -4
- data/lib/shakapacker/dev_server_runner.rb +4 -0
- data/lib/shakapacker/doctor.rb +7 -7
- data/lib/shakapacker/runner.rb +1 -1
- data/lib/shakapacker/swc_migrator.rb +2 -2
- data/lib/shakapacker/version.rb +1 -1
- data/lib/tasks/shakapacker/binstubs.rake +4 -2
- data/lib/tasks/shakapacker/check_binstubs.rake +2 -2
- data/lib/tasks/shakapacker/doctor.rake +3 -3
- data/lib/tasks/shakapacker/export_bundler_config.rake +5 -9
- data/lib/tasks/shakapacker/install.rake +4 -2
- data/lib/tasks/shakapacker/switch_bundler.rake +30 -40
- data/package/config.ts +2 -3
- data/package/configExporter/cli.ts +29 -24
- data/package/dev_server.ts +2 -2
- data/package/env.ts +1 -1
- data/package/environments/__type-tests__/rspack-plugin-compatibility.ts +6 -6
- data/package/environments/base.ts +2 -2
- data/package/environments/development.ts +3 -6
- data/package/environments/production.ts +2 -2
- data/package/environments/test.ts +2 -1
- data/package/esbuild/index.ts +0 -2
- data/package/index.d.ts +1 -0
- data/package/index.d.ts.template +1 -0
- data/package/index.ts +0 -1
- data/package/loaders.d.ts +1 -1
- data/package/plugins/rspack.ts +3 -1
- data/package/plugins/webpack.ts +5 -3
- data/package/rspack/index.ts +3 -3
- data/package/rules/file.ts +1 -1
- data/package/rules/raw.ts +3 -1
- data/package/rules/rspack.ts +0 -2
- data/package/rules/sass.ts +0 -2
- data/package/rules/webpack.ts +0 -1
- data/package/swc/index.ts +0 -2
- data/package/types.ts +8 -11
- data/package/utils/debug.ts +0 -4
- data/package/utils/getStyleRule.ts +17 -9
- data/package/utils/helpers.ts +8 -3
- data/package/utils/pathValidation.ts +10 -11
- data/package/utils/requireOrError.ts +4 -3
- data/package/webpack-types.d.ts +1 -1
- data/package/webpackDevServerConfig.ts +4 -4
- data/package.json +3 -3
- data/test/package/transpiler-defaults.test.js +42 -0
- data/yarn.lock +1 -1
- metadata +5 -2
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
# Shakapacker API Reference
|
|
2
|
+
|
|
3
|
+
This document provides a comprehensive reference for Shakapacker's public Ruby API. For JavaScript/webpack configuration, see [Webpack Configuration](./webpack-configuration.md).
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Overview](#overview)
|
|
8
|
+
- [Main Module: Shakapacker](#main-module-shakapacker)
|
|
9
|
+
- [Configuration API](#configuration-api)
|
|
10
|
+
- [View Helpers](#view-helpers)
|
|
11
|
+
- [Manifest API](#manifest-api)
|
|
12
|
+
- [Dev Server API](#dev-server-api)
|
|
13
|
+
- [Compiler API](#compiler-api)
|
|
14
|
+
- [Advanced Usage](#advanced-usage)
|
|
15
|
+
|
|
16
|
+
## Overview
|
|
17
|
+
|
|
18
|
+
Shakapacker provides a Ruby API for integrating webpack/rspack with Rails applications. The API is divided into several key areas:
|
|
19
|
+
|
|
20
|
+
- **Configuration**: Access to `config/shakapacker.yml` settings
|
|
21
|
+
- **View Helpers**: Rails helpers for rendering script/link tags
|
|
22
|
+
- **Manifest**: Asset lookup and resolution
|
|
23
|
+
- **Compilation**: Programmatic asset compilation
|
|
24
|
+
- **Dev Server**: Development server status and management
|
|
25
|
+
|
|
26
|
+
### What's Public API?
|
|
27
|
+
|
|
28
|
+
Methods and classes marked with `@api public` in the source code are considered stable public API. Other methods may change between minor versions.
|
|
29
|
+
|
|
30
|
+
## Main Module: Shakapacker
|
|
31
|
+
|
|
32
|
+
The `Shakapacker` module provides singleton-style access to all major functionality.
|
|
33
|
+
|
|
34
|
+
### Configuration Access
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
# Get the configuration object
|
|
38
|
+
Shakapacker.config
|
|
39
|
+
# => #<Shakapacker::Configuration>
|
|
40
|
+
|
|
41
|
+
# Access configuration values
|
|
42
|
+
Shakapacker.config.source_path
|
|
43
|
+
# => #<Pathname:/path/to/app/javascript>
|
|
44
|
+
|
|
45
|
+
Shakapacker.config.public_output_path
|
|
46
|
+
# => "packs"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Compilation
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
# Compile all packs
|
|
53
|
+
Shakapacker.compile
|
|
54
|
+
# => true
|
|
55
|
+
|
|
56
|
+
# Check if compilation is needed
|
|
57
|
+
Shakapacker.compiler.stale?
|
|
58
|
+
# => false
|
|
59
|
+
|
|
60
|
+
# Get compiler output
|
|
61
|
+
Shakapacker.compiler.compile
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Manifest Lookup
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# Look up compiled asset path
|
|
68
|
+
Shakapacker.manifest.lookup("application.js")
|
|
69
|
+
# => "/packs/application-abc123.js"
|
|
70
|
+
|
|
71
|
+
# Look up with error if not found
|
|
72
|
+
Shakapacker.manifest.lookup!("application.js")
|
|
73
|
+
# => "/packs/application-abc123.js" (or raises Shakapacker::Manifest::MissingEntryError)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Dev Server Status
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
# Check if dev server is running
|
|
80
|
+
Shakapacker.dev_server.running?
|
|
81
|
+
# => true
|
|
82
|
+
|
|
83
|
+
# Get dev server host
|
|
84
|
+
Shakapacker.dev_server.host
|
|
85
|
+
# => "localhost"
|
|
86
|
+
|
|
87
|
+
# Get dev server port
|
|
88
|
+
Shakapacker.dev_server.port
|
|
89
|
+
# => 3035
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Environment Management
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
# Get current environment
|
|
96
|
+
Shakapacker.env
|
|
97
|
+
# => #<ActiveSupport::StringInquirer "development">
|
|
98
|
+
|
|
99
|
+
# Temporarily use different NODE_ENV
|
|
100
|
+
Shakapacker.with_node_env("production") do
|
|
101
|
+
Shakapacker.compile
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Logging
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
# Access logger
|
|
109
|
+
Shakapacker.logger
|
|
110
|
+
# => #<Logger>
|
|
111
|
+
|
|
112
|
+
# Redirect to STDOUT temporarily
|
|
113
|
+
Shakapacker.ensure_log_goes_to_stdout do
|
|
114
|
+
Shakapacker.compile
|
|
115
|
+
end
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Configuration API
|
|
119
|
+
|
|
120
|
+
The `Shakapacker::Configuration` class provides access to all settings from `config/shakapacker.yml`.
|
|
121
|
+
|
|
122
|
+
### Accessing Configuration Data
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
config = Shakapacker.config
|
|
126
|
+
|
|
127
|
+
# Get raw configuration hash (public API as of v9.1.0)
|
|
128
|
+
config.data
|
|
129
|
+
# => { "source_path" => "app/javascript", ... }
|
|
130
|
+
|
|
131
|
+
# Access specific values
|
|
132
|
+
config.data["source_path"]
|
|
133
|
+
# => "app/javascript"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**See Also:** [Configuration Guide](./configuration.md) for all available options.
|
|
137
|
+
|
|
138
|
+
### Path Configuration
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
# Source paths
|
|
142
|
+
config.source_path # => #<Pathname:/app/app/javascript>
|
|
143
|
+
config.source_entry_path # => #<Pathname:/app/app/javascript/packs>
|
|
144
|
+
config.additional_paths # => [#<Pathname:/app/app/assets>, ...]
|
|
145
|
+
|
|
146
|
+
# Output paths
|
|
147
|
+
config.public_path # => #<Pathname:/app/public>
|
|
148
|
+
config.public_output_path # => "packs"
|
|
149
|
+
config.public_manifest_path # => #<Pathname:/app/public/packs/manifest.json>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Bundler Detection
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
# Check which bundler is configured
|
|
156
|
+
config.webpack? # => true
|
|
157
|
+
config.rspack? # => false
|
|
158
|
+
config.bundler # => "webpack"
|
|
159
|
+
|
|
160
|
+
# Get bundler config path
|
|
161
|
+
config.assets_bundler_config_path
|
|
162
|
+
# => #<Pathname:/app/config/webpack/webpack.config.js>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Compilation Settings
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
config.compile? # => true (auto-compile enabled?)
|
|
169
|
+
config.cache_manifest? # => false
|
|
170
|
+
config.extract_css? # => false (use MiniCssExtractPlugin?)
|
|
171
|
+
config.nested_entries? # => false
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Dev Server Configuration
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
dev_server = config.dev_server
|
|
178
|
+
dev_server["host"] # => "localhost"
|
|
179
|
+
dev_server["port"] # => 3035
|
|
180
|
+
dev_server["hmr"] # => true
|
|
181
|
+
dev_server["https"] # => false
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## View Helpers
|
|
185
|
+
|
|
186
|
+
Shakapacker provides Rails view helpers in the `Shakapacker::Helper` module, automatically included in ActionView.
|
|
187
|
+
|
|
188
|
+
### JavaScript Pack Tag
|
|
189
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
# Basic usage
|
|
192
|
+
<%= javascript_pack_tag 'application' %>
|
|
193
|
+
# => <script src="/packs/application-abc123.js" defer></script>
|
|
194
|
+
|
|
195
|
+
# Multiple packs (handles chunk deduplication)
|
|
196
|
+
<%= javascript_pack_tag 'calendar', 'map' %>
|
|
197
|
+
|
|
198
|
+
# Custom attributes
|
|
199
|
+
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
|
|
200
|
+
|
|
201
|
+
# Async loading
|
|
202
|
+
<%= javascript_pack_tag 'application', async: true %>
|
|
203
|
+
|
|
204
|
+
# Disable defer
|
|
205
|
+
<%= javascript_pack_tag 'application', defer: false %>
|
|
206
|
+
|
|
207
|
+
# Early hints configuration
|
|
208
|
+
<%= javascript_pack_tag 'application', early_hints: 'preload' %>
|
|
209
|
+
<%= javascript_pack_tag 'application', early_hints: false %>
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**Important:** Call `javascript_pack_tag` only once per page to avoid duplicate chunks.
|
|
213
|
+
|
|
214
|
+
### Stylesheet Pack Tag
|
|
215
|
+
|
|
216
|
+
```ruby
|
|
217
|
+
# Basic usage
|
|
218
|
+
<%= stylesheet_pack_tag 'application' %>
|
|
219
|
+
# => <link rel="stylesheet" href="/packs/application-abc123.css">
|
|
220
|
+
|
|
221
|
+
# Multiple packs with attributes
|
|
222
|
+
<%= stylesheet_pack_tag 'application', 'calendar', media: 'screen' %>
|
|
223
|
+
|
|
224
|
+
# Early hints
|
|
225
|
+
<%= stylesheet_pack_tag 'application', early_hints: 'preload' %>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Dynamic Pack Loading
|
|
229
|
+
|
|
230
|
+
```ruby
|
|
231
|
+
# In view or partial - queue packs
|
|
232
|
+
<% append_javascript_pack_tag 'calendar' %>
|
|
233
|
+
<% append_stylesheet_pack_tag 'calendar' %>
|
|
234
|
+
|
|
235
|
+
# Prepend to queue
|
|
236
|
+
<% prepend_javascript_pack_tag 'critical' %>
|
|
237
|
+
|
|
238
|
+
# In layout - render all queued packs
|
|
239
|
+
<%= javascript_pack_tag 'application' %>
|
|
240
|
+
<%= stylesheet_pack_tag 'application' %>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Asset Helpers
|
|
244
|
+
|
|
245
|
+
```ruby
|
|
246
|
+
# Get pack path
|
|
247
|
+
<%= asset_pack_path 'logo.svg' %>
|
|
248
|
+
# => "/packs/logo-abc123.svg"
|
|
249
|
+
|
|
250
|
+
# Get pack URL
|
|
251
|
+
<%= asset_pack_url 'logo.svg' %>
|
|
252
|
+
# => "https://cdn.example.com/packs/logo-abc123.svg"
|
|
253
|
+
|
|
254
|
+
# Image pack tag
|
|
255
|
+
<%= image_pack_tag 'logo.png', size: '16x10', alt: 'Logo' %>
|
|
256
|
+
# => <img src="/packs/logo-abc123.png" width="16" height="10" alt="Logo">
|
|
257
|
+
|
|
258
|
+
# With srcset
|
|
259
|
+
<%= image_pack_tag 'photo.png', srcset: { 'photo-2x.png' => '2x' } %>
|
|
260
|
+
|
|
261
|
+
# Favicon
|
|
262
|
+
<%= favicon_pack_tag 'icon.png', rel: 'apple-touch-icon' %>
|
|
263
|
+
|
|
264
|
+
# Preload asset
|
|
265
|
+
<%= preload_pack_asset 'fonts/custom.woff2' %>
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Manifest API
|
|
269
|
+
|
|
270
|
+
The `Shakapacker::Manifest` class handles asset lookup from the compiled manifest.
|
|
271
|
+
|
|
272
|
+
### Basic Lookup
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
manifest = Shakapacker.manifest
|
|
276
|
+
|
|
277
|
+
# Look up an asset (returns nil if not found)
|
|
278
|
+
manifest.lookup("application.js")
|
|
279
|
+
# => "/packs/application-abc123.js"
|
|
280
|
+
|
|
281
|
+
# Look up with error on missing
|
|
282
|
+
manifest.lookup!("application.js")
|
|
283
|
+
# => "/packs/application-abc123.js" (raises if missing)
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Lookup with Type
|
|
287
|
+
|
|
288
|
+
```ruby
|
|
289
|
+
# Lookup stylesheets
|
|
290
|
+
manifest.lookup("application.css")
|
|
291
|
+
# => "/packs/application-abc123.css"
|
|
292
|
+
|
|
293
|
+
# Lookup images
|
|
294
|
+
manifest.lookup("logo.svg")
|
|
295
|
+
# => "/packs/static/logo-abc123.svg"
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Full Manifest Access
|
|
299
|
+
|
|
300
|
+
```ruby
|
|
301
|
+
# Get all entries
|
|
302
|
+
manifest.data
|
|
303
|
+
# => { "application.js" => "/packs/application-abc123.js", ... }
|
|
304
|
+
|
|
305
|
+
# Refresh manifest from disk
|
|
306
|
+
manifest.refresh
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Dev Server API
|
|
310
|
+
|
|
311
|
+
The `Shakapacker::DevServer` class provides status and configuration for the development server.
|
|
312
|
+
|
|
313
|
+
### Status Checking
|
|
314
|
+
|
|
315
|
+
```ruby
|
|
316
|
+
dev_server = Shakapacker.dev_server
|
|
317
|
+
|
|
318
|
+
# Check if running
|
|
319
|
+
dev_server.running?
|
|
320
|
+
# => true
|
|
321
|
+
|
|
322
|
+
# Get full status URL
|
|
323
|
+
dev_server.status_url
|
|
324
|
+
# => "http://localhost:3035"
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Configuration
|
|
328
|
+
|
|
329
|
+
```ruby
|
|
330
|
+
dev_server.host
|
|
331
|
+
# => "localhost"
|
|
332
|
+
|
|
333
|
+
dev_server.port
|
|
334
|
+
# => 3035
|
|
335
|
+
|
|
336
|
+
dev_server.https?
|
|
337
|
+
# => false
|
|
338
|
+
|
|
339
|
+
dev_server.hmr?
|
|
340
|
+
# => true
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Proxying
|
|
344
|
+
|
|
345
|
+
```ruby
|
|
346
|
+
# Get asset URL (uses dev server if running, otherwise public path)
|
|
347
|
+
dev_server.asset_url("application.js")
|
|
348
|
+
# => "http://localhost:3035/packs/application.js" (or "/packs/application.js")
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
## Compiler API
|
|
352
|
+
|
|
353
|
+
The `Shakapacker::Compiler` class handles compilation of webpack/rspack assets.
|
|
354
|
+
|
|
355
|
+
### Compilation
|
|
356
|
+
|
|
357
|
+
```ruby
|
|
358
|
+
compiler = Shakapacker.compiler
|
|
359
|
+
|
|
360
|
+
# Compile all packs
|
|
361
|
+
compiler.compile
|
|
362
|
+
# => true
|
|
363
|
+
|
|
364
|
+
# Check if compilation needed
|
|
365
|
+
compiler.stale?
|
|
366
|
+
# => false
|
|
367
|
+
|
|
368
|
+
# Get last compilation time
|
|
369
|
+
compiler.last_compilation_digest
|
|
370
|
+
# => "abc123..."
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### Configuration
|
|
374
|
+
|
|
375
|
+
```ruby
|
|
376
|
+
# Get watched paths
|
|
377
|
+
compiler.watched_paths
|
|
378
|
+
# => [#<Pathname:/app/app/javascript>, ...]
|
|
379
|
+
|
|
380
|
+
# Get config files
|
|
381
|
+
compiler.config_files
|
|
382
|
+
# => [#<Pathname:/app/config/webpack/webpack.config.js>, ...]
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
## Advanced Usage
|
|
386
|
+
|
|
387
|
+
### Multiple Shakapacker Instances
|
|
388
|
+
|
|
389
|
+
For advanced scenarios like Rails engines with separate webpack configs:
|
|
390
|
+
|
|
391
|
+
```ruby
|
|
392
|
+
# Create custom instance
|
|
393
|
+
custom_instance = Shakapacker::Instance.new(
|
|
394
|
+
root_path: Rails.root,
|
|
395
|
+
config_path: Rails.root.join("config/custom_shakapacker.yml")
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
# Use in view helper
|
|
399
|
+
def current_shakapacker_instance
|
|
400
|
+
@custom_instance ||= Shakapacker::Instance.new(...)
|
|
401
|
+
end
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### Testing
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
# In tests, you can stub the instance
|
|
408
|
+
RSpec.describe "my feature" do
|
|
409
|
+
let(:mock_manifest) { instance_double(Shakapacker::Manifest) }
|
|
410
|
+
let(:mock_instance) { instance_double(Shakapacker::Instance, manifest: mock_manifest) }
|
|
411
|
+
|
|
412
|
+
before do
|
|
413
|
+
allow(Shakapacker).to receive(:instance).and_return(mock_instance)
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Programmatic Configuration Access
|
|
419
|
+
|
|
420
|
+
```ruby
|
|
421
|
+
# Access raw configuration for custom tooling
|
|
422
|
+
config_data = Shakapacker.config.data
|
|
423
|
+
|
|
424
|
+
# Use in custom rake task
|
|
425
|
+
namespace :assets do
|
|
426
|
+
task :analyze do
|
|
427
|
+
config = Shakapacker.config
|
|
428
|
+
puts "Source: #{config.source_path}"
|
|
429
|
+
puts "Output: #{config.public_output_path}"
|
|
430
|
+
puts "Bundler: #{config.bundler}"
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### Custom Webpack Configuration
|
|
436
|
+
|
|
437
|
+
Access Shakapacker config from your webpack config:
|
|
438
|
+
|
|
439
|
+
```javascript
|
|
440
|
+
// config/webpack/webpack.config.js
|
|
441
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
442
|
+
|
|
443
|
+
// The shakapacker.yml is automatically loaded
|
|
444
|
+
const config = generateWebpackConfig()
|
|
445
|
+
|
|
446
|
+
console.log(config.output.path) // Uses public_output_path from shakapacker.yml
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Environment Variables
|
|
450
|
+
|
|
451
|
+
Shakapacker respects these environment variables:
|
|
452
|
+
|
|
453
|
+
| Variable | Purpose | Example |
|
|
454
|
+
| ---------------------------- | ----------------------------- | ------------------------- |
|
|
455
|
+
| `SHAKAPACKER_CONFIG` | Custom config file path | `/custom/shakapacker.yml` |
|
|
456
|
+
| `SHAKAPACKER_PRECOMPILE` | Enable/disable precompilation | `false` |
|
|
457
|
+
| `SHAKAPACKER_ASSET_HOST` | CDN hostname | `https://cdn.example.com` |
|
|
458
|
+
| `SHAKAPACKER_ASSETS_BUNDLER` | Override bundler selection | `rspack` |
|
|
459
|
+
| `RAILS_ENV` | Rails environment | `production` |
|
|
460
|
+
| `NODE_ENV` | Node environment | `production` |
|
|
461
|
+
|
|
462
|
+
## Error Handling
|
|
463
|
+
|
|
464
|
+
```ruby
|
|
465
|
+
# Manifest lookup errors
|
|
466
|
+
begin
|
|
467
|
+
path = Shakapacker.manifest.lookup!("missing.js")
|
|
468
|
+
rescue Shakapacker::Manifest::MissingEntryError => e
|
|
469
|
+
Rails.logger.error "Missing pack: #{e.message}"
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# Compilation errors
|
|
473
|
+
begin
|
|
474
|
+
Shakapacker.compiler.compile
|
|
475
|
+
rescue Shakapacker::Compiler::CompilationError => e
|
|
476
|
+
Rails.logger.error "Compilation failed: #{e.message}"
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
# Configuration errors
|
|
480
|
+
begin
|
|
481
|
+
config = Shakapacker::Configuration.new(...)
|
|
482
|
+
rescue Shakapacker::Configuration::InvalidConfigurationError => e
|
|
483
|
+
Rails.logger.error "Invalid config: #{e.message}"
|
|
484
|
+
end
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
## Deprecations
|
|
488
|
+
|
|
489
|
+
Shakapacker follows semantic versioning. Deprecated methods will:
|
|
490
|
+
|
|
491
|
+
1. Issue warnings in the current major version
|
|
492
|
+
2. Be removed in the next major version
|
|
493
|
+
|
|
494
|
+
Check the [CHANGELOG](../CHANGELOG.md) for deprecation notices.
|
|
495
|
+
|
|
496
|
+
## See Also
|
|
497
|
+
|
|
498
|
+
- [Configuration Guide](./configuration.md) - All `shakapacker.yml` options
|
|
499
|
+
- [View Helpers](../README.md#view-helpers) - Detailed view helper usage
|
|
500
|
+
- [Webpack Configuration](../README.md#webpack-configuration) - JavaScript API
|
|
501
|
+
- [TypeScript](./typescript.md) - Type definitions for Shakapacker
|
|
502
|
+
- [Troubleshooting](./troubleshooting.md) - Common issues
|
|
503
|
+
|
|
504
|
+
## Generating Full API Documentation
|
|
505
|
+
|
|
506
|
+
For complete API documentation with all methods and parameters:
|
|
507
|
+
|
|
508
|
+
```bash
|
|
509
|
+
# Using YARD (recommended)
|
|
510
|
+
gem install yard
|
|
511
|
+
yard doc
|
|
512
|
+
yard server # View at http://localhost:8808
|
|
513
|
+
|
|
514
|
+
# Using RDoc
|
|
515
|
+
rdoc lib/
|
|
516
|
+
open doc/index.html
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
The generated documentation includes all public and private methods with detailed parameter descriptions.
|
data/docs/cdn_setup.md
CHANGED
|
@@ -91,7 +91,7 @@ During deployment, compile your assets as usual:
|
|
|
91
91
|
```bash
|
|
92
92
|
# The SHAKAPACKER_ASSET_HOST will be used during compilation
|
|
93
93
|
# to set the webpack publicPath
|
|
94
|
-
RAILS_ENV=production bundle exec
|
|
94
|
+
RAILS_ENV=production bundle exec rake assets:precompile
|
|
95
95
|
```
|
|
96
96
|
|
|
97
97
|
This ensures that:
|
|
@@ -204,7 +204,7 @@ import("./components/HeavyComponent").then((module) => {
|
|
|
204
204
|
**Solutions**:
|
|
205
205
|
|
|
206
206
|
1. Ensure you set `SHAKAPACKER_ASSET_HOST` **before** running `assets:precompile`
|
|
207
|
-
2. Clear Rails cache: `
|
|
207
|
+
2. Clear Rails cache: `bundle exec rake tmp:cache:clear`
|
|
208
208
|
3. Check the manifest.json file includes CDN URLs:
|
|
209
209
|
```bash
|
|
210
210
|
cat public/packs/manifest.json
|
|
@@ -369,7 +369,7 @@ end
|
|
|
369
369
|
```bash
|
|
370
370
|
# Deployment script
|
|
371
371
|
export SHAKAPACKER_ASSET_HOST=https://cdn.example.com
|
|
372
|
-
RAILS_ENV=production bundle exec
|
|
372
|
+
RAILS_ENV=production bundle exec rake assets:precompile
|
|
373
373
|
```
|
|
374
374
|
|
|
375
375
|
## Summary
|
data/docs/common-upgrades.md
CHANGED
|
@@ -411,16 +411,15 @@ bin/shakapacker compile
|
|
|
411
411
|
Shakapacker provides a convenient rake task to automate the migration:
|
|
412
412
|
|
|
413
413
|
```bash
|
|
414
|
-
# Switch to rspack with automatic dependency management
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
# Or with rake (note the -- separator)
|
|
418
|
-
rake shakapacker:switch_bundler rspack -- --install-deps
|
|
414
|
+
# Switch to rspack with automatic dependency management (note the -- separator)
|
|
415
|
+
bin/rake shakapacker:switch_bundler rspack -- --install-deps
|
|
419
416
|
|
|
420
417
|
# Fast switching without uninstalling webpack (keeps both)
|
|
421
|
-
|
|
418
|
+
bin/rake shakapacker:switch_bundler rspack -- --install-deps --no-uninstall
|
|
422
419
|
```
|
|
423
420
|
|
|
421
|
+
> **⚠️ Important:** This task must be run with `bin/rake`, not `bin/rails`.
|
|
422
|
+
|
|
424
423
|
The task will:
|
|
425
424
|
|
|
426
425
|
- Update `config/shakapacker.yml` to use rspack
|
|
@@ -432,7 +431,7 @@ The task will:
|
|
|
432
431
|
**Custom dependencies:** You can customize which dependencies are installed:
|
|
433
432
|
|
|
434
433
|
```bash
|
|
435
|
-
|
|
434
|
+
bin/rake shakapacker:switch_bundler -- --init-config
|
|
436
435
|
```
|
|
437
436
|
|
|
438
437
|
#### 2. Manual installation (alternative)
|
data/docs/configuration.md
CHANGED
|
@@ -25,7 +25,7 @@ Common configuration options with their defaults:
|
|
|
25
25
|
| ------------------------------ | ------- | --------------------------------------- | ---------------------------------------------------------- |
|
|
26
26
|
| `assets_bundler` | string | `"webpack"` | Bundler to use: `"webpack"` or `"rspack"` |
|
|
27
27
|
| `assets_bundler_config_path` | string | `"config/webpack"` or `"config/rspack"` | Directory containing bundler config files |
|
|
28
|
-
| `javascript_transpiler` | string | `"swc"
|
|
28
|
+
| `javascript_transpiler` | string | `"swc"`\* | Transpiler: `"swc"`, `"babel"`, or `"esbuild"` |
|
|
29
29
|
| `source_path` | string | `"app/javascript"` | Root directory for JavaScript source files |
|
|
30
30
|
| `source_entry_path` | string | `"packs"` | Subdirectory within `source_path` for entry points |
|
|
31
31
|
| `nested_entries` | boolean | `true` | Discover entry points in subdirectories |
|
|
@@ -36,7 +36,7 @@ Common configuration options with their defaults:
|
|
|
36
36
|
| `compiler_strategy` | string | `"mtime"` (dev), `"digest"` (prod) | How to determine if recompilation is needed |
|
|
37
37
|
| `useContentHash` | boolean | `false` (dev), `true` (prod) | Include content hashes in asset filenames |
|
|
38
38
|
| `webpack_compile_output` | boolean | `true` | Show webpack/rspack compilation output |
|
|
39
|
-
| `shakapacker_precompile` | boolean | `true` | Include in `
|
|
39
|
+
| `shakapacker_precompile` | boolean | `true` | Include in `bundle exec rake assets:precompile` |
|
|
40
40
|
| `ensure_consistent_versioning` | boolean | `true` | Enforce gem/npm version matching |
|
|
41
41
|
| `dev_server.host` | string | `"localhost"` | Development server host |
|
|
42
42
|
| `dev_server.port` | number | `3035` | Development server port |
|
|
@@ -44,6 +44,8 @@ Common configuration options with their defaults:
|
|
|
44
44
|
|
|
45
45
|
For detailed explanations, examples, and additional options, see the sections below.
|
|
46
46
|
|
|
47
|
+
**\*Note on `javascript_transpiler` default**: The installation template sets this to `"swc"` for new projects. However, at runtime, if no explicit value is configured, webpack defaults to `"babel"` (for backward compatibility) while rspack defaults to `"swc"`.
|
|
48
|
+
|
|
47
49
|
## Basic Configuration
|
|
48
50
|
|
|
49
51
|
### `assets_bundler`
|
|
@@ -91,23 +93,27 @@ assets_bundler_config_path: "."
|
|
|
91
93
|
### `javascript_transpiler`
|
|
92
94
|
|
|
93
95
|
**Type:** `string`
|
|
94
|
-
**Default:** `"swc"` (
|
|
96
|
+
**Default:** `"swc"` (new installations), `"babel"` (webpack runtime), `"swc"` (rspack runtime)
|
|
95
97
|
**Options:** `"swc"`, `"babel"`, or `"esbuild"`
|
|
96
98
|
|
|
97
99
|
Specifies which transpiler to use for JavaScript/TypeScript.
|
|
98
100
|
|
|
99
101
|
```yaml
|
|
100
|
-
# Use SWC (recommended - 20x faster than Babel)
|
|
102
|
+
# Use SWC (recommended - 20x faster than Babel, set by default in new installations)
|
|
101
103
|
javascript_transpiler: "swc"
|
|
102
104
|
|
|
103
|
-
# Use Babel (for maximum compatibility)
|
|
105
|
+
# Use Babel (for maximum compatibility, webpack runtime default if not configured)
|
|
104
106
|
javascript_transpiler: "babel"
|
|
105
107
|
|
|
106
108
|
# Use esbuild (fastest, but may have compatibility issues)
|
|
107
109
|
javascript_transpiler: "esbuild"
|
|
108
110
|
```
|
|
109
111
|
|
|
110
|
-
**
|
|
112
|
+
**Important default behavior:**
|
|
113
|
+
|
|
114
|
+
- **New installations**: The installation template explicitly sets `javascript_transpiler: "swc"`
|
|
115
|
+
- **Webpack runtime default**: If not explicitly configured, defaults to `"babel"` for backward compatibility
|
|
116
|
+
- **Rspack runtime default**: If not explicitly configured, defaults to `"swc"` as rspack is a newer bundler
|
|
111
117
|
|
|
112
118
|
See [Transpiler Performance Guide](transpiler-performance.md) for benchmarks and migration guides.
|
|
113
119
|
|
|
@@ -395,7 +401,7 @@ production:
|
|
|
395
401
|
**Type:** `boolean`
|
|
396
402
|
**Default:** `true`
|
|
397
403
|
|
|
398
|
-
Whether `
|
|
404
|
+
Whether `bundle exec rake assets:precompile` should compile webpack/rspack assets.
|
|
399
405
|
|
|
400
406
|
```yaml
|
|
401
407
|
# Include in assets:precompile (recommended)
|
|
@@ -408,7 +414,7 @@ shakapacker_precompile: false
|
|
|
408
414
|
**Override via environment variable:**
|
|
409
415
|
|
|
410
416
|
```bash
|
|
411
|
-
SHAKAPACKER_PRECOMPILE=false
|
|
417
|
+
SHAKAPACKER_PRECOMPILE=false bundle exec rake assets:precompile
|
|
412
418
|
```
|
|
413
419
|
|
|
414
420
|
### `cache_manifest`
|