Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept undefined and null in plugins #17329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion declarations/WebpackOptions.d.ts
Expand Up @@ -596,7 +596,7 @@ export type Performance = false | PerformanceOptions;
/**
* Add additional plugins to the compiler.
*/
export type Plugins = (WebpackPluginInstance | WebpackPluginFunction)[];
export type Plugins = (null | WebpackPluginInstance | WebpackPluginFunction)[];
/**
* Capture timing information for each module.
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/Compiler.js
Expand Up @@ -1073,7 +1073,9 @@ ${other}`);
childCompiler.root = this.root;
if (Array.isArray(plugins)) {
for (const plugin of plugins) {
plugin.apply(childCompiler);
if ("apply" in plugin) {
plugin.apply(childCompiler);
}
}
}
for (const name in this.hooks) {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/RuleSetCompiler.js
Expand Up @@ -54,7 +54,9 @@ class RuleSetCompiler {
});
if (plugins) {
for (const plugin of plugins) {
plugin.apply(this);
if ("apply" in plugin) {
plugin.apply(this);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/webpack.js
Expand Up @@ -69,7 +69,7 @@ const createCompiler = rawOptions => {
for (const plugin of options.plugins) {
if (typeof plugin === "function") {
plugin.call(compiler, compiler);
} else {
} else if ("apply" in plugin) {
plugin.apply(compiler);
}
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -3561,6 +3561,9 @@
"items": {
"description": "Plugin of type object or instanceof Function.",
"anyOf": [
{
"type": "null"
},
{
"$ref": "#/definitions/WebpackPluginInstance"
},
Expand Down
12 changes: 8 additions & 4 deletions test/Validation.test.js
Expand Up @@ -315,9 +315,10 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.plugins[0] should be one of these:
object { apply, … } | function
null | object { apply, … } | function
-> Plugin of type object or instanceof Function.
Details:
* configuration.plugins[0] should be a null.
* configuration.plugins[0] should be an object:
object { apply, … }
-> Plugin instance.
Expand All @@ -336,9 +337,10 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.plugins[0] should be one of these:
object { apply, … } | function
null | object { apply, … } | function
-> Plugin of type object or instanceof Function.
Details:
* configuration.plugins[0] should be a null.
* configuration.plugins[0] should be an object:
object { apply, … }
-> Plugin instance.
Expand All @@ -357,9 +359,10 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.plugins[0] should be one of these:
object { apply, … } | function
null | object { apply, … } | function
-> Plugin of type object or instanceof Function.
Details:
* configuration.plugins[0] should be a null.
* configuration.plugins[0] should be an object:
object { apply, … }
-> Plugin instance.
Expand All @@ -378,9 +381,10 @@ describe("Validation", () => {
expect(msg).toMatchInlineSnapshot(`
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
- configuration.plugins[0] should be one of these:
object { apply, … } | function
null | object { apply, … } | function
-> Plugin of type object or instanceof Function.
Details:
* configuration.plugins[0] should be a null.
* configuration.plugins[0] should be an object:
object { apply, … }
-> Plugin instance.
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/plugins/null-undefined/index.js
@@ -0,0 +1,3 @@
it("should accept null and undefined as plugins and define TRUE", function() {
expect(TRUE).toBe(true);
});
12 changes: 12 additions & 0 deletions test/configCases/plugins/null-undefined/webpack.config.js
@@ -0,0 +1,12 @@
var DefinePlugin = require("../../../../").DefinePlugin;

/** @type {import("../../../../").Configuration[]} */
module.exports = {
plugins: [
null,
undefined,
new DefinePlugin({
TRUE: true
})
]
};
2 changes: 2 additions & 0 deletions types.d.ts
Expand Up @@ -2378,6 +2378,7 @@ declare interface Configuration {
* Add additional plugins to the compiler.
*/
plugins?: (
| null
| ((this: Compiler, compiler: Compiler) => void)
| WebpackPluginInstance
)[];
Expand Down Expand Up @@ -13149,6 +13150,7 @@ declare interface WebpackOptionsNormalized {
* Add additional plugins to the compiler.
*/
plugins: (
| null
| ((this: Compiler, compiler: Compiler) => void)
| WebpackPluginInstance
)[];
Expand Down