webpacker 5.4.1 → 6.0.0.rc.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/jest.yml +5 -2
  4. data/.github/workflows/js-lint.yml +5 -2
  5. data/.github/workflows/rubocop.yml +1 -1
  6. data/.github/workflows/ruby.yml +19 -4
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +4 -3
  10. data/CHANGELOG.md +24 -6
  11. data/CONTRIBUTING.md +1 -1
  12. data/Gemfile.lock +82 -82
  13. data/README.md +377 -172
  14. data/config/README.md +3 -0
  15. data/config/webpacker.yml +1 -0
  16. data/docs/deployment.md +9 -11
  17. data/docs/developing_webpacker.md +29 -0
  18. data/docs/troubleshooting.md +57 -25
  19. data/docs/v6_upgrade.md +75 -0
  20. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  21. data/lib/install/bin/yarn +18 -0
  22. data/lib/install/config/webpack/base.js +3 -0
  23. data/lib/install/config/webpack/development.js +2 -2
  24. data/lib/install/config/webpack/production.js +2 -2
  25. data/lib/install/config/webpack/test.js +2 -2
  26. data/lib/install/config/webpacker.yml +20 -50
  27. data/lib/install/package.json +17 -0
  28. data/lib/install/{javascript/packs → packs/entrypoints}/application.js +4 -3
  29. data/lib/install/template.rb +54 -28
  30. data/lib/tasks/webpacker/binstubs.rake +6 -4
  31. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  32. data/lib/tasks/webpacker/check_node.rake +3 -0
  33. data/lib/tasks/webpacker/check_yarn.rake +3 -0
  34. data/lib/tasks/webpacker/clobber.rake +1 -1
  35. data/lib/tasks/webpacker/compile.rake +4 -2
  36. data/lib/tasks/webpacker/info.rake +12 -10
  37. data/lib/tasks/webpacker/install.rake +6 -4
  38. data/lib/tasks/webpacker/verify_config.rake +14 -0
  39. data/lib/tasks/webpacker/verify_install.rake +1 -10
  40. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  41. data/lib/tasks/webpacker.rake +2 -11
  42. data/lib/tasks/yarn.rake +36 -0
  43. data/lib/webpacker/commands.rb +21 -16
  44. data/lib/webpacker/compiler.rb +16 -9
  45. data/lib/webpacker/configuration.rb +8 -32
  46. data/lib/webpacker/dev_server.rb +6 -0
  47. data/lib/webpacker/dev_server_runner.rb +28 -4
  48. data/lib/webpacker/helper.rb +47 -81
  49. data/lib/webpacker/instance.rb +4 -0
  50. data/lib/webpacker/manifest.rb +2 -3
  51. data/lib/webpacker/railtie.rb +1 -2
  52. data/lib/webpacker/runner.rb +1 -1
  53. data/lib/webpacker/version.rb +1 -1
  54. data/lib/webpacker/webpack_runner.rb +6 -0
  55. data/lib/webpacker.rb +1 -1
  56. data/package/__tests__/config.js +5 -37
  57. data/package/__tests__/development.js +14 -15
  58. data/package/__tests__/env.js +16 -4
  59. data/package/__tests__/index.js +9 -0
  60. data/package/__tests__/production.js +6 -6
  61. data/package/__tests__/staging.js +7 -6
  62. data/package/__tests__/test.js +4 -5
  63. data/package/babel/preset.js +54 -0
  64. data/package/config.js +6 -14
  65. data/package/env.js +13 -4
  66. data/package/environments/__tests__/base.js +20 -52
  67. data/package/environments/base.js +68 -128
  68. data/package/environments/development.js +50 -44
  69. data/package/environments/production.js +66 -64
  70. data/package/environments/test.js +2 -2
  71. data/package/index.js +15 -8
  72. data/package/inliningCss.js +7 -0
  73. data/package/rules/babel.js +10 -8
  74. data/package/rules/coffee.js +6 -0
  75. data/package/rules/erb.js +15 -0
  76. data/package/rules/file.js +21 -19
  77. data/package/rules/index.js +16 -18
  78. data/package/rules/less.js +22 -0
  79. data/package/rules/raw.js +5 -0
  80. data/package/rules/sass.js +9 -10
  81. data/package/rules/stylus.js +26 -0
  82. data/package/utils/get_style_rule.js +28 -36
  83. data/package/utils/helpers.js +28 -35
  84. data/package.json +12 -32
  85. data/test/command_test.rb +76 -0
  86. data/test/compiler_test.rb +0 -12
  87. data/test/configuration_test.rb +4 -35
  88. data/test/dev_server_runner_test.rb +36 -6
  89. data/test/engine_rake_tasks_test.rb +39 -0
  90. data/test/helper_test.rb +79 -77
  91. data/test/manifest_test.rb +16 -0
  92. data/test/mounted_app/Rakefile +4 -0
  93. data/test/mounted_app/test/dummy/Rakefile +3 -0
  94. data/test/mounted_app/test/dummy/bin/rails +3 -0
  95. data/test/mounted_app/test/dummy/bin/rake +3 -0
  96. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  97. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  98. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  99. data/test/mounted_app/test/dummy/config.ru +5 -0
  100. data/test/mounted_app/test/dummy/package.json +7 -0
  101. data/test/rake_tasks_test.rb +1 -10
  102. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  103. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  104. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
  105. data/test/test_app/config/webpacker.yml +5 -31
  106. data/test/test_app/config/webpacker_other_location.yml +79 -0
  107. data/test/test_app/config/webpacker_public_root.yml +0 -1
  108. data/test/test_app/public/packs/manifest.json +36 -18
  109. data/test/test_app/some.config.js +0 -0
  110. data/test/webpack_runner_test.rb +9 -3
  111. data/test/webpacker_test.rb +17 -0
  112. data/webpacker.gemspec +1 -1
  113. data/yarn.lock +1050 -3966
  114. metadata +59 -91
  115. data/.travis.yml +0 -43
  116. data/docs/assets.md +0 -119
  117. data/docs/cloud9.md +0 -310
  118. data/docs/css.md +0 -308
  119. data/docs/docker.md +0 -68
  120. data/docs/engines.md +0 -213
  121. data/docs/env.md +0 -63
  122. data/docs/es6.md +0 -72
  123. data/docs/folder-structure.md +0 -66
  124. data/docs/integrations.md +0 -220
  125. data/docs/misc.md +0 -23
  126. data/docs/props.md +0 -223
  127. data/docs/target.md +0 -22
  128. data/docs/testing.md +0 -136
  129. data/docs/typescript.md +0 -190
  130. data/docs/v4-upgrade.md +0 -142
  131. data/docs/webpack-dev-server.md +0 -92
  132. data/docs/webpack.md +0 -364
  133. data/docs/yarn.md +0 -23
  134. data/lib/install/angular.rb +0 -23
  135. data/lib/install/coffee.rb +0 -25
  136. data/lib/install/config/.browserslistrc +0 -1
  137. data/lib/install/config/babel.config.js +0 -76
  138. data/lib/install/config/postcss.config.js +0 -12
  139. data/lib/install/config/webpack/environment.js +0 -3
  140. data/lib/install/elm.rb +0 -39
  141. data/lib/install/erb.rb +0 -25
  142. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  143. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  144. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  145. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  146. data/lib/install/examples/angular/hello_angular.js +0 -7
  147. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  148. data/lib/install/examples/elm/Main.elm +0 -55
  149. data/lib/install/examples/elm/hello_elm.js +0 -16
  150. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  151. data/lib/install/examples/react/babel.config.js +0 -93
  152. data/lib/install/examples/react/hello_react.jsx +0 -26
  153. data/lib/install/examples/react/tsconfig.json +0 -21
  154. data/lib/install/examples/stimulus/application.js +0 -1
  155. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  156. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  157. data/lib/install/examples/svelte/app.svelte +0 -11
  158. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  159. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  160. data/lib/install/examples/typescript/tsconfig.json +0 -24
  161. data/lib/install/examples/vue/app.vue +0 -22
  162. data/lib/install/examples/vue/hello_vue.js +0 -72
  163. data/lib/install/loaders/coffee.js +0 -6
  164. data/lib/install/loaders/elm.js +0 -25
  165. data/lib/install/loaders/erb.js +0 -11
  166. data/lib/install/loaders/svelte.js +0 -9
  167. data/lib/install/loaders/vue.js +0 -6
  168. data/lib/install/react.rb +0 -18
  169. data/lib/install/stimulus.rb +0 -12
  170. data/lib/install/svelte.rb +0 -29
  171. data/lib/install/typescript.rb +0 -39
  172. data/lib/install/vue.rb +0 -49
  173. data/lib/tasks/installers.rake +0 -42
  174. data/package/config_types/__tests__/config_list.js +0 -118
  175. data/package/config_types/__tests__/config_object.js +0 -43
  176. data/package/config_types/config_list.js +0 -75
  177. data/package/config_types/config_object.js +0 -55
  178. data/package/config_types/index.js +0 -7
  179. data/package/rules/module.css.js +0 -3
  180. data/package/rules/module.sass.js +0 -8
  181. data/package/rules/node_modules.js +0 -22
  182. data/package/utils/__tests__/deep_assign.js +0 -32
  183. data/package/utils/__tests__/deep_merge.js +0 -10
  184. data/package/utils/__tests__/get_style_rule.js +0 -65
  185. data/package/utils/__tests__/objectify.js +0 -9
  186. data/package/utils/deep_assign.js +0 -22
  187. data/package/utils/deep_merge.js +0 -22
  188. data/package/utils/objectify.js +0 -3
