Skip to content

Commit ffa7ce7

Browse files
feat: accept glob patterns in more path options (#22478)
* feat: accept glob patterns in more path options * type * fix * test: regenerate validator and align module-test-glob after rebase module-test lost its fixed waits on main; the glob copy raced the update. * refactor: share glob helpers across path options compileGlobList backs both matchGlobCondition and RuleSetCompiler's glob condition, matchModuleGlobCondition replaces three nameForCondition copies, and compileRelativeGlobPaths serves resolve.restrictions, WatchIgnorePlugin and snapshot paths. --------- Co-authored-by: alexander-akait <[email protected]>
1 parent 1850e5a commit ffa7ce7

61 files changed

Lines changed: 912 additions & 133 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/009-snapshot-paths-glob.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Accept a glob such as `node_modules` in `snapshot.managedPaths`, `immutablePaths` and `unmanagedPaths`.
5+
Accept glob patterns in more path, module and file filter options.

‎declarations/WebpackOptions.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,11 @@ export interface CleanOptions {
309309
/**
310310
* Keep these assets.
311311
*/
312-
keep?: RegExp | RelativePath | import("../lib/output/CleanPlugin").KeepFn;
312+
keep?:
313+
| RegExp
314+
| RelativePath
315+
| import("../lib/output/CleanPlugin").KeepFn
316+
| GlobCondition;
313317
}
314318

315319
/**
@@ -1883,6 +1887,17 @@ export interface GeneratorOptionsByModuleTypeUnknown {
18831887
export type GeneratorOptionsByModuleType = GeneratorOptionsByModuleTypeKnown &
18841888
GeneratorOptionsByModuleTypeUnknown;
18851889

1890+
/**
1891+
* Match glob patterns against the value, `!` in front of a pattern excludes it. Path separators are normalized to `/` on every OS, and a relative pattern matches at any depth.
1892+
* @since 5.112.0
1893+
*/
1894+
export interface GlobCondition {
1895+
/**
1896+
* Glob patterns to match.
1897+
*/
1898+
glob: RuleSetGlobs | RuleSetGlob;
1899+
}
1900+
18861901
/**
18871902
* An expression which is used to address the global object/scope in runtime code.
18881903
*/
@@ -2173,9 +2188,11 @@ export type IgnoreWarnings = Array<
21732188
| /** Ignore specific warnings. */ /** A RegExp to select the warning message. */ RegExp
21742189
| {
21752190
/**
2176-
* A RegExp to select the origin file for the warning.
2191+
* A RegExp or a glob to select the origin file for the warning.
21772192
*/
2178-
file?: RegExp;
2193+
file?:
2194+
| RegExp
2195+
| /** A glob matching the origin file for the warning at any depth. @since 5.112.0 */ NonEmptyString;
21792196
/**
21802197
* A RegExp to select the warning message.
21812198
*/
@@ -2632,7 +2649,11 @@ export interface LazyCompilationOptions {
26322649
/**
26332650
* Specify which entrypoints or import()ed modules should be lazily compiled. This is matched with the imported module and not the entrypoint name.
26342651
*/
2635-
test?: RegExp | string | import("../lib/hmr/LazyCompilationPlugin").TestFn;
2652+
test?:
2653+
| RegExp
2654+
| string
2655+
| import("../lib/hmr/LazyCompilationPlugin").TestFn
2656+
| GlobCondition;
26362657
}
26372658

26382659
/**
@@ -2953,10 +2974,12 @@ export type NoParse =
29532974
| /** @minItems 1 */ Array<
29542975
| /** Don't parse files matching. It's matched against the full resolved request. */ /** A regular expression, when matched the module is not parsed. */ RegExp
29552976
| /** An absolute path, when the module starts with this path it is not parsed. */ AbsolutePath
2977+
| /** A glob, when it matches the module's resource path at any depth the module is not parsed. @since 5.112.0 */ NonEmptyRelativePath
29562978
| import("../lib/module/NormalModule").NoParseFn
29572979
>
29582980
| /** A regular expression, when matched the module is not parsed. */ RegExp
29592981
| /** An absolute path, when the module starts with this path it is not parsed. */ AbsolutePath
2982+
| /** A glob, when it matches the module's resource path at any depth the module is not parsed. @since 5.112.0 */ NonEmptyRelativePath
29602983
| import("../lib/module/NormalModule").NoParseFn;
29612984

29622985
/**
@@ -3658,7 +3681,10 @@ export interface OptimizationSplitChunksCacheGroup {
36583681
* Assign modules to a cache group by module name.
36593682
*/
36603683
test?:
3661-
RegExp | string | import("../lib/optimize/SplitChunksPlugin").CheckTestFn;
3684+
| RegExp
3685+
| string
3686+
| import("../lib/optimize/SplitChunksPlugin").CheckTestFn
3687+
| GlobCondition;
36623688
/**
36633689
* Assign modules to a cache group by module type.
36643690
*/
@@ -4956,6 +4982,7 @@ export interface ResolveOptions {
49564982
restrictions?: Array<
49574983
| /** Resolve restriction. Resolve result must fulfill this restriction. */ RegExp
49584984
| AbsolutePath
4985+
| /** A glob the resolve result must match at any depth. @since 5.112.0 */ NonEmptyRelativePath
49594986
>;
49604987
/**
49614988
* A list of directories in which requests that are server-relative URLs (starting with '/') are resolved.

‎lib/config/WebpackOptionsApply.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ const DefaultStatsPrinterPlugin = require("../stats/DefaultStatsPrinterPlugin");
5555
const TemplatedPathPlugin = require("../template/TemplatedPathPlugin");
5656

5757
const { cleverMerge } = require("../util/cleverMerge");
58-
const {
59-
ABSOLUTE_PATH_REGEXP,
60-
NODE_MODULES_REGEXP
61-
} = require("../util/identifier");
58+
const { NODE_MODULES_REGEXP } = require("../util/identifier");
6259
const OptionsApply = require("./OptionsApply");
6360

6461
/** @import { WebpackPluginFunction } from "../webpack" */
@@ -1110,15 +1107,19 @@ class WebpackOptionsApply extends OptionsApply {
11101107
* @param {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} paths snapshot paths
11111108
* @returns {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} paths with relative globs compiled
11121109
*/
1113-
const compileSnapshotPaths = (paths) =>
1114-
paths.map((item) =>
1115-
typeof item === "string" && !ABSOLUTE_PATH_REGEXP.test(item)
1116-
? require("../util/globUtils").createPathGlobPrefixRegExp(
1117-
item,
1118-
/** @type {string} */ (options.context)
1119-
)
1120-
: item
1110+
const compileSnapshotPaths = (paths) => {
1111+
const {
1112+
compileRelativeGlobPaths,
1113+
createPathGlobPrefixRegExp
1114+
} = require("../util/globUtils");
1115+
1116+
return compileRelativeGlobPaths(paths, (pattern) =>
1117+
createPathGlobPrefixRegExp(
1118+
pattern,
1119+
/** @type {string} */ (options.context)
1120+
)
11211121
);
1122+
};
11221123
const snapshotOptions = {
11231124
...options.snapshot,
11241125
managedPaths: compileSnapshotPaths(

‎lib/config/normalization.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
const util = require("util");
99
const { fileUrlToPath } = require("../util/identifier");
10+
const memoize = require("../util/memoize");
11+
12+
const getCreatePathGlobRegExp = memoize(
13+
() => require("../util/globUtils").createPathGlobRegExp
14+
);
1015

1116
/**
1217
* @import {
@@ -307,6 +312,10 @@ const getNormalizedWebpackOptions = (config) => ({
307312
? config.ignoreWarnings.map((ignore) => {
308313
if (typeof ignore === "function") return ignore;
309314
const i = ignore instanceof RegExp ? { message: ignore } : ignore;
315+
const file =
316+
typeof i.file === "string"
317+
? getCreatePathGlobRegExp()(i.file)
318+
: i.file;
310319
return (warning, { requestShortener }) => {
311320
if (!i.message && !i.module && !i.file) return false;
312321
if (i.message && !i.message.test(warning.message)) {
@@ -323,9 +332,9 @@ const getNormalizedWebpackOptions = (config) => ({
323332
return false;
324333
}
325334
if (
326-
i.file &&
335+
file &&
327336
(!(/** @type {WebpackError} */ (warning).file) ||
328-
!i.file.test(/** @type {WebpackError} */ (warning).file))
337+
!file.test(/** @type {WebpackError} */ (warning).file))
329338
) {
330339
return false;
331340
}

‎lib/devtool/ModuleFilenameHelpers.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ const { DEFAULTS } = require("../config/defaults");
99
const createHash = require("../util/createHash");
1010
const memoize = require("../util/memoize");
1111

12+
const getMatchGlobCondition = memoize(
13+
() => require("../util/globUtils").matchGlobCondition
14+
);
15+
1216
/** @import { HashFunction } from "../../declarations/WebpackOptions" */
1317
/** @import ChunkGraph from "../graph/ChunkGraph" */
1418
/** @import Module from "../module/Module" */
1519
/** @import RequestShortener from "../util/RequestShortener" */
1620

1721
/** @typedef {(str: string) => boolean} MatcherFn */
18-
/** @typedef {string | RegExp | MatcherFn | (string | RegExp | MatcherFn)[]} Matcher */
22+
/** @typedef {import("../util/globUtils").GlobCondition} GlobCondition */
23+
/** @typedef {string | RegExp | MatcherFn | GlobCondition | (string | RegExp | MatcherFn | GlobCondition)[]} Matcher */
1924
/** @typedef {{ test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
2025

2126
const ModuleFilenameHelpers = module.exports;
@@ -360,6 +365,8 @@ const matchPart = (str, test) => {
360365
return str.startsWith(test);
361366
} else if (typeof test === "function") {
362367
return test(str);
368+
} else if (!Array.isArray(test)) {
369+
return getMatchGlobCondition()(test, str);
363370
}
364371

365372
return test.some((test) => matchPart(str, test));

‎lib/devtool/SourceMapDevToolPlugin.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,30 @@ const generateDebugId = require("./generateDebugId");
8080
* @typedef {import("./ModuleFilenameHelpers").ModuleFilenameTemplateFunction} SourceMapDevToolPluginOptionsModuleFilenameTemplateBranch2
8181
*/
8282

83+
/**
84+
* A glob pattern.
85+
* @inline
86+
* @minLength 1
87+
* @typedef {string} RuleGlobPattern
88+
*/
89+
90+
/**
91+
* @minItems 1
92+
* @inline
93+
* @typedef {RuleGlobPattern[]} RuleGlobList
94+
*/
95+
96+
/**
97+
* Glob patterns matched against the file name, `!` in front of a pattern excludes it, and a relative pattern matches at any depth.
98+
* @since 5.112.0
99+
* @inline
100+
* @typedef {object} RuleGlob
101+
* @property {RuleGlobList | NonEmptyString} glob Glob patterns to match.
102+
*/
103+
83104
/**
84105
* Condition used to match resource (string, RegExp or Function).
85-
* @typedef {RegExp | NonEmptyString | import("./ModuleFilenameHelpers").MatcherFn} Rule
106+
* @typedef {RegExp | NonEmptyString | RuleGlob | import("./ModuleFilenameHelpers").MatcherFn} Rule
86107
*/
87108

88109
/**

‎lib/hmr/LazyCompilationPlugin.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ const {
1919
const RuntimeGlobals = require("../runtime/RuntimeGlobals");
2020
const Template = require("../template/Template");
2121
const { resolveByProperty } = require("../util/cleverMerge");
22+
const memoize = require("../util/memoize");
2223
const { registerNotSerializable } = require("../util/serialization");
2324

25+
const getMatchModuleGlobCondition = memoize(
26+
() => require("../util/globUtils").matchModuleGlobCondition
27+
);
28+
2429
/**
2530
* @import {
2631
* WebpackOptionsNormalizedWithDefaults as WebpackOptions
@@ -182,6 +187,9 @@ const checkTest = (test, module) => {
182187
const name = module.nameForCondition();
183188
return name && test.test(name);
184189
}
190+
if (typeof test === "object") {
191+
return getMatchModuleGlobCondition()(test, module);
192+
}
185193
return false;
186194
};
187195

@@ -505,14 +513,15 @@ class LazyCompilationDependencyFactory extends ModuleFactory {
505513
/** @typedef {BackendHandler | PromiseBackendHandler} BackEnd */
506514

507515
/** @typedef {(module: Module) => boolean} TestFn */
516+
/** @typedef {import("../util/globUtils").GlobCondition} GlobCondition */
508517

509518
/**
510519
* Defines the options type used by this module.
511520
* @typedef {object} Options options
512521
* @property {BackEnd} backend the backend
513522
* @property {boolean=} entries
514523
* @property {boolean=} imports
515-
* @property {RegExp | string | TestFn=} test additional filter for lazy compiled entrypoint modules
524+
* @property {RegExp | string | TestFn | GlobCondition=} test additional filter for lazy compiled entrypoint modules
516525
*/
517526

518527
const PLUGIN_NAME = "LazyCompilationPlugin";
@@ -529,7 +538,7 @@ class LazyCompilationPlugin {
529538
this.entries = entries;
530539
/** @type {boolean | undefined} */
531540
this.imports = imports;
532-
/** @type {string | RegExp | TestFn | undefined} */
541+
/** @type {string | RegExp | GlobCondition | TestFn | undefined} */
533542
this.test = test;
534543
}
535544

‎lib/module/NormalModule.js‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const {
4848
ABSOLUTE_PATH_REGEXP,
4949
absolutify,
5050
contextify,
51-
contextifySourceUrl
51+
contextifySourceUrl,
52+
parseResource
5253
} = require("../util/identifier");
5354
const makeSerializable = require("../util/makeSerializable");
5455
const memoize = require("../util/memoize");
@@ -58,6 +59,13 @@ const { JAVASCRIPT_MODULE_TYPE_AUTO } = require("./ModuleTypeConstants");
5859

5960
const getValidate = memoize(() => require("schema-utils").validate);
6061

62+
const getCreatePathGlobMatcher = memoize(
63+
() => require("../util/globUtils").createPathGlobMatcher
64+
);
65+
66+
/** @type {Map<string, (path: string) => boolean>} */
67+
const noParseGlobMatchers = new Map();
68+
6169
const getGenerator = memoize(() => require("./Generator"));
6270

6371
const getExtractSourceMap = memoize(() => require("./extractSourceMap"));
@@ -1655,14 +1663,25 @@ class NormalModule extends Module {
16551663
}
16561664

16571665
/**
1666+
* An absolute path string is a prefix of `content`; a relative one is a glob
1667+
* matched against the resource path, without loaders and query.
16581668
* @param {Exclude<NoParse, EXPECTED_ANY[]>} rule rule
16591669
* @param {string} content content
16601670
* @returns {boolean} result
16611671
*/
16621672
applyNoParseRule(rule, content) {
1663-
// must start with "rule" if rule is a string
16641673
if (typeof rule === "string") {
1665-
return content.startsWith(rule);
1674+
if (ABSOLUTE_PATH_REGEXP.test(rule)) return content.startsWith(rule);
1675+
let match = noParseGlobMatchers.get(rule);
1676+
if (match === undefined) {
1677+
const matcher = getCreatePathGlobMatcher()(rule);
1678+
if (matcher === null) {
1679+
throw new Error(`Invalid glob pattern in module.noParse: ${rule}`);
1680+
}
1681+
match = matcher;
1682+
noParseGlobMatchers.set(rule, match);
1683+
}
1684+
return match(parseResource(this.resource).path);
16661685
}
16671686

16681687
if (typeof rule === "function") {
@@ -1931,7 +1950,16 @@ class NormalModule extends Module {
19311950
// check if this module should !not! be parsed.
19321951
// if so, exit here;
19331952
const noParseRule = options.module && options.module.noParse;
1934-
if (this.shouldPreventParsing(noParseRule, this.request)) {
1953+
/** @type {boolean} */
1954+
let preventParsing;
1955+
try {
1956+
preventParsing = this.shouldPreventParsing(noParseRule, this.request);
1957+
} catch (err_) {
1958+
this.markModuleAsErrored(err_);
1959+
this._initBuildHash(compilation);
1960+
return callback();
1961+
}
1962+
if (preventParsing) {
19351963
// We assume that we need module and exports
19361964
/** @type {NormalModuleBuildInfo} */
19371965
(this.buildInfo).parsed = false;

‎lib/optimize/SplitChunksPlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const { STAGE_ADVANCED } = require("./OptimizationStages");
2222
const { discoverIntersections } = require("./chunkSetIntersections");
2323
const deterministicGrouping = require("./deterministicGrouping");
2424

25+
const getMatchModuleGlobCondition = memoize(
26+
() => require("../util/globUtils").matchModuleGlobCondition
27+
);
28+
2529
/**
2630
* @import {
2731
* OptimizationSplitChunksCacheGroup,
@@ -670,6 +674,9 @@ const checkTest = (test, module, context) => {
670674
const name = module.nameForCondition();
671675
return name ? test.test(name) : false;
672676
}
677+
if (typeof test === "object") {
678+
return getMatchModuleGlobCondition()(test, module);
679+
}
673680
return false;
674681
};
675682

@@ -690,6 +697,9 @@ const matchesStaticTest = (test, module) => {
690697
const name = module.nameForCondition();
691698
return name ? test.test(name) : false;
692699
}
700+
if (typeof test === "object") {
701+
return getMatchModuleGlobCondition()(test, module);
702+
}
693703
if (typeof test === "boolean") return test;
694704
return true;
695705
};

0 commit comments

Comments
 (0)