Skip to content

Commit 5a4fa0c

Browse files
Spencer Elliottelliottsj
Spencer Elliott
authored andcommitted
docs: add ProgressPlugin + progress reporting
1 parent cd7e330 commit 5a4fa0c

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

src/content/api/plugins.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,38 @@ compiler.hooks.myCustomHook.call(a, b, c);
9999
Again, see the [documentation](https://github.com/webpack/tapable) for `tapable` to learn more about the
100100
different hook classes and how they work.
101101

102+
## Reporting Progress
103+
104+
Plugins can report progress via [`ProgressPlugin`](/plugins/progress-plugin/), which prints progress messages to stderr by default. In order to enable progress reporting, pass a `--progress` argument when running the [webpack CLI](/api/cli/).
105+
106+
It is possible to customize the printed output by passing different arguments to the `reportProgress` function of [`ProgressPlugin`](/plugins/progress-plugin/).
107+
108+
To report progress, a plugin must `tap` into a hook using the `context: true` option:
109+
110+
```js
111+
compiler.hooks.emit.tapAsync({
112+
name: 'MyPlugin',
113+
context: true
114+
}, (context, compiler, callback) => {
115+
const reportProgress = context && context.reportProgress;
116+
if (reportProgress) reportProgress(0.95, 'Starting work');
117+
setTimeout(() => {
118+
if (reportProgress) reportProgress(0.95, 'Done work');
119+
callback();
120+
}, 1000);
121+
});
122+
```
123+
124+
The `reportProgress` function may be called with these arguments:
125+
126+
```js
127+
reportProgress(percentage, ...args);
128+
```
129+
130+
* `percentage`: This argument is unused; instead, [`ProgressPlugin`](/plugins/progress-plugin/) will calculate a percentage based on the current hook.
131+
* `...args`: Any number of strings, which will be passed to the `ProgressPlugin` handler to be reported to the user.
132+
133+
Note that only a subset of compiler and compilation hooks support the `reportProgress` function. See [`ProgressPlugin`](/plugins/progress-plugin/#supported-hooks) for a full list.
102134

103135
## Next Steps
104136

src/content/plugins/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Name | Description
3434
[`NoEmitOnErrorsPlugin`](/configuration/optimization/#optimization-noemitonerrors) | Skip the emitting phase when there are compilation errors
3535
[`NormalModuleReplacementPlugin`](/plugins/normal-module-replacement-plugin) | Replace resource(s) that matches a regexp
3636
[`NpmInstallWebpackPlugin`](/plugins/npm-install-webpack-plugin) | Auto-install missing dependencies during development
37+
[`ProgressPlugin`](/plugins/progress-plugin) | Report compilation progress
3738
[`ProvidePlugin`](/plugins/provide-plugin) | Use modules without having to use import/require
3839
[`SourceMapDevToolPlugin`](/plugins/source-map-dev-tool-plugin) | Enables a more fine grained control of source maps
3940
[`EvalSourceMapDevToolPlugin`](/plugins/eval-source-map-dev-tool-plugin) | Enables a more fine grained control of eval source maps
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: ProgressPlugin
3+
contributors:
4+
- elliottsj
5+
---
6+
7+
The `ProgressPlugin` provides a way to customize how progress is reported during a compilation.
8+
9+
## Usage
10+
11+
Create an instance of `ProgressPlugin` with a handler function which will be called when hooks report progress:
12+
13+
```js
14+
const handler = (percentage, message, ...args) => {
15+
// e.g. Output each progress message directly to the console:
16+
console.info(percentage, message, ...args);
17+
};
18+
19+
new webpack.ProgressPlugin(handler);
20+
```
21+
22+
* `handler` is a function which takes these arguments:
23+
* `percentage`: a number between 0 and 1 indicating the completion percentage of the compilation.
24+
* `message`: a short description of the currently-executing hook.
25+
* `...args`: zero or more additional strings describing the current progress.
26+
27+
## Supported Hooks
28+
29+
The following hooks report progress information to `ProgressPlugin`.
30+
31+
T> _Hooks marked with * allow plugins to report progress information using `reportProgress`. For more, see [Plugin API: Reporting Progress](/api/plugins/#reporting-progress)_
32+
33+
**Compiler**
34+
35+
* compilation
36+
* emit*
37+
* afterEmit*
38+
* done
39+
40+
**Compilation**
41+
42+
* buildModule
43+
* failedModule
44+
* succeedModule
45+
* finishModules*
46+
* seal*
47+
* optimizeDependenciesBasic*
48+
* optimizeDependencies*
49+
* optimizeDependenciesAdvanced*
50+
* afterOptimizeDependencies*
51+
* optimize*
52+
* optimizeModulesBasic*
53+
* optimizeModules*
54+
* optimizeModulesAdvanced*
55+
* afterOptimizeModules*
56+
* optimizeChunksBasic*
57+
* optimizeChunks*
58+
* optimizeChunksAdvanced*
59+
* afterOptimizeChunks*
60+
* optimizeTree*
61+
* afterOptimizeTree*
62+
* optimizeChunkModulesBasic*
63+
* optimizeChunkModules*
64+
* optimizeChunkModulesAdvanced*
65+
* afterOptimizeChunkModules*
66+
* reviveModules*
67+
* optimizeModuleOrder*
68+
* advancedOptimizeModuleOrder*
69+
* beforeModuleIds*
70+
* moduleIds*
71+
* optimizeModuleIds*
72+
* afterOptimizeModuleIds*
73+
* reviveChunks*
74+
* optimizeChunkOrder*
75+
* beforeChunkIds*
76+
* optimizeChunkIds*
77+
* afterOptimizeChunkIds*
78+
* recordModules*
79+
* recordChunks*
80+
* beforeHash*
81+
* afterHash*
82+
* recordHash*
83+
* beforeModuleAssets*
84+
* beforeChunkAssets*
85+
* additionalChunkAssets*
86+
* record*
87+
* additionalAssets*
88+
* optimizeChunkAssets*
89+
* afterOptimizeChunkAssets*
90+
* optimizeAssets*
91+
* afterOptimizeAssets*
92+
* afterSeal*
93+
94+
## References
95+
96+
### Source
97+
98+
* [`ProgressPlugin` source](https://github.com/webpack/webpack/blob/master/lib/ProgressPlugin.js)

0 commit comments

Comments
 (0)