webpacker 6.0.0.beta.7 → 6.0.0.rc.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/jest.yml +5 -2
  3. data/.github/workflows/js-lint.yml +4 -1
  4. data/.github/workflows/rubocop.yml +1 -1
  5. data/.github/workflows/ruby.yml +12 -12
  6. data/.node-version +1 -1
  7. data/.rubocop.yml +1 -0
  8. data/CHANGELOG.md +21 -10
  9. data/Gemfile.lock +2 -2
  10. data/README.md +159 -156
  11. data/config/webpacker.yml +1 -1
  12. data/docs/developing_webpacker.md +29 -0
  13. data/docs/troubleshooting.md +53 -23
  14. data/docs/v6_upgrade.md +31 -42
  15. data/gemfiles/Gemfile-rails-edge +1 -1
  16. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  17. data/lib/install/bin/yarn +18 -0
  18. data/lib/install/config/webpacker.yml +2 -6
  19. data/lib/install/package.json +17 -0
  20. data/lib/install/packs/entrypoints/application.js +3 -4
  21. data/lib/install/template.rb +28 -15
  22. data/lib/tasks/webpacker/check_node.rake +3 -1
  23. data/lib/tasks/webpacker/check_yarn.rake +4 -2
  24. data/lib/tasks/webpacker/clobber.rake +1 -1
  25. data/lib/tasks/webpacker/verify_config.rake +14 -0
  26. data/lib/tasks/webpacker/verify_install.rake +1 -11
  27. data/lib/tasks/yarn.rake +36 -0
  28. data/lib/webpacker/configuration.rb +15 -4
  29. data/lib/webpacker/dev_server.rb +6 -0
  30. data/lib/webpacker/dev_server_runner.rb +5 -2
  31. data/lib/webpacker/env.rb +5 -1
  32. data/lib/webpacker/helper.rb +14 -8
  33. data/lib/webpacker/instance.rb +4 -0
  34. data/lib/webpacker/manifest.rb +1 -2
  35. data/lib/webpacker/railtie.rb +1 -2
  36. data/lib/webpacker/runner.rb +1 -1
  37. data/lib/webpacker/version.rb +1 -1
  38. data/lib/webpacker.rb +1 -1
  39. data/package/__tests__/development.js +2 -2
  40. data/package/__tests__/env.js +8 -4
  41. data/package/babel/preset.js +0 -1
  42. data/package/env.js +7 -1
  43. data/package/environments/__tests__/base.js +3 -3
  44. data/package/environments/base.js +12 -7
  45. data/package/environments/development.js +7 -9
  46. data/package/index.js +2 -0
  47. data/package/inliningCss.js +7 -0
  48. data/package/rules/file.js +1 -1
  49. data/package/rules/sass.js +1 -2
  50. data/package/utils/get_style_rule.js +4 -2
  51. data/package.json +25 -29
  52. data/test/configuration_test.rb +1 -1
  53. data/test/dev_server_runner_test.rb +4 -1
  54. data/test/helper_test.rb +48 -34
  55. data/test/manifest_test.rb +10 -2
  56. data/test/mounted_app/test/dummy/config/webpacker.yml +1 -1
  57. data/test/test_app/config/webpacker.yml +1 -3
  58. data/test/test_app/config/webpacker_other_location.yml +79 -0
  59. data/test/test_app/public/packs/manifest.json +12 -5
  60. data/test/webpacker_test.rb +17 -0
  61. data/yarn.lock +1567 -1039
  62. metadata +14 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2425c109984a7a6029a78cecf6cbc2a9567cc91ea9fb9d85efc11bb4c7f5e591
4
- data.tar.gz: c58f07ce11e2e4d8813016947f4365e681a3366225142cb5ba35a352063da428
3
+ metadata.gz: 96eeffae92e201748b583ac421869d0e76ec806df6dc750d2f0459b2aad05f62
4
+ data.tar.gz: 3ed4f39584aa504dd761c59d160014eacc2e8392f847f3c95d5380c32d449142
5
5
  SHA512:
