Skip to content

Commit f036017

Browse files
committed
Merge branch 'master' into rebuild
# Conflicts: # src/content/configuration/index.md # src/content/configuration/resolve.md # yarn.lock
2 parents 74ec5bd + 6f78590 commit f036017

18 files changed

+311
-97
lines changed

src/content/api/compiler-hooks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Runs a plugin before the environment is prepared.
8181

8282
`SyncHook`
8383

84-
Executes a plugin a environment setup is complete.
84+
Executes a plugin after the environment setup is complete.
8585

8686

8787
### `beforeRun`

src/content/api/loaders.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ contributors:
66
- jhnns
77
- tbroadley
88
- byzyk
9+
- sokra
10+
- EugeneHlushko
11+
- jantimon
912
---
1013

1114
A loader is just a JavaScript module that exports a function. The [loader runner](https://github.com/webpack/loader-runner) calls this function and passes the result of the previous loader or the resource file into it. The `this` context of the function is filled-in by webpack and the [loader runner](https://github.com/webpack/loader-runner) with some useful methods that allow the loader (among other things) to change its invocation style to async, or get query parameters.
@@ -565,3 +568,64 @@ As you can see below, not only error message, but also details about which loade
565568
W> The loader path in the error is displayed since webpack 4.12
566569
567570
T> All the errors and warnings will be recorded into `stats`. Please see [Stats Data](/api/stats/#errors-and-warnings).
571+
572+
573+
### Inline matchResource
574+
575+
A new inline request syntax was introduced in webpack v4. Prefixing `<match-resource>!=!` to a request will set the `matchResource` for this request.
576+
577+
W> It is not recommended to use this syntax in application code.
578+
Inline request syntax is intended to only be used by loader generated code.
579+
Not following this recommendation will make your code webpack-specific and non-standard.
580+
581+
T> A relative `matchResource` will resolve relative to the current context of the containing module.
582+
583+
When a `matchResource` is set, it will be used to match with the [`module.rules`](/configuration/module/#module-rules) instead of the original resource. This can be useful if further loaders should be applied to the resource, or if the module type need to be changed. It's also displayed in the stats and used for matching [`Rule.issuer`](/configuration/module/#rule-issuer) and [`test` in `splitChunks`](/plugins/split-chunks-plugin/#splitchunks-cachegroups-cachegroup-test).
584+
585+
Example:
586+
587+
__file.js__
588+
589+
```javascript
590+
/* STYLE: body { background: red; } */
591+
console.log('yep');
592+
```
593+
594+
A loader could transform the file into the following file and use the `matchResource` to apply the user-specified CSS processing rules:
595+
596+
__file.js__ (transformed by loader)
597+
598+
```javascript
599+
import './file.js.css!=!extract-style-loader/getStyles!./file.js';
600+
console.log('yep');
601+
```
602+
603+
This will add a dependency to `extract-style-loader/getStyles!./file.js` and treat the result as `file.js.css`. Because [`module.rules`](/configuration/module/#module-rules) has a rule matching `/\.css$/` and it will apply to this dependency.
604+
605+
The loader could look like this:
606+
607+
__extract-style-loader/index.js__
608+
609+
```javascript
610+
const stringifyRequest = require('loader-utils').stringifyRequest;
611+
const getRemainingRequest = require('loader-utils').getRemainingRequest;
612+
const getStylesLoader = require.resolve('./getStyle');
613+
614+
module.exports = function (source) {
615+
if (STYLES_REGEXP.test(source)) {
616+
source = source.replace(STYLES_REGEXP, '');
617+
const remReq = getRemainingRequest(this);
618+
return `import ${stringifyRequest(`${this.resource}.css!=!${getStylesLoader}!${remReq}`)};${source}`;
619+
}
620+
return source;
621+
};
622+
```
623+
624+
__extract-style-loader/getStyles.js__
625+
626+
```javascript
627+
module.exports = function(source) {
628+
const match = STYLES_REGEXP.match(source);
629+
return match[0];
630+
};
631+
```

src/content/api/stats.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ What good would these statistics be without some description of the compiled app
134134
],
135135
"errors": 0, // Number of errors when resolving or processing the module
136136
"failed": false, // Whether or not compilation failed on this module
137-
"id": 0, // The ID of the module (analagous to [`module.id`](/api/module-variables#module-id-commonjs-))
137+
"id": 0, // The ID of the module (analogous to [`module.id`](/api/module-variables#module-id-commonjs-))
138138
"identifier": "(webpack)\\test\\browsertest\\lib\\index.web.js", // A unique ID used internally
139139
"name": "./lib/index.web.js", // Path to the actual file
140140
"optional": false, // All requests to this module are with `try... catch` blocks (irrelevant with ESM)

src/content/concepts/entry-points.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Below is a list of entry configurations and their real-world use cases:
6767

6868
### Separate App and Vendor Entries
6969

70-
T> In webpack version < 4 it was common to add vendors as separate entrypoint to compile it as separate file (in combination with the `CommonsChunkPlugin`). This is discouraged in webpack 4. Instead the `optimization.splitChunks` option takes care of separating vendors and app modules and creating a separate file. __Do not__ create a entry for vendors or other stuff which is not the starting point of execution.
70+
T> In webpack version < 4 it was common to add vendors as separate entrypoint to compile it as separate file (in combination with the `CommonsChunkPlugin`). This is discouraged in webpack 4. Instead the `optimization.splitChunks` option takes care of separating vendors and app modules and creating a separate file. __Do not__ create an entry for vendors or other stuff which is not the starting point of execution.
7171

7272
### Multi Page Application
7373

src/content/concepts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ To get started you only need to understand its __Core Concepts__:
3535

3636
This document is intended to give a __high-level__ overview of these concepts, while providing links to detailed concept specific use cases.
3737

38-
For a better understanding of the ideas behind module bundlers and how they work under the hood consult these resources:
38+
For a better understanding of the ideas behind module bundlers and how they work under the hood, consult these resources:
3939

4040
- [Manually Bundling an Application](https://www.youtube.com/watch?v=UNMkLHzofQI)
4141
- [Live Coding a Simple Module Bundler](https://www.youtube.com/watch?v=Gc9-7PBqOC8)

src/content/concepts/mode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Option | Description
4242
`production` | Sets `process.env.NODE_ENV` on `DefinePlugin` to value `production`. Enables `FlagDependencyUsagePlugin`, `FlagIncludedChunksPlugin`, `ModuleConcatenationPlugin`, `NoEmitOnErrorsPlugin`, `OccurrenceOrderPlugin`, `SideEffectsFlagPlugin` and `TerserPlugin`.
4343
`none` | Opts out of any default optimization options
4444

45-
If not set, webpack sets `production` as the default value for `mode`. The supported values for mode are:
45+
If not set, webpack sets `production` as the default value for `mode`.
4646

4747
T> Please remember that setting `NODE_ENV` doesn't automatically set `mode`.
4848

src/content/configuration/configuration-languages.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ __package.json__
8484
```json
8585
{
8686
"scripts": {
87-
"build": "TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack"
87+
"build": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack"
8888
}
8989
}
9090
```
9191

92+
W> We had been getting reports that `TS_NODE_PROJECT` might not work with `"TS_NODE_PROJECT" unrecognized command` error. Therefore running it with `cross-env` seems to fix the issue, for more info [see this issue](https://github.com/webpack/webpack.js.org/issues/2733).
93+
9294

9395
## CoffeeScript
9496

src/content/configuration/externals.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ contributors:
1212

1313
The `externals` configuration option provides a way of excluding dependencies from the output bundles. Instead, the created bundle relies on that dependency to be present in the consumer's environment. This feature is typically most useful to __library developers__, however there are a variety of applications for it.
1414

15-
T> __consumer__ here is any end user application that includes the library that you have bundled using webpack.
15+
T> __consumer__ here is any end-user application that includes the library that you have bundled using webpack.
1616

1717

1818
## `externals`
@@ -117,7 +117,7 @@ This syntax is used to describe all the possible ways that an external library c
117117

118118
### function
119119

120-
It might be useful to define your own function to control the behavior of what you want to externalize from webpack. [webpack-node-externals](https://www.npmjs.com/package/webpack-node-externals), for example, excludes all modules from the `node_modules` directory and provides some options to, for example, whitelist packages.
120+
It might be useful to define your own function to control the behavior of what you want to externalize from webpack. [webpack-node-externals](https://www.npmjs.com/package/webpack-node-externals), for example, excludes all modules from the `node_modules` directory and provides some options too, for example, whitelist packages.
121121

122122
It basically comes down to this:
123123

@@ -149,7 +149,7 @@ module.exports = {
149149
};
150150
```
151151

152-
In this case any dependency named `jQuery`, capitalized or not, or `$` would be externalized.
152+
In this case, any dependency named `jQuery`, capitalized or not, or `$` would be externalized.
153153

154154
### Combining syntaxes
155155

src/content/configuration/module.md

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ These options determine how the [different types of modules](/concepts/modules)
1919

2020
## `module.noParse`
2121

22-
`RegExp | [RegExp] | function | string | [string]`
22+
`RegExp` `[RegExp]` `function(resource)` `string` `[string]`
2323

2424
Prevent webpack from parsing any files matching the given regular expression(s). Ignored files __should not__ have calls to `import`, `require`, `define` or any other importing mechanism. This can boost build performance when ignoring large libraries.
2525

@@ -308,25 +308,16 @@ module.exports = {
308308

309309
## `Rule.use`
310310

311-
`[UseEntry] | function`
311+
`[UseEntry]` `function(info)`
312+
313+
__`[UseEntry]`__
312314

313315
`Rule.use` can be an array of [UseEntry](#useentry) which are applied to modules. Each entry specifies a loader to be used.
314316

315317
Passing a string (i.e. `use: [ 'style-loader' ]`) is a shortcut to the loader property (i.e. `use: [ { loader: 'style-loader '} ]`).
316318

317319
Loaders can be chained by passing multiple loaders, which will be applied from right to left (last to first configured).
318320

319-
`Rule.use` can be a function which receives the object argument describing the module being loaded, and must return an array of `UseEntry`.
320-
321-
The object parameter has the following fields:
322-
323-
- `compiler`: The current webpack compiler (can be undefined)
324-
- `issuer`: The path to the module that is importing the module being loaded
325-
- `realResource`: Always the path to the module being loaded
326-
- `resource`: The path to the module being loaded, it is usually equal to `realResource` except when the resource name is overwritten via `!=!` in request string
327-
328-
The same shortcut as an array can be used for the return value (i.e. `use: [ 'style-loader' ]`).
329-
330321
__webpack.config.js__
331322

332323
```javascript
@@ -357,6 +348,46 @@ module.exports = {
357348
};
358349
```
359350

351+
__`function(info)`__
352+
353+
`Rule.use` can also be a function which receives the object argument describing the module being loaded, and must return an array of `UseEntry` items.
354+
355+
The `info` object parameter has the following fields:
356+
357+
- `compiler`: The current webpack compiler (can be undefined)
358+
- `issuer`: The path to the module that is importing the module being loaded
359+
- `realResource`: Always the path to the module being loaded
360+
- `resource`: The path to the module being loaded, it is usually equal to `realResource` except when the resource name is overwritten via `!=!` in request string
361+
362+
The same shortcut as an array can be used for the return value (i.e. `use: [ 'style-loader' ]`).
363+
364+
__webpack.config.js__
365+
366+
```javascript
367+
module.exports = {
368+
//...
369+
module: {
370+
rules: [
371+
{
372+
use: (info) => ([
373+
{
374+
loader: 'custom-svg-loader'
375+
},
376+
{
377+
loader: 'svgo-loader',
378+
options: {
379+
plugins: [{
380+
cleanupIDs: { prefix: basename(info.resource) }
381+
}]
382+
}
383+
}
384+
])
385+
}
386+
]
387+
}
388+
};
389+
```
390+
360391
See [UseEntry](#useentry) for details.
361392

362393

@@ -404,14 +435,20 @@ module.exports = {
404435

405436
## `UseEntry`
406437

407-
`object`
438+
`object` `function(info)`
439+
440+
__`object`__
408441

409442
It must have a `loader` property being a string. It is resolved relative to the configuration [`context`](/configuration/entry-context#context) with the loader resolving options ([resolveLoader](/configuration/resolve#resolveloader)).
410443

411444
It can have an `options` property being a string or object. This value is passed to the loader, which should interpret it as loader options.
412445

413446
For compatibility a `query` property is also possible, which is an alias for the `options` property. Use the `options` property instead.
414447

448+
Note that webpack needs to generate a unique module identifier from the resource and all loaders including options. It tries to do this with a `JSON.stringify` of the options object. This is fine in 99.9% of cases, but may be not unique if you apply the same loaders with different options to the resource and the options have some stringified values.
449+
450+
It also breaks if the options object cannot be stringified (i.e. circular JSON). Because of this you can have a `ident` property in the options object which is used as unique identifier.
451+
415452
__webpack.config.js__
416453

417454
```javascript
@@ -430,9 +467,42 @@ module.exports = {
430467
};
431468
```
432469

433-
Note that webpack needs to generate a unique module identifier from the resource and all loaders including options. It tries to do this with a `JSON.stringify` of the options object. This is fine in 99.9% of cases, but may be not unique if you apply the same loaders with different options to the resource and the options have some stringified values.
470+
__`function(info)`__
434471

435-
It also breaks if the options object cannot be stringified (i.e. circular JSON). Because of this you can have a `ident` property in the options object which is used as unique identifier.
472+
A `UseEntry` can also be a function which receives the object argument describing the module being loaded, and must return an options object. This can be used to vary the loader options on a per-module basis.
473+
474+
The `info` object parameter has the following fields:
475+
476+
- `compiler`: The current webpack compiler (can be undefined)
477+
- `issuer`: The path to the module that is importing the module being loaded
478+
- `realResource`: Always the path to the module being loaded
479+
- `resource`: The path to the module being loaded, it is usually equal to `realResource` except when the resource name is overwritten via `!=!` in request string
480+
481+
__webpack.config.js__
482+
483+
```javascript
484+
module.exports = {
485+
//...
486+
module: {
487+
rules: [
488+
{
489+
loader: 'file-loader',
490+
options: {
491+
outputPath: 'svgs'
492+
}
493+
},
494+
(info) => ({
495+
loader: 'svgo-loader',
496+
options: {
497+
plugins: [{
498+
cleanupIDs: { prefix: basename(info.resource) }
499+
}]
500+
}
501+
})
502+
]
503+
}
504+
};
505+
```
436506

437507

438508
## Module Contexts

src/content/configuration/plugins.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ contributors:
66
- skipjack
77
- yatharthk
88
- byzyk
9+
- EugeneHlushko
910
---
1011

1112
The `plugins` option is used to customize the webpack build process in a variety of ways. webpack comes with a variety built-in plugins available under `webpack.[plugin-name]`. See [Plugins page](/plugins) for a list of plugins and documentation but note that there are a lot more out in the community.
1213

13-
T> Note: This page only discusses using plugins, however if you are interested in writing your own please visit [Writing a Plugin](/development/how-to-write-a-plugin/).
14+
T> Note: This page only discusses using plugins, however if you are interested in writing your own please visit [Writing a Plugin](/contribute/writing-a-plugin/).
1415

1516

1617
## `plugins`
1718

18-
`array`
19+
[`[Plugin]`](/plugins/)
1920

20-
A list of webpack plugins. For example, [`DefinePlugin`](/plugins/define-plugin/) allows you to create global constants which can be configured at compile time. This can be useful for allowing different behavior between development builds and release builds.
21+
An array of webpack plugins. For example, [`DefinePlugin`](/plugins/define-plugin/) allows you to create global constants which can be configured at compile time. This can be useful for allowing different behavior between development builds and release builds.
22+
23+
__webpack.config.js__
2124

2225
```js
2326
module.exports = {
@@ -32,6 +35,8 @@ module.exports = {
3235

3336
A more complex example, using multiple plugins, might look something like this:
3437

38+
__webpack.config.js__
39+
3540
```js
3641
var webpack = require('webpack');
3742
// importing plugins that do not come by default in webpack

0 commit comments

Comments
 (0)