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/README.md CHANGED
@@ -1,19 +1,19 @@
1
1
  # Webpacker
2
2
 
3
- [![Build Status](https://travis-ci.org/rails/webpacker.svg?branch=master)](https://travis-ci.org/rails/webpacker)
4
- [![node.js](https://img.shields.io/badge/node-%3E%3D%2010.17.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
3
+ [![Ruby specs](https://github.com/rails/webpacker/workflows/Ruby%20specs/badge.svg)](https://github.com/rails/webpacker/actions)
4
+ [![Jest specs](https://github.com/rails/webpacker/workflows/Jest%20specs/badge.svg)](https://github.com/rails/webpacker/actions)
5
+ [![Rubocop](https://github.com/rails/webpacker/workflows/Rubocop/badge.svg)](https://github.com/rails/webpacker/actions)
6
+ [![JS lint](https://github.com/rails/webpacker/workflows/JS%20lint/badge.svg)](https://github.com/rails/webpacker/actions)
7
+
8
+ [![node.js](https://img.shields.io/badge/node-%3E%3D%2012.0.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
5
9
  [![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://rubygems.org/gems/webpacker)
6
10
 
7
11
  Webpacker makes it easy to use the JavaScript pre-processor and bundler
8
- [webpack 4.x.x+](https://webpack.js.org/)
9
- to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
10
- as the primary purpose for webpack is app-like JavaScript, not images, CSS, or
11
- even JavaScript Sprinkles (that all continues to live in app/assets).
12
-
13
- However, it is possible to use Webpacker for CSS, images and fonts assets as well,
14
- in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.
12
+ [Webpack v5](https://webpack.js.org/)
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.
15
15
 
16
- **NOTE:** The master branch now hosts the code for v5.x.x. Please refer to [4-x-stable](https://github.com/rails/webpacker/tree/4-x-stable) branch for 4.x documentation.
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.
17
17
 
18
18
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
19
19
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
@@ -22,27 +22,29 @@ in which case you may not even need the asset pipeline. This is mostly relevant
22
22
 
23
23
  - [Prerequisites](#prerequisites)
24
24
  - [Features](#features)
25
+ - [Optional support](#optional-support)
25
26
  - [Installation](#installation)
26
27
  - [Usage](#usage)
28
+ - [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
27
29
  - [Development](#development)
28
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)
29
42
  - [Custom Rails environments](#custom-rails-environments)
30
43
  - [Upgrading](#upgrading)
31
- - [Integrations](#integrations)
32
- - [React](./docs/integrations.md#react)
33
- - [Angular with TypeScript](./docs/integrations.md#angular-with-typescript)
34
- - [Vue](./docs/integrations.md#vue)
35
- - [Elm](./docs/integrations.md#elm)
36
- - [Stimulus](./docs/integrations.md#stimulus)
37
- - [Svelte](./docs/integrations.md#svelte)
38
- - [Typescript](./docs/typescript.md)
39
- - [CoffeeScript](./docs/integrations.md#coffeescript)
40
- - [Erb](./docs/integrations.md#erb)
41
44
  - [Paths](#paths)
42
- - [Resolved](#resolved)
43
- - [Watched](#watched)
45
+ - [Additional paths](#additional-paths)
44
46
  - [Deployment](#deployment)
45
- - [Docs](#docs)
47
+ - [Troubleshooting](#troubleshooting)
46
48
  - [Contributing](#contributing)
47
49
  - [License](#license)
48
50
 
@@ -52,59 +54,52 @@ in which case you may not even need the asset pipeline. This is mostly relevant
52
54
 
53
55
  - Ruby 2.4+
54
56
  - Rails 5.2+
55
- - Node.js 10.17.0+
56
- - Yarn 1.x+
57
+ - Node.js 12.13.0+ || 14+
58
+ - Yarn
57
59
 
58
60
  ## Features
59
61
 
60
- - [webpack 4.x.x](https://webpack.js.org/)
62
+ - [Webpack v5](https://webpack.js.org/)
61
63
  - ES6 with [babel](https://babeljs.io/)
62
64
  - Automatic code splitting using multiple entry points
63
- - Stylesheets - Sass and CSS
64
- - Images and fonts
65
- - PostCSS - Auto-Prefixer
66
65
  - Asset compression, source-maps, and minification
67
66
  - CDN support
68
- - React, Angular, Elm and Vue support out-of-the-box
69
67
  - Rails view helpers
70
68
  - Extensible and configurable
71
69
 
70
+ ### Optional support
71
+
72
+ _requires extra packages to be installed_
73
+
74
+ - Stylesheets - Sass, Less, Stylus and Css, PostCSS
75
+ - CoffeeScript
76
+ - TypeScript
77
+ - React
78
+
72
79
  ## Installation
73
80
 
74
- You can either add Webpacker during setup of a new Rails 5.1+ application
75
- using new `--webpack` option:
81
+ You can configure a new Rails application with Webpacker right from the start using the `--webpack` option:
76
82
 
77
83
  ```bash
78
- # Available Rails 5.1+
79
84
  rails new myapp --webpack
80
85
  ```
81
86
 
82
- Or add it to your `Gemfile`:
87
+ Or you can add it later by changing your `Gemfile`:
83
88
 
84
89
  ```ruby
85
90
  # Gemfile
86
- gem 'webpacker', '~> 5.x'
91
+ gem 'webpacker', '~> 6.0'
87
92
 
88
93
  # OR if you prefer to use master
89
94
  gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
90
95
  yarn add https://github.com/rails/webpacker.git
91
- yarn add core-js regenerator-runtime
92
- ```
93
-
94
- Finally, run the following to install Webpacker:
95
-
96
- ```bash
97
- bundle
98
- bundle exec rails webpacker:install
99
-
100
- # OR (on rails version < 5.0)
101
- bundle exec rake webpacker:install
102
96
  ```
103
97
 
104
- Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/webpacker/issues/1078),
98
+ Then running the following to install Webpacker:
105
99
 
106
100
  ```bash
107
- yarn upgrade
101
+ ./bin/bundle install
102
+ ./bin/rails webpacker:install
108
103
  ```
109
104
 
110
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:
@@ -118,9 +113,9 @@ yarn install
118
113
  Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
119
114
 
120
115
  ```yml
121
- app/javascript:
122
- ├── packs:
123
- │ # only webpack entry files here
116
+ app/packs:
117
+ ├── entrypoints:
118
+ │ # Only Webpack entry files here
124
119
  │ └── application.js
125
120
  │ └── application.css
126
121
  └── src:
@@ -131,34 +126,20 @@ app/javascript:
131
126
  └── logo.svg
132
127
  ```
133
128
 
134
- In `/packs/application.js`, include this at the top of the file:
135
-
136
- ```js
137
- import 'core-js/stable'
138
- import 'regenerator-runtime/runtime'
139
- ```
140
-
141
- You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper.
142
- If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
129
+ You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
143
130
 
144
131
  ```erb
145
132
  <%= javascript_pack_tag 'application' %>
146
133
  <%= stylesheet_pack_tag 'application' %>
147
134
  ```
148
135
 
149
- If you want to link a static asset for `<link rel="prefetch">` or `<img />` tag, you
150
- can use the `asset_pack_path` helper:
151
-
152
- ```erb
153
- <link rel="prefetch" href="<%= asset_pack_path 'application.css' %>" />
154
- <img src="<%= asset_pack_path 'images/logo.svg' %>" />
155
- ```
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.
156
138
 
157
- If you are using new webpack 4 split chunks API, then consider using `javascript_packs_with_chunks_tag` helper, which creates html
158
- tags for a pack and all the dependent chunks.
139
+ The result looks like this:
159
140
 
160
141
  ```erb
161
- <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
142
+ <%= javascript_pack_tag 'calendar', 'map' %>
162
143
 
163
144
  <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
164
145
  <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
@@ -167,36 +148,63 @@ tags for a pack and all the dependent chunks.
167
148
  <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
168
149
  ```
169
150
 
170
- **Important:** Pass all your pack names when using `javascript_packs_with_chunks_tag`
171
- helper otherwise you will get duplicated chunks on the page.
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.
172
152
 
173
153
  ```erb
174
154
  <%# DO %>
175
- <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
155
+ <%= javascript_pack_tag 'calendar', 'map' %>
156
+ <%= stylesheet_pack_tag 'calendar', 'map' %>
176
157
 
177
158
  <%# DON'T %>
178
- <%= javascript_packs_with_chunks_tag 'calendar' %>
179
- <%= javascript_packs_with_chunks_tag 'map' %>
159
+ <%= javascript_pack_tag 'calendar' %>
160
+ <%= javascript_pack_tag 'map' %>
161
+ <%= stylesheet_pack_tag 'calendar' %>
162
+ <%= stylesheet_pack_tag 'map' %>
180
163
  ```
181
164
 
182
- **Note:** In order for your styles or static assets files to be available in your view,
183
- you would need to link them in your "pack" or entry file.
165
+ If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
166
+ ```erb
167
+ <img src="<%= asset_pack_path 'images/logo.svg' %>" />
168
+ ```
169
+
170
+ Or use the dedicated helper:
171
+ ```erb
172
+ <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
173
+ <%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
174
+ ```
175
+
176
+ If you want to create a favicon:
177
+ ```erb
178
+ <%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
179
+ ```
180
+
181
+ If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
182
+ ```erb
183
+ <%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
184
+ ```
185
+
186
+ If you want to use images in your stylesheets:
187
+
188
+ ```css
189
+ .foo {
190
+ background-image: url('../images/logo.svg')
191
+ }
192
+ ```
193
+
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.
199
+
184
200
 
185
201
  ### Development
186
202
 
187
- Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
188
- Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js`
189
- executables to ensure that the right configuration files and environmental variables
190
- are loaded based on your environment.
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.
191
204
 
192
- In development, Webpacker compiles on demand rather than upfront by default. This
193
- happens when you refer to any of the pack assets using the Webpacker helper methods.
194
- This means that you don't have to run any separate processes. Compilation errors are logged
195
- to the standard Rails log.
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`
196
206
 
197
- 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
198
- in a terminal separate from `bundle exec rails s`. This process will watch for changes
199
- in the `app/javascript/packs/*.js` files and automatically reload the browser to match.
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,24 +217,15 @@ in the `app/javascript/packs/*.js` files and automatically reload the browser to
209
217
  ./bin/webpack
210
218
  ```
211
219
 
212
- Once you start this development server, Webpacker will automatically start proxying all
213
- webpack asset requests to this server. When you stop the server, it'll revert back to
214
- on-demand compilation.
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`
215
221
 
216
- You can use environment variables as options supported by
217
- [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
218
- form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
219
- variables will always take precedence over the ones already set in the
220
- configuration file, and that the _same_ environmental variables must
221
- 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.
222
223
 
223
224
  ```bash
224
225
  WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
225
226
  ```
226
227
 
227
- By default, the webpack dev server listens on `localhost` in development for security purposes.
228
- However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
229
- 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:
230
229
 
231
230
  ```bash
232
231
  WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
@@ -235,16 +234,254 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
235
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:
236
235
 
237
236
  ```ruby
238
- Rails.application.config.content_security_policy do |policy|
239
- policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
240
- 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
241
240
  ```
242
241
 
243
242
  **Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
244
243
 
244
+
245
245
  ### Webpack Configuration
246
246
 
247
- See [docs/webpack](docs/webpack.md) for modifying webpack configuration and loaders.
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
248
+ `config/webpack/base.js` file.
249
+
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.
251
+
252
+ Here is how you can modify webpack configuration:
253
+
254
+ You might add separate files to keep your code more organized.
255
+
256
+ ```js
257
+ // config/webpack/custom.js
258
+ module.exports = {
259
+ resolve: {
260
+ alias: {
261
+ jquery: 'jquery/src/jquery',
262
+ vue: 'vue/dist/vue.js',
263
+ React: 'react',
264
+ ReactDOM: 'react-dom',
265
+ vue_resource: 'vue-resource/dist/vue-resource'
266
+ }
267
+ }
268
+ }
269
+ ```
270
+
271
+ Then `require` this file in your `config/webpack/base.js`:
272
+
273
+ ```js
274
+ // config/webpack/base.js
275
+ const { webpackConfig, merge } = require('@rails/webpacker')
276
+ const customConfig = require('./custom')
277
+
278
+ module.exports = merge(webpackConfig, customConfig)
279
+ ```
280
+
281
+ If you need access to configs within Webpacker's configuration, you can import them like so:
282
+
283
+ ```js
284
+ // config/webpack/base.js
285
+ const { webpackConfig } = require('@rails/webpacker')
286
+
287
+ console.log(webpackConfig.output_path)
288
+ console.log(webpackConfig.source_path)
289
+
290
+ // Or to print out your whole webpack configuration
291
+ console.log(JSON.stringify(webpackConfig, undefined, 2))
292
+ ```
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
+
309
+ ### Integrations
310
+
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
+ ```
339
+
340
+ #### CoffeeScript
341
+
342
+ ```bash
343
+ yarn add coffeescript coffee-loader
344
+ ```
345
+
346
+ #### TypeScript
347
+
348
+ ```bash
349
+ yarn add typescript @babel/preset-typescript
350
+ ```
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
+
358
+ Add tsconfig.json
359
+
360
+ ```json
361
+ {
362
+ "compilerOptions": {
363
+ "declaration": false,
364
+ "emitDecoratorMetadata": true,
365
+ "experimentalDecorators": true,
366
+ "lib": ["es6", "dom"],
367
+ "module": "es6",
368
+ "moduleResolution": "node",
369
+ "baseUrl": ".",
370
+ "paths": {
371
+ "*": ["node_modules/*", "app/packs/*"]
372
+ },
373
+ "sourceMap": true,
374
+ "target": "es5",
375
+ "noEmit": true
376
+ },
377
+ "exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
378
+ "compileOnSave": false
379
+ }
380
+ ```
381
+
382
+ Then modify the webpack config to use it as a plugin:
383
+
384
+ ```js
385
+ // config/webpack/base.js
386
+ const { webpackConfig, merge } = require("@rails/webpacker");
387
+ const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
388
+
389
+ module.exports = merge(webpackConfig, {
390
+ plugins: [new ForkTSCheckerWebpackPlugin()],
391
+ });
392
+ ```
393
+
394
+ #### CSS
395
+
396
+ To enable CSS support in your application, add following packages:
397
+
398
+ ```bash
399
+ yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
400
+ ```
401
+
402
+ Optionally, add the `CSS` extension to webpack config for easy resolution.
403
+
404
+ ```js
405
+ // config/webpack/base.js
406
+ const { webpackConfig, merge } = require('@rails/webpacker')
407
+ const customConfig = {
408
+ resolve: {
409
+ extensions: ['.css']
410
+ }
411
+ }
412
+
413
+ module.exports = merge(webpackConfig, customConfig)
414
+ ```
415
+
416
+ To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
417
+ then add the relevant pre-processors:
418
+
419
+ #### Postcss
420
+
421
+ ```bash
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
428
+ ```
429
+
430
+ #### Sass
431
+
432
+ ```bash
433
+ yarn add sass sass-loader
434
+ ```
435
+
436
+ #### Less
437
+
438
+ ```bash
439
+ yarn add less less-loader
440
+ ```
441
+
442
+ #### Stylus
443
+
444
+ ```bash
445
+ yarn add stylus stylus-loader
446
+ ```
447
+
448
+ #### Other frameworks
449
+
450
+ Please follow webpack integration guide for relevant framework or library,
451
+
452
+ 1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
453
+ 2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
454
+ 3. [Vue](https://vue-loader.vuejs.org/guide/)
455
+
456
+ For example to add Vue support:
457
+ ```js
458
+ // config/webpack/rules/vue.js
459
+ const { VueLoaderPlugin } = require('vue-loader')
460
+
461
+ module.exports = {
462
+ module: {
463
+ rules: [
464
+ {
465
+ test: /\.vue$/,
466
+ loader: 'vue-loader'
467
+ }
468
+ ]
469
+ },
470
+ plugins: [new VueLoaderPlugin()],
471
+ resolve: {
472
+ extensions: ['.vue']
473
+ }
474
+ }
475
+ ```
476
+
477
+ ```js
478
+ // config/webpack/base.js
479
+ const { webpackConfig, merge } = require('@rails/webpacker')
480
+ const vueConfig = require('./rules/vue')
481
+
482
+ module.exports = merge(vueConfig, webpackConfig)
483
+ ```
484
+
248
485
 
249
486
  ### Custom Rails environments
250
487
 
@@ -266,8 +503,7 @@ staging:
266
503
  public_output_path: packs-staging
267
504
  ```
268
505
 
269
- 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`.
270
- 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`.
271
507
 
272
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:
273
509
 
@@ -275,22 +511,20 @@ For example, the below command will compile assets in production mode but will u
275
511
  RAILS_ENV=staging bundle exec rails assets:precompile
276
512
  ```
277
513
 
278
- And, this will compile in development mode and load configuration for cucumber environment
279
- 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
280
515
 
281
516
  ```bash
282
517
  RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
283
518
  ```
284
519
 
285
- Please note, binstubs compiles in development mode however rake tasks
286
- compiles in production mode.
520
+ Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
287
521
 
288
522
  ```bash
289
- # Compiles in development mode unless NODE_ENV is specified
523
+ # Compiles in development mode unless NODE_ENV is specified, per the binstub source
290
524
  ./bin/webpack
291
525
  ./bin/webpack-dev-server
292
526
 
293
- # compiles in production mode by default unless NODE_ENV is specified
527
+ # Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
294
528
  bundle exec rails assets:precompile
295
529
  bundle exec rails webpacker:compile
296
530
  ```
@@ -300,53 +534,37 @@ bundle exec rails webpacker:compile
300
534
  You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
301
535
 
302
536
  ```bash
537
+ # check your Gemfile for version restrictions
303
538
  bundle update webpacker
304
- rails webpacker:binstubs
539
+
540
+ # overwrite your changes to the default install files and revert any unwanted changes from the install
541
+ rails webpacker:install
542
+
543
+ # yarn 1 instructions
305
544
  yarn upgrade @rails/webpacker --latest
306
545
  yarn upgrade webpack-dev-server --latest
307
546
 
547
+ # yarn 2 instructions
548
+ yarn up @rails/webpacker@latest
549
+ yarn up webpack-dev-server@latest
550
+
308
551
  # Or to install the latest release (including pre-releases)
309
552
  yarn add @rails/webpacker@next
310
553
  ```
311
554
 
312
- ## Integrations
313
-
314
- Webpacker ships with basic out-of-the-box integration. You can see a list of available commands/tasks by running `bundle exec rails webpacker`.
315
-
316
- Included install integrations:
317
-
318
- - [React](./docs/integrations.md#React)
319
- - [Angular with TypeScript](./docs/integrations.md#Angular-with-TypeScript)
320
- - [Vue](./docs/integrations.md#Vue)
321
- - [Elm](./docs/integrations.md#Elm)
322
- - [Svelte](./docs/integrations.md#Svelte)
323
- - [Stimulus](./docs/integrations.md#Stimulus)
324
- - [CoffeeScript](./docs/integrations.md#CoffeeScript)
325
- - [Typescript](./docs/typescript.md)
326
- - [Erb](./docs/integrations.md#Erb)
327
-
328
- See [Integrations](./docs/integrations.md) for further details.
555
+ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
329
556
 
330
557
  ## Paths
331
558
 
332
- By default, Webpacker ships with simple conventions for where the JavaScript
333
- app files and compiled webpack bundles will go in your Rails app.
334
- 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.
335
560
 
336
- The configuration for what webpack is supposed to compile by default rests
337
- on the convention that every file in `app/javascript/packs/*`**(default)**
338
- or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
339
- 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
340
- an entry file. As a rule of thumb, put all files you want to link in your views inside
341
- "packs" directory and keep everything else under `app/javascript`.
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`.
342
562
 
343
- Suppose you want to change the source directory from `app/javascript`
344
- 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:
345
564
 
346
565
  ```yml
347
566
  # config/webpacker.yml
348
- source_path: frontend
349
- source_entry_path: packs
567
+ source_path: frontend # packs are in frontend/packs
350
568
  public_output_path: assets/packs # outputs to => public/assets/packs
351
569
  ```
352
570
 
@@ -362,16 +580,13 @@ development:
362
580
 
363
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.
364
582
 
365
- ### Resolved
583
+ ### Additional paths
366
584
 
367
- If you are adding Webpacker to an existing app that has most of the assets inside
368
- `app/assets` or inside an engine, and you want to share that
369
- with webpack modules, you can use the `additional_paths`
370
- option available in `config/webpacker.yml`. This lets you
371
- add additional paths that webpack should lookup when resolving modules:
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
586
+ add additional paths that webpack should look up when resolving modules:
372
587
 
373
588
  ```yml
374
- additional_paths: ['app/assets']
589
+ additional_paths: ['app/assets', 'vendor/assets']
375
590
  ```
376
591
 
377
592
  You can then import these items inside your modules like so:
@@ -382,9 +597,10 @@ import 'stylesheets/main'
382
597
  import 'images/rails.png'
383
598
  ```
384
599
 
385
- **Note:** Please be careful when adding paths here otherwise it
386
- will make the compilation slow, consider adding specific paths instead of
387
- 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
+
388
604
 
389
605
  ## Deployment
390
606
 
@@ -392,25 +608,13 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
392
608
 
393
609
  When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use `yarn install --frozen-lockfile` to install NPM packages on the remote host to ensure that the installed packages match the `yarn.lock` file.
394
610
 
395
- ## Docs
396
-
397
- - [Development](https://github.com/rails/webpacker#development)
398
- - [Webpack](./docs/webpack.md)
399
- - [Webpack-dev-server](./docs/webpack-dev-server.md)
400
- - [Environment Variables](./docs/env.md)
401
- - [Folder Structure](./docs/folder-structure.md)
402
- - [Assets](./docs/assets.md) - [CSS, Sass and SCSS](./docs/css.md) - [ES6](./docs/es6.md), [Target browsers](./docs/target.md)
403
- - [Props](./docs/props.md)
404
- - [Typescript](./docs/typescript.md)
405
- - [Yarn](./docs/yarn.md)
406
- - [Misc](./docs/misc.md)
407
- - [Deployment](./docs/deployment.md)
408
- - [Docker](./docs/docker.md)
409
- - [Using in Rails engines](./docs/engines.md)
410
- - [Webpacker on Cloud9](./docs/cloud9.md)
411
- - [Testing](./docs/testing.md)
412
- - [Troubleshooting](./docs/troubleshooting.md)
413
- - [v3 to v4 Upgrade Guide](./docs/v4-upgrade.md)
611
+ If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
612
+
613
+
614
+ ## Troubleshooting
615
+
616
+ See the doc page for [Troubleshooting](./docs/troubleshooting.md).
617
+
414
618
 
415
619
  ## Contributing
416
620
 
@@ -418,6 +622,7 @@ When compiling assets for production on a remote server, such as a continuous in
418
622
 
419
623
  We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
420
624
 
625
+
421
626
  ## License
422
627
 
423
628
  Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).