6
- metadata.gz: 405ea2a5d1be7ee478b7556e2306be7db07b48d48cbccf260a6bfcad71ccde747f114c1d71309571db3061dca03cd75bc719e2a400d09f7ab1eb6096e86662f9
7
- data.tar.gz: 81458c1b088cf49dc03072a6fcd94dded64739b1820f2a83631c0eb36ecd2174d180a325353f2f950cefbc45db6d016e4151d0d81cb9b00f0615cb9667b1218f
6
+ metadata.gz: d70ec25c9ee0ca7cbbe42f1806a7e3e142f0b7b55a0a9cdcd43d1922cf23bd69346bbdab6e37cd31a1cbb4fc533b951b60ef3fed3d622f36208a3841eccc4ff7
7
+ data.tar.gz: 47b10bc8e36c8e0697a2341714f27ffc2d749d465b840d14cea71847f592b30f5e6512325198fcedbb2c406859acbc15384c32a71c3cbc62a0ecd2c26b4f9e62
@@ -3,16 +3,19 @@ name: Jest specs
3
3
  on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ jest:
7
7
  name: Jest specs
8
8
  strategy:
9
9
  matrix:
10
10
  os: [ubuntu-latest]
11
- node: [10.x, 12.x, 14.x, 15.x]
11
+ node: [12.x, 14.x, 15.x]
12
12
 
13
13
  runs-on: ${{ matrix.os }}
14
14
 
15
15
  steps:
16
+ - name: Install yarn maybe
17
+ run: which yarn || sudo npm install -g yarn
18
+
16
19
  - uses: actions/checkout@v2
17
20
  - name: Get yarn cache directory path
18
21
  id: yarn-cache-dir-path
@@ -3,7 +3,7 @@ name: JS lint
3
3
  on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ js-lint:
7
7
  name: JS Lint
8
8
 
9
9
  strategy:
@@ -14,6 +14,9 @@ jobs:
14
14
  runs-on: ${{ matrix.os }}
15
15
 
16
16
  steps:
17
+ - name: Install yarn maybe
18
+ run: which yarn || sudo npm install -g yarn
19
+
17
20
  - uses: actions/checkout@v2
18
21
  - name: Get yarn cache directory path
19
22
  id: yarn-cache-dir-path
@@ -3,7 +3,7 @@ name: Rubocop
3
3
  on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ rubocop:
7
7
  name: Rubocop
8
8
  runs-on: ${{ matrix.os }}
9
9
  env:
@@ -3,7 +3,7 @@ name: Ruby specs
3
3
  on: [push, pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ test:
7
7
  name: Ruby specs
8
8
  runs-on: ${{ matrix.os }}
9
9
  continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' || matrix.experimental }}
@@ -23,23 +23,20 @@ jobs:
23
23
  ]
24
24
  gemfile: [
25
25
  "gemfiles/Gemfile-rails.5.2.x",
26
- "gemfiles/Gemfile-rails.6.0.x"
26
+ "gemfiles/Gemfile-rails.6.0.x",
27
+ "gemfiles/Gemfile-rails.6.1.x"
27
28
  ]
28
29
  exclude:
29
- - ruby: "2.4"
30
+ - ruby: 2.4
30
31
  gemfile: gemfiles/Gemfile-rails.6.0.x
31
- - ruby: "3.0"
32
+ - ruby: 2.4
33
+ gemfile: gemfiles/Gemfile-rails.6.1.x
34
+ - ruby: 2.5
35
+ gemfile: gemfiles/Gemfile-rails.6.1.x
36
+ - ruby: 3.0
32
37
  gemfile: gemfiles/Gemfile-rails.5.2.x
33
38
  experimental: [false]
34
39
  include:
35
- - ruby: 2.5
36
- os: ubuntu-latest
37
- gemfile: gemfiles/Gemfile-rails-edge
38
- experimental: true
39
- - ruby: 2.6
40
- os: ubuntu-latest
41
- gemfile: gemfiles/Gemfile-rails-edge
42
- experimental: true
43
40
  - ruby: 2.7
44
41
  os: ubuntu-latest
45
42
  gemfile: gemfiles/Gemfile-rails-edge
@@ -58,6 +55,9 @@ jobs:
58
55
  restore-keys: |
59
56
  bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.gemfile }}-gems-
60
57
 
58
+ - name: Install yarn maybe
59
+ run: which yarn || sudo npm install -g yarn
60
+
61
61
  - uses: ruby/setup-ruby@v1
