Skip to content

Commit bd5366b

Browse files
committed
feat: accept glob pattern in snapshot paths
1 parent 089b4c0 commit bd5366b

8 files changed

Lines changed: 223 additions & 22 deletions

File tree

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+
Accept a glob such as `node_modules` in `snapshot.managedPaths`, `immutablePaths` and `unmanagedPaths`.

‎declarations/WebpackOptions.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5394,13 +5394,15 @@ export interface SnapshotOptions {
53945394
immutablePaths?: Array<
53955395
| /** List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable. */ /** A RegExp matching an immutable directory (usually a package manager cache directory, including the tailing slash) */ RegExp
53965396
| /** A path to an immutable directory (usually a package manager cache directory). */ AbsolutePath
5397+
| /** A glob matching an immutable directory at any depth, or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost. @since 5.112.0 */ NonEmptyRelativePath
53975398
>;
53985399
/**
53995400
* List of paths that are managed by a package manager and can be trusted to not be modified otherwise.
54005401
*/
54015402
managedPaths?: Array<
54025403
| /** List of paths that are managed by a package manager and can be trusted to not be modified otherwise. */ /** A RegExp matching a managed directory (usually a node_modules directory, including the tailing slash) */ RegExp
54035404
| /** A path to a managed directory (usually a node_modules directory). */ AbsolutePath
5405+
| /** A glob matching a managed directory at any depth (e.g. `node_modules`), or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost. @since 5.112.0 */ NonEmptyRelativePath
54045406
>;
54055407
/**
54065408
* Options for snapshotting dependencies of modules to determine if they need to be built again.
@@ -5447,6 +5449,7 @@ export interface SnapshotOptions {
54475449
unmanagedPaths?: Array<
54485450
| /** List of paths that are not managed by a package manager and the contents are subject to change. */ /** A RegExp matching an unmanaged directory. */ RegExp
54495451
| /** A path to an unmanaged directory. */ AbsolutePath
5452+
| /** A glob matching an unmanaged directory at any depth, or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost. @since 5.112.0 */ NonEmptyRelativePath
54505453
>;
54515454
}
54525455

‎lib/config/WebpackOptionsApply.js‎

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

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

6164
/** @import { WebpackPluginFunction } from "../webpack" */
@@ -1103,13 +1106,39 @@ class WebpackOptionsApply extends OptionsApply {
11031106

11041107
const AddManagedPathsPlugin = require("../cache/AddManagedPathsPlugin");
11051108

1109+
/**
1110+
* @param {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} paths snapshot paths
1111+
* @returns {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} paths with relative globs compiled
1112+
*/
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
1121+
);
1122+
const snapshotOptions = {
1123+
...options.snapshot,
1124+
managedPaths: compileSnapshotPaths(
1125+
/** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
1126+
(options.snapshot.managedPaths)
1127+
),
1128+
immutablePaths: compileSnapshotPaths(
1129+
/** @type {NonNullable<WebpackOptions["snapshot"]["immutablePaths"]>} */
1130+
(options.snapshot.immutablePaths)
1131+
),
1132+
unmanagedPaths: compileSnapshotPaths(
1133+
/** @type {NonNullable<WebpackOptions["snapshot"]["unmanagedPaths"]>} */
1134+
(options.snapshot.unmanagedPaths)
1135+
)
1136+
};
1137+
11061138
new AddManagedPathsPlugin(
1107-
/** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
1108-
(options.snapshot.managedPaths),
1109-
/** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
1110-
(options.snapshot.immutablePaths),
1111-
/** @type {NonNullable<WebpackOptions["snapshot"]["managedPaths"]>} */
1112-
(options.snapshot.unmanagedPaths)
1139+
snapshotOptions.managedPaths,
1140+
snapshotOptions.immutablePaths,
1141+
snapshotOptions.unmanagedPaths
11131142
).apply(compiler);
11141143

