shakapacker 9.0.0 β 9.2.0
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/.gitignore +3 -0
- data/CHANGELOG.md +108 -11
- data/Gemfile.lock +1 -1
- data/README.md +182 -107
- data/bin/export-bundler-config +11 -0
- data/docs/common-upgrades.md +615 -0
- data/docs/deployment.md +52 -8
- data/docs/releasing.md +197 -0
- data/docs/rspack_migration_guide.md +120 -17
- data/docs/transpiler-migration.md +21 -0
- data/docs/troubleshooting.md +124 -23
- data/docs/typescript-migration.md +2 -1
- data/docs/using_swc_loader.md +108 -8
- data/docs/v9_upgrade.md +45 -0
- data/lib/install/bin/export-bundler-config +11 -0
- data/lib/install/bin/shakapacker +1 -1
- data/lib/install/bin/shakapacker-dev-server +1 -1
- data/lib/shakapacker/bundler_switcher.rb +329 -0
- data/lib/shakapacker/configuration.rb +28 -2
- data/lib/shakapacker/doctor.rb +65 -4
- data/lib/shakapacker/rspack_runner.rb +1 -1
- data/lib/shakapacker/runner.rb +1 -1
- data/lib/shakapacker/swc_migrator.rb +14 -6
- data/lib/shakapacker/version.rb +1 -1
- data/lib/shakapacker/webpack_runner.rb +1 -1
- data/lib/shakapacker.rb +10 -0
- data/lib/tasks/shakapacker/export_bundler_config.rake +72 -0
- data/lib/tasks/shakapacker/switch_bundler.rake +82 -0
- data/lib/tasks/shakapacker.rake +2 -1
- data/package/configExporter/cli.ts +683 -0
- data/package/configExporter/configDocs.ts +102 -0
- data/package/configExporter/fileWriter.ts +92 -0
- data/package/configExporter/index.ts +5 -0
- data/package/configExporter/types.ts +36 -0
- data/package/configExporter/yamlSerializer.ts +266 -0
- data/package/environments/__type-tests__/rspack-plugin-compatibility.ts +30 -0
- data/package/environments/types.ts +22 -14
- data/package/index.ts +12 -9
- data/package/swc/index.ts +5 -3
- data/package/types/README.md +2 -1
- data/package/types/index.ts +1 -0
- data/package/utils/debug.ts +5 -5
- data/package-lock.json +13047 -0
- data/package.json +7 -1
- data/yarn.lock +261 -389
- metadata +17 -2
data/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# Shakapacker (v9)
|
|
2
|
+
|
|
2
3
|
---
|
|
3
4
|
|
|
4
5
|
_π Shakapacker 9 supports [Rspack](https://rspack.rs/)! 10x faster than webpack!_
|
|
@@ -7,13 +8,13 @@ _π Shakapacker 9 supports [Rspack](https://rspack.rs/)! 10x faster than webpa
|
|
|
7
8
|
|
|
8
9
|
_Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker). ShakaCode stands behind the long-term maintenance and development of this project for the Rails community._
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
- β οΈ See the [6-stable](https://github.com/shakacode/shakapacker/tree/6-stable) branch for Shakapacker v6.x code and documentation. :warning:
|
|
12
|
+
- **See [V9 Upgrade](./docs/v9_upgrade.md) for upgrading from the v8 release.**
|
|
13
|
+
- See [V8 Upgrade](./docs/v8_upgrade.md) for upgrading from the v7 release.
|
|
14
|
+
- See [V7 Upgrade](./docs/v7_upgrade.md) for upgrading from the v6 release.
|
|
15
|
+
- See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
|
|
15
16
|
|
|
16
|
-
[](https://github.com/shakacode/shakapacker/actions)
|
|
17
18
|
[](https://github.com/shakacode/shakapacker/actions)
|
|
18
19
|
[](https://github.com/shakacode/shakapacker/actions)
|
|
19
20
|
[](https://github.com/shakacode/shakapacker/actions)
|
|
@@ -33,9 +34,10 @@ See a comparison of [Shakapacker with jsbundling-rails](https://github.com/rails
|
|
|
33
34
|
For discussions, see our [Slack Channel](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
34
35
|
|
|
35
36
|
---
|
|
37
|
+
|
|
36
38
|
## ShakaCode Support
|
|
37
39
|
|
|
38
|
-
[ShakaCode](https://www.shakacode.com) focuses on helping Ruby on Rails teams use React and Webpack better. We can upgrade your project and improve your development and customer experiences, allowing you to focus on building new features or fixing bugs instead.
|
|
40
|
+
[ShakaCode](https://www.shakacode.com) focuses on helping Ruby on Rails teams use React and Webpack better. We can upgrade your project and improve your development and customer experiences, allowing you to focus on building new features or fixing bugs instead.
|
|
39
41
|
|
|
40
42
|
For an overview of working with us, see our [Client Engagement Model](https://www.shakacode.com/blog/client-engagement-model/) article and [how we bill for time](https://www.shakacode.com/blog/shortcut-jira-trello-github-toggl-time-and-task-tracking/).
|
|
41
43
|
|
|
@@ -44,6 +46,7 @@ We also specialize in helping development teams lower infrastructure and CI cost
|
|
|
44
46
|
If you think ShakaCode can help your project, [click here](https://meetings.hubspot.com/justingordon/30-minute-consultation) to book a call with [Justin Gordon](mailto:justin@shakacode.com), the creator of React on Rails and Shakapacker.
|
|
45
47
|
|
|
46
48
|
Here's a testimonial of how ShakaCode can help from [Florian GΓΆΓler](https://github.com/FGoessler) of [Blinkist](https://www.blinkist.com/), January 2, 2023:
|
|
49
|
+
|
|
47
50
|
> Hey Justin π
|
|
48
51
|
>
|
|
49
52
|
> I just wanted to let you know that we today shipped the webpacker to shakapacker upgrades and it all seems to be running smoothly! Thanks again for all your support and your teams work! π
|
|
@@ -59,50 +62,50 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
|
|
|
59
62
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
60
63
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
65
|
+
- [Prerequisites](#prerequisites)
|
|
66
|
+
- [Features](#features)
|
|
67
|
+
- [Optional support](#optional-support)
|
|
68
|
+
- [Installation](#installation)
|
|
69
|
+
- [Rails v6+](#rails-v6)
|
|
70
|
+
- [Concepts](#concepts)
|
|
71
|
+
- [Usage](#usage)
|
|
72
|
+
- [Configuration and Code](#configuration-and-code)
|
|
73
|
+
- [View Helpers](#view-helpers)
|
|
74
|
+
- [View Helpers `javascript_pack_tag` and `stylesheet_pack_tag`](#view-helpers-javascript_pack_tag-and-stylesheet_pack_tag)
|
|
75
|
+
- [View Helpers `append_javascript_pack_tag`, `prepend_javascript_pack_tag` and `append_stylesheet_pack_tag`](#view-helper-append_javascript_pack_tag-prepend_javascript_pack_tag-and-append_stylesheet_pack_tag)
|
|
76
|
+
- [View Helper: `asset_pack_path`](#view-helper-asset_pack_path)
|
|
77
|
+
- [View Helper: `image_pack_tag`](#view-helper-image_pack_tag)
|
|
78
|
+
- [View Helper: `favicon_pack_tag`](#view-helper-favicon_pack_tag)
|
|
79
|
+
- [View Helper: `preload_pack_asset`](#view-helper-preload_pack_asset)
|
|
80
|
+
- [Images in Stylesheets](#images-in-stylesheets)
|
|
81
|
+
- [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
|
|
82
|
+
- [Development](#development)
|
|
83
|
+
- [Automatic Webpack Code Building](#automatic-webpack-code-building)
|
|
84
|
+
- [Compiler strategies](#compiler-strategies)
|
|
85
|
+
- [Common Development Commands](#common-development-commands)
|
|
86
|
+
- [Webpack Configuration](#webpack-configuration)
|
|
87
|
+
- [Babel configuration](#babel-configuration)
|
|
88
|
+
- [SWC configuration](#swc-configuration)
|
|
89
|
+
- [esbuild loader configuration](#esbuild-loader-configuration)
|
|
90
|
+
- [Integrations](#integrations)
|
|
91
|
+
- [React](#react)
|
|
92
|
+
- [Typescript](#typescript)
|
|
93
|
+
- [CSS](#css)
|
|
94
|
+
- [Postcss](#postcss)
|
|
95
|
+
- [Sass](#sass)
|
|
96
|
+
- [Less](#less)
|
|
97
|
+
- [Stylus](#stylus)
|
|
98
|
+
- [CoffeeScript](#coffeescript)
|
|
99
|
+
- [Other frameworks](#other-frameworks)
|
|
100
|
+
- [Custom Rails environments](#custom-rails-environments)
|
|
101
|
+
- [Upgrading](#upgrading)
|
|
102
|
+
- [Paths](#paths)
|
|
103
|
+
- [Additional paths](#additional-paths)
|
|
104
|
+
- [Deployment](#deployment)
|
|
105
|
+
- [Example Apps](#example-apps)
|
|
106
|
+
- [Troubleshooting](#troubleshooting)
|
|
107
|
+
- [Contributing](#contributing)
|
|
108
|
+
- [License](#license)
|
|
106
109
|
- [Supporters](#supporters)
|
|
107
110
|
|
|
108
111
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
@@ -114,6 +117,7 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
|
|
|
114
117
|
- Node.js 14+
|
|
115
118
|
|
|
116
119
|
## Features
|
|
120
|
+
|
|
117
121
|
- Rails view helpers that fully support Webpack output, including HMR and code splitting.
|
|
118
122
|
- Convenient but not required webpack configuration. The only requirement is that your webpack configuration creates a manifest.
|
|
119
123
|
- HMR with the `shakapacker-dev-server`, such as for hot-reloading React!
|
|
@@ -126,17 +130,20 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
|
|
|
126
130
|
- Extensible and configurable. For example, all major dependencies are specified as peers, so you can upgrade easily.
|
|
127
131
|
|
|
128
132
|
### Optional support
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
|
|
134
|
+
_Requires extra packages to be installed._
|
|
135
|
+
|
|
136
|
+
- React
|
|
137
|
+
- TypeScript
|
|
138
|
+
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
|
|
139
|
+
- CoffeeScript
|
|
134
140
|
|
|
135
141
|
## Installation
|
|
136
142
|
|
|
137
143
|
### Rails v6+
|
|
138
144
|
|
|
139
145
|
With Rails v6+, skip JavaScript for a new app and follow below Manual Installation Steps to manually add the `shakapacker` gem to your Gemfile.
|
|
146
|
+
|
|
140
147
|
```bash
|
|
141
148
|
rails new myapp --skip-javascript
|
|
142
149
|
```
|
|
@@ -178,7 +185,7 @@ If you wish to use [Yarn PnP](https://yarnpkg.com/features/pnp) you will need to
|
|
|
178
185
|
> [!NOTE]
|
|
179
186
|
>
|
|
180
187
|
> The rest of the documentation will only reference `npm` when providing commands such as to install optional packages except in cases where
|
|
181
|
-
> a particular package manager requires a very different command; otherwise it should be safe to just replace `npm` with the name of your
|
|
188
|
+
> a particular package manager requires a very different command; otherwise it should be safe to just replace `npm` with the name of your
|
|
182
189
|
> preferred package manager when running the command
|
|
183
190
|
|
|
184
191
|
Note, in v6+, most JS packages are peer dependencies. Thus, the installer will add the packages:
|
|
@@ -203,6 +210,7 @@ making these peer dependencies, you have control over the versions used in your
|
|
|
203
210
|
### Optional Peer Dependencies
|
|
204
211
|
|
|
205
212
|
All peer dependencies in Shakapacker are marked as optional via `peerDependenciesMeta`. This design decision ensures:
|
|
213
|
+
|
|
206
214
|
- **No warnings during package installation** when dependencies are not needed
|
|
207
215
|
- **Clear visibility of supported package versions** for upgrades
|
|
208
216
|
- **Flexibility to choose only the tools you need** (webpack vs rspack, babel vs swc vs esbuild)
|
|
@@ -215,6 +223,7 @@ version compatibility constraints when you do install those packages.
|
|
|
215
223
|
Depending on your setup, you'll need different subsets of the optional peer dependencies:
|
|
216
224
|
|
|
217
225
|
**For Webpack + Babel (traditional setup):**
|
|
226
|
+
|
|
218
227
|
```json
|
|
219
228
|
{
|
|
220
229
|
"dependencies": {
|
|
@@ -235,6 +244,7 @@ Depending on your setup, you'll need different subsets of the optional peer depe
|
|
|
235
244
|
```
|
|
236
245
|
|
|
237
246
|
**For Webpack + SWC (faster alternative):**
|
|
247
|
+
|
|
238
248
|
```json
|
|
239
249
|
{
|
|
240
250
|
"dependencies": {
|
|
@@ -252,6 +262,7 @@ Depending on your setup, you'll need different subsets of the optional peer depe
|
|
|
252
262
|
```
|
|
253
263
|
|
|
254
264
|
**For Rspack + SWC (10x faster bundling):**
|
|
265
|
+
|
|
255
266
|
```json
|
|
256
267
|
{
|
|
257
268
|
"dependencies": {
|
|
@@ -265,7 +276,21 @@ Depending on your setup, you'll need different subsets of the optional peer depe
|
|
|
265
276
|
}
|
|
266
277
|
```
|
|
267
278
|
|
|
279
|
+
**Quick tip:** You can easily switch between webpack and rspack using:
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
rails shakapacker:switch_bundler rspack --install-deps
|
|
283
|
+
# or with rake (note the -- separator)
|
|
284
|
+
rake shakapacker:switch_bundler rspack -- --install-deps
|
|
285
|
+
|
|
286
|
+
# For faster switching, use --no-uninstall to keep both bundlers installed
|
|
287
|
+
rails shakapacker:switch_bundler webpack --install-deps --no-uninstall
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
See the [Rspack Migration Guide](./docs/rspack_migration_guide.md) for details.
|
|
291
|
+
|
|
268
292
|
**For CSS/Sass processing (add to any config above):**
|
|
293
|
+
|
|
269
294
|
```json
|
|
270
295
|
{
|
|
271
296
|
"dependencies": {
|
|
@@ -294,10 +319,11 @@ You will need your file system to correspond to the setup of your `config/shakap
|
|
|
294
319
|
Suppose you have the following configuration:
|
|
295
320
|
|
|
296
321
|
`shakapacker.yml`
|
|
322
|
+
|
|
297
323
|
```yml
|
|
298
324
|
default: &default
|
|
299
325
|
source_path: app/javascript
|
|
300
|
-
source_entry_path: packs
|
|
326
|
+
source_entry_path: packs
|
|
301
327
|
public_root_path: public
|
|
302
328
|
public_output_path: packs
|
|
303
329
|
nested_entries: false
|
|
@@ -331,6 +357,7 @@ To enable/disable the usage of contentHash in any node environment (specified us
|
|
|
331
357
|
You can use the environment variable `SHAKAPACKER_CONFIG` to enforce a particular path to the config file rather than the default `config/shakapacker.yml`.
|
|
332
358
|
|
|
333
359
|
### View Helpers
|
|
360
|
+
|
|
334
361
|
The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views.
|
|
335
362
|
|
|
336
363
|
Be sure to consult the API documentation in the source code of [helper.rb](./lib/shakapacker/helper.rb).
|
|
@@ -358,6 +385,7 @@ You can provide multiple packs and other attributes. Note, `defer` defaults to s
|
|
|
358
385
|
```
|
|
359
386
|
|
|
360
387
|
The resulting HTML would look like this:
|
|
388
|
+
|
|
361
389
|
```html
|
|
362
390
|
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
363
391
|
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
@@ -390,7 +418,7 @@ Note that when using `async: true`, scripts may execute in any order as soon as
|
|
|
390
418
|
>
|
|
391
419
|
> When both `async` and `defer` attributes are specified, `async` takes precedence according to HTML5 specifications. So if you pass both `async: true` and `defer: true`, the script tag will use `async`.
|
|
392
420
|
|
|
393
|
-
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using `javascript_pack_tag` and the `stylesheet_pack_tag`. Otherwise, you will get duplicated chunks on the page.
|
|
421
|
+
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using `javascript_pack_tag` and the `stylesheet_pack_tag`. Otherwise, you will get duplicated chunks on the page.
|
|
394
422
|
|
|
395
423
|
```erb
|
|
396
424
|
<%# DO %>
|
|
@@ -406,7 +434,7 @@ you may use multiple calls to stylesheet_pack_tag if,
|
|
|
406
434
|
say,
|
|
407
435
|
you require multiple `<style>` tags for different output media:
|
|
408
436
|
|
|
409
|
-
```
|
|
437
|
+
```erb
|
|
410
438
|
<%= stylesheet_pack_tag 'application', media: 'screen' %>
|
|
411
439
|
<%= stylesheet_pack_tag 'print', media: 'print' %>
|
|
412
440
|
```
|
|
@@ -416,18 +444,21 @@ you require multiple `<style>` tags for different output media:
|
|
|
416
444
|
If you need to configure your script pack names or stylesheet pack names from the view for a route or partials, then you will need some logic to ensure you call the helpers only once with multiple arguments. The new view helpers, `append_javascript_pack_tag` and `append_stylesheet_pack_tag` can solve this problem. The helper `append_javascript_pack_tag` will queue up script packs when the `javascript_pack_tag` is finally used. Similarly,`append_stylesheet_pack_tag` will queue up style packs when the `stylesheet_pack_tag` is finally used.
|
|
417
445
|
|
|
418
446
|
Main view:
|
|
447
|
+
|
|
419
448
|
```erb
|
|
420
449
|
<% append_javascript_pack_tag 'calendar' %>
|
|
421
450
|
<% append_stylesheet_pack_tag 'calendar' %>
|
|
422
451
|
```
|
|
423
452
|
|
|
424
453
|
Some partial:
|
|
454
|
+
|
|
425
455
|
```erb
|
|
426
456
|
<% append_javascript_pack_tag 'map' %>
|
|
427
457
|
<% append_stylesheet_pack_tag 'map' %>
|
|
428
458
|
```
|
|
429
459
|
|
|
430
460
|
And the main layout has:
|
|
461
|
+
|
|
431
462
|
```erb
|
|
432
463
|
<%= javascript_pack_tag 'application' %>
|
|
433
464
|
<%= stylesheet_pack_tag 'application' %>
|
|
@@ -449,9 +480,9 @@ Thus, you can distribute the logic of what packs are needed for any route. All t
|
|
|
449
480
|
The typical issue is that your layout might reference some partials that need to configure packs. A good way to solve this problem is to use `content_for` to ensure that the code to render your partial comes before the call to `javascript_pack_tag`.
|
|
450
481
|
|
|
451
482
|
```erb
|
|
452
|
-
<% content_for :footer do
|
|
483
|
+
<% content_for :footer do
|
|
453
484
|
render 'shared/footer' %>
|
|
454
|
-
|
|
485
|
+
|
|
455
486
|
<%= javascript_pack_tag %>
|
|
456
487
|
|
|
457
488
|
<%= content_for :footer %>
|
|
@@ -460,16 +491,19 @@ The typical issue is that your layout might reference some partials that need to
|
|
|
460
491
|
There is also `prepend_javascript_pack_tag` that will put the entry at the front of the queue. This is handy when you want an entry in the main layout to go before the partial and main layout `append_javascript_pack_tag` entries.
|
|
461
492
|
|
|
462
493
|
Main view:
|
|
494
|
+
|
|
463
495
|
```erb
|
|
464
496
|
<% append_javascript_pack_tag 'map' %>
|
|
465
497
|
```
|
|
466
498
|
|
|
467
499
|
Some partial:
|
|
500
|
+
|
|
468
501
|
```erb
|
|
469
502
|
<% append_javascript_pack_tag 'map' %>
|
|
470
503
|
```
|
|
471
504
|
|
|
472
505
|
And the main layout has:
|
|
506
|
+
|
|
473
507
|
```erb
|
|
474
508
|
<% prepend_javascript_pack_tag 'main' %>
|
|
475
509
|
<%= javascript_pack_tag 'application' %>
|
|
@@ -486,6 +520,7 @@ For alternative options for setting the additional packs, [see this discussion](
|
|
|
486
520
|
#### View Helper: `asset_pack_path`
|
|
487
521
|
|
|
488
522
|
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
|
523
|
+
|
|
489
524
|
```erb
|
|
490
525
|
<img src="<%= asset_pack_path 'static/logo.svg' %>" />
|
|
491
526
|
```
|
|
@@ -493,30 +528,35 @@ If you want to link a static asset for `<img />` tag, you can use the `asset_pac
|
|
|
493
528
|
#### View Helper: `image_pack_tag`
|
|
494
529
|
|
|
495
530
|
Or use the dedicated helper:
|
|
531
|
+
|
|
496
532
|
```erb
|
|
497
533
|
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
|
498
534
|
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
|
499
535
|
```
|
|
500
536
|
|
|
501
537
|
#### View Helper: `favicon_pack_tag`
|
|
538
|
+
|
|
502
539
|
If you want to create a favicon:
|
|
540
|
+
|
|
503
541
|
```erb
|
|
504
542
|
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
|
505
543
|
```
|
|
506
544
|
|
|
507
545
|
#### View Helper: `preload_pack_asset`
|
|
546
|
+
|
|
508
547
|
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
|
548
|
+
|
|
509
549
|
```erb
|
|
510
550
|
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
|
511
551
|
```
|
|
512
552
|
|
|
513
|
-
|
|
514
553
|
### Images in Stylesheets
|
|
554
|
+
|
|
515
555
|
If you want to use images in your stylesheets:
|
|
516
556
|
|
|
517
557
|
```css
|
|
518
558
|
.foo {
|
|
519
|
-
background-image: url(
|
|
559
|
+
background-image: url("../images/logo.svg");
|
|
520
560
|
}
|
|
521
561
|
```
|
|
522
562
|
|
|
@@ -596,25 +636,24 @@ end
|
|
|
596
636
|
|
|
597
637
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
|
598
638
|
|
|
599
|
-
|
|
600
639
|
### Webpack Configuration
|
|
601
640
|
|
|
602
641
|
First, you don't _need_ to use Shakapacker's webpack configuration. However, the `shakapacker` NPM package provides convenient access to configuration code that reads the `config/shakapacker.yml` file which the view helpers also use. If you have your customized webpack configuration, at the minimum, you must ensure:
|
|
603
642
|
|
|
604
643
|
1. Your output files go to the right directory
|
|
605
644
|
2. Your output includes a manifest, via package [`webpack-assets-manifest`](https://github.com/webdeveric/webpack-assets-manifest) that maps output names (your 'packs') to the fingerprinted versions, including bundle-splitting dependencies. That's the main secret sauce of Shakapacker!
|
|
606
|
-
|
|
645
|
+
|
|
607
646
|
The webpack configuration used by Shakapacker lives in `config/webpack/webpack.config.js`; this makes it easy to customize the configuration beyond what's available in `config/shakapacker.yml` by giving you complete control of the final configuration. By default, this file exports the result of `generateWebpackConfig` which handles generating a webpack configuration based on `config/shakapacker.yml`.
|
|
608
647
|
|
|
609
648
|
The easiest way to modify this config is to pass your desired customizations to `generateWebpackConfig` which will use [webpack-merge](https://github.com/survivejs/webpack-merge) to merge them with the configuration generated from `config/shakapacker.yml`:
|
|
610
649
|
|
|
611
650
|
```js
|
|
612
651
|
// config/webpack/webpack.config.js
|
|
613
|
-
const { generateWebpackConfig } = require(
|
|
652
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
614
653
|
|
|
615
654
|
const options = {
|
|
616
655
|
resolve: {
|
|
617
|
-
|
|
656
|
+
extensions: [".css", ".ts", ".tsx"]
|
|
618
657
|
}
|
|
619
658
|
}
|
|
620
659
|
|
|
@@ -626,13 +665,13 @@ The `shakapacker` package also exports the `merge` function from [webpack-merge]
|
|
|
626
665
|
|
|
627
666
|
```js
|
|
628
667
|
// config/webpack/webpack.config.js
|
|
629
|
-
const { generateWebpackConfig, merge } = require(
|
|
668
|
+
const { generateWebpackConfig, merge } = require("shakapacker")
|
|
630
669
|
|
|
631
670
|
const webpackConfig = generateWebpackConfig()
|
|
632
671
|
|
|
633
672
|
const options = {
|
|
634
673
|
resolve: {
|
|
635
|
-
extensions: [
|
|
674
|
+
extensions: [".css", ".ts", ".tsx"]
|
|
636
675
|
}
|
|
637
676
|
}
|
|
638
677
|
|
|
@@ -650,11 +689,11 @@ You might add separate files to keep your code more organized.
|
|
|
650
689
|
module.exports = {
|
|
651
690
|
resolve: {
|
|
652
691
|
alias: {
|
|
653
|
-
jquery:
|
|
654
|
-
vue:
|
|
655
|
-
React:
|
|
656
|
-
ReactDOM:
|
|
657
|
-
vue_resource:
|
|
692
|
+
jquery: "jquery/src/jquery",
|
|
693
|
+
vue: "vue/dist/vue.js",
|
|
694
|
+
React: "react",
|
|
695
|
+
ReactDOM: "react-dom",
|
|
696
|
+
vue_resource: "vue-resource/dist/vue-resource"
|
|
658
697
|
}
|
|
659
698
|
}
|
|
660
699
|
}
|
|
@@ -665,9 +704,9 @@ Then `require` this file in your `config/webpack/webpack.config.js`:
|
|
|
665
704
|
```js
|
|
666
705
|
// config/webpack/webpack.config.js
|
|
667
706
|
// use the new NPM package name, `shakapacker`.
|
|
668
|
-
const { generateWebpackConfig } = require(
|
|
707
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
669
708
|
|
|
670
|
-
const customConfig = require(
|
|
709
|
+
const customConfig = require("./custom")
|
|
671
710
|
|
|
672
711
|
module.exports = generateWebpackConfig(customConfig)
|
|
673
712
|
```
|
|
@@ -676,7 +715,7 @@ If you need access to configs within Shakapacker's configuration, you can import
|
|
|
676
715
|
|
|
677
716
|
```js
|
|
678
717
|
// config/webpack/webpack.config.js
|
|
679
|
-
const { generateWebpackConfig } = require(
|
|
718
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
680
719
|
|
|
681
720
|
const webpackConfig = generateWebpackConfig()
|
|
682
721
|
|
|
@@ -690,11 +729,12 @@ console.log(JSON.stringify(webpackConfig, undefined, 2))
|
|
|
690
729
|
You may want to modify the rules in the default configuration. For instance, if you are using a custom svg loader, you may want to remove `.svg` from the default file loader rules. You can search and filter the default rules like so:
|
|
691
730
|
|
|
692
731
|
```js
|
|
693
|
-
const fileRule = config.module.rules.find(rule => rule.test.test(
|
|
732
|
+
const fileRule = config.module.rules.find((rule) => rule.test.test(".svg"))
|
|
694
733
|
// removing svg from asset file rule's test RegExp
|
|
695
|
-
fileRule.test =
|
|
734
|
+
fileRule.test =
|
|
735
|
+
/\.(bmp|gif|jpe?g|png|tiff|ico|avif|webp|eot|otf|ttf|woff|woff2)$/
|
|
696
736
|
// changing the rule type from 'asset/resource' to 'asset'. See https://webpack.js.org/guides/asset-modules/
|
|
697
|
-
fileRule.type =
|
|
737
|
+
fileRule.type = "asset"
|
|
698
738
|
```
|
|
699
739
|
|
|
700
740
|
### Babel configuration
|
|
@@ -708,8 +748,8 @@ By default, you will find the Shakapacker preset in your `package.json`. Note, y
|
|
|
708
748
|
]
|
|
709
749
|
},
|
|
710
750
|
```
|
|
711
|
-
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and adding [a Babel configuration file](https://babeljs.io/docs/en/config-files) to your project. For an example of customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
|
|
712
751
|
|
|
752
|
+
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and adding [a Babel configuration file](https://babeljs.io/docs/en/config-files) to your project. For an example of customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
|
|
713
753
|
|
|
714
754
|
### SWC configuration
|
|
715
755
|
|
|
@@ -727,6 +767,27 @@ Please note that if you want opt-in to use esbuild-loader, you can skip [React](
|
|
|
727
767
|
|
|
728
768
|
To switch between Babel, SWC, or esbuild, or to configure environment-specific transpiler settings, see the [Transpiler Migration Guide](./docs/transpiler-migration.md).
|
|
729
769
|
|
|
770
|
+
### Debugging Configuration
|
|
771
|
+
|
|
772
|
+
Shakapacker provides a powerful utility to export and analyze your webpack/rspack configuration:
|
|
773
|
+
|
|
774
|
+
```bash
|
|
775
|
+
# Export all configs for troubleshooting (recommended)
|
|
776
|
+
bin/export-bundler-config --doctor
|
|
777
|
+
|
|
778
|
+
# Or via rake task
|
|
779
|
+
bundle exec rake shakapacker:export_bundler_config -- --doctor
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
This exports development and production configurations for both client and server bundles to `shakapacker-config-exports/` directory in annotated YAML format. Perfect for:
|
|
783
|
+
|
|
784
|
+
- Debugging configuration issues
|
|
785
|
+
- Comparing webpack vs rspack configs (works with `rake shakapacker:switch_bundler`)
|
|
786
|
+
- Understanding differences between development and production
|
|
787
|
+
- Analyzing client vs server bundle configurations
|
|
788
|
+
|
|
789
|
+
For more options and usage examples, see the [Troubleshooting Guide](./docs/troubleshooting.md#exporting-webpack--rspack-configuration).
|
|
790
|
+
|
|
730
791
|
### Integrations
|
|
731
792
|
|
|
732
793
|
Shakapacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
|
|
@@ -776,30 +837,30 @@ Then modify the webpack config to use it as a plugin:
|
|
|
776
837
|
|
|
777
838
|
```js
|
|
778
839
|
// config/webpack/webpack.config.js
|
|
779
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
780
|
-
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
|
|
840
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
841
|
+
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
|
|
781
842
|
|
|
782
843
|
module.exports = generateWebpackConfig({
|
|
783
|
-
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
784
|
-
})
|
|
844
|
+
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
845
|
+
})
|
|
785
846
|
```
|
|
786
847
|
|
|
787
848
|
Optionally, your webpack config file itself can be written in Typescript:
|
|
788
849
|
|
|
789
|
-
```
|
|
850
|
+
```bash
|
|
790
851
|
npm install ts-node @types/node @types/webpack
|
|
791
852
|
```
|
|
792
853
|
|
|
793
854
|
```ts
|
|
794
855
|
// config/webpack/webpack.config.ts
|
|
795
|
-
import { generateWebpackConfig } from "shakapacker"
|
|
796
|
-
import ForkTSCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"
|
|
856
|
+
import { generateWebpackConfig } from "shakapacker"
|
|
857
|
+
import ForkTSCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"
|
|
797
858
|
|
|
798
859
|
const config = generateWebpackConfig({
|
|
799
|
-
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
800
|
-
})
|
|
860
|
+
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
861
|
+
})
|
|
801
862
|
|
|
802
|
-
export default config
|
|
863
|
+
export default config
|
|
803
864
|
```
|
|
804
865
|
|
|
805
866
|
#### CSS
|
|
@@ -814,11 +875,11 @@ Optionally, add the `CSS` extension to webpack config for easy resolution.
|
|
|
814
875
|
|
|
815
876
|
```js
|
|
816
877
|
// config/webpack/webpack.config.js
|
|
817
|
-
const { generateWebpackConfig } = require(
|
|
878
|
+
const { generateWebpackConfig } = require("shakapacker")
|
|
818
879
|
|
|
819
880
|
const customConfig = {
|
|
820
881
|
resolve: {
|
|
821
|
-
extensions: [
|
|
882
|
+
extensions: [".css"]
|
|
822
883
|
}
|
|
823
884
|
}
|
|
824
885
|
|
|
@@ -835,6 +896,7 @@ npm install postcss postcss-loader
|
|
|
835
896
|
```
|
|
836
897
|
|
|
837
898
|
Optionally add these two plugins if they are required in your `postcss.config.js`:
|
|
899
|
+
|
|
838
900
|
```bash
|
|
839
901
|
npm install postcss-preset-env postcss-flexbugs-fixes
|
|
840
902
|
```
|
|
@@ -850,16 +912,19 @@ You will also need to install [Dart Sass](https://github.com/sass/dart-sass), [N
|
|
|
850
912
|
Please refer to [sass-loader documentation](https://www.npmjs.com/package/sass-loader) and individual packages repos for more information on all the options.
|
|
851
913
|
|
|
852
914
|
##### Dart Sass
|
|
915
|
+
|
|
853
916
|
```bash
|
|
854
917
|
npm install sass
|
|
855
918
|
```
|
|
856
919
|
|
|
857
920
|
##### Node Sass
|
|
921
|
+
|
|
858
922
|
```bash
|
|
859
923
|
npm install node-sass
|
|
860
924
|
```
|
|
861
925
|
|
|
862
926
|
##### Sass Embedded
|
|
927
|
+
|
|
863
928
|
```bash
|
|
864
929
|
npm install sass-embedded
|
|
865
930
|
```
|
|
@@ -891,33 +956,34 @@ Please follow Webpack integration guide for the relevant framework or library,
|
|
|
891
956
|
3. [Vue](https://vue-loader.vuejs.org/guide/)
|
|
892
957
|
|
|
893
958
|
For example to add Vue support:
|
|
959
|
+
|
|
894
960
|
```js
|
|
895
961
|
// config/webpack/rules/vue.js
|
|
896
|
-
const { VueLoaderPlugin } = require(
|
|
962
|
+
const { VueLoaderPlugin } = require("vue-loader")
|
|
897
963
|
|
|
898
964
|
module.exports = {
|
|
899
965
|
module: {
|
|
900
966
|
rules: [
|
|
901
967
|
{
|
|
902
968
|
test: /\.vue$/,
|
|
903
|
-
loader:
|
|
969
|
+
loader: "vue-loader"
|
|
904
970
|
}
|
|
905
971
|
]
|
|
906
972
|
},
|
|
907
973
|
plugins: [new VueLoaderPlugin()],
|
|
908
974
|
resolve: {
|
|
909
|
-
extensions: [
|
|
975
|
+
extensions: [".vue"]
|
|
910
976
|
}
|
|
911
977
|
}
|
|
912
978
|
```
|
|
913
979
|
|
|
914
980
|
```js
|
|
915
981
|
// config/webpack/webpack.config.js
|
|
916
|
-
const { generateWebpackConfig, merge } = require(
|
|
982
|
+
const { generateWebpackConfig, merge } = require("shakapacker")
|
|
917
983
|
|
|
918
984
|
const webpackConfig = generateWebpackConfig()
|
|
919
985
|
|
|
920
|
-
const vueConfig = require(
|
|
986
|
+
const vueConfig = require("./rules/vue")
|
|
921
987
|
|
|
922
988
|
module.exports = merge(vueConfig, webpackConfig)
|
|
923
989
|
```
|
|
@@ -1001,6 +1067,14 @@ npm install shakapacker@next
|
|
|
1001
1067
|
|
|
1002
1068
|
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
1003
1069
|
|
|
1070
|
+
#### Common Upgrade Scenarios
|
|
1071
|
+
|
|
1072
|
+
For step-by-step guides on common migrations, see the [Common Upgrades Guide](./docs/common-upgrades.md):
|
|
1073
|
+
|
|
1074
|
+
- [Migrating Package Managers](./docs/common-upgrades.md#migrating-package-managers) (Yarn β npm, pnpm)
|
|
1075
|
+
- [Migrating from Babel to SWC](./docs/common-upgrades.md#migrating-from-babel-to-swc) (20-70x faster builds)
|
|
1076
|
+
- [Migrating from Webpack to Rspack](./docs/common-upgrades.md#migrating-from-webpack-to-rspack) (5-10x faster builds)
|
|
1077
|
+
|
|
1004
1078
|
### Paths
|
|
1005
1079
|
|
|
1006
1080
|
By default, Shakapacker ships with simple conventions for where the JavaScript app files and compiled webpack bundles will go in your Rails app. All these options are configurable from `config/shakapacker.yml` file.
|
|
@@ -1029,11 +1103,12 @@ This is particularly useful when working with libraries like React on Rails wher
|
|
|
1029
1103
|
If you're using React on Rails with separate client and server bundles, you can now leverage the `private_output_path` configuration instead of using custom webpack configurations:
|
|
1030
1104
|
|
|
1031
1105
|
1. Update your `config/shakapacker.yml`:
|
|
1106
|
+
|
|
1032
1107
|
```yml
|
|
1033
1108
|
# Before: both client and server bundles in public/
|
|
1034
1109
|
# After: separate directories
|
|
1035
|
-
public_output_path: packs
|
|
1036
|
-
private_output_path: ssr-bundles
|
|
1110
|
+
public_output_path: packs # Client bundles (publicly served)
|
|
1111
|
+
private_output_path: ssr-bundles # Server bundles (not publicly served)
|
|
1037
1112
|
```
|
|
1038
1113
|
|
|
1039
1114
|
2. Update your webpack configuration to use the appropriate output path based on the bundle type
|
|
@@ -1059,15 +1134,15 @@ If you are adding Shakapacker to an existing app that has most of the assets ins
|
|
|
1059
1134
|
add additional paths that webpack should look up when resolving modules:
|
|
1060
1135
|
|
|
1061
1136
|
```yml
|
|
1062
|
-
additional_paths: [
|
|
1137
|
+
additional_paths: ["app/assets", "vendor/assets"]
|
|
1063
1138
|
```
|
|
1064
1139
|
|
|
1065
1140
|
You can then import these items inside your modules like so:
|
|
1066
1141
|
|
|
1067
1142
|
```js
|
|
1068
1143
|
// Note it's relative to parent directory i.e. app/assets
|
|
1069
|
-
import
|
|
1070
|
-
import
|
|
1144
|
+
import "stylesheets/main"
|
|
1145
|
+
import "images/rails.png"
|
|
1071
1146
|
```
|
|
1072
1147
|
|
|
1073
1148
|
Assets put in these folders will also have their path stripped just like with the `source_path`.
|
|
@@ -1080,7 +1155,6 @@ A file in `app/assets/images/image.svg` with `additional_paths: ['app/assets']`
|
|
|
1080
1155
|
|
|
1081
1156
|
**Also note:** While importing assets living outside your `source_path` defined in shakapacker.yml (like, for instance, assets under `app/assets`) from within your packs using _relative_ paths like `import '../../assets/javascripts/file.js'` will work in development, Shakapacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Shakapacker::MtimeStrategy#latest_modified_timestamp` or `Shakapacker::DigestStrategy#watched_files_digest` depending on strategy configured by `compiler_strategy` option in `shakapacker.yml`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
|
|
1082
1157
|
|
|
1083
|
-
|
|
1084
1158
|
## Deployment
|
|
1085
1159
|
|
|
1086
1160
|
Shakapacker hooks up a new `shakapacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `shakapacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/shakapacker.yml` (if available).
|
|
@@ -1113,6 +1187,7 @@ Shakapacker supports serving JavaScript bundles and assets from a CDN. The key c
|
|
|
1113
1187
|
For detailed CDN setup instructions, including CloudFlare configuration, troubleshooting, and advanced setups, see the [CDN Setup Guide](./docs/cdn_setup.md).
|
|
1114
1188
|
|
|
1115
1189
|
**Quick example:**
|
|
1190
|
+
|
|
1116
1191
|
```bash
|
|
1117
1192
|
export SHAKAPACKER_ASSET_HOST=https://cdn.example.com
|
|
1118
1193
|
RAILS_ENV=production bundle exec rails assets:precompile
|
|
@@ -1121,8 +1196,8 @@ RAILS_ENV=production bundle exec rails assets:precompile
|
|
|
1121
1196
|
For more deployment documentation, see [Deployment](./docs/deployment.md).
|
|
1122
1197
|
|
|
1123
1198
|
## Example Apps
|
|
1124
|
-
* [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh)
|
|
1125
1199
|
|
|
1200
|
+
- [React on Rails Tutorial With SSR, HMR fast refresh, and TypeScript](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh)
|
|
1126
1201
|
|
|
1127
1202
|
## Troubleshooting
|
|
1128
1203
|
|
|
@@ -1135,7 +1210,7 @@ We encourage you to contribute to Shakapacker! See [CONTRIBUTING](CONTRIBUTING.m
|
|
|
1135
1210
|
## License
|
|
1136
1211
|
|
|
1137
1212
|
Shakapacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
|
1138
|
-
|
|
1213
|
+
|
|
1139
1214
|
## Supporters
|
|
1140
1215
|
|
|
1141
1216
|
The following companies support our Open Source projects, and ShakaCode uses their products!
|