62
62
  with:
63
63
  ruby-version: ${{ matrix.ruby }}
data/.node-version CHANGED
@@ -1 +1 @@
1
- 10.22.1
1
+ 16.7.0
data/.rubocop.yml CHANGED
@@ -8,6 +8,7 @@ AllCops:
8
8
  - "lib/install/templates/**"
9
9
  - "vendor/**/*"
10
10
  - "node_modules/**/*"
11
+ - "_actions/**/*"
11
12
 
12
13
  # Prefer &&/|| over and/or.
13
14
  Style/AndOr:
data/CHANGELOG.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## [[6.0.0]](https://github.com/rails/webpacker/compare/v5.1.1...master) - 2021-TBD
6
6
 
7
- Please see [UPGRADE GUIDE](./docs/v6_upgrade.md) for more informations.
7
+ Please see [UPGRADE GUIDE](./docs/v6_upgrade.md) for more information.
8
8
 
9
9
  - `node_modules` will no longer be compiled by default. This primarily fixes [rails issue #35501](https://github.com/rails/rails/issues/35501) as well as [numerous other webpacker issues](https://github.com/rails/webpacker/issues/2131#issuecomment-581618497). The disabled loader can still be required explicitly via:
10
10
 
@@ -14,26 +14,37 @@ environment.loaders.append('nodeModules', nodeModules)
14
14
  ```
15
15
 
16
16
  - If you have added `environment.loaders.delete('nodeModules')` to your `environment.js`, this must be removed or you will receive an error (`Item nodeModules not found`).
17
- - The install task will now set the `extract_css` default to `true` in all environments and generate a separate `application.css` file for the default `application` pack, as supported by multiple files per entry introduced in 5.0.0. [#2608](https://github.com/rails/webpacker/pull/2608)
17
+ - `extract_css` option was removed. Webpacker will generate a separate `application.css` file for the default `application` pack, as supported by multiple files per entry introduced in 5.0.0. [#2608](https://github.com/rails/webpacker/pull/2608). However, CSS will be inlined when the webpack-dev-server is used with `hmr: true`. JS package exports `inliningCss`. This is useful to enable HMR for React.
18
18
  - Webpacker's wrapper to the `splitChunks()` API will now default `runtimeChunk: 'single'` which will help prevent potential issues when using multiple entry points per page [#2708](https://github.com/rails/webpacker/pull/2708).
19
19
  - Changes `@babel/preset-env` modules option to `'auto'` per recommendation in the Babel docs [#2709](https://github.com/rails/webpacker/pull/2709)
20
20
  - Adds experimental Yarn 2 support. Note you must manually set `nodeLinker: node-modules` in your `.yarnrc.yml`.
21
-
22
21
  - Fixes dev server issues [#2898](https://github.com/rails/webpacker/pull/2898)
22
+ - Update static files path to from `media/` to `static/`.
23
23
 
24
24
  ### Breaking changes
25
-
26
- - Simple webpack config
27
25
  - Removed integration installers
28
26
  - Splitchunks enabled by default
29
- - CSS extraction enabled by default
30
- - Optional CSS support
27
+ - CSS extraction enabled by default, except when devServer is configured and running
28
+
29
+ ## [[5.4.0]](https://github.com/rails/webpacker/compare/v5.3.0...v5.4.0) - 2021-05-18
30
+
31
+ - Fix compatibility with Psych 4
32
+
33
+ ## [[5.3.0]](https://github.com/rails/webpacker/compare/v5.2.1...v5.3.0) - 2021-04-27
34
+
35
+ - Adds experimental Yarn 2 support. Note you must manually set `nodeLinker: node-modules` in your `.yarnrc.yml`.
36
+
37
+ - Keep backups, even when they're old [#2912](https://github.com/rails/webpacker/pull/2912)
38
+
39
+ ## [[5.2.2]](https://github.com/rails/webpacker/compare/v5.2.1...v5.2.2) - 2021-04-27
40
+
41
+ - Bump deps and remove node-sass [#2997](https://github.com/rails/webpacker/pull/2997).
31
42
 
32
- ## [[5.2.1]](https://github.com/rails/webpacker/compare/v5.2.0...5.2.1) - 2020-08-17
43
+ ## [[5.2.1]](https://github.com/rails/webpacker/compare/v5.2.0...v5.2.1) - 2020-08-17
33
44
 
34
45
  - Revert [#1311](https://github.com/rails/webpacker/pull/1311).
35
46
 
36
- ## [[5.2.0]](https://github.com/rails/webpacker/compare/v5.1.1...5.2.0) - 2020-08-16
47
+ ## [[5.2.0]](https://github.com/rails/webpacker/compare/v5.1.1...v5.2.0) - 2020-08-16
37
48
 
38
49
  - Bump dependencies and fixes. See [diff](https://github.com/rails/webpacker/compare/v5.1.1...5-x-stable) for changes.
39
50
 
@@ -412,7 +423,7 @@ See changes: https://github.com/rails/webpacker/compare/e8b197e36c77181ca2e4765c
412
423
 
413
424
  ### Added
414
425
 
415
- - On CI, sort files & check modified w/ digest intead of mtime[#1522](https://github.com/rails/webpacker/pull/1522)
426
+ - On CI, sort files & check modified w/ digest instead of mtime[#1522](https://github.com/rails/webpacker/pull/1522)
416
427
 
417
428
  ## [3.5.3] - 2018-05-03
418
429
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webpacker (6.0.0.beta.7)
4
+ webpacker (6.0.0.rc.2)
5
5
  activesupport (>= 5.2)
6
6
  rack-proxy (>= 0.6.1)
7
7
  railties (>= 5.2)
@@ -180,4 +180,4 @@ DEPENDENCIES
180
180
  webpacker!
181
181
 
182
182
  BUNDLED WITH
183
- 2.2.3
183
+ 2.2.25
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, fronts, and CSS as well.
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,7 +54,7 @@ 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+
57
+ - Node.js 12+ || 14+
49
58
  - Yarn 1.x+ || 2.x+
50
59
 
51
60
  ## Features
@@ -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 `--webpack` option:
74
82
 
75
83
  ```bash
76
- # Available Rails 5.1+
77
84
  rails new myapp --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:
@@ -135,6 +133,35 @@ You can then link the JavaScript pack in Rails views using the `javascript_pack_
135
133
  <%= stylesheet_pack_tag 'application' %>
136
134
  ```
137
135
 
136
+ The `javascript_pack_tag` and `stylesheet_pack_tag` helpers will include all the transpiled
137
+ packs with the chunks in your view, which creates html tags for all the chunks.
138
+
139
+ The result looks like this:
140
+
141
+ ```erb
142
+ <%= javascript_pack_tag 'calendar', 'map' %>
143
+
144
+ <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
145
+ <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
146
+ <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
147
+ <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
148
+ <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
149
+ ```
150
+
151
+ **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.
152
+
153
+ ```erb
154
+ <%# DO %>
155
+ <%= javascript_pack_tag 'calendar', 'map' %>
156
+ <%= stylesheet_pack_tag 'calendar', 'map' %>
157
+
158
+ <%# DON'T %>
159
+ <%= javascript_pack_tag 'calendar' %>
160
+ <%= javascript_pack_tag 'map' %>
161
+ <%= stylesheet_pack_tag 'calendar' %>
162
+ <%= stylesheet_pack_tag 'map' %>
163
+ ```
164
+
138
165
  If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
139
166
  ```erb
140
167
  <img src="<%= asset_pack_path 'images/logo.svg' %>" />
@@ -164,39 +191,20 @@ If you want to use images in your stylesheets:
164
191
  }
165
192
  ```
166
193
 
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.
194
+ #### Server-Side Rendering (SSR)
195
+
196
+ Note, if you are using server-side rendering of JavaScript with dynamic code-spliting, 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.
197
+
198
+ **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.
171
199
 
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.
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 `app/packs/entrypoints/*.js` files and 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
@@ -209,25 +217,15 @@ on how to configure rails/webpacker for Fast Refresh with React, see article
209
217
  ./bin/webpack
210
218
  ```
211
219
 
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`
220
+ 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
221
 
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.
222
+ 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
223
 
224
224
  ```bash
225
225
  WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
226
226
  ```
227
227
 
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:
228
+ 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
229
 
232
230
  ```bash
233
231
  WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
@@ -236,23 +234,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
236
234
  **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
235
 
238
236
  ```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
237
+ Rails.application.config.content_security_policy do |policy|
238
+ policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
239
+ end
242
240
  ```
243
241
 
244
242
  **Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
245
243
 
244
+
246
245
  ### Webpack Configuration
247
246
 
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
247
+ 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
248
  `config/webpack/base.js` file.
252
249
 
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.
250
+ 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
251
 
257
252
  Here is how you can modify webpack configuration:
258
253
 
@@ -283,8 +278,7 @@ const customConfig = require('./custom')
283
278
  module.exports = merge(webpackConfig, customConfig)
284
279
  ```
285
280
 
286
- If you need access to configs within Webpacker's configuration,
287
- you can import them like so:
281
+ If you need access to configs within Webpacker's configuration, you can import them like so:
288
282
 
289
283
  ```js
290
284
  // config/webpack/base.js
@@ -297,11 +291,51 @@ console.log(webpackConfig.source_path)
297
291
  console.log(JSON.stringify(webpackConfig, undefined, 2))
298
292
  ```
299
293
 
294
+ ### Babel configuration
295
+
296
+ By default, you will find the Webpacker preset in your `package.json`.
297
+
298
+ ```json
299
+ "babel": {
300
+ "presets": [
301
+ "./node_modules/@rails/webpacker/package/babel/preset.js"
302
+ ]
303
+ },
304
+ ```
305
+
306
+ 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.
307
+
308
+
300
309
  ### Integrations
301
310
 
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:
311
+ Webpacker out of the box supports JS and static assets (fonts, images etc.) compilation. To enable support for CoffeeScript or TypeScript install relevant packages:
312
+
313
+ #### React
314
+
315
+ ```bash
316
+ yarn add react react-dom @babel/preset-react
317
+ ```
318
+
319
+ ...if you are using typescript, update your `tsconfig.json`
320
+
321
+ ```json
322
+ {
323
+ "compilerOptions": {
324
+ "declaration": false,
325
+ "emitDecoratorMetadata": true,
326
+ "experimentalDecorators": true,
327
+ "lib": ["es6", "dom"],
328
+ "module": "es6",
329
+ "moduleResolution": "node",
330
+ "sourceMap": true,
331
+ "target": "es5",
332
+ "jsx": "react",
333
+ "noEmit": true
334
+ },
335
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
336
+ "compileOnSave": false
337
+ }
338
+ ```
305
339
 
306
340
  #### CoffeeScript
307
341
 
@@ -315,6 +349,12 @@ yarn add coffeescript coffee-loader
315
349
  yarn add typescript @babel/preset-typescript
316
350
  ```
317
351
 
352
+ Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
353
+
354
+ ```bash
355
+ yarn add fork-ts-checker-webpack-plugin
356
+ ```
357
+
318
358
  Add tsconfig.json
319
359
 
320
360
  ```json
@@ -339,12 +379,6 @@ Add tsconfig.json
339
379
  }
