webpacker 6.0.0.pre.2 → 6.0.0.rc.1
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 +5 -2
- data/.github/workflows/js-lint.yml +5 -2
- data/.github/workflows/rubocop.yml +1 -1
- data/.github/workflows/ruby.yml +17 -14
- data/.node-version +1 -1
- data/.rubocop.yml +106 -0
- data/CHANGELOG.md +36 -9
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +93 -90
- data/README.md +363 -107
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/deployment.md +9 -29
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +57 -25
- data/docs/v6_upgrade.md +75 -0
- data/gemfiles/Gemfile-rails-edge +1 -1
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/config/webpacker.yml +5 -7
- data/lib/install/{javascript/packs → packs/entrypoints}/application.js +4 -3
- data/lib/install/template.rb +17 -10
- data/lib/tasks/webpacker/binstubs.rake +2 -2
- data/lib/tasks/webpacker/check_node.rake +3 -0
- data/lib/tasks/webpacker/check_yarn.rake +4 -1
- data/lib/tasks/webpacker/clobber.rake +1 -1
- data/lib/tasks/webpacker/install.rake +2 -2
- data/lib/tasks/webpacker/verify_config.rake +14 -0
- data/lib/tasks/webpacker/verify_install.rake +1 -11
- data/lib/webpacker.rb +1 -1
- data/lib/webpacker/commands.rb +2 -1
- data/lib/webpacker/compiler.rb +9 -3
- data/lib/webpacker/configuration.rb +19 -8
- data/lib/webpacker/dev_server.rb +6 -0
- data/lib/webpacker/dev_server_runner.rb +7 -2
- data/lib/webpacker/env.rb +5 -1
- data/lib/webpacker/helper.rb +26 -50
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +1 -2
- data/lib/webpacker/railtie.rb +1 -2
- data/lib/webpacker/runner.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +1 -0
- data/package.json +25 -29
- data/package/__tests__/development.js +3 -2
- data/package/__tests__/env.js +8 -4
- data/package/__tests__/index.js +9 -0
- data/package/babel/preset.js +24 -14
- data/package/env.js +7 -1
- data/package/environments/__tests__/base.js +7 -7
- data/package/environments/base.js +25 -25
- data/package/environments/development.js +7 -8
- data/package/environments/production.js +28 -30
- data/package/index.js +9 -2
- data/package/inliningCss.js +7 -0
- data/package/rules/babel.js +1 -1
- data/package/rules/coffee.js +5 -5
- data/package/rules/erb.js +5 -3
- data/package/rules/file.js +5 -3
- data/package/rules/index.js +9 -17
- data/package/rules/less.js +14 -10
- data/package/rules/raw.js +5 -0
- data/package/rules/sass.js +12 -9
- data/package/rules/stylus.js +26 -0
- data/package/utils/get_style_rule.js +28 -30
- data/package/utils/helpers.js +25 -0
- data/test/configuration_test.rb +3 -3
- data/test/dev_server_runner_test.rb +13 -2
- data/test/helper_test.rb +59 -60
- data/test/manifest_test.rb +16 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +4 -4
- 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 +4 -6
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/public/packs/manifest.json +19 -5
- data/test/webpacker_test.rb +17 -0
- data/yarn.lock +1567 -1039
- metadata +24 -36
- data/docs/assets.md +0 -135
- data/docs/cloud9.md +0 -310
- data/docs/css.md +0 -303
- data/docs/docker.md +0 -68
- data/docs/engines.md +0 -213
- data/docs/env.md +0 -68
- data/docs/es6.md +0 -72
- data/docs/folder-structure.md +0 -66
- data/docs/integrations.md +0 -220
- data/docs/misc.md +0 -23
- data/docs/props.md +0 -187
- data/docs/react.md +0 -183
- data/docs/target.md +0 -22
- data/docs/testing.md +0 -147
- data/docs/typescript.md +0 -190
- data/docs/v4-upgrade.md +0 -142
- data/docs/webpack-dev-server.md +0 -94
- data/docs/webpack.md +0 -315
- data/docs/yarn.md +0 -23
- data/lib/install/examples/vue3/app.vue +0 -27
- data/lib/install/examples/vue3/hello_vue.js +0 -15
- data/lib/install/javascript/packs/application.css +0 -9
- data/package/babel/preset-react.js +0 -62
- data/package/rules/svg.js +0 -23
data/docs/typescript.md
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
# TypeScript
|
2
|
-
|
3
|
-
## Installation
|
4
|
-
|
5
|
-
1. Run the TypeScript installer
|
6
|
-
|
7
|
-
```bash
|
8
|
-
bundle exec rails webpacker:install:typescript
|
9
|
-
```
|
10
|
-
|
11
|
-
After that, a new file called `hello_typescript.ts` will be present in your `packs` directory (or rather the `source_entry_path` of your `webpacker.yml` configuration). You're now ready to write TypeScript.
|
12
|
-
|
13
|
-
## (Optional) Adding Compile-Time Type Checking
|
14
|
-
|
15
|
-
The default installation only transpiles your TypeScript code using Babel. If you would like to enable type checking as part of the Webpack compilation process (i.e. fail the build if there are TS errors), you can do the following:
|
16
|
-
|
17
|
-
1. Install the Fork TS Checker Webpack Plugin
|
18
|
-
|
19
|
-
```sh
|
20
|
-
yarn add --dev fork-ts-checker-webpack-plugin
|
21
|
-
```
|
22
|
-
|
23
|
-
2. Then add it to your development environment config in `config/webpack/development.js`
|
24
|
-
|
25
|
-
```js
|
26
|
-
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
27
|
-
const path = require("path");
|
28
|
-
|
29
|
-
environment.plugins.append(
|
30
|
-
"ForkTsCheckerWebpackPlugin",
|
31
|
-
new ForkTsCheckerWebpackPlugin({
|
32
|
-
typescript: {
|
33
|
-
configFile: path.resolve(__dirname, "../../tsconfig.json"),
|
34
|
-
},
|
35
|
-
async: false,
|
36
|
-
})
|
37
|
-
);
|
38
|
-
```
|
39
|
-
|
40
|
-
If you are using `fork-ts-checker-webpack-plugin` older than 5.0, the `tsconfig` option also needs to be specified:
|
41
|
-
|
42
|
-
```js
|
43
|
-
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
44
|
-
const path = require("path");
|
45
|
-
|
46
|
-
environment.plugins.append(
|
47
|
-
"ForkTsCheckerWebpackPlugin",
|
48
|
-
new ForkTsCheckerWebpackPlugin({
|
49
|
-
// this is a relative path to your project's TypeScript config
|
50
|
-
tsconfig: path.resolve(__dirname, "../../tsconfig.json"),
|
51
|
-
// non-async so type checking will block compilation
|
52
|
-
async: false,
|
53
|
-
})
|
54
|
-
);
|
55
|
-
```
|
56
|
-
|
57
|
-
## Upgrading to 5.1
|
58
|
-
|
59
|
-
If you update your App to `webpacker >= 5.1` and had TypeScript installed before, you need to add some new/remove some old configurations:
|
60
|
-
|
61
|
-
1. Remove old packages:
|
62
|
-
- `yarn remove ts-loader`
|
63
|
-
|
64
|
-
2. Add new packages:
|
65
|
-
- `yarn add @babel/preset-typescript`
|
66
|
-
|
67
|
-
3. Remove old configuration files:
|
68
|
-
- Delete this file: `config/webpack/loaders/typescript.js`
|
69
|
-
|
70
|
-
4. Remove the following lines from `config/webpack/environment.js`:
|
71
|
-
- `const typescript = require('./loaders/typescript')`
|
72
|
-
- `environment.loaders.prepend('typescript', typescript)`
|
73
|
-
|
74
|
-
5. Add the TypeScript preset to your `babel.config.js`:
|
75
|
-
- This line `['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]` has to be added as the last item to the `presets` array in your `babel.config.js`
|
76
|
-
|
77
|
-
### Upgrading to 5.1 for Vue users
|
78
|
-
|
79
|
-
1. Remove old packages:
|
80
|
-
- `yarn remove ts-loader pnp-webpack-plugin`
|
81
|
-
|
82
|
-
2. Follow point 3 and 4 from the `TypeScript with Vue components` section
|
83
|
-
|
84
|
-
## TypeScript with React
|
85
|
-
|
86
|
-
1. Setup react using Webpacker [react installer](integrations.md#react). Then run the TypeScript installer
|
87
|
-
|
88
|
-
```bash
|
89
|
-
bundle exec rails webpacker:install:typescript
|
90
|
-
```
|
91
|
-
|
92
|
-
2. Rename the generated `hello_react.js` to `hello_react.tsx`. Make the file valid TypeScript and
|
93
|
-
now you can use TypeScript, JSX with React.
|
94
|
-
|
95
|
-
## TypeScript with Vue components
|
96
|
-
|
97
|
-
1. Setup Vue using the Webpacker [Vue installer](../README.md#vue). Then run the TypeScript installer
|
98
|
-
|
99
|
-
```bash
|
100
|
-
bundle exec rails webpacker:install:typescript
|
101
|
-
```
|
102
|
-
|
103
|
-
2. Rename generated `hello_vue.js` to `hello_vue.ts`.
|
104
|
-
3. Install the right Babel preset: `yarn add babel-preset-typescript-vue`
|
105
|
-
4. Change the generated `babel.config.js` from
|
106
|
-
|
107
|
-
```js
|
108
|
-
["@babel/preset-typescript", { "allExtensions": true, "isTSX": true }]
|
109
|
-
```
|
110
|
-
|
111
|
-
to
|
112
|
-
|
113
|
-
```js
|
114
|
-
["babel-preset-typescript-vue", { "allExtensions": true, "isTSX": true }]
|
115
|
-
```
|
116
|
-
|
117
|
-
and now you can use `<script lang="ts">` in your `.vue` component files. See [the babel-preset-typescript-vue docs](https://www.npmjs.com/package/babel-preset-typescript-vue) for more info.
|
118
|
-
|
119
|
-
## HTML templates with TypeScript and Angular
|
120
|
-
|
121
|
-
After you have installed Angular using `bundle exec rails webpacker:install:angular`
|
122
|
-
you would need to follow these steps to add HTML templates support:
|
123
|
-
|
124
|
-
1. Use `yarn` to add html-loader
|
125
|
-
|
126
|
-
```bash
|
127
|
-
yarn add html-loader
|
128
|
-
```
|
129
|
-
|
130
|
-
2. Add html-loader to `config/webpack/environment.js`
|
131
|
-
|
132
|
-
```js
|
133
|
-
environment.loaders.append('html', {
|
134
|
-
test: /\.html$/,
|
135
|
-
use: [{
|
136
|
-
loader: 'html-loader',
|
137
|
-
options: {
|
138
|
-
minimize: true,
|
139
|
-
removeAttributeQuotes: false,
|
140
|
-
caseSensitive: true,
|
141
|
-
customAttrSurround: [ [/#/, /(?:)/], [/\*/, /(?:)/], [/\[?\(?/, /(?:)/] ],
|
142
|
-
customAttrAssign: [ /\)?\]?=/ ]
|
143
|
-
}
|
144
|
-
}]
|
145
|
-
})
|
146
|
-
```
|
147
|
-
|
148
|
-
3. Add `.html` to `config/webpacker.yml`
|
149
|
-
|
150
|
-
```yml
|
151
|
-
extensions:
|
152
|
-
- .elm
|
153
|
-
- .coffee
|
154
|
-
- .html
|
155
|
-
```
|
156
|
-
|
157
|
-
4. Setup a custom `d.ts` definition
|
158
|
-
|
159
|
-
```ts
|
160
|
-
// app/javascript/hello_angular/html.d.ts
|
161
|
-
|
162
|
-
declare module "*.html" {
|
163
|
-
const content: string
|
164
|
-
export default content
|
165
|
-
}
|
166
|
-
```
|
167
|
-
|
168
|
-
5. Add a template.html file relative to `app.component.ts`
|
169
|
-
|
170
|
-
```html
|
171
|
-
<h1>Hello {{name}}</h1>
|
172
|
-
```
|
173
|
-
|
174
|
-
6. Import template into `app.component.ts`
|
175
|
-
|
176
|
-
```ts
|
177
|
-
import { Component } from '@angular/core'
|
178
|
-
import templateString from './template.html'
|
179
|
-
|
180
|
-
@Component({
|
181
|
-
selector: 'hello-angular',
|
182
|
-
template: templateString
|
183
|
-
})
|
184
|
-
|
185
|
-
export class AppComponent {
|
186
|
-
name = 'Angular!'
|
187
|
-
}
|
188
|
-
```
|
189
|
-
|
190
|
-
That's all. Voila!
|
data/docs/v4-upgrade.md
DELETED
@@ -1,142 +0,0 @@
|
|
1
|
-
# Webpacker Upgrade Guide
|
2
|
-
|
3
|
-
|
4
|
-
To update a Webpacker v3.5 app to v4, follow these steps:
|
5
|
-
|
6
|
-
1. Update the `webpacker` gem and the `@rails/webpacker` package to v4. This will upgrade Webpack itself from 3.x to 4.x, make sure you're aware of [any deprecations which might effect you](https://webpack.js.org/migrate/4/). Also make sure any other packages you depend on support Webpack 4 and don't require any changes, e.g. if you explicitly include `webpack` you need to upgrade it to 4.x, and if you use `webpack-dev-server` you need to upgrade it to 3.x.
|
7
|
-
|
8
|
-
2. Browser support definitions have been moved from [`.browserslistrc`](../lib/install/config/.browserslistrc) to `/`.
|
9
|
-
|
10
|
-
3. Merge any differences between [`config/webpacker.yml`](../lib/install/config/webpacker.yml) and your `config/webpacker.yml`.
|
11
|
-
|
12
|
-
4. Webpacker v4 upgrades Babel to [v7](https://babeljs.io/docs/en/v7-migration), see also [the release blog post](https://babeljs.io/blog/2018/08/27/7.0.0). Many packages were moved to the `@babel/` namespace, any babel plugins you have will need to be updated. It may be worth checking out [babel-upgrade](https://github.com/babel/babel-upgrade) if you have problems. ([#1564](https://github.com/rails/webpacker/pull/1564))
|
13
|
-
|
14
|
-
5. `.babelrc` should be replaced with `babel.config.js` and `.postcssrc.yml` should be replaced with `postcss.config.js` ([#1822](https://github.com/rails/webpacker/pull/1822)). If you never changed these files from their defaults, the versions of [babel.config.js](../lib/install/config/babel.config.js) and [postcss.config.js](../lib/install/config/postcss.config.js) in the webpacker repository should be usable.
|
15
|
-
|
16
|
-
6. Due to the change in [#1625](https://github.com/rails/webpacker/pull/1625), you'll want to make sure that `extract_css` is set to true for the `default` environment in `webpacker.yml` if you want to have Webpacker supply your CSS.
|
17
|
-
|
18
|
-
## SplitChunks Configuration
|
19
|
-
|
20
|
-
If you used the `CommonsChunkPlugin` you'll need to upgrade to using the new `splitChunks`.
|
21
|
-
|
22
|
-
Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal Webpack graph. The `CommonsChunkPlugin` was used to avoid duplicated dependencies across them, but further optimizations were not possible.
|
23
|
-
|
24
|
-
In Webpack v4, the `CommonsChunkPlugin` was removed in favor of `optimization.splitChunks`.
|
25
|
-
|
26
|
-
For the full configuration options of `splitChunks`, see the [Webpack documentation](https://webpack.js.org/plugins/split-chunks-plugin/).
|
27
|
-
|
28
|
-
```js
|
29
|
-
// config/webpack/environment.js
|
30
|
-
const WebpackAssetsManifest = require('webpack-assets-manifest');
|
31
|
-
|
32
|
-
// Enable the default config
|
33
|
-
environment.splitChunks()
|
34
|
-
|
35
|
-
// or using custom config
|
36
|
-
environment.splitChunks((config) => Object.assign({}, config, { optimization: { splitChunks: false }}))
|
37
|
-
```
|
38
|
-
|
39
|
-
Then use the `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` helpers to include all the transpiled
|
40
|
-
packs with the chunks in your view, which creates html tags for all the chunks.
|
41
|
-
|
42
|
-
```erb
|
43
|
-
<%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
|
44
|
-
|
45
|
-
<!-- Creates the following: -->
|
46
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
47
|
-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
48
|
-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
49
|
-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
50
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
51
|
-
```
|
52
|
-
|
53
|
-
**Important:** Pass all your pack names to the helper otherwise you will get duplicated chunks on the page.
|
54
|
-
|
55
|
-
```erb
|
56
|
-
<%# DO %>
|
57
|
-
<%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
|
58
|
-
|
59
|
-
<%# DON'T %>
|
60
|
-
<%= javascript_packs_with_chunks_tag 'calendar' %>
|
61
|
-
<%= javascript_packs_with_chunks_tag 'map' %>
|
62
|
-
```
|
63
|
-
|
64
|
-
## Package-Specific Notes
|
65
|
-
|
66
|
-
- If you're using React, you need to add `"@babel/preset-react"`, to the list of `presets` in your babel config.
|
67
|
-
- If you're using Vue Loader, you'll need to upgrade to [v15](https://vue-loader.vuejs.org/migrating.html) for Webpack 4.
|
68
|
-
- To see what Webpacker generates for a given framework with v4, you may want to re-run `bundle exec rake webpacker:install:FRAMEWORK` and let it override the files for your given JavaScript framework, and then compare them to see what changes you'll need to make.
|
69
|
-
|
70
|
-
## Excluding node_modules From Being Transpiled By Babel-Loader
|
71
|
-
|
72
|
-
One change to take into consideration, is that Webpacker 4 transpiles the
|
73
|
-
`node_modules` folder with the `babel-loader`. This folder used to be ignored by
|
74
|
-
Webpacker 3. The new behavior helps in case some library contains ES6 code, but in
|
75
|
-
some cases it can lead to issues. To avoid running `babel-loader` in the
|
76
|
-
`node_modules` folder, replicating the same behavior as Webpacker 3, the
|
77
|
-
following code can be added to `config/webpack/environment.js`:
|
78
|
-
|
79
|
-
```javascript
|
80
|
-
environment.loaders.delete('nodeModules')
|
81
|
-
```
|
82
|
-
|
83
|
-
Alternatively, in order to skip only a specific library in the `node_modules`
|
84
|
-
folder, this code can be added:
|
85
|
-
|
86
|
-
```javascript
|
87
|
-
const nodeModulesLoader = environment.loaders.get('nodeModules')
|
88
|
-
if (!Array.isArray(nodeModulesLoader.exclude)) {
|
89
|
-
nodeModulesLoader.exclude = (nodeModulesLoader.exclude == null)
|
90
|
-
? []
|
91
|
-
: [nodeModulesLoader.exclude]
|
92
|
-
}
|
93
|
-
nodeModulesLoader.exclude.push(/some-library/) // replace `some-library` with
|
94
|
-
// the actual path to exclude
|
95
|
-
```
|
96
|
-
|
97
|
-
## Source Maps Enabled By Default
|
98
|
-
|
99
|
-
Source maps are now enabled in production to make debugging in production easier. Enabling source maps doesn't have drawbacks for most of the applications since maps are compressed by default and aren't loaded by browsers unless Dev Tools are opened.
|
100
|
-
|
101
|
-
If you want to keep the old behavior source maps can be disabled in any environment configuration, e.g:
|
102
|
-
|
103
|
-
```js
|
104
|
-
// config/webpack/production.js
|
105
|
-
|
106
|
-
const environment = require('./environment')
|
107
|
-
environment.config.merge({ devtool: false })
|
108
|
-
|
109
|
-
module.exports = environment.toWebpackConfig()
|
110
|
-
```
|
111
|
-
|
112
|
-
## Namespaces
|
113
|
-
|
114
|
-
The compiled packs in the public directory are now stored under namespaces:
|
115
|
-
|
116
|
-
- JavaScripts are stored under `js`
|
117
|
-
- Stylesheets are stored under `css`
|
118
|
-
- Other resources are stored under `media`
|
119
|
-
|
120
|
-
```rb
|
121
|
-
# Before
|
122
|
-
"runtime~hello_react" => "/packs/runtime~hello_react-da2baf7fd07b0e8b6d17.js"
|
123
|
-
|
124
|
-
# After
|
125
|
-
"runtime~hello_react" => "/packs/js/runtime~hello_react-da2baf7fd07b0e8b6d17.js"
|
126
|
-
```
|
127
|
-
|
128
|
-
## Upgrading projects with custom Webpack setups that use only the view helpers
|
129
|
-
The default value for `extract_css` is **false** in `config/webpack.yml`. Custom webpack builds that extract the CSS such as often used with [React on Rails](https://github.com/shakacode/react_on_rails) should set this value to true or else no CSS link tags are generated.
|
130
|
-
|
131
|
-
```yml
|
132
|
-
default: &default
|
133
|
-
# other stuff
|
134
|
-
extract_css: true
|
135
|
-
# by default, extract and emit a css file. The default is false
|
136
|
-
```
|
137
|
-
|
138
|
-
## Example Upgrades
|
139
|
-
|
140
|
-
This is what an upgrade to Webpacker 4 looked like for existing Rails apps (please contribute yours!):
|
141
|
-
|
142
|
-
- https://github.com/connorshea/ContinueFromCheckpoint/pull/77
|
data/docs/webpack-dev-server.md
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
# webpack-dev-server
|
2
|
-
|
3
|
-
## HTTPS
|
4
|
-
|
5
|
-
If you're using the `webpack-dev-server` in development, you can serve your packs over HTTPS
|
6
|
-
by setting the `https` option for `webpack-dev-server` to `true` in `config/webpacker.yml`,
|
7
|
-
then start the dev server as usual with `./bin/webpack-dev-server`.
|
8
|
-
|
9
|
-
Please note that the `webpack-dev-server` will use a self-signed certificate,
|
10
|
-
so your web browser will display a warning/exception upon accessing the page. If you get
|
11
|
-
`https://localhost:3035/sockjs-node/info?t=1503127986584 net::ERR_INSECURE_RESPONSE`
|
12
|
-
in your console, simply open the link in your browser and accept the SSL exception.
|
13
|
-
Now if you refresh your Rails view everything should work as expected.
|
14
|
-
|
15
|
-
## Hot Module Replacement
|
16
|
-
|
17
|
-
Webpacker out-of-the-box supports HMR with `webpack-dev-server` and
|
18
|
-
you can toggle it by setting options in `config/webpacker.yml`:
|
19
|
-
|
20
|
-
```yaml
|
21
|
-
development:
|
22
|
-
# ...
|
23
|
-
dev_server:
|
24
|
-
# ...
|
25
|
-
hmr: true
|
26
|
-
inline: true
|
27
|
-
# ...
|
28
|
-
```
|
29
|
-
|
30
|
-
`dev_server/hmr` option inside `webpacker.yml`.
|
31
|
-
|
32
|
-
Check out this guide for more information:
|
33
|
-
|
34
|
-
- https://webpack.js.org/configuration/dev-server/#devserver-hot
|
35
|
-
|
36
|
-
To support HMR with React, see [docs/react.md](./react.md#hmr-and-react-hot-reloading).
|
37
|
-
|
38
|
-
## Nginx
|
39
|
-
|
40
|
-
If you use Nginx in development to proxy requests to your Rails server from
|
41
|
-
another domain, like `myapp.dev`, the Webpacker middleware will be able to
|
42
|
-
forward requests for "packs" to the webpack dev server.
|
43
|
-
|
44
|
-
If you're using `inline` mode behind Nginx, you may also need to provide the
|
45
|
-
hostname to webpack dev server so it can initiate the websocket connection for
|
46
|
-
live reloading ([Webpack
|
47
|
-
docs](https://webpack.js.org/configuration/dev-server/#devserver-public)).
|
48
|
-
|
49
|
-
To do so, set the `public` option in `config/webpacker.yml`:
|
50
|
-
|
51
|
-
```yaml
|
52
|
-
development:
|
53
|
-
# ...
|
54
|
-
dev_server:
|
55
|
-
# ...
|
56
|
-
public: myapp.dev
|
57
|
-
```
|
58
|
-
|
59
|
-
You may also need to add the following location block to your local Nginx server
|
60
|
-
configuration for your Rails app.
|
61
|
-
|
62
|
-
```
|
63
|
-
server {
|
64
|
-
listen 80;
|
65
|
-
server_name myapp.dev
|
66
|
-
|
67
|
-
# Proxy webpack dev server websocket requests
|
68
|
-
location /sockjs-node {
|
69
|
-
proxy_redirect off;
|
70
|
-
proxy_http_version 1.1;
|
71
|
-
proxy_set_header Upgrade $http_upgrade;
|
72
|
-
proxy_set_header Connection "upgrade";
|
73
|
-
proxy_pass http://127.0.0.1:3035; # change to match your webpack-dev-server host
|
74
|
-
}
|
75
|
-
|
76
|
-
# ...
|
77
|
-
}
|
78
|
-
```
|
79
|
-
|
80
|
-
## Customizing Logging
|
81
|
-
|
82
|
-
By default, the dev server will display a colored progress notification while
|
83
|
-
your code is being compiled. (Under the hood, we are using `webpack-dev-server --progress --color`). However, this might cause issues if you don't use
|
84
|
-
`foreman` and/or try to log webpack-dev-server's output to a file. You can
|
85
|
-
disable this stylized output by adding `pretty: false` to your `dev_server`
|
86
|
-
config:
|
87
|
-
|
88
|
-
```yaml
|
89
|
-
development:
|
90
|
-
# ...
|
91
|
-
dev_server:
|
92
|
-
# ...
|
93
|
-
pretty: false
|
94
|
-
```
|
data/docs/webpack.md
DELETED
@@ -1,315 +0,0 @@
|
|
1
|
-
# webpack
|
2
|
-
|
3
|
-
|
4
|
-
## Configuration
|
5
|
-
|
6
|
-
Webpacker gives you a default set of configuration files for test, development and
|
7
|
-
production environments in `config/webpack/*.js`. You can configure each individual
|
8
|
-
environment in their respective files or configure them all in the base
|
9
|
-
`config/webpack/environment.js` file.
|
10
|
-
|
11
|
-
By default, you don't need to make any changes to `config/webpack/*.js`
|
12
|
-
files since it's all standard production-ready configuration. However,
|
13
|
-
if you do need to customize or add a new loader, this is where you would go.
|
14
|
-
|
15
|
-
Here is how you can modify webpack configuration:
|
16
|
-
|
17
|
-
You might add separate files to keep your code more organized.
|
18
|
-
```js
|
19
|
-
// config/webpack/custom.js
|
20
|
-
module.exports = {
|
21
|
-
resolve: {
|
22
|
-
alias: {
|
23
|
-
jquery: 'jquery/src/jquery',
|
24
|
-
vue: 'vue/dist/vue.js',
|
25
|
-
React: 'react',
|
26
|
-
ReactDOM: 'react-dom',
|
27
|
-
vue_resource: 'vue-resource/dist/vue-resource',
|
28
|
-
}
|
29
|
-
}
|
30
|
-
}
|
31
|
-
```
|
32
|
-
|
33
|
-
Then `require` this file in your `config/webpack/environment.js`:
|
34
|
-
|
35
|
-
```js
|
36
|
-
// config/webpack/environment.js
|
37
|
-
const { environment } = require('@rails/webpacker')
|
38
|
-
const customConfig = require('./custom')
|
39
|
-
|
40
|
-
// Set nested object prop using path notation
|
41
|
-
environment.config.set('resolve.extensions', ['.foo', '.bar'])
|
42
|
-
environment.config.set('output.filename', '[name].js')
|
43
|
-
|
44
|
-
// Merge custom config
|
45
|
-
environment.config.merge(customConfig)
|
46
|
-
|
47
|
-
// Merge other options
|
48
|
-
environment.config.merge({ devtool: 'none' })
|
49
|
-
|
50
|
-
// Delete a property
|
51
|
-
environment.config.delete('output.chunkFilename')
|
52
|
-
|
53
|
-
module.exports = environment
|
54
|
-
```
|
55
|
-
|
56
|
-
If you need access to configs within Webpacker's configuration,
|
57
|
-
you can import them like so:
|
58
|
-
|
59
|
-
```js
|
60
|
-
const { config } = require('@rails/webpacker')
|
61
|
-
|
62
|
-
console.log(config.output_path)
|
63
|
-
console.log(config.source_path)
|
64
|
-
```
|
65
|
-
|
66
|
-
## Loaders
|
67
|
-
|
68
|
-
You can add additional loaders beyond the base set that Webpacker provides by
|
69
|
-
adding it to your environment. We'll use `url-loader` as an example:
|
70
|
-
|
71
|
-
```
|
72
|
-
yarn add url-loader
|
73
|
-
```
|
74
|
-
|
75
|
-
```js
|
76
|
-
// config/webpack/environment.js
|
77
|
-
const { environment } = require('@rails/webpacker')
|
78
|
-
|
79
|
-
const urlLoader = {
|
80
|
-
test: /\.png$/,
|
81
|
-
use: 'url-loader'
|
82
|
-
}
|
83
|
-
|
84
|
-
// Insert url loader at the end of list
|
85
|
-
environment.loaders.append('url', urlLoader)
|
86
|
-
|
87
|
-
// Insert url loader at the top of list
|
88
|
-
environment.loaders.prepend('url', urlLoader)
|
89
|
-
|
90
|
-
// Insert url loader after/before a given loader
|
91
|
-
environment.loaders.insert('url', urlLoader, { after: 'style'} )
|
92
|
-
environment.loaders.insert('url', urlLoader, { before: 'babel'} )
|
93
|
-
|
94
|
-
module.exports = environment
|
95
|
-
```
|
96
|
-
|
97
|
-
Finally, add `.png` to the list of extensions in `config/webpacker.yml`. Now if you `import()` any `.png` files inside your JavaScript
|
98
|
-
they will be processed using `url-loader`. Voila!
|
99
|
-
|
100
|
-
You can also modify the loaders that Webpacker pre-configures for you. We'll update
|
101
|
-
the `babel` loader as an example:
|
102
|
-
|
103
|
-
```js
|
104
|
-
// config/webpack/environment.js
|
105
|
-
const { environment } = require('@rails/webpacker')
|
106
|
-
|
107
|
-
const babelLoader = environment.loaders.get('babel')
|
108
|
-
babelLoader.options.cacheDirectory = false
|
109
|
-
|
110
|
-
module.exports = environment
|
111
|
-
```
|
112
|
-
|
113
|
-
### Coffeescript 2
|
114
|
-
|
115
|
-
Out of the box webpacker supports coffeescript 1,
|
116
|
-
but here is how you can use Coffeescript 2:
|
117
|
-
|
118
|
-
```
|
119
|
-
yarn add coffeescript@2.0.1
|
120
|
-
```
|
121
|
-
|
122
|
-
```js
|
123
|
-
// config/webpack/environment.js
|
124
|
-
const { environment } = require('@rails/webpacker')
|
125
|
-
|
126
|
-
const babelLoader = environment.loaders.get('babel')
|
127
|
-
|
128
|
-
// Replace existing coffee loader with CS2 version
|
129
|
-
environment.loaders.insert('coffee', {
|
130
|
-
test: /\.coffee(\.erb)?$/,
|
131
|
-
use: babelLoader.use.concat(['coffee-loader'])
|
132
|
-
})
|
133
|
-
|
134
|
-
module.exports = environment
|
135
|
-
```
|
136
|
-
|
137
|
-
### React SVG loader
|
138
|
-
|
139
|
-
To use react svg loader, you should append svg loader before file loader:
|
140
|
-
|
141
|
-
```js
|
142
|
-
// config/webpack/environment.js
|
143
|
-
const { environment } = require('@rails/webpacker')
|
144
|
-
|
145
|
-
const babelLoader = environment.loaders.get('babel')
|
146
|
-
|
147
|
-
environment.loaders.insert('svg', {
|
148
|
-
test: /\.svg$/,
|
149
|
-
use: babelLoader.use.concat([
|
150
|
-
{
|
151
|
-
loader: 'react-svg-loader',
|
152
|
-
options: {
|
153
|
-
jsx: true // true outputs JSX tags
|
154
|
-
}
|
155
|
-
}
|
156
|
-
])
|
157
|
-
}, { before: 'file' })
|
158
|
-
|
159
|
-
const fileLoader = environment.loaders.get('file')
|
160
|
-
fileLoader.exclude = /\.(svg)$/i
|
161
|
-
```
|
162
|
-
|
163
|
-
|
164
|
-
### Url Loader
|
165
|
-
|
166
|
-
Be sure to add the default options from the file loader, as those are applied with the file loader if the size is greater than the `limit`.
|
167
|
-
|
168
|
-
```js
|
169
|
-
// config/webpack/environment.js
|
170
|
-
|
171
|
-
const { environment } = require('@rails/webpacker');
|
172
|
-
const rules = environment.loaders;
|
173
|
-
|
174
|
-
const urlFileSizeCutover = 10000;
|
175
|
-
const urlLoaderOptions = Object.assign({ limit: urlFileSizeCutover }, fileLoader.use[0].options);
|
176
|
-
const urlLoader = {
|
177
|
-
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
|
178
|
-
use: {
|
179
|
-
loader: 'url-loader',
|
180
|
-
options: urlLoaderOptions,
|
181
|
-
},
|
182
|
-
};
|
183
|
-
|
184
|
-
environment.loaders.prepend('url', urlLoader)
|
185
|
-
|
186
|
-
// avoid using both file and url loaders
|
187
|
-
// Note, this list should take into account the config value for static_assets_extensions
|
188
|
-
environment.loaders.get('file').test = /\.(tiff|ico|svg|eot|otf|ttf|woff|woff2)$/i
|
189
|
-
```
|
190
|
-
|
191
|
-
## Plugins
|
192
|
-
|
193
|
-
The process for adding or modifying webpack plugins is the same as the process
|
194
|
-
for loaders above:
|
195
|
-
|
196
|
-
```js
|
197
|
-
// config/webpack/environment.js
|
198
|
-
const { environment } = require('@rails/webpacker')
|
199
|
-
const webpack = require('webpack')
|
200
|
-
|
201
|
-
// Get a pre-configured plugin
|
202
|
-
const manifestPlugin = environment.plugins.get('Manifest')
|
203
|
-
manifestPlugin.options.writeToFileEmit = false
|
204
|
-
|
205
|
-
// Add an additional plugin of your choosing : ProvidePlugin
|
206
|
-
environment.plugins.prepend(
|
207
|
-
'Provide',
|
208
|
-
new webpack.ProvidePlugin({
|
209
|
-
$: 'jquery',
|
210
|
-
jQuery: 'jquery',
|
211
|
-
jquery: 'jquery',
|
212
|
-
'window.Tether': 'tether',
|
213
|
-
Popper: ['popper.js', 'default'],
|
214
|
-
ActionCable: 'actioncable',
|
215
|
-
Vue: 'vue',
|
216
|
-
VueResource: 'vue-resource',
|
217
|
-
})
|
218
|
-
)
|
219
|
-
|
220
|
-
module.exports = environment
|
221
|
-
```
|
222
|
-
|
223
|
-
## Resolved modules
|
224
|
-
|
225
|
-
To add new paths to `resolve.modules`, the API is same as loaders and plugins:
|
226
|
-
|
227
|
-
```js
|
228
|
-
const { environment } = require('@rails/webpacker')
|
229
|
-
|
230
|
-
// Resolved modules list API - prepend, append, insert
|
231
|
-
environment.resolvedModules.append('vendor', 'vendor')
|
232
|
-
```
|
233
|
-
|
234
|
-
### Add SplitChunks (Webpack V4+)
|
235
|
-
Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal webpack graph. The CommonsChunkPlugin was used to avoid duplicated dependencies across them, but further optimizations were not possible.
|
236
|
-
|
237
|
-
Since webpack v4, the CommonsChunkPlugin was removed in favor of optimization.splitChunks.
|
238
|
-
|
239
|
-
For the full configuration options of SplitChunks, see the [Webpack documentation](https://webpack.js.org/plugins/split-chunks-plugin/).
|
240
|
-
|
241
|
-
```js
|
242
|
-
// config/webpack/environment.js
|
243
|
-
|
244
|
-
// Enable the default config
|
245
|
-
environment.splitChunks()
|
246
|
-
|
247
|
-
// or using custom config
|
248
|
-
environment.splitChunks((config) => Object.assign({}, config, { optimization: { splitChunks: false }}))
|
249
|
-
```
|
250
|
-
|
251
|
-
Then use the `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` helpers to include all the transpiled
|
252
|
-
packs with the chunks in your view, which creates html tags for all the chunks.
|
253
|
-
|
254
|
-
```erb
|
255
|
-
<%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
|
256
|
-
|
257
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
258
|
-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
259
|
-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
260
|
-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
261
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
262
|
-
```
|
263
|
-
|
264
|
-
**Important:** Pass all your pack names when using this helper otherwise you will
|
265
|
-
get duplicated chunks on the page.
|
266
|
-
|
267
|
-
```erb
|
268
|
-
<%# DO %>
|
269
|
-
<%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
|
270
|
-
|
271
|
-
<%# DON'T %>
|
272
|
-
<%= javascript_packs_with_chunks_tag 'calendar' %>
|
273
|
-
<%= javascript_packs_with_chunks_tag 'map' %>
|
274
|
-
```
|
275
|
-
|
276
|
-
#### Preloading
|
277
|
-
|
278
|
-
Before preload or prefetch your assets, please read [https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content](https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content).
|
279
|
-
|
280
|
-
Webpack also provide it's own methods for preload or prefetch [https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c](https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c).
|
281
|
-
|
282
|
-
You can preload your assets with the `preload_pack_asset` helper if you have Rails >= 5.2.x.
|
283
|
-
|
284
|
-
```erb
|
285
|
-
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
286
|
-
```
|
287
|
-
|
288
|
-
**Warning:** You don't want to preload the css, you want to preload the fonts and images inside the css so that fonts, css, and images can all be downloaded in parallel instead of waiting for the browser to parse the css.
|
289
|
-
|
290
|
-
More detailed guides available here: [webpack guides](https://webpack.js.org/guides/)
|
291
|
-
|
292
|
-
## Webpack Multi-Compiler and Server-Side Rendering
|
293
|
-
You can export an Array of Object to have both `bin/webpack` and `bin/webpack-dev-server`
|
294
|
-
use multiple configurations. This is commonly done for React server-side rendering (SSR).
|
295
|
-
|
296
|
-
For an example of this, see the configuration within the [`/config/webpack` dir of the React on Rails Example](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/webpack).
|
297
|
-
|
298
|
-
Take special care in that you need to make a deep copy of the output from the the basic "client" configuration.
|
299
|
-
|
300
|
-
In the example below, you _cannot_ modify the clientConfigObject as that would mutate the "environment" that is global.
|
301
|
-
|
302
|
-
```js
|
303
|
-
const environment = require('./environment');
|
304
|
-
|
305
|
-
// make a deep copy
|
306
|
-
const clientConfigObject = environment.toWebpackConfig();
|
307
|
-
const serverWebpackConfig = merge({}, clientConfigObject);
|
308
|
-
|
309
|
-
// make whatever changes you want for the serverWebpackConfig
|
310
|
-
|
311
|
-
// No splitting of chunks for a server bundle
|
312
|
-
serverWebpackConfig.optimization = {
|
313
|
-
minimize: false,
|
314
|
-
};
|
315
|
-
```
|