webpacker 6.0.0.rc.1 → 6.0.0.rc.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0286ff65b615aa4824d81ece818746d0263dc0de83e1e736d5686119cb7e82a5'
4
- data.tar.gz: '0010459c42442c8648202d4f06f5a90108c2e662be4b2e3687a84dbeac1d1274'
3
+ metadata.gz: 96eeffae92e201748b583ac421869d0e76ec806df6dc750d2f0459b2aad05f62
4
+ data.tar.gz: 3ed4f39584aa504dd761c59d160014eacc2e8392f847f3c95d5380c32d449142
5
5
  SHA512:
6
- metadata.gz: 647a63b6653cd892f7f125b36d1916599e44c2ce9fdcfb0afeb0145ca8f26937d3774294158ae4967c2a90b1403ef7a98880deaa3a57ffc9dbc5b9971d1dad60
7
- data.tar.gz: 79d4d41bc7c1fe12f009b19da3a4603a2f87e8e483e1c2496d9561ae22a5bbd57d536c7fbab80f2b0dfc9af6d5599d531b3f13055824309900d8b2bbd76e518f
6
+ metadata.gz: d70ec25c9ee0ca7cbbe42f1806a7e3e142f0b7b55a0a9cdcd43d1922cf23bd69346bbdab6e37cd31a1cbb4fc533b951b60ef3fed3d622f36208a3841eccc4ff7
7
+ data.tar.gz: 47b10bc8e36c8e0697a2341714f27ffc2d749d465b840d14cea71847f592b30f5e6512325198fcedbb2c406859acbc15384c32a71c3cbc62a0ecd2c26b4f9e62
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webpacker (6.0.0.rc.1)
4
+ webpacker (6.0.0.rc.2)
5
5
  activesupport (>= 5.2)
6
6
  rack-proxy (>= 0.6.1)
7
7
  railties (>= 5.2)
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 coexists with the asset pipeline,
14
- as the primary purpose for webpack is app-like JavaScript, not images, CSS, or
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, 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
 
@@ -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)
@@ -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 either add Webpacker during setup of a new Rails 5.1+ application
85
- using new `--webpack` option:
81
+ You can configure a new Rails application with Webpacker right from the start using the `--webpack` option:
86
82
 
87
83
  ```bash
88
- # Available Rails 5.1+
89
84
  rails new myapp --webpack
90
85
  ```
91
86
 
92
- Or add it to your `Gemfile`:
87
+ Or you can add it later by changing your `Gemfile`:
93
88
 
94
89
  ```ruby
95
90
  # Gemfile
96
- gem 'webpacker', '~> 6.x'
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
- Finally, run the following to install Webpacker:
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
- yarn upgrade
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:
@@ -162,8 +148,7 @@ The result looks like this:
162
148
  <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
163
149
  ```
164
150
 
165
- **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
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.
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.
167
152
 
168
153
  ```erb
169
154
  <%# DO %>
@@ -207,39 +192,19 @@ If you want to use images in your stylesheets:
207
192
  ```
208
193
 
209
194
  #### 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
195
 
215
- **Note:** In order for your styles or static assets files to be available in your view,
216
- you would need to link them in your "pack" or entry file. Otherwise, Webpack won't know
217
- to package up those files.
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
+
218
200
 
219
201
  ### Development
220
202
 
221
- Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
222
- Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js`
223
- executables to ensure that the right configuration files and environmental variables
224
- are loaded based on your environment.
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 `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/).
243
208
 
244
209
  ```bash
245
210
  # webpack dev server
@@ -252,25 +217,15 @@ on how to configure rails/webpacker for Fast Refresh with React, see article
252
217
  ./bin/webpack
253
218
  ```
254
219
 
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`
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`
259
221
 
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.
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.
266
223
 
267
224
  ```bash
268
225
  WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server
269
226
  ```
270
227
 
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:
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:
274
229
 
275
230
  ```bash
276
231
  WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
@@ -279,23 +234,20 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
279
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:
280
235
 
281
236
  ```ruby