340
380
  ```
341
381
 
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
382
  Then modify the webpack config to use it as a plugin:
349
383
 
350
384
  ```js
@@ -362,10 +396,10 @@ module.exports = merge(webpackConfig, {
362
396
  To enable CSS support in your application, add following packages:
363
397
 
364
398
  ```bash
365
- yarn add css-loader mini-css-extract-plugin css-minimizer-webpack-plugin
399
+ yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
366
400
  ```
367
401
 
368
- optionally, add the css extension to webpack config for easy resolution.
402
+ Optionally, add the `CSS` extension to webpack config for easy resolution.
369
403
 
370
404
  ```js
371
405
  // config/webpack/base.js
@@ -379,13 +413,18 @@ const customConfig = {
379
413
  module.exports = merge(webpackConfig, customConfig)
380
414
  ```
381
415
 
382
- To enable postcss, sass or less support, add css support first and
416
+ To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
383
417
  then add the relevant pre-processors:
384
418
 
385
419
  #### Postcss
386
420
 
387
421
  ```bash
388
- yarn add postcss-loader
422
+ yarn add postcss postcss-loader
423
+ ```
424
+
425
+ Optionally add these two plugins if they are required in your `postcss.config.js`:
426
+ ```bash
427
+ yarn add postcss-preset-env postcss-flexbugs-fixes
389
428
  ```
390
429
 
391
430
  #### Sass
@@ -406,39 +445,6 @@ yarn add less less-loader
406
445
  yarn add stylus stylus-loader
407
446
  ```
408
447
 
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
448
  #### Other frameworks
443
449
 
444
450
  Please follow webpack integration guide for relevant framework or library,
@@ -450,7 +456,7 @@ Please follow webpack integration guide for relevant framework or library,
450
456
  For example to add Vue support:
451
457
  ```js
452
458
  // config/webpack/rules/vue.js
453
- const VueLoaderPlugin = require('vue-loader/lib/plugin')
459
+ const { VueLoaderPlugin } = require('vue-loader')
454
460
 
455
461
  module.exports = {
456
462
  module: {
@@ -461,7 +467,10 @@ module.exports = {
461
467
  }
462
468
  ]
463
469
  },
464
- plugins: [new VueLoaderPlugin()]
470
+ plugins: [new VueLoaderPlugin()],
471
+ resolve: {
472
+ extensions: ['.vue']
473
+ }
465
474
  }
466
475
  ```
467
476
 
@@ -470,9 +479,10 @@ module.exports = {
470
479
  const { webpackConfig, merge } = require('@rails/webpacker')
471
480
  const vueConfig = require('./rules/vue')
472
481
 
473
- module.exports = merge(webpackConfig, vueConfig)
482
+ module.exports = merge(vueConfig, webpackConfig)
474
483
  ```
475
484
 
485
+
476
486
  ### Custom Rails environments
477
487
 
478
488
  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 +503,7 @@ staging:
493
503
  public_output_path: packs-staging
494
504
  ```
495
505
 
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`.
506
+ 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
507
 
499
508
  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
509
 
@@ -502,15 +511,13 @@ For example, the below command will compile assets in production mode but will u
502
511
  RAILS_ENV=staging bundle exec rails assets:precompile
503
512
  ```
504
513
 
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
514
+ And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
507
515
 
508
516
  ```bash
509
517
  RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
510
518
  ```
511
519
 
512
- Please note, binstubs compiles in development mode however rake tasks
513
- compiles in production mode.
520
+ Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
514
521
 
515
522
  ```bash
516
523
  # Compiles in development mode unless NODE_ENV is specified, per the binstub source
@@ -533,9 +540,14 @@ bundle update webpacker
533
540
  # overwrite your changes to the default install files and revert any unwanted changes from the install
534
541
  rails webpacker:install
535
542
 
543
+ # yarn 1 instructions
536
544
  yarn upgrade @rails/webpacker --latest
537
545
  yarn upgrade webpack-dev-server --latest
538
546
 
547
+ # yarn 2 instructions
548
+ yarn up @rails/webpacker@latest
549
+ yarn up webpack-dev-server@latest
550
+
539
551
  # Or to install the latest release (including pre-releases)
540
552
  yarn add @rails/webpacker@next
541
553
  ```
@@ -544,19 +556,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
544
556
 
545
557
  ## Paths
546
558
 
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.
559
+ 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
560
 
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`.
561
+ 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
562
 
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:
563
+ 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
564
 
561
565
  ```yml
562
566
  # config/webpacker.yml
@@ -574,16 +578,11 @@ development:
574
578
  port: 3035
575
579
  ```
576
580
 
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.
581
+ 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
582
 
581
583
  ### Additional paths
582
584
 
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
585
+ 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
586
  add additional paths that webpack should look up when resolving modules:
588
587
 
589
588
  ```yml
@@ -598,9 +597,10 @@ import 'stylesheets/main'
598
597
  import 'images/rails.png'
599
598
  ```
600
599
 
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
600
+ **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
601
+
602
+ **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.
603
+
604
604
 
605
605
  ## Deployment
606
606
 
@@ -610,16 +610,19 @@ When compiling assets for production on a remote server, such as a continuous in
610
610
 
611
611
  If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
612
612
 
613
+
613
614
  ## Troubleshooting
614
615
 
615
616
  See the doc page for [Troubleshooting](./docs/troubleshooting.md).
616
617
 
618
+
617
619
  ## Contributing
618
620
 
619
621
  [![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
620
622
 
621
623
  We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
622
624
 
625
+
623
626
  ## License
624
627
 
625
628
  Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).