webpacker 6.0.0.beta.7 → 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/jest.yml +7 -15
  3. data/.github/workflows/js-lint.yml +7 -15
  4. data/.github/workflows/rubocop.yml +1 -1
  5. data/.github/workflows/ruby.yml +15 -40
  6. data/.node-version +1 -1
  7. data/.rubocop.yml +2 -1
  8. data/CHANGELOG.md +34 -11
  9. data/CONTRIBUTING.md +19 -0
  10. data/Gemfile.lock +84 -84
  11. data/README.md +177 -165
  12. data/config/webpacker.yml +1 -1
  13. data/docs/developing_webpacker.md +29 -0
  14. data/docs/troubleshooting.md +57 -23
  15. data/docs/v6_upgrade.md +69 -42
  16. data/gemfiles/Gemfile-rails-edge +1 -1
  17. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  18. data/lib/install/{packs/entrypoints/application.js → application.js} +3 -8
  19. data/lib/install/bin/webpack +4 -7
  20. data/lib/install/bin/yarn +18 -0
  21. data/lib/install/config/webpacker.yml +18 -19
  22. data/lib/install/package.json +15 -0
  23. data/lib/install/template.rb +38 -16
  24. data/lib/tasks/webpacker/check_node.rake +3 -1
  25. data/lib/tasks/webpacker/check_yarn.rake +4 -2
  26. data/lib/tasks/webpacker/clobber.rake +1 -1
  27. data/lib/tasks/webpacker/verify_config.rake +14 -0
  28. data/lib/tasks/webpacker/verify_install.rake +1 -11
  29. data/lib/tasks/yarn.rake +38 -0
  30. data/lib/webpacker/commands.rb +19 -15
  31. data/lib/webpacker/configuration.rb +15 -4
  32. data/lib/webpacker/dev_server.rb +6 -0
  33. data/lib/webpacker/dev_server_runner.rb +6 -3
  34. data/lib/webpacker/env.rb +5 -1
  35. data/lib/webpacker/helper.rb +14 -8
  36. data/lib/webpacker/instance.rb +4 -0
  37. data/lib/webpacker/manifest.rb +1 -2
  38. data/lib/webpacker/railtie.rb +8 -2
  39. data/lib/webpacker/runner.rb +1 -1
  40. data/lib/webpacker/version.rb +1 -1
  41. data/lib/webpacker/webpack_runner.rb +27 -7
  42. data/lib/webpacker.rb +1 -1
  43. data/package/__tests__/development.js +4 -11
  44. data/package/__tests__/env.js +8 -4
  45. data/package/babel/preset.js +0 -1
  46. data/package/config.js +3 -3
  47. data/package/env.js +6 -3
  48. data/package/environments/__tests__/base.js +3 -3
  49. data/package/environments/base.js +13 -8
  50. data/package/environments/development.js +36 -36
  51. data/package/index.js +2 -0
  52. data/package/inliningCss.js +7 -0
  53. data/package/rules/file.js +1 -1
  54. data/package/rules/sass.js +1 -2
  55. data/package/utils/get_style_rule.js +4 -2
  56. data/package.json +25 -29
  57. data/test/command_test.rb +76 -0
  58. data/test/configuration_test.rb +1 -1
  59. data/test/dev_server_runner_test.rb +5 -2
  60. data/test/helper_test.rb +48 -34
  61. data/test/manifest_test.rb +10 -2
  62. data/test/mounted_app/test/dummy/config/webpacker.yml +1 -1
  63. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  64. data/test/test_app/config/webpacker.yml +1 -3
  65. data/test/test_app/config/webpacker_other_location.yml +79 -0
  66. data/test/test_app/public/packs/manifest.json +12 -5
  67. data/test/webpacker_test.rb +21 -0
  68. data/webpacker.gemspec +2 -2
  69. data/yarn.lock +2357 -3262
  70. metadata +22 -11
