Skip to content

Commit 71f7703

Browse files
authored
docs(plugins): clean up and improve plugin documentation (#1254)
Add lead-in descriptions and clean up content. Document the concept and range of the limit-chunk-count-plugin. Resolves #1161 (combined with @simon04's work) Resolves #815
1 parent 40b3f25 commit 71f7703

12 files changed

+161
-92
lines changed

content/plugins/commons-chunk-plugin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ contributors:
77
- kevinzwhuang
88
---
99

10+
The `CommonsChunkPlugin` is an opt-in feature that creates a separate file (known as a chunk), consisting of common modules shared between multiple entry points. By separating common modules from bundles, the resulting chunked file can be loaded once initially, and stored in cache for later use. This results in pagespeed optimizations as the browser can quickly serve the shared code from cache, rather than being forced to load a larger bundle whenever a new page is visited.
11+
1012
```javascript
1113
new webpack.optimize.CommonsChunkPlugin(options)
1214
```
1315

14-
The `CommonsChunkPlugin` is an opt-in feature that creates a separate file (known as a chunk), consisting of common modules shared between multiple entry points. By separating common modules from bundles, the resulting chunked file can be loaded once initially, and stored in cache for later use. This results in pagespeed optimizations as the browser can quickly serve the shared code from cache, rather than being forced to load a larger bundle whenever a new page is visited.
15-
1616

1717
## Options
1818

content/plugins/context-replacement-plugin.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
title: ContextReplacementPlugin
33
contributors:
44
- simon04
5+
related:
6+
- title: Issue #2783 - ContextReplacementPlugin Description
7+
- url: https://github.com/webpack/webpack/issues/2783#issuecomment-234137265
58
---
69

710
*Context* refers to a [require with an expression](/guides/dependency-management/#require-with-expression) such as `require('./locale/' + name + '.json')`. When encountering such an expression, webpack infers the directory (`'./locale/'`) and a regular expression (`/^.*\.json$/`). Since the `name` is not known at compile time, webpack includes every file as module in the bundle.
@@ -20,7 +23,7 @@ new webpack.ContextReplacementPlugin(
2023
)
2124
```
2225

23-
If the resource (directory) matches `resourceRegExp`, the plugin replaces the default resource, recursive flag or generated regular expression with `newContentResource`, `newContentRecursive` or `newContextRegExp` respectively. If `newContentResource` is relative, it is resolved relative to the previous resource.
26+
If the resource (directory) matches `resourceRegExp`, the plugin replaces the default resource, recursive flag or generated regular expression with `newContentResource`, `newContentRecursive` or `newContextRegExp` respectively. If `newContentResource` is relative, it is resolved relative to the previous resource.
2427

2528
**Example**
2629

@@ -74,8 +77,3 @@ new ContextReplacementPlugin(/selector/, './folder', {
7477
/* runtime-request: compile-time request */
7578
})
7679
```
77-
78-
79-
## References
80-
81-
* https://github.com/webpack/webpack/issues/2783#issuecomment-234137265

content/plugins/define-plugin.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ contributors:
55
- rouzbeh84
66
---
77

8+
The `DefinePlugin` 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. If you perform logging in your development build but not in the release build you might use a global constant to determine whether logging takes place. That's where `DefinePlugin` shines, set it and forget it rules for development and release builds.
9+
810
``` javascript
9-
new webpack.DefinePlugin(definitions)
11+
new webpack.DefinePlugin({
12+
// Definitions...
13+
})
1014
```
1115

12-
The `DefinePlugin` 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. If you perform logging in your development build but not in the release build you might use a global constant to determine whether logging takes place. That's where `DefinePlugin` shines, set it and forget it rules for development and release builds.
1316

14-
**Example:**
17+
## Usage
18+
19+
Each key passed into `DefinePlugin` is an identifier or multiple identifiers joined with `.`.
20+
21+
* If the value is a string it will be used as a code fragment.
22+
* If the value isn't a string, it will be stringified (including functions).
23+
* If the value is an object all keys are defined the same way.
24+
* If you prefix `typeof` to the key, it's only defined for typeof calls.
25+
26+
The values will be inlined into the code allowing a minification pass to remove the redundant conditional.
1527

1628
``` javascript
1729
new webpack.DefinePlugin({
@@ -30,21 +42,13 @@ if(!BROWSER_SUPPORTS_HTML5) require("html5shiv");
3042

3143
T> Note that because the plugin does a direct text replacement, the value given to it must include **actual quotes** inside of the string itself. Typically, this is done either with either alternate quotes, such as `'"production"'`, or by using `JSON.stringify('production')`.
3244

33-
Each key passed into `DefinePlugin` is an identifier or multiple identifiers joined with `.`.
34-
35-
* If the value is a string it will be used as a code fragment.
36-
* If the value isn't a string, it will be stringified (including functions).
37-
* If the value is an object all keys are defined the same way.
38-
* If you prefix `typeof` to the key, it's only defined for typeof calls.
39-
40-
The values will be inlined into the code allowing a minification pass to remove the redundant conditional.
41-
42-
**Example:**
45+
__index.js__
4346

4447
``` javascript
4548
if (!PRODUCTION) {
4649
console.log('Debug info')
4750
}
51+
4852
if (PRODUCTION) {
4953
console.log('Production log')
5054
}
@@ -67,7 +71,8 @@ and then after a minification pass results in:
6771
console.log('Production log')
6872
```
6973

70-
## Use Case: Feature Flags
74+
75+
## Feature Flags
7176

7277
Enable/disable features in production/development build using [feature flags](https://en.wikipedia.org/wiki/Feature_toggle).
7378

@@ -78,7 +83,8 @@ new webpack.DefinePlugin({
7883
})
7984
```
8085

81-
## Use Case: Service URLs
86+
87+
## Service URLs
8288

8389
Use a different service URL in production/development builds:
8490

content/plugins/dll-plugin.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ related:
1111
url: https://github.com/webpack/webpack/tree/master/examples/explicit-vendor-chunk/README.md
1212
---
1313

14-
## Introduction
15-
1614
The `DllPlugin` and `DllReferencePlugin` provide means to split bundles in a way that can drastically improve build time performance.
1715

18-
[`DllPlugin`](/plugins/dll-plugin#dllplugin) is used in a separate webpack config exclusively to create a dll-only-bundle. It creates a `manifest.json` file, which is used by the [`DllReferencePlugin`](/plugins/dll-plugin#dllreferenceplugin) to map dependencies.
19-
20-
`DllReferencePlugin` is used in the primary webpack config, it references the dll-only-bundle(s) to require pre-built dependencies.
21-
2216

2317
## `DllPlugin`
2418

19+
This plugin is used in a separate webpack config exclusively to create a dll-only-bundle. It creates a `manifest.json` file, which is used by the [`DllReferencePlugin`](/plugins/dll-plugin#dllreferenceplugin) to map dependencies.
20+
2521
* `context` (optional): context of requests in the manifest file (defaults to the webpack context.)
2622
* `name`: name of the exposed dll function ([TemplatePaths](https://github.com/webpack/webpack/blob/master/lib/TemplatedPathPlugin.js): `[hash]` & `[name]` )
2723
* `path`: **absolute path** to the manifest json file (output)
@@ -35,7 +31,9 @@ Creates a `manifest.json` which is written to the given `path`. It contains mapp
3531
Combine this plugin with [`output.library`](/configuration/output/#output-library) option to expose (aka, put into the global scope) the dll function.
3632

3733

38-
### `DllReferencePlugin`
34+
## `DllReferencePlugin`
35+
36+
This plugin is used in the primary webpack config, it references the dll-only-bundle(s) to require pre-built dependencies.
3937

4038
* `context`: (**absolute path**) context of requests in the manifest (or content property)
4139
* `manifest` (object): an object containing `content` and `name`

content/plugins/ignore-plugin.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ contributors:
44
- simon04
55
---
66

7-
```js
8-
new webpack.IgnorePlugin(requestRegExp, [contextRegExp])
9-
```
10-
11-
Don't generate modules for requests matching the provided RegExp.
7+
Prevent generation of modules for `import` or `require` calls matching the following regular expressions:
128

139
* `requestRegExp` A RegExp to test the request against.
1410
* `contextRegExp` (optional) A RegExp to test the context (directory) against.
1511

16-
## Typical use-cases
12+
``` js
13+
new webpack.IgnorePlugin(requestRegExp, [contextRegExp])
14+
```
15+
16+
The following examples demonstrate a few ways this plugin can be used.
17+
1718

18-
### Ignore moment locales
19+
## Ignore Moment Locales
1920

2021
As of [moment](https://momentjs.com/) 2.18, all locales are bundled together with the core library (see [this GitHub issue](https://github.com/moment/moment/issues/2373)). You can use the `IgnorePlugin` to stop any locale being bundled with moment:
2122

content/plugins/limit-chunk-count-plugin.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,45 @@
22
title: LimitChunkCountPlugin
33
contributors:
44
- rouzbeh84
5+
- skipjack
56
---
67

7-
While writing your code, you may have already added many code split points to load stuff on demand. After compiling you might notice that there are too many chunks that are too small - creating larger HTTP overhead. Luckily, webpack can post-process your chunks by merging them. You can provide two options as _objects_:
8+
While writing your code, you may have already added many code split points to load stuff on demand. After compiling you might notice that some chunks are too small - creating larger HTTP overhead. Luckily, this plugin can post-process your chunks by merging them.
89

9-
- Limit the maximum chunk count inline with `--optimize-max-chunks 15` or in your config as `new webpack.optimize.LimitChunkCountPlugin({maxChunks: 15})`
10-
- Limit the minimum chunk size inline with `--optimize-min-chunk-size 10000` or in your config as `new webpack.optimize.MinChunkSizePlugin({minChunkSize: 10000})`
10+
``` js
11+
new webpack.optimize.LimitChunkCountPlugin({
12+
// Options...
13+
})
14+
```
1115

12-
webpack will take care of it by merging chunks (it will prefer merging chunk that have duplicate modules). Nothing will be merged into the entry chunk, so as not to impact initial page loading time.
16+
17+
## Options
18+
19+
The following options are supported:
20+
21+
- `maxChunks`: Limit the maximum number of chunks using a value greater greater than or equal to `1`. Using `1` will prevent any additional chunks from being added as the entry/main chunk is also included in the count.
22+
- `minChunkSize`: Set a minimum chunk size.
23+
24+
While merging chunks, webpack will try to identify those that have duplicate modules and merge them first. Nothing will be merged into the entry chunk, so as not to impact initial page loading time. Here's a small example:
25+
26+
``` js
27+
new webpack.optimize.LimitChunkCountPlugin({
28+
maxChunks: 5, // Must be greater than or equal to one
29+
minChunkSize: 1000
30+
})
31+
```
32+
33+
34+
## CLI
35+
36+
This plugin and it's options can also be invoked via the CLI:
37+
38+
``` bash
39+
--optimize-max-chunks 15
40+
```
41+
42+
or
43+
44+
``` bash
45+
--optimize-min-chunk-size 10000
46+
```

content/plugins/loader-options-plugin.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
---
22
title: LoaderOptionsPlugin
33
contributors:
4-
- johnnyreilly
4+
- johnnyreilly
5+
- skipjack
56
---
67

7-
?> Review this content
8+
The `LoaderOptionsPlugin` is unlike other plugins in that it is built for migration from webpack version 1 to version 2. In webpack v2, the schema for a `webpack.config.js` became stricter; no longer open for extension by other loaders and plugins. The intention is that you pass `options` directly to loaders and plugins (i.e. `options` are __not__ global or shared).
89

9-
The `LoaderOptionsPlugin` is unlike other plugins. It exists to help people move from webpack 1 to webpack 2. With webpack 2 the schema for a `webpack.config.js` became stricter; no longer open for extension by other loaders / plugins. With webpack 2 the intention is that you pass `options` directly to loaders / plugins. i.e. options are **not** global / shared.
10+
However, until a loader has been updated to depend upon options being passed directly to them, the `LoaderOptionsPlugin` exists to bridge the gap. You can configure global loader options with this plugin and all loaders will receive these options.
1011

11-
However, until a loader has been updated to depend upon options being passed directly to them, the `LoaderOptionsPlugin` exists to bridge the gap. You can configure global / shared loader options with this plugin and all loaders will receive these options.
12+
``` js
13+
new webpack.LoaderOptionsPlugin({
14+
// Options...
15+
})
16+
```
1217

13-
In the future this plugin may be removed.
18+
W> This plugin will be removed in the future as it only exists for migration.
1419

15-
```javascript
16-
new webpack.LoaderOptionsPlugin(options)
17-
```
20+
21+
## Options
22+
23+
This plugin supports the following options:
1824

1925
* `options.debug` (`boolean`): Whether loaders should be in `debug` mode or not. `debug` will be removed as of webpack 3.
2026
* `options.minimize` (`boolean`): Where loaders can be switched to minimize mode.
21-
* `options.options` (`object`): A configuration object that can be used to configure older loaders - this will take the same schema a `webpack.config.js`
27+
* `options.options` (`object`): A configuration object that can be used to configure older loaders - this will take the same schema a `webpack.config.js`.
28+
* `options.options.context` (`string`): The context that can be used to configure older loaders.
29+
* any other options allowed in a `webpack.config.js`....
30+
2231

23-
* `options.options.context` (`string`): The context that can be used to configure older loaders
24-
* other options as in a `webpack.config.js`....
32+
## Usage
2533

26-
## Examples
34+
Here's an example of how this plugin might be used:
2735

2836
```javascript
2937
new webpack.LoaderOptionsPlugin({

content/plugins/min-chunk-size-plugin.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ contributors:
44
sort: 1
55
---
66

7-
Try to keep the chunk size above a limit.
7+
Keep chunk size above the specified limit by merging chunks that are smaller than the `minChunkSize`.
8+
9+
``` js
10+
new webpack.optimize.MinChunkSizePlugin({
11+
minChunkSize: 10000 // Minimum number of characters
12+
})
13+
```

content/plugins/no-emit-on-errors-plugin.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ contributors:
44
- simon04
55
---
66

7-
```javascript
7+
Use the `NoEmitOnErrorsPlugin` to skip the emitting phase whenever there are errors while compiling. This ensures that no assets are emitted that include errors. The `emitted` flag in the stats is `false` for all assets.
8+
9+
``` js
810
new webpack.NoEmitOnErrorsPlugin()
911
```
1012

11-
Use the `NoEmitOnErrorsPlugin` to skip the emitting phase whenever there are errors while compiling. This ensures that no assets are emitted that include errors. The `emitted` flag in the stats is `false` for all assets.
12-
13-
If you are using the [CLI](/api/cli/), the webpack process will not exit with an error code by enabling this plugin. If you want webpack to "fail" when using the CLI, please check out the [`bail` option](/api/cli/#advanced-options).
13+
T> This supersedes the (now deprecated) webpack 1 plugin `NoErrorsPlugin`.
1414

15-
Note: This supersedes the (now deprecated) webpack 1 plugin `NoErrorsPlugin`.
15+
W> If you are using the [CLI](/api/cli/), the webpack process will not exit with an error code by enabling this plugin. If you want webpack to "fail" when using the CLI, please check out the [`bail` option](/api/cli/#advanced-options).

content/plugins/normal-module-replacement-plugin.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ contributors:
44
- gonzoyumo
55
---
66

7-
## Install
8-
9-
The `NormalModuleReplacementPlugin` is a built-in webpack plugin.
10-
11-
12-
## Usage
13-
14-
``` javascript
15-
new webpack.NormalModuleReplacementPlugin(resourceRegExp, newResource)
16-
```
17-
187
The `NormalModuleReplacementPlugin` allows you to replace resources that match `resourceRegExp` with `newResource`. If `newResource` is relative, it is resolved relative to the previous resource. If `newResource` is a function, it is expected to overwrite the request attribute of the supplied resource.
198

209
This can be useful for allowing different behaviour between builds.
2110

11+
``` js
12+
new webpack.NormalModuleReplacementPlugin(
13+
resourceRegExp,
14+
newResource
15+
)
16+
```
2217

23-
## Basic example
18+
19+
## Basic Example
2420

2521
Replace a specific module when building for a [development environment](/guides/production-build).
2622

@@ -36,7 +32,7 @@ new webpack.NormalModuleReplacementPlugin(
3632
```
3733

3834

39-
## Advanced example
35+
## Advanced Example
4036

4137
Conditional build depending on an [specified environment](/configuration/configuration-types).
4238

@@ -52,7 +48,7 @@ module.exports = function(env) {
5248
})
5349
]
5450
}
55-
51+
5652
}
5753
```
5854

0 commit comments

Comments
 (0)