webpacker 4.0.2 → 5.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +8 -8
  3. data/.github/workflows/jest.yml +38 -0
  4. data/.github/workflows/js-lint.yml +39 -0
  5. data/.github/workflows/rubocop.yml +39 -0
  6. data/.github/workflows/ruby.yml +58 -0
  7. data/.gitignore +1 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +115 -14
  10. data/.travis.yml +16 -18
  11. data/CHANGELOG.md +329 -130
  12. data/Gemfile +1 -1
  13. data/Gemfile.lock +127 -105
  14. data/README.md +84 -217
  15. data/docs/assets.md +20 -10
  16. data/docs/css.md +126 -7
  17. data/docs/deployment.md +55 -10
  18. data/docs/docker.md +33 -14
  19. data/docs/engines.md +60 -2
  20. data/docs/env.md +0 -2
  21. data/docs/es6.md +24 -5
  22. data/docs/integrations.md +220 -0
  23. data/docs/target.md +22 -0
  24. data/docs/testing.md +2 -3
  25. data/docs/troubleshooting.md +46 -7
  26. data/docs/typescript.md +93 -26
  27. data/docs/v4-upgrade.md +12 -2
  28. data/docs/webpack-dev-server.md +2 -2
  29. data/docs/webpack.md +21 -7
  30. data/gemfiles/Gemfile-rails-edge +2 -3
  31. data/gemfiles/Gemfile-rails.5.2.x +1 -2
  32. data/gemfiles/Gemfile-rails.6.0.x +9 -0
  33. data/lib/install/bin/webpack +0 -1
  34. data/lib/install/bin/webpack-dev-server +0 -1
  35. data/lib/install/coffee.rb +1 -1
  36. data/lib/install/config/babel.config.js +24 -12
  37. data/lib/install/config/webpacker.yml +3 -6
  38. data/lib/install/elm.rb +3 -3
  39. data/lib/install/erb.rb +2 -2
  40. data/lib/install/examples/angular/hello_angular/polyfills.ts +2 -2
  41. data/lib/install/examples/react/babel.config.js +31 -15
  42. data/lib/install/examples/react/tsconfig.json +2 -1
  43. data/lib/install/examples/svelte/app.svelte +11 -0
  44. data/lib/install/examples/svelte/hello_svelte.js +20 -0
  45. data/lib/install/examples/typescript/tsconfig.json +2 -1
  46. data/lib/install/examples/vue/hello_vue.js +2 -3
  47. data/lib/install/loaders/elm.js +9 -6
  48. data/lib/install/loaders/svelte.js +9 -0
  49. data/lib/install/svelte.rb +29 -0
  50. data/lib/install/template.rb +12 -4
  51. data/lib/install/typescript.rb +6 -13
  52. data/lib/install/vue.rb +1 -1
  53. data/lib/tasks/installers.rake +1 -0
  54. data/lib/tasks/webpacker/check_node.rake +15 -8
  55. data/lib/tasks/webpacker/check_yarn.rake +16 -10
  56. data/lib/tasks/webpacker/clean.rake +25 -0
  57. data/lib/tasks/webpacker/clobber.rake +8 -4
  58. data/lib/tasks/webpacker/compile.rake +10 -15
  59. data/lib/tasks/webpacker/yarn_install.rake +11 -1
  60. data/lib/tasks/webpacker.rake +2 -0
  61. data/lib/webpacker/commands.rb +53 -1
  62. data/lib/webpacker/compiler.rb +24 -9
  63. data/lib/webpacker/configuration.rb +36 -13
  64. data/lib/webpacker/dev_server.rb +1 -1
  65. data/lib/webpacker/dev_server_proxy.rb +5 -9
  66. data/lib/webpacker/dev_server_runner.rb +10 -4
  67. data/lib/webpacker/env.rb +6 -2
  68. data/lib/webpacker/helper.rb +71 -26
  69. data/lib/webpacker/manifest.rb +4 -4
  70. data/lib/webpacker/railtie.rb +6 -43
  71. data/lib/webpacker/runner.rb +1 -0
  72. data/lib/webpacker/version.rb +1 -1
  73. data/lib/webpacker/webpack_runner.rb +6 -0
  74. data/lib/webpacker.rb +9 -1
  75. data/package/__tests__/config.js +12 -24
  76. data/package/__tests__/dev_server.js +2 -0
  77. data/package/__tests__/development.js +14 -1
  78. data/package/config.js +6 -11
  79. data/package/configPath.js +3 -0
  80. data/package/config_types/config_list.js +3 -3
  81. data/package/config_types/config_object.js +1 -1
  82. data/package/dev_server.js +1 -1
  83. data/package/env.js +1 -2
  84. data/package/environments/__tests__/base.js +30 -3
  85. data/package/environments/base.js +20 -10
  86. data/package/environments/development.js +39 -37
  87. data/package/environments/production.js +12 -2
  88. data/package/rules/babel.js +12 -5
  89. data/package/rules/file.js +3 -2
  90. data/package/rules/node_modules.js +3 -4
  91. data/package/rules/sass.js +11 -2
  92. data/package/utils/__tests__/get_style_rule.js +9 -0
  93. data/package/utils/deep_merge.js +5 -5
  94. data/package/utils/get_style_rule.js +7 -12
  95. data/package/utils/helpers.js +10 -10
  96. data/package.json +43 -42
  97. data/test/command_test.rb +6 -0
  98. data/test/compiler_test.rb +10 -6
  99. data/test/configuration_test.rb +40 -30
  100. data/test/dev_server_runner_test.rb +1 -1
  101. data/test/dev_server_test.rb +22 -0
  102. data/test/helper_test.rb +58 -9
  103. data/test/manifest_test.rb +37 -6
  104. data/test/rake_tasks_test.rb +17 -0
  105. data/test/test_app/app/javascript/packs/multi_entry.css +4 -0
  106. data/test/test_app/app/javascript/packs/multi_entry.js +4 -0
  107. data/test/test_app/bin/webpack +0 -1
  108. data/test/test_app/bin/webpack-dev-server +0 -1
  109. data/test/test_app/config/application.rb +0 -1
  110. data/test/test_app/config/webpacker.yml +8 -1
  111. data/test/test_app/public/packs/manifest.json +4 -0
  112. data/test/webpack_runner_test.rb +1 -1
  113. data/webpacker.gemspec +7 -4
  114. data/yarn.lock +4552 -4077
  115. metadata +73 -19
  116. data/gemfiles/Gemfile-rails.4.2.x +0 -10
  117. data/gemfiles/Gemfile-rails.5.0.x +0 -10
  118. data/gemfiles/Gemfile-rails.5.1.x +0 -10
  119. data/lib/install/loaders/typescript.js +0 -11
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Webpacker
2
2
 
