shakapacker 6.3.0.pre.rc.1 → 6.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -5
- data/Gemfile +2 -1
- data/Gemfile.lock +94 -84
- data/README.md +152 -60
- data/docs/react.md +262 -0
- data/docs/sprockets.md +10 -0
- data/lib/install/config/webpacker.yml +12 -0
- data/lib/tasks/webpacker/yarn_install.rake +6 -0
- data/lib/tasks/webpacker.rake +0 -1
- data/lib/tasks/yarn.rake +6 -0
- data/lib/webpacker/base_strategy.rb +24 -0
- data/lib/webpacker/compiler.rb +6 -51
- data/lib/webpacker/compiler_strategy.rb +20 -0
- data/lib/webpacker/configuration.rb +8 -1
- data/lib/webpacker/digest_strategy.rb +59 -0
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/mtime_strategy.rb +40 -0
- data/lib/webpacker/version.rb +1 -1
- data/package/environments/__tests__/base.js +30 -3
- data/package/environments/base.js +8 -1
- data/package/rules/erb.js +7 -1
- data/package/rules/file.js +2 -2
- data/package.json +4 -4
- data/test/compiler_strategy_test.rb +27 -0
- data/test/compiler_test.rb +27 -30
- data/test/digest_strategy_test.rb +33 -0
- 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
- metadata +21 -5
data/README.md
CHANGED
@@ -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
|
159
|
+
|
160
|
+
You will need your file system to correspond to the setup of your `webpacker.yml` file.
|
160
161
|
|
161
|
-
|
162
|
+
Suppose you have the following configuration:
|
162
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,61 +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
|
+
```
|
220
265
|
|
266
|
+
And the main layout has:
|
221
267
|
```erb
|
222
|
-
<% append_javascript_pack_tag 'calendar', 'map' %>
|
223
268
|
<%= javascript_pack_tag 'application' %>
|
224
269
|
```
|
225
270
|
|
226
|
-
|
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 %>
|
227
290
|
|
228
|
-
|
291
|
+
<%= content_for :footer %>
|
292
|
+
```
|
229
293
|
|
230
294
|
For alternative options of setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
|
231
295
|
|
296
|
+
#### View Helper: `asset_pack_path`
|
297
|
+
|
232
298
|
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
233
299
|
```erb
|
234
300
|
<img src="<%= asset_pack_path 'static/logo.svg' %>" />
|
235
301
|
```
|
236
302
|
|
303
|
+
#### View Helper: `image_pack_tag`
|
304
|
+
|
237
305
|
Or use the dedicated helper:
|
238
306
|
```erb
|
239
307
|
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
240
308
|
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
241
309
|
```
|
242
310
|
|
311
|
+
#### View Helper: `favicon_pack_tag`
|
243
312
|
If you want to create a favicon:
|
244
313
|
```erb
|
245
314
|
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
246
315
|
```
|
247
316
|
|
317
|
+
#### View Helper: `preload_pack_asset`
|
248
318
|
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
249
319
|
```erb
|
250
320
|
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
251
321
|
```
|
252
322
|
|
323
|
+
|
324
|
+
### Images in Stylesheets
|
253
325
|
If you want to use images in your stylesheets:
|
254
326
|
|
255
327
|
```css
|
@@ -257,24 +329,39 @@ If you want to use images in your stylesheets:
|
|
257
329
|
background-image: url('../images/logo.svg')
|
258
330
|
}
|
259
331
|
```
|
260
|
-
### Defer for `javascript_pack_tag`
|
261
|
-
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`.
|
262
332
|
|
263
333
|
### Server-Side Rendering (SSR)
|
264
334
|
|
265
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.
|
266
336
|
|
267
|
-
**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.
|
268
|
-
|
269
337
|
### Development
|
270
338
|
|
271
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.
|
272
340
|
|
273
|
-
|
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.
|
274
344
|
|
275
|
-
|
345
|
+
The `compile: true` option can be more useful for test and production builds.
|
276
346
|
|
277
|
-
|
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
|
278
365
|
|
279
366
|
```bash
|
280
367
|
# webpack dev server
|
@@ -285,15 +372,6 @@ If you want to use live code reloading, or you have enough JavaScript that on-de
|
|
285
372
|
|
286
373
|
# standalone build
|
287
374
|
./bin/webpacker --progress
|
288
|
-
|
289
|
-
# Help
|
290
|
-
./bin/webpacker help
|
291
|
-
|
292
|
-
# Version
|
293
|
-
./bin/webpacker version
|
294
|
-
|
295
|
-
# Info
|
296
|
-
./bin/webpacker info
|
297
375
|
```
|
298
376
|
|
299
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`
|
@@ -326,7 +404,7 @@ end
|
|
326
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:
|
327
405
|
|
328
406
|
1. Your output files go the right directory
|
329
|
-
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!
|
330
408
|
|
331
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`:
|
332
410
|
|
@@ -349,7 +427,7 @@ module.exports = merge({}, baseWebpackConfig, options)
|
|
349
427
|
|
350
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)
|
351
429
|
|
352
|
-
|
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.
|
353
431
|
|
354
432
|
You might add separate files to keep your code more organized.
|
355
433
|
|
@@ -379,7 +457,7 @@ const customConfig = require('./custom')
|
|
379
457
|
module.exports = merge(webpackConfig, customConfig)
|
380
458
|
```
|
381
459
|
|
382
|
-
If you need access to configs within
|
460
|
+
If you need access to configs within Shakapacker's configuration, you can import them like so:
|
383
461
|
|
384
462
|
```js
|
385
463
|
// config/webpack/webpack.config.js
|
@@ -401,7 +479,7 @@ svgRule.test = svgRule.test.filter(t => !t.test('.svg'))
|
|
401
479
|
|
402
480
|
### Babel configuration
|
403
481
|
|
404
|
-
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`.
|
405
483
|
|
406
484
|
```json
|
407
485
|
"babel": {
|
@@ -410,9 +488,9 @@ By default, you will find the Webpacker preset in your `package.json`. Note, you
|
|
410
488
|
]
|
411
489
|
},
|
412
490
|
```
|
413
|
-
|
414
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).
|
415
492
|
|
493
|
+
|
416
494
|
### SWC configuration
|
417
495
|
|
418
496
|
You can try out experimental integration with the SWC loader. You can read more at [SWC usage docs](./docs/using_swc_loader.md).
|
@@ -427,11 +505,13 @@ Please note that if you want opt-in to use esbuild-loader, you can skip [React](
|
|
427
505
|
|
428
506
|
### Integrations
|
429
507
|
|
430
|
-
|
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:
|
431
509
|
|
432
510
|
#### React
|
433
511
|
|
434
|
-
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.
|
435
515
|
|
436
516
|
#### Typescript
|
437
517
|
...if you are using typescript, update your `tsconfig.json`
|
@@ -602,7 +682,7 @@ module.exports = merge(vueConfig, webpackConfig)
|
|
602
682
|
|
603
683
|
### Custom Rails environments
|
604
684
|
|
605
|
-
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.
|
606
686
|
|
607
687
|
You can choose to define additional environment configurations in webpacker.yml,
|
608
688
|
|
@@ -620,7 +700,7 @@ staging:
|
|
620
700
|
public_output_path: packs-staging
|
621
701
|
```
|
622
702
|
|
623
|
-
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`.
|
624
704
|
|
625
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:
|
626
706
|
|
@@ -648,7 +728,7 @@ bundle exec rails webpacker:compile
|
|
648
728
|
|
649
729
|
### Upgrading
|
650
730
|
|
651
|
-
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:
|
652
732
|
|
653
733
|
```bash
|
654
734
|
# check your Gemfile for version restrictions
|
@@ -673,7 +753,7 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
673
753
|
|
674
754
|
### Paths
|
675
755
|
|
676
|
-
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.
|
677
757
|
|
678
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`.
|
679
759
|
|
@@ -701,7 +781,7 @@ If you want to have HMR and separate link tags, set `hmr: true` and `inline_css:
|
|
701
781
|
|
702
782
|
### Additional paths
|
703
783
|
|
704
|
-
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
|
705
785
|
add additional paths that webpack should look up when resolving modules:
|
706
786
|
|
707
787
|
```yml
|
@@ -718,12 +798,12 @@ import 'images/rails.png'
|
|
718
798
|
|
719
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
|
720
800
|
|
721
|
-
**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.
|
722
802
|
|
723
803
|
|
724
804
|
## Deployment
|
725
805
|
|
726
|
-
|
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).
|
727
807
|
|
728
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))
|
729
809
|
|
@@ -746,3 +826,15 @@ We encourage you to contribute to Shakapacker/Webpacker! See [CONTRIBUTING](CONT
|
|
746
826
|
## License
|
747
827
|
|
748
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!
|