8000 Merge pull request #14509 from webpack/feature/regexp-managed-paths · webpack/webpack@9b784a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b784a6

Browse files
authored
Merge pull request #14509 from webpack/feature/regexp-managed-paths
allow RegExp as managed and immutable paths
2 parents ccdeabf + 5e16d64 commit 9b784a6

File tree

10 files changed

+216
-83
lines changed

10 files changed

+216
-83
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,11 +941,11 @@ export interface FileCacheOptions {
941941
/**
942942
* List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.
943943
*/
944-
immutablePaths?: string[];
944+
immutablePaths?: (RegExp | string)[];
945945
/**
946946
* List of paths that are managed by a package manager and can be trusted to not be modified otherwise.
947947
*/
948-
managedPaths?: string[];
948+
managedPaths?: (RegExp | string)[];
949949
/**
950950
* Time for which unused cache entries stay in the filesystem cache at minimum (in milliseconds).
951951
*/
@@ -2276,11 +2276,11 @@ export interface SnapshotOptions {
22762276
/**
22772277
* List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.
22782278
*/
2279-
immutablePaths?: string[];
2279+
immutablePaths?: (RegExp | string)[];
22802280
/**
22812281
* List of paths that are managed by a package manager and can be trusted to not be modified otherwise.
22822282
*/
2283-
managedPaths?: string[];
2283+
managedPaths?: (RegExp | string)[];
22842284
/**
22852285
* Options for snapshotting dependencies of modules to determine if they need to be built again.
22862286
*/
F438

lib/Compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ class Compiler {
219219
/** @type {string|null} */
220220
this.recordsOutputPath = null;
221221
this.records = {};
222-
/** @type {Set<string>} */
222+
/** @type {Set<string | RegExp>} */
223223
this.managedPaths = new Set();
224-
/** @type {Set<string>} */
224+
/** @type {Set<string | RegExp>} */
225225
this.immutablePaths = new Set();
226226

227227
/** @type {ReadonlySet<string>} */

lib/FileSystemInfo.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,8 @@ class FileSystemInfo {
853853
/**
854854
* @param {InputFileSystem} fs file system
855855
* @param {Object} options options
856-
* @param {Iterable<string>=} options.managedPaths paths that are only managed by a package manager
857-
* @param {Iterable<string>=} options.immutablePaths paths that are immutable
856+
* @param {Iterable<string | RegExp>=} options.managedPaths paths that are only managed by a package manager
857+
* @param {Iterable<string | RegExp>=} options.immutablePaths paths that are immutable
858858
* @param {Logger=} options.logger logger used to log invalid snapshots
859859
* @param {string | Hash=} options.hashFunction the hash function to use
860860
*/
@@ -996,12 +996,19 @@ class FileSystemInfo {
996996
processor: this._getManagedItemDirectoryInfo.bind(this)
997997
});
998998
this.managedPaths = Array.from(managedPaths);
999-
this.managedPathsWithSlash = this.managedPaths.map(p =>
1000-
join(fs, p, "_").slice(0, -1)
999+
this.managedPathsWithSlash = /** @type {string[]} */ (
1000+
this.managedPaths.filter(p => typeof p === "string")
1001+
).map(p => join(fs, p, "_").slice(0, -1));
1002+
1003+
this.managedPathsRegExps = /** @type {RegExp[]} */ (
1004+
this.managedPaths.filter(p => typeof p !== "string")
10011005
);
10021006
this.immutablePaths = Array.from(immutablePaths);
1003-
this.immutablePathsWithSlash = this.immutablePaths.map(p =>
1004-
join(fs, p, "_").slice(0, -1)
1007+
this.immutablePathsWithSlash = /** @type {string[]} */ (
1008+
this.immutablePaths.filter(p => typeof p === "string")
1009+
).map(p => join(fs, p, "_").slice(0, -1));
1010+
this.immutablePathsRegExps = /** @type {RegExp[]} */ (
1011+
this.immutablePaths.filter(p => typeof p !== "string")
10051012
);
10061013

10071014
this._cachedDeprecatedFileTimestamps = undefined;
@@ -1966,12 +1973,29 @@ class FileSystemInfo {
19661973
}
19671974
};
19681975
const checkManaged = (path, managedSet) => {
1976+
for (const immutablePath of this.immutablePathsRegExps) {
1977+
if (immutablePath.test(path)) {
1978+
managedSet.add(path);
1979+
return true;
1980+
}
1981+
}
19691982
for (const immutablePath of this.immutablePathsWithSlash) {
19701983
if (path.startsWith(immutablePath)) {
19711984
managedSet.add(path);
19721985
return true;
19731986
}
19741987
}
1988+
for (const managedPath of this.managedPathsRegExps) {
1989+
const match = managedPath.exec(path);
1990+
if (match) {
1991+
const managedItem = getManagedItem(managedPath[1], path);
1992+
if (managedItem) {
1993+
managedItems.add(managedItem);
1994+
managedSet.add(path);
1995+
return true;
1996+
}
1997+
}
1998+
}
19751999
for (const managedPath of this.managedPathsWithSlash) {
19762000
if (path.startsWith(managedPath)) {
19772001
const managedItem = getManagedItem(managedPath, path);
@@ -2923,10 +2947,29 @@ class FileSystemInfo {
29232947
files,
29242948
(file, callback) => {
29252949
const child = join(this.fs, path, file);
2950+
for (const immutablePath of this.immutablePathsRegExps) {
2951+
if (immutablePath.test(path)) {
2952+
// ignore any immutable path for timestamping
2953+
return callback(null, fromImmutablePath(path));
2954+
}
2955+
}
29262956
for (const immutablePath of this.immutablePathsWithSlash) {
29272957
if (path.startsWith(immutablePath)) {
29282958
// ignore any immutable path for timestamping
2929-
return callback(null, fromImmutablePath(immutablePath));
2959+
return callback(null, fromImmutablePath(path));
2960+
}
2961+
}
2962+
for (const managedPath of this.managedPathsRegExps) {
2963+
const match = managedPath.exec(path);
2964+
if (match) {
2965+
const managedItem = getManagedItem(managedPath[1], path);
2966+
if (managedItem) {
2967+
// construct timestampHash from managed info
2968+
return this.managedItemQueue.add(managedItem, (err, info) => {
2969+
if (err) return callback(err);
2970+
return callback(null, fromManagedItem(info));
2971+
});
2972+
}
29302973
}
29312974
}
29322975
for (const managedPath of this.managedPathsWithSlash) {

lib/cache/AddManagedPathsPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
class AddManagedPathsPlugin {
1111
/**
12-
* @param {Iterable<string>} managedPaths list of managed paths
13-
* @param {Iterable<string>} immutablePaths list of immutable paths
12+
* @param {Iterable<string | RegExp>} managedPaths list of managed paths
13+
* @param {Iterable<string | RegExp>} immutablePaths list of immutable paths
1414
*/
1515
constructor(managedPaths, immutablePaths) {
1616
this.managedPaths = new Set(managedPaths);

lib/config/defaults.js

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ const applyWebpackOptionsDefaults = options => {
161161

162162
applyExperimentsDefaults(options.experiments, { production, development });
163163

164+
const futureDefaults = options.experiments.futureDefaults;
165+
164166
F(options, "cache", () =>
165167
development ? { type: /** @type {"memory"} */ ("memory") } : false
166168
);
@@ -172,7 +174,10 @@ const applyWebpackOptionsDefaults = options => {
172174
});
173175
const cache = !!options.cache;
174176

175-
applySnapshotDefaults(options.snapshot, { production });
177+
applySnapshotDefaults(options.snapshot, {
178+
production,
179+
futureDefaults
180+
});
176181

177182
applyModuleDefaults(options.module, {
178183
cache,
@@ -192,7 +197,7 @@ const applyWebpackOptionsDefaults = options => {
192197
development,
193198
entry: options.entry,
194199
module: options.module,
195-
futureDefaults: options.experiments.futureDefaults
200+
futureDefaults
196201
});
197202

198203
applyExternalsPresetsDefaults(options.externalsPresets, {
@@ -348,49 +353,65 @@ const applyCacheDefaults = (
348353
* @param {SnapshotOptions} snapshot options
349354
* @param {Object} options options
350355
* @param {boolean} options.production is production
356+
* @param {boolean} options.futureDefaults is future defaults enabled
351357
* @returns {void}
352358
*/
353-
const applySnapshotDefaults = (snapshot, { production }) => {
354-
A(snapshot, "managedPaths", () => {
355-
if (process.versions.pnp === "3") {
356-
const match =
357-
/^(.+?)[\\/]cache[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
358-
require.resolve("watchpack")
359-
);
360-
if (match) {
361-
return [path.resolve(match[1], "unplugged")];
362-
}
363-
} else {
364-
const match = /^(.+?[\\/]node_modules)[\\/]/.exec(
365-
// eslint-disable-next-line node/no-extraneous-require
366-
require.resolve("watchpack")
367-
);
368-
if (match) {
369-
return [match[1]];
370-
}
371-
}
372-
return [];
373-
});
374-
A(snapshot, "immutablePaths", () => {
375-
if (process.versions.pnp === "1") {
376-
const match =
377-
/^(.+?[\\/]v4)[\\/]npm-watchpack-[^\\/]+-[\da-f]{40}[\\/]node_modules[\\/]/.exec(
359+
const applySnapshotDefaults = (snapshot, { production, futureDefaults }) => {
360+
if (futureDefaults) {
361+
F(snapshot, "managedPaths", () =>
362+
process.versions.pnp === "3"
363+
? [
364+
/^(.+?(?:[\\/]\.yarn[\\/]unplugged[\\/][^\\/]+)?[\\/]node_modules[\\/])/
365+
]
366+
: [/^(.+?[\\/]node_modules[\\/])/]
367+
);
368+
F(snapshot, "immutablePaths", () =>
369+
process.versions.pnp === "3"
370+
? [/^(.+?[\\/]cache[\\/][^\\/]+\.zip[\\/]node_modules[\\/])/]
371+
: []
372+
);
373+
} else {
374+
A(snapshot, "managedPaths", () => {
375+
if (process.versions.pnp === "3") {
376+
const match =
377+
/^(.+?)[\\/]cache[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
378+
require.resolve("watchpack")
379+
);
380+
if (match) {
381+
return [path.resolve(match[1], "unplugged")];
382+
}
383+
} else {
384+
const match = /^(.+?[\\/]node_modules)[\\/]/.exec(
385+
// eslint-disable-next-line node/no-extraneous-require
378386
require.resolve("watchpack")
379387
);
380-
if (match) {
381-
return [match[1]];
388+
if (match) {
389+
return [match[1]];
390+
}
382391
}
383-
} else if (process.versions.pnp === "3") {
384-
const match =
385-
/^(.+?)[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
386-
require.resolve("watchpack")
387-
);
388-
if (match) {
389-
return [match[1]];
392+
return [];
393+
});
394+
A(snapshot, "immutablePaths", () => {
395+
if (process.versions.pnp === "1") {
396+
const match =
397+
/^(.+?[\\/]v4)[\\/]npm-watchpack-[^\\/]+-[\da-f]{40}[\\/]node_modules[\\/]/.exec(
398+
require.resolve("watchpack")
399+
);
400+
if (match) {
401+
return [match[1]];
402+
}
403+
} else if (process.versions.pnp === "3") {
404+
const match =
405+
/^(.+?)[\\/]watchpack-npm-[^\\/]+\.zip[\\/]node_modules[\\/]/.exec(
406+
require.resolve("watchpack")
407+
);
408+
if (match) {
409+
return [match[1]];
410+
}
390411
}
391-
}
392-
return [];
393-
});
412+
return [];
413+
});
414+
}
394415
F(snapshot, "resolveBuildDependencies", () => ({
395416
timestamp: true,
396417
hash: true

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: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,20 +1005,40 @@
10051005
"description": "List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.",
10061006
"type": "array",
10071007
"items": {
1008-
"description": "A path to a immutable directory (usually a package manager cache directory).",
1009-
"type": "string",
1010-
"absolutePath": true,
1011-
"minLength": 1
1008+
"description": "List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.",
1009+
"anyOf": [
1010+
{
1011+
"description": "A RegExp matching a immutable directory (usually a package manager cache directory, including the tailing slash)",
1012+
"instanceof": "RegExp",
1013+
"tsType": "RegExp"
1014+
},
1015+
{
1016+
"description": "A path to a immutable directory (usually a package manager cache directory).",
1017+
"type": "string",
1018+
"absolutePath": true,
1019+
"minLength": 1
1020+
}
1021+
]
10121022
}
10131023
},
10141024
"managedPaths": {
10151025
"description": "List of paths that are managed by a package manager and can be trusted to not be modified otherwise.",
10161026
"type": "array",
10171027
"items": {
1018-
"description": "A path to a managed directory (usually a node_modules directory).",
1019-
"type": "string",
1020-
"absolutePath": true,
1021-
"minLength": 1
1028+
"description": "List of paths that are managed by a package manager and can be trusted to not be modified otherwise.",
1029+
"anyOf": [
1030+
{
1031+
"description": "A RegExp matching a managed directory (usually a node_modules directory, including the tailing slash)",
1032+
"instanceof": "RegExp",
1033+
"tsType": "RegExp"
1034+
},
1035+
{
1036+
"description": "A path to a managed directory (usually a node_modules directory).",
1037+
"type": "string",
1038+
"absolutePath": true,
1039+
"minLength": 1
1040+
}
1041+
]
10221042
}
10231043
},
10241044
"maxAge": {
@@ -3952,20 +3972,40 @@
39523972
"description": "List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.",
39533973
"type": "array",
39543974
"items": {
3955-
"descr F438 iption": "A path to a immutable directory (usually a package manager cache directory).",
3956-
"type": "string",
3957-
"absolutePath": true,
3958-
"minLength": 1
3975+
"description": "List of paths that are managed by a package manager and contain a version or hash in its path so all files are immutable.",
3976+
"anyOf": [
3977+
{
3978+
"description": "A RegExp matching a immutable directory (usually a package manager cache directory, including the tailing slash)",
3979+
"instanceof": "RegExp",
3980+
"tsType": "RegExp"
3981+
},
3982+
{
3983+
"description": "A path to a immutable directory (usually a package manager cache directory).",
3984+
"type": "string",
3985+
"absolutePath": true,
3986+
"minLength": 1
3987+
}
3988+
]
39593989
}
39603990
},
39613991
"managedPaths": {
39623992
"description": "List of paths that are managed by a package manager and can be trusted to not be modified otherwise.",
39633993
"type": "array",
39643994
"items": {
3965-
"description": "A path to a managed directory (usually a node_modules directory).",
3966-
"type": "string",
3967-
"absolutePath": true,
3968-
"minLength": 1
3995+
"description": "List of paths that are managed by a package manager and can be trusted to not be modified otherwise.",
3996+
"anyOf": [
3997+
{
3998+
"description": "A RegExp matching a managed directory (usually a node_modules directory, including the tailing slash)",
3999+
"instanceof": "RegExp",
4000+
"tsType": "RegExp"
4001+
},
4002+
{
4003+
"description": "A path to a managed directory (usually a node_modules directory).",
4004+
"type": "string",
4005+
"absolutePath": true,
4006+
"minLength": 1
4007+
}
4008+
]
39694009 }
39704010
},
39714011
"module": {

test/Defaults.unittest.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,9 @@ Object {
19101910
@@ ... @@
19111911
- "hashFunction": "md4",
19121912
+ "hashFunction": "xxhash64",
1913+
@@ ... @@
1914+
- "<cwd>/node_modules",
1915+
+ /^(.+?[\\\\/]node_modules[\\\\/])/,
19131916
`)
19141917
);
19151918
});

0 commit comments

Comments
 (0)
0