Skip to content

Commit 87d1880

Browse files
authored
docs: add validation hook details for webpack 5.106 (#8158)
1 parent acf9a8e commit 87d1880

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/content/api/compiler-hooks.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ Triggered after resolver setup is complete.
8989

9090
- Callback Parameters: `compiler`
9191

92+
### validate
93+
94+
`SyncHook`
95+
96+
<Badge text="5.106.0+" />
97+
98+
Called to register validation work for webpack configuration, plugins, and loaders. Plugin authors can tap this hook and use `compiler.validate(...)` to participate in webpack's built-in validation flow.
99+
100+
W> The hook is always available. `compiler.validate(...)` skips validation when `validate: false` is set. With `experiments.futureDefaults`, webpack keeps validation enabled by default in development and disables it by default in production.
101+
102+
```js
103+
compiler.hooks.validate.tap("MyPlugin", () => {
104+
compiler.validate(
105+
() => require("./schema/MyPlugin.json"),
106+
options,
107+
{ name: "My Plugin", baseDataPath: "options" },
108+
(validatedOptions) => require("./schema/MyPlugin.check")(validatedOptions),
109+
);
110+
});
111+
```
112+
92113
### initialize
93114

94115
`SyncHook`

src/content/contribute/writing-a-plugin.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default {
8989
};
9090
```
9191

92-
Use [`schema-utils`](https://github.com/webpack/schema-utils) in order to validate the options being passed through the plugin options. Here is an example:
92+
A familiar way to validate plugin options in webpack versions **before `5.106`** is to use [`schema-utils`](https://github.com/webpack/schema-utils) in the constructor:
9393

9494
```js
9595
import { validate } from "schema-utils";
@@ -116,6 +116,27 @@ export default class HelloWorldPlugin {
116116
}
117117
```
118118

119+
Starting in webpack `5.106`, you can move that validation into `apply()` by tapping `compiler.hooks.validate` and calling `compiler.validate(...)` instead:
120+
121+
```js
122+
export default class HelloWorldPlugin {
123+
constructor(options = {}) {
124+
this.options = options;
125+
}
126+
127+
apply(compiler) {
128+
compiler.hooks.validate.tap("HelloWorldPlugin", () => {
129+
compiler.validate(
130+
() => require("./schema/hello-world-plugin.json"),
131+
this.options,
132+
);
133+
});
134+
}
135+
}
136+
```
137+
138+
W> This validation flow is skipped when webpack is configured with `validate: false`. With `experiments.futureDefaults`, validation is enabled by default in development mode and disabled by default in production mode.
139+
119140
## Compiler and Compilation
120141

121142
Among the two most important resources while developing plugins are the [`compiler`](/api/node/#compiler-instance) and [`compilation`](/api/compilation-hooks/) objects. Understanding their roles is an important first step in extending the webpack engine.

0 commit comments

Comments
 (0)