shakapacker 10.1.0 → 10.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/CHANGELOG.md +29 -1
- data/README.md +116 -1368
- data/lib/install/bin/diff-bundler-config +89 -18
- data/lib/install/bin/shakapacker-config +89 -18
- data/lib/install/config/shakapacker.yml +7 -1
- data/lib/install/package.json +5 -3
- data/lib/install/template.rb +91 -12
- data/lib/shakapacker/base_strategy.rb +10 -4
- data/lib/shakapacker/bundler_switcher.rb +22 -6
- data/lib/shakapacker/compiler.rb +38 -8
- data/lib/shakapacker/compiler_strategy.rb +4 -4
- data/lib/shakapacker/configuration.rb +57 -0
- data/lib/shakapacker/dev_server_runner.rb +44 -19
- data/lib/shakapacker/digest_strategy.rb +3 -3
- data/lib/shakapacker/doctor.rb +446 -76
- data/lib/shakapacker/helper.rb +3 -3
- data/lib/shakapacker/install/env.rb +50 -0
- data/lib/shakapacker/instance.rb +1 -1
- data/lib/shakapacker/mtime_strategy.rb +1 -1
- data/lib/shakapacker/runner.rb +93 -55
- data/lib/shakapacker/utils/misc.rb +38 -0
- data/lib/shakapacker/version.rb +1 -1
- data/lib/shakapacker/version_checker.rb +16 -4
- data/lib/tasks/shakapacker/export_bundler_config.rake +30 -21
- data/lib/tasks/shakapacker/install.rake +5 -3
- data/lib/tasks/shakapacker.rake +1 -1
- data/package.json +15 -11
- data/sig/shakapacker/configuration.rbs +3 -0
- metadata +3 -3
data/README.md
CHANGED
|
@@ -6,44 +6,124 @@
|
|
|
6
6
|
|
|
7
7
|
# Shakapacker (v10)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Shakapacker is the official, actively maintained successor to
|
|
10
|
+
[rails/webpacker](https://github.com/rails/webpacker). It gives Rails apps a
|
|
11
|
+
manifest-backed bridge to webpack 5 or Rspack, with view helpers, installer
|
|
12
|
+
tasks, binstubs, and configuration conventions that still allow direct bundler
|
|
13
|
+
customization.
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
_Shakapacker 10 supports [Rspack](https://rspack.rs/) — up to 17x faster than
|
|
16
|
+
webpack per
|
|
17
|
+
[upstream benchmarks](https://shakapacker.com/docs/transpiler-performance/#published-benchmarks)._
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
The canonical markdown source stays in this repository's [`docs/`](./docs/)
|
|
20
|
+
directory and is published to the docs site.
|
|
14
21
|
|
|
15
|
-
|
|
22
|
+
<p align="center">
|
|
23
|
+
<a href="https://shakapacker.com/docs/">
|
|
24
|
+
<img src="https://img.shields.io/badge/%F0%9F%93%96%20Read%20the%20Docs-shakapacker.com-cc0000?style=for-the-badge" alt="Read the Shakapacker documentation at shakapacker.com">
|
|
25
|
+
</a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
[](https://github.com/shakacode/shakapacker/actions/workflows/ruby.yml)
|
|
29
|
+
[](https://github.com/shakacode/shakapacker/actions/workflows/node.yml)
|
|
30
|
+
[](https://github.com/shakacode/shakapacker/actions/workflows/generator.yml)
|
|
31
|
+
[](https://github.com/shakacode/shakapacker/actions/workflows/test-bundlers.yml)
|
|
16
32
|
|
|
17
|
-
|
|
33
|
+
[](https://www.npmjs.com/package/shakapacker)
|
|
34
|
+
[](https://rubygems.org/gems/shakapacker)
|
|
35
|
+
[](https://www.npmjs.com/package/shakapacker)
|
|
36
|
+
[](https://www.npmjs.com/package/shakapacker-webpack)
|
|
37
|
+
[](https://www.npmjs.com/package/shakapacker-rspack)
|
|
38
|
+
|
|
39
|
+
## Documentation
|
|
40
|
+
|
|
41
|
+
Full documentation lives at **[shakapacker.com/docs](https://shakapacker.com/docs/)**. Start here:
|
|
42
|
+
|
|
43
|
+
| Need | Link |
|
|
44
|
+
| ---------------------------------------- | ----------------------------------------------------------------------------------- |
|
|
45
|
+
| Decide if Shakapacker + Rspack is right | [Why Shakapacker with Rspack](https://shakapacker.com/docs/why-shakapacker-rspack/) |
|
|
46
|
+
| Install Shakapacker | [Installation](https://shakapacker.com/docs/installation/) |
|
|
47
|
+
| Configure `config/shakapacker.yml` | [Configuration](https://shakapacker.com/docs/configuration/) |
|
|
48
|
+
| Use the JavaScript/Node API | [Node Package API](https://shakapacker.com/docs/node_package_api/) |
|
|
49
|
+
| Render assets with view helpers | [API Reference](https://shakapacker.com/docs/api-reference/) |
|
|
50
|
+
| Add React, TypeScript, or CSS | [React & integrations](https://shakapacker.com/docs/react/) |
|
|
51
|
+
| Move from webpack to Rspack | [Rspack Migration](https://shakapacker.com/docs/rspack_migration_guide/) |
|
|
52
|
+
| Compare generated webpack/Rspack configs | [Config Diff](https://shakapacker.com/docs/config-diff/) |
|
|
53
|
+
| Deploy compiled assets | [Deployment](https://shakapacker.com/docs/deployment/) |
|
|
54
|
+
| Upgrade an existing app | [Common Upgrades](https://shakapacker.com/docs/common-upgrades/) |
|
|
55
|
+
| Troubleshoot builds | [Troubleshooting](https://shakapacker.com/docs/troubleshooting/) |
|
|
56
|
+
| Review releases | [Changelog](./CHANGELOG.md) |
|
|
57
|
+
|
|
58
|
+
## Why Shakapacker
|
|
59
|
+
|
|
60
|
+
- Rails-native manifest integration and view helpers for bundled assets.
|
|
61
|
+
- Support for webpack 5 and first-class Rspack builds.
|
|
62
|
+
- JavaScript and TypeScript transpilation through SWC, Babel, or esbuild.
|
|
63
|
+
- Development binstubs for watch mode and the webpack dev server.
|
|
64
|
+
- Code splitting, HMR, CDN asset hosts, Subresource Integrity, and HTTP 103
|
|
65
|
+
Early Hints support.
|
|
66
|
+
- Compatibility with npm, Yarn, pnpm, and Bun.
|
|
67
|
+
- Optional `shakapacker-webpack` and `shakapacker-rspack` packages in 10.1+
|
|
68
|
+
that consolidate the managed bundler stack into one dependency.
|
|
69
|
+
|
|
70
|
+
### Optional support
|
|
18
71
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- **See the [v10.0.0 release notes](https://github.com/shakacode/shakapacker/releases/tag/v10.0.0) for upgrading from v9 to v10.**
|
|
22
|
-
- **See [V9 Upgrade](./docs/v9_upgrade.md) for upgrading from v8 to v9.**
|
|
23
|
-
- See [V8 Upgrade](./docs/v8_upgrade.md) for upgrading from the v7 release.
|
|
24
|
-
- See [V7 Upgrade](./docs/v7_upgrade.md) for upgrading from the v6 release.
|
|
25
|
-
- See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
|
|
72
|
+
Optional integrations require extra packages only when you use them: React,
|
|
73
|
+
TypeScript, stylesheets with Sass, Less, Stylus, CSS, PostCSS, and CoffeeScript.
|
|
26
74
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
[
|
|
30
|
-
|
|
75
|
+
For why you might choose this stack over importmaps, jsbundling-rails, or
|
|
76
|
+
vite-rails, see
|
|
77
|
+
[Why Shakapacker with Rspack](https://shakapacker.com/docs/why-shakapacker-rspack/).
|
|
78
|
+
See also a comparison of
|
|
79
|
+
[Shakapacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md).
|
|
80
|
+
For an in-depth discussion of choosing between `shakapacker` and
|
|
81
|
+
`jsbundling-rails`, see the discussion
|
|
82
|
+
[Webpacker alternatives - which path should we go to? #8783](https://github.com/decidim/decidim/discussions/8783)
|
|
83
|
+
and the resulting PR
|
|
84
|
+
[Switch away from Webpacker to Shakapacker #10389](https://github.com/decidim/decidim/pull/10389).
|
|
31
85
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
86
|
+
## Installation
|
|
87
|
+
|
|
88
|
+
For an existing Rails app:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
bundle add shakapacker --strict
|
|
92
|
+
bundle exec rake shakapacker:install
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
For a new Rails 6+ app:
|
|
35
96
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
97
|
+
```bash
|
|
98
|
+
rails new myapp --skip-javascript
|
|
99
|
+
cd myapp
|
|
100
|
+
bundle add shakapacker --strict
|
|
101
|
+
bundle exec rake shakapacker:install
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
See the [installation guide](https://shakapacker.com/docs/installation/) for
|
|
105
|
+
requirements, package-manager selection, non-interactive installer modes, and
|
|
106
|
+
verification steps.
|
|
107
|
+
|
|
108
|
+
## Upgrading
|
|
109
|
+
|
|
110
|
+
Shakapacker ships as both a Ruby gem and an npm package; keep them on matching
|
|
111
|
+
versions. Use the [common upgrade guides](https://shakapacker.com/docs/common-upgrades/)
|
|
112
|
+
for package-manager changes, Babel-to-SWC migration, webpack-to-Rspack migration,
|
|
113
|
+
and release-to-release upgrade paths.
|
|
39
114
|
|
|
40
|
-
|
|
115
|
+
Older major-version docs:
|
|
41
116
|
|
|
42
|
-
|
|
117
|
+
- [v10.0.0 release notes](https://github.com/shakacode/shakapacker/releases/tag/v10.0.0)
|
|
118
|
+
- [v9 upgrade guide](https://shakapacker.com/docs/v9_upgrade/)
|
|
119
|
+
- [v8 upgrade guide](https://shakapacker.com/docs/v8_upgrade/)
|
|
120
|
+
- [v7 upgrade guide](https://shakapacker.com/docs/v7_upgrade/)
|
|
121
|
+
- [v6 upgrade guide](https://shakapacker.com/docs/v6_upgrade/)
|
|
122
|
+
- [v6 stable branch](https://github.com/shakacode/shakapacker/tree/6-stable)
|
|
43
123
|
|
|
44
|
-
|
|
124
|
+
## Example Apps
|
|
45
125
|
|
|
46
|
-
|
|
126
|
+
- [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)
|
|
47
127
|
|
|
48
128
|
## ShakaCode Support
|
|
49
129
|
|
|
@@ -81,1352 +161,20 @@ Here's a testimonial from Jon Rajavuori of [Academia.edu](https://www.academia.e
|
|
|
81
161
|
> - Production incremental builds: **~10 seconds**
|
|
82
162
|
> - HMR rebuild time: unchanged at ~8s (bottleneck is orchestration, not compilation)
|
|
83
163
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
87
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
88
|
-
|
|
89
|
-
- [Prerequisites](#prerequisites)
|
|
90
|
-
- [Features](#features)
|
|
91
|
-
- [Optional support](#optional-support)
|
|
92
|
-
- [Installation](#installation)
|
|
93
|
-
- [Rails v6+](#rails-v6)
|
|
94
|
-
- [Concepts](#concepts)
|
|
95
|
-
- [Usage](#usage)
|
|
96
|
-
- [Configuration and Code](#configuration-and-code)
|
|
97
|
-
- [Configuration Guide](./docs/configuration.md)
|
|
98
|
-
- [View Helpers](#view-helpers)
|
|
99
|
-
- [View Helpers `javascript_pack_tag` and `stylesheet_pack_tag`](#view-helpers-javascript_pack_tag-and-stylesheet_pack_tag)
|
|
100
|
-
- [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)
|
|
101
|
-
- [View Helper: `asset_pack_path`](#view-helper-asset_pack_path)
|
|
102
|
-
- [View Helper: `image_pack_tag`](#view-helper-image_pack_tag)
|
|
103
|
-
- [View Helper: `favicon_pack_tag`](#view-helper-favicon_pack_tag)
|
|
104
|
-
- [View Helper: `preload_pack_asset`](#view-helper-preload_pack_asset)
|
|
105
|
-
- [HTTP 103 Early Hints](#http-103-early-hints)
|
|
106
|
-
- [Images in Stylesheets](#images-in-stylesheets)
|
|
107
|
-
- [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
|
|
108
|
-
- [Development](#development)
|
|
109
|
-
- [Automatic Webpack Code Building](#automatic-webpack-code-building)
|
|
110
|
-
- [Compiler strategies](#compiler-strategies)
|
|
111
|
-
- [Common Development Commands](#common-development-commands)
|
|
112
|
-
- [Ruby API Reference](#ruby-api-reference)
|
|
113
|
-
- [Webpack Configuration](#webpack-configuration)
|
|
114
|
-
- [Babel configuration](#babel-configuration)
|
|
115
|
-
- [SWC configuration](#swc-configuration)
|
|
116
|
-
- [esbuild loader configuration](#esbuild-loader-configuration)
|
|
117
|
-
- [Integrations](#integrations)
|
|
118
|
-
- [React](#react)
|
|
119
|
-
- [Typescript](#typescript)
|
|
120
|
-
- [CSS](#css)
|
|
121
|
-
- [Postcss](#postcss)
|
|
122
|
-
- [Sass](#sass)
|
|
123
|
-
- [Less](#less)
|
|
124
|
-
- [Stylus](#stylus)
|
|
125
|
-
- [CoffeeScript](#coffeescript)
|
|
126
|
-
- [Other frameworks](#other-frameworks)
|
|
127
|
-
- [Custom Rails environments](#custom-rails-environments)
|
|
128
|
-
- [Upgrading](#upgrading)
|
|
129
|
-
- [Paths](#paths)
|
|
130
|
-
- [Additional paths](#additional-paths)
|
|
131
|
-
- [Deployment](#deployment)
|
|
132
|
-
- [Example Apps](#example-apps)
|
|
133
|
-
- [Troubleshooting](#troubleshooting)
|
|
134
|
-
- [Contributing](#contributing)
|
|
135
|
-
- [License](#license)
|
|
136
|
-
- [Supporters](#supporters)
|
|
137
|
-
|
|
138
|
-
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
139
|
-
|
|
140
|
-
## Prerequisites
|
|
141
|
-
|
|
142
|
-
- Ruby 2.7+
|
|
143
|
-
- Rails 5.2+
|
|
144
|
-
- Node.js `^20.19.0` or `>=22.12.0`
|
|
145
|
-
|
|
146
|
-
## Features
|
|
147
|
-
|
|
148
|
-
- Rails view helpers that fully support Webpack output, including HMR and code splitting.
|
|
149
|
-
- Convenient but not required webpack configuration. The only requirement is that your webpack configuration creates a manifest.
|
|
150
|
-
- HMR with the `shakapacker-dev-server`, such as for hot-reloading React!
|
|
151
|
-
- Automatic code splitting using multiple entry points to optimize JavaScript downloads.
|
|
152
|
-
- Support for [NPM](https://www.npmjs.com/package/npm), Yarn ([classic](https://classic.yarnpkg.com/lang/en/) and [berry](https://yarnpkg.com/getting-started)), [PNPM](https://pnpm.io/), and [Bun](https://bun.sh/)
|
|
153
|
-
- [Webpack v5+](https://webpack.js.org/)
|
|
154
|
-
- ES6 with [babel](https://babeljs.io/), [SWC](https://swc.rs/), or [Esbuild](https://github.com/privatenumber/esbuild-loader)
|
|
155
|
-
- Asset compression, source-maps, and minification
|
|
156
|
-
- CDN support
|
|
157
|
-
- Extensible and configurable. For example, all major dependencies are specified as peers, so you can upgrade easily.
|
|
158
|
-
|
|
159
|
-
### Optional support
|
|
160
|
-
|
|
161
|
-
_Requires extra packages to be installed._
|
|
162
|
-
|
|
163
|
-
- React
|
|
164
|
-
- TypeScript
|
|
165
|
-
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
|
|
166
|
-
- CoffeeScript
|
|
167
|
-
|
|
168
|
-
## Installation
|
|
169
|
-
|
|
170
|
-
See the [Installation guide](./docs/installation.md) for a step-by-step walkthrough.
|
|
171
|
-
|
|
172
|
-
### Rails v6+
|
|
173
|
-
|
|
174
|
-
With Rails v6+, skip JavaScript for a new app and follow below Manual Installation Steps to manually add the `shakapacker` gem to your Gemfile.
|
|
175
|
-
|
|
176
|
-
```bash
|
|
177
|
-
rails new myapp --skip-javascript
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
_Note, Rails 6 installs the older v5 version of webpacker unless you specify `--skip-javascript`._
|
|
181
|
-
|
|
182
|
-
Add `shakapacker` gem to your `Gemfile`:
|
|
183
|
-
|
|
184
|
-
```bash
|
|
185
|
-
bundle add shakapacker --strict
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Then run the following to install Shakapacker:
|
|
189
|
-
|
|
190
|
-
```bash
|
|
191
|
-
./bin/bundle install
|
|
192
|
-
bundle exec rake shakapacker:install
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
Before initiating the installation process, ensure you have committed all changes. During install, Shakapacker may encounter conflicts with existing files. You can approve prompts interactively, use `FORCE=true` to overwrite without prompting, or use `SKIP=true` to preserve existing files and only create missing ones. Accepted truthy values are `true`, `1`, and `yes` (case-insensitive). If both are set, `FORCE` takes precedence.
|
|
196
|
-
|
|
197
|
-
Shakapacker uses the [`package_json`](https://github.com/shakacode/package_json) gem to handle updating the `package.json` and interacting with the underlying package manager of choice for managing dependencies and running commands; the package manager is managed using the [`packageManager`](https://nodejs.org/api/packages.html#packagemanager) property in the `package.json`, otherwise falling back to the value of `PACKAGE_JSON_FALLBACK_MANAGER` if set or otherwise `npm`.
|
|
198
|
-
|
|
199
|
-
If `packageManager` is not set when running `shakapacker:install`, Shakapacker will set it based on the lockfile and the result of calling `--version` on the inferred manager; if no lockfile is present, then `npm` be used unless you choose to explicitly set the `PACKAGE_JSON_FALLBACK_MANAGER` to your preferred package manager.
|
|
200
|
-
|
|
201
|
-
> [!NOTE]
|
|
202
|
-
>
|
|
203
|
-
> The `packageManager` property is only used to determine the package manager to use, based primarily on its name.
|
|
204
|
-
> The version (if present) is only used to determine if Yarn Classic or Yarn Berry should be used, but is otherwise
|
|
205
|
-
> _not_ checked, nor is [`corepack`](https://nodejs.org/api/corepack.html) used to ensure that the package manager is installed.
|
|
206
|
-
>
|
|
207
|
-
> It is up to the developer to ensure that the desired package manager is actually install at the right version, which can be done
|
|
208
|
-
> using `corepack` or by other means.
|
|
209
|
-
|
|
210
|
-
See [here](https://github.com/G-Rath/package_json#specifying-a-package-manager) for a list of the supported package managers and more information; note that `package_json` does not handle ensuring the manager is installed.
|
|
211
|
-
|
|
212
|
-
If you wish to use [Yarn PnP](https://yarnpkg.com/features/pnp) you will need to configure Babel using a `babel.config.js` file rather than via `package.json` - see [customizing Babel Config](./docs/customizing_babel_config.md) for examples on how to do this.
|
|
213
|
-
|
|
214
|
-
> [!NOTE]
|
|
215
|
-
>
|
|
216
|
-
> The rest of the documentation will only reference `npm` when providing commands such as to install optional packages except in cases where
|
|
217
|
-
> a particular package manager requires a very different command; otherwise it should be safe to just replace `npm` with the name of your
|
|
218
|
-
> preferred package manager when running the command
|
|
219
|
-
|
|
220
|
-
Note, in v6+, most JS packages are peer dependencies. During `shakapacker:install`, Shakapacker
|
|
221
|
-
adds the subset required for your chosen bundler/transpiler setup rather than forcing every
|
|
222
|
-
supported package into every app.
|
|
223
|
-
|
|
224
|
-
See:
|
|
225
|
-
|
|
226
|
-
- [Optional Peer Dependencies](./docs/optional-peer-dependencies.md) for how optional peers work
|
|
227
|
-
- [Shakapacker's Peer Dependencies](./docs/peer-dependencies.md) for the current supported version ranges
|
|
228
|
-
|
|
229
|
-
This keeps installation flexible while still making the supported dependency ranges explicit.
|
|
230
|
-
|
|
231
|
-
### Optional Peer Dependencies
|
|
232
|
-
|
|
233
|
-
All peer dependencies in Shakapacker are marked as optional via `peerDependenciesMeta`. This design decision ensures:
|
|
234
|
-
|
|
235
|
-
- **No warnings during package installation** when dependencies are not needed
|
|
236
|
-
- **Clear visibility of supported package versions** for upgrades
|
|
237
|
-
- **Flexibility to choose only the tools you need** (webpack vs rspack, babel vs swc vs esbuild)
|
|
238
|
-
|
|
239
|
-
The optional peer dependencies approach means you only install what you actually use, while still maintaining
|
|
240
|
-
version compatibility constraints when you do install those packages.
|
|
241
|
-
|
|
242
|
-
#### Required Dependencies by Configuration
|
|
243
|
-
|
|
244
|
-
Depending on your setup, you'll need different subsets of the optional peer dependencies:
|
|
245
|
-
|
|
246
|
-
**For Webpack + Babel (traditional setup):**
|
|
247
|
-
|
|
248
|
-
```json
|
|
249
|
-
{
|
|
250
|
-
"dependencies": {
|
|
251
|
-
"shakapacker": "^10.0.0",
|
|
252
|
-
"@babel/core": "^7.17.9",
|
|
253
|
-
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
254
|
-
"@babel/preset-env": "^7.16.11",
|
|
255
|
-
"@babel/runtime": "^7.17.9",
|
|
256
|
-
"babel-loader": "^8.2.4",
|
|
257
|
-
"compression-webpack-plugin": "^9.0.0",
|
|
258
|
-
"terser-webpack-plugin": "^5.3.1",
|
|
259
|
-
"webpack": "^5.101.0",
|
|
260
|
-
"webpack-assets-manifest": "^5.0.6",
|
|
261
|
-
"webpack-cli": "^6.0.0",
|
|
262
|
-
"webpack-dev-server": "^5.2.2"
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
> **Note:** `webpack-cli` v7 is also supported but requires Node.js >= 20.9.0. If you're on Node >= 20.9.0, you can use `"webpack-cli": "^7.0.0"` instead. See [peer-dependencies.md](./docs/peer-dependencies.md) for the full supported range.
|
|
268
|
-
|
|
269
|
-
**For Webpack + SWC (faster alternative):**
|
|
270
|
-
|
|
271
|
-
```json
|
|
272
|
-
{
|
|
273
|
-
"dependencies": {
|
|
274
|
-
"shakapacker": "^10.0.0",
|
|
275
|
-
"@swc/core": "^1.3.0",
|
|
276
|
-
"swc-loader": "^0.2.0",
|
|
277
|
-
"compression-webpack-plugin": "^9.0.0",
|
|
278
|
-
"terser-webpack-plugin": "^5.3.1",
|
|
279
|
-
"webpack": "^5.101.0",
|
|
280
|
-
"webpack-assets-manifest": "^5.0.6",
|
|
281
|
-
"webpack-cli": "^6.0.0",
|
|
282
|
-
"webpack-dev-server": "^5.2.2"
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
**For Rspack + SWC (largest end-to-end speedup — see [transpiler-performance guide](./docs/transpiler-performance.md#published-benchmarks)):**
|
|
288
|
-
|
|
289
|
-
```json
|
|
290
|
-
{
|
|
291
|
-
"dependencies": {
|
|
292
|
-
"shakapacker": "^10.0.0",
|
|
293
|
-
"@rspack/core": "^2.0.0-0",
|
|
294
|
-
"@rspack/cli": "^2.0.0-0",
|
|
295
|
-
"@swc/core": "^1.3.0",
|
|
296
|
-
"swc-loader": "^0.2.0",
|
|
297
|
-
"rspack-manifest-plugin": "^5.0.0"
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
**Quick tip:** You can easily switch between webpack and rspack using:
|
|
303
|
-
|
|
304
|
-
```bash
|
|
305
|
-
bundle exec rake shakapacker:switch_bundler rspack -- --install-deps
|
|
306
|
-
|
|
307
|
-
# For faster switching, use --no-uninstall to keep both bundlers installed
|
|
308
|
-
bundle exec rake shakapacker:switch_bundler webpack -- --install-deps --no-uninstall
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
See the [Rspack Migration Guide](./docs/rspack_migration_guide.md) for details.
|
|
312
|
-
|
|
313
|
-
**For CSS/Sass processing (add to any config above):**
|
|
314
|
-
|
|
315
|
-
```json
|
|
316
|
-
{
|
|
317
|
-
"dependencies": {
|
|
318
|
-
"css-loader": "^6.8.1",
|
|
319
|
-
"mini-css-extract-plugin": "^2.0.0",
|
|
320
|
-
"sass": "^1.50.0",
|
|
321
|
-
"sass-loader": "^13.0.0"
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
## Concepts
|
|
327
|
-
|
|
328
|
-
At its core, Shakapacker's essential function is to:
|
|
329
|
-
|
|
330
|
-
1. Provide configuration by a single file used by both Rails view helpers and JavaScript webpack compilation code.
|
|
331
|
-
2. Provide Rails view helpers, utilizing this configuration file so that a webpage can load JavaScript, CSS, and other static assets compiled by webpack, supporting bundle splitting, fingerprinting, and HMR.
|
|
332
|
-
3. Provide a community-supported, default webpack compilation that generates the necessary bundles and manifest, using the same configuration file. This compilation can be extended for any needs.
|
|
333
|
-
|
|
334
|
-
## Usage
|
|
335
|
-
|
|
336
|
-
### Configuration and Code
|
|
337
|
-
|
|
338
|
-
**📖 For a comprehensive guide to all configuration options, see the [Configuration Guide](./docs/configuration.md)**
|
|
339
|
-
|
|
340
|
-
**📦 For Node package exports and config object docs, see the [Node Package API Guide](./docs/node_package_api.md)**
|
|
341
|
-
|
|
342
|
-
This includes documentation for:
|
|
343
|
-
|
|
344
|
-
- All `config/shakapacker.yml` options (including `assets_bundler_config_path`)
|
|
345
|
-
- Environment-specific configuration
|
|
346
|
-
- Development server settings
|
|
347
|
-
- Build configurations (`config/shakapacker-builds.yml`)
|
|
348
|
-
- Best practices and common patterns
|
|
349
|
-
|
|
350
|
-
You will need your file system to correspond to the setup of your `config/shakapacker.yml` file.
|
|
351
|
-
|
|
352
|
-
Suppose you have the following configuration:
|
|
353
|
-
|
|
354
|
-
`shakapacker.yml`
|
|
355
|
-
|
|
356
|
-
```yml
|
|
357
|
-
default: &default
|
|
358
|
-
source_path: app/javascript
|
|
359
|
-
source_entry_path: packs
|
|
360
|
-
public_root_path: public
|
|
361
|
-
public_output_path: packs
|
|
362
|
-
nested_entries: false
|
|
363
|
-
# And more
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
And that maps to a directory structure like this:
|
|
367
|
-
|
|
368
|
-
```
|
|
369
|
-
app/javascript:
|
|
370
|
-
└── packs: # sets up webpack entries
|
|
371
|
-
│ └── application.js # references ../src/my_component.js
|
|
372
|
-
│ └── application.css
|
|
373
|
-
└── src: # any directory name is fine. Referenced files need to be under source_path
|
|
374
|
-
│ └── my_component.js
|
|
375
|
-
└── stylesheets:
|
|
376
|
-
│ └── my_styles.css
|
|
377
|
-
└── images:
|
|
378
|
-
└── logo.svg
|
|
379
|
-
public/packs # webpack output
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
Webpack intelligently includes only necessary files. In this example, the file `packs/application.js` would reference `../src/my_component.js`
|
|
383
|
-
|
|
384
|
-
The `nested_entries` option allows webpack entry points in subdirectories (defaults to `true`). See the [Configuration Guide](./docs/configuration.md#nested_entries) for details.
|
|
385
|
-
|
|
386
|
-
The `useContentHash` option enables content-based cache busting. It's disabled by default (except in production) to speed up development builds. See the [Configuration Guide](./docs/configuration.md#usecontenthash) for details.
|
|
387
|
-
|
|
388
|
-
#### Precompile Hook
|
|
389
|
-
|
|
390
|
-
Shakapacker supports running custom commands before compilation via the `precompile_hook` configuration option.
|
|
391
|
-
|
|
392
|
-
For configuration details, see [precompile_hook in the Configuration Guide](./docs/configuration.md#precompile_hook).
|
|
393
|
-
For complete usage guide, see the [Precompile Hook Guide](./docs/precompile_hook.md).
|
|
394
|
-
|
|
395
|
-
#### Setting custom config path
|
|
396
|
-
|
|
397
|
-
You can use the `SHAKAPACKER_CONFIG` environment variable to specify a custom config file path. See [Environment Variables in the Configuration Guide](./docs/configuration.md#environment-variables) for this and other configuration options.
|
|
398
|
-
|
|
399
|
-
### View Helpers
|
|
400
|
-
|
|
401
|
-
The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views.
|
|
402
|
-
|
|
403
|
-
Be sure to consult the API documentation in the source code of [helper.rb](./lib/shakapacker/helper.rb).
|
|
404
|
-
|
|
405
|
-
**Note:** For your styles or static assets files to be available in your view, you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know to package up those files.
|
|
406
|
-
|
|
407
|
-
#### View Helpers `javascript_pack_tag` and `stylesheet_pack_tag`
|
|
408
|
-
|
|
409
|
-
These view helpers take your `shakapacker.yml` configuration file and the resulting webpack compilation `manifest.json` and generate the HTML to load the assets.
|
|
410
|
-
|
|
411
|
-
You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
|
|
412
|
-
|
|
413
|
-
```erb
|
|
414
|
-
<%= javascript_pack_tag 'application' %>
|
|
415
|
-
<%= stylesheet_pack_tag 'application' %>
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
|
|
419
|
-
packs with the chunks in your view, which creates HTML tags for all the chunks.
|
|
420
|
-
|
|
421
|
-
You can provide multiple packs and other attributes. Note, `defer` defaults to showing.
|
|
422
|
-
|
|
423
|
-
```erb
|
|
424
|
-
<%= javascript_pack_tag 'calendar', 'map', 'data-turbo-track': 'reload' %>
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
The resulting HTML would look like this:
|
|
428
|
-
|
|
429
|
-
```html
|
|
430
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
431
|
-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
432
|
-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbo-track="reload" defer"></script>
|
|
433
|
-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
434
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-turbo-track="reload" defer></script>
|
|
435
|
-
```
|
|
436
|
-
|
|
437
|
-
In this output, both the calendar and map codes might refer to other common libraries. Those get placed in something like the vendor bundle. The view helper removes any duplication.
|
|
438
|
-
|
|
439
|
-
Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
|
|
440
|
-
|
|
441
|
-
The `javascript_pack_tag` also supports the `async` attribute, which you can enable by passing `async: true`:
|
|
442
|
-
|
|
443
|
-
```erb
|
|
444
|
-
<%= javascript_pack_tag 'application', async: true %>
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
This will generate script tags with the `async` attribute, which allows the browser to download and execute the script asynchronously without blocking HTML parsing:
|
|
448
|
-
|
|
449
|
-
```html
|
|
450
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" async></script>
|
|
451
|
-
<script src="/packs/application~runtime-16838bab065ae1e314.js" async></script>
|
|
452
|
-
<script src="/packs/application-1016838bab065ae1e314.js" async></script>
|
|
453
|
-
```
|
|
454
|
-
|
|
455
|
-
Note that when using `async: true`, scripts may execute in any order as soon as they're downloaded, which could cause issues if your code has dependencies between files. In most cases, `defer` (the default) is preferred as it maintains execution order.
|
|
456
|
-
|
|
457
|
-
> [!NOTE]
|
|
458
|
-
>
|
|
459
|
-
> 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`.
|
|
460
|
-
|
|
461
|
-
**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.
|
|
462
|
-
|
|
463
|
-
```erb
|
|
464
|
-
<%# DO %>
|
|
465
|
-
<%= javascript_pack_tag 'calendar', 'map' %>
|
|
466
|
-
|
|
467
|
-
<%# DON'T %>
|
|
468
|
-
<%= javascript_pack_tag 'calendar' %>
|
|
469
|
-
<%= javascript_pack_tag 'map' %>
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
While this also generally applies to `stylesheet_pack_tag`,
|
|
473
|
-
you may use multiple calls to stylesheet_pack_tag if,
|
|
474
|
-
say,
|
|
475
|
-
you require multiple `<style>` tags for different output media:
|
|
476
|
-
|
|
477
|
-
```erb
|
|
478
|
-
<%= stylesheet_pack_tag 'application', media: 'screen' %>
|
|
479
|
-
<%= stylesheet_pack_tag 'print', media: 'print' %>
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
#### View Helper `append_javascript_pack_tag`, `prepend_javascript_pack_tag` and `append_stylesheet_pack_tag`
|
|
483
|
-
|
|
484
|
-
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.
|
|
485
|
-
|
|
486
|
-
Main view:
|
|
487
|
-
|
|
488
|
-
```erb
|
|
489
|
-
<% append_javascript_pack_tag 'calendar' %>
|
|
490
|
-
<% append_stylesheet_pack_tag 'calendar' %>
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
Some partial:
|
|
494
|
-
|
|
495
|
-
```erb
|
|
496
|
-
<% append_javascript_pack_tag 'map' %>
|
|
497
|
-
<% append_stylesheet_pack_tag 'map' %>
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
And the main layout has:
|
|
501
|
-
|
|
502
|
-
```erb
|
|
503
|
-
<%= javascript_pack_tag 'application' %>
|
|
504
|
-
<%= stylesheet_pack_tag 'application' %>
|
|
505
|
-
```
|
|
506
|
-
|
|
507
|
-
is the same as using this in the main layout:
|
|
508
|
-
|
|
509
|
-
```erb
|
|
510
|
-
<%= javascript_pack_tag 'calendar', 'map', 'application' %>
|
|
511
|
-
<%= stylesheet_pack_tag 'calendar', 'map', 'application' %>
|
|
512
|
-
```
|
|
513
|
-
|
|
514
|
-
However, you typically can't do that in the main layout, as the view and partial codes will depend on the route.
|
|
515
|
-
|
|
516
|
-
Thus, you can distribute the logic of what packs are needed for any route. All the magic of splitting up the code and CSS was automatic!
|
|
517
|
-
|
|
518
|
-
**Important:** These helpers can be used anywhere in your application as long as they are executed BEFORE `(javascript/stylesheet)_pack_tag` respectively. If you attempt to call one of these helpers after the respective `(javascript/stylesheet)_pack_tag`, an error will be raised.
|
|
519
|
-
|
|
520
|
-
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`.
|
|
521
|
-
|
|
522
|
-
```erb
|
|
523
|
-
<% content_for :footer do %>
|
|
524
|
-
<%= render 'shared/footer' %>
|
|
525
|
-
<% end %>
|
|
526
|
-
|
|
527
|
-
<%= javascript_pack_tag %>
|
|
528
|
-
|
|
529
|
-
<%= yield :footer %>
|
|
530
|
-
```
|
|
531
|
-
|
|
532
|
-
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.
|
|
533
|
-
|
|
534
|
-
Main view:
|
|
535
|
-
|
|
536
|
-
```erb
|
|
537
|
-
<% append_javascript_pack_tag 'map' %>
|
|
538
|
-
```
|
|
539
|
-
|
|
540
|
-
Some partial:
|
|
541
|
-
|
|
542
|
-
```erb
|
|
543
|
-
<% append_javascript_pack_tag 'map' %>
|
|
544
|
-
```
|
|
545
|
-
|
|
546
|
-
And the main layout has:
|
|
547
|
-
|
|
548
|
-
```erb
|
|
549
|
-
<% prepend_javascript_pack_tag 'main' %>
|
|
550
|
-
<%= javascript_pack_tag 'application' %>
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
is the same as using this in the main layout:
|
|
554
|
-
|
|
555
|
-
```erb
|
|
556
|
-
<%= javascript_pack_tag 'main', 'calendar', 'map', 'application' %>
|
|
557
|
-
```
|
|
558
|
-
|
|
559
|
-
For alternative options for setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
|
|
560
|
-
|
|
561
|
-
**Important:** To prevent FOUC (Flash of Unstyled Content), always place `stylesheet_pack_tag` in the `<head>` section of your layout. When using `append_*` helpers with dynamic pack loading (e.g., React on Rails), use the `content_for` pattern to control execution order. See the [Preventing FOUC guide](./docs/preventing_fouc.md) for detailed examples.
|
|
562
|
-
|
|
563
|
-
#### View Helper: `asset_pack_path`
|
|
564
|
-
|
|
565
|
-
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
|
566
|
-
|
|
567
|
-
```erb
|
|
568
|
-
<img src="<%= asset_pack_path 'static/logo.svg' %>" />
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
#### View Helper: `image_pack_tag`
|
|
572
|
-
|
|
573
|
-
Or use the dedicated helper:
|
|
574
|
-
|
|
575
|
-
```erb
|
|
576
|
-
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
|
577
|
-
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
#### View Helper: `favicon_pack_tag`
|
|
581
|
-
|
|
582
|
-
If you want to create a favicon:
|
|
583
|
-
|
|
584
|
-
```erb
|
|
585
|
-
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
|
586
|
-
```
|
|
587
|
-
|
|
588
|
-
#### View Helper: `preload_pack_asset`
|
|
589
|
-
|
|
590
|
-
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
|
591
|
-
|
|
592
|
-
```erb
|
|
593
|
-
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
|
594
|
-
```
|
|
595
|
-
|
|
596
|
-
#### HTTP 103 Early Hints
|
|
597
|
-
|
|
598
|
-
Automatically send early hints to browsers for faster asset loading. Supports `preload`/`prefetch`/`none` configuration per-page.
|
|
599
|
-
|
|
600
|
-
```yaml
|
|
601
|
-
# config/shakapacker.yml
|
|
602
|
-
production:
|
|
603
|
-
early_hints:
|
|
604
|
-
enabled: true
|
|
605
|
-
debug: false # Enable to see what hints are sent (as HTML comments)
|
|
606
|
-
```
|
|
607
|
-
|
|
608
|
-
⚠️ **Important**: May improve or hurt performance depending on content. See the [Early Hints Guide](./docs/early_hints.md) for configuration, performance guidance, and examples.
|
|
609
|
-
|
|
610
|
-
**Troubleshooting**: Enable `debug: true` to see HTML comments showing what hints were sent or why they were skipped.
|
|
611
|
-
|
|
612
|
-
**Requirements:** Rails 5.2+, HTTP/2 server, modern browsers. Gracefully degrades if not supported.
|
|
613
|
-
|
|
614
|
-
### Images in Stylesheets
|
|
615
|
-
|
|
616
|
-
If you want to use images in your stylesheets:
|
|
617
|
-
|
|
618
|
-
```css
|
|
619
|
-
.foo {
|
|
620
|
-
background-image: url("../images/logo.svg");
|
|
621
|
-
}
|
|
622
|
-
```
|
|
623
|
-
|
|
624
|
-
### Server-Side Rendering (SSR)
|
|
625
|
-
|
|
626
|
-
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to Shakapacker, like [React on Rails](https://github.com/shakacode/react_on_rails), your JavaScript should create the link prefetch HTML tags that you will use, so you won't need to use to `asset_pack_path` in those circumstances.
|
|
627
|
-
|
|
628
|
-
### Development
|
|
629
|
-
|
|
630
|
-
Shakapacker ships with three binstubs: `./bin/shakapacker`, `./bin/shakapacker-dev-server`, and `./bin/shakapacker-watch`. The first two are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js` executables to ensure that the right configuration files and environmental variables are loaded based on your environment. `./bin/shakapacker-watch` is a shell wrapper around `./bin/shakapacker` that traps INT/TERM signals for clean shutdown — use it in Procfile-based workflows (e.g., `foreman`, `bin/dev`) to avoid Ruby interrupt backtraces when pressing Ctrl-C.
|
|
631
|
-
|
|
632
|
-
_Note: older Shakapacker installations had set a missing NODE_ENV in the binstubs. Please remove this for versions 6.5.2 and newer._
|
|
633
|
-
|
|
634
|
-
#### Automatic Webpack Code Building
|
|
635
|
-
|
|
636
|
-
Shakapacker can be configured to automatically compile on demand when needed using `compile` option in the `shakapacker.yml`. This happens when you refer to any of the pack assets using the Shakapacker helper methods. This means that you don't have to run any separate processes. Compilation errors are logged to the standard Rails log. However, this auto-compilation happens when a web request is made that requires an updated webpack build, not when files change. Thus, that can be **painfully slow** for front-end development in this default way. Instead, you should either run the `bin/shakapacker --watch` or run `./bin/shakapacker-dev-server` during development.
|
|
637
|
-
|
|
638
|
-
The `compile: true` option can be more useful for test and production builds.
|
|
639
|
-
|
|
640
|
-
#### Compiler strategies
|
|
641
|
-
|
|
642
|
-
Shakapacker ships with two different strategies that are used to determine whether assets need recompilation per the `compile: true` option:
|
|
643
|
-
|
|
644
|
-
- `digest` - This strategy calculates SHA1 digest of files in your watched paths (see below). The calculated digest is then stored in a temp file. To check whether the assets need to be recompiled, Shakapacker calculates the SHA1 of the watched files and compares it with the one stored. If the digests are equal, no recompilation occurs. If the digests are different or the temp file is missing, files are recompiled.
|
|
645
|
-
- `mtime` - This strategy looks at the last "modified at" timestamps of both files AND directories in your watched paths. The timestamp of the most recent file or directory is then compared with the timestamp of `manifest.json` file generated. If the manifest file timestamp is newer than one of the most recently modified files or directories in the watched paths, no recompilation occurs. If the manifest file is older, files are recompiled.
|
|
646
|
-
|
|
647
|
-
The `compiler_strategy` option determines how Shakapacker checks if assets need recompilation (`mtime` for development, `digest` for production). See the [Configuration Guide](./docs/configuration.md#compiler_strategy) for detailed comparison and recommendations.
|
|
648
|
-
|
|
649
|
-
> [!NOTE]
|
|
650
|
-
>
|
|
651
|
-
> If you are not using the `shakapacker-dev-server`, your packs will be served by the Rails public file server.
|
|
652
|
-
> If you've enabled caching (Rails application `config.action_controller.perform_caching` setting),
|
|
653
|
-
> your changes will likely not be picked up due to `Cache-Control` header being set and assets being cached in the browser memory.
|
|
654
|
-
>
|
|
655
|
-
> For more details see [issue 88: Caching issues in Development since migrating to Shakapacker](https://github.com/shakacode/shakapacker/issues/88).
|
|
656
|
-
|
|
657
|
-
If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
|
658
|
-
|
|
659
|
-
#### Common Development Commands
|
|
660
|
-
|
|
661
|
-
```bash
|
|
662
|
-
# webpack dev server
|
|
663
|
-
./bin/shakapacker-dev-server
|
|
664
|
-
|
|
665
|
-
# watcher (use in Procfiles for clean Ctrl-C shutdown)
|
|
666
|
-
./bin/shakapacker-watch --watch --progress
|
|
667
|
-
|
|
668
|
-
# watcher (standalone, without signal handling)
|
|
669
|
-
./bin/shakapacker --watch --progress
|
|
670
|
-
|
|
671
|
-
# standalone build
|
|
672
|
-
./bin/shakapacker --progress
|
|
673
|
-
```
|
|
674
|
-
|
|
675
|
-
Once you start this webpack development server, Shakapacker will automatically start proxying all webpack asset requests to this server. When you stop this server, Rails will detect that it's not running and Rails will revert back to on-demand compilation _if_ you have the `compile` option set to true in your `config/shakapacker.yml`
|
|
676
|
-
|
|
677
|
-
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `SHAKAPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the _same_ environmental variables must be available to the `rails server` process.
|
|
678
|
-
|
|
679
|
-
```bash
|
|
680
|
-
SHAKAPACKER_DEV_SERVER_PORT=4305 SHAKAPACKER_DEV_SERVER_HOST=example.com SHAKAPACKER_DEV_SERVER_INLINE=true SHAKAPACKER_DEV_SERVER_HOT=false ./bin/shakapacker-dev-server
|
|
681
|
-
```
|
|
682
|
-
|
|
683
|
-
By default, the webpack dev server listens on `localhost:3035` in development for security purposes. However, if you want your app to be available on port 4035 over local LAN IP or a VM instance like vagrant, you can set the `port` and `host` when running `./bin/shakapacker-dev-server` binstub:
|
|
684
|
-
|
|
685
|
-
```bash
|
|
686
|
-
SHAKAPACKER_DEV_SERVER_PORT=4305 SHAKAPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/shakapacker-dev-server
|
|
687
|
-
```
|
|
688
|
-
|
|
689
|
-
**Note:** You need to allow webpack-dev-server host as an allowed origin for `connect-src` if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this:
|
|
690
|
-
|
|
691
|
-
```ruby
|
|
692
|
-
Rails.application.config.content_security_policy do |policy|
|
|
693
|
-
policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
|
|
694
|
-
end
|
|
695
|
-
```
|
|
696
|
-
|
|
697
|
-
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
|
698
|
-
|
|
699
|
-
### Ruby API Reference
|
|
700
|
-
|
|
701
|
-
**📚 For comprehensive Ruby API documentation, see the [API Reference Guide](./docs/api-reference.md).**
|
|
702
|
-
|
|
703
|
-
This guide covers:
|
|
704
|
-
|
|
705
|
-
- **Main Shakapacker Module** - Configuration, compilation, and manifest access
|
|
706
|
-
- **Configuration API** - Accessing `config/shakapacker.yml` settings programmatically
|
|
707
|
-
- **View Helpers** - Complete reference for all Rails helpers
|
|
708
|
-
- **Manifest API** - Asset lookup and resolution methods
|
|
709
|
-
- **Dev Server API** - Development server status and management
|
|
710
|
-
- **Advanced Usage** - Multiple instances, testing, custom configurations
|
|
711
|
-
|
|
712
|
-
#### Quick Examples
|
|
713
|
-
|
|
714
|
-
```ruby
|
|
715
|
-
# Access configuration
|
|
716
|
-
Shakapacker.config.source_path
|
|
717
|
-
# => #<Pathname:/app/app/javascript>
|
|
718
|
-
|
|
719
|
-
# Get raw configuration hash
|
|
720
|
-
Shakapacker.config.data
|
|
721
|
-
# => { "source_path" => "app/javascript", ... }
|
|
722
|
-
|
|
723
|
-
# Look up compiled assets
|
|
724
|
-
Shakapacker.manifest.lookup("application.js")
|
|
725
|
-
# => "/packs/application-abc123.js"
|
|
726
|
-
|
|
727
|
-
# Check dev server status
|
|
728
|
-
Shakapacker.dev_server.running?
|
|
729
|
-
# => true
|
|
730
|
-
```
|
|
731
|
-
|
|
732
|
-
#### Generating Full API Documentation
|
|
733
|
-
|
|
734
|
-
For complete API documentation with all methods and parameters:
|
|
735
|
-
|
|
736
|
-
```bash
|
|
737
|
-
# Using YARD (recommended - better formatting)
|
|
738
|
-
gem install yard
|
|
739
|
-
yard doc
|
|
740
|
-
yard server # Browse at http://localhost:8808
|
|
741
|
-
|
|
742
|
-
# Using RDoc (standard Ruby documentation)
|
|
743
|
-
rdoc lib/
|
|
744
|
-
open doc/index.html
|
|
745
|
-
```
|
|
746
|
-
|
|
747
|
-
The generated documentation includes all public and private methods with detailed descriptions.
|
|
748
|
-
|
|
749
|
-
#### Type Signatures with RBS
|
|
750
|
-
|
|
751
|
-
Shakapacker includes **RBS type signatures** for all public APIs, enabling static type checking and improved IDE support:
|
|
752
|
-
|
|
753
|
-
**Benefits:**
|
|
754
|
-
|
|
755
|
-
- **IDE Autocomplete**: Get accurate method signatures and parameter hints in your editor
|
|
756
|
-
- **Static Type Checking**: Catch type errors before runtime using [Steep](https://github.com/soutaro/steep) or [TypeProf](https://github.com/ruby/typeprof)
|
|
757
|
-
- **Self-Documenting Code**: Types provide machine-readable API documentation
|
|
758
|
-
- **Safer Refactoring**: Type checker catches breaking changes across your codebase
|
|
759
|
-
|
|
760
|
-
**RBS Signatures Location:**
|
|
761
|
-
Type signatures are in the `sig/` directory and included with the gem:
|
|
762
|
-
|
|
763
|
-
```
|
|
764
|
-
sig/
|
|
765
|
-
├── shakapacker.rbs # Main module
|
|
766
|
-
└── shakapacker/
|
|
767
|
-
├── configuration.rbs # Configuration API
|
|
768
|
-
├── helper.rbs # View helpers
|
|
769
|
-
├── manifest.rbs # Asset lookup
|
|
770
|
-
├── compiler.rbs # Compilation
|
|
771
|
-
├── dev_server.rbs # Dev server
|
|
772
|
-
└── ... # Other components
|
|
773
|
-
```
|
|
774
|
-
|
|
775
|
-
**Using with Steep (Type Checker):**
|
|
776
|
-
|
|
777
|
-
```yaml
|
|
778
|
-
# Steepfile
|
|
779
|
-
target :app do
|
|
780
|
-
signature "sig"
|
|
781
|
-
check "app"
|
|
782
|
-
library "shakapacker"
|
|
783
|
-
end
|
|
784
|
-
```
|
|
785
|
-
|
|
786
|
-
**Example Type Checking:**
|
|
787
|
-
|
|
788
|
-
```ruby
|
|
789
|
-
# Your code
|
|
790
|
-
config = Shakapacker.config
|
|
791
|
-
config.source_path # Type checker knows this returns Pathname
|
|
792
|
-
config.webpack? # Type checker knows this returns bool
|
|
793
|
-
|
|
794
|
-
# Type error caught at development time:
|
|
795
|
-
config.invalid_method # ⚠️ Steep reports: Method `invalid_method` is not defined
|
|
796
|
-
```
|
|
797
|
-
|
|
798
|
-
**Learn More:**
|
|
799
|
-
|
|
800
|
-
- [RBS Documentation](https://github.com/ruby/rbs)
|
|
801
|
-
- [Steep Type Checker](https://github.com/soutaro/steep)
|
|
802
|
-
- [TypeProf](https://github.com/ruby/typeprof)
|
|
803
|
-
|
|
804
|
-
### Webpack Configuration
|
|
805
|
-
|
|
806
|
-
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:
|
|
807
|
-
|
|
808
|
-
1. Your output files go to the right directory
|
|
809
|
-
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!
|
|
810
|
-
|
|
811
|
-
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`.
|
|
812
|
-
|
|
813
|
-
#### Using a Completely Custom Webpack Configuration
|
|
814
|
-
|
|
815
|
-
If you're providing a completely custom webpack configuration without using `generateWebpackConfig()`, you should set `javascript_transpiler: 'none'` in your `config/shakapacker.yml` to skip Shakapacker's transpiler validation and dependency checks:
|
|
816
|
-
|
|
817
|
-
```yml
|
|
818
|
-
# config/shakapacker.yml
|
|
819
|
-
default: &default
|
|
820
|
-
javascript_transpiler: "none" # Skip Shakapacker's transpiler setup
|
|
821
|
-
# ... other config
|
|
822
|
-
```
|
|
823
|
-
|
|
824
|
-
This is useful when you're managing your own transpiler configuration entirely outside of Shakapacker's defaults.
|
|
825
|
-
|
|
826
|
-
**Note:** Only use `javascript_transpiler: 'none'` if you're providing a completely custom webpack configuration without using `generateWebpackConfig()`. If you're using Shakapacker's webpack generation (which is the common case), use one of the supported transpilers (`'babel'`, `'swc'`, or `'esbuild'`) instead.
|
|
827
|
-
|
|
828
|
-
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`:
|
|
829
|
-
|
|
830
|
-
```js
|
|
831
|
-
// config/webpack/webpack.config.js
|
|
832
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
833
|
-
|
|
834
|
-
const options = {
|
|
835
|
-
resolve: {
|
|
836
|
-
extensions: [".css", ".ts", ".tsx"]
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
// This results in a new object copied from the mutable global
|
|
841
|
-
module.exports = generateWebpackConfig(options)
|
|
842
|
-
```
|
|
843
|
-
|
|
844
|
-
The `shakapacker` package also exports the `merge` function from [webpack-merge](https://github.com/survivejs/webpack-merge) to make it easier to do more advanced customizations:
|
|
845
|
-
|
|
846
|
-
```js
|
|
847
|
-
// config/webpack/webpack.config.js
|
|
848
|
-
const { generateWebpackConfig, merge } = require("shakapacker")
|
|
849
|
-
|
|
850
|
-
const webpackConfig = generateWebpackConfig()
|
|
851
|
-
|
|
852
|
-
const options = {
|
|
853
|
-
resolve: {
|
|
854
|
-
extensions: [".css", ".ts", ".tsx"]
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
module.exports = merge(options, webpackConfig)
|
|
859
|
-
```
|
|
860
|
-
|
|
861
|
-
This example is based on [an example project](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/webpack.config.js)
|
|
862
|
-
|
|
863
|
-
Shakapacker gives you a default configuration file `config/webpack/webpack.config.js`, which, by default, you don't need to make any changes to `config/webpack/webpack.config.js` since it's a standard production-ready configuration. However, you will probably want to customize or add a new loader by modifying the webpack configuration, as shown above.
|
|
864
|
-
|
|
865
|
-
You might add separate files to keep your code more organized.
|
|
866
|
-
|
|
867
|
-
```js
|
|
868
|
-
// config/webpack/custom.js
|
|
869
|
-
module.exports = {
|
|
870
|
-
resolve: {
|
|
871
|
-
alias: {
|
|
872
|
-
jquery: "jquery/src/jquery",
|
|
873
|
-
vue: "vue/dist/vue.js",
|
|
874
|
-
React: "react",
|
|
875
|
-
ReactDOM: "react-dom",
|
|
876
|
-
vue_resource: "vue-resource/dist/vue-resource"
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
```
|
|
881
|
-
|
|
882
|
-
Then `require` this file in your `config/webpack/webpack.config.js`:
|
|
883
|
-
|
|
884
|
-
```js
|
|
885
|
-
// config/webpack/webpack.config.js
|
|
886
|
-
// use the new NPM package name, `shakapacker`.
|
|
887
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
888
|
-
|
|
889
|
-
const customConfig = require("./custom")
|
|
890
|
-
|
|
891
|
-
module.exports = generateWebpackConfig(customConfig)
|
|
892
|
-
```
|
|
893
|
-
|
|
894
|
-
If you need access to configs within Shakapacker's configuration, you can import them like so:
|
|
895
|
-
|
|
896
|
-
```js
|
|
897
|
-
// config/webpack/webpack.config.js
|
|
898
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
899
|
-
|
|
900
|
-
const webpackConfig = generateWebpackConfig()
|
|
901
|
-
|
|
902
|
-
console.log(webpackConfig.output_path)
|
|
903
|
-
console.log(webpackConfig.source_path)
|
|
904
|
-
|
|
905
|
-
// Or to print out your whole webpack configuration
|
|
906
|
-
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
|
907
|
-
```
|
|
908
|
-
|
|
909
|
-
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:
|
|
910
|
-
|
|
911
|
-
```js
|
|
912
|
-
const fileRule = config.module.rules.find((rule) => rule.test.test(".svg"))
|
|
913
|
-
// removing svg from asset file rule's test RegExp
|
|
914
|
-
fileRule.test =
|
|
915
|
-
/\.(bmp|gif|jpe?g|png|tiff|ico|avif|webp|eot|otf|ttf|woff|woff2)$/
|
|
916
|
-
// changing the rule type from 'asset/resource' to 'asset'. See https://webpack.js.org/guides/asset-modules/
|
|
917
|
-
fileRule.type = "asset"
|
|
918
|
-
```
|
|
919
|
-
|
|
920
|
-
### Babel configuration
|
|
921
|
-
|
|
922
|
-
If you choose to use Babel instead of the default SWC transpiler, you will need to configure it in your `package.json`:
|
|
923
|
-
|
|
924
|
-
```json
|
|
925
|
-
"babel": {
|
|
926
|
-
"presets": [
|
|
927
|
-
"./node_modules/shakapacker/package/babel/preset.js"
|
|
928
|
-
]
|
|
929
|
-
},
|
|
930
|
-
```
|
|
931
|
-
|
|
932
|
-
You can also 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).
|
|
933
|
-
|
|
934
|
-
### SWC configuration
|
|
935
|
-
|
|
936
|
-
SWC is the recommended JavaScript transpiler in Shakapacker v10 (this default was introduced in v9). SWC's own benchmark reports being [20x faster than Babel on a single thread and 70x faster on four cores](https://swc.rs/); end-to-end Shakapacker speedups are typically smaller but still substantial. New installations use SWC by default via the installation template. You can read more at [SWC usage docs](./docs/using_swc_loader.md).
|
|
937
|
-
|
|
938
|
-
**Note on defaults**: The installation template explicitly sets `javascript_transpiler: "swc"` for new projects. However, for backward compatibility, webpack's runtime default (when no explicit config exists) remains `"babel"`. Rspack always defaults to `"swc"`.
|
|
939
|
-
|
|
940
|
-
Please note that SWC supports [React](#react) integration out of the box - no additional configuration needed.
|
|
941
|
-
|
|
942
|
-
### esbuild loader configuration
|
|
943
|
-
|
|
944
|
-
You can use esbuild as an alternative JavaScript transpiler. You can read more at [esbuild-loader usage docs](./docs/using_esbuild_loader.md).
|
|
945
|
-
|
|
946
|
-
Please note that esbuild supports [React](#react) integration out of the box - no additional configuration needed.
|
|
947
|
-
|
|
948
|
-
### Switching between transpilers
|
|
949
|
-
|
|
950
|
-
To switch between Babel, SWC, or esbuild, or to configure environment-specific transpiler settings, see the [Transpiler Migration Guide](./docs/transpiler-migration.md).
|
|
951
|
-
|
|
952
|
-
### Debugging Configuration
|
|
953
|
-
|
|
954
|
-
Shakapacker provides a powerful utility to export and analyze your webpack/rspack configuration:
|
|
955
|
-
|
|
956
|
-
```bash
|
|
957
|
-
# Export all configs for troubleshooting (recommended)
|
|
958
|
-
bin/shakapacker-config --doctor
|
|
959
|
-
|
|
960
|
-
# Or via rake task
|
|
961
|
-
bundle exec rake shakapacker:export_bundler_config -- --doctor
|
|
962
|
-
```
|
|
963
|
-
|
|
964
|
-
This exports development and production configurations for both client and server bundles to `shakapacker-config-exports/` directory in annotated YAML format. Perfect for:
|
|
965
|
-
|
|
966
|
-
- Debugging configuration issues
|
|
967
|
-
- Comparing webpack vs rspack configs (works with `rake shakapacker:switch_bundler`)
|
|
968
|
-
- Understanding differences between development and production
|
|
969
|
-
- Analyzing client vs server bundle configurations
|
|
970
|
-
|
|
971
|
-
For more options and usage examples, see the [Troubleshooting Guide](./docs/troubleshooting.md#exporting-webpack--rspack-configuration).
|
|
972
|
-
|
|
973
|
-
#### Comparing Configurations
|
|
974
|
-
|
|
975
|
-
Once you've exported configurations, compare them semantically with `bin/diff-bundler-config` (powered by [`pack-config-diff`](https://github.com/shakacode/pack-config-diff)):
|
|
976
|
-
|
|
977
|
-
```bash
|
|
978
|
-
# Compare development vs production configs
|
|
979
|
-
bin/diff-bundler-config \
|
|
980
|
-
--left=shakapacker-config-exports/webpack-development-client.yaml \
|
|
981
|
-
--right=shakapacker-config-exports/webpack-production-client.yaml
|
|
982
|
-
|
|
983
|
-
# Get a quick summary
|
|
984
|
-
bin/diff-bundler-config \
|
|
985
|
-
--left=config1.yaml \
|
|
986
|
-
--right=config2.yaml \
|
|
987
|
-
--format=summary
|
|
988
|
-
```
|
|
989
|
-
|
|
990
|
-
This is useful for:
|
|
991
|
-
|
|
992
|
-
- Understanding environment-specific config changes
|
|
993
|
-
- Debugging unexpected behavior differences
|
|
994
|
-
- Migration validation (webpack -> rspack)
|
|
995
|
-
- Reviewing bundler config changes in PRs
|
|
996
|
-
|
|
997
|
-
For full usage, see the [Configuration Diff Guide](./docs/config-diff.md).
|
|
998
|
-
|
|
999
|
-
### Integrations
|
|
1000
|
-
|
|
1001
|
-
Shakapacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
|
|
1002
|
-
|
|
1003
|
-
#### React
|
|
1004
|
-
|
|
1005
|
-
See here for detailed instructions on how to [configure Shakapacker to bundle a React app](./docs/react.md) (with optional HMR).
|
|
1006
|
-
|
|
1007
|
-
See also [Customizing Babel Config](./docs/customizing_babel_config.md) for an example React configuration.
|
|
1008
|
-
|
|
1009
|
-
#### TypeScript
|
|
1010
|
-
|
|
1011
|
-
**📚 TypeScript Support:** See the **[TypeScript Documentation](./docs/typescript.md)** for type-safe configuration.
|
|
1012
|
-
|
|
1013
|
-
```bash
|
|
1014
|
-
npm install --save-dev typescript
|
|
1015
|
-
|
|
1016
|
-
# If you explicitly use `javascript_transpiler: babel`
|
|
1017
|
-
npm install --save-dev @babel/preset-typescript
|
|
1018
|
-
```
|
|
1019
|
-
|
|
1020
|
-
Shakapacker does not type-check TypeScript during builds. For webpack projects, you can optionally
|
|
1021
|
-
add type-checking during builds with:
|
|
1022
|
-
|
|
1023
|
-
```bash
|
|
1024
|
-
npm install --save-dev fork-ts-checker-webpack-plugin
|
|
1025
|
-
```
|
|
1026
|
-
|
|
1027
|
-
You can also run `tsc --noEmit` separately in CI or local development if you prefer not to wire
|
|
1028
|
-
type-checking into the bundler.
|
|
1029
|
-
|
|
1030
|
-
Add tsconfig.json
|
|
1031
|
-
|
|
1032
|
-
```json
|
|
1033
|
-
{
|
|
1034
|
-
"compilerOptions": {
|
|
1035
|
-
"declaration": false,
|
|
1036
|
-
"emitDecoratorMetadata": true,
|
|
1037
|
-
"experimentalDecorators": true,
|
|
1038
|
-
"lib": ["es6", "dom"],
|
|
1039
|
-
"module": "es6",
|
|
1040
|
-
"moduleResolution": "node",
|
|
1041
|
-
"sourceMap": true,
|
|
1042
|
-
"target": "es5",
|
|
1043
|
-
"jsx": "react",
|
|
1044
|
-
"noEmit": true
|
|
1045
|
-
},
|
|
1046
|
-
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
|
1047
|
-
"compileOnSave": false
|
|
1048
|
-
}
|
|
1049
|
-
```
|
|
1050
|
-
|
|
1051
|
-
Then modify the webpack config to use it as a plugin:
|
|
1052
|
-
|
|
1053
|
-
```js
|
|
1054
|
-
// config/webpack/webpack.config.js
|
|
1055
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
1056
|
-
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
|
|
1057
|
-
|
|
1058
|
-
module.exports = generateWebpackConfig({
|
|
1059
|
-
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
1060
|
-
})
|
|
1061
|
-
```
|
|
1062
|
-
|
|
1063
|
-
Optionally, your webpack config file itself can be written in Typescript:
|
|
1064
|
-
|
|
1065
|
-
```bash
|
|
1066
|
-
npm install ts-node @types/node @types/webpack
|
|
1067
|
-
```
|
|
1068
|
-
|
|
1069
|
-
```ts
|
|
1070
|
-
// config/webpack/webpack.config.ts
|
|
1071
|
-
import { generateWebpackConfig } from "shakapacker"
|
|
1072
|
-
import ForkTSCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin"
|
|
1073
|
-
|
|
1074
|
-
const config = generateWebpackConfig({
|
|
1075
|
-
plugins: [new ForkTSCheckerWebpackPlugin()]
|
|
1076
|
-
})
|
|
1077
|
-
|
|
1078
|
-
export default config
|
|
1079
|
-
```
|
|
1080
|
-
|
|
1081
|
-
#### CSS
|
|
1082
|
-
|
|
1083
|
-
To enable CSS support in your application, add the following packages:
|
|
1084
|
-
|
|
1085
|
-
```bash
|
|
1086
|
-
npm install css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
|
|
1087
|
-
```
|
|
1088
|
-
|
|
1089
|
-
Optionally, add the `CSS` extension to webpack config for easy resolution.
|
|
1090
|
-
|
|
1091
|
-
```js
|
|
1092
|
-
// config/webpack/webpack.config.js
|
|
1093
|
-
const { generateWebpackConfig } = require("shakapacker")
|
|
1094
|
-
|
|
1095
|
-
const customConfig = {
|
|
1096
|
-
resolve: {
|
|
1097
|
-
extensions: [".css"]
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
module.exports = generateWebpackConfig(customConfig)
|
|
1102
|
-
```
|
|
1103
|
-
|
|
1104
|
-
To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
|
|
1105
|
-
then add the relevant pre-processors:
|
|
1106
|
-
|
|
1107
|
-
#### Postcss
|
|
1108
|
-
|
|
1109
|
-
```bash
|
|
1110
|
-
npm install postcss postcss-loader
|
|
1111
|
-
```
|
|
1112
|
-
|
|
1113
|
-
Optionally add these two plugins if they are required in your `postcss.config.js`:
|
|
1114
|
-
|
|
1115
|
-
```bash
|
|
1116
|
-
npm install postcss-preset-env postcss-flexbugs-fixes
|
|
1117
|
-
```
|
|
1118
|
-
|
|
1119
|
-
#### Sass
|
|
1120
|
-
|
|
1121
|
-
```bash
|
|
1122
|
-
npm install sass-loader
|
|
1123
|
-
```
|
|
1124
|
-
|
|
1125
|
-
You will also need to install [Dart Sass](https://github.com/sass/dart-sass), [Node Sass](https://github.com/sass/node-sass) or [Sass Embedded](https://github.com/sass/embedded-host-node) to pick the implementation to use. sass-loader will automatically pick an implementation based on installed packages.
|
|
1126
|
-
|
|
1127
|
-
Please refer to [sass-loader documentation](https://www.npmjs.com/package/sass-loader) and individual packages repos for more information on all the options.
|
|
1128
|
-
|
|
1129
|
-
##### Dart Sass
|
|
1130
|
-
|
|
1131
|
-
```bash
|
|
1132
|
-
npm install sass
|
|
1133
|
-
```
|
|
1134
|
-
|
|
1135
|
-
##### Node Sass
|
|
1136
|
-
|
|
1137
|
-
```bash
|
|
1138
|
-
npm install node-sass
|
|
1139
|
-
```
|
|
1140
|
-
|
|
1141
|
-
##### Sass Embedded
|
|
1142
|
-
|
|
1143
|
-
```bash
|
|
1144
|
-
npm install sass-embedded
|
|
1145
|
-
```
|
|
1146
|
-
|
|
1147
|
-
#### Less
|
|
1148
|
-
|
|
1149
|
-
```bash
|
|
1150
|
-
npm install less less-loader
|
|
1151
|
-
```
|
|
1152
|
-
|
|
1153
|
-
#### Stylus
|
|
1154
|
-
|
|
1155
|
-
```bash
|
|
1156
|
-
npm install stylus stylus-loader
|
|
1157
|
-
```
|
|
1158
|
-
|
|
1159
|
-
#### CoffeeScript
|
|
1160
|
-
|
|
1161
|
-
```bash
|
|
1162
|
-
npm install coffeescript coffee-loader
|
|
1163
|
-
```
|
|
1164
|
-
|
|
1165
|
-
#### Other frameworks
|
|
1166
|
-
|
|
1167
|
-
Please follow Webpack integration guide for the relevant framework or library,
|
|
1168
|
-
|
|
1169
|
-
1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
|
|
1170
|
-
2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
|
|
1171
|
-
3. [Vue](https://vue-loader.vuejs.org/guide/)
|
|
1172
|
-
|
|
1173
|
-
For example to add Vue support:
|
|
1174
|
-
|
|
1175
|
-
```js
|
|
1176
|
-
// config/webpack/rules/vue.js
|
|
1177
|
-
const { VueLoaderPlugin } = require("vue-loader")
|
|
1178
|
-
|
|
1179
|
-
module.exports = {
|
|
1180
|
-
module: {
|
|
1181
|
-
rules: [
|
|
1182
|
-
{
|
|
1183
|
-
test: /\.vue$/,
|
|
1184
|
-
loader: "vue-loader"
|
|
1185
|
-
}
|
|
1186
|
-
]
|
|
1187
|
-
},
|
|
1188
|
-
plugins: [new VueLoaderPlugin()],
|
|
1189
|
-
resolve: {
|
|
1190
|
-
extensions: [".vue"]
|
|
1191
|
-
}
|
|
1192
|
-
}
|
|
1193
|
-
```
|
|
1194
|
-
|
|
1195
|
-
```js
|
|
1196
|
-
// config/webpack/webpack.config.js
|
|
1197
|
-
const { generateWebpackConfig, merge } = require("shakapacker")
|
|
1198
|
-
|
|
1199
|
-
const webpackConfig = generateWebpackConfig()
|
|
1200
|
-
|
|
1201
|
-
const vueConfig = require("./rules/vue")
|
|
1202
|
-
|
|
1203
|
-
module.exports = merge(vueConfig, webpackConfig)
|
|
1204
|
-
```
|
|
1205
|
-
|
|
1206
|
-
### Custom Rails environments
|
|
1207
|
-
|
|
1208
|
-
Out of the box Shakapacker ships with - development, test and production environments in `config/shakapacker.yml` however, in most production apps extra environments are needed as part of the deployment workflow. Shakapacker supports this out of the box from version 3.4.0+ onwards.
|
|
1209
|
-
|
|
1210
|
-
You can choose to define additional environment configurations in shakapacker.yml,
|
|
1211
|
-
|
|
1212
|
-
```yml
|
|
1213
|
-
staging:
|
|
1214
|
-
<<: *default
|
|
1215
|
-
|
|
1216
|
-
# Production depends on precompilation of packs prior to booting for performance.
|
|
1217
|
-
compile: false
|
|
1218
|
-
|
|
1219
|
-
# Cache manifest.json for performance
|
|
1220
|
-
cache_manifest: true
|
|
1221
|
-
|
|
1222
|
-
# Compile staging packs to a separate directory
|
|
1223
|
-
public_output_path: packs-staging
|
|
1224
|
-
```
|
|
1225
|
-
|
|
1226
|
-
Otherwise, Shakapacker will use the production environment as a fallback environment for loading configurations. Please note, `NODE_ENV` can either be set to `production`, `development` or `test`. This means you don't need to create additional environment files inside `config/shakapacker/*` and instead use shakapacker.yml to load different configurations using `RAILS_ENV`.
|
|
1227
|
-
|
|
1228
|
-
For example, the below command will compile assets in production mode but will use staging configurations from `config/shakapacker.yml` if available or use fallback production environment configuration:
|
|
1229
|
-
|
|
1230
|
-
```bash
|
|
1231
|
-
RAILS_ENV=staging bundle exec rake assets:precompile
|
|
1232
|
-
```
|
|
1233
|
-
|
|
1234
|
-
And, this will compile in development mode and load configuration for the cucumber environment if defined in `shakapacker.yml` or fallback to production configuration
|
|
1235
|
-
|
|
1236
|
-
```bash
|
|
1237
|
-
RAILS_ENV=cucumber NODE_ENV=development bundle exec rake assets:precompile
|
|
1238
|
-
```
|
|
1239
|
-
|
|
1240
|
-
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
|
1241
|
-
|
|
1242
|
-
```bash
|
|
1243
|
-
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
|
1244
|
-
./bin/shakapacker
|
|
1245
|
-
./bin/shakapacker-dev-server
|
|
1246
|
-
|
|
1247
|
-
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/shakapacker/compile.rake`
|
|
1248
|
-
bundle exec rake assets:precompile
|
|
1249
|
-
bundle exec rake shakapacker:compile
|
|
1250
|
-
```
|
|
1251
|
-
|
|
1252
|
-
### Upgrading
|
|
1253
|
-
|
|
1254
|
-
You can run the following commands to upgrade Shakapacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
|
1255
|
-
|
|
1256
|
-
```bash
|
|
1257
|
-
# check your Gemfile for version restrictions
|
|
1258
|
-
bundle update shakapacker
|
|
1259
|
-
|
|
1260
|
-
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
|
1261
|
-
bundle exec rake shakapacker:install
|
|
1262
|
-
|
|
1263
|
-
# using npm
|
|
1264
|
-
npm install shakapacker@latest
|
|
1265
|
-
npm install webpack-dev-server@latest
|
|
1266
|
-
|
|
1267
|
-
# using yarn classic
|
|
1268
|
-
yarn upgrade shakapacker --latest
|
|
1269
|
-
yarn upgrade webpack-dev-server --latest
|
|
1270
|
-
|
|
1271
|
-
# using yarn berry
|
|
1272
|
-
yarn up shakapacker@latest
|
|
1273
|
-
yarn up webpack-dev-server@latest
|
|
1274
|
-
|
|
1275
|
-
# using pnpm
|
|
1276
|
-
pnpm up shakapacker@latest
|
|
1277
|
-
pnpm up webpack-dev-server@latest
|
|
1278
|
-
|
|
1279
|
-
# Or to install the latest release (including pre-releases)
|
|
1280
|
-
npm install shakapacker@next
|
|
1281
|
-
```
|
|
1282
|
-
|
|
1283
|
-
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
1284
|
-
|
|
1285
|
-
#### Automating Updates with Dependabot
|
|
1286
|
-
|
|
1287
|
-
Shakapacker is shipped as both a Ruby gem and an npm package, so they must be
|
|
1288
|
-
upgraded together. See [Dependabot configuration for Shakapacker](./docs/dependabot.md)
|
|
1289
|
-
for a `.github/dependabot.yml` example that updates both in a single PR.
|
|
1290
|
-
|
|
1291
|
-
#### Common Upgrade Scenarios
|
|
1292
|
-
|
|
1293
|
-
For step-by-step guides on common migrations, see the [Common Upgrades Guide](./docs/common-upgrades.md):
|
|
1294
|
-
|
|
1295
|
-
- [Migrating Package Managers](./docs/common-upgrades.md#migrating-package-managers) (Yarn ↔ npm, pnpm)
|
|
1296
|
-
- [Migrating from Babel to SWC](./docs/common-upgrades.md#migrating-from-babel-to-swc) (upstream: ~20x faster transpilation)
|
|
1297
|
-
- [Migrating from Webpack to Rspack](./docs/common-upgrades.md#migrating-from-webpack-to-rspack) (upstream: ~8–17x faster bundler)
|
|
1298
|
-
|
|
1299
|
-
### Paths
|
|
1300
|
-
|
|
1301
|
-
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.
|
|
1302
|
-
|
|
1303
|
-
The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/javascript/`**(default)** or whatever path you set for `source_entry_path` in the `shakapacker.yml` configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put any file inside `app/javascript` directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "app/javascript/" directory and keep everything else under subdirectories like `app/javascript/controllers`.
|
|
1304
|
-
|
|
1305
|
-
Suppose you want to change the source directory from `app/javascript` to `frontend` and output to `assets/packs`. This is how you would do it:
|
|
1306
|
-
|
|
1307
|
-
```yml
|
|
1308
|
-
# config/shakapacker.yml
|
|
1309
|
-
source_path: frontend # packs are the files in frontend/
|
|
1310
|
-
public_output_path: assets/packs # outputs to => public/assets/packs
|
|
1311
|
-
```
|
|
1312
|
-
|
|
1313
|
-
For server-side rendering (SSR) scenarios where you need to generate bundles that should not be served publicly, you can use the `private_output_path` configuration:
|
|
1314
|
-
|
|
1315
|
-
```yml
|
|
1316
|
-
# config/shakapacker.yml
|
|
1317
|
-
private_output_path: ssr-generated # outputs to => ssr-generated/
|
|
1318
|
-
```
|
|
1319
|
-
|
|
1320
|
-
This is particularly useful when working with libraries like React on Rails where server bundles need to be kept separate from client bundles.
|
|
1321
|
-
|
|
1322
|
-
#### Migration Guide for React on Rails Users
|
|
1323
|
-
|
|
1324
|
-
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:
|
|
1325
|
-
|
|
1326
|
-
1. Update your `config/shakapacker.yml`:
|
|
1327
|
-
|
|
1328
|
-
```yml
|
|
1329
|
-
# Before: both client and server bundles in public/
|
|
1330
|
-
# After: separate directories
|
|
1331
|
-
public_output_path: packs # Client bundles (publicly served)
|
|
1332
|
-
private_output_path: ssr-bundles # Server bundles (not publicly served)
|
|
1333
|
-
```
|
|
1334
|
-
|
|
1335
|
-
2. Update your webpack configuration to use the appropriate output path based on the bundle type
|
|
1336
|
-
3. The validation ensures `private_output_path` and `public_output_path` are different to prevent configuration errors
|
|
1337
|
-
|
|
1338
|
-
Similarly, you can also control and configure `webpack-dev-server` settings from `config/shakapacker.yml` file:
|
|
1339
|
-
|
|
1340
|
-
```yml
|
|
1341
|
-
# config/shakapacker.yml
|
|
1342
|
-
development:
|
|
1343
|
-
dev_server:
|
|
1344
|
-
host: localhost
|
|
1345
|
-
port: 3035
|
|
1346
|
-
```
|
|
1347
|
-
|
|
1348
|
-
If you have `hmr` turned to true and `inline_css` is not false, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
|
|
1349
|
-
|
|
1350
|
-
If you want to have HMR and separate link tags, set `hmr: true` and `inline_css: false`. This will cause styles to be extracted and reloaded with the `mini-css-extract-plugin` loader. Note that in this scenario, you do not need to include style-loader in your project dependencies.
|
|
1351
|
-
|
|
1352
|
-
### Additional paths
|
|
1353
|
-
|
|
1354
|
-
If you are adding Shakapacker to an existing app that has most of the assets inside `app/assets` or inside an engine, and you want to share that with webpack modules, you can use the `additional_paths` option available in `config/shakapacker.yml`. This lets you
|
|
1355
|
-
add additional paths that webpack should look up when resolving modules:
|
|
1356
|
-
|
|
1357
|
-
```yml
|
|
1358
|
-
additional_paths: ["app/assets", "vendor/assets"]
|
|
1359
|
-
```
|
|
1360
|
-
|
|
1361
|
-
You can then import these items inside your modules like so:
|
|
1362
|
-
|
|
1363
|
-
```js
|
|
1364
|
-
// Note it's relative to parent directory i.e. app/assets
|
|
1365
|
-
import "stylesheets/main"
|
|
1366
|
-
import "images/rails.png"
|
|
1367
|
-
```
|
|
1368
|
-
|
|
1369
|
-
Assets put in these folders will also have their path stripped just like with the `source_path`.
|
|
1370
|
-
|
|
1371
|
-
Example:
|
|
1372
|
-
|
|
1373
|
-
A file in `app/assets/images/image.svg` with `additional_paths: ['app/assets']` will result in `static/images/image.svg`
|
|
1374
|
-
|
|
1375
|
-
**Note:** Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of the whole parent directory if you just need to reference one or two modules
|
|
1376
|
-
|
|
1377
|
-
**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.
|
|
1378
|
-
|
|
1379
|
-
## Deployment
|
|
1380
|
-
|
|
1381
|
-
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).
|
|
1382
|
-
|
|
1383
|
-
This behavior is optional & can be disabled by either setting a `SHAKAPACKER_PRECOMPILE` environment variable to `false`, `no`, `n`, or `f`, or by setting a `shakapacker_precompile` key in your `shakapacker.yml` to `false`. ([source code](./lib/shakapacker/configuration.rb#L34))
|
|
1384
|
-
|
|
1385
|
-
When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to ensure the exact versions specified in your lockfile are installed:
|
|
1386
|
-
|
|
1387
|
-
```
|
|
1388
|
-
# using npm
|
|
1389
|
-
npm ci
|
|
1390
|
-
|
|
1391
|
-
# using yarn classic
|
|
1392
|
-
yarn install --frozen-lockfile
|
|
1393
|
-
|
|
1394
|
-
# using yarn berry
|
|
1395
|
-
yarn install --immutable
|
|
1396
|
-
|
|
1397
|
-
# using pnpm
|
|
1398
|
-
pnpm install --frozen-lockfile
|
|
1399
|
-
|
|
1400
|
-
# using bun
|
|
1401
|
-
bun install --frozen-lockfile
|
|
1402
|
-
```
|
|
1403
|
-
|
|
1404
|
-
### CDN
|
|
1405
|
-
|
|
1406
|
-
Shakapacker supports serving JavaScript bundles and assets from a CDN. The key configuration is setting the `SHAKAPACKER_ASSET_HOST` environment variable (NOT the Rails `ASSET_HOST` variable).
|
|
1407
|
-
|
|
1408
|
-
For detailed CDN setup instructions, including CloudFlare configuration, troubleshooting, and advanced setups, see the [CDN Setup Guide](./docs/cdn_setup.md).
|
|
1409
|
-
|
|
1410
|
-
**Quick example:**
|
|
1411
|
-
|
|
1412
|
-
```bash
|
|
1413
|
-
export SHAKAPACKER_ASSET_HOST=https://cdn.example.com
|
|
1414
|
-
RAILS_ENV=production bundle exec rake assets:precompile
|
|
1415
|
-
```
|
|
1416
|
-
|
|
1417
|
-
For more deployment documentation, see [Deployment](./docs/deployment.md).
|
|
1418
|
-
|
|
1419
|
-
## Example Apps
|
|
1420
|
-
|
|
1421
|
-
- [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)
|
|
1422
|
-
|
|
1423
|
-
## Troubleshooting
|
|
164
|
+
## Community and Support
|
|
1424
165
|
|
|
1425
|
-
|
|
166
|
+
- Ask questions in the
|
|
167
|
+
[React + Rails Slack community](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE).
|
|
168
|
+
- Report bugs or request features in
|
|
169
|
+
[GitHub Issues](https://github.com/shakacode/shakapacker/issues).
|
|
170
|
+
- Get direct help from [ShakaCode](https://www.shakacode.com), the team that
|
|
171
|
+
maintains Shakapacker and helps Rails teams migrate to Rspack, speed up build
|
|
172
|
+
pipelines, and stabilize production deploys.
|
|
1426
173
|
|
|
1427
174
|
## Contributing
|
|
1428
175
|
|
|
1429
|
-
|
|
176
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, test commands,
|
|
177
|
+
and pull request guidelines.
|
|
1430
178
|
|
|
1431
179
|
## License
|
|
1432
180
|
|