Skip to content

Commit b5bb3a1

Browse files
feat: allow to use falsy loaders and plugins
1 parent 5636399 commit b5bb3a1

16 files changed

Lines changed: 349 additions & 79 deletions

File tree

declarations/WebpackOptions.d.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ export type RuleSetConditionsAbsolute = RuleSetConditionAbsolute[];
289289
* A loader request.
290290
*/
291291
export type RuleSetLoader = string;
292+
/**
293+
* Falsy value.
294+
*/
295+
export type Falsy = false | 0 | "" | null;
292296
/**
293297
* Options passed to a loader.
294298
*/
@@ -325,14 +329,14 @@ export type ResolveAlias =
325329
* A list of descriptions of loaders applied.
326330
*/
327331
export type RuleSetUse =
328-
| RuleSetUseItem[]
332+
| (Falsy | RuleSetUseItem)[]
329333
| ((data: {
330334
resource: string;
331335
realResource: string;
332336
resourceQuery: string;
333337
issuer: string;
334338
compiler: string;
335-
}) => RuleSetUseItem[])
339+
}) => (Falsy | RuleSetUseItem)[])
336340
| RuleSetUseItem;
337341
/**
338342
* A description of an applied loader.
@@ -352,12 +356,12 @@ export type RuleSetUseItem =
352356
*/
353357
options?: RuleSetLoaderOptions;
354358
}
355-
| ((data: object) => RuleSetUseItem | RuleSetUseItem[])
359+
| ((data: object) => RuleSetUseItem | (Falsy | RuleSetUseItem)[])
356360
| RuleSetLoader;
357361
/**
358362
* A list of rules.
359363
*/
360-
export type RuleSetRules = ("..." | RuleSetRule)[];
364+
export type RuleSetRules = ("..." | (false | 0 | "" | null) | RuleSetRule)[];
361365
/**
362366
* Specify options for each generator.
363367
*/
@@ -596,7 +600,7 @@ export type Performance = false | PerformanceOptions;
596600
/**
597601
* Add additional plugins to the compiler.
598602
*/
599-
export type Plugins = (WebpackPluginInstance | WebpackPluginFunction)[];
603+
export type Plugins = (Falsy | WebpackPluginInstance | WebpackPluginFunction)[];
600604
/**
601605
* Capture timing information for each module.
602606
*/
@@ -1392,7 +1396,7 @@ export interface RuleSetRule {
13921396
/**
13931397
* Only execute the first matching rule in this array.
13941398
*/
1395-
oneOf?: RuleSetRule[];
1399+
oneOf?: (Falsy | RuleSetRule)[];
13961400
/**
13971401
* Shortcut for use.options.
13981402
*/
@@ -1695,7 +1699,7 @@ export interface Optimization {
16951699
/**
16961700
* Minimizer(s) to use for minimizing the output.
16971701
*/
1698-
minimizer?: ("..." | WebpackPluginInstance | WebpackPluginFunction)[];
1702+
minimizer?: ("..." | Falsy | WebpackPluginInstance | WebpackPluginFunction)[];
16991703
/**
17001704
* Define the algorithm to choose module ids (natural: numeric ids in order of usage, named: readable ids for better debugging, hashed: (deprecated) short hashes as ids for better long term caching, deterministic: numeric hash ids for better long term caching, size: numeric ids focused on minimal initial download size, false: no algorithm used, as custom one can be provided via plugin).
17011705
*/

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 (plugin) {
1077+
plugin.apply(childCompiler);
1078+
}
10771079
}
10781080
}
10791081
for (const name in this.hooks) {

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ class WebpackOptionsApply extends OptionsApply {
566566
for (const minimizer of options.optimization.minimizer) {
567567
if (typeof minimizer === "function") {
568568
minimizer.call(compiler, compiler);
569-
} else if (minimizer !== "...") {
569+
} else if (minimizer !== "..." && minimizer) {
570570
minimizer.apply(compiler);
571571
}
572572
}

lib/rules/RuleSetCompiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ class RuleSetCompiler {
150150
* @returns {CompiledRule[]} rules
151151
*/
152152
compileRules(path, rules, refs) {
153-
return rules.map((rule, i) =>
154-
this.compileRule(`${path}[${i}]`, rule, refs)
155-
);
153+
return rules
154+
.filter(Boolean)
155+
.map((rule, i) => this.compileRule(`${path}[${i}]`, rule, refs));
156156
}
157157

158158
/**

lib/rules/UseEffectRulePlugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ class UseEffectRulePlugin {
106106
*/
107107
const useToEffectsWithoutIdent = (path, items) => {
108108
if (Array.isArray(items)) {
109-
return items.map((item, idx) =>
110-
useToEffectRaw(`${path}[${idx}]`, "[[missing ident]]", item)
111-
);
109+
return items
110+
.filter(Boolean)
111+
.map((item, idx) =>
112+
useToEffectRaw(`${path}[${idx}]`, "[[missing ident]]", item)
113+
);
112114
}
113115
return [useToEffectRaw(path, "[[missing ident]]", items)];
114116
};
@@ -120,7 +122,7 @@ class UseEffectRulePlugin {
120122
*/
121123
const useToEffects = (path, items) => {
122124
if (Array.isArray(items)) {
123-
return items.map((item, idx) => {
125+
return items.filter(Boolean).map((item, idx) => {
124126
const subPath = `${path}[${idx}]`;
125127
return useToEffect(subPath, subPath, item);
126128
});

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 (plugin) {
7373
plugin.apply(compiler);
7474
}
7575
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"core-js": "^3.6.5",
5050
"coveralls": "^3.1.0",
5151
"cspell": "^6.31.1",
52-
"css-loader": "^5.0.1",
52+
"css-loader": "^6.8.1",
5353
"date-fns": "^2.15.0",
5454
"es5-ext": "^0.10.53",
5555
"es6-promise-polyfill": "^1.2.0",
@@ -91,10 +91,12 @@
9191
"react": "^18.2.0",
9292
"react-dom": "^18.2.0",
9393
"rimraf": "^3.0.2",
94+
"sass": "^1.62.1",
95+
"sass-loader": "^13.3.1",
9496
"script-loader": "^0.7.2",
9597
"simple-git": "^3.17.0",
9698
"strip-ansi": "^6.0.0",
97-
"style-loader": "^2.0.0",
99+
"style-loader": "^3.3.3",
98100
"terser": "^5.17.0",
99101
"toml": "^3.0.0",
100102
"tooling": "webpack/tooling#v1.22.1",

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: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,10 @@
11451145
"node-commonjs"
11461146
]
11471147
},
1148+
"Falsy": {
1149+
"description": "Falsy value.",
1150+
"enum": [false, 0, "", null]
1151+
},
11481152
"FileCacheOptions": {
11491153
"description": "Options object for persistent file-based caching.",
11501154
"type": "object",
@@ -2472,6 +2476,9 @@
24722476
{
24732477
"enum": ["..."]
24742478
},
2479+
{
2480+
"$ref": "#/definitions/Falsy"
2481+
},
24752482
{
24762483
"$ref": "#/definitions/WebpackPluginInstance"
24772484
},
@@ -3561,6 +3568,9 @@
35613568
"items": {
35623569
"description": "Plugin of type object or instanceof Function.",
35633570
"anyOf": [
3571+
{
3572+
"$ref": "#/definitions/Falsy"
3573+
},
35643574
{
35653575
"$ref": "#/definitions/WebpackPluginInstance"
35663576
},
@@ -4272,6 +4282,9 @@
42724282
"items": {
42734283
"description": "A rule.",
42744284
"oneOf": [
4285+
{
4286+
"$ref": "#/definitions/Falsy"
4287+
},
42754288
{
42764289
"$ref": "#/definitions/RuleSetRule"
42774290
}
@@ -4393,6 +4406,12 @@
43934406
},
43944407
"enum": ["..."]
43954408
},
4409+
{
4410+
"$ref": "#/definitions/Falsy",
4411+
"cli": {
4412+
"exclude": true
4413+
}
4414+
},
43964415
{
43974416
"$ref": "#/definitions/RuleSetRule"
43984417
}
@@ -4407,6 +4426,9 @@
44074426
"items": {
44084427
"description": "An use item.",
44094428
"oneOf": [
4429+
{
4430+
"$ref": "#/definitions/Falsy"
4431+
},
44104432
{
44114433
"$ref": "#/definitions/RuleSetUseItem"
44124434
}
@@ -4415,7 +4437,7 @@
44154437
},
44164438
{
44174439
"instanceof": "Function",
4418-
"tsType": "((data: { resource: string, realResource: string, resourceQuery: string, issuer: string, compiler: string }) => RuleSetUseItem[])"
4440+
"tsType": "((data: { resource: string, realResource: string, resourceQuery: string, issuer: string, compiler: string }) => (Falsy | RuleSetUseItem)[])"
44194441
},
44204442
{
44214443
"$ref": "#/definitions/RuleSetUseItem"
@@ -4453,7 +4475,7 @@
44534475
},
44544476
{
44554477
"instanceof": "Function",
4456-
"tsType": "((data: object) => RuleSetUseItem|RuleSetUseItem[])"
4478+
"tsType": "((data: object) => RuleSetUseItem | (Falsy | RuleSetUseItem)[])"
44574479
},
44584480
{
44594481
"$ref": "#/definitions/RuleSetLoader"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "test";

0 commit comments

Comments
 (0)