282
- Rails.application.config.content_security_policy do |policy|
283
- policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
284
- end
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
285
240
  ```
286
241
 
287
242
  **Note:** Don't forget to prefix `ruby` when running these binstubs on Windows
288
243
 
244
+
289
245
  ### Webpack Configuration
290
246
 
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
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
294
248
  `config/webpack/base.js` file.
295
249
 
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.
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.
299
251
 
300
252
  Here is how you can modify webpack configuration:
301
253
 
@@ -326,8 +278,7 @@ const customConfig = require('./custom')
326
278
  module.exports = merge(webpackConfig, customConfig)
327
279
  ```
328
280
 
329
- If you need access to configs within Webpacker's configuration,
330
- you can import them like so:
281
+ If you need access to configs within Webpacker's configuration, you can import them like so:
331
282
 
332
283
  ```js
333
284
  // config/webpack/base.js
@@ -340,11 +291,51 @@ console.log(webpackConfig.source_path)
340
291
  console.log(JSON.stringify(webpackConfig, undefined, 2))
341
292
  ```
342
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
+
343
309
  ### Integrations
344
310
 
345
- Webpacker out of the box supports JS and static assets (fonts, images etc.)
346
- compilation. To enable support for CoffeeScript or TypeScript install
347
- relevant packages:
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
+ ```
348
339
 
349
340
  #### CoffeeScript
350
341
 
@@ -358,6 +349,12 @@ yarn add coffeescript coffee-loader
358
349
  yarn add typescript @babel/preset-typescript
359
350
  ```
360
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
+
361
358
  Add tsconfig.json
362
359
 
363
360
  ```json
@@ -382,12 +379,6 @@ Add tsconfig.json
382
379
  }
383
380
  ```
384
381
 
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
382
  Then modify the webpack config to use it as a plugin:
392
383
 
393
384
  ```js
@@ -454,39 +445,6 @@ yarn add less less-loader
454
445
  yarn add stylus stylus-loader
455
446
  ```
456
447
 
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
448
  #### Other frameworks
491
449
 
492
450
  Please follow webpack integration guide for relevant framework or library,
@@ -524,6 +482,7 @@ const vueConfig = require('./rules/vue')
524
482
  module.exports = merge(vueConfig, webpackConfig)
525
483
  ```
526
484
 
485
+
527
486
  ### Custom Rails environments
528
487
 
529
488
  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 +503,7 @@ staging:
544
503
  public_output_path: packs-staging
545
504
  ```
546
505
 
547
- or, 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`.
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`.
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`.
549
507
 
550
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:
551
509
 
@@ -553,15 +511,13 @@ For example, the below command will compile assets in production mode but will u
553
511
  RAILS_ENV=staging bundle exec rails assets:precompile
554
512
  ```
555
513
 
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
514
+ And, this will compile in development mode and load configuration for cucumber environment if defined in webpacker.yml or fallback to production configuration
558
515
 
559
516
  ```bash
560
517
  RAILS_ENV=cucumber NODE_ENV=development bundle exec rails assets:precompile
561
518
  ```
562
519
 
563
- Please note, binstubs compiles in development mode however rake tasks
564
- compiles in production mode.
520
+ Please note, binstubs compiles in development mode however rake tasks compiles in production mode.
565
521
 
566
522
  ```bash
567
523
  # Compiles in development mode unless NODE_ENV is specified, per the binstub source
@@ -600,19 +556,11 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
600
556
 
601
557
  ## Paths
602
558
 
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.
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.
606
560
 
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`.
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`.
613
562
 
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:
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:
616
564
 
617
565
  ```yml
618
566
  # config/webpacker.yml
@@ -630,16 +578,11 @@ development:
630
578
  port: 3035
631
579
  ```
632
580
 
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.
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.
636
582
 
637
583
  ### Additional paths
638
584
 
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
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
643
586
  add additional paths that webpack should look up when resolving modules:
644
587
 
645
588
  ```yml
@@ -654,12 +597,11 @@ import 'stylesheets/main'
654
597
  import 'images/rails.png'
