webpacker 6.0.0.beta.2 → 6.0.0.beta.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/jest.yml +1 -1
- data/.github/workflows/js-lint.yml +1 -1
- data/.github/workflows/ruby.yml +9 -6
- data/.rubocop.yml +105 -0
- data/CHANGELOG.md +6 -4
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +93 -90
- data/README.md +133 -58
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/deployment.md +128 -0
- data/docs/troubleshooting.md +160 -0
- data/docs/v6_upgrade.md +86 -0
- data/lib/install/config/webpacker.yml +5 -3
- data/lib/install/{javascript/packs → packs/entrypoints}/application.js +1 -1
- data/lib/install/template.rb +16 -9
- data/lib/tasks/webpacker/binstubs.rake +2 -2
- data/lib/tasks/webpacker/check_node.rake +1 -0
- data/lib/tasks/webpacker/check_yarn.rake +1 -0
- data/lib/tasks/webpacker/install.rake +2 -2
- data/lib/webpacker/commands.rb +2 -1
- data/lib/webpacker/compiler.rb +2 -2
- data/lib/webpacker/configuration.rb +4 -4
- data/lib/webpacker/dev_server_runner.rb +2 -0
- data/lib/webpacker/helper.rb +13 -43
- data/lib/webpacker/manifest.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +1 -0
- data/package.json +1 -1
- data/package/__tests__/development.js +2 -1
- data/package/__tests__/index.js +9 -0
- data/package/environments/__tests__/base.js +4 -4
- data/package/environments/base.js +3 -8
- data/package/environments/development.js +1 -0
- data/package/environments/production.js +1 -1
- data/package/index.js +3 -3
- data/package/rules/babel.js +1 -1
- data/package/rules/stylus.js +1 -1
- data/package/utils/helpers.js +4 -2
- data/test/configuration_test.rb +2 -2
- data/test/dev_server_runner_test.rb +10 -2
- data/test/helper_test.rb +33 -39
- data/test/manifest_test.rb +8 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +3 -3
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
- data/test/test_app/config/webpacker.yml +3 -3
- data/test/test_app/public/packs/manifest.json +7 -0
- metadata +18 -14
- data/6_0_upgrade.md +0 -43
- data/lib/install/javascript/packs/application.css +0 -9
data/README.md
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
[![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://rubygems.org/gems/webpacker)
|
10
10
|
|
11
11
|
Webpacker makes it easy to use the JavaScript pre-processor and bundler
|
12
|
-
[
|
12
|
+
[Webpack v5](https://webpack.js.org/)
|
13
13
|
to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
|
14
14
|
as the primary purpose for webpack is app-like JavaScript, not images, CSS, or
|
15
15
|
even JavaScript Sprinkles (that all continues to live in app/assets).
|
@@ -36,7 +36,6 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
36
36
|
- [Resolved](#resolved)
|
37
37
|
- [Watched](#watched)
|
38
38
|
- [Deployment](#deployment)
|
39
|
-
- [Docs](#docs)
|
40
39
|
- [Contributing](#contributing)
|
41
40
|
- [License](#license)
|
42
41
|
|
@@ -47,11 +46,11 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
47
46
|
- Ruby 2.4+
|
48
47
|
- Rails 5.2+
|
49
48
|
- Node.js 10.22.1+ || 12+ || 14+
|
50
|
-
- Yarn 1.x+
|
49
|
+
- Yarn 1.x+ || 2.x+
|
51
50
|
|
52
51
|
## Features
|
53
52
|
|
54
|
-
- [
|
53
|
+
- [Webpack v5](https://webpack.js.org/)
|
55
54
|
- ES6 with [babel](https://babeljs.io/)
|
56
55
|
- Automatic code splitting using multiple entry points
|
57
56
|
- Asset compression, source-maps, and minification
|
@@ -59,13 +58,13 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
59
58
|
- Rails view helpers
|
60
59
|
- Extensible and configurable
|
61
60
|
|
62
|
-
|
61
|
+
### Optional support
|
63
62
|
|
64
63
|
_requires extra packages to be installed_
|
65
64
|
|
66
|
-
- Stylesheets -
|
67
|
-
-
|
68
|
-
-
|
65
|
+
- Stylesheets - Sass, Less, Stylus and Css, PostCSS
|
66
|
+
- CoffeeScript
|
67
|
+
- TypeScript
|
69
68
|
- React
|
70
69
|
|
71
70
|
## Installation
|
@@ -116,9 +115,9 @@ yarn install
|
|
116
115
|
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
117
116
|
|
118
117
|
```yml
|
119
|
-
app/
|
120
|
-
├──
|
121
|
-
│ #
|
118
|
+
app/packs:
|
119
|
+
├── entrypoints:
|
120
|
+
│ # Only Webpack entry files here
|
122
121
|
│ └── application.js
|
123
122
|
│ └── application.css
|
124
123
|
└── src:
|
@@ -129,23 +128,50 @@ app/javascript:
|
|
129
128
|
└── logo.svg
|
130
129
|
```
|
131
130
|
|
132
|
-
You can then link the JavaScript pack in Rails views using the `
|
131
|
+
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`:
|
133
132
|
|
134
133
|
```erb
|
135
|
-
<%=
|
136
|
-
<%=
|
134
|
+
<%= javascript_pack_tag 'application' %>
|
135
|
+
<%= stylesheet_pack_tag 'application' %>
|
137
136
|
```
|
138
137
|
|
139
|
-
If you want to link a static asset for `<
|
140
|
-
can use the `asset_pack_path` helper:
|
141
|
-
|
138
|
+
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
142
139
|
```erb
|
143
|
-
<link rel="prefetch" href="<%= asset_pack_path 'application.css' %>" />
|
144
140
|
<img src="<%= asset_pack_path 'images/logo.svg' %>" />
|
145
141
|
```
|
146
142
|
|
143
|
+
Or use the dedicated helper:
|
144
|
+
```erb
|
145
|
+
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
146
|
+
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
147
|
+
```
|
148
|
+
|
149
|
+
If you want to create a favicon:
|
150
|
+
```erb
|
151
|
+
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
152
|
+
```
|
153
|
+
|
154
|
+
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
155
|
+
```erb
|
156
|
+
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
157
|
+
```
|
158
|
+
|
159
|
+
If you want to use images in your stylesheets:
|
160
|
+
|
161
|
+
```css
|
162
|
+
.foo {
|
163
|
+
background-image: url('../images/logo.svg')
|
164
|
+
}
|
165
|
+
```
|
166
|
+
|
167
|
+
Note, if you are using server-side rendering of JavaScript with dynamic code-spliting,
|
168
|
+
as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails)
|
169
|
+
your JavaScript should create the link prefetch HTML tags that you will use, so you won't
|
170
|
+
need to use to `asset_pack_path` in those circumstances.
|
171
|
+
|
147
172
|
**Note:** In order for your styles or static assets files to be available in your view,
|
148
|
-
you would need to link them in your "pack" or entry file.
|
173
|
+
you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know
|
174
|
+
to package up those files.
|
149
175
|
|
150
176
|
### Development
|
151
177
|
|
@@ -157,11 +183,20 @@ are loaded based on your environment.
|
|
157
183
|
In development, Webpacker compiles on demand rather than upfront by default. This
|
158
184
|
happens when you refer to any of the pack assets using the Webpacker helper methods.
|
159
185
|
This means that you don't have to run any separate processes. Compilation errors are logged
|
160
|
-
to the standard Rails log.
|
186
|
+
to the standard Rails log. However, this auto-compilation happens when a web request
|
187
|
+
is made that requires an updated webpack build, not when files change. Thus, that can
|
188
|
+
be painfully slow for front-end development in this default way. Instead, you should either
|
189
|
+
run the `bin/webpack --watch` or run `./bin/webpack-dev-server`
|
190
|
+
|
191
|
+
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/webpack-dev-server` or `ruby ./bin/webpack-dev-server`.
|
192
|
+
Windows users will need to run these commands in a terminal separate from `bundle exec rails s`.
|
193
|
+
This process will watch for changes in the `app/packs/entrypoints/*.js` files and automatically
|
194
|
+
reload the browser to match. This feature is also known as
|
195
|
+
[Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
161
196
|
|
162
|
-
|
163
|
-
|
164
|
-
|
197
|
+
HMR is only the first step to running "Fast Refresh" with React. For more information
|
198
|
+
on how to configure rails/webpacker for Fast Refresh with React, see article
|
199
|
+
[HMR and React Hot Reloading](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md#hmr-and-react-hot-reloading).
|
165
200
|
|
166
201
|
```bash
|
167
202
|
# webpack dev server
|
@@ -174,9 +209,10 @@ in the `app/javascript/packs/*.js` files and automatically reload the browser to
|
|
174
209
|
./bin/webpack
|
175
210
|
```
|
176
211
|
|
177
|
-
Once you start this development server, Webpacker will automatically start proxying all
|
178
|
-
webpack asset requests to this server. When you stop
|
179
|
-
on-demand compilation
|
212
|
+
Once you start this webpack development server, Webpacker will automatically start proxying all
|
213
|
+
webpack asset requests to this server. When you stop this server, Rails will detect
|
214
|
+
that it's not running and Rails will revert back to on-demand compilation _if_ you have
|
215
|
+
the `compile` option set to true in your `config/webpacker.yml`
|
180
216
|
|
181
217
|
You can use environment variables as options supported by
|
182
218
|
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
|
@@ -212,7 +248,7 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
212
248
|
Webpacker gives you a default set of configuration files for test, development and
|
213
249
|
production environments in `config/webpack/*.js`. You can configure each individual
|
214
250
|
environment in their respective files or configure them all in the base
|
215
|
-
`config/webpack/
|
251
|
+
`config/webpack/base.js` file.
|
216
252
|
|
217
253
|
By default, you don't need to make any changes to `config/webpack/*.js`
|
218
254
|
files since it's all standard production-ready configuration. However,
|
@@ -256,23 +292,26 @@ const { webpackConfig } = require('@rails/webpacker')
|
|
256
292
|
|
257
293
|
console.log(webpackConfig.output_path)
|
258
294
|
console.log(webpackConfig.source_path)
|
295
|
+
|
296
|
+
// Or to print out your whole webpack configuration
|
297
|
+
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
259
298
|
```
|
260
299
|
|
261
300
|
### Integrations
|
262
301
|
|
263
302
|
Webpacker out of the box supports JS and static assets (fonts, images etc.)
|
264
|
-
compilation. To enable support for
|
265
|
-
relevant packages
|
303
|
+
compilation. To enable support for CoffeeScript or TypeScript install
|
304
|
+
relevant packages:
|
266
305
|
|
267
|
-
|
306
|
+
#### CoffeeScript
|
268
307
|
|
269
|
-
```
|
308
|
+
```bash
|
270
309
|
yarn add coffeescript coffee-loader
|
271
310
|
```
|
272
311
|
|
273
|
-
|
312
|
+
#### TypeScript
|
274
313
|
|
275
|
-
```
|
314
|
+
```bash
|
276
315
|
yarn add typescript @babel/preset-typescript
|
277
316
|
```
|
278
317
|
|
@@ -289,7 +328,7 @@ Add tsconfig.json
|
|
289
328
|
"moduleResolution": "node",
|
290
329
|
"baseUrl": ".",
|
291
330
|
"paths": {
|
292
|
-
"*": ["node_modules/*", "app/
|
331
|
+
"*": ["node_modules/*", "app/packs/*"]
|
293
332
|
},
|
294
333
|
"sourceMap": true,
|
295
334
|
"target": "es5",
|
@@ -300,15 +339,33 @@ Add tsconfig.json
|
|
300
339
|
}
|
301
340
|
```
|
302
341
|
|
303
|
-
|
342
|
+
Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
|
343
|
+
|
344
|
+
```bash
|
345
|
+
yarn add fork-ts-checker-webpack-plugin
|
346
|
+
```
|
347
|
+
|
348
|
+
Then modify the webpack config to use it as a plugin:
|
304
349
|
|
305
|
-
|
350
|
+
```js
|
351
|
+
// config/webpack/base.js
|
352
|
+
const { webpackConfig, merge } = require("@rails/webpacker");
|
353
|
+
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
306
354
|
|
355
|
+
module.exports = merge(webpackConfig, {
|
356
|
+
plugins: [new ForkTSCheckerWebpackPlugin()],
|
357
|
+
});
|
307
358
|
```
|
359
|
+
|
360
|
+
#### CSS
|
361
|
+
|
362
|
+
To enable CSS support in your application, add following packages:
|
363
|
+
|
364
|
+
```bash
|
308
365
|
yarn add css-loader mini-css-extract-plugin css-minimizer-webpack-plugin
|
309
366
|
```
|
310
367
|
|
311
|
-
optionally, add css extension to webpack config for easy resolution
|
368
|
+
optionally, add the css extension to webpack config for easy resolution.
|
312
369
|
|
313
370
|
```js
|
314
371
|
// config/webpack/base.js
|
@@ -327,25 +384,25 @@ then add the relevant pre-processors:
|
|
327
384
|
|
328
385
|
#### Postcss
|
329
386
|
|
330
|
-
```
|
387
|
+
```bash
|
331
388
|
yarn add postcss-loader
|
332
389
|
```
|
333
390
|
|
334
391
|
#### Sass
|
335
392
|
|
336
|
-
```
|
393
|
+
```bash
|
337
394
|
yarn add sass sass-loader
|
338
395
|
```
|
339
396
|
|
340
397
|
#### Less
|
341
398
|
|
342
|
-
```
|
399
|
+
```bash
|
343
400
|
yarn add less less-loader
|
344
401
|
```
|
345
402
|
|
346
|
-
####
|
403
|
+
#### Stylus
|
347
404
|
|
348
|
-
```
|
405
|
+
```bash
|
349
406
|
yarn add stylus stylus-loader
|
350
407
|
```
|
351
408
|
|
@@ -353,7 +410,7 @@ yarn add stylus stylus-loader
|
|
353
410
|
|
354
411
|
React is supported and you just need to add relevant packages,
|
355
412
|
|
356
|
-
```
|
413
|
+
```bash
|
357
414
|
yarn add react react-dom @babel/preset-react
|
358
415
|
```
|
359
416
|
|
@@ -378,16 +435,19 @@ if you are using typescript, update your `tsconfig.json`
|
|
378
435
|
}
|
379
436
|
```
|
380
437
|
|
438
|
+
For more information on React props hydration and Server-Side Rendering (SSR), see the article
|
439
|
+
[Rails/Webpacker React Integration Options](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md)
|
440
|
+
in the [ShakaCode/react_on_rails](https://github.com/shakacode/react_on_rails) repo.
|
441
|
+
|
381
442
|
#### Other frameworks
|
382
443
|
|
383
444
|
Please follow webpack integration guide for relevant framework or library,
|
384
445
|
|
385
|
-
1. Svelte
|
386
|
-
2. Angular
|
387
|
-
3. Vue
|
388
|
-
|
389
|
-
For example to add Vue support,
|
446
|
+
1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
|
447
|
+
2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
|
448
|
+
3. [Vue](https://vue-loader.vuejs.org/guide/)
|
390
449
|
|
450
|
+
For example to add Vue support:
|
391
451
|
```js
|
392
452
|
// config/webpack/rules/vue.js
|
393
453
|
const VueLoaderPlugin = require('vue-loader/lib/plugin')
|
@@ -403,7 +463,9 @@ module.exports = {
|
|
403
463
|
},
|
404
464
|
plugins: [new VueLoaderPlugin()]
|
405
465
|
}
|
466
|
+
```
|
406
467
|
|
468
|
+
```js
|
407
469
|
// config/webpack/base.js
|
408
470
|
const { webpackConfig, merge } = require('@rails/webpacker')
|
409
471
|
const vueConfig = require('./rules/vue')
|
@@ -451,11 +513,11 @@ Please note, binstubs compiles in development mode however rake tasks
|
|
451
513
|
compiles in production mode.
|
452
514
|
|
453
515
|
```bash
|
454
|
-
# Compiles in development mode unless NODE_ENV is specified
|
516
|
+
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
455
517
|
./bin/webpack
|
456
518
|
./bin/webpack-dev-server
|
457
519
|
|
458
|
-
#
|
520
|
+
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
|
459
521
|
bundle exec rails assets:precompile
|
460
522
|
bundle exec rails webpacker:compile
|
461
523
|
```
|
@@ -465,8 +527,12 @@ bundle exec rails webpacker:compile
|
|
465
527
|
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
466
528
|
|
467
529
|
```bash
|
530
|
+
# check your Gemfile for version restrictions
|
468
531
|
bundle update webpacker
|
532
|
+
|
533
|
+
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
469
534
|
rails webpacker:install
|
535
|
+
|
470
536
|
yarn upgrade @rails/webpacker --latest
|
471
537
|
yarn upgrade webpack-dev-server --latest
|
472
538
|
|
@@ -474,6 +540,8 @@ yarn upgrade webpack-dev-server --latest
|
|
474
540
|
yarn add @rails/webpacker@next
|
475
541
|
```
|
476
542
|
|
543
|
+
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
544
|
+
|
477
545
|
## Paths
|
478
546
|
|
479
547
|
By default, Webpacker ships with simple conventions for where the JavaScript
|
@@ -481,19 +549,18 @@ app files and compiled webpack bundles will go in your Rails app.
|
|
481
549
|
All these options are configurable from `config/webpacker.yml` file.
|
482
550
|
|
483
551
|
The configuration for what webpack is supposed to compile by default rests
|
484
|
-
on the convention that every file in `app/
|
552
|
+
on the convention that every file in `app/packs/entrypoints/*`**(default)**
|
485
553
|
or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
|
486
554
|
is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside `packs` directory that you do not want to be
|
487
555
|
an entry file. As a rule of thumb, put all files you want to link in your views inside
|
488
|
-
"packs" directory and keep everything else under `app/
|
556
|
+
"packs" directory and keep everything else under `app/packs`.
|
489
557
|
|
490
|
-
Suppose you want to change the source directory from `app/
|
558
|
+
Suppose you want to change the source directory from `app/packs`
|
491
559
|
to `frontend` and output to `assets/packs`. This is how you would do it:
|
492
560
|
|
493
561
|
```yml
|
494
562
|
# config/webpacker.yml
|
495
|
-
source_path: frontend
|
496
|
-
source_entry_path: packs
|
563
|
+
source_path: frontend # packs are in frontend/packs
|
497
564
|
public_output_path: assets/packs # outputs to => public/assets/packs
|
498
565
|
```
|
499
566
|
|
@@ -507,7 +574,9 @@ development:
|
|
507
574
|
port: 3035
|
508
575
|
```
|
509
576
|
|
510
|
-
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output,
|
577
|
+
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output,
|
578
|
+
as you will want to configure your styles to be inlined in your JavaScript for hot reloading.
|
579
|
+
During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
|
511
580
|
|
512
581
|
### Additional paths
|
513
582
|
|
@@ -515,10 +584,10 @@ If you are adding Webpacker to an existing app that has most of the assets insid
|
|
515
584
|
`app/assets` or inside an engine, and you want to share that
|
516
585
|
with webpack modules, you can use the `additional_paths`
|
517
586
|
option available in `config/webpacker.yml`. This lets you
|
518
|
-
add additional paths that webpack should
|
587
|
+
add additional paths that webpack should look up when resolving modules:
|
519
588
|
|
520
589
|
```yml
|
521
|
-
additional_paths: ['app/assets
|
590
|
+
additional_paths: ['app/assets', 'vendor/assets']
|
522
591
|
```
|
523
592
|
|
524
593
|
You can then import these items inside your modules like so:
|
@@ -539,6 +608,12 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
|
|
539
608
|
|
540
609
|
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.
|
541
610
|
|
611
|
+
If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
|
612
|
+
|
613
|
+
## Troubleshooting
|
614
|
+
|
615
|
+
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
616
|
+
|
542
617
|
## Contributing
|
543
618
|
|
544
619
|
[![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
|
data/config/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
config/lib/install/config/webpacker.yml
|
data/docs/deployment.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# Deployment
|
2
|
+
|
3
|
+
Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`.
|
4
|
+
If you are not using Sprockets `webpacker:compile` is automatically aliased to `assets:precompile`. Remember to set `NODE_ENV` environment variable to production during deployment or when running the rake task.
|
5
|
+
|
6
|
+
The `javascript_pack_tag` and `stylesheet_pack_tag` helper method will automatically insert the correct HTML tag for compiled pack. Just like the asset pipeline does it.
|
7
|
+
|
8
|
+
By default the output will look like this in different environments:
|
9
|
+
|
10
|
+
```html
|
11
|
+
<!-- In development mode with webpack-dev-server -->
|
12
|
+
<script src="http://localhost:8080/calendar-0bd141f6d9360cf4a7f5.js"></script>
|
13
|
+
<link rel="stylesheet" media="screen" href="http://localhost:8080/calendar-dc02976b5f94b507e3b6.css">
|
14
|
+
|
15
|
+
<!-- In production or development mode -->
|
16
|
+
<script src="/packs/js/calendar-0bd141f6d9360cf4a7f5.js"></script>
|
17
|
+
<link rel="stylesheet" media="screen" href="/packs/css/calendar-dc02976b5f94b507e3b6.css">
|
18
|
+
```
|
19
|
+
|
20
|
+
## Heroku
|
21
|
+
|
22
|
+
In order for your Webpacker app to run on Heroku, you'll need to do a bit of configuration before hand.
|
23
|
+
|
24
|
+
```bash
|
25
|
+
heroku create my-webpacker-heroku-app
|
26
|
+
heroku addons:create heroku-postgresql:hobby-dev
|
27
|
+
heroku buildpacks:add heroku/nodejs
|
28
|
+
heroku buildpacks:add heroku/ruby
|
29
|
+
git push heroku master
|
30
|
+
```
|
31
|
+
|
32
|
+
We're essentially doing the following here:
|
33
|
+
|
34
|
+
* Creating an app on Heroku
|
35
|
+
* Creating a Postgres database for the app (this is assuming that you're using Heroku Postgres for your app)
|
36
|
+
* Adding the Heroku NodeJS and Ruby buildpacks for your app. This allows the `npm` or `yarn` executables to properly function when compiling your app - as well as Ruby.
|
37
|
+
* Pushing our code to Heroku and kicking off the deployment
|
38
|
+
|
39
|
+
## Nginx
|
40
|
+
|
41
|
+
Webpacker doesn't serve anything in production. You’re expected to configure your web server to serve files in public/ directly.
|
42
|
+
|
43
|
+
Some servers support sending precompressed versions of files when they're available. For example, nginx offers a `gzip_static` directive that serves files with the `.gz` extension to supported clients. With an optional module, nginx can also serve Brotli compressed files with the `.br` extension (see below for installation and configuration instructions).
|
44
|
+
|
45
|
+
Here's a sample nginx site config for a Rails app using Webpacker:
|
46
|
+
|
47
|
+
```nginx
|
48
|
+
upstream app {
|
49
|
+
# server unix:///path/to/app/tmp/puma.sock;
|
50
|
+
}
|
51
|
+
|
52
|
+
server {
|
53
|
+
listen 80;
|
54
|
+
server_name www.example.com;
|
55
|
+
root /path/to/app/public;
|
56
|
+
|
57
|
+
location @app {
|
58
|
+
proxy_pass http://app;
|
59
|
+
proxy_redirect off;
|
60
|
+
|
61
|
+
proxy_set_header Host $host;
|
62
|
+
proxy_set_header X-Real-IP $remote_addr;
|
63
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
64
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
65
|
+
}
|
66
|
+
|
67
|
+
location / {
|
68
|
+
try_files $uri @app;
|
69
|
+
}
|
70
|
+
|
71
|
+
location = /favicon.ico { access_log off; log_not_found off; }
|
72
|
+
location = /robots.txt { access_log off; log_not_found off; }
|
73
|
+
|
74
|
+
location ~ /\.(?!well-known).* {
|
75
|
+
deny all;
|
76
|
+
}
|
77
|
+
|
78
|
+
location ~ ^/(assets|packs)/ {
|
79
|
+
gzip_static on;
|
80
|
+
brotli_static on; # Optional, see below
|
81
|
+
expires max;
|
82
|
+
add_header Cache-Control public;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
### Installing the ngx_brotli module
|
88
|
+
|
89
|
+
If you want to serve Brotli compressed files with nginx, you will need to install the `nginx_brotli` module. Installation instructions from source can be found in the official [google/ngx_brotli](https://github.com/google/ngx_brotli) git repository. Alternatively, depending on your platform, the module might be available via a pre-compiled package.
|
90
|
+
|
91
|
+
Once installed, you need to load the module. As we want to serve the pre-compressed files, we only need the static module. Add the following line to your `nginx.conf` file and reload nginx:
|
92
|
+
|
93
|
+
```
|
94
|
+
load_module modules/ngx_http_brotli_static_module.so;
|
95
|
+
```
|
96
|
+
|
97
|
+
Now, you can set `brotli_static on;` in your nginx site config, as per the config in the last section above.
|
98
|
+
|
99
|
+
## CDN
|
100
|
+
|
101
|
+
Webpacker out-of-the-box provides CDN support using your Rails app `config.action_controller.asset_host` setting. If you already have [CDN](http://guides.rubyonrails.org/asset_pipeline.html#cdns) added in your Rails app
|
102
|
+
you don't need to do anything extra for Webpacker, it just works.
|
103
|
+
|
104
|
+
## Capistrano
|
105
|
+
|
106
|
+
### Assets compiling on every deployment even if JavaScript and CSS files are not changed
|
107
|
+
|
108
|
+
Make sure you have `public/packs` and `node_modules` in `:linked_dirs`
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/packs", ".bundle", "node_modules"
|
112
|
+
```
|
113
|
+
|
114
|
+
If you have `node_modules` added to `:linked_dirs` you'll need to run yarn install before `deploy:assets:precompile`, so you can add this code snippet at the bottom deploy.rb
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
before "deploy:assets:precompile", "deploy:yarn_install"
|
118
|
+
namespace :deploy do
|
119
|
+
desc "Run rake yarn install"
|
120
|
+
task :yarn_install do
|
121
|
+
on roles(:web) do
|
122
|
+
within release_path do
|
123
|
+
execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|