Skip to content

Commit 792de0e

Browse files
committed
Accept undefined and null in plugins
1 parent 5636399 commit 792de0e

10 files changed

Lines changed: 37 additions & 9 deletions

File tree

declarations/WebpackOptions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ export type Performance = false | PerformanceOptions;
596596
/**
597597
* Add additional plugins to the compiler.
598598
*/
599-
export type Plugins = (WebpackPluginInstance | WebpackPluginFunction)[];
599+
export type Plugins = (null | WebpackPluginInstance | WebpackPluginFunction)[];
600600
/**
601601
* Capture timing information for each module.
602602
*/

lib/Compiler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,9 @@ ${other}`);
10731073
childCompiler.root = this.root;
10741074
if (Array.isArray(plugins)) {
10751075
for (const plugin of plugins) {
1076-
plugin.apply(childCompiler);
1076+
if ("apply" in plugin) {
1077+
plugin.apply(childCompiler);
1078+
}
10771079
}
10781080
}
10791081
for (const name in this.hooks) {

lib/rules/RuleSetCompiler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class RuleSetCompiler {
5454
});
5555
if (plugins) {
5656
for (const plugin of plugins) {
57-
plugin.apply(this);
57+
if ("apply" in plugin) {
58+
plugin.apply(this);
59+
}
5860
}
5961
}
6062
}

lib/webpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const createCompiler = rawOptions => {
6969
for (const plugin of options.plugins) {
7070
if (typeof plugin === "function") {
7171
plugin.call(compiler, compiler);
72-
} else {
72+
} else if ("apply" in plugin) {
7373
plugin.apply(compiler);
7474
}
7575
}

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,6 +3561,9 @@
35613561
"items": {
35623562
"description": "Plugin of type object or instanceof Function.",
35633563
"anyOf": [
3564+
{
3565+
"type": "null"
3566+
},
35643567
{
35653568
"$ref": "#/definitions/WebpackPluginInstance"
35663569
},

test/Validation.test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ describe("Validation", () => {
315315
expect(msg).toMatchInlineSnapshot(`
316316
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
317317
- configuration.plugins[0] should be one of these:
318-
object { apply, … } | function
318+
null | object { apply, … } | function
319319
-> Plugin of type object or instanceof Function.
320320
Details:
321+
* configuration.plugins[0] should be a null.
321322
* configuration.plugins[0] should be an object:
322323
object { apply, … }
323324
-> Plugin instance.
@@ -336,9 +337,10 @@ describe("Validation", () => {
336337
expect(msg).toMatchInlineSnapshot(`
337338
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
338339
- configuration.plugins[0] should be one of these:
339-
object { apply, … } | function
340+
null | object { apply, … } | function
340341
-> Plugin of type object or instanceof Function.
341342
Details:
343+
* configuration.plugins[0] should be a null.
342344
* configuration.plugins[0] should be an object:
343345
object { apply, … }
344346
-> Plugin instance.
@@ -357,9 +359,10 @@ describe("Validation", () => {
357359
expect(msg).toMatchInlineSnapshot(`
358360
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
359361
- configuration.plugins[0] should be one of these:
360-
object { apply, … } | function
362+
null | object { apply, … } | function
361363
-> Plugin of type object or instanceof Function.
362364
Details:
365+
* configuration.plugins[0] should be a null.
363366
* configuration.plugins[0] should be an object:
364367
object { apply, … }
365368
-> Plugin instance.
@@ -378,9 +381,10 @@ describe("Validation", () => {
378381
expect(msg).toMatchInlineSnapshot(`
379382
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
380383
- configuration.plugins[0] should be one of these:
381-
object { apply, … } | function
384+
null | object { apply, … } | function
382385
-> Plugin of type object or instanceof Function.
383386
Details:
387+
* configuration.plugins[0] should be a null.
384388
* configuration.plugins[0] should be an object:
385389
object { apply, … }
386390
-> Plugin instance.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should accept null and undefined as plugins and define TRUE", function() {
2+
expect(TRUE).toBe(true);
3+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var DefinePlugin = require("../../../../").DefinePlugin;
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = {
5+
plugins: [
6+
null,
7+
undefined,
8+
new DefinePlugin({
9+
TRUE: true
10+
})
11+
]
12+
};

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,7 @@ declare interface Configuration {
23782378
* Add additional plugins to the compiler.
23792379
*/
23802380
plugins?: (
2381+
| null
23812382
| ((this: Compiler, compiler: Compiler) => void)
23822383
| WebpackPluginInstance
23832384
)[];
@@ -13149,6 +13150,7 @@ declare interface WebpackOptionsNormalized {
1314913150
* Add additional plugins to the compiler.
1315013151
*/
1315113152
plugins: (
13153+
| null
1315213154
| ((this: Compiler, compiler: Compiler) => void)
1315313155
| WebpackPluginInstance
1315413156
)[];

0 commit comments

Comments
 (0)