11151144
if (options.cache && typeof options.cache === "object") {
@@ -1190,7 +1219,7 @@ class WebpackOptionsApply extends OptionsApply {
11901219
logger: compiler.getInfrastructureLogger(
11911220
"webpack.cache.PackFileCacheStrategy"
11921221
),
1193-
snapshot: options.snapshot,
1222+
snapshot: snapshotOptions,
11941223
maxAge: /** @type {number} */ (cacheOptions.maxAge),
11951224
profile: cacheOptions.profile,
11961225
allowCollectingMemory: cacheOptions.allowCollectingMemory,

‎lib/util/globUtils.js‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,12 @@ const segmentGlobToRegExpSource = (segment) => {
518518
* `path.matchesGlob` semantics: `*`, `?` and classes stay within a segment,
519519
* a whole-segment `**` spans segments, and neither crosses a dot segment.
520520
* @param {string} glob brace-free glob
521+
* @param {boolean} lazy whether `**` spans as few segments as it can
521522
* @returns {string} regexp source without anchors
522523
*/
523-
const braceFreeGlobToPathRegExpSource = (glob) => {
524+
const braceFreeGlobToPathRegExpSource = (glob, lazy) => {
524525
const segments = optimizeGlobSegments(glob.split("/"));
526+
const repeat = lazy ? "*?" : "*";
525527
let source = "";
526528
let needSeparator = false;
527529
for (let i = 0; i < segments.length; i++) {
@@ -536,8 +538,8 @@ const braceFreeGlobToPathRegExpSource = (glob) => {
536538
source += isLast
537539
? `${needSeparator ? "/" : ""}(?:${NON_DOT_SEGMENT}(?:/${NON_DOT_SEGMENT})*)?`
538540
: needSeparator
539-
? `(?:/${NON_DOT_SEGMENT})*`
540-
: `/?(?:${NON_DOT_SEGMENT}/)*`;
541+
? `(?:/${NON_DOT_SEGMENT})${repeat}`
542+
: `/?(?:${NON_DOT_SEGMENT}/)${repeat}`;
541543
needSeparator = !isLast && needSeparator;
542544
continue;
543545
}
@@ -574,8 +576,8 @@ const createPathGlobMatcher = (pattern, options = {}) => {
574576
) {
575577
normalizedPattern = `**/${normalizedPattern}`;
576578
}
577-
const sources = expandGlobBraces(normalizedPattern).map(
578-
braceFreeGlobToPathRegExpSource
579+
const sources = expandGlobBraces(normalizedPattern).map((glob) =>
580+
braceFreeGlobToPathRegExpSource(glob, false)
579581
);
580582
const source = sources.length === 1 ? sources[0] : `(?:${sources.join("|")})`;
581583
/** @type {RegExp} */
@@ -598,6 +600,54 @@ const createPathGlobMatcher = (pattern, options = {}) => {
598600
};
599601
};
600602

603+
// a separator in a path regexp source, which a native path may spell `\`
604+
const PATH_SOURCE_SEPARATOR_REGEXP = /\\[\s\S]|\[\^\/|\//g;
605+
/** @type {Record<string, string>} */
606+
const PATH_SOURCE_SEPARATORS = { "/": "[\\\\/]", "[^/": "[^\\\\/" };
607+
608+
/**
609+
* Compiles a relative glob into a RegExp whose first group is the outermost
610+
* directory it matches at the start of a path, trailing separator included — the
611+
* shape `snapshot.managedPaths` and its siblings read. Like `module.rules[].glob`
612+
* it matches at any depth, unless a leading `./` or `../` anchors it to `context`.
613+
* @param {string} pattern relative glob pattern
614+
* @param {string} context directory a `./` or `../` pattern is relative to
615+
* @returns {RegExp} prefix regexp
616+
*/
617+
const createPathGlobPrefixRegExp = (pattern, context) => {
618+
let glob = normalizePathSeparatorsForPath(pattern);
619+
let baseSource = "";
620+
if (glob.startsWith("./") || glob.startsWith("../")) {
621+
let base = context;
622+
for (;;) {
623+
if (glob.startsWith("./")) {
624+
glob = glob.slice(2);
625+
} else if (glob.startsWith("../")) {
626+
base = path.dirname(base);
627+
glob = glob.slice(3);
628+
} else {
629+
break;
630+
}
631+
}
632+
for (const c of path.join(base, "/")) {
633+
baseSource += c === "/" || c === "\\" ? "[\\\\/]" : quoteRegExpChar(c);
634+
}
635+
} else if (!glob.startsWith("**/")) {
636+
glob = `**/${glob}`;
637+
}
638+
if (!glob.endsWith("/")) glob += "/";
639+
const sources = expandGlobBraces(glob).map((brace) =>
640+
braceFreeGlobToPathRegExpSource(brace, true)
641+
);
642+
const source = (
643+
sources.length === 1 ? sources[0] : `(?:${sources.join("|")})`
644+
).replace(
645+
PATH_SOURCE_SEPARATOR_REGEXP,
646+
(match) => PATH_SOURCE_SEPARATORS[match] || match
647+
);
648+
return new RegExp(`^(${baseSource}${source})`);
649+
};
650+
601651
/**
602652
* @param {string} path path
603653
* @param {string} baseDir base directory
@@ -962,6 +1012,7 @@ const globPatternBaseReachesDir = (patterns, dirPath) => {
9621012
module.exports = {
9631013
commonGlobBaseDir,
9641014
createPathGlobMatcher,
1015+
createPathGlobPrefixRegExp,
9651016
escapeGlobPattern,
9661017
extractGlobBaseDir,
9671018
getGlobToRegExpSource,

‎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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,6 +7234,13 @@
72347234
"description": "A path to an immutable directory (usually a package manager cache directory).",
72357235
"type": "string",
72367236
"absolutePath": true
7237+
},
7238+
{
7239+
"description": "A glob matching an immutable directory at any depth, or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost.",
7240+
"type": "string",
7241+
"absolutePath": false,
7242+
"minLength": 1,
7243+
"added": "5.112.0"
72377244
}
72387245
]
72397246
}
@@ -7253,6 +7260,13 @@
72537260
"description": "A path to a managed directory (usually a node_modules directory).",
72547261
"type": "string",
72557262
"absolutePath": true
7263+
},
7264+
{
7265+
"description": "A glob matching a managed directory at any depth (e.g. `node_modules`), or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost.",
7266+
"type": "string",
7267+
"absolutePath": false,
7268+
"minLength": 1,
7269+
"added": "5.112.0"
72567270
}
72577271
]
72587272
}
@@ -7317,6 +7331,13 @@
73177331
"description": "A path to an unmanaged directory.",
73187332
"type": "string",
73197333
"absolutePath": true
7334+
},
7335+
{
7336+
"description": "A glob matching an unmanaged directory at any depth, or relative to the context when it starts with `./` or `../`; a path under several matches takes the outermost.",
7337+
"type": "string",
7338+
"absolutePath": false,
7339+
"minLength": 1,
7340+
"added": "5.112.0"
73207341
}
73217342
]
73227343
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17937,8 +17937,14 @@ Object {
1793717937
"path": "snapshot.immutablePaths[]",
1793817938
"type": "path",
1793917939
},
17940+
Object {
17941+
"description": "A glob matching an immutable directory at any depth, or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
17942+
"multiple": true,
17943+
"path": "snapshot.immutablePaths[]",
17944+
"type": "string",
17945+
},
1794017946
],
17941-
"description": "A RegExp matching an immutable directory (usually a package manager cache directory, including the tailing slash) A path to an immutable directory (usually a package manager cache directory).",
17947+
"description": "A RegExp matching an immutable directory (usually a package manager cache directory, including the tailing slash) A path to an immutable directory (usually a package manager cache directory). A glob matching an immutable directory at any depth, or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
1794217948
"multiple": true,
1794317949
"simpleType": "string",
1794417950
},
@@ -17969,8 +17975,14 @@ Object {
1796917975
"path": "snapshot.managedPaths[]",
1797017976
"type": "path",
1797117977
},
17978+
Object {
17979+
"description": "A glob matching a managed directory at any depth (e.g. \`node_modules\`), or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
17980+
"multiple": true,
17981+
"path": "snapshot.managedPaths[]",
17982+
"type": "string",
17983+
},
1797217984
],
17973-
"description": "A RegExp matching a managed directory (usually a node_modules directory, including the tailing slash) A path to a managed directory (usually a node_modules directory).",
17985+
"description": "A RegExp matching a managed directory (usually a node_modules directory, including the tailing slash) A path to a managed directory (usually a node_modules directory). A glob matching a managed directory at any depth (e.g. \`node_modules\`), or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
1797417986
"multiple": true,
1797517987
"simpleType": "string",
1797617988
},
@@ -18079,8 +18091,14 @@ Object {
1807918091
"path": "snapshot.unmanagedPaths[]",
1808018092
"type": "path",
1808118093
},
18094+
Object {
18095+
"description": "A glob matching an unmanaged directory at any depth, or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
18096+
"multiple": true,
18097+
"path": "snapshot.unmanagedPaths[]",
18098+
"type": "string",
18099+
},
1808218100
],
18083-
"description": "A RegExp matching an unmanaged directory. A path to an unmanaged directory.",
18101+
"description": "A RegExp matching an unmanaged directory. A path to an unmanaged directory. A glob matching an unmanaged directory at any depth, or relative to the context when it starts with \`./\` or \`../\`; a path under several matches takes the outermost.",
1808418102
"multiple": true,
1808518103
"simpleType": "string",
1808618104
},

‎test/configCases/cache-dependencies/managed-paths-regexp/webpack.config.js‎

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
const path = require("path");
44

5+
/** @typedef {import("../../../../").NormalModule} NormalModule */
6+
/** @typedef {NonNullable<NormalModule["buildInfo"]>} BuildInfo */
7+
/** @typedef {NonNullable<BuildInfo["snapshot"]>} Snapshot */
8+
/** @typedef {import("../../../../").Configuration["snapshot"]} SnapshotOptions */
9+
510
// A `managedPaths` RegExp is documented as matching the managed directory
611
// itself, so one without a capture group names it with the whole match.
712
const managedPath = new RegExp(
@@ -10,11 +15,31 @@ const managedPath = new RegExp(
1015
.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&")}[\\\\/]`
1116
);
1217

13-
/** @type {import("../../../../").Configuration} */
14-
module.exports = {
15-
snapshot: {
16-
managedPaths: [managedPath]
17-
},
18+
/**
19+
* @param {Snapshot} snapshot snapshot
20+
* @param {("managedItemInfo" | "fileTimestamps" | "fileHashes" | "fileTshs")[]} keys snapshot entries
21+
* @returns {string[]} paths of those entries in the snapshot and its children
22+
*/
23+
const collect = (snapshot, keys) => {
24+
/** @type {string[]} */
25+
const items = [];
26+
for (const key of keys) {
27+
const map = snapshot[key];
28+
if (map) items.push(...map.keys());
29+
}
30+
for (const child of snapshot.children || []) {
31+
items.push(...collect(child, keys));
32+
}
33+
return items;
34+
};
35+
36+
/**
37+
* @param {SnapshotOptions} snapshot snapshot options
38+
* @param {{ managedItems: string[], files: string[] }} expected paths relative to the case
39+
* @returns {import("../../../../").Configuration} configuration
40+
*/
41+
const createConfig = (snapshot, expected) => ({
42+
snapshot,
1843
plugins: [
1944
(compiler) => {
2045
compiler.hooks.done.tap("Test", ({ compilation }) => {
@@ -23,10 +48,59 @@ module.exports = {
2348
path.resolve(__dirname, "node_modules/package/index.js")
2449
);
2550
expect(fileDeps).toContain(path.resolve(__dirname, "index.js"));
51+
const packageModule = /** @type {NormalModule} */ (
52+
[...compilation.modules].find(
53+
(module) =>
54+
/** @type {NormalModule} */ (module).resource ===
55+
path.resolve(__dirname, "node_modules/package/index.js")
56+
)
57+
);
58+
const moduleSnapshot = /** @type {Snapshot} */ (
59+
/** @type {BuildInfo} */ (packageModule.buildInfo).snapshot
60+
);
61+
expect(collect(moduleSnapshot, ["managedItemInfo"])).toEqual(
62+
expected.managedItems.map((item) => path.resolve(__dirname, item))
63+
);
64+
expect(
65+
collect(moduleSnapshot, [
66+
"fileTimestamps",
67+
"fileHashes",
68+
"fileTshs"
69+
]).filter((file) =>
70+
file.startsWith(path.resolve(__dirname, "node_modules"))
71+
)
72+
).toEqual(expected.files.map((item) => path.resolve(__dirname, item)));
2673
});
2774
}
2875
],
2976
module: {
3077
unsafeCache: false
3178
}
79+
});
80+
81+
const managed = {
82+
managedItems: ["node_modules/package"],
83+
files: []
3284
};
85+
86+
/** @type {import("../../../../").Configuration[]} */
87+
module.exports = [
88+
createConfig({ managedPaths: [managedPath] }, managed),
89+
createConfig({ managedPaths: ["./**/node_modules"] }, managed),
90+
createConfig({ managedPaths: ["../*/node_modules"] }, managed),
91+
createConfig({ managedPaths: ["node_modules"] }, managed),
92+
createConfig(
93+
{
94+
managedPaths: ["**/node_modules"],
95+
unmanagedPaths: ["**/node_modules/package"]
96+
},
97+
{
98+
managedItems: [],
99+
files: ["node_modules/package/index.js", "node_modules/package/extra.js"]
100+
}
101+
),
102+
createConfig(
103+
{ managedPaths: [], immutablePaths: ["**/node_modules"] },
104+
{ managedItems: [], files: [] }
105+
)
106+
];

0 commit comments

Comments
 (0)