webpacker 6.0.0.rc.1 → 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 +6 -17
- data/.github/workflows/js-lint.yml +6 -17
- data/.github/workflows/ruby.yml +13 -38
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +15 -3
- data/CONTRIBUTING.md +19 -0
- data/Gemfile.lock +84 -84
- data/README.md +118 -164
- data/docs/developing_webpacker.md +4 -4
- data/docs/troubleshooting.md +5 -1
- data/docs/v6_upgrade.md +72 -34
- data/lib/install/{packs/entrypoints/application.js → application.js} +3 -7
- data/lib/install/bin/webpack +4 -7
- data/lib/install/bin/yarn +18 -0
- data/lib/install/config/webpacker.yml +16 -13
- data/lib/install/package.json +15 -0
- data/lib/install/template.rb +37 -15
- data/lib/tasks/yarn.rake +38 -0
- data/lib/webpacker/commands.rb +19 -15
- data/lib/webpacker/dev_server_runner.rb +1 -1
- data/lib/webpacker/helper.rb +2 -2
- data/lib/webpacker/railtie.rb +7 -0
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +27 -7
- data/package/__tests__/development.js +4 -11
- data/package/config.js +3 -3
- data/package/env.js +3 -6
- data/package/environments/base.js +1 -1
- data/package/environments/development.js +34 -32
- data/package/rules/file.js +1 -1
- data/package.json +15 -15
- data/test/command_test.rb +76 -0
- data/test/dev_server_runner_test.rb +1 -1
- data/test/helper_test.rb +13 -4
- data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/test/webpacker_test.rb +4 -0
- data/webpacker.gemspec +2 -2
- data/yarn.lock +1392 -2825
- metadata +16 -11
data/README.md
CHANGED
@@ -10,12 +10,8 @@
|
|
10
10
|
|
11
11
|
Webpacker makes it easy to use the JavaScript pre-processor and bundler
|
12
12
|
[Webpack v5](https://webpack.js.org/)
|
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.
|
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
|
|
@@ -32,7 +28,9 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
32
28
|
- [Server-Side Rendering (SSR)](#server-side-rendering-ssr)
|
33
29
|
- [Development](#development)
|
34
30
|
- [Webpack Configuration](#webpack-configuration)
|
31
|
+
- [Babel Configuration](#babel-configuration)
|
35
32
|
- [Integrations](#integrations)
|
33
|
+
- [React](#react)
|
36
34
|
- [CoffeeScript](#coffeescript)
|
37
35
|
- [TypeScript](#typescript)
|
38
36
|
- [CSS](#css)
|
@@ -40,7 +38,6 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
40
38
|
- [Sass](#sass)
|
41
39
|
- [Less](#less)
|
42
40
|
- [Stylus](#stylus)
|
43
|
-
- [React](#react)
|
44
41
|
- [Other frameworks](#other-frameworks)
|
45
42
|
- [Custom Rails environments](#custom-rails-environments)
|
46
43
|
- [Upgrading](#upgrading)
|
@@ -57,8 +54,8 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
57
54
|
|
58
55
|
- Ruby 2.4+
|
59
56
|
- Rails 5.2+
|
60
|
-
- Node.js 12+ || 14+
|
61
|
-
- Yarn
|
57
|
+
- Node.js 12.13.0+ || 14+
|
58
|
+
- Yarn
|
62
59
|
|
63
60
|
## Features
|
64
61
|
|
@@ -81,39 +78,28 @@ in which case you may not even need the asset pipeline. This is mostly relevant
|
|
81
78
|
|
82
79
|
## Installation
|
83
80
|
|
84
|
-
You can
|
85
|
-
using new `--webpack` option:
|
81
|
+
You can configure a new Rails application with Webpacker right from the start using the `-j webpack` option:
|
86
82
|
|
87
83
|
```bash
|
88
|
-
|
89
|
-
rails new myapp --webpack
|
84
|
+
rails new myapp -j webpack
|
90
85
|
```
|
91
86
|
|
92
|
-
Or add it
|
87
|
+
Or you can add it later by changing your `Gemfile`:
|
93
88
|
|
94
89
|
```ruby
|
95
90
|
# Gemfile
|
96
|
-
gem 'webpacker', '~> 6.
|
91
|
+
gem 'webpacker', '~> 6.0'
|
97
92
|
|
98
93
|
# OR if you prefer to use master
|
99
94
|
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
|
100
95
|
yarn add https://github.com/rails/webpacker.git
|
101
96
|
```
|
102
97
|
|
103
|
-
|
104
|
-
|
105
|
-
```bash
|
106
|
-
bundle
|
107
|
-
bundle exec rails webpacker:install
|
108
|
-
|
109
|
-
# OR (on rails version < 5.0)
|
110
|
-
bundle exec rake webpacker:install
|
111
|
-
```
|
112
|
-
|
113
|
-
Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/webpacker/issues/1078),
|
98
|
+
Then running the following to install Webpacker:
|
114
99
|
|
115
100
|
```bash
|
116
|
-
|
101
|
+
./bin/bundle install
|
102
|
+
./bin/rails webpacker:install
|
117
103
|
```
|
118
104
|
|
119
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:
|
@@ -127,11 +113,10 @@ yarn install
|
|
127
113
|
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
|
128
114
|
|
129
115
|
```yml
|
130
|
-
app/
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
│ └── application.css
|
116
|
+
app/javascript:
|
117
|
+
# Only Webpack entry files here
|
118
|
+
└── application.js
|
119
|
+
└── application.css
|
135
120
|
└── src:
|
136
121
|
│ └── my_component.js
|
137
122
|
└── stylesheets:
|
@@ -153,17 +138,16 @@ packs with the chunks in your view, which creates html tags for all the chunks.
|
|
153
138
|
The result looks like this:
|
154
139
|
|
155
140
|
```erb
|
156
|
-
<%= javascript_pack_tag 'calendar', 'map' %>
|
141
|
+
<%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
|
157
142
|
|
158
|
-
<script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
159
|
-
<script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
160
|
-
<script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
161
|
-
<script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
162
|
-
<script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
|
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>
|
163
148
|
```
|
164
149
|
|
165
|
-
**Important:** Pass all your pack names as multiple arguments, not multiple calls, when using
|
166
|
-
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.
|
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.
|
167
151
|
|
168
152
|
```erb
|
169
153
|
<%# DO %>
|
@@ -205,72 +189,52 @@ If you want to use images in your stylesheets:
|
|
205
189
|
background-image: url('../images/logo.svg')
|
206
190
|
}
|
207
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`.
|
208
194
|
|
209
195
|
#### Server-Side Rendering (SSR)
|
210
|
-
Note, if you are using server-side rendering of JavaScript with dynamic code-spliting,
|
211
|
-
as is often done with extensions to Webpacker, like [React on Rails](https://github.com/shakacode/react_on_rails)
|
212
|
-
your JavaScript should create the link prefetch HTML tags that you will use, so you won't
|
213
|
-
need to use to `asset_pack_path` in those circumstances.
|
214
196
|
|
215
|
-
|
216
|
-
|
217
|
-
to package up those files.
|
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.
|
218
200
|
|
219
201
|
### Development
|
220
202
|
|
221
|
-
Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
In development, Webpacker compiles on demand rather than upfront by default. This
|
227
|
-
happens when you refer to any of the pack assets using the Webpacker helper methods.
|
228
|
-
This means that you don't have to run any separate processes. Compilation errors are logged
|
229
|
-
to the standard Rails log. However, this auto-compilation happens when a web request
|
230
|
-
is made that requires an updated webpack build, not when files change. Thus, that can
|
231
|
-
be painfully slow for front-end development in this default way. Instead, you should either
|
232
|
-
run the `bin/webpack --watch` or run `./bin/webpack-dev-server`
|
233
|
-
|
234
|
-
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`.
|
235
|
-
Windows users will need to run these commands in a terminal separate from `bundle exec rails s`.
|
236
|
-
This process will watch for changes in the `app/packs/entrypoints/*.js` files and automatically
|
237
|
-
reload the browser to match. This feature is also known as
|
238
|
-
[Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
|
239
|
-
|
240
|
-
HMR is only the first step to running "Fast Refresh" with React. For more information
|
241
|
-
on how to configure rails/webpacker for Fast Refresh with React, see article
|
242
|
-
[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/).
|
243
208
|
|
244
209
|
```bash
|
245
210
|
# webpack dev server
|
246
211
|
./bin/webpack-dev-server
|
247
212
|
|
248
213
|
# watcher
|
249
|
-
./bin/webpack --watch --
|
214
|
+
./bin/webpack --watch --progress
|
250
215
|
|
251
216
|
# standalone build
|
252
|
-
./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
|
253
227
|
```
|
254
228
|
|
255
|
-
Once you start this webpack development server, Webpacker will automatically start proxying all
|
256
|
-
webpack asset requests to this server. When you stop this server, Rails will detect
|
257
|
-
that it's not running and Rails will revert back to on-demand compilation _if_ you have
|
258
|
-
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`
|
259
230
|
|
260
|
-
You can use environment variables as options supported by
|
261
|
-
[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the
|
262
|
-
form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental
|
263
|
-
variables will always take precedence over the ones already set in the
|
264
|
-
configuration file, and that the _same_ environmental variables must
|
265
|
-
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.
|
266
232
|
|
267
233
|
```bash
|
268
234
|
WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
|
269
235
|
```
|
270
236
|
|
271
|
-
By default, the webpack dev server listens on `localhost` in development for security purposes.
|
272
|
-
However, if you want your app to be available over local LAN IP or a VM instance like vagrant,
|
273
|
-
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:
|
274
238
|
|
275
239
|
```bash
|
276
240
|
WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
@@ -279,23 +243,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
|
|
279
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:
|
280
244
|
|
281
245
|
```ruby
|
282
|
-
|
283
|
-
|
284
|
-
|
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
|
285
249
|
```
|
286
250
|
|
287
251
|
**Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
|
288
252
|
|
253
|
+
|
289
254
|
### Webpack Configuration
|
290
255
|
|
291
|
-
Webpacker gives you a default set of configuration files for test, development and
|
292
|
-
production environments in `config/webpack/*.js`. You can configure each individual
|
293
|
-
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
|
294
257
|
`config/webpack/base.js` file.
|
295
258
|
|
296
|
-
By default, you don't need to make any changes to `config/webpack/*.js`
|
297
|
-
files since it's all standard production-ready configuration. However,
|
298
|
-
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.
|
299
260
|
|
300
261
|
Here is how you can modify webpack configuration:
|
301
262
|
|
@@ -326,8 +287,7 @@ const customConfig = require('./custom')
|
|
326
287
|
module.exports = merge(webpackConfig, customConfig)
|
327
288
|
```
|
328
289
|
|
329
|
-
If you need access to configs within Webpacker's configuration,
|
330
|
-
you can import them like so:
|
290
|
+
If you need access to configs within Webpacker's configuration, you can import them like so:
|
331
291
|
|
332
292
|
```js
|
333
293
|
// config/webpack/base.js
|
@@ -340,11 +300,51 @@ console.log(webpackConfig.source_path)
|
|
340
300
|
console.log(JSON.stringify(webpackConfig, undefined, 2))
|
341
301
|
```
|
342
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
|
+
|
343
318
|
### Integrations
|
344
319
|
|
345
|
-
Webpacker out of the box supports JS and static assets (fonts, images etc.)
|
346
|
-
|
347
|
-
|
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
348
|
|
349
349
|
#### CoffeeScript
|
350
350
|
|
@@ -358,6 +358,12 @@ yarn add coffeescript coffee-loader
|
|
358
358
|
yarn add typescript @babel/preset-typescript
|
359
359
|
```
|
360
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
|
+
|
361
367
|
Add tsconfig.json
|
362
368
|
|
363
369
|
```json
|
@@ -382,12 +388,6 @@ Add tsconfig.json
|
|
382
388
|
}
|
383
389
|
```
|
384
390
|
|
385
|
-
Babel won’t perform any type-checking on TypeScript code. To optionally use type-checking run:
|
386
|
-
|
387
|
-
```bash
|
388
|
-
yarn add fork-ts-checker-webpack-plugin
|
389
|
-
```
|
390
|
-
|
391
391
|
Then modify the webpack config to use it as a plugin:
|
392
392
|
|
393
393
|
```js
|
@@ -454,39 +454,6 @@ yarn add less less-loader
|
|
454
454
|
yarn add stylus stylus-loader
|
455
455
|
```
|
456
456
|
|
457
|
-
#### React
|
458
|
-
|
459
|
-
React is supported and you just need to add relevant packages,
|
460
|
-
|
461
|
-
```bash
|
462
|
-
yarn add react react-dom @babel/preset-react
|
463
|
-
```
|
464
|
-
|
465
|
-
if you are using typescript, update your `tsconfig.json`
|
466
|
-
|
467
|
-
```json
|
468
|
-
{
|
469
|
-
"compilerOptions": {
|
470
|
-
"declaration": false,
|
471
|
-
"emitDecoratorMetadata": true,
|
472
|
-
"experimentalDecorators": true,
|
473
|
-
"lib": ["es6", "dom"],
|
474
|
-
"module": "es6",
|
475
|
-
"moduleResolution": "node",
|
476
|
-
"sourceMap": true,
|
477
|
-
"target": "es5",
|
478
|
-
"jsx": "react",
|
479
|
-
"noEmit": true
|
480
|
-
},
|
481
|
-
"exclude": ["**/*.spec.ts", "node_modules", "vendor", "public"],
|
482
|
-
"compileOnSave": false
|
483
|
-
}
|
484
|
-
```
|
485
|
-
|
486
|
-
For more information on React props hydration and Server-Side Rendering (SSR), see the article
|
487
|
-
[Rails/Webpacker React Integration Options](https://github.com/shakacode/react_on_rails/blob/master/docs/rails-webpacker-react-integration-options.md)
|
488
|
-
in the [ShakaCode/react_on_rails](https://github.com/shakacode/react_on_rails) repo.
|
489
|
-
|
490
457
|
#### Other frameworks
|
491
458
|
|
492
459
|
Please follow webpack integration guide for relevant framework or library,
|
@@ -524,6 +491,7 @@ const vueConfig = require('./rules/vue')
|
|
524
491
|
module.exports = merge(vueConfig, webpackConfig)
|
525
492
|
```
|
526
493
|
|
494
|
+
|
527
495
|
### Custom Rails environments
|
528
496
|
|
529
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.
|
@@ -544,8 +512,7 @@ staging:
|
|
544
512
|
public_output_path: packs-staging
|
545
513
|
```
|
546
514
|
|
547
|
-
|
548
|
-
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`.
|
549
516
|
|
550
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:
|
551
518
|
|
@@ -553,15 +520,13 @@ For example, the below command will compile assets in production mode but will u
|
|
553
520
|
RAILS_ENV=staging bundle exec rails assets:precompile
|
554
521
|
```
|
555
522
|
|
556
|
-
And, this will compile in development mode and load configuration for cucumber environment
|
557
|
-
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
|
558
524
|
|
559
525
|
```bash
|
560
526
|
RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
|
561
527
|
```
|
562
528
|
|
563
|
-
Please note, binstubs compiles in development mode however rake tasks
|
564
|
-
compiles in production mode.
|
529
|
+
Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
|
565
530
|
|
566
531
|
```bash
|
567
532
|
# Compiles in development mode unless NODE_ENV is specified, per the binstub source
|
@@ -600,19 +565,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
|
|
600
565
|
|
601
566
|
## Paths
|
602
567
|
|
603
|
-
By default, Webpacker ships with simple conventions for where the JavaScript
|
604
|
-
app files and compiled webpack bundles will go in your Rails app.
|
605
|
-
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.
|
606
569
|
|
607
|
-
The configuration for what webpack is supposed to compile by default rests
|
608
|
-
on the convention that every file in `app/packs/entrypoints/*`**(default)**
|
609
|
-
or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration
|
610
|
-
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
|
611
|
-
an entry file. As a rule of thumb, put all files you want to link in your views inside
|
612
|
-
"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`.
|
613
571
|
|
614
|
-
Suppose you want to change the source directory from `app/packs`
|
615
|
-
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:
|
616
573
|
|
617
574
|
```yml
|
618
575
|
# config/webpacker.yml
|
@@ -630,16 +587,11 @@ development:
|
|
630
587
|
port: 3035
|
631
588
|
```
|
632
589
|
|
633
|
-
If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output,
|
634
|
-
as you will want to configure your styles to be inlined in your JavaScript for hot reloading.
|
635
|
-
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.
|
636
591
|
|
637
592
|
### Additional paths
|
638
593
|
|
639
|
-
If you are adding Webpacker to an existing app that has most of the assets inside
|
640
|
-
`app/assets` or inside an engine, and you want to share that
|
641
|
-
with webpack modules, you can use the `additional_paths`
|
642
|
-
option available in `config/webpacker.yml`. This lets you
|
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
|
643
595
|
add additional paths that webpack should look up when resolving modules:
|
644
596
|
|
645
597
|
```yml
|
@@ -654,12 +606,11 @@ import 'stylesheets/main'
|
|
654
606
|
import 'images/rails.png'
|
655
607
|
```
|
656
608
|
|
657
|
-
**Note:** Please be careful when adding paths here otherwise it
|
658
|
-
will make the compilation slow, consider adding specific paths instead of
|
659
|
-
whole parent directory if you just need to reference one or two modules
|
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
|
660
610
|
|
661
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.
|
662
612
|
|
613
|
+
|
663
614
|
## Deployment
|
664
615
|
|
665
616
|
Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/webpacker.yml` (if available).
|
@@ -668,16 +619,19 @@ When compiling assets for production on a remote server, such as a continuous in
|
|
668
619
|
|
669
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.
|
670
621
|
|
622
|
+
|
671
623
|
## Troubleshooting
|
672
624
|
|
673
625
|
See the doc page for [Troubleshooting](./docs/troubleshooting.md).
|
674
626
|
|
627
|
+
|
675
628
|
## Contributing
|
676
629
|
|
677
630
|
[![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
|
678
631
|
|
679
632
|
We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
|
680
633
|
|
634
|
+
|
681
635
|
## License
|
682
636
|
|
683
637
|
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -11,10 +11,10 @@ Let's call the rails/webpacker directory `WEBPACKER_DIR` which has rails/webpack
|
|
11
11
|
|
12
12
|
## Changing the Package
|
13
13
|
### Setup with Yalc
|
14
|
-
Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors.
|
15
|
-
1. In `WEBPACKER_DIR`, run `yalc publish`
|
16
|
-
2. In `TEST_APP_DIR`, run `
|
17
|
-
|
14
|
+
Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors.
|
15
|
+
1. In `WEBPACKER_DIR`, run `yalc publish`
|
16
|
+
2. In `TEST_APP_DIR`, run `yalc link @rails/webpacker`
|
17
|
+
|
18
18
|
## Update the Package Code
|
19
19
|
1. Make some JS change in WEBPACKER_DIR
|
20
20
|
2. Run `yalc push` and your changes will be pushed to your `TEST_APP_DIR`'s node_modules.
|
data/docs/troubleshooting.md
CHANGED
@@ -160,7 +160,11 @@ default: &default
|
|
160
160
|
|
161
161
|
### Using global variables for dependencies
|
162
162
|
|
163
|
-
If you want to access any dependency without importing it everywhere or use it directly in your dev tools, please check: [https://webpack.js.org/plugins/provide-plugin/](https://webpack.js.org/plugins/provide-plugin/)
|
163
|
+
If you want to access any dependency without importing it everywhere or use it directly in your dev tools, please check: [https://webpack.js.org/plugins/provide-plugin/](https://webpack.js.org/plugins/provide-plugin/) and the [webpack docs on shimming globals](https://webpack.js.org/guides/shimming/#shimming-globals).
|
164
|
+
|
165
|
+
Note, if you are exposing globals, like jQuery, to non-webpack dependencies (like an inline script) via the [expose-loader](https://webpack.js.org/loaders/expose-loader/), you will need to override the default of `defer: true` to be `defer:false` your call to the `javascript_pack_tag` so that the browser will load your bundle to setup the global variable before other code depends on it. However, you really should try to remove the dependendency on such globals.
|
166
|
+
|
167
|
+
Thus ProvidePlugin manages build-time dependencies to global symbols whereas the expose-loader manages runtime dependencies to global symbols.
|
164
168
|
|
165
169
|
**You don't need to assign dependencies on `window`.**
|
166
170
|
|