Skip to content

Commit 524ecdd

Browse files
feat(loaders): support sourceTest for loader input (#23029)
* feat(loaders): support sourceTest for loader input * feat(loaders): take a rule condition in sourceTest Compile sourceTest with RuleSetCompiler, so it accepts what a rule's test takes (string prefix, RegExp, function, array, and/or/not, glob), once per use item. Attach it to a loader item only when set, so other loaders' items keep their size. Treat a buffer as undecodable only when re-encoding loses bytes, so valid U+FFFD input is still filtered, and reset lastIndex of a global or sticky regexp condition before each match. * fix(loaders): type a use effect's value as EffectUse's Effect's value includes EffectBasic's any, so sourceTest was inferred as any. * fix(loaders): report a sourceTest that throws as a module error The source test can run inside a loader callback that already finished, whose catch only logs, so a throwing condition hung the build. * fix(loaders): wrap a non-error thrown by sourceTest in an Error A falsy thrown value reached the callback as success and failed later in processResult with an unrelated TypeError. * fix(loaders): describe a symbol thrown by sourceTest without throwing A template literal throws on a symbol, which brought back the hang the wrapper prevents; String converts it, with a fallback for any other throw. * test: cover every changed line of the loader sourceTest Cover a loader that returns no content, a cached loader resolve and a sourceTest that throws a value String() can't convert. The resource is always a Buffer, so matchesSourceTest no longer needs fromLoader. --------- Co-authored-by: alexander-akait <[email protected]>
1 parent 353202a commit 524ecdd

54 files changed

Lines changed: 887 additions & 14 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add a `sourceTest` condition to `use` items; match `/g` regexps reliably.

‎declarations/WebpackOptions.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,6 +5433,11 @@ export type RuleSetUseItem =
54335433
* Loader options.
54345434
*/
54355435
options?: RuleSetLoaderOptions;
5436+
/**
5437+
* Run the loader's normal function only when its current input matches this condition, the same kind of condition 'test' takes. Input that is not valid UTF-8 text always runs it.
5438+
* @since 5.113.0
5439+
*/
5440+
sourceTest?: RuleSetConditionOrConditions;
54365441
}
54375442
| RuleSetUseFunction
54385443
| RuleSetLoader;

‎lib/loaders/LoaderRunner.js‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const loadLoader = require("./loadLoader");
1919
// unless something asked for the measurement, and read once per loader.
2020
const LOADER_TIMING = Symbol("loader timing");
2121

22-
/** @typedef {string | { loader: string, options?: LoaderItem["options"], ident?: string | null, type?: string | null, fragment?: string }} LoaderItemInput */
22+
/** @typedef {string | { loader: string, options?: LoaderItem["options"], ident?: string | null, type?: string | null, fragment?: string, sourceTest?: (value: string) => boolean }} LoaderItemInput */
2323

2424
/**
2525
* What a loader hands on: content (`null` for a resource without a path),
@@ -105,6 +105,7 @@ const LOADER_TIMING = Symbol("loader timing");
105105
* @property {DataType | undefined} data
106106
* @property {boolean} pitchExecuted
107107
* @property {boolean} normalExecuted
108+
* @property {((value: string) => boolean)=} sourceTest
108109
* @property {("commonjs" | "module")=} type
109110
*/
110111

@@ -225,6 +226,34 @@ const UTF8_BOM_2 = 0xbf;
225226
// U+FEFF, the BOM once decoded
226227
const BOM_CHAR_CODE = 0xfeff;
227228

229+
/**
230+
* Checks whether a loader's current input matches its source test.
231+
* @param {(value: string) => boolean} sourceTest compiled source test condition
232+
* @param {string | Buffer | null | undefined} content current input
233+
* @returns {boolean} whether to run the loader
234+
*/
235+
function matchesSourceTest(sourceTest, content) {
236+
let text;
237+
if (typeof content === "string") {
238+
text = stringWithoutBOM(content);
239+
} else if (Buffer.isBuffer(content)) {
240+
text = utf8BufferToString(content);
241+
// Invalid UTF-8 decodes to U+FFFD and doesn't re-encode to the same bytes;
242+
// such input leaves the loader in charge.
243+
if (
244+
text.includes("\uFFFD") &&
245+
!Buffer.from(text, "utf8").equals(
246+
content.subarray(content.length - Buffer.byteLength(text, "utf8"))
247+
)
248+
) {
249+
return true;
250+
}
251+
} else {
252+
return true;
253+
}
254+
return sourceTest(text);
255+
}
256+
228257
/**
229258
* @param {Buffer} buf buffer
230259
* @returns {string} string, with a leading UTF-8 BOM skipped at the buffer level
@@ -296,6 +325,8 @@ class LoaderObject {
296325
this.ident = null;
297326
/** @type {(string | null)=} */
298327
this.type = undefined;
328+
/** @type {((value: string) => boolean) | undefined} */
329+
this.sourceTest = undefined;
299330
/** @type {LoaderDefinitionFunction<LoaderOptions> | RawLoaderDefinitionFunction<LoaderOptions> | null=} */
300331
this.normal = null;
301332
/** @type {PitchLoaderDefinitionFunction<LoaderOptions> | null=} */
@@ -342,12 +373,13 @@ class LoaderObject {
342373
);
343374
}
344375

