webpacker 5.4.4 → 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/.eslintrc.js +1 -1
- 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 +18 -16
- data/.gitignore +2 -0
- data/.node-version +1 -1
- data/.rubocop.yml +5 -7
- data/CHANGELOG.md +30 -12
- data/CONTRIBUTING.md +20 -1
- data/Gemfile.lock +83 -83
- data/README.md +389 -179
- data/config/README.md +3 -0
- data/config/webpacker.yml +1 -0
- data/docs/deployment.md +9 -11
- data/docs/developing_webpacker.md +29 -0
- data/docs/troubleshooting.md +61 -25
- data/docs/v6_upgrade.md +113 -0
- data/gemfiles/Gemfile-rails.6.1.x +12 -0
- data/lib/install/{javascript/packs/application.js → application.js} +2 -5
- data/lib/install/bin/webpack +4 -7
- data/lib/install/bin/yarn +18 -0
- data/lib/install/config/webpack/base.js +3 -0
- data/lib/install/config/webpack/development.js +2 -2
- data/lib/install/config/webpack/production.js +2 -2
- data/lib/install/config/webpack/test.js +2 -2
- data/lib/install/config/webpacker.yml +21 -49
- data/lib/install/package.json +15 -0
- data/lib/install/template.rb +64 -29
- data/lib/tasks/webpacker/binstubs.rake +6 -4
- data/lib/tasks/webpacker/check_binstubs.rake +4 -4
- data/lib/tasks/webpacker/check_node.rake +3 -0
- data/lib/tasks/webpacker/check_yarn.rake +3 -0
- data/lib/tasks/webpacker/clobber.rake +1 -1
- data/lib/tasks/webpacker/compile.rake +4 -2
- data/lib/tasks/webpacker/info.rake +12 -10
- data/lib/tasks/webpacker/install.rake +6 -4
- data/lib/tasks/webpacker/verify_config.rake +14 -0
- data/lib/tasks/webpacker/verify_install.rake +1 -10
- data/lib/tasks/webpacker/yarn_install.rake +9 -7
- data/lib/tasks/webpacker.rake +2 -11
- data/lib/tasks/yarn.rake +38 -0
- data/lib/webpacker/commands.rb +21 -16
- data/lib/webpacker/compiler.rb +16 -9
- data/lib/webpacker/configuration.rb +8 -32
- data/lib/webpacker/dev_server.rb +6 -0
- data/lib/webpacker/dev_server_runner.rb +28 -4
- data/lib/webpacker/helper.rb +47 -81
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +2 -3
- 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 +29 -3
- data/lib/webpacker.rb +1 -1
- data/package/__tests__/config.js +5 -37
- data/package/__tests__/development.js +13 -21
- data/package/__tests__/env.js +16 -4
- data/package/__tests__/index.js +9 -0
- data/package/__tests__/production.js +6 -6
- data/package/__tests__/staging.js +7 -6
- data/package/__tests__/test.js +4 -5
- data/package/babel/preset.js +54 -0
- data/package/config.js +6 -14
- data/package/env.js +13 -4
- data/package/environments/__tests__/base.js +20 -52
- data/package/environments/base.js +68 -128
- data/package/environments/development.js +49 -47
- data/package/environments/production.js +66 -64
- data/package/environments/test.js +2 -2
- data/package/index.js +15 -8
- data/package/inliningCss.js +7 -0
- data/package/rules/babel.js +10 -8
- data/package/rules/coffee.js +6 -0
- data/package/rules/erb.js +15 -0
- data/package/rules/file.js +21 -19
- data/package/rules/index.js +16 -18
- data/package/rules/less.js +22 -0
- data/package/rules/raw.js +5 -0
- data/package/rules/sass.js +9 -10
- data/package/rules/stylus.js +26 -0
- data/package/utils/get_style_rule.js +28 -36
- data/package/utils/helpers.js +28 -35
- data/package.json +18 -38
- data/test/command_test.rb +76 -0
- data/test/compiler_test.rb +0 -12
- data/test/configuration_test.rb +4 -35
- data/test/dev_server_runner_test.rb +36 -6
- data/test/engine_rake_tasks_test.rb +39 -0
- data/test/helper_test.rb +79 -77
- data/test/manifest_test.rb +16 -0
- data/test/mounted_app/Rakefile +4 -0
- data/test/mounted_app/test/dummy/Rakefile +3 -0
- data/test/mounted_app/test/dummy/bin/rails +3 -0
- data/test/mounted_app/test/dummy/bin/rake +3 -0
- data/test/mounted_app/test/dummy/config/application.rb +10 -0
- data/test/mounted_app/test/dummy/config/environment.rb +3 -0
- data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
- data/test/mounted_app/test/dummy/config.ru +5 -0
- data/test/mounted_app/test/dummy/package.json +7 -0
- data/test/rake_tasks_test.rb +1 -10
- data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
- data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/test/test_app/config/webpacker.yml +5 -31
- data/test/test_app/config/webpacker_other_location.yml +79 -0
- data/test/test_app/config/webpacker_public_root.yml +0 -1
- data/test/test_app/public/packs/manifest.json +36 -18
- data/test/test_app/some.config.js +0 -0
- data/test/webpack_runner_test.rb +9 -3
- data/test/webpacker_test.rb +21 -0
- data/webpacker.gemspec +2 -2
- data/yarn.lock +1277 -4620
- metadata +63 -92
- data/docs/assets.md +0 -119
- data/docs/cloud9.md +0 -310
- data/docs/css.md +0 -308
- data/docs/docker.md +0 -68
- data/docs/engines.md +0 -213
- data/docs/env.md +0 -63
- 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 -223
- data/docs/target.md +0 -22
- data/docs/testing.md +0 -136
- data/docs/typescript.md +0 -190
- data/docs/v4-upgrade.md +0 -142
- data/docs/webpack-dev-server.md +0 -92
- data/docs/webpack.md +0 -364
- data/docs/yarn.md +0 -23
- data/lib/install/angular.rb +0 -23
- data/lib/install/coffee.rb +0 -25
- data/lib/install/config/.browserslistrc +0 -1
- data/lib/install/config/babel.config.js +0 -82
- data/lib/install/config/postcss.config.js +0 -12
- data/lib/install/config/webpack/environment.js +0 -3
- data/lib/install/elm.rb +0 -39
- data/lib/install/erb.rb +0 -25
- data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
- data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
- data/lib/install/examples/angular/hello_angular/index.ts +0 -8
- data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
- data/lib/install/examples/angular/hello_angular.js +0 -7
- data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
- data/lib/install/examples/elm/Main.elm +0 -55
- data/lib/install/examples/elm/hello_elm.js +0 -16
- data/lib/install/examples/erb/hello_erb.js.erb +0 -6
- data/lib/install/examples/react/babel.config.js +0 -99
- data/lib/install/examples/react/hello_react.jsx +0 -26
- data/lib/install/examples/react/tsconfig.json +0 -21
- data/lib/install/examples/stimulus/application.js +0 -1
- data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
- data/lib/install/examples/stimulus/controllers/index.js +0 -9
- data/lib/install/examples/svelte/app.svelte +0 -11
- data/lib/install/examples/svelte/hello_svelte.js +0 -20
- data/lib/install/examples/typescript/hello_typescript.ts +0 -4
- data/lib/install/examples/typescript/tsconfig.json +0 -24
- data/lib/install/examples/vue/app.vue +0 -22
- data/lib/install/examples/vue/hello_vue.js +0 -72
- data/lib/install/loaders/coffee.js +0 -6
- data/lib/install/loaders/elm.js +0 -25
- data/lib/install/loaders/erb.js +0 -11
- data/lib/install/loaders/svelte.js +0 -9
- data/lib/install/loaders/vue.js +0 -6
- data/lib/install/react.rb +0 -18
- data/lib/install/stimulus.rb +0 -12
- data/lib/install/svelte.rb +0 -29
- data/lib/install/typescript.rb +0 -39
- data/lib/install/vue.rb +0 -49
- data/lib/tasks/installers.rake +0 -42
- data/package/config_types/__tests__/config_list.js +0 -118
- data/package/config_types/__tests__/config_object.js +0 -43
- data/package/config_types/config_list.js +0 -75
- data/package/config_types/config_object.js +0 -55
- data/package/config_types/index.js +0 -7
- data/package/rules/module.css.js +0 -3
- data/package/rules/module.sass.js +0 -8
- data/package/rules/node_modules.js +0 -22
- data/package/utils/__tests__/deep_assign.js +0 -32
- data/package/utils/__tests__/deep_merge.js +0 -10
- data/package/utils/__tests__/get_style_rule.js +0 -65
- data/package/utils/__tests__/objectify.js +0 -9
- data/package/utils/deep_assign.js +0 -22
- data/package/utils/deep_merge.js +0 -22
- data/package/utils/objectify.js +0 -3
- /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/README.md
CHANGED
|
@@ -5,19 +5,15 @@
|
|
|
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).
|
|
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.
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.
|
|
19
|
-
|
|
20
|
-
**NOTE:** The master branch now hosts the code for v5.x.x. Please refer to [4-x-stable](https://github.com/rails/webpacker/tree/4-x-stable) branch for 4.x documentation.
|
|
16
|
+
**NOTE:** The master branch now hosts the code for v6.x.x. Please refer to [5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable) branch for 5.x documentation.
|
|
21
17
|
|
|
22
18
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
23
19
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
@@ -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 `-j webpack` option:
|
|
80
82
|
|
|
81
83
|
```bash
|
|
82
|
-
|
|
83
|
-
rails new myapp --webpack
|
|
84
|
+
rails new myapp -j 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:
|
|
@@ -123,10 +114,9 @@ Once installed, you can start writing modern ES6-flavored JavaScript apps right
|
|
|
123
114
|
|
|
124
115
|
```yml
|
|
125
116
|
app/javascript:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
│ └── application.css
|
|
117
|
+
# Only Webpack entry files here
|
|
118
|
+
└── application.js
|
|
119
|
+
└── application.css
|
|
130
120
|
└── src:
|
|
131
121
|
│ └── my_component.js
|
|
132
122
|
└── stylesheets:
|
|
@@ -135,102 +125,116 @@ app/javascript:
|
|
|
135
125
|
└── logo.svg
|
|
136
126
|
```
|
|
137
127
|
|
|
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`:
|
|
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`:
|
|
147
129
|
|
|
148
130
|
```erb
|
|
149
131
|
<%= javascript_pack_tag 'application' %>
|
|
150
132
|
<%= stylesheet_pack_tag 'application' %>
|
|
151
133
|
```
|
|
152
134
|
|
|
153
|
-
|
|
154
|
-
|
|
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:
|
|
155
139
|
|
|
156
140
|
```erb
|
|
157
|
-
|
|
158
|
-
|
|
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>
|
|
159
148
|
```
|
|
160
149
|
|
|
161
|
-
|
|
162
|
-
|
|
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
163
|
|
|
164
|
+
If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
|
|
164
165
|
```erb
|
|
165
|
-
<%=
|
|
166
|
+
<img src="<%= asset_pack_path 'images/logo.svg' %>" />
|
|
167
|
+
```
|
|
166
168
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
|
169
|
+
Or use the dedicated helper:
|
|
170
|
+
```erb
|
|
171
|
+
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
|
|
172
|
+
<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
|
|
172
173
|
```
|
|
173
174
|
|
|
174
|
-
|
|
175
|
-
|
|
175
|
+
If you want to create a favicon:
|
|
176
|
+
```erb
|
|
177
|
+
<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>
|
|
178
|
+
```
|
|
176
179
|
|
|
180
|
+
If you want to preload a static asset in your `<head>`, you can use the `preload_pack_asset` helper:
|
|
177
181
|
```erb
|
|
178
|
-
|
|
179
|
-
|
|
182
|
+
<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %>
|
|
183
|
+
```
|
|
180
184
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
185
|
+
If you want to use images in your stylesheets:
|
|
186
|
+
|
|
187
|
+
```css
|
|
188
|
+
.foo {
|
|
189
|
+
background-image: url('../images/logo.svg')
|
|
190
|
+
}
|
|
184
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)
|
|
185
196
|
|
|
186
|
-
|
|
187
|
-
|
|
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.
|
|
198
|
+
|
|
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.
|
|
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 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/).
|
|
204
208
|
|
|
205
209
|
```bash
|
|
206
210
|
# webpack dev server
|
|
207
211
|
./bin/webpack-dev-server
|
|
208
212
|
|
|
209
213
|
# watcher
|
|
210
|
-
./bin/webpack --watch --
|
|
214
|
+
./bin/webpack --watch --progress
|
|
211
215
|
|
|
212
216
|
# standalone build
|
|
213
|
-
./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
|
|
214
227
|
```
|
|
215
228
|
|
|
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.
|
|
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`
|
|
219
230
|
|
|
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.
|
|
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.
|
|
226
232
|
|
|
227
233
|
```bash
|
|
228
234
|
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
|
|
229
235
|
```
|
|
230
236
|
|
|
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:
|
|
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:
|
|
234
238
|
|
|
235
239
|
```bash
|
|
236
240
|
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
@@ -239,16 +243,254 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
|
239
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:
|
|
240
244
|
|
|
241
245
|
```ruby
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
|
245
249
|
```
|
|
246
250
|
|
|
247
251
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
|
248
252
|
|
|
253
|
+
|
|
249
254
|
### Webpack Configuration
|
|
250
255
|
|
|
251
|
-
|
|
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
|
|
257
|
+
`config/webpack/base.js` file.
|
|
258
|
+
|
|
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.
|
|
260
|
+
|
|
261
|
+
Here is how you can modify webpack configuration:
|
|
262
|
+
|
|
263
|
+
You might add separate files to keep your code more organized.
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
// config/webpack/custom.js
|
|
267
|
+
module.exports = {
|
|
268
|
+
resolve: {
|
|
269
|
+
alias: {
|
|
270
|
+
jquery: 'jquery/src/jquery',
|
|
271
|
+
vue: 'vue/dist/vue.js',
|
|
272
|
+
React: 'react',
|
|
273
|
+
ReactDOM: 'react-dom',
|
|
274
|
+
vue_resource: 'vue-resource/dist/vue-resource'
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Then `require` this file in your `config/webpack/base.js`:
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
// config/webpack/base.js
|
|
284
|
+
const { webpackConfig, merge } = require('@rails/webpacker')
|
|
285
|
+
const customConfig = require('./custom')
|
|
286
|
+
|
|
287
|
+
module.exports = merge(webpackConfig, customConfig)
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
If you need access to configs within Webpacker's configuration, you can import them like so:
|
|
291
|
+
|
|
292
|
+
```js
|
|
293
|
+
// config/webpack/base.js
|
|
294
|
+
const { webpackConfig } = require('@rails/webpacker')
|
|
295
|
+
|
|
296
|
+
console.log(webpackConfig.output_path)
|
|
297
|
+
console.log(webpackConfig.source_path)
|
|
298
|
+
|
|
299
|
+
// Or to print out your whole webpack configuration
|
|
300
|
+
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
|
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
|
+
|
|
318
|
+
### Integrations
|
|
319
|
+
|
|
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
|
+
```
|
|
348
|
+
|
|
349
|
+
#### CoffeeScript
|
|
350
|
+
|
|
351
|
+
```bash
|
|
352
|
+
yarn add coffeescript coffee-loader
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
#### TypeScript
|
|
356
|
+
|
|
357
|
+
```bash
|
|
358
|
+
yarn add typescript @babel/preset-typescript
|
|
359
|
+
```
|
|
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
|
+
|
|
367
|
+
Add tsconfig.json
|
|
368
|
+
|
|
369
|
+
```json
|
|
370
|
+
{
|
|
371
|
+
"compilerOptions": {
|
|
372
|
+
"declaration": false,
|
|
373
|
+
"emitDecoratorMetadata": true,
|
|
374
|
+
"experimentalDecorators": true,
|
|
375
|
+
"lib": ["es6", "dom"],
|
|
376
|
+
"module": "es6",
|
|
377
|
+
"moduleResolution": "node",
|
|
378
|
+
"baseUrl": ".",
|
|
379
|
+
"paths": {
|
|
380
|
+
"*": ["node_modules/*", "app/packs/*"]
|
|
381
|
+
},
|
|
382
|
+
"sourceMap": true,
|
|
383
|
+
"target": "es5",
|
|
384
|
+
"noEmit": true
|
|
385
|
+
},
|
|
386
|
+
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
|
387
|
+
"compileOnSave": false
|
|
388
|
+
}
|
|
389
|
+
```
|
|
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
|
+
|
|
403
|
+
#### CSS
|
|
404
|
+
|
|
405
|
+
To enable CSS support in your application, add following packages:
|
|
406
|
+
|
|
407
|
+
```bash
|
|
408
|
+
yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
Optionally, add the `CSS` extension to webpack config for easy resolution.
|
|
412
|
+
|
|
413
|
+
```js
|
|
414
|
+
// config/webpack/base.js
|
|
415
|
+
const { webpackConfig, merge } = require('@rails/webpacker')
|
|
416
|
+
const customConfig = {
|
|
417
|
+
resolve: {
|
|
418
|
+
extensions: ['.css']
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
module.exports = merge(webpackConfig, customConfig)
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
To enable `PostCSS`, `Sass` or `Less` support, add `CSS` support first and
|
|
426
|
+
then add the relevant pre-processors:
|
|
427
|
+
|
|
428
|
+
#### Postcss
|
|
429
|
+
|
|
430
|
+
```bash
|
|
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
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
#### Sass
|
|
440
|
+
|
|
441
|
+
```bash
|
|
442
|
+
yarn add sass sass-loader
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
#### Less
|
|
446
|
+
|
|
447
|
+
```bash
|
|
448
|
+
yarn add less less-loader
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
#### Stylus
|
|
452
|
+
|
|
453
|
+
```bash
|
|
454
|
+
yarn add stylus stylus-loader
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
#### Other frameworks
|
|
458
|
+
|
|
459
|
+
Please follow webpack integration guide for relevant framework or library,
|
|
460
|
+
|
|
461
|
+
1. [Svelte](https://github.com/sveltejs/svelte-loader#install)
|
|
462
|
+
2. [Angular](https://v2.angular.io/docs/ts/latest/guide/webpack.html#!#configure-webpack)
|
|
463
|
+
3. [Vue](https://vue-loader.vuejs.org/guide/)
|
|
464
|
+
|
|
465
|
+
For example to add Vue support:
|
|
466
|
+
```js
|
|
467
|
+
// config/webpack/rules/vue.js
|
|
468
|
+
const { VueLoaderPlugin } = require('vue-loader')
|
|
469
|
+
|
|
470
|
+
module.exports = {
|
|
471
|
+
module: {
|
|
472
|
+
rules: [
|
|
473
|
+
{
|
|
474
|
+
test: /\.vue$/,
|
|
475
|
+
loader: 'vue-loader'
|
|
476
|
+
}
|
|
477
|
+
]
|
|
478
|
+
},
|
|
479
|
+
plugins: [new VueLoaderPlugin()],
|
|
480
|
+
resolve: {
|
|
481
|
+
extensions: ['.vue']
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
```js
|
|
487
|
+
// config/webpack/base.js
|
|
488
|
+
const { webpackConfig, merge } = require('@rails/webpacker')
|
|
489
|
+
const vueConfig = require('./rules/vue')
|
|
490
|
+
|
|
491
|
+
module.exports = merge(vueConfig, webpackConfig)
|
|
492
|
+
```
|
|
493
|
+
|
|
252
494
|
|
|
253
495
|
### Custom Rails environments
|
|
254
496
|
|
|
@@ -270,8 +512,7 @@ staging:
|
|
|
270
512
|
public_output_path: packs-staging
|
|
271
513
|
```
|
|
272
514
|
|
|
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`.
|
|
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`.
|
|
275
516
|
|
|
276
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:
|
|
277
518
|
|
|
@@ -279,22 +520,20 @@ For example, the below command will compile assets in production mode but will u
|
|
|
279
520
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
|
280
521
|
```
|
|
281
522
|
|
|
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
|
|
523
|
+
And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
|
|
284
524
|
|
|
285
525
|
```bash
|
|
286
526
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
|
287
527
|
```
|
|
288
528
|
|
|
289
|
-
Please note, binstubs compiles in development mode however rake tasks
|
|
290
|
-
compiles in production mode.
|
|
529
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
|
291
530
|
|
|
292
531
|
```bash
|
|
293
|
-
# Compiles in development mode unless NODE_ENV is specified
|
|
532
|
+
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
|
294
533
|
./bin/webpack
|
|
295
534
|
./bin/webpack-dev-server
|
|
296
535
|
|
|
297
|
-
#
|
|
536
|
+
# Compiles in production mode by default unless NODE_ENV is specified, per `lib/tasks/webpacker/compile.rake`
|
|
298
537
|
bundle exec rails assets:precompile
|
|
299
538
|
bundle exec rails webpacker:compile
|
|
300
539
|
```
|
|
@@ -304,53 +543,37 @@ bundle exec rails webpacker:compile
|
|
|
304
543
|
You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
|
|
305
544
|
|
|
306
545
|
```bash
|
|
546
|
+
# check your Gemfile for version restrictions
|
|
307
547
|
bundle update webpacker
|
|
308
|
-
|
|
548
|
+
|
|
549
|
+
# overwrite your changes to the default install files and revert any unwanted changes from the install
|
|
550
|
+
rails webpacker:install
|
|
551
|
+
|
|
552
|
+
# yarn 1 instructions
|
|
309
553
|
yarn upgrade @rails/webpacker --latest
|
|
310
554
|
yarn upgrade webpack-dev-server --latest
|
|
311
555
|
|
|
556
|
+
# yarn 2 instructions
|
|
557
|
+
yarn up @rails/webpacker@latest
|
|
558
|
+
yarn up webpack-dev-server@latest
|
|
559
|
+
|
|
312
560
|
# Or to install the latest release (including pre-releases)
|
|
313
561
|
yarn add @rails/webpacker@next
|
|
314
562
|
```
|
|
315
563
|
|
|
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.
|
|
564
|
+
Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
333
565
|
|
|
334
566
|
## Paths
|
|
335
567
|
|
|
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.
|
|
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.
|
|
339
569
|
|
|
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`.
|
|
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`.
|
|
346
571
|
|
|
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:
|
|
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:
|
|
349
573
|
|
|
350
574
|
```yml
|
|
351
575
|
# config/webpacker.yml
|
|
352
|
-
source_path: frontend
|
|
353
|
-
source_entry_path: packs
|
|
576
|
+
source_path: frontend # packs are in frontend/packs
|
|
354
577
|
public_output_path: assets/packs # outputs to => public/assets/packs
|
|
355
578
|
```
|
|
356
579
|
|
|
@@ -366,16 +589,13 @@ development:
|
|
|
366
589
|
|
|
367
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.
|
|
368
591
|
|
|
369
|
-
###
|
|
592
|
+
### Additional paths
|
|
370
593
|
|
|
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:
|
|
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:
|
|
376
596
|
|
|
377
597
|
```yml
|
|
378
|
-
additional_paths: ['app/assets']
|
|
598
|
+
additional_paths: ['app/assets', 'vendor/assets']
|
|
379
599
|
```
|
|
380
600
|
|
|
381
601
|
You can then import these items inside your modules like so:
|
|
@@ -386,9 +606,10 @@ import 'stylesheets/main'
|
|
|
386
606
|
import 'images/rails.png'
|
|
387
607
|
```
|
|
388
608
|
|
|
389
|
-
**Note:** Please be careful when adding paths here otherwise it
|
|
390
|
-
|
|
391
|
-
|
|
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
|
+
|
|
392
613
|
|
|
393
614
|
## Deployment
|
|
394
615
|
|
|
@@ -396,25 +617,13 @@ Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which
|
|
|
396
617
|
|
|
397
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.
|
|
398
619
|
|
|
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)
|
|
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
|
+
|
|
623
|
+
## Troubleshooting
|
|
624
|
+
|
|
625
|
+
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
|
626
|
+
|
|
418
627
|
|
|
419
628
|
## Contributing
|
|
420
629
|
|
|
@@ -422,6 +631,7 @@ When compiling assets for production on a remote server, such as a continuous in
|
|
|
422
631
|
|
|
423
632
|
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
|
424
633
|
|
|
634
|
+
|
|
425
635
|
## License
|
|
426
636
|
|
|
427
637
|
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|