data/README.md CHANGED
@@ -5,17 +5,13 @@
5
5
  [![Rubocop](https://github.com/rails/webpacker/workflows/Rubocop/badge.svg)](https://github.com/rails/webpacker/actions)
6
6
  [![JS lint](https://github.com/rails/webpacker/workflows/JS%20lint/badge.svg)](https://github.com/rails/webpacker/actions)
7
7
 
8
- [![node.js](https://img.shields.io/badge/node-%3E%3D%2010.17.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
8
+ [![node.js](https://img.shields.io/badge/node-%3E%3D%2012.0.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
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
- to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
14
- as the primary purpose for webpack is app-like JavaScript, not images, CSS, or
15
- even JavaScript Sprinkles (that all continues to live in app/assets).
16
-
17
- However, it is possible to use Webpacker for CSS, images and fonts assets as well,
18
- in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.
13
+ to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
14
+ leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fonts, and CSS.
19
15
 
20
16
  **NOTE:** The master branch now hosts the code for v6.x.x. Please refer to [5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable) branch for 5.x documentation.
21
17
 
@@ -26,16 +22,29 @@ in which case you may not even need the asset pipeline. This is mostly relevant
26
22
 
27
23
  - [Prerequisites](#prerequisites)
28
24
  - [Features](#features)
25
+ - [Optional support](#optional-support)
29
26
  - [Installation](#installation)
30
27
  - [Usage](#usage)
28
+ - [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
31
29
  - [Development](#development)
32
30
  - [Webpack Configuration](#webpack-configuration)
31
+ - [Babel Configuration](#babel-configuration)
32
+ - [Integrations](#integrations)
33
+ - [React](#react)
34
+ - [CoffeeScript](#coffeescript)
35
+ - [TypeScript](#typescript)
36
+ - [CSS](#css)
37
+ - [Postcss](#postcss)
38
+ - [Sass](#sass)
39
+ - [Less](#less)
40
+ - [Stylus](#stylus)
41
+ - [Other frameworks](#other-frameworks)
33
42
  - [Custom Rails environments](#custom-rails-environments)
34
43
  - [Upgrading](#upgrading)
35
44
  - [Paths](#paths)
36
- - [Resolved](#resolved)
37
- - [Watched](#watched)
45
+ - [Additional paths](#additional-paths)
38
46
  - [Deployment](#deployment)
47
+ - [Troubleshooting](#troubleshooting)
39
48
  - [Contributing](#contributing)
40
49
  - [License](#license)
41
50
 
@@ -45,8 +54,8 @@ in which case you may not even need the asset pipeline. This is mostly relevant
45
54
 
46
55
  - Ruby 2.4+
47
56
  - Rails 5.2+
48
- - Node.js 10.22.1+ || 12+ || 14+
49
- - Yarn 1.x+ || 2.x+
57
+ - Node.js 12.13.0+ || 14+
58
+ - Yarn
50
59
 
51
60
  ## Features
52
61
 
@@ -69,39 +78,28 @@ in which case you may not even need the asset pipeline. This is mostly relevant
69
78
 
70
79
  ## Installation
71
80
 
72
- You can either add Webpacker during setup of a new Rails 5.1+ application
73
- using new `--webpack` option:
81
+ You can configure a new Rails application with Webpacker right from the start using the `-j webpack` option:
74
82
 
75
83
  ```bash
76
- # Available Rails 5.1+
77
- rails new myapp --webpack
84
+ rails new myapp -j webpack
78
85
  ```
79
86
 
80
- Or add it to your `Gemfile`:
87
+ Or you can add it later by changing your `Gemfile`:
81
88
 
82
89
  ```ruby
83
90
  # Gemfile
84
- gem 'webpacker', '~> 6.x'
91
+ gem 'webpacker', '~> 6.0'
85
92
 
86
93
  # OR if you prefer to use master
87
94
  gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
88
95
  yarn add https://github.com/rails/webpacker.git
89
96
  ```
90
97
 
91
- Finally, run the following to install Webpacker:
98
+ Then running the following to install Webpacker:
92
99
 
93
100
  ```bash
94
- bundle
95
- bundle exec rails webpacker:install
96
-
97
- # OR (on rails version < 5.0)
98
- bundle exec rake webpacker:install
99
- ```
100
-
101
- Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/webpacker/issues/1078),
102
-
103
- ```bash
104
- yarn upgrade
101
+ ./bin/bundle install
102
+ ./bin/rails webpacker:install
105
103
  ```
106
104
 
107
105
  When `package.json` and/or `yarn.lock` changes, such as when pulling down changes to your local environment in a team settings, be sure to keep your NPM packages up-to-date:
@@ -115,11 +113,10 @@ yarn install
115
113
  Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
116
114
 
117
115
  ```yml
118
- app/packs:
119
- ├── entrypoints:
120
- │ # Only Webpack entry files here
121
- └── application.js
122
- │ └── application.css
116
+ app/javascript:
117
+ # Only Webpack entry files here
118
+ └── application.js
119
+ └── application.css
123
120
  └── src:
124
121
  │ └── my_component.js
125
122
  └── stylesheets:
@@ -135,6 +132,35 @@ You can then link the JavaScript pack in Rails views using the `javascript_pack_
135
132
  <%= stylesheet_pack_tag 'application' %>
136
133
  ```
137
134
 
135
+ The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
136
+ packs with the chunks in your view, which creates html tags for all the chunks.
137
+
138
+ The result looks like this:
139
+
140
+ ```erb
141
+ <%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
142
+
143
+ <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
144
+ <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
145
+ <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload" defer"></script>
146
+ <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
147
+ <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload" defer></script>
148
+ ```
149
+
150
+ **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. Be especially careful if you might be calling these view helpers from your view, partials, and the layout for a page. You will need some logic to ensure you call the helpers only once with multiple arguments.
151
+
152
+ ```erb
153
+ <%# DO %>
154
+ <%= javascript_pack_tag 'calendar', 'map' %>
155
+ <%= stylesheet_pack_tag 'calendar', 'map' %>
156
+
157
+ <%# DON'T %>
158
+ <%= javascript_pack_tag 'calendar' %>
159
+ <%= javascript_pack_tag 'map' %>
160
+ <%= stylesheet_pack_tag 'calendar' %>
161
+ <%= stylesheet_pack_tag 'map' %>
162
+ ```
163
+
138
164
  If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
139
165
  ```erb
140
166
  <img src="<%= asset_pack_path 'images/logo.svg' %>" />
@@ -163,71 +189,52 @@ If you want to use images in your stylesheets:
163
189
  background-image: url('../images/logo.svg')
164
190
  }
165
191
  ```
192
+ ##### Defer for `javascript_pack_tag`
193
+ Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
194
+
195
+ #### Server-Side Rendering (SSR)
166
196
 
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.
197
+ 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.
171
198
 
172
- **Note:** In order for your styles or static assets files to be available in your view,
173
- you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know
174
- to package up those files.
199
+ **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.
175
200
 
176
201
  ### Development
177
202
 
178
- Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
179
- Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js`
180
- executables to ensure that the right configuration files and environmental variables
181
- are loaded based on your environment.
182
-
183
- In development, Webpacker compiles on demand rather than upfront by default. This
184
- happens when you refer to any of the pack assets using the Webpacker helper methods.
185
- This means that you don't have to run any separate processes. Compilation errors are logged
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/).
196
-
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).
203
+ Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-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.
204
+
205
+ In development, Webpacker compiles on demand rather than upfront by default. This happens when you refer to any of the pack assets using the Webpacker 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/webpack --watch` or run `./bin/webpack-dev-server`
206
+
207
+ 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`. Windows users will need to run these commands in a terminal separate from `bundle exec rails s`. 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/).
200
208
 
201
209
  ```bash
202
210
  # webpack dev server
203
211
  ./bin/webpack-dev-server
204
212
 
205
213
  # watcher
206
- ./bin/webpack --watch --colors --progress
214
+ ./bin/webpack --watch --progress
207
215
 
208
216
  # standalone build
209
- ./bin/webpack
217
+ ./bin/webpack --progress
218
+
219
+ # Help
220
+ ./bin/webpack help
221
+
222
+ # Version
223
+ ./bin/webpack version
224
+
225
+ # Info
226
+ ./bin/webpack info
210
227
  ```
211
228
 
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`
229
+ 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`
216
230
 
217
- You can use environment variables as options supported by
218
- [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
219
- form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
220
- variables will always take precedence over the ones already set in the
221
- configuration file, and that the _same_ environmental variables must
222
- be available to the `rails server` process.
231
+ You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the _same_ environmental variables must be available to the `rails server` process.
223
232
 
224
233
  ```bash
225
234
  WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
226
235
  ```
227
236
 
228
- By default, the webpack dev server listens on `localhost` in development for security purposes.
229
- However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
230
- you can set the `host` when running `./bin/webpack-dev-server` binstub:
237
+ By default, the webpack dev server listens on `localhost` in development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the `host` when running `./bin/webpack-dev-server` binstub:
231
238
 
232
239
  ```bash
233
240
  WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
@@ -236,23 +243,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
236
243
  **Note:** You need to allow webpack-dev-server host as an allowed origin for `connect-src` if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this:
237
244
 
238
245
  ```ruby
239
- Rails.application.config.content_security_policy do |policy|
240
- policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
241
- end
246
+ Rails.application.config.content_security_policy do |policy|
247
+ policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
248
+ end
242
249
  ```
243
250
 
244
251
  **Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
245
252
 
253
+
246
254
  ### Webpack Configuration
247
255
 
248
- Webpacker gives you a default set of configuration files for test, development and
249
- production environments in `config/webpack/*.js`. You can configure each individual
250
- environment in their respective files or configure them all in the base
256
+ Webpacker gives you a default set of configuration files for test, development and production environments in `config/webpack/*.js`. You can configure each individual environment in their respective files or configure them all in the base
251
257
  `config/webpack/base.js` file.
252
258
 
253
- By default, you don't need to make any changes to `config/webpack/*.js`
254
- files since it's all standard production-ready configuration. However,
255
- if you do need to customize or add a new loader, this is where you would go.
259
+ By default, you don't need to make any changes to `config/webpack/*.js` files since it's all standard production-ready configuration. However, if you do need to customize or add a new loader, this is where you would go.
256
260
 
257
261
  Here is how you can modify webpack configuration:
258
262
 
@@ -283,8 +287,7 @@ const customConfig = require('./custom')
283
287
  module.exports = merge(webpackConfig, customConfig)
284
288
  ```
285
289
 
286
- If you need access to configs within Webpacker's configuration,
287
- you can import them like so:
290
+ If you need access to configs within Webpacker's configuration, you can import them like so:
288
291
 
289
292
  ```js
290
293
  // config/webpack/base.js
@@ -297,11 +300,51 @@ console.log(webpackConfig.source_path)
297
300
  console.log(JSON.stringify(webpackConfig, undefined, 2))
298
301
  ```
299
302
 
303
+ ### Babel configuration
304
+
305
+ By default, you will find the Webpacker preset in your `package.json`.
306
+
307
+ ```json
308
+ "babel": {
309
+ "presets": [
310
+ "./node_modules/@rails/webpacker/package/babel/preset.js"
311
+ ]
312
+ },
313
+ ```
314
+
315
+ 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.
316
+
317
+
300
318
  ### Integrations
301
319
 
302
- Webpacker out of the box supports JS and static assets (fonts, images etc.)
303
- compilation. To enable support for CoffeeScript or TypeScript install
304
- relevant packages:
320
+ Webpacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
321
+
322
+ #### React
323
+
324
+ ```bash
325
+ yarn add react react-dom @babel/preset-react
326
+ ```
327
+
328
+ ...if you are using typescript, update your `tsconfig.json`
329
+
330
+ ```json
331
+ {
332
+ "compilerOptions": {
333
+ "declaration": false,
334
+ "emitDecoratorMetadata": true,
335
+ "experimentalDecorators": true,
336
+ "lib": ["es6", "dom"],
337
+ "module": "es6",
338
+ "moduleResolution": "node",
339
+ "sourceMap": true,
340
+ "target": "es5",
341
+ "jsx": "react",
342
+ "noEmit": true
343
+ },
344
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
345
+ "compileOnSave": false
346
+ }
347
+ ```
305
348
 
306
349
  #### CoffeeScript
307
350
 
@@ -315,6 +358,12 @@ yarn add coffeescript coffee-loader
315
358
  yarn add typescript @babel/preset-typescript
316
359
  ```
317
360
 
361
+ Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
362
+
363
+ ```bash
364
+ yarn add fork-ts-checker-webpack-plugin
365
+ ```
366
+
318
367
  Add tsconfig.json
319
368
 
320
369
  ```json
@@ -339,12 +388,6 @@ Add tsconfig.json
339
388
  }
340
389
  ```
341
390
 
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
391
  Then modify the webpack config to use it as a plugin:
349
392
 
350
393
  ```js
@@ -362,10 +405,10 @@ module.exports = merge(webpackConfig, {
362
405
  To enable CSS support in your application, add following packages:
363
406
 
364
407
  ```bash
365
- yarn add css-loader mini-css-extract-plugin css-minimizer-webpack-plugin
408
+ yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
366
409
  ```
367
410
 
368
- optionally, add the css extension to webpack config for easy resolution.
411
+ Optionally, add the `CSS` extension to webpack config for easy resolution.
369
412
 
370
413
  ```js
371
414
  // config/webpack/base.js
@@ -379,13 +422,18 @@ const customConfig = {
379
422
  module.exports = merge(webpackConfig, customConfig)
380
423
  ```
381
424
 
382
- To enable postcss, sass or less support, add css support first and
425
+ To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
383
426
  then add the relevant pre-processors:
384
427
 
385
428
  #### Postcss
386
429
 
387
430
  ```bash
388
- yarn add postcss-loader
431
+ yarn add postcss postcss-loader
432
+ ```
433
+
434
+ Optionally add these two plugins if they are required in your `postcss.config.js`:
435
+ ```bash
436
+ yarn add postcss-preset-env postcss-flexbugs-fixes
389
437
  ```
390
438
 
391
439
  #### Sass
@@ -406,39 +454,6 @@ yarn add less less-loader
406
454
  yarn add stylus stylus-loader
407
455
  ```
408
456
 
409
- #### React
410
-
411
- React is supported and you just need to add relevant packages,
412
-
413
- ```bash
414
- yarn add react react-dom @babel/preset-react
415
- ```
416
-
417
- if you are using typescript, update your `tsconfig.json`
418
-
419
- ```json
420
- {
421
- "compilerOptions": {
422
- "declaration": false,
423
- "emitDecoratorMetadata": true,
424
- "experimentalDecorators": true,
425
- "lib": ["es6", "dom"],
426
- "module": "es6",
427
- "moduleResolution": "node",
428
- "sourceMap": true,
429
- "target": "es5",
430
- "jsx": "react",
431
- "noEmit": true
432
- },
433
- "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
434
- "compileOnSave": false
435
- }
436
- ```
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
-
442
457
  #### Other frameworks
443
458
 
444
459
  Please follow webpack integration guide for relevant framework or library,
@@ -450,7 +465,7 @@ Please follow webpack integration guide for relevant framework or library,
450
465
  For example to add Vue support:
451
466
  ```js
452
467
  // config/webpack/rules/vue.js
453
- const VueLoaderPlugin = require('vue-loader/lib/plugin')
468
+ const { VueLoaderPlugin } = require('vue-loader')
454
469
 
455
470
  module.exports = {
456
471
  module: {
@@ -461,7 +476,10 @@ module.exports = {
461
476
  }
462
477
  ]
463
478
  },
464
- plugins: [new VueLoaderPlugin()]
479
+ plugins: [new VueLoaderPlugin()],
480
+ resolve: {
481
+ extensions: ['.vue']
482
+ }
465
483
  }
466
484
  ```
467
485
 
@@ -470,9 +488,10 @@ module.exports = {
470
488
  const { webpackConfig, merge } = require('@rails/webpacker')
471
489
  const vueConfig = require('./rules/vue')
472
490
 
473
- module.exports = merge(webpackConfig, vueConfig)
491
+ module.exports = merge(vueConfig, webpackConfig)
474
492
  ```
475
493
 
494
+
476
495
  ### Custom Rails environments
477
496
 
478
497
  Out of the box Webpacker 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. Webpacker supports this out of the box from version 3.4.0+ onwards.
@@ -493,8 +512,7 @@ staging:
493
512
  public_output_path: packs-staging
494
513
  ```
495
514
 
496
- or, Webpacker will use production environment as a fallback environment for loading configurations. Please note, `NODE_ENV` can either be set to `production`, `development` or `test`.
497
- 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`.
515
+ Otherwise Webpacker 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`.
498
516
 
499
517
  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:
500
518
 
@@ -502,15 +520,13 @@ For example, the below command will compile assets in production mode but will u
502
520
  RAILS_ENV=staging bundle exec rails assets:precompile
503
521
  ```
504
522
 
505
- And, this will compile in development mode and load configuration for cucumber environment
506
- if defined in webpacker.yml or fallback to production configuration
523
+ And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
507
524
 
508
525
  ```bash
509
526
  RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
510
527
  ```
511
528
 
512
- Please note, binstubs compiles in development mode however rake tasks
513
- compiles in production mode.
529
+ Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
514
530
 
515
531
  ```bash
516
532
  # Compiles in development mode unless NODE_ENV is specified, per the binstub source
@@ -533,9 +549,14 @@ bundle update webpacker
533
549
  # overwrite your changes to the default install files and revert any unwanted changes from the install
534
550
  rails webpacker:install
535
551
 
552
+ # yarn 1 instructions
536
553
  yarn upgrade @rails/webpacker --latest
537
554
  yarn upgrade webpack-dev-server --latest
538
555
 
556
+ # yarn 2 instructions
557
+ yarn up @rails/webpacker@latest
558
+ yarn up webpack-dev-server@latest
559
+
539
560
  # Or to install the latest release (including pre-releases)
540
561
  yarn add @rails/webpacker@next
541
562
  ```
@@ -544,19 +565,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
544
565
 
545
566
  ## Paths
546
567
 
547
- By default, Webpacker ships with simple conventions for where the JavaScript
548
- app files and compiled webpack bundles will go in your Rails app.
549
- All these options are configurable from `config/webpacker.yml` file.
568
+ By default, Webpacker 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.
550
569
 
551
- The configuration for what webpack is supposed to compile by default rests
552
- on the convention that every file in `app/packs/entrypoints/*`**(default)**
553
- or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
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
555
- an entry file. As a rule of thumb, put all files you want to link in your views inside
556
- "packs" directory and keep everything else under `app/packs`.
570
+ The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/packs/entrypoints/*`**(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 anything inside `packs` 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 "packs" directory and keep everything else under `app/packs`.
557
571
 
558
- Suppose you want to change the source directory from `app/packs`
559
- to `frontend` and output to `assets/packs`. This is how you would do it:
572
+ Suppose you want to change the source directory from `app/packs` to `frontend` and output to `assets/packs`. This is how you would do it:
560
573
 
561
574
  ```yml
562
575
  # config/webpacker.yml
@@ -574,16 +587,11 @@ development:
574
587
  port: 3035
575
588
  ```
576
589
 
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.
590
+ If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
580
591
 
581
592
  ### Additional paths
582
593
 
583
- If you are adding Webpacker to an existing app that has most of the assets inside
584
- `app/assets` or inside an engine, and you want to share that
585
- with webpack modules, you can use the `additional_paths`
586
- option available in `config/webpacker.yml`. This lets you
594
+ If you are adding Webpacker 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
587
595
  add additional paths that webpack should look up when resolving modules:
588
596
 
589
597
  ```yml
@@ -598,9 +606,10 @@ import 'stylesheets/main'
598
606
  import 'images/rails.png'
599
607
  ```
600
608
 
601
- **Note:** Please be careful when adding paths here otherwise it
602
- will make the compilation slow, consider adding specific paths instead of
603
- whole parent directory if you just need to reference one or two modules
609
+ **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
610
+
611
+ **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, Webpacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Webpacker::Compiler#watched_files_digest`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
612
+
604
613
 
605
614
  ## Deployment
606
615
 
@@ -610,16 +619,19 @@ When compiling assets for production on a remote server, such as a continuous in
610
619
 
611
620
  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
621
 
622
+
613
623
  ## Troubleshooting
614
624
 
615
625
  See the doc page for [Troubleshooting](./docs/troubleshooting.md).
616
626
 
627
+
617
628
  ## Contributing
618
629
 
619
630
  [![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
620
631
 
621
632
  We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
622
633
 
634
+
623
635
  ## License
624
636
 
625
637
  Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
data/config/webpacker.yml CHANGED
@@ -1 +1 @@
1
- config/lib/install/config/webpacker.yml
1
+ ../lib/install/config/webpacker.yml
@@ -0,0 +1,29 @@
1
+ # Developing Webpacker
2
+
3
+ It's a little trickier for Rails developers to work on the JS code of a project like rails/webpacker. So here are some tips!
4
+
5
+ ## Use some test app
6
+ For example, for React on Rails Changes, I'm using [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh).
7
+ This directory is the `TEST_APP_DIR`.
8
+
9
+ ## Fork rails/webpacker
10
+ Let's call the rails/webpacker directory `WEBPACKER_DIR` which has rails/webpacker's `package.json`.
11
+
12
+ ## Changing the Package
13
+ ### Setup with Yalc
14
+ Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors.
15
+ 1. In `WEBPACKER_DIR`, run `yalc publish`
16
+ 2. In `TEST_APP_DIR`, run `yalc link @rails/webpacker`
17
+
18
+ ## Update the Package Code
19
+ 1. Make some JS change in WEBPACKER_DIR
20
+ 2. Run `yalc push` and your changes will be pushed to your `TEST_APP_DIR`'s node_modules.
21
+ 3. You may need to run `yarn` in `TEST_APP_DIR` if you added or removed dependencies of rails/webpacker.
22
+
23
+ ## Updating the Ruby Code
24
+
25
+ For the Ruby part, just change the gem reference `TEST_APP_DIR`, like:
26
+
27
+ ```ruby
28
+ gem "webpacker", path: "../../forks/webpacker"
29
+ ```