345-
const { loader: path, fragment, type, options, ident } = value;
376+
const { loader: path, fragment, type, options, ident, sourceTest } = value;
346377
this.path = path;
347378
this.fragment = fragment || "";
348379
this.type = type;
349380
this.options = options;
350381
this.ident = ident;
382+
this.sourceTest = sourceTest;
351383

352384
if (options === null || options === undefined) {
353385
this.query = "";
@@ -514,6 +546,30 @@ function iterateNormalLoaders(
514546
continue;
515547
}
516548

549+
if (currentLoaderObject.sourceTest !== undefined) {
550+
let matched;
551+
try {
552+
matched = matchesSourceTest(currentLoaderObject.sourceTest, args[0]);
553+
} catch (err) {
554+
// This can run inside a finished loader's callback, which only logs a throw,
555+
// and a falsy error would read as success.
556+
if (err instanceof Error) return callback(err);
557+
let thrown;
558+
try {
559+
// `String` converts a symbol, which a template literal would throw on.
560+
thrown = String(err);
561+
} catch (_err) {
562+
thrown = typeof err;
563+
}
564+
return callback(new Error(`sourceTest threw ${thrown}`));
565+
}
566+
if (!matched) {
567+
currentLoaderObject.normalExecuted = true;
568+
loaderContext.loaderIndex--;
569+
continue;
570+
}
571+
}
572+
517573
const fn = currentLoaderObject.normal;
518574
currentLoaderObject.normalExecuted = true;
519575

‎lib/module/NormalModule.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ const walkSideEffects = (rootMod, moduleGraph) => {
549549
* @property {string | null | undefined | Record<string, EXPECTED_ANY>} options
550550
* @property {string | null=} ident
551551
* @property {string | null=} type
552+
* @property {((value: string) => boolean)=} sourceTest compiled condition the loader's current input has to match for its normal function to run
552553
*/
553554

554555
/**

‎lib/module/NormalModuleFactory.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,8 @@ If changing the source code is not an option there is also a resolve options cal
17821782
const cached = this._loaderResolveCache.get(cacheKey);
17831783
if (cached !== undefined) {
17841784
replayResolveDependencies(resolveContext, cached);
1785-
return callback(null, {
1785+
/** @type {LoaderItem} */
1786+
const resolved = {
17861787
loader: cached.loader,
17871788
type: cached.type,
17881789
options:
@@ -1792,7 +1793,12 @@ If changing the source code is not an option there is also a resolve options cal
17921793
: undefined
17931794
: item.options,
17941795
ident: item.options === undefined ? undefined : item.ident
1795-
});
1796+
};
1797+
// Set only when used, so other loaders' items keep their size.
1798+
if (item.sourceTest !== undefined) {
1799+
resolved.sourceTest = item.sourceTest;
1800+
}
1801+
return callback(null, resolved);
17961802
}
17971803
// Capture this resolve's dependencies in dedicated sets so a future hit
17981804
// can replay them; also feed them back into the caller's resolveContext.
@@ -1874,6 +1880,9 @@ If changing the source code is not an option there is also a resolve options cal
18741880
: item.options,
18751881
ident: item.options === undefined ? undefined : item.ident
18761882
};
1883+
if (item.sourceTest !== undefined) {
1884+
resolved.sourceTest = item.sourceTest;
1885+
}
18771886

18781887
return callback(null, resolved);
18791888
}