655
598
  ```
656
599
 
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
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
660
601
 
661
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.
662
603
 
604
+
663
605
  ## Deployment
664
606
 
665
607
  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 +610,19 @@ When compiling assets for production on a remote server, such as a continuous in
668
610
 
669
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.
670
612
 
613
+
671
614
  ## Troubleshooting
672
615
 
673
616
  See the doc page for [Troubleshooting](./docs/troubleshooting.md).
674
617
 
618
+
675
619
  ## Contributing
676
620
 
677
621
  [![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
678
622
 
679
623
  We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
680
624
 
625
+
681
626
  ## License
682
627
 
683
628
  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 `yarn link @rails/webpacker`
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/v6_upgrade.md CHANGED
@@ -17,31 +17,31 @@ This means you have to configure integration with frameworks yourself, but webpa
17
17
  3. Rename `config/webpack` to `config/webpack_old`
18
18
  4. Rename `config/webpacker.yml` to `config/webpacker_old.yml`
19
19
  5. Uninstall the current version of `webpack-dev-server`: `yarn remove webpack-dev-server`
20
- 6. Upgrade the Webpacker Ruby gem and NPM package
20
+ 6. Remove .browserslistrc from the root of your Rails app
21
+ 7. Upgrade the Webpacker Ruby gem and NPM package
21
22
 
22
23
  Note: [Check the releases page to verify the latest version](https://github.com/rails/webpacker/releases), and make sure to install identical version numbers of webpacker gem and `@rails/webpacker` npm package. (Gems use a period and packages use a dot between the main version number and the beta version.)
23
24
 
24
- Example going to a specific (beta) version:
25
+ Example going to a specific version:
25
26
 
26
27
  ```ruby
27
28
  # Gemfile
28
- gem 'webpacker', '6.0.0.beta.7'
29
+ gem 'webpacker', '6.0.0.rc.2'
29
30
  ```
30
31
 
31
- Do *not* specify `'^6.0.0.beta.7'` (with a caret) or you will accidentally install `6.0.0.pre.2` which is months older than `beta.7`.
32
-
33
32
  ```bash
34
33
  bundle install
35
34
  ```
36
35
 
37
36
  ```bash
38
- yarn add @rails/webpacker@6.0.0-beta.7 --exact
37
+ yarn add @rails/webpacker@6.0.0-rc.2 --exact
39
38
  ```
40
39
 
41
40
  ```bash
42
41
  bundle exec rails webpacker:install
43
42
  ```
44
- 7. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` or `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932).
43
+
44
+ 7. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` or `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). 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`.
45
45
  8. If you are using any integrations like `css`, `React` or `TypeScript`. Please see https://github.com/rails/webpacker#integrations section on how they work in v6.
46
46
  9. Copy over any custom webpack config from `config/webpack_old`. Common code previously called 'environment' should be changed to 'base', and import `environment` changed to `webpackConfig`.
47
47
 
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ APP_ROOT = File.expand_path("..", __dir__)
4
+ Dir.chdir(APP_ROOT) do
5
+ yarn = ENV["PATH"].split(File::PATH_SEPARATOR).
6
+ select { |dir| File.expand_path(dir) != __dir__ }.
7
+ product(["yarn", "yarnpkg", "yarn.cmd", "yarn.ps1"]).
8
+ map { |dir, file| File.expand_path(file, dir) }.
9
+ find { |file| File.executable?(file) }
10
+
11
+ if yarn
12
+ exec yarn, *ARGV
13
+ else
14
+ $stderr.puts "Yarn executable was not detected in the system."
15
+ $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
16
+ exit 1
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "app",
3
+ "private": true,
4
+ "dependencies": {
5
+ "@rails/activestorage": "latest",
6
+ "@rails/actioncable": "latest"
7
+ },
8
+ "version": "0.1.0",
9
+ "babel": {
10
+ "presets": [
11
+ "./node_modules/@rails/webpacker/package/babel/preset.js"
12
+ ]
13
+ },
14
+ "browserslist": [
15
+ "defaults"
16
+ ]
17
+ }
@@ -1,5 +1,6 @@
1
1
  # Install Webpacker
2
2
  copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml"