3
- ![travis-ci status](https://api.travis-ci.org/rails/webpacker.svg?branch=master)
4
- [![node.js](https://img.shields.io/badge/node-%3E%3D%206.14.0-brightgreen.svg)](https://nodejs.org/en/)
5
- [![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://github.com/rails/webpacker)
3
+ [![Build Status](https://travis-ci.org/rails/webpacker.svg?branch=master)](https://travis-ci.org/rails/webpacker)
4
+ [![node.js](https://img.shields.io/badge/node-%3E%3D%2010.17.0-brightgreen.svg)](https://www.npmjs.com/package/@rails/webpacker)
5
+ [![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://rubygems.org/gems/webpacker)
6
6
 
7
7
  Webpacker makes it easy to use the JavaScript pre-processor and bundler
8
8
  [webpack 4.x.x+](https://webpack.js.org/)
@@ -13,10 +13,11 @@ even JavaScript Sprinkles (that all continues to live in app/assets).
13
13
  However, it is possible to use Webpacker for CSS, images and fonts assets as well,
14
14
  in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks.
15
15
 
16
- **NOTE:** The master branch now hosts the code for v4.x.x. Please refer to [3-x-stable](https://github.com/rails/webpacker/tree/3-x-stable) branch for 3.x documentation. See the [v4-upgrade guide](docs/v4-upgrade.md) for an overview of the changes.
16
+ **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.
17
17
 
18
18
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
19
19
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
20
+
20
21
  ## Table of Contents
21
22
 
22
23
  - [Prerequisites](#prerequisites)
@@ -27,15 +28,16 @@ in which case you may not even need the asset pipeline. This is mostly relevant
27
28
  - [Webpack Configuration](#webpack-configuration)
28
29
  - [Custom Rails environments](#custom-rails-environments)
29
30
  - [Upgrading](#upgrading)
30
- - [Yarn Integrity](#yarn-integrity)
31
31
  - [Integrations](#integrations)
32
- - [React](#react)
33
- - [Angular with TypeScript](#angular-with-typescript)
34
- - [Vue](#vue)
35
- - [Elm](#elm)
36
- - [Stimulus](#stimulus)
37
- - [Coffeescript](#coffeescript)
38
- - [Erb](#erb)
32
+ - [React](./docs/integrations.md#react)
33
+ - [Angular with TypeScript](./docs/integrations.md#angular-with-typescript)
34
+ - [Vue](./docs/integrations.md#vue)
35
+ - [Elm](./docs/integrations.md#elm)
36
+ - [Stimulus](./docs/integrations.md#stimulus)
37
+ - [Svelte](./docs/integrations.md#svelte)
38
+ - [Typescript](./docs/typescript.md)
39
+ - [CoffeeScript](./docs/integrations.md#coffeescript)
40
+ - [Erb](./docs/integrations.md#erb)
39
41
  - [Paths](#paths)
40
42
  - [Resolved](#resolved)
41
43
  - [Watched](#watched)
@@ -46,29 +48,26 @@ in which case you may not even need the asset pipeline. This is mostly relevant
46
48
 
47
49
  <!-- END doctoc generated TOC please keep comment here to allow auto update -->
48
50
 
49
-
50
51
  ## Prerequisites
51
52
 
52
- * Ruby 2.2+
53
- * Rails 4.2+
54
- * Node.js 6.14.4+
55
- * Yarn 1.x+
56
-
53
+ - Ruby 2.4+
54
+ - Rails 5.2+
55
+ - Node.js 10.17.0+
56
+ - Yarn 1.x+
57
57
 
58
58
  ## Features
59
59
 
60
- * [webpack 4.x.x](https://webpack.js.org/)
61
- * ES6 with [babel](https://babeljs.io/)
62
- * Automatic code splitting using multiple entry points
63
- * Stylesheets - Sass and CSS
64
- * Images and fonts
65
- * PostCSS - Auto-Prefixer
66
- * Asset compression, source-maps, and minification
67
- * CDN support
68
- * React, Angular, Elm and Vue support out-of-the-box
69
- * Rails view helpers
70
- * Extensible and configurable
71
-
60
+ - [webpack 4.x.x](https://webpack.js.org/)
61
+ - ES6 with [babel](https://babeljs.io/)
62
+ - Automatic code splitting using multiple entry points
63
+ - Stylesheets - Sass and CSS
64
+ - Images and fonts
65
+ - PostCSS - Auto-Prefixer
66
+ - Asset compression, source-maps, and minification
67
+ - CDN support
68
+ - React, Angular, Elm and Vue support out-of-the-box
69
+ - Rails view helpers
70
+ - Extensible and configurable
72
71
 
73
72
  ## Installation
74
73
 
@@ -84,15 +83,12 @@ Or add it to your `Gemfile`:
84
83
 
85
84
  ```ruby
86
85
  # Gemfile
87
- gem 'webpacker', '~> 3.5'
86
+ gem 'webpacker', '~> 5.x'
88
87
 
89
88
  # OR if you prefer to use master
90
89
  gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
91
90
  yarn add https://github.com/rails/webpacker.git
92
-
93
- # OR to try out 4.x pre-release
94
- gem 'webpacker', '>= 4.0.x'
95
- yarn add @rails/webpacker@next
91
+ yarn add core-js regenerator-runtime
96
92
  ```
97
93
 
98
94
  Finally, run the following to install Webpacker:
@@ -111,6 +107,12 @@ Optional: To fix ["unmet peer dependency" warnings](https://github.com/rails/web
111
107
  yarn upgrade
112
108
  ```
113
109
 
110
+ 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:
111
+
112
+ ```bash
113
+ yarn install
114
+ ```
115
+
114
116
  ### Usage
115
117
 
116
118
  Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
@@ -120,12 +122,22 @@ app/javascript:
120
122
  ├── packs:
121
123
  │ # only webpack entry files here
122
124
  │ └── application.js
123
- └── src:
124
125
  │ └── application.css
126
+ └── src:
127
+ │ └── my_component.js
128
+ └── stylesheets:
129
+ │ └── my_styles.css
125
130
  └── images:
126
131
  └── logo.svg
127
132
  ```
128
133
 
134
+ In `/packs/application.js`, include this at the top of the file:
135
+
136
+ ```js
137
+ import 'core-js/stable'
138
+ import 'regenerator-runtime/runtime'
139
+ ```
140
+
129
141
  You can then link the JavaScript pack in Rails views using the `javascript_pack_tag` helper.
130
142
  If you have styles imported in your pack file, you can link them by using `stylesheet_pack_tag`:
131
143
 
@@ -170,7 +182,6 @@ helper otherwise you will get duplicated chunks on the page.
170
182
  **Note:** In order for your styles or static assets files to be available in your view,
171
183
  you would need to link them in your "pack" or entry file.
172
184
 
173
-
174
185
  ### Development
175
186
 
176
187
  Webpacker ships with two binstubs: `./bin/webpack` and `./bin/webpack-dev-server`.
@@ -235,7 +246,6 @@ WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server
235
246
 
236
247
  See [docs/webpack](docs/webpack.md) for modifying webpack configuration and loaders.
237
248
 
238
-
239
249
  ### Custom Rails environments
240
250
 
241
251
  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.
@@ -285,7 +295,6 @@ bundle exec rails assets:precompile
285
295
  bundle exec rails webpacker:compile
286
296
  ```
287
297
 
288
-
289
298
  ### Upgrading
290
299
 
291
300
  You can run following commands to upgrade Webpacker to the latest stable version. This process involves upgrading the gem and related JavaScript packages:
@@ -294,173 +303,29 @@ You can run following commands to upgrade Webpacker to the latest stable version
294
303
  bundle update webpacker
295
304
  rails webpacker:binstubs
296
305
  yarn upgrade @rails/webpacker --latest
297
- yarn add webpack-dev-server@^3.1.14
306
+ yarn upgrade webpack-dev-server --latest
298
307
 
299
- # Or to install a latest release (including pre-releases)
308
+ # Or to install the latest release (including pre-releases)
300
309
  yarn add @rails/webpacker@next
301
310
  ```
302
311
 
303
- ### Yarn Integrity
304
-
305
- By default, in development, webpacker runs a yarn integrity check to ensure that all local JavaScript packages are up-to-date. This is similar to what bundler does currently in Rails, but for JavaScript packages. If your system is out of date, then Rails will not initialize. You will be asked to upgrade your local JavaScript packages by running `yarn install`.
306
-
307
- To turn off this option, you will need to change the default setting in `config/webpacker.yml`:
308
-
309
- ```yaml
310
- # config/webpacker.yml
311
- development:
312
- ...
313
- # Verifies that versions and hashed value of the package contents in the project's package.json
314
- check_yarn_integrity: false
315
- ```
316
-
317
- You may also turn on this feature by adding the config option for any Rails environment in `config/webpacker.yml`:
318
-
319
- ```yaml
320
- check_yarn_integrity: true
321
- ```
322
-
323
312
  ## Integrations
324
313
 
325
- Webpacker ships with basic out-of-the-box integration for React, Angular, Vue and Elm.
326
- You can see a list of available commands/tasks by running `bundle exec rails webpacker`:
327
-
328
- ### React
329
-
330
- To use Webpacker with [React](https://facebook.github.io/react/), create a
331
- new Rails 5.1+ app using `--webpack=react` option:
332
-
333
- ```bash
334
- # Rails 5.1+
335
- rails new myapp --webpack=react
336
- ```
337
-
338
- (or run `bundle exec rails webpacker:install:react` in a existing Rails app already
339
- setup with Webpacker).
340
-
341
- The installer will add all relevant dependencies using Yarn, changes
342
- to the configuration files, and an example React component to your
343
- project in `app/javascript/packs` so that you can experiment with React right away.
344
-
345
-
346
- ### Angular with TypeScript
347
-
348
- To use Webpacker with [Angular](https://angular.io/), create a
349
- new Rails 5.1+ app using `--webpack=angular` option:
350
-
351
- ```bash
352
- # Rails 5.1+
353
- rails new myapp --webpack=angular
354
- ```
355
-
356
- (or run `bundle exec rails webpacker:install:angular` on a Rails app already
357
- setup with Webpacker).
358
-
359
- The installer will add the TypeScript and Angular core libraries using Yarn alongside
360
- a few changes to the configuration files. An example component written in
361
- TypeScript will also be added to your project in `app/javascript` so that
362
- you can experiment with Angular right away.
363
-
364
- By default, Angular uses a JIT compiler for development environment. This
365
- compiler is not compatible with restrictive CSP (Content Security
366
- Policy) environments like Rails 5.2+. You can use Angular AOT compiler
367
- in development with the [@ngtools/webpack](https://www.npmjs.com/package/@ngtools/webpack#usage) plugin.
368
-
369
- Alternatively if you're using Rails 5.2+ you can enable `unsafe-eval` rule for your
370
- development environment. This can be done in the `config/initializers/content_security_policy.rb`
371
- with the following code:
314
+ Webpacker ships with basic out-of-the-box integration. You can see a list of available commands/tasks by running `bundle exec rails webpacker`.
372
315
 
373
- ```ruby
374
- Rails.application.config.content_security_policy do |policy|
375
- if Rails.env.development?
376
- policy.script_src :self, :https, :unsafe_eval
377
- else
378
- policy.script_src :self, :https
379
- end
380
- end
381
- ```
382
-
383
-
384
- ### Vue
385
-
386
- To use Webpacker with [Vue](https://vuejs.org/), create a
387
- new Rails 5.1+ app using `--webpack=vue` option:
388
-
389
- ```bash
390
- # Rails 5.1+
391
- rails new myapp --webpack=vue
392
- ```
393
- (or run `bundle exec rails webpacker:install:vue` on a Rails app already setup with Webpacker).
394
-
395
- The installer will add Vue and its required libraries using Yarn alongside
396
- automatically applying changes needed to the configuration files. An example component will
397
- be added to your project in `app/javascript` so that you can experiment with Vue right away.
398
-
399
- If you're using Rails 5.2+ you'll need to enable `unsafe-eval` rule for your development environment.
400
- This can be done in the `config/initializers/content_security_policy.rb` with the following
401
- configuration:
402
-
403
- ```ruby
404
- Rails.application.config.content_security_policy do |policy|
405
- if Rails.env.development?
406
- policy.script_src :self, :https, :unsafe_eval
407
- else
408
- policy.script_src :self, :https
409
- end
410
- end
411
- ```
412
- You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments).
413
-
414
-
415
- ### Elm
416
-
417
- To use Webpacker with [Elm](http://elm-lang.org), create a
418
- new Rails 5.1+ app using `--webpack=elm` option:
419
-
420
- ```
421
- # Rails 5.1+
422
- rails new myapp --webpack=elm
423
- ```
424
-
425
- (or run `bundle exec rails webpacker:install:elm` on a Rails app already setup with Webpacker).
426
-
427
- The Elm library and its core packages will be added via Yarn and Elm.
428
- An example `Main.elm` app will also be added to your project in `app/javascript`
429
- so that you can experiment with Elm right away.
316
+ Included install integrations:
430
317
 
431
- ### Stimulus
432
-
433
- To use Webpacker with [Stimulus](http://stimulusjs.org), create a
434
- new Rails 5.1+ app using `--webpack=stimulus` option:
435
-
436
- ```
437
- # Rails 5.1+
438
- rails new myapp --webpack=stimulus
439
- ```
440
-
441
- (or run `bundle exec rails webpacker:install:stimulus` on a Rails app already setup with Webpacker).
442
-
443
- Please read [The Stimulus Handbook](https://stimulusjs.org/handbook/introduction) or learn more about its source code at https://github.com/stimulusjs/stimulus
444
-
445
- ### Coffeescript
446
-
447
- To add [Coffeescript](http://coffeescript.org/) support,
448
- run `bundle exec rails webpacker:install:coffee` on a Rails app already
449
- setup with Webpacker.
450
-
451
- An example `hello_coffee.coffee` file will also be added to your project
452
- in `app/javascript/packs` so that you can experiment with Coffeescript right away.
453
-
454
- ### Erb
455
-
456
- To add [Erb](https://apidock.com/ruby/ERB) support in your JS templates,
457
- run `bundle exec rails webpacker:install:erb` on a Rails app already
458
- setup with Webpacker.
459
-
460
- An example `hello_erb.js.erb` file will also be added to your project
461
- in `app/javascript/packs` so that you can experiment with Erb-flavoured
462
- javascript right away.
318
+ - [React](./docs/integrations.md#React)
319
+ - [Angular with TypeScript](./docs/integrations.md#Angular-with-TypeScript)
320
+ - [Vue](./docs/integrations.md#Vue)
321
+ - [Elm](./docs/integrations.md#Elm)
322
+ - [Svelte](./docs/integrations.md#Svelte)
323
+ - [Stimulus](./docs/integrations.md#Stimulus)
324
+ - [CoffeeScript](./docs/integrations.md#CoffeeScript)
325
+ - [Typescript](./docs/typescript.md)
326
+ - [Erb](./docs/integrations.md#Erb)
463
327
 
328
+ See [Integrations](./docs/integrations.md) for further details.
464
329
 
465
330
  ## Paths
466
331
 
@@ -497,17 +362,16 @@ development:
497
362
 
498
363
  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.
499
364
 
500
-
501
365
  ### Resolved
502
366
 
503
367
  If you are adding Webpacker to an existing app that has most of the assets inside
504
368
  `app/assets` or inside an engine, and you want to share that
505
- with webpack modules, you can use the `resolved_paths`
369
+ with webpack modules, you can use the `additional_paths`
506
370
  option available in `config/webpacker.yml`. This lets you
507
371
  add additional paths that webpack should lookup when resolving modules:
508
372
 
509
373
  ```yml
510
- resolved_paths: ['app/assets']
374
+ additional_paths: ['app/assets']
511
375
  ```
512
376
 
513
377
  You can then import these items inside your modules like so:
@@ -522,35 +386,38 @@ import 'images/rails.png'
522
386
  will make the compilation slow, consider adding specific paths instead of
523
387
  whole parent directory if you just need to reference one or two modules
524
388
 
525
-
526
- ### Watched
527
-
528
- By default, the lazy compilation is cached until a file is changed under your
529
- tracked paths. You can configure which paths are tracked
530
- by adding new paths to `watched_paths` array. This is much like Rails' `autoload_paths`:
531
-
532
- ```rb
533
- # config/initializers/webpacker.rb
534
- # or config/application.rb
535
- Webpacker::Compiler.watched_paths << 'bower_components'
536
- ```
537
-
538
-
539
389
  ## Deployment
540
390
 
541
391
  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).
542
392
 
393
+ 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.
543
394
 
544
395
  ## Docs
545
396
 
546
- You can find more detailed guides under [docs](./docs).
547
-
397
+ - [Development](https://github.com/rails/webpacker#development)
398
+ - [Webpack](./docs/webpack.md)
399
+ - [Webpack-dev-server](./docs/webpack-dev-server.md)
400
+ - [Environment Variables](./docs/env.md)
401
+ - [Folder Structure](./docs/folder-structure.md)
402
+ - [Assets](./docs/assets.md) - [CSS, Sass and SCSS](./docs/css.md) - [ES6](./docs/es6.md), [Target browsers](./docs/target.md)
403
+ - [Props](./docs/props.md)
404
+ - [Typescript](./docs/typescript.md)
405
+ - [Yarn](./docs/yarn.md)
406
+ - [Misc](./docs/misc.md)
407
+ - [Deployment](./docs/deployment.md)
408
+ - [Docker](./docs/docker.md)
409
+ - [Using in Rails engines](./docs/engines.md)
410
+ - [Webpacker on Cloud9](./docs/cloud9.md)
411
+ - [Testing](./docs/testing.md)
412
+ - [Troubleshooting](./docs/troubleshooting.md)
413
+ - [v3 to v4 Upgrade Guide](./docs/v4-upgrade.md)
548
414
 
549
415
  ## Contributing
416
+
550
417
  [![Code Helpers](https://www.codetriage.com/rails/webpacker/badges/users.svg)](https://www.codetriage.com/rails/webpacker)
551
418
 
552
419
  We encourage you to contribute to Webpacker! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines about how to proceed.
553
420
 
554
-
555
421
  ## License
422
+
556
423
  Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
data/docs/assets.md CHANGED
@@ -84,26 +84,36 @@ import 'assets/stylesheets/bar'
84
84
  ## Link in your Rails views
85
85
 
86
86
  You can also link `js/images/styles/fonts` used within your js app in views using
87
- `asset_pack_path` helper. This helper is useful in cases where you just want to
87
+ `asset_pack_path` and `image_pack_tag` helpers. These helpers are useful in cases where you just want to
88
88
  create a `<link rel="prefetch">` or `<img />` for an asset.
89
89
 
90
90
  ```yml
91
91
  app/javascript:
92
92
  - packs
93
- - hello_react.js
94
- - styles
95
- - hello_react.css
93
+ - app.js
96
94
  - images
97
95
  - calendar.png
98
96
  ```
99
97
 
98
+ ```js
99
+ // app/javascript/packs/app.js (or any of your packs)
100
+
101
+ // import all image files in a folder:
102
+ require.context('../images', true)
103
+ ```
104
+
100
105
  ```erb
101
- <%= asset_pack_path 'hello_react.css' %>
102
- <%# => "/packs/hello_react.css" %>
106
+ <%# Rails view, for example app/views/layouts/application.html.erb %>
107
+
108
+ <img src="<%= asset_pack_path 'media/images/calendar.png' %>" />
109
+ <% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
103
110
 
104
- <img src="<%= asset_pack_path 'images/calendar.png' %>" />
105
- <% # => <img src="/packs/images/calendar.png" /> %>
111
+ <%= image_pack_tag 'media/images/calendar.png' %>
112
+ <% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
106
113
 
107
- <%= image_pack_tag 'images/calendar.png' %>
108
- <% # => <img src="/packs/images/calendar.png" /> %>
114
+ <%# no path resolves to default 'images' folder: %>
115
+ <%= image_pack_tag 'calendar.png' %>
116
+ <% # => <img src="/packs/media/images/calendar-k344a6d59eef8632c9d1.png" /> %>
109
117
  ```
118
+
119
+ Note you need to add a `media/` prefix (not `/media/`) to any subfolder structure you might have in `app/javascript`. See more examples in the [tests](https://github.com/rails/webpacker/blob/0b86cadb5ed921e2c1538382e72a236ec30a5d97/test/helper_test.rb#L37).
data/docs/css.md CHANGED
@@ -3,9 +3,66 @@
3
3
 
4
4
  Webpacker supports importing CSS, Sass and SCSS files directly into your JavaScript files.
5
5
 
6
+ Importing and loading styles is a two step process:
7
+
8
+ 1. You need to tell webpack which file(s) it has to compile and know how to load
9
+
10
+ When you do `import '../scss/application.scss'`, you're telling webpack to include `application.scss` in the build. This does not mean it's going to be compiled into your javascript, only that webpack now compiles and knows how to load this file. (How that file compilation is handled is depending on how your loaders (`css-loader`, `sass-loader`, `file-loader`, etc.) are configured.)
11
+
12
+ 2. You need to load those files in your views
13
+
14
+ In order to have styles load in production, you need to include `stylesheet_pack_tag` with the same name as the javascript file that imports the styles.
15
+
16
+ When you do `<%= stylesheet_pack_tag 'application' %>`, that's a run-time inclusion from Rails, where Rails gets the correct "asset path" to that file from webpack.
17
+
6
18
 
7
19
  ## Import global styles into your JS app
8
20
 
21
+ ### Importing CSS as a multi-file pack (Webpacker v5)
22
+
23
+ When you add a CSS/SCSS/SASS file to `app/javascript/packs/` directory, make sure to use the same pack name as its complementary JavaScript pack, e.g. `application.js` and `application.css`. By Webpacker convention (as of Webpacker v5), this will bundle `application.js` and `application.scss` as part of the same entry point (also described as [a multi-file entry point in the webpack docs](https://webpack.js.org/concepts/entry-points/#single-entry-shorthand-syntax)). With this approach, you can avoid importing CSS from JS, if desired.
24
+
25
+ ```
26
+ app/
27
+ javascript/
28
+ packs/
29
+ application.js
30
+ application.scss
31
+ ```
32
+
33
+ ### Importing CSS from CSS
34
+
35
+ You can import additional CSS/SCSS/SASS files from within a CSS file:
36
+
37
+ ```
38
+ app/
39
+ javascript/
40
+ stylesheets/
41
+ application.scss
42
+ posts.scss
43
+ comments.scss
44
+ ```
45
+
46
+ ```css
47
+ /* app/javascript/stylesheets/application.scss */
48
+
49
+ @import './posts';
50
+ @import './comments';
51
+ ```
52
+
53
+ ### Importing CSS provided by an NPM package from SCSS/CSS
54
+
55
+ Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`:
56
+
57
+ ```js
58
+ /* app/javascript/stylesheets/application.scss */
59
+
60
+ @import "flatpickr/dist/flatpickr.css"
61
+ ```
62
+
63
+
64
+ ### Importing CSS from JS
65
+
9
66
  ```sass
10
67
  // app/javascript/hello_react/styles/hello-react.sass
11
68
 
@@ -30,9 +87,19 @@ const Hello = props => (
30
87
  )
31
88
  ```
32
89
 
90
+ ### Importing CSS provided by an NPM package from JS
91
+
92
+ Given your application installs an NPM package that provides CSS, such as `flatpickr`, you can import the CSS file(s) by path from the package directory within `node_modules/`. This is an alternative to importing from within a CSS file, as above:
93
+
94
+ ```js
95
+ // app/javascript/packs/application.js
96
+
97
+ import "flatpickr/dist/flatpickr.css"
98
+ ```
99
+
33
100
  ## Import scoped styles into your JS app
34
101
 
35
- Stylesheets end with `.module.*` is treated as [CSS Modules](https://github.com/css-modules/css-modules).
102
+ Stylesheets that end with `.module.*` are treated as [CSS Modules](https://github.com/css-modules/css-modules).
36
103
 
37
104
  ```sass
38
105
  // app/javascript/hello_react/styles/hello-react.module.sass
@@ -60,6 +127,59 @@ const Hello = props => (
60
127
 
61
128
  **Note:** Declared class is referenced as object property in JavaScript.
62
129
 
130
+ ## Import scoped styles into your TypeScript app
131
+
132
+ Using CSS modules with a TypeScript application requires a few differences from a JavaScript app. The CSS / Sass files are the same:
133
+
134
+ ```sass
135
+ // app/javascript/hello_react/styles/hello-react.module.sass
136
+
137
+ .helloReact
138
+ padding: 20px
139
+ font-size: 12px
140
+ ```
141
+
142
+ There must also be a type definition file for these styles:
143
+
144
+ ```typescript
145
+ export const helloReact: string;
146
+ ```
147
+
148
+ You can then import the styles like this:
149
+
150
+ ```typescript
151
+ // React component example
152
+ // app/javascripts/packs/hello_react.tsx
153
+
154
+ import React from 'react'
155
+ import helloIcon from '../hello_react/images/icon.png'
156
+ import * as styles from '../hello_react/styles/hello-react.module.sass'
157
+
158
+ const Hello = props => (
159
+ <div className={styles.helloReact}>
160
+ <img src={helloIcon} alt="hello-icon" />
161
+ <p>Hello {props.name}!</p>
162
+ </div>
163
+ )
164
+ ```
165
+
166
+ You can automatically generate type definitions for the styles by installing the `typed-scss-modules` as a development dependency:
167
+
168
+ ```
169
+ yarn add typed-scss-modules --dev
170
+ ```
171
+
172
+ Then by adding these lines to your `package.json`:
173
+
174
+ ```
175
+ "scripts": {
176
+ "gen-typings": "yarn run tsm app/javascript/**/*.sass",
177
+ "watch-typings": "yarn run tsm app/javascript/**/*.sass -w"
178
+ },
179
+ ```
180
+
181
+ You can generate the typings for the stylesheet by running the command `yarn gen-typings` when you've finished writing CSS, or run `yarn watch-typings` to have it automatically generate them as you go.
182
+
63
183
 
64
184
  ## Link styles from your Rails views
65
185
 
@@ -82,14 +202,14 @@ You can use Yarn to add bootstrap or any other modules available on npm:
82
202
  yarn add bootstrap
83
203
  ```
84
204
 
85
- Import Bootstrap and theme (optional) CSS in your app/javascript/packs/app.js file:
205
+ Import Bootstrap and theme (optional) CSS in your app/javascript/packs/application.js file:
86
206
 
87
207
  ```js
88
208
  import 'bootstrap/dist/css/bootstrap'
89
209
  import 'bootstrap/dist/css/bootstrap-theme'
90
210
  ```
91
211
 
92
- Or in your app/javascript/app.sass file:
212
+ Or in your app/javascript/packs/application.sass file:
93
213
 
94
214
  ```sass
95
215
  // ~ to tell that this is not a relative import
@@ -148,11 +268,10 @@ const { environment } = require('@rails/webpacker')
148
268
 
149
269
  // resolve-url-loader must be used before sass-loader
150
270
  environment.loaders.get('sass').use.splice(-1, 0, {
151
- loader: 'resolve-url-loader',
152
- options: {
153
- attempts: 1
154
- }
271
+ loader: 'resolve-url-loader'
155
272
  });
273
+
274
+ module.exports = environment
156
275
  ```
157
276
 
158
277
  ## Working with TypeScript