‎lib/rules/RuleSetCompiler.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const {
8888
* Defines the effect use type used by this module.
8989
* @typedef {object} EffectUse
9090
* @property {EffectUseType} type
91-
* @property {{ loader: string, options?: RuleSetLoaderOptions | null, ident?: string }} value
91+
* @property {{ loader: string, options?: RuleSetLoaderOptions | null, ident?: string, sourceTest?: (value: string) => boolean }} value
9292
*/
9393

9494
/**
@@ -368,6 +368,16 @@ class RuleSetCompiler {
368368
}
369369
}
370370
if (condition instanceof RegExp) {
371+
if (condition.global || condition.sticky) {
372+
// A global or sticky regexp resumes at `lastIndex`, so each match starts over.
373+
/** @type {RuleConditionFunction} */
374+
const fn = (v) => {
375+
if (typeof v !== "string") return false;
376+
condition.lastIndex = 0;
377+
return condition.test(v);
378+
};
379+
return { matchWhenEmpty: fn(""), fn };
380+
}
371381
return {
372382
matchWhenEmpty: condition.test(""),
373383
fn: (v) => typeof v === "string" && condition.test(v)

‎lib/rules/UseEffectRulePlugin.js‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const util = require("util");
2222
* @import RuleSetCompiler, {
2323
* Effect,
2424
* EffectData,
25+
* EffectUse,
2526
* EffectUseType
2627
* } from "./RuleSetCompiler"
2728
*/
@@ -118,14 +119,15 @@ class UseEffectRulePlugin {
118119
"DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING"
119120
)();
120121
}
121-
return {
122-
type: enforce ? `use-${enforce}` : "use",
123-
value: {
124-
loader,
125-
options,
126-
ident
127-
}
128-
};
122+
/** @type {EffectUse["value"]} */
123+
const value = { loader, options, ident };
124+
if (item.sourceTest !== undefined) {
125+
value.sourceTest = ruleSetCompiler.compileCondition(
126+
`${path}.sourceTest`,
127+
item.sourceTest
128+
).fn;
129+
}
130+
return { type: enforce ? `use-${enforce}` : "use", value };
129131
};
130132

131133
/**

‎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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7285,6 +7285,15 @@
72857285
"$ref": "#/definitions/RuleSetLoaderOptions"
72867286
}
72877287
]
7288+
},
7289+
"sourceTest": {
7290+
"description": "Run the loader's normal function only when its current input matches this condition, the same kind of condition 'test' takes. Input that is not valid UTF-8 text always runs it.",
7291+
"oneOf": [
7292+
{
7293+
"$ref": "#/definitions/RuleSetConditionOrConditions"
7294+
}
7295+
],
7296+
"added": "5.113.0"
72887297
}
72897298
}
72907299
},

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10936,6 +10936,70 @@ Object {
1093610936
"multiple": true,
1093710937
"simpleType": "string",
1093810938
},
10939+
"module-rules-use-source-test": Object {
10940+
"configs": Array [
10941+
Object {
10942+
"description": "Run the loader's normal function only when its current input matches this condition, the same kind of condition 'test' takes. Input that is not valid UTF-8 text always runs it.",
10943+
"multiple": true,
10944+
"path": "module.rules[].use.sourceTest",
10945+
"type": "RegExp",
10946+
},
10947+
Object {
10948+
"description": "Run the loader's normal function only when its current input matches this condition, the same kind of condition 'test' takes. Input that is not valid UTF-8 text always runs it.",
10949+
"multiple": true,
10950+
"path": "module.rules[].use.sourceTest",
10951+
"type": "string",
10952+
},
10953+
],
10954+
"description": "Run the loader's normal function only when its current input matches this condition, the same kind of condition 'test' takes. Input that is not valid UTF-8 text always runs it.",
10955+
"multiple": true,
10956+
"simpleType": "string",
10957+
},
10958+
"module-rules-use-source-test-glob": Object {
10959+
"configs": Array [
10960+
Object {
10961+
"description": "A glob pattern matched against a path, using \`/\` as path separator on every OS; a \`!\` prefix excludes what it matches.",
10962+
"multiple": true,
10963+
"path": "module.rules[].use.sourceTest.glob",
10964+
"type": "string",
10965+
},
10966+
],
10967+
"description": "A glob pattern matched against a path, using \`/\` as path separator on every OS; a \`!\` prefix excludes what it matches.",
10968+
"multiple": true,
10969+
"simpleType": "string",
10970+
},
10971+
"module-rules-use-source-test-not": Object {
10972+
"configs": Array [
10973+
Object {
10974+
"description": "Logical NOT.",
10975+
"multiple": true,
10976+
"path": "module.rules[].use.sourceTest.not",
10977+
"type": "RegExp",
10978+
},
10979+
Object {
10980+
"description": "Logical NOT.",
10981+
"multiple": true,
10982+
"path": "module.rules[].use.sourceTest.not",
10983+
"type": "string",
10984+
},
10985+
],
10986+
"description": "Logical NOT.",
10987+
"multiple": true,
10988+
"simpleType": "string",
10989+
},
10990+
"module-rules-use-source-test-not-glob": Object {
10991+
"configs": Array [
10992+
Object {
10993+
"description": "A glob pattern matched against a path, using \`/\` as path separator on every OS; a \`!\` prefix excludes what it matches.",
10994+
"multiple": true,
10995+
"path": "module.rules[].use.sourceTest.not.glob",
10996+
"type": "string",
10997+
},
10998+
],
10999+
"description": "A glob pattern matched against a path, using \`/\` as path separator on every OS; a \`!\` prefix excludes what it matches.",
11000+
"multiple": true,
11001+
"simpleType": "string",
11002+
},
1093911003
"module-strict-export-presence": Object {
1094011004
"configs": Array [
1094111005
Object {

0 commit comments

Comments
 (0)