Skip to content

Commit 8a40610

Browse files
fix(output): stop the copy walk where the glob cannot reach
Only `**` matches across depths, so a glob without one names how deep a match can sit and the walk no longer descends past it. `from: "*.txt"` at a project root drops from 239 stats and 7 readdirs to 29 and 1 over a 200-file `node_modules`. Also states 5.111.0 as the release the option ships in, and gives the zero-match case a directory to match nothing in.
1 parent ba4a173 commit 8a40610

8 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎declarations/WebpackOptions.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ export type Clean = boolean | CleanOptions;
555555
export type CompareBeforeEmit = boolean;
556556
/**
557557
* Copy files and directories to the output directory.
558-
* @since 5.110.0
558+
* @since 5.111.0
559559
*/
560560
export type Copy = CopyPattern[] | string;
561561
/**
@@ -2921,7 +2921,7 @@ export interface Output {
29212921
compareBeforeEmit?: CompareBeforeEmit;
29222922
/**
29232923
* Copy files and directories to the output directory.
2924-
* @since 5.110.0
2924+
* @since 5.111.0
29252925
*/
29262926
copy?: Copy;
29272927
/**

‎lib/CopyPlugin.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ const realpath = (fs, directory) =>
166166
* @param {InputFileSystem} fs input file system
167167
* @param {string} directory absolute path of the directory
168168
* @param {Set<string>} visited real paths of the directories already walked, guarding against symlink cycles
169+
* @param {number} maxDepth how many directories below this one can still hold a match
169170
* @returns {Promise<string[]>} absolute paths of the files below it, in a stable order
170171
*/
171-
const walkDirectory = async (fs, directory, visited) => {
172+
const walkDirectory = async (fs, directory, visited, maxDepth) => {
172173
const realPath = await realpath(fs, directory);
173174
if (visited.has(realPath)) return [];
174175
visited.add(realPath);
@@ -182,7 +183,9 @@ const walkDirectory = async (fs, directory, visited) => {
182183
// the entry is gone between the readdir and the stat
183184
if (stats === undefined) return [];
184185
if (stats.isDirectory()) {
185-
return walkDirectory(fs, entry, visited);
186+
return maxDepth > 0
187+
? walkDirectory(fs, entry, visited, maxDepth - 1)
188+
: [];
186189
}
187190
return stats.isFile() ? [entry] : [];
188191
}
@@ -334,14 +337,25 @@ class CopyPlugin {
334337
}
335338
// everything is copied relative to what `from` names literally, so a
336339
// directory, a file and a glob all keep the structure below it
337-
const base = unescapeGlobPath(extractGlobBaseDir(glob));
340+
const escapedBase = extractGlobBaseDir(glob);
341+
const base = unescapeGlobPath(escapedBase);
342+
// only `**` matches across depths, so any other glob names how deep a
343+
// match can sit and the walk never descends past it
344+
const segments = glob.slice(escapedBase.length).split("/");
345+
const maxDepth = segments.includes("**")
346+
? Number.POSITIVE_INFINITY
347+
: segments.length - 1;
338348
logger.debug(`'${from}' is a ${typeOfFrom}, globbing '${glob}'`);
339349

340350
const entries =
341351
typeOfFrom === "file"
342352
? [absoluteFrom]
343-
: (await walkDirectory(fs, base, new Set())).filter((filePath) =>
344-
globMatchWithOptions(glob, normalizePathSeparatorsForPath(filePath))
353+
: (await walkDirectory(fs, base, new Set(), maxDepth)).filter(
354+
(filePath) =>
355+
globMatchWithOptions(
356+
glob,
357+
normalizePathSeparatorsForPath(filePath)
358+
)
345359
);
346360

347361
if (entries.length === 0) {

‎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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@
395395
"minLength": 1
396396
}
397397
],
398-
"added": "5.110.0"
398+
"added": "5.111.0"
399399
},
400400
"CopyObjectPattern": {
401401
"description": "A pattern of files which are copied to the output directory.",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
too deep

‎test/configCases/copy/glob/index.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ it("should copy only what the glob names", () => {
1515
expect(exists("all/skip.log")).toBe(false);
1616
});
1717

18-
it("should copy a dot directory the glob names", () => {
18+
it("should copy a dot directory the glob names, no deeper than it reaches", () => {
1919
expect(read("explicit-dot/inside.txt")).toBe("inside");
20+
expect(exists("explicit-dot/deeper/ignored.txt")).toBe(false);
2021
});
2122

2223
it("should copy an absolute 'from'", () => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not a png

‎types.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21060,7 +21060,7 @@ declare interface Output {
2106021060

2106121061
/**
2106221062
* Copy files and directories to the output directory.
21063-
* @since 5.110.0
21063+
* @since 5.111.0
2106421064
*/
2106521065
copy?: string | CopyPattern[];
2106621066

0 commit comments

Comments
 (0)