shakapacker 6.2.1 → 6.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +53 -4
- data/CONTRIBUTING.md +2 -2
- data/Gemfile +2 -1
- data/Gemfile.lock +84 -74
- data/README.md +172 -65
- data/docs/react.md +262 -0
- data/docs/sprockets.md +10 -0
- data/docs/v6_upgrade.md +1 -1
- data/lib/install/config/webpacker.yml +14 -0
- data/lib/tasks/webpacker/clean.rake +1 -3
- data/lib/tasks/webpacker/clobber.rake +1 -3
- data/lib/tasks/webpacker/compile.rake +3 -14
- data/lib/tasks/webpacker.rake +0 -1
- data/lib/webpacker/base_strategy.rb +24 -0
- data/lib/webpacker/compiler.rb +4 -67
- data/lib/webpacker/compiler_strategy.rb +20 -0
- data/lib/webpacker/configuration.rb +13 -0
- data/lib/webpacker/digest_strategy.rb +59 -0
- data/lib/webpacker/helper.rb +26 -1
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +2 -2
- data/lib/webpacker/mtime_strategy.rb +40 -0
- data/lib/webpacker/version.rb +1 -1
- data/package/babel/preset.js +0 -1
- data/package/environments/__tests__/base.js +30 -3
- data/package/environments/base.js +8 -1
- data/package/rules/file.js +2 -2
- data/package.json +13 -12
- data/test/compiler_strategy_test.rb +27 -0
- data/test/compiler_test.rb +26 -34
- data/test/configuration_test.rb +24 -4
- data/test/digest_strategy_test.rb +33 -0
- data/test/helper_test.rb +22 -0
- data/test/manifest_test.rb +3 -3
- data/test/mtime_strategy_test.rb +42 -0
- data/test/rake_tasks_test.rb +0 -34
- data/test/test_app/app/packs/entrypoints/generated/something.js +2 -0
- data/test/test_app/config/webpacker.yml +1 -0
- data/test/test_app/config/webpacker_nested_entries.yml +83 -0
- data/test/test_app/config/webpacker_no_precompile.yml +7 -0
- data/yarn.lock +917 -884
- metadata +21 -5
- data/lib/tasks/webpacker/yarn_install.rake +0 -18
- data/lib/tasks/yarn.rake +0 -38
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Shakapacker
|
2
2
|
|
3
|
-
_Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker). Internal naming for `shakapacker` will continue to use `webpacker` where possible for v6. ShakaCode stands behind long-term maintainence and development of this project for the Rails community._
|
3
|
+
_Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker). Internal naming for `shakapacker` will continue to use `webpacker` where possible for v6. ShakaCode stands behind the long-term maintainence and development of this project for the Rails community._
|
4
4
|
|
5
5
|
* See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
|
6
6
|
|
@@ -26,7 +26,7 @@ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please
|
|
26
26
|
2. [Slack discussion channel](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE)
|
27
27
|
3. [Tweets with tag `#shakapacker`](https://twitter.com/hashtag/shakapacker?src=hashtag_click)
|
28
28
|
|
29
|
-
[ShakaCode](https://www.shakacode.com) offers
|
29
|
+
[ShakaCode](https://www.shakacode.com) offers support for upgrading from webpacker or using Shakapacker. If interested, contact [justin@shakacode.com](mailto:justin@shakacode.com). ShakaCode is [hiring passionate engineers](https://jobs.lever.co/shakacode/3bdbfdb3-4495-4611-a279-01dddb351abe) that love open source.
|
30
30
|
|
31
31
|
---
|
32
32
|
|
@@ -62,6 +62,7 @@ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please
|
|
62
62
|
- [Other frameworks](#other-frameworks)
|
63
63
|
- [Custom Rails environments](#custom-rails-environments)
|
64
64
|
- [Upgrading](#upgrading)
|
65
|
+
- [Compiler strategies](#compiler-strategies)
|
65
66
|
- [Paths](#paths)
|
66
67
|
- [Additional paths](#additional-paths)
|
67
68
|
- [Deployment](#deployment)
|
@@ -82,8 +83,8 @@ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please
|
|
82
83
|
## Features
|
83
84
|
- Rails view helpers that fully support Webpack output, including HMR and code splitting.
|
84
85
|
- Convenient but not required webpack configuration. The only requirement is that your webpack configuration create a manifest.
|
85
|
-
- HMR with the webpack-dev-server, such as for hot-reloading
|
86
|
-
- Automatic code splitting using multiple entry points to optimize JavaScript downloads
|
86
|
+
- HMR with the webpack-dev-server, such as for hot-reloading React!
|
87
|
+
- Automatic code splitting using multiple entry points to optimize JavaScript downloads.
|
87
88
|
- [Webpack v5+](https://webpack.js.org/)
|
88
89
|
- ES6 with [babel](https://babeljs.io/), [SWC](https://swc.rs/), or [Esbuild](https://github.com/privatenumber/esbuild-loader)
|
89
90
|
- Asset compression, source-maps, and minification
|
@@ -99,7 +100,6 @@ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please
|
|
99
100
|
|
100
101
|
## Installation
|
101
102
|
|
102
|
-
|
103
103
|
### Rails v6+
|
104
104
|
|
105
105
|
With Rails v6+, skip JavaScript for a new app and follow below Manual Installation Steps to manually add the `shakapacker` gem to your Gemfile.
|
@@ -128,7 +128,7 @@ When `package.json` and/or `yarn.lock` changes, such as when pulling down change
|
|
128
128
|
yarn
|
129
129
|
```
|
130
130
|
|
131
|
-
Note, in v6
|
131
|
+
Note, in v6+, most JS packages are peer dependencies. Thus, the installer will add the packages:
|
132
132
|
|
133
133
|
```bash
|
134
134
|
yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader \
|
@@ -139,40 +139,70 @@ yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/ru
|
|
139
139
|
Previously, these "webpack" and "babel" packages were direct dependencies for `webpacker`. By
|
140
140
|
making these peer dependencies, you have control over the versions used in your webpack and babel configs.
|
141
141
|
|
142
|
-
### Note for Sprockets usage
|
143
|
-
|
144
|
-
If you are still using Sprockets for some of your assets, you might want to include files from `node_modules` directory in your asset pipeline. This is useful, for example, if you want to reference a stylesheet from a node package in your `.scss` stylesheet.
|
145
|
-
|
146
|
-
In order to enable this, make sure you add `node_modules` to the asset load path by adding the following in an initializer (for example `config/initializers/assets.rb`)
|
147
|
-
```ruby
|
148
|
-
Rails.application.config.assets.paths << Rails.root.join('node_modules')
|
149
|
-
```
|
150
|
-
|
151
142
|
### Note for Yarn v2 usage
|
152
143
|
|
153
144
|
If you are using Yarn v2 (berry), please note that PnP modules are not supported.
|
154
145
|
|
155
146
|
In order to use Shakapacker with Yarn v2, make sure you set `nodeLinker: node-modules` in your `.yarnrc.yml` file as per the [Yarn docs](https://yarnpkg.com/getting-started/migration#step-by-step) to opt out of Plug'n'Play behaviour.
|
156
147
|
|
148
|
+
## Concepts
|
149
|
+
|
150
|
+
At it's core, Shakapacker's essential functionality is to:
|
151
|
+
|
152
|
+
1. Provide configuration by a single file used by both Rails view helpers and JavaScript webpack compilation code.
|
153
|
+
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.
|
154
|
+
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.
|
155
|
+
|
157
156
|
## Usage
|
158
157
|
|
159
|
-
###
|
158
|
+
### Configuration and Code
|
160
159
|
|
161
|
-
|
160
|
+
You will need your file system to correspond to the setup of your `webpacker.yml` file.
|
162
161
|
|
162
|
+
Suppose you have the following configuration:
|
163
|
+
|
164
|
+
`webacker.yml`
|
163
165
|
```yml
|
166
|
+
default: &default
|
167
|
+
source_path: app/javascript
|
168
|
+
source_entry_path: packs
|
169
|
+
public_root_path: public
|
170
|
+
public_output_path: packs
|
171
|
+
nested_entries: false
|
172
|
+
# And more
|
173
|
+
```
|
174
|
+
|
175
|
+
And that maps to a directory structure like this:
|
176
|
+
|
177
|
+
```
|
164
178
|
app/javascript:
|
165
|
-
#
|
166
|
-
└── application.js
|
167
|
-
└── application.css
|
168
|
-
└── src:
|
179
|
+
└── packs: # sets up webpack entries
|
180
|
+
│ └── application.js # references ../src/my_component.js
|
181
|
+
│ └── application.css
|
182
|
+
└── src: # any directory name is fine. Referenced files need to be under source_path
|
169
183
|
│ └── my_component.js
|
170
184
|
└── stylesheets:
|
171
185
|
│ └── my_styles.css
|
172
186
|
└── images:
|
173
187
|
└── logo.svg
|
188
|
+
public/packs # webpack output
|
174
189
|
```
|
175
190
|
|
191
|
+
Webpack intelligently includes only necessary files. In this example, the file `packs/application.js` would reference `../src/my_component.js`
|
192
|
+
|
193
|
+
`nested_entries` allows you to have webpack entry points nested in subdirectories. This defaults to false so you don't accidentally create entry points for an entire tree of files. In other words, with `nested_entries: false`, you can have your entire `source_path` used for your source (using the `source_entry_path: /`) and you place files at the top level that you want as entry points. `nested_entries: true` allows you to have entries that are in subdirectories. This is useful if you have entries that are generated, so you can have a `generated` subdirectory and easily separate generated files from the rest of your codebase.
|
194
|
+
|
195
|
+
### View Helpers
|
196
|
+
The Shakapacker view helpers generate the script and link tags to get the webpack output onto your views.
|
197
|
+
|
198
|
+
Be sure to consult the API documentation in the source code of [helper.rb](./lib/webpacker/helper.rb).
|
199
|
+
|
200
|
+
**Note:** In order 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.
|
201
|
+
|
202
|
+
#### View Helpers `javascript_pack_tag` and `stylesheet_pack_tag`
|
203
|
+
|
204
|
+
These view helpers take your `webpacker.yml` configuration file, along with the resulting webpack compilation `manifest.json` and generates the HTML to load the assets.
|
205
|
+
|
176
206
|
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`:
|
177
207
|
|
178
208
|
```erb
|
@@ -183,11 +213,14 @@ You can then link the JavaScript pack in Rails views using the `javascript_pack_
|
|
183
213
|
The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
|
184
214
|
packs with the chunks in your view, which creates html tags for all the chunks.
|
185
215
|
|
186
|
-
|
216
|
+
You can provide multiple packs and other attributes. Note, `defer` defaults to showing.
|
187
217
|
|
188
218
|
```erb
|
189
219
|
<%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
|
220
|
+
```
|
190
221
|
|
222
|
+
The resulting HTML would look like:
|
223
|
+
```
|
191
224
|
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
192
225
|
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
193
226
|
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload" defer"></script>
|
@@ -195,50 +228,100 @@ The result looks like this:
|
|
195
228
|
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
|
196
229
|
```
|
197
230
|
|
198
|
-
|
231
|
+
In this output, both the calendar and map codes might refer to other common libraries. Those get placed something like the vendor bundle. The view helper removes any duplication.
|
232
|
+
|
233
|
+
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`.
|
234
|
+
|
235
|
+
**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.
|
199
236
|
|
200
237
|
```erb
|
201
238
|
<%# DO %>
|
202
239
|
<%= javascript_pack_tag 'calendar', 'map' %>
|
203
|
-
<%= stylesheet_pack_tag 'calendar', 'map' %>
|
204
240
|
|
205
241
|
<%# DON'T %>
|
206
242
|
<%= javascript_pack_tag 'calendar' %>
|
207
243
|
<%= javascript_pack_tag 'map' %>
|
208
|
-
<%= stylesheet_pack_tag 'calendar' %>
|
209
|
-
<%= stylesheet_pack_tag 'map' %>
|
210
244
|
```
|
211
|
-
|
212
|
-
However, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple <style> tags for different output media:
|
245
|
+
While this also generally applies to `stylesheet_pack_tag`, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple <style> tags for different output media:
|
213
246
|
|
214
247
|
``` erb
|
215
248
|
<%= stylesheet_pack_tag 'application', media: 'screen' %>
|
216
249
|
<%= stylesheet_pack_tag 'print', media: 'print' %>
|
217
250
|
```
|
218
251
|
|
219
|
-
|
252
|
+
#### View Helper `append_javascript_pack_tag`
|
253
|
+
|
254
|
+
If you need configure your 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 helper `append_javascript_pack_tag` can solve this problem. This helper will queue up packs when the `javascript_pack_tag` is finally used.
|
255
|
+
|
256
|
+
Main view:
|
257
|
+
```erb
|
258
|
+
<% append_javascript_pack_tag 'calendar' %>
|
259
|
+
```
|
260
|
+
|
261
|
+
Some partial:
|
262
|
+
```erb
|
263
|
+
<% append_javascript_pack_tag 'map' %>
|
264
|
+
```
|
265
|
+
|
266
|
+
And the main layout has:
|
267
|
+
```erb
|
268
|
+
<%= javascript_pack_tag 'application' %>
|
269
|
+
```
|
270
|
+
|
271
|
+
is the same as using this in the main layout:
|
272
|
+
|
273
|
+
```erb
|
274
|
+
<%= javascript_pack_tag 'calendar', 'map', application' %>
|
275
|
+
```
|
276
|
+
|
277
|
+
However, you typically can't do that in the main layout, as the view and partial codes will depend on the route.
|
278
|
+
|
279
|
+
Thus, you can distribute the logic of what packs are needed for any route. All the magic of splitting up the code was automatic!
|
280
|
+
|
281
|
+
**Important:** `append_javascript_pack_tag` can be used anywhere in your application as long as it is executed BEFORE the `javascript_pack_tag`. If you attempt to call `append_javascript_pack_tag` helper after `javascript_pack_tag`, an error will be raised. You should have only a single `javascript_pack_tag` invocation in your page load.
|
282
|
+
|
283
|
+
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`.
|
284
|
+
|
285
|
+
```erb
|
286
|
+
<% content_for :footer do
|
287
|
+
render 'shared/footer' %>
|
288
|
+
|
289
|
+
<%= javascript_pack_tag %>
|
290
|
+
|
291
|
+
<%= content_for :footer %>
|
292
|
+
```
|
293
|
+
|
294
|
+
For alternative options of setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
|
295
|
+
|
296
|
+
#### View Helper: `asset_pack_path`
|
220
297
|
|
221
298
|
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
222
299
|
```erb
|
223
300
|
<img src="<%= asset_pack_path 'static/logo.svg' %>" />
|
224
301
|
```
|
225
302
|
|
303
|
+
#### View Helper: `image_pack_tag`
|
304
|
+
|
226
305
|
Or use the dedicated helper:
|
227
306
|
```erb
|
228
307
|
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
229
308
|
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
230
309
|
```
|
231
310
|
|
311
|
+
#### View Helper: `favicon_pack_tag`
|
232
312
|
If you want to create a favicon:
|
233
313
|
```erb
|
234
314
|
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
235
315
|
```
|
236
316
|
|
317
|
+
#### View Helper: `preload_pack_asset`
|
237
318
|
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
238
319
|
```erb
|
239
320
|
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
240
321
|
```
|
241
322
|
|
323
|
+
|
324
|
+
### Images in Stylesheets
|
242
325
|
If you want to use images in your stylesheets:
|
243
326
|
|
244
327
|
```css
|
@@ -246,22 +329,39 @@ If you want to use images in your stylesheets:
|
|
246
329
|
background-image: url('../images/logo.svg')
|
247
330
|
}
|
248
331
|
```
|
249
|
-
### Defer for `javascript_pack_tag`
|
250
|
-
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/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
|
251
332
|
|
252
333
|
### Server-Side Rendering (SSR)
|
253
334
|
|
254
335
|
Note, if you are using server-side rendering of JavaScript with dynamic code-splitting, as is often done with extensions to Webpacker, 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.
|
255
336
|
|
256
|
-
**Note:** In order 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.
|
257
|
-
|
258
337
|
### Development
|
259
338
|
|
260
339
|
Webpacker ships with two binstubs: `./bin/webpacker` and `./bin/webpacker-dev-server`. Both 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.
|
261
340
|
|
262
|
-
|
341
|
+
#### Automatic Webpack Code Building
|
342
|
+
|
343
|
+
Shakapacker can be configured to automatically compile on demand when needed using the `webpacker.yml` `compile` option. 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/webpacker --watch` or run `./bin/webpacker-dev-server` during development.
|
263
344
|
|
264
|
-
|
345
|
+
The `compile: true` option can be more useful for test and production builds.
|
346
|
+
|
347
|
+
#### Compiler strategies
|
348
|
+
|
349
|
+
Shakapacker ships with two different strategies that are used to determine whether assets need recompilation per the `compile: true` option:
|
350
|
+
|
351
|
+
- `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.
|
352
|
+
- `mtime` - This strategy looks at 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 file or directory in the watched paths, no recompilation occurs. If the manifest file is order, files are recompiled.
|
353
|
+
|
354
|
+
The `mtime` strategy is generally faster than the `digest` one, but it requires stable timestamps, this makes it perfect for a development environment, such as needing to rebuild bundles for tests, or if you're not changing frontend assets much.
|
355
|
+
|
356
|
+
In production or CI environments, the `digest` strategy is more suitable, unless you are using incremental builds or caching and can guarantee that the timestamps will not change after e.g. cache restore. However, many production or CI environments will explicitly compile assets, so `compile: false` is more appropriate. Otherwise, you'll waste time either checking file timestamps or computing digests.
|
357
|
+
|
358
|
+
You can control what strategy is used by `compiler_strategy` option in `webpacker.yml` config file. By default `mtime` strategy is used in development environment, `digest` is used elsewhere.
|
359
|
+
|
360
|
+
**Note:** If you are not using the webpack-dev-server, your packs will be served by Rails public file server. If you've enabled caching (Rails application `config.action_controller.perform_caching` setting), your changes will likely not be picked up due to `Cache-Control` header being set and assets being cached in browser memory. For more details see the [issue #88](https://github.com/shakacode/shakapacker/issues/88).
|
361
|
+
|
362
|
+
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/webpacker-dev-server`. This process will watch for changes in the relevant files, defined by `webpacker.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/).
|
363
|
+
|
364
|
+
#### Common Development Commands
|
265
365
|
|
266
366
|
```bash
|
267
367
|
# webpack dev server
|
@@ -272,15 +372,6 @@ If you want to use live code reloading, or you have enough JavaScript that on-de
|
|
272
372
|
|
273
373
|
# standalone build
|
274
374
|
./bin/webpacker --progress
|
275
|
-
|
276
|
-
# Help
|
277
|
-
./bin/webpacker help
|
278
|
-
|
279
|
-
# Version
|
280
|
-
./bin/webpacker version
|
281
|
-
|
282
|
-
# Info
|
283
|
-
./bin/webpacker info
|
284
375
|
```
|
285
376
|
|
286
377
|
Once you start this webpack development server, Webpacker 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/webpacker.yml`
|
@@ -288,13 +379,13 @@ Once you start this webpack development server, Webpacker will automatically sta
|
|
288
379
|
You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_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.
|
289
380
|
|
290
381
|
```bash
|
291
|
-
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
|
382
|
+
WEBPACKER_DEV_SERVER_PORT=4305 WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
|
292
383
|
```
|
293
384
|
|
294
|
-
By default, the webpack dev server listens on `localhost` in development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the `host` when running `./bin/webpacker-dev-server` binstub:
|
385
|
+
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/webpacker-dev-server` binstub:
|
295
386
|
|
296
387
|
```bash
|
297
|
-
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
|
388
|
+
WEBPACKER_DEV_SERVER_PORT=4305 WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
|
298
389
|
```
|
299
390
|
|
300
391
|
**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:
|
@@ -313,7 +404,7 @@ end
|
|
313
404
|
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/webpacker.yml` file which the view helpers also use. If you have your own customized webpack configuration, at the minimum, you must ensure:
|
314
405
|
|
315
406
|
1. Your output files go the right directory
|
316
|
-
2.
|
407
|
+
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 webpacker!
|
317
408
|
|
318
409
|
The most practical webpack configuration is to take the default from Shakapacker and then use [webpack-merge](https://github.com/survivejs/webpack-merge) to merge your customizations with the default. For example, suppose you want to add some `resolve.extensions`:
|
319
410
|
|
@@ -336,7 +427,7 @@ module.exports = merge({}, baseWebpackConfig, options)
|
|
336
427
|
|
337
428
|
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)
|
338
429
|
|
339
|
-
|
430
|
+
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.
|
340
431
|
|
341
432
|
You might add separate files to keep your code more organized.
|
342
433
|
|
@@ -366,7 +457,7 @@ const customConfig = require('./custom')
|
|
366
457
|
module.exports = merge(webpackConfig, customConfig)
|
367
458
|
```
|
368
459
|
|
369
|
-
If you need access to configs within
|
460
|
+
If you need access to configs within Shakapacker's configuration, you can import them like so:
|
370
461
|
|
371
462
|
```js
|
372
463
|
// config/webpack/webpack.config.js
|
@@ -388,7 +479,7 @@ svgRule.test = svgRule.test.filter(t => !t.test('.svg'))
|
|
388
479
|
|
389
480
|
### Babel configuration
|
390
481
|
|
391
|
-
By default, you will find the
|
482
|
+
By default, you will find the Shakapacker preset in your `package.json`. Note, you need to use the new NPM package name, `shakapacker`.
|
392
483
|
|
393
484
|
```json
|
394
485
|
"babel": {
|
@@ -397,9 +488,9 @@ By default, you will find the Webpacker preset in your `package.json`. Note, you
|
|
397
488
|
]
|
398
489
|
},
|
399
490
|
```
|
400
|
-
|
401
491
|
Optionally, you can change your Babel configuration by removing these lines in your `package.json` and add [a Babel configuration file](https://babeljs.io/docs/en/config-files) in your project. For an example customization based on the original, see [Customizing Babel Config](./docs/customizing_babel_config.md).
|
402
492
|
|
493
|
+
|
403
494
|
### SWC configuration
|
404
495
|
|
405
496
|
You can try out experimental integration with the SWC loader. You can read more at [SWC usage docs](./docs/using_swc_loader.md).
|
@@ -414,11 +505,13 @@ Please note that if you want opt-in to use esbuild-loader, you can skip [React](
|
|
414
505
|
|
415
506
|
### Integrations
|
416
507
|
|
417
|
-
|
508
|
+
Shakapacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
|
418
509
|
|
419
510
|
#### React
|
420
511
|
|
421
|
-
See
|
512
|
+
See here for detailed instructions on how to [configure Shakapacker to bundle a React app](./docs/react.md) (with optional HMR).
|
513
|
+
|
514
|
+
See also [Customizing Babel Config](./docs/customizing_babel_config.md) for an example React configuration.
|
422
515
|
|
423
516
|
#### Typescript
|
424
517
|
...if you are using typescript, update your `tsconfig.json`
|
@@ -473,7 +566,7 @@ Add tsconfig.json
|
|
473
566
|
"moduleResolution": "node",
|
474
567
|
"baseUrl": ".",
|
475
568
|
"paths": {
|
476
|
-
"*": ["node_modules/*", "app/
|
569
|
+
"*": ["node_modules/*", "app/javascript/*"]
|
477
570
|
},
|
478
571
|
"sourceMap": true,
|
479
572
|
"target": "es5",
|
@@ -589,7 +682,7 @@ module.exports = merge(vueConfig, webpackConfig)
|
|
589
682
|
|
590
683
|
### Custom Rails environments
|
591
684
|
|
592
|
-
Out of the box
|
685
|
+
Out of the box Shakapacker ships with - development, test and production environments in `config/webpacker.yml` however, in most production apps extra environments are needed as part of deployment workflow. Shakapacker supports this out of the box from version 3.4.0+ onwards.
|
593
686
|
|
594
687
|
You can choose to define additional environment configurations in webpacker.yml,
|
595
688
|
|
@@ -607,7 +700,7 @@ staging:
|
|
607
700
|
public_output_path: packs-staging
|
608
701
|
```
|
609
702
|
|
610
|
-
Otherwise
|
703
|
+
Otherwise Shakapacker will use 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/webpacker/*` and instead use webpacker.yml to load different configurations using `RAILS_ENV`.
|
611
704
|
|
612
705
|
For example, the below command will compile assets in production mode but will use staging configurations from `config/webpacker.yml` if available or use fallback production environment configuration:
|
613
706
|
|
@@ -635,7 +728,7 @@ bundle exec rails webpacker:compile
|
|
635
728
|
|
636
729
|
### Upgrading
|
637
730
|
|
638
|
-
You can run following commands to upgrade
|
731
|
+
You can run following commands to upgrade Shakapacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
639
732
|
|
640
733
|
```bash
|
641
734
|
# check your Gemfile for version restrictions
|
@@ -660,15 +753,15 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
660
753
|
|
661
754
|
### Paths
|
662
755
|
|
663
|
-
By default,
|
756
|
+
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/webpacker.yml` file.
|
664
757
|
|
665
|
-
The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/
|
758
|
+
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 `webpacker.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`.
|
666
759
|
|
667
|
-
Suppose you want to change the source directory from `app/
|
760
|
+
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:
|
668
761
|
|
669
762
|
```yml
|
670
763
|
# config/webpacker.yml
|
671
|
-
source_path: frontend # packs are in frontend/
|
764
|
+
source_path: frontend # packs are the files in frontend/
|
672
765
|
public_output_path: assets/packs # outputs to => public/assets/packs
|
673
766
|
```
|
674
767
|
|
@@ -688,7 +781,7 @@ If you want to have HMR and separate link tags, set `hmr: true` and `inline_css:
|
|
688
781
|
|
689
782
|
### Additional paths
|
690
783
|
|
691
|
-
If you are adding
|
784
|
+
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/webpacker.yml`. This lets you
|
692
785
|
add additional paths that webpack should look up when resolving modules:
|
693
786
|
|
694
787
|
```yml
|
@@ -705,12 +798,14 @@ import 'images/rails.png'
|
|
705
798
|
|
706
799
|
**Note:** Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules
|
707
800
|
|
708
|
-
**Also note:** While importing assets living outside your `source_path` defined in webpacker.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,
|
801
|
+
**Also note:** While importing assets living outside your `source_path` defined in webpacker.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 `Webpacker::MtimeStrategy#latest_modified_timestamp` or `Webpacker::DigestStrategy#watched_files_digest` depending on strategy configured by `compiler_strategy` option in `webpacker.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.
|
709
802
|
|
710
803
|
|
711
804
|
## Deployment
|
712
805
|
|
713
|
-
|
806
|
+
Shakapacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker: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/webpacker.yml` (if available).
|
807
|
+
|
808
|
+
This behavior is optional & can be disabled by either setting an `WEBPACKER_PRECOMPILE` environment variable to `false`, `no`, `n`, or `f`, or by setting a `webpacker_precompile` key in your `webpacker.yml` to `false`. ([source code](./lib/webpacker/configuration.rb#L30))
|
714
809
|
|
715
810
|
When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use `yarn install --frozen-lockfile` to install NPM packages on the remote host to ensure that the installed packages match the `yarn.lock` file.
|
716
811
|
|
@@ -731,3 +826,15 @@ We encourage you to contribute to Shakapacker/Webpacker! See [CONTRIBUTING](CONT
|
|
731
826
|
## License
|
732
827
|
|
733
828
|
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
829
|
+
|
830
|
+
# Supporters
|
831
|
+
|
832
|
+
The following companies support this open source project, and ShakaCode uses their products! Justin writes React on Rails on [RubyMine](https://www.jetbrains.com/ruby/). We use [Scout](https://scoutapp.com/) to monitor the live performance of [HiChee.com](https://HiChee.com), [Rails AutoScale](https://railsautoscale.com) to scale the dynos of HiChee, and [HoneyBadger](https://www.honeybadger.io/) to monitor application errors. We love [BrowserStack](https://www.browserstack.com) to solve problems with oddball browsers.
|
833
|
+
|
834
|
+
[![RubyMine](https://user-images.githubusercontent.com/1118459/114100597-3b0e3000-9860-11eb-9b12-73beb1a184b2.png)](https://www.jetbrains.com/ruby/)
|
835
|
+
[![Scout](https://user-images.githubusercontent.com/1118459/171088197-81555b69-9ed0-4235-9acf-fcb37ecfb949.png)](https://scoutapp.com/)
|
836
|
+
[![Rails AutoScale](https://user-images.githubusercontent.com/1118459/103197530-48dc0e80-488a-11eb-8b1b-a16664b30274.png)](https://railsautoscale.com/)
|
837
|
+
[![BrowserStack](https://cloud.githubusercontent.com/assets/1118459/23203304/1261e468-f886-11e6-819e-93b1a3f17da4.png)](https://www.browserstack.com)
|
838
|
+
[![HoneyBadger](https://user-images.githubusercontent.com/1118459/114100696-63962a00-9860-11eb-8ac1-75ca02856d8e.png)](https://www.honeybadger.io/)
|
839
|
+
|
840
|
+
ShakaCode's favorite project tracking tool is [Shortcut](https://shortcut.com/). If you want to **try Shortcut and get 2 months free beyond the 14-day trial period**, click [here to use ShakaCode's referral code](http://r.clbh.se/mvfoNeH). We're participating in their awesome triple-sided referral program, which you can read about [here](https://shortcut.com/referral/). By using our [referral code](http://r.clbh.se/mvfoNeH) you'll be supporting ShakaCode and, thus, React on Rails!
|