webpacker 6.0.0.pre.1 → 6.0.0.rc.3
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 +5 -2
- data/.github/workflows/js-lint.yml +5 -2
- data/.github/workflows/rubocop.yml +1 -1
- data/.github/workflows/ruby.yml +17 -14
- data/.node-version +1 -1
- data/.rubocop.yml +106 -0
- data/CHANGELOG.md +36 -9
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +107 -104
- data/README.md +370 -169
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/deployment.md +9 -29
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +57 -25
- data/docs/v6_upgrade.md +75 -0
- data/gemfiles/Gemfile-rails-edge +1 -1
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/bin/yarn +18 -0
- data/lib/install/config/webpacker.yml +17 -18
- data/lib/install/package.json +17 -0
- data/lib/install/{javascript/packs → packs/entrypoints}/application.js +4 -3
- data/lib/install/template.rb +41 -21
- 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 +36 -0
- data/lib/webpacker/commands.rb +21 -16
- data/lib/webpacker/compiler.rb +9 -3
- data/lib/webpacker/configuration.rb +19 -8
- data/lib/webpacker/dev_server.rb +6 -0
- data/lib/webpacker/dev_server_runner.rb +7 -2
- data/lib/webpacker/env.rb +5 -1
- data/lib/webpacker/helper.rb +26 -50
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +1 -2
- data/lib/webpacker/railtie.rb +1 -2
- data/lib/webpacker/runner.rb +1 -1
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +1 -0
- data/lib/webpacker.rb +1 -1
- data/package/__tests__/development.js +3 -2
- data/package/__tests__/env.js +8 -4
- data/package/__tests__/index.js +9 -0
- data/package/babel/preset.js +24 -14
- data/package/config.js +3 -3
- data/package/env.js +9 -3
- data/package/environments/__tests__/base.js +7 -7
- data/package/environments/base.js +25 -25
- data/package/environments/development.js +37 -32
- data/package/environments/production.js +28 -30
- data/package/index.js +9 -2
- data/package/inliningCss.js +7 -0
- data/package/rules/babel.js +1 -1
- data/package/rules/coffee.js +5 -5
- data/package/rules/erb.js +5 -3
- data/package/rules/file.js +5 -3
- data/package/rules/index.js +9 -17
- data/package/rules/less.js +14 -10
- data/package/rules/raw.js +5 -0
- data/package/rules/sass.js +12 -9
- data/package/rules/stylus.js +26 -0
- data/package/utils/get_style_rule.js +28 -30
- data/package/utils/helpers.js +25 -0
- data/package.json +25 -29
- data/test/command_test.rb +76 -0
- data/test/configuration_test.rb +3 -3
- data/test/dev_server_runner_test.rb +13 -2
- data/test/helper_test.rb +69 -61
- data/test/manifest_test.rb +16 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +4 -4
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
- data/test/test_app/config/webpacker.yml +4 -6
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/public/packs/manifest.json +19 -5
- data/test/webpacker_test.rb +17 -0
- data/webpacker.gemspec +1 -1
- data/yarn.lock +2202 -2680
- metadata +31 -40
- data/docs/assets.md +0 -135
- data/docs/cloud9.md +0 -310
- data/docs/css.md +0 -303
- data/docs/docker.md +0 -68
- data/docs/engines.md +0 -213
- data/docs/env.md +0 -68
- data/docs/es6.md +0 -72
- data/docs/folder-structure.md +0 -66
- data/docs/integrations.md +0 -220
- data/docs/misc.md +0 -23
- data/docs/props.md +0 -187
- data/docs/react.md +0 -183
- data/docs/target.md +0 -22
- data/docs/testing.md +0 -147
- data/docs/typescript.md +0 -190
- data/docs/v4-upgrade.md +0 -142
- data/docs/webpack-dev-server.md +0 -94
- data/docs/webpack.md +0 -315
- data/docs/yarn.md +0 -23
- data/lib/install/examples/vue3/app.vue +0 -27
- data/lib/install/examples/vue3/hello_vue.js +0 -15
- data/lib/install/javascript/packs/application.css +0 -9
- data/package/babel/preset-react.js +0 -62
- data/package/rules/svg.js +0 -23
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, fronts, and CSS as well.
|
19
15
|
|
20
16
|
**NOTE:** The master branch now hosts the code for v6.x.x. Please refer to [5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable) branch for 5.x documentation.
|
21
17
|
|
@@ -26,27 +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
|
-
- [Integrations](#integrations)
|
36
|
-
- [React](./docs/integrations.md#react)
|
37
|
-
- [Angular with TypeScript](./docs/integrations.md#angular-with-typescript)
|
38
|
-
- [Vue](./docs/integrations.md#vue)
|
39
|
-
- [Elm](./docs/integrations.md#elm)
|
40
|
-
- [Stimulus](./docs/integrations.md#stimulus)
|
41
|
-
- [Svelte](./docs/integrations.md#svelte)
|
42
|
-
- [Typescript](./docs/typescript.md)
|
43
|
-
- [CoffeeScript](./docs/integrations.md#coffeescript)
|
44
|
-
- [Erb](./docs/integrations.md#erb)
|
45
44
|
- [Paths](#paths)
|
46
|
-
- [
|
47
|
-
- [Watched](#watched)
|
45
|
+
- [Additional paths](#additional-paths)
|
48
46
|
- [Deployment](#deployment)
|
49
|
-
- [
|
47
|
+
- [Troubleshooting](#troubleshooting)
|
50
48
|
- [Contributing](#contributing)
|
51
49
|
- [License](#license)
|
52
50
|
|
@@ -56,59 +54,52 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
56
54
|
|
57
55
|
- Ruby 2.4+
|
58
56
|
- Rails 5.2+
|
59
|
-
- Node.js
|
60
|
-
- Yarn
|
57
|
+
- Node.js 12.13.0+ || 14+
|
58
|
+
- Yarn
|
61
59
|
|
62
60
|
## Features
|
63
61
|
|
64
|
-
- [
|
62
|
+
- [Webpack v5](https://webpack.js.org/)
|
65
63
|
- ES6 with [babel](https://babeljs.io/)
|
66
64
|
- Automatic code splitting using multiple entry points
|
67
|
-
- Stylesheets - Sass and CSS
|
68
|
-
- Images and fonts
|
69
|
-
- PostCSS - Auto-Prefixer
|
70
65
|
- Asset compression, source-maps, and minification
|
71
66
|
- CDN support
|
72
|
-
- React, Angular, Elm and Vue support out-of-the-box
|
73
67
|
- Rails view helpers
|
74
68
|
- Extensible and configurable
|
75
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
|
+
|
76
79
|
## Installation
|
77
80
|
|
78
|
-
You can
|
79
|
-
using new `--webpack` option:
|
81
|
+
You can configure a new Rails application with Webpacker right from the start using the `--webpack` option:
|
80
82
|
|
81
83
|
```bash
|
82
|
-
# Available Rails 5.1+
|
83
84
|
rails new myapp --webpack
|
84
85
|
```
|
85
86
|
|
86
|
-
Or add it
|
87
|
+
Or you can add it later by changing your `Gemfile`:
|
87
88
|
|
88
89
|
```ruby
|
89
90
|
# Gemfile
|
90
|
-
gem 'webpacker', '~>
|
91
|
+
gem 'webpacker', '~> 6.0'
|
91
92
|
|
92
93
|
# OR if you prefer to use master
|
93
94
|
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
|
94
95
|
yarn add https://github.com/rails/webpacker.git
|
95
|
-
yarn add core-js regenerator-runtime
|
96
|
-
```
|
97
|
-
|
98
|
-
Finally, run the following to install Webpacker:
|
99
|
-
|
100
|
-
```bash
|
101
|
-
bundle
|
102
|
-
bundle exec rails webpacker:install
|
103
|
-
|
104
|
-
# OR (on rails version < 5.0)
|
105
|
-
bundle exec rake webpacker:install
|
106
96
|
```
|
107
97
|
|
108
|
-
|
98
|
+
Then running the following to install Webpacker:
|
109
99
|
|
110
100
|
```bash
|
111
|
-
|
101
|
+
./bin/bundle install
|
102
|
+
./bin/rails webpacker:install
|
112
103
|
```
|
113
104
|
|
114
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:
|
@@ -122,9 +113,9 @@ yarn install
|
|
122
113
|
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
123
114
|
|
124
115
|
```yml
|
125
|
-
app/
|
126
|
-
├──
|
127
|
-
│ #
|
116
|
+
app/packs:
|
117
|
+
├── entrypoints:
|
118
|
+
│ # Only Webpack entry files here
|
128
119
|
│ └── application.js
|
129
120
|
│ └── application.css
|
130
121
|
└── src:
|
@@ -135,34 +126,20 @@ app/javascript:
|
|
135
126
|
└── logo.svg
|
136
127
|
```
|
137
128
|
|
138
|
-
|
139
|
-
|
140
|
-
```js
|
141
|
-
import 'core-js/stable'
|
142
|
-
import 'regenerator-runtime/runtime'
|
143
|
-
```
|
144
|
-
|
145
|
-
You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper.
|
146
|
-
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`:
|
147
130
|
|
148
131
|
```erb
|
149
132
|
<%= javascript_pack_tag 'application' %>
|
150
133
|
<%= stylesheet_pack_tag 'application' %>
|
151
134
|
```
|
152
135
|
|
153
|
-
|
154
|
-
|
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.
|
155
138
|
|
156
|
-
|
157
|
-
<link rel="prefetch" href="<%= asset_pack_path 'application.css' %>" />
|
158
|
-
<img src="<%= asset_pack_path 'images/logo.svg' %>" />
|
159
|
-
```
|
160
|
-
|
161
|
-
If you are using new webpack 4 split chunks API, then consider using `javascript_packs_with_chunks_tag` helper, which creates html
|
162
|
-
tags for a pack and all the dependent chunks.
|
139
|
+
The result looks like this:
|
163
140
|
|
164
141
|
```erb
|
165
|
-
<%=
|
142
|
+
<%= javascript_pack_tag 'calendar', 'map' %>
|
166
143
|
|
167
144
|
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
168
145
|
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
@@ -171,36 +148,63 @@ tags for a pack and all the dependent chunks.
|
|
171
148
|
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
172
149
|
```
|
173
150
|
|
174
|
-
**Important:** Pass all your pack names when using `
|
175
|
-
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.
|
176
152
|
|
177
153
|
```erb
|
178
154
|
<%# DO %>
|
179
|
-
<%=
|
155
|
+
<%= javascript_pack_tag 'calendar', 'map' %>
|
156
|
+
<%= stylesheet_pack_tag 'calendar', 'map' %>
|
180
157
|
|
181
158
|
<%# DON'T %>
|
182
|
-
<%=
|
183
|
-
<%=
|
159
|
+
<%= javascript_pack_tag 'calendar' %>
|
160
|
+
<%= javascript_pack_tag 'map' %>
|
161
|
+
<%= stylesheet_pack_tag 'calendar' %>
|
162
|
+
<%= stylesheet_pack_tag 'map' %>
|
163
|
+
```
|
164
|
+
|
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' %>
|
184
179
|
```
|
185
180
|
|
186
|
-
|
187
|
-
|
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
|
+
|
188
200
|
|
189
201
|
### Development
|
190
202
|
|
191
|
-
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
|
192
|
-
Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js`
|
193
|
-
executables to ensure that the right configuration files and environmental variables
|
194
|
-
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.
|
195
204
|
|
196
|
-
In development, Webpacker compiles on demand rather than upfront by default. This
|
197
|
-
happens when you refer to any of the pack assets using the Webpacker helper methods.
|
198
|
-
This means that you don't have to run any separate processes. Compilation errors are logged
|
199
|
-
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`
|
200
206
|
|
201
|
-
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
|
202
|
-
in a terminal separate from `bundle exec rails s`. This process will watch for changes
|
203
|
-
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/).
|
204
208
|
|
205
209
|
```bash
|
206
210
|
# webpack dev server
|
@@ -213,24 +217,15 @@ in the `app/javascript/packs/*.js` files and automatically reload the browser to
|
|
213
217
|
./bin/webpack
|
214
218
|
```
|
215
219
|
|
216
|
-
Once you start this development server, Webpacker will automatically start proxying all
|
217
|
-
webpack asset requests to this server. When you stop the server, it'll revert back to
|
218
|
-
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`
|
219
221
|
|
220
|
-
You can use environment variables as options supported by
|
221
|
-
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
|
222
|
-
form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
|
223
|
-
variables will always take precedence over the ones already set in the
|
224
|
-
configuration file, and that the _same_ environmental variables must
|
225
|
-
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.
|
226
223
|
|
227
224
|
```bash
|
228
225
|
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
|
229
226
|
```
|
230
227
|
|
231
|
-
By default, the webpack dev server listens on `localhost` in development for security purposes.
|
232
|
-
However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
|
233
|
-
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:
|
234
229
|
|
235
230
|
```bash
|
236
231
|
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
@@ -239,16 +234,254 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
239
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:
|
240
235
|
|
241
236
|
```ruby
|
242
|
-
|
243
|
-
|
244
|
-
|
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
|
245
240
|
```
|
246
241
|
|
247
242
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
248
243
|
|
244
|
+
|
249
245
|
### Webpack Configuration
|
250
246
|
|
251
|
-
|
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
|
+
|
252
485
|
|
253
486
|
### Custom Rails environments
|
254
487
|
|
@@ -270,8 +503,7 @@ staging:
|
|
270
503
|
public_output_path: packs-staging
|
271
504
|
```
|
272
505
|
|
273
|
-
|
274
|
-
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`.
|
275
507
|
|
276
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:
|
277
509
|
|
@@ -279,22 +511,20 @@ For example, the below command will compile assets in production mode but will u
|
|
279
511
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
280
512
|
```
|
281
513
|
|
282
|
-
And, this will compile in development mode and load configuration for cucumber environment
|
283
|
-
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
|
284
515
|
|
285
516
|
```bash
|
286
517
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
287
518
|
```
|
288
519
|
|
289
|
-
Please note, binstubs compiles in development mode however rake tasks
|
290
|
-
compiles in production mode.
|
520
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
291
521
|
|
292
522
|
```bash
|
293
|
-
# Compiles in development mode unless NODE_ENV is specified
|
523
|
+
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
294
524
|
./bin/webpack
|
295
525
|
./bin/webpack-dev-server
|
296
526
|
|
297
|
-
#
|
527
|
+
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
|
298
528
|
bundle exec rails assets:precompile
|
299
529
|
bundle exec rails webpacker:compile
|
300
530
|
```
|
@@ -304,53 +534,37 @@ bundle exec rails webpacker:compile
|
|
304
534
|
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
305
535
|
|
306
536
|
```bash
|
537
|
+
# check your Gemfile for version restrictions
|
307
538
|
bundle update webpacker
|
539
|
+
|
540
|
+
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
308
541
|
rails webpacker:install
|
542
|
+
|
543
|
+
# yarn 1 instructions
|
309
544
|
yarn upgrade @rails/webpacker --latest
|
310
545
|
yarn upgrade webpack-dev-server --latest
|
311
546
|
|
547
|
+
# yarn 2 instructions
|
548
|
+
yarn up @rails/webpacker@latest
|
549
|
+
yarn up webpack-dev-server@latest
|
550
|
+
|
312
551
|
# Or to install the latest release (including pre-releases)
|
313
552
|
yarn add @rails/webpacker@next
|
314
553
|
```
|
315
554
|
|
316
|
-
|
317
|
-
|
318
|
-
Webpacker ships with basic out-of-the-box integration. You can see a list of available commands/tasks by running `bundle exec rails webpacker`.
|
319
|
-
|
320
|
-
Included install integrations:
|
321
|
-
|
322
|
-
- [React](./docs/integrations.md#React)
|
323
|
-
- [Angular with TypeScript](./docs/integrations.md#Angular-with-TypeScript)
|
324
|
-
- [Vue](./docs/integrations.md#Vue)
|
325
|
-
- [Elm](./docs/integrations.md#Elm)
|
326
|
-
- [Svelte](./docs/integrations.md#Svelte)
|
327
|
-
- [Stimulus](./docs/integrations.md#Stimulus)
|
328
|
-
- [CoffeeScript](./docs/integrations.md#CoffeeScript)
|
329
|
-
- [Typescript](./docs/typescript.md)
|
330
|
-
- [Erb](./docs/integrations.md#Erb)
|
331
|
-
|
332
|
-
See [Integrations](./docs/integrations.md) for further details.
|
555
|
+
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
333
556
|
|
334
557
|
## Paths
|
335
558
|
|
336
|
-
By default, Webpacker ships with simple conventions for where the JavaScript
|
337
|
-
app files and compiled webpack bundles will go in your Rails app.
|
338
|
-
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.
|
339
560
|
|
340
|
-
The configuration for what webpack is supposed to compile by default rests
|
341
|
-
on the convention that every file in `app/javascript/packs/*`**(default)**
|
342
|
-
or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
|
343
|
-
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
|
344
|
-
an entry file. As a rule of thumb, put all files you want to link in your views inside
|
345
|
-
"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`.
|
346
562
|
|
347
|
-
Suppose you want to change the source directory from `app/
|
348
|
-
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:
|
349
564
|
|
350
565
|
```yml
|
351
566
|
# config/webpacker.yml
|
352
|
-
source_path: frontend
|
353
|
-
source_entry_path: packs
|
567
|
+
source_path: frontend # packs are in frontend/packs
|
354
568
|
public_output_path: assets/packs # outputs to => public/assets/packs
|
355
569
|
```
|
356
570
|
|
@@ -366,16 +580,13 @@ development:
|
|
366
580
|
|
367
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.
|
368
582
|
|
369
|
-
###
|
583
|
+
### Additional paths
|
370
584
|
|
371
|
-
If you are adding Webpacker to an existing app that has most of the assets inside
|
372
|
-
|
373
|
-
with webpack modules, you can use the `additional_paths`
|
374
|
-
option available in `config/webpacker.yml`. This lets you
|
375
|
-
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:
|
376
587
|
|
377
588
|
```yml
|
378
|
-
additional_paths: ['app/assets']
|
589
|
+
additional_paths: ['app/assets', 'vendor/assets']
|
379
590
|
```
|
380
591
|
|
381
592
|
You can then import these items inside your modules like so:
|
@@ -386,9 +597,10 @@ import 'stylesheets/main'
|
|
386
597
|
import 'images/rails.png'
|
387
598
|
```
|
388
599
|
|
389
|
-
**Note:** Please be careful when adding paths here otherwise it
|
390
|
-
|
391
|
-
|
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
|
+
|
392
604
|
|
393
605
|
## Deployment
|
394
606
|
|
@@ -396,25 +608,13 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
|
|
396
608
|
|
397
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.
|
398
610
|
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
- [Assets](./docs/assets.md) - [CSS, Sass and SCSS](./docs/css.md) - [ES6](./docs/es6.md), [Target browsers](./docs/target.md)
|
407
|
-
- [Props](./docs/props.md)
|
408
|
-
- [Typescript](./docs/typescript.md)
|
409
|
-
- [Yarn](./docs/yarn.md)
|
410
|
-
- [Misc](./docs/misc.md)
|
411
|
-
- [Deployment](./docs/deployment.md)
|
412
|
-
- [Docker](./docs/docker.md)
|
413
|
-
- [Using in Rails engines](./docs/engines.md)
|
414
|
-
- [Webpacker on Cloud9](./docs/cloud9.md)
|
415
|
-
- [Testing](./docs/testing.md)
|
416
|
-
- [Troubleshooting](./docs/troubleshooting.md)
|
417
|
-
- [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
|
+
|
418
618
|
|
419
619
|
## Contributing
|
420
620
|
|
@@ -422,6 +622,7 @@ When compiling assets for production on a remote server, such as a continuous in
|
|
422
622
|
|
423
623
|
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
424
624
|
|
625
|
+
|
425
626
|
## License
|
426
627
|
|
427
628
|
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|