data/docs/target.md DELETED
@@ -1,22 +0,0 @@
1
- # Target browsers
2
-
3
- By default webpacker provides these front-end tools:
4
- - [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env)
5
- - [Autoprefixer](https://github.com/postcss/autoprefixer)
6
- - [postcss-preset-env](https://github.com/csstools/postcss-preset-env)
7
-
8
- All these tools use [Browserslist](https://github.com/browserslist/browserslist) to detect which environment your users have
9
-
10
- Webpacker browserslist default target:
11
- ```
12
- defaults
13
- ```
14
-
15
- `defaults`: `(> 0.5%, last 2 versions, Firefox ESR, not dead)`, [browserl.ist](https://browserl.ist/) is an online tool to check what browsers will be selected by some query.
16
-
17
- To keep browsers data up to date, you need to run:
18
- ```bash
19
- yarn upgrade caniuse-lite
20
- ```
21
-
22
- at least once every few months, to prevent such [problems](https://github.com/browserslist/browserslist/issues/492)
data/docs/testing.md DELETED
@@ -1,136 +0,0 @@
1
- # Testing
2
-
3
- ## Karma setup for Typescript
4
-
5
- Webpacker does not setup `Karma` by default, so you've to manually install it along with its dependencies as per your need. Following things marked as optional can be used to fancify the test results (Recommended).
6
-
7
- ```js
8
- // package.json
9
- "scripts": {
10
- "test": "NODE_ENV=test karma start"
11
- },
12
- "dependencies": {
13
- "typescript": "^2.5.2"
14
- },
15
- "devDependencies": {
16
- "karma": "^1.7.1",
17
- "karma-webpack": "^2.0.4",
18
- "karma-chrome-launcher": "^2.2.0",
19
- "karma-jquery": "^0.2.2",
20
- "karma-jasmine": "^1.1.0",
21
- "karma-jasmine-jquery": "^0.1.1",
22
- "jasmine-core": "^2.8.0",
23
- [optional] "karma-coverage": "^1.1.1",
24
- [optional] "karma-coverage-istanbul-reporter": "^1.3.0",
25
- [optional] "karma-spec-reporter": "0.0.31",
26
- [optional] "istanbul-instrumenter-loader": "^3.0.0",
27
- }
28
- ```
29
-
30
- It is beneficial to use the same webpack configuration file (generated by webpacker) in Karma configuration to avoid redundancy. Following line tells Karma not to write transpiled source files onto filesystem while testing to avoid `Error: EACCES: permission denied, mkdir '/_karma_webpack_' ` error. Then inject a new rule a.k.a. loader in the existing ones (needed only if you have installed `istanbul-instrumenter-loader`) to generate a coverage report under `/coverage` directory.
31
-
32
- ```js
33
- // config/webpack/test.js
34
- const environment = require('./environment')
35
- environment.plugins.get('Manifest').options.writeToFileEmit = process.env.NODE_ENV !== 'test'
36
- environment.loaders.append('istanbul-instrumenter', {
37
- test: /\.ts$/,
38
- enforce: "post",
39
- loader: "istanbul-instrumenter-loader",
40
- query: {
41
- esModules: true
42
- },
43
- exclude: ["node_modules", /\.test\.ts$/]
44
- }) /* optional */
45
- module.exports = environment.toWebpackConfig()
46
- ```
47
-
48
- Finally, update `karma.conf.js` to read the same `test.js` file mentioned above. Rest of the things are mandatory (few marked as optional wherever appropriate).
49
-
50
- ```js
51
- // karma.conf.js
52
- const webpackConfig = require('./config/webpack/test.js')
53
- module.exports = function(config) {
54
- config.set({
55
- basePath: "",
56
- frameworks: ["jquery-3.2.1", "jasmine-jquery", "jasmine"],
57
- plugins: [
58
- "karma-jquery",
59
- "karma-jasmine-jquery",
60
- "karma-jasmine",
61
- "karma-webpack",
62
- "karma-chrome-launcher",
63
- "karma-coverage-istanbul-reporter" /* optional */,
64
- "karma-spec-reporter" /* optional */
65
- ],
66
- files: [ "/* add spec files */" ],
67
- exclude: [],
68
- webpack: webpackConfig,
69
- preprocessors: {"/* add spec files */" : ["webpack"]},
70
- mime: { "text/x-typescript": ["ts"] },
71
- reporters: ["progress", "coverage-istanbul" /* optional */],
72
- coverageIstanbulReporter: {
73
- reports: [ 'html', 'lcovonly', 'text-summary' ],
74
- fixWebpackSourcePaths: true
75
- } /* optional */,
76
- port: 9876,
77
- colors: true,
78
- logLevel: config.LOG_INFO,
79
- autoWatch: true,
80
- browsers: ["Chrome"],
81
- singleRun: true
82
- });
83
- };
84
- ```
85
-
86
- ## Lazy compilation
87
-
88
- Webpacker lazily compiles assets in test env so you can write your tests without any extra
89
- setup and everything will just work out of the box.
90
-
91
- Here is a sample system test case with hello_react example component:
92
-
93
- ```js
94
- // Example React component
95
-
96
- import React from 'react'
97
- import ReactDOM from 'react-dom'
98
- import PropTypes from 'prop-types'
99
-
100
- const Hello = props => (
101
- <div>Hello David</div>
102
- )
103
-
104
- document.addEventListener('DOMContentLoaded', () => {
105
- ReactDOM.render(
106
- <Hello />,
107
- document.body.appendChild(document.createElement('div')),
108
- )
109
- })
110
- ```
111
-
112
- ```erb
113
- <%# views/pages/home.html.erb %>
114
-
115
- <%= javascript_pack_tag "hello_react" %>
116
- ```
117
-
118
- ```rb
119
- # Tests example React component
120
- require "application_system_test_case"
121
- class HomesTest < ApplicationSystemTestCase
122
- test "can see the hello message" do
123
- visit root_url
124
- assert_selector "h5", text: "Hello! David"
125
- end
126
- end
127
- ```
128
-
129
- ## Capybara setup for Rails
130
- Make sure you configure Rails to serve static files from the public directory in the test environment.
131
-
132
- ```rb
133
- # config/environments/test.rb
134
- # Configure public file server for tests with Cache-Control for performance.
135
- config.public_file_server.enabled = true
136
- ```
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 `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](../README.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