3
+ copy_file "#{__dir__}/package.json", "package.json"
3
4
 
4
5
  say "Copying webpack core config"
5
6
  directory "#{__dir__}/config/webpack", "config/webpack"
@@ -26,6 +27,32 @@ if File.exists?(git_ignore_path)
26
27
  end
27
28
  end
28
29
 
30
+ if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
31
+ say "Add JavaScript include tag in application layout"
32
+ insert_into_file app_layout_path.to_s, %(\n <%= javascript_pack_tag "application" %>), before: /\s*<\/head>/
33
+ else
34
+ say "Default application.html.erb is missing!", :red
35
+ say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
36
+ end
37
+
38
+ if (setup_path = Rails.root.join("bin/setup")).exist?
39
+ say "Run bin/yarn during bin/setup"
40
+ insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)
41
+
42
+ # Install JavaScript dependencies
43
+ system! "bin/yarn"
44
+ RUBY
45
+ end
46
+
47
+ if (asset_config_path = Rails.root.join("config/initializers/assets.rb")).exist?
48
+ say "Add node_modules to the asset load path"
49
+ append_to_file asset_config_path, <<-RUBY
50
+
51
+ # Add node_modules folder to the asset load path.
52
+ Rails.application.config.assets.paths << Rails.root.join("node_modules")
53
+ RUBY
54
+ end
55
+
29
56
  results = []
30
57
 
31
58
  Dir.chdir(Rails.root) do
@@ -49,20 +76,6 @@ Dir.chdir(Rails.root) do
49
76
  results << run("yarn add --dev webpack-dev-server @webpack-cli/serve")
50
77
  end
51
78
 
52
- insert_into_file Rails.root.join("package.json").to_s, before: /\n}\n*$/ do
53
- <<~JSON.chomp
54
- ,
55
- "babel": {
56
- "presets": [
57
- "./node_modules/@rails/webpacker/package/babel/preset.js"
58
- ]
59
- },
60
- "browserslist": [
61
- "defaults"
62
- ]
63
- JSON
64
- end
65
-
66
79
  if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
67
80
  say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
68
81
  say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Duplicate of the yarn tasks still present in Rails until Webpacker <5 have been deprecated
4
+
5
+ namespace :yarn do
6
+ desc "Install all JavaScript dependencies as specified via Yarn"
7
+ task :install do
8
+ # Install only production deps when for not usual envs.
9
+ valid_node_envs = %w[test development production]
10
+ node_env = ENV.fetch("NODE_ENV") do
11
+ valid_node_envs.include?(Rails.env) ? Rails.env : "production"
12
+ end
13
+
14
+ yarn_flags =
15
+ if `#{RbConfig.ruby} "#{Rails.root}/bin/yarn" --version`.start_with?("1")
16
+ "--no-progress --frozen-lockfile"
17
+ else
18
+ "--immutable"
19
+ end
20
+
21
+ system(
22
+ { "NODE_ENV" => node_env },
23
+ "#{RbConfig.ruby} \"#{Rails.root}/bin/yarn\" install #{yarn_flags}",
24
+ exception: true
25
+ )
26
+ rescue Errno::ENOENT
27
+ $stderr.puts "bin/yarn was not found."
28
+ $stderr.puts "Please run `bundle exec rails app:update:bin` to create it."
29
+ exit 1
30
+ end
31
+ end
32
+
33
+ # Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use.
34
+ if Rake::Task.task_defined?("assets:precompile") && File.exist?(Rails.root.join("bin", "yarn"))
35
+ Rake::Task["assets:precompile"].enhance [ "yarn:install" ]
36
+ end
@@ -95,8 +95,8 @@ module Webpacker::Helper
95
95
  #
96
96
  # <%= javascript_pack_tag 'calendar' %>
97
97
  # <%= javascript_pack_tag 'map' %>
98
- def javascript_pack_tag(*names, **options)
99
- javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), defer: true, **options)
98
+ def javascript_pack_tag(*names, defer: true, **options)
99
+ javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options.tap { |o| o[:defer] = defer })
100
100
  end
101
101
 
