webpacker 6.0.0.beta.5 → 6.0.0.rc.6
Sign up to get free protection for your applications and to get access to all the features.
- 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 +197 -167
- 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} +4 -9
- 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/babel.js +1 -1
- data/package/rules/file.js +1 -1
- data/package/rules/sass.js +1 -2
- data/package/rules/stylus.js +1 -1
- 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 +16 -0
- 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
|
[![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%
|
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
|
-
[
|
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,17 +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)
|
39
|
-
- [
|
47
|
+
- [Troubleshooting](#troubleshooting)
|
40
48
|
- [Contributing](#contributing)
|
41
49
|
- [License](#license)
|
42
50
|
|
@@ -46,12 +54,12 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
46
54
|
|
47
55
|
- Ruby 2.4+
|
48
56
|
- Rails 5.2+
|
49
|
-
- Node.js
|
50
|
-
- Yarn
|
57
|
+
- Node.js 12.13.0+ || 14+
|
58
|
+
- Yarn
|
51
59
|
|
52
60
|
## Features
|
53
61
|
|
54
|
-
- [
|
62
|
+
- [Webpack v5](https://webpack.js.org/)
|
55
63
|
- ES6 with [babel](https://babeljs.io/)
|
56
64
|
- Automatic code splitting using multiple entry points
|
57
65
|
- Asset compression, source-maps, and minification
|
@@ -70,46 +78,34 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
70
78
|
|
71
79
|
## Installation
|
72
80
|
|
73
|
-
You can
|
74
|
-
using new `--webpack` option:
|
81
|
+
You can configure a new Rails application with Webpacker right from the start using the `-j webpack` option:
|
75
82
|
|
76
83
|
```bash
|
77
|
-
|
78
|
-
rails new myapp --webpack
|
84
|
+
rails new myapp -j webpack
|
79
85
|
```
|
80
86
|
|
81
|
-
Or add it
|
87
|
+
Or you can add it later by changing your `Gemfile`:
|
82
88
|
|
83
89
|
```ruby
|
84
90
|
# Gemfile
|
85
|
-
gem 'webpacker', '~> 6.
|
91
|
+
gem 'webpacker', '~> 6.0'
|
86
92
|
|
87
93
|
# OR if you prefer to use master
|
88
94
|
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
|
89
95
|
yarn add https://github.com/rails/webpacker.git
|
90
96
|
```
|
91
97
|
|
92
|
-
|
98
|
+
Then running the following to install Webpacker:
|
93
99
|
|
94
100
|
```bash
|
95
|
-
bundle
|
96
|
-
|
97
|
-
|
98
|
-
# OR (on rails version < 5.0)
|
99
|
-
bundle exec rake webpacker:install
|
100
|
-
```
|
101
|
-
|
102
|
-
Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/webpacker/issues/1078),
|
103
|
-
|
104
|
-
```bash
|
105
|
-
yarn upgrade
|
101
|
+
./bin/bundle install
|
102
|
+
./bin/rails webpacker:install
|
106
103
|
```
|
107
104
|
|
108
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:
|
109
106
|
|
110
107
|
```bash
|
111
|
-
|
112
|
-
yarn
|
108
|
+
yarn install
|
113
109
|
```
|
114
110
|
|
115
111
|
### Usage
|
@@ -117,11 +113,10 @@ yarn
|
|
117
113
|
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
118
114
|
|
119
115
|
```yml
|
120
|
-
app/
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
│ └── application.css
|
116
|
+
app/javascript:
|
117
|
+
# Only Webpack entry files here
|
118
|
+
└── application.js
|
119
|
+
└── application.css
|
125
120
|
└── src:
|
126
121
|
│ └── my_component.js
|
127
122
|
└── stylesheets:
|
@@ -130,11 +125,40 @@ app/packs:
|
|
130
125
|
└── logo.svg
|
131
126
|
```
|
132
127
|
|
133
|
-
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 `
|
128
|
+
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`:
|
134
129
|
|
135
130
|
```erb
|
136
131
|
<%= javascript_pack_tag 'application' %>
|
137
|
-
<%=
|
132
|
+
<%= stylesheet_pack_tag 'application' %>
|
133
|
+
```
|
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' %>
|
138
162
|
```
|
139
163
|
|
140
164
|
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
@@ -165,71 +189,52 @@ If you want to use images in your stylesheets:
|
|
165
189
|
background-image: url('../images/logo.svg')
|
166
190
|
}
|
167
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)
|
168
196
|
|
169
|
-
Note, if you are using server-side rendering of JavaScript with dynamic code-
|
170
|
-
as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails)
|
171
|
-
your JavaScript should create the link prefetch HTML tags that you will use, so you won't
|
172
|
-
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.
|
173
198
|
|
174
|
-
**Note:** In order for your styles or static assets files to be available in your view,
|
175
|
-
you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know
|
176
|
-
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.
|
177
200
|
|
178
201
|
### Development
|
179
202
|
|
180
|
-
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
In development, Webpacker compiles on demand rather than upfront by default. This
|
186
|
-
happens when you refer to any of the pack assets using the Webpacker helper methods.
|
187
|
-
This means that you don't have to run any separate processes. Compilation errors are logged
|
188
|
-
to the standard Rails log. However, this auto-compilation happens when a web request
|
189
|
-
is made that requires an updated webpack build, not when files change. Thus, that can
|
190
|
-
be painfully slow for front-end development in this default way. Instead, you should either
|
191
|
-
run the `bin/webpack --watch` or run `./bin/webpack-dev-server`
|
192
|
-
|
193
|
-
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`.
|
194
|
-
Windows users will need to run these commands in a terminal separate from `bundle exec rails s`.
|
195
|
-
This process will watch for changes in the `app/packs/entrypoints/*.js` files and automatically
|
196
|
-
reload the browser to match. This feature is also known as
|
197
|
-
[Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
198
|
-
|
199
|
-
HMR is only the first step to running "Fast Refresh" with React. For more information
|
200
|
-
on how to configure rails/webpacker for Fast Refresh with React, see article
|
201
|
-
[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/).
|
202
208
|
|
203
209
|
```bash
|
204
210
|
# webpack dev server
|
205
211
|
./bin/webpack-dev-server
|
206
212
|
|
207
213
|
# watcher
|
208
|
-
./bin/webpack --watch --
|
214
|
+
./bin/webpack --watch --progress
|
209
215
|
|
210
216
|
# standalone build
|
211
|
-
./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
|
212
227
|
```
|
213
228
|
|
214
|
-
Once you start this webpack development server, Webpacker will automatically start proxying all
|
215
|
-
webpack asset requests to this server. When you stop this server, Rails will detect
|
216
|
-
that it's not running and Rails will revert back to on-demand compilation _if_ you have
|
217
|
-
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`
|
218
230
|
|
219
|
-
You can use environment variables as options supported by
|
220
|
-
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
|
221
|
-
form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
|
222
|
-
variables will always take precedence over the ones already set in the
|
223
|
-
configuration file, and that the _same_ environmental variables must
|
224
|
-
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.
|
225
232
|
|
226
233
|
```bash
|
227
234
|
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
|
228
235
|
```
|
229
236
|
|
230
|
-
By default, the webpack dev server listens on `localhost` in development for security purposes.
|
231
|
-
However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
|
232
|
-
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:
|
233
238
|
|
234
239
|
```bash
|
235
240
|
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
@@ -238,23 +243,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
238
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:
|
239
244
|
|
240
245
|
```ruby
|
241
|
-
|
242
|
-
|
243
|
-
|
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
|
244
249
|
```
|
245
250
|
|
246
251
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
247
252
|
|
253
|
+
|
248
254
|
### Webpack Configuration
|
249
255
|
|
250
|
-
Webpacker gives you a default set of configuration files for test, development and
|
251
|
-
production environments in `config/webpack/*.js`. You can configure each individual
|
252
|
-
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
|
253
257
|
`config/webpack/base.js` file.
|
254
258
|
|
255
|
-
By default, you don't need to make any changes to `config/webpack/*.js`
|
256
|
-
files since it's all standard production-ready configuration. However,
|
257
|
-
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.
|
258
260
|
|
259
261
|
Here is how you can modify webpack configuration:
|
260
262
|
|
@@ -285,8 +287,7 @@ const customConfig = require('./custom')
|
|
285
287
|
module.exports = merge(webpackConfig, customConfig)
|
286
288
|
```
|
287
289
|
|
288
|
-
If you need access to configs within Webpacker's configuration,
|
289
|
-
you can import them like so:
|
290
|
+
If you need access to configs within Webpacker's configuration, you can import them like so:
|
290
291
|
|
291
292
|
```js
|
292
293
|
// config/webpack/base.js
|
@@ -299,11 +300,51 @@ console.log(webpackConfig.source_path)
|
|
299
300
|
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
300
301
|
```
|
301
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
|
+
|
302
318
|
### Integrations
|
303
319
|
|
304
|
-
Webpacker out of the box supports JS and static assets (fonts, images etc.)
|
305
|
-
|
306
|
-
|
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
|
+
```
|
307
348
|
|
308
349
|
#### CoffeeScript
|
309
350
|
|
@@ -317,6 +358,12 @@ yarn add coffeescript coffee-loader
|
|
317
358
|
yarn add typescript @babel/preset-typescript
|
318
359
|
```
|
319
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
|
+
|
320
367
|
Add tsconfig.json
|
321
368
|
|
322
369
|
```json
|
@@ -341,15 +388,27 @@ Add tsconfig.json
|
|
341
388
|
}
|
342
389
|
```
|
343
390
|
|
391
|
+
Then modify the webpack config to use it as a plugin:
|
392
|
+
|
393
|
+
```js
|
394
|
+
// config/webpack/base.js
|
395
|
+
const { webpackConfig, merge } = require("@rails/webpacker");
|
396
|
+
const ForkTSCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
397
|
+
|
398
|
+
module.exports = merge(webpackConfig, {
|
399
|
+
plugins: [new ForkTSCheckerWebpackPlugin()],
|
400
|
+
});
|
401
|
+
```
|
402
|
+
|
344
403
|
#### CSS
|
345
404
|
|
346
405
|
To enable CSS support in your application, add following packages:
|
347
406
|
|
348
407
|
```bash
|
349
|
-
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
|
350
409
|
```
|
351
410
|
|
352
|
-
|
411
|
+
Optionally, add the `CSS` extension to webpack config for easy resolution.
|
353
412
|
|
354
413
|
```js
|
355
414
|
// config/webpack/base.js
|
@@ -363,13 +422,18 @@ const customConfig = {
|
|
363
422
|
module.exports = merge(webpackConfig, customConfig)
|
364
423
|
```
|
365
424
|
|
366
|
-
To enable
|
425
|
+
To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
|
367
426
|
then add the relevant pre-processors:
|
368
427
|
|
369
428
|
#### Postcss
|
370
429
|
|
371
430
|
```bash
|
372
|
-
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
|
373
437
|
```
|
374
438
|
|
375
439
|
#### Sass
|
@@ -390,39 +454,6 @@ yarn add less less-loader
|
|
390
454
|
yarn add stylus stylus-loader
|
391
455
|
```
|
392
456
|
|
393
|
-
#### React
|
394
|
-
|
395
|
-
React is supported and you just need to add relevant packages,
|
396
|
-
|
397
|
-
```bash
|
398
|
-
yarn add react react-dom @babel/preset-react
|
399
|
-
```
|
400
|
-
|
401
|
-
if you are using typescript, update your `tsconfig.json`
|
402
|
-
|
403
|
-
```json
|
404
|
-
{
|
405
|
-
"compilerOptions": {
|
406
|
-
"declaration": false,
|
407
|
-
"emitDecoratorMetadata": true,
|
408
|
-
"experimentalDecorators": true,
|
409
|
-
"lib": ["es6", "dom"],
|
410
|
-
"module": "es6",
|
411
|
-
"moduleResolution": "node",
|
412
|
-
"sourceMap": true,
|
413
|
-
"target": "es5",
|
414
|
-
"jsx": "react",
|
415
|
-
"noEmit": true
|
416
|
-
},
|
417
|
-
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
418
|
-
"compileOnSave": false
|
419
|
-
}
|
420
|
-
```
|
421
|
-
|
422
|
-
For more information on React props hydration and Server-Side Rendering (SSR), see the article
|
423
|
-
[Rails/Webpacker React Integration Options](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md)
|
424
|
-
in the [ShakaCode/react_on_rails](https://github.com/shakacode/react_on_rails) repo.
|
425
|
-
|
426
457
|
#### Other frameworks
|
427
458
|
|
428
459
|
Please follow webpack integration guide for relevant framework or library,
|
@@ -434,7 +465,7 @@ Please follow webpack integration guide for relevant framework or library,
|
|
434
465
|
For example to add Vue support:
|
435
466
|
```js
|
436
467
|
// config/webpack/rules/vue.js
|
437
|
-
const VueLoaderPlugin = require('vue-loader
|
468
|
+
const { VueLoaderPlugin } = require('vue-loader')
|
438
469
|
|
439
470
|
module.exports = {
|
440
471
|
module: {
|
@@ -445,7 +476,10 @@ module.exports = {
|
|
445
476
|
}
|
446
477
|
]
|
447
478
|
},
|
448
|
-
plugins: [new VueLoaderPlugin()]
|
479
|
+
plugins: [new VueLoaderPlugin()],
|
480
|
+
resolve: {
|
481
|
+
extensions: ['.vue']
|
482
|
+
}
|
449
483
|
}
|
450
484
|
```
|
451
485
|
|
@@ -454,9 +488,10 @@ module.exports = {
|
|
454
488
|
const { webpackConfig, merge } = require('@rails/webpacker')
|
455
489
|
const vueConfig = require('./rules/vue')
|
456
490
|
|
457
|
-
module.exports = merge(
|
491
|
+
module.exports = merge(vueConfig, webpackConfig)
|
458
492
|
```
|
459
493
|
|
494
|
+
|
460
495
|
### Custom Rails environments
|
461
496
|
|
462
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.
|
@@ -477,8 +512,7 @@ staging:
|
|
477
512
|
public_output_path: packs-staging
|
478
513
|
```
|
479
514
|
|
480
|
-
|
481
|
-
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`.
|
482
516
|
|
483
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:
|
484
518
|
|
@@ -486,15 +520,13 @@ For example, the below command will compile assets in production mode but will u
|
|
486
520
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
487
521
|
```
|
488
522
|
|
489
|
-
And, this will compile in development mode and load configuration for cucumber environment
|
490
|
-
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
|
491
524
|
|
492
525
|
```bash
|
493
526
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
494
527
|
```
|
495
528
|
|
496
|
-
Please note, binstubs compiles in development mode however rake tasks
|
497
|
-
compiles in production mode.
|
529
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
498
530
|
|
499
531
|
```bash
|
500
532
|
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
@@ -517,9 +549,14 @@ bundle update webpacker
|
|
517
549
|
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
518
550
|
rails webpacker:install
|
519
551
|
|
552
|
+
# yarn 1 instructions
|
520
553
|
yarn upgrade @rails/webpacker --latest
|
521
554
|
yarn upgrade webpack-dev-server --latest
|
522
555
|
|
556
|
+
# yarn 2 instructions
|
557
|
+
yarn up @rails/webpacker@latest
|
558
|
+
yarn up webpack-dev-server@latest
|
559
|
+
|
523
560
|
# Or to install the latest release (including pre-releases)
|
524
561
|
yarn add @rails/webpacker@next
|
525
562
|
```
|
@@ -528,19 +565,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
528
565
|
|
529
566
|
## Paths
|
530
567
|
|
531
|
-
By default, Webpacker ships with simple conventions for where the JavaScript
|
532
|
-
app files and compiled webpack bundles will go in your Rails app.
|
533
|
-
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.
|
534
569
|
|
535
|
-
The configuration for what webpack is supposed to compile by default rests
|
536
|
-
on the convention that every file in `app/packs/entrypoints/*`**(default)**
|
537
|
-
or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
|
538
|
-
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
|
539
|
-
an entry file. As a rule of thumb, put all files you want to link in your views inside
|
540
|
-
"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`.
|
541
571
|
|
542
|
-
Suppose you want to change the source directory from `app/packs`
|
543
|
-
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:
|
544
573
|
|
545
574
|
```yml
|
546
575
|
# config/webpacker.yml
|
@@ -558,17 +587,12 @@ development:
|
|
558
587
|
port: 3035
|
559
588
|
```
|
560
589
|
|
561
|
-
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output,
|
562
|
-
as you will want to configure your styles to be inlined in your JavaScript for hot reloading.
|
563
|
-
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.
|
564
591
|
|
565
592
|
### Additional paths
|
566
593
|
|
567
|
-
If you are adding Webpacker to an existing app that has most of the assets inside
|
568
|
-
|
569
|
-
with webpack modules, you can use the `additional_paths`
|
570
|
-
option available in `config/webpacker.yml`. This lets you
|
571
|
-
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:
|
572
596
|
|
573
597
|
```yml
|
574
598
|
additional_paths: ['app/assets', 'vendor/assets']
|
@@ -582,9 +606,10 @@ import 'stylesheets/main'
|
|
582
606
|
import 'images/rails.png'
|
583
607
|
```
|
584
608
|
|
585
|
-
**Note:** Please be careful when adding paths here otherwise it
|
586
|
-
|
587
|
-
|
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
|
+
|
588
613
|
|
589
614
|
## Deployment
|
590
615
|
|
@@ -592,16 +617,21 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
|
|
592
617
|
|
593
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.
|
594
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
|
+
|
595
623
|
## Troubleshooting
|
596
624
|
|
597
625
|
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
598
626
|
|
627
|
+
|
599
628
|
## Contributing
|
600
629
|
|
601
630
|
[![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
|
602
631
|
|
603
632
|
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
604
633
|
|
634
|
+
|
605
635
|
## License
|
606
636
|
|
607
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
|