webpacker 6.0.0.beta.6 → 6.0.0.rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/jest.yml +7 -15
- data/.github/workflows/js-lint.yml +7 -15
- data/.github/workflows/rubocop.yml +1 -1
- data/.github/workflows/ruby.yml +15 -40
- data/.node-version +1 -1
- data/.rubocop.yml +107 -1
- data/CHANGELOG.md +36 -13
- data/CONTRIBUTING.md +19 -0
- data/Gemfile.lock +105 -104
- data/README.md +183 -170
- data/config/webpacker.yml +1 -1
- data/docs/deployment.md +128 -0
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +57 -23
- data/docs/v6_upgrade.md +113 -0
- data/gemfiles/Gemfile-rails-edge +1 -1
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/{packs/entrypoints/application.js → application.js} +3 -8
- data/lib/install/bin/webpack +4 -7
- data/lib/install/bin/yarn +18 -0
- data/lib/install/config/webpacker.yml +19 -20
- data/lib/install/package.json +15 -0
- data/lib/install/template.rb +48 -19
- data/lib/tasks/webpacker/binstubs.rake +2 -2
- data/lib/tasks/webpacker/check_node.rake +3 -0
- data/lib/tasks/webpacker/check_yarn.rake +4 -1
- data/lib/tasks/webpacker/clobber.rake +1 -1
- data/lib/tasks/webpacker/install.rake +2 -2
- data/lib/tasks/webpacker/verify_config.rake +14 -0
- data/lib/tasks/webpacker/verify_install.rake +1 -11
- data/lib/tasks/yarn.rake +38 -0
- data/lib/webpacker/commands.rb +19 -15
- data/lib/webpacker/configuration.rb +19 -8
- data/lib/webpacker/dev_server.rb +6 -0
- data/lib/webpacker/dev_server_runner.rb +6 -3
- data/lib/webpacker/env.rb +5 -1
- data/lib/webpacker/helper.rb +14 -8
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +1 -2
- data/lib/webpacker/railtie.rb +8 -2
- data/lib/webpacker/runner.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +27 -7
- data/lib/webpacker.rb +1 -1
- data/package/__tests__/development.js +4 -11
- data/package/__tests__/env.js +8 -4
- data/package/babel/preset.js +0 -1
- data/package/config.js +3 -3
- data/package/env.js +6 -3
- data/package/environments/__tests__/base.js +3 -3
- data/package/environments/base.js +13 -13
- data/package/environments/development.js +36 -36
- data/package/environments/production.js +1 -1
- data/package/index.js +2 -0
- data/package/inliningCss.js +7 -0
- data/package/rules/file.js +1 -1
- data/package/rules/sass.js +1 -2
- data/package/utils/get_style_rule.js +4 -2
- data/package.json +25 -29
- data/test/command_test.rb +76 -0
- data/test/configuration_test.rb +1 -1
- data/test/dev_server_runner_test.rb +5 -2
- data/test/helper_test.rb +48 -34
- data/test/manifest_test.rb +10 -2
- data/test/mounted_app/test/dummy/config/webpacker.yml +2 -2
- data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/test/test_app/config/webpacker.yml +2 -4
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/public/packs/manifest.json +12 -5
- data/test/webpacker_test.rb +21 -0
- data/webpacker.gemspec +2 -2
- data/yarn.lock +2357 -3262
- metadata +24 -12
- data/6_0_upgrade.md +0 -62
data/README.md
CHANGED
@@ -5,17 +5,13 @@
|
|
5
5
|
[](https://github.com/rails/webpacker/actions)
|
6
6
|
[](https://github.com/rails/webpacker/actions)
|
7
7
|
|
8
|
-
[](https://www.npmjs.com/package/@rails/webpacker)
|
9
9
|
[](https://rubygems.org/gems/webpacker)
|
10
10
|
|
11
11
|
Webpacker makes it easy to use the JavaScript pre-processor and bundler
|
12
|
-
[
|
13
|
-
to manage application-like JavaScript in Rails. It
|
14
|
-
|
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.
|
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, 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
|
-
- [
|
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,12 +54,12 @@ 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
|
49
|
-
- Yarn
|
57
|
+
- Node.js 12.13.0+ || 14+
|
58
|
+
- Yarn
|
50
59
|
|
51
60
|
## Features
|
52
61
|
|
53
|
-
- [
|
62
|
+
- [Webpack v5](https://webpack.js.org/)
|
54
63
|
- ES6 with [babel](https://babeljs.io/)
|
55
64
|
- Automatic code splitting using multiple entry points
|
56
65
|
- Asset compression, source-maps, and minification
|
@@ -69,46 +78,34 @@ 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
|
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
|
-
|
77
|
-
rails new myapp --webpack
|
84
|
+
rails new myapp -j webpack
|
78
85
|
```
|
79
86
|
|
80
|
-
Or add it
|
87
|
+
Or you can add it later by changing your `Gemfile`:
|
81
88
|
|
82
89
|
```ruby
|
83
90
|
# Gemfile
|
84
|
-
gem 'webpacker', '~> 6.
|
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
|
-
|
98
|
+
Then running the following to install Webpacker:
|
92
99
|
|
93
100
|
```bash
|
94
|
-
bundle
|
95
|
-
|
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:
|
108
106
|
|
109
107
|
```bash
|
110
|
-
|
111
|
-
yarn
|
108
|
+
yarn install
|
112
109
|
```
|
113
110
|
|
114
111
|
### Usage
|
@@ -116,11 +113,10 @@ yarn
|
|
116
113
|
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
117
114
|
|
118
115
|
```yml
|
119
|
-
app/
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
│ └── application.css
|
116
|
+
app/javascript:
|
117
|
+
# Only Webpack entry files here
|
118
|
+
└── application.js
|
119
|
+
└── application.css
|
124
120
|
└── src:
|
125
121
|
│ └── my_component.js
|
126
122
|
└── stylesheets:
|
@@ -136,6 +132,35 @@ You can then link the JavaScript pack in Rails views using the `javascript_pack_
|
|
136
132
|
<%= stylesheet_pack_tag 'application' %>
|
137
133
|
```
|
138
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
|
+
|
139
164
|
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
140
165
|
```erb
|
141
166
|
<img src="<%= asset_pack_path 'images/logo.svg' %>" />
|
@@ -164,71 +189,52 @@ If you want to use images in your stylesheets:
|
|
164
189
|
background-image: url('../images/logo.svg')
|
165
190
|
}
|
166
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)
|
167
196
|
|
168
|
-
Note, if you are using server-side rendering of JavaScript with dynamic code-
|
169
|
-
as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails)
|
170
|
-
your JavaScript should create the link prefetch HTML tags that you will use, so you won't
|
171
|
-
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.
|
172
198
|
|
173
|
-
**Note:** In order for your styles or static assets files to be available in your view,
|
174
|
-
you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know
|
175
|
-
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.
|
176
200
|
|
177
201
|
### Development
|
178
202
|
|
179
|
-
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
In development, Webpacker compiles on demand rather than upfront by default. This
|
185
|
-
happens when you refer to any of the pack assets using the Webpacker helper methods.
|
186
|
-
This means that you don't have to run any separate processes. Compilation errors are logged
|
187
|
-
to the standard Rails log. However, this auto-compilation happens when a web request
|
188
|
-
is made that requires an updated webpack build, not when files change. Thus, that can
|
189
|
-
be painfully slow for front-end development in this default way. Instead, you should either
|
190
|
-
run the `bin/webpack --watch` or run `./bin/webpack-dev-server`
|
191
|
-
|
192
|
-
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`.
|
193
|
-
Windows users will need to run these commands in a terminal separate from `bundle exec rails s`.
|
194
|
-
This process will watch for changes in the `app/packs/entrypoints/*.js` files and automatically
|
195
|
-
reload the browser to match. This feature is also known as
|
196
|
-
[Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
197
|
-
|
198
|
-
HMR is only the first step to running "Fast Refresh" with React. For more information
|
199
|
-
on how to configure rails/webpacker for Fast Refresh with React, see article
|
200
|
-
[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/).
|
201
208
|
|
202
209
|
```bash
|
203
210
|
# webpack dev server
|
204
211
|
./bin/webpack-dev-server
|
205
212
|
|
206
213
|
# watcher
|
207
|
-
./bin/webpack --watch --
|
214
|
+
./bin/webpack --watch --progress
|
208
215
|
|
209
216
|
# standalone build
|
210
|
-
./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
|
211
227
|
```
|
212
228
|
|
213
|
-
Once you start this webpack development server, Webpacker will automatically start proxying all
|
214
|
-
webpack asset requests to this server. When you stop this server, Rails will detect
|
215
|
-
that it's not running and Rails will revert back to on-demand compilation _if_ you have
|
216
|
-
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`
|
217
230
|
|
218
|
-
You can use environment variables as options supported by
|
219
|
-
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
|
220
|
-
form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
|
221
|
-
variables will always take precedence over the ones already set in the
|
222
|
-
configuration file, and that the _same_ environmental variables must
|
223
|
-
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.
|
224
232
|
|
225
233
|
```bash
|
226
234
|
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
|
227
235
|
```
|
228
236
|
|
229
|
-
By default, the webpack dev server listens on `localhost` in development for security purposes.
|
230
|
-
However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
|
231
|
-
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:
|
232
238
|
|
233
239
|
```bash
|
234
240
|
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
@@ -237,23 +243,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
237
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:
|
238
244
|
|
239
245
|
```ruby
|
240
|
-
|
241
|
-
|
242
|
-
|
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
|
243
249
|
```
|
244
250
|
|
245
251
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
246
252
|
|
253
|
+
|
247
254
|
### Webpack Configuration
|
248
255
|
|
249
|
-
Webpacker gives you a default set of configuration files for test, development and
|
250
|
-
production environments in `config/webpack/*.js`. You can configure each individual
|
251
|
-
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
|
252
257
|
`config/webpack/base.js` file.
|
253
258
|
|
254
|
-
By default, you don't need to make any changes to `config/webpack/*.js`
|
255
|
-
files since it's all standard production-ready configuration. However,
|
256
|
-
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.
|
257
260
|
|
258
261
|
Here is how you can modify webpack configuration:
|
259
262
|
|
@@ -284,8 +287,7 @@ const customConfig = require('./custom')
|
|
284
287
|
module.exports = merge(webpackConfig, customConfig)
|
285
288
|
```
|
286
289
|
|
287
|
-
If you need access to configs within Webpacker's configuration,
|
288
|
-
you can import them like so:
|
290
|
+
If you need access to configs within Webpacker's configuration, you can import them like so:
|
289
291
|
|
290
292
|
```js
|
291
293
|
// config/webpack/base.js
|
@@ -298,11 +300,51 @@ console.log(webpackConfig.source_path)
|
|
298
300
|
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
299
301
|
```
|
300
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
|
+
|
301
318
|
### Integrations
|
302
319
|
|
303
|
-
Webpacker out of the box supports JS and static assets (fonts, images etc.)
|
304
|
-
|
305
|
-
|
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
|
+
```
|
306
348
|
|
307
349
|
#### CoffeeScript
|
308
350
|
|
@@ -316,6 +358,12 @@ yarn add coffeescript coffee-loader
|
|
316
358
|
yarn add typescript @babel/preset-typescript
|
317
359
|
```
|
318
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
|
+
|
319
367
|
Add tsconfig.json
|
320
368
|
|
321
369
|
```json
|
@@ -340,12 +388,6 @@ Add tsconfig.json
|
|
340
388
|
}
|
341
389
|
```
|
342
390
|
|
343
|
-
Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
|
344
|
-
|
345
|
-
```bash
|
346
|
-
yarn add fork-ts-checker-webpack-plugin
|
347
|
-
```
|
348
|
-
|
349
391
|
Then modify the webpack config to use it as a plugin:
|
350
392
|
|
351
393
|
```js
|
@@ -363,10 +405,10 @@ module.exports = merge(webpackConfig, {
|
|
363
405
|
To enable CSS support in your application, add following packages:
|
364
406
|
|
365
407
|
```bash
|
366
|
-
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
|
367
409
|
```
|
368
410
|
|
369
|
-
|
411
|
+
Optionally, add the `CSS` extension to webpack config for easy resolution.
|
370
412
|
|
371
413
|
```js
|
372
414
|
// config/webpack/base.js
|
@@ -380,13 +422,18 @@ const customConfig = {
|
|
380
422
|
module.exports = merge(webpackConfig, customConfig)
|
381
423
|
```
|
382
424
|
|
383
|
-
To enable
|
425
|
+
To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
|
384
426
|
then add the relevant pre-processors:
|
385
427
|
|
386
428
|
#### Postcss
|
387
429
|
|
388
430
|
```bash
|
389
|
-
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
|
390
437
|
```
|
391
438
|
|
392
439
|
#### Sass
|
@@ -407,39 +454,6 @@ yarn add less less-loader
|
|
407
454
|
yarn add stylus stylus-loader
|
408
455
|
```
|
409
456
|
|
410
|
-
#### React
|
411
|
-
|
412
|
-
React is supported and you just need to add relevant packages,
|
413
|
-
|
414
|
-
```bash
|
415
|
-
yarn add react react-dom @babel/preset-react
|
416
|
-
```
|
417
|
-
|
418
|
-
if you are using typescript, update your `tsconfig.json`
|
419
|
-
|
420
|
-
```json
|
421
|
-
{
|
422
|
-
"compilerOptions": {
|
423
|
-
"declaration": false,
|
424
|
-
"emitDecoratorMetadata": true,
|
425
|
-
"experimentalDecorators": true,
|
426
|
-
"lib": ["es6", "dom"],
|
427
|
-
"module": "es6",
|
428
|
-
"moduleResolution": "node",
|
429
|
-
"sourceMap": true,
|
430
|
-
"target": "es5",
|
431
|
-
"jsx": "react",
|
432
|
-
"noEmit": true
|
433
|
-
},
|
434
|
-
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
435
|
-
"compileOnSave": false
|
436
|
-
}
|
437
|
-
```
|
438
|
-
|
439
|
-
For more information on React props hydration and Server-Side Rendering (SSR), see the article
|
440
|
-
[Rails/Webpacker React Integration Options](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md)
|
441
|
-
in the [ShakaCode/react_on_rails](https://github.com/shakacode/react_on_rails) repo.
|
442
|
-
|
443
457
|
#### Other frameworks
|
444
458
|
|
445
459
|
Please follow webpack integration guide for relevant framework or library,
|
@@ -451,7 +465,7 @@ Please follow webpack integration guide for relevant framework or library,
|
|
451
465
|
For example to add Vue support:
|
452
466
|
```js
|
453
467
|
// config/webpack/rules/vue.js
|
454
|
-
const VueLoaderPlugin = require('vue-loader
|
468
|
+
const { VueLoaderPlugin } = require('vue-loader')
|
455
469
|
|
456
470
|
module.exports = {
|
457
471
|
module: {
|
@@ -462,7 +476,10 @@ module.exports = {
|
|
462
476
|
}
|
463
477
|
]
|
464
478
|
},
|
465
|
-
plugins: [new VueLoaderPlugin()]
|
479
|
+
plugins: [new VueLoaderPlugin()],
|
480
|
+
resolve: {
|
481
|
+
extensions: ['.vue']
|
482
|
+
}
|
466
483
|
}
|
467
484
|
```
|
468
485
|
|
@@ -471,9 +488,10 @@ module.exports = {
|
|
471
488
|
const { webpackConfig, merge } = require('@rails/webpacker')
|
472
489
|
const vueConfig = require('./rules/vue')
|
473
490
|
|
474
|
-
module.exports = merge(
|
491
|
+
module.exports = merge(vueConfig, webpackConfig)
|
475
492
|
```
|
476
493
|
|
494
|
+
|
477
495
|
### Custom Rails environments
|
478
496
|
|
479
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.
|
@@ -494,8 +512,7 @@ staging:
|
|
494
512
|
public_output_path: packs-staging
|
495
513
|
```
|
496
514
|
|
497
|
-
|
498
|
-
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`.
|
499
516
|
|
500
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:
|
501
518
|
|
@@ -503,15 +520,13 @@ For example, the below command will compile assets in production mode but will u
|
|
503
520
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
504
521
|
```
|
505
522
|
|
506
|
-
And, this will compile in development mode and load configuration for cucumber environment
|
507
|
-
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
|
508
524
|
|
509
525
|
```bash
|
510
526
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
511
527
|
```
|
512
528
|
|
513
|
-
Please note, binstubs compiles in development mode however rake tasks
|
514
|
-
compiles in production mode.
|
529
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
515
530
|
|
516
531
|
```bash
|
517
532
|
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
@@ -534,9 +549,14 @@ bundle update webpacker
|
|
534
549
|
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
535
550
|
rails webpacker:install
|
536
551
|
|
552
|
+
# yarn 1 instructions
|
537
553
|
yarn upgrade @rails/webpacker --latest
|
538
554
|
yarn upgrade webpack-dev-server --latest
|
539
555
|
|
556
|
+
# yarn 2 instructions
|
557
|
+
yarn up @rails/webpacker@latest
|
558
|
+
yarn up webpack-dev-server@latest
|
559
|
+
|
540
560
|
# Or to install the latest release (including pre-releases)
|
541
561
|
yarn add @rails/webpacker@next
|
542
562
|
```
|
@@ -545,19 +565,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
545
565
|
|
546
566
|
## Paths
|
547
567
|
|
548
|
-
By default, Webpacker ships with simple conventions for where the JavaScript
|
549
|
-
app files and compiled webpack bundles will go in your Rails app.
|
550
|
-
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.
|
551
569
|
|
552
|
-
The configuration for what webpack is supposed to compile by default rests
|
553
|
-
on the convention that every file in `app/packs/entrypoints/*`**(default)**
|
554
|
-
or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
|
555
|
-
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
|
556
|
-
an entry file. As a rule of thumb, put all files you want to link in your views inside
|
557
|
-
"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`.
|
558
571
|
|
559
|
-
Suppose you want to change the source directory from `app/packs`
|
560
|
-
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:
|
561
573
|
|
562
574
|
```yml
|
563
575
|
# config/webpacker.yml
|
@@ -575,17 +587,12 @@ development:
|
|
575
587
|
port: 3035
|
576
588
|
```
|
577
589
|
|
578
|
-
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output,
|
579
|
-
as you will want to configure your styles to be inlined in your JavaScript for hot reloading.
|
580
|
-
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.
|
581
591
|
|
582
592
|
### Additional paths
|
583
593
|
|
584
|
-
If you are adding Webpacker to an existing app that has most of the assets inside
|
585
|
-
|
586
|
-
with webpack modules, you can use the `additional_paths`
|
587
|
-
option available in `config/webpacker.yml`. This lets you
|
588
|
-
add additional paths that webpack should lookup when resolving modules:
|
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
|
595
|
+
add additional paths that webpack should look up when resolving modules:
|
589
596
|
|
590
597
|
```yml
|
591
598
|
additional_paths: ['app/assets', 'vendor/assets']
|
@@ -599,9 +606,10 @@ import 'stylesheets/main'
|
|
599
606
|
import 'images/rails.png'
|
600
607
|
```
|
601
608
|
|
602
|
-
**Note:** Please be careful when adding paths here otherwise it
|
603
|
-
|
604
|
-
|
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
|
+
|
605
613
|
|
606
614
|
## Deployment
|
607
615
|
|
@@ -609,16 +617,21 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
|
|
609
617
|
|
610
618
|
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.
|
611
619
|
|
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.
|
621
|
+
|
622
|
+
|
612
623
|
## Troubleshooting
|
613
624
|
|
614
625
|
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
615
626
|
|
627
|
+
|
616
628
|
## Contributing
|
617
629
|
|
618
630
|
[](https://www.codetriage.com/rails/webpacker)
|
619
631
|
|
620
632
|
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
621
633
|
|
634
|
+
|
622
635
|
## License
|
623
636
|
|
624
637
|
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
data/config/webpacker.yml
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
../lib/install/config/webpacker.yml
|