102
102
  # Creates a link tag, for preloading, that references a given Webpacker asset.
@@ -1,4 +1,4 @@
1
1
  module Webpacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "6.0.0.rc.1".freeze
3
+ VERSION = "6.0.0.rc.2".freeze
4
4
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rails/webpacker",
3
- "version": "6.0.0-rc.1",
3
+ "version": "6.0.0-rc.2",
4
4
  "description": "Use webpack to manage app-like JavaScript modules in Rails",
5
5
  "main": "package/index.js",
6
6
  "files": [
data/test/helper_test.rb CHANGED
@@ -96,12 +96,21 @@ class HelperTest < ActionView::TestCase
96
96
  end
97
97
 
98
98
  def test_javascript_pack_tag
99
+ assert_equal \
100
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
101
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
102
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>\n) +
103
+ %(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js" defer="defer"></script>),
104
+ javascript_pack_tag("application", "bootstrap")
105
+ end
106
+
107
+ def test_javascript_pack_with_no_defer_tag
99
108
  assert_equal \
100
109
  %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
101
110
  %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>\n) +
102
111
  %(<script src="/packs/application-k344a6d59eef8632c9d1.js"></script>\n) +
103
112
  %(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
104
- javascript_pack_tag("application", "bootstrap")
113
+ javascript_pack_tag("application", "bootstrap", defer: false)
105
114
  end
106
115
 
107
116
  def test_javascript_pack_tag_splat
@@ -114,9 +123,9 @@ class HelperTest < ActionView::TestCase
114
123
 
115
124
  def test_javascript_pack_tag_symbol
116
125
  assert_equal \
117
- %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
118
- %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>\n) +
119
- %(<script src="/packs/application-k344a6d59eef8632c9d1.js"></script>),
126
+ %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js" defer="defer"></script>\n) +
127
+ %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js" defer="defer"></script>\n) +
128
+ %(<script src="/packs/application-k344a6d59eef8632c9d1.js" defer="defer"></script>),
120
129
  javascript_pack_tag(:application)
121
130
  end
122
131
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0.rc.1
4
+ version: 6.0.0.rc.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-08-18 00:00:00.000000000 Z
12
+ date: 2021-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -145,12 +145,14 @@ files:
145
145
  - gemfiles/Gemfile-rails.6.1.x
146
146
  - lib/install/bin/webpack
147
147
  - lib/install/bin/webpack-dev-server
148
+ - lib/install/bin/yarn
148
149
  - lib/install/binstubs.rb
149
150
  - lib/install/config/webpack/base.js
150
151
  - lib/install/config/webpack/development.js
151
152
  - lib/install/config/webpack/production.js
152
153
  - lib/install/config/webpack/test.js
153
154
  - lib/install/config/webpacker.yml
155
+ - lib/install/package.json
154
156
  - lib/install/packs/entrypoints/application.js
155
157
  - lib/install/template.rb
156
158
  - lib/tasks/webpacker.rake
@@ -166,6 +168,7 @@ files:
166
168
  - lib/tasks/webpacker/verify_config.rake
167
169
  - lib/tasks/webpacker/verify_install.rake
168
170
  - lib/tasks/webpacker/yarn_install.rake
171
+ - lib/tasks/yarn.rake
169
172
  - lib/webpacker.rb
170
173
  - lib/webpacker/commands.rb
171
174
  - lib/webpacker/compiler.rb
@@ -259,8 +262,8 @@ homepage: https://github.com/rails/webpacker
259
262
  licenses:
260
263
  - MIT
261
264
  metadata:
262
- source_code_uri: https://github.com/rails/webpacker/tree/v6.0.0.rc.1
263
- changelog_uri: https://github.com/rails/webpacker/blob/v6.0.0.rc.1/CHANGELOG.md
265
+ source_code_uri: https://github.com/rails/webpacker/tree/v6.0.0.rc.2
266
+ changelog_uri: https://github.com/rails/webpacker/blob/v6.0.0.rc.2/CHANGELOG.md
264
267
  post_install_message:
265
268
  rdoc_options: []
266
269
  require_paths: