fix: fix potential performance issue in CleanPlugin (#19735)
Github Actions / lint (push) Has been cancelled Details
Github Actions / validate-legacy-node (push) Has been cancelled Details
Github Actions / benchmark (1/4) (push) Has been cancelled Details
Github Actions / benchmark (2/4) (push) Has been cancelled Details
Github Actions / benchmark (3/4) (push) Has been cancelled Details
Github Actions / benchmark (4/4) (push) Has been cancelled Details
Github Actions / basic (push) Has been cancelled Details
Github Actions / unit (push) Has been cancelled Details
Github Actions / integration (10.x, macos-latest, a) (push) Has been cancelled Details
Github Actions / integration (10.x, macos-latest, b) (push) Has been cancelled Details
Github Actions / integration (10.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (10.x, ubuntu-latest, b) (push) Has been cancelled Details
Github Actions / integration (10.x, windows-latest, a) (push) Has been cancelled Details
Github Actions / integration (10.x, windows-latest, b) (push) Has been cancelled Details
Github Actions / integration (12.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (14.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (16.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (18.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (20.x, macos-latest, a) (push) Has been cancelled Details
Github Actions / integration (20.x, macos-latest, b) (push) Has been cancelled Details
Github Actions / integration (20.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (20.x, ubuntu-latest, b) (push) Has been cancelled Details
Github Actions / integration (20.x, windows-latest, a) (push) Has been cancelled Details
Github Actions / integration (20.x, windows-latest, b) (push) Has been cancelled Details
Github Actions / integration (22.x, macos-latest, a) (push) Has been cancelled Details
Github Actions / integration (22.x, macos-latest, b) (push) Has been cancelled Details
Github Actions / integration (22.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (22.x, ubuntu-latest, b) (push) Has been cancelled Details
Github Actions / integration (22.x, windows-latest, a) (push) Has been cancelled Details
Github Actions / integration (22.x, windows-latest, b) (push) Has been cancelled Details
Github Actions / integration (24.x, macos-latest, a) (push) Has been cancelled Details
Github Actions / integration (24.x, macos-latest, b) (push) Has been cancelled Details
Github Actions / integration (24.x, ubuntu-latest, a) (push) Has been cancelled Details
Github Actions / integration (24.x, ubuntu-latest, b) (push) Has been cancelled Details
Github Actions / integration (24.x, windows-latest, a) (push) Has been cancelled Details
Github Actions / integration (24.x, windows-latest, b) (push) Has been cancelled Details
Github Actions / integration (lts/*, ubuntu-latest, a, 1) (push) Has been cancelled Details
Github Actions / integration (lts/*, ubuntu-latest, b, 1) (push) Has been cancelled Details

This commit is contained in:
Natsu 2025-07-26 19:03:53 +08:00 committed by GitHub
parent 22596936cf
commit 42daf55d3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 69 deletions

View File

@ -87,38 +87,6 @@ function getDirectories(assets) {
return directories;
}
/**
* @param {string} a First directory path to compare
* @param {string} b Second directory path to compare
* @returns {boolean} True if both paths have the same parent directory
*/
function isEqualPath(a, b) {
return path.normalize(a) === path.normalize(b);
}
/**
* @param {Map<string, number>|Set<string>} files Collection of files to check against
* @param {string} file File path to check
* @returns {boolean} True if the file or its parent exists in the collection
*/
function hasFile(files, file) {
if (files instanceof Set) {
for (const dir of files) {
if (isEqualPath(dir, file)) {
return true;
}
}
}
if (files instanceof Map) {
for (const dir of files.keys()) {
if (isEqualPath(dir, file)) {
return true;
}
}
}
return false;
}
/** @typedef {Set<string>} Diff */
/**
@ -147,11 +115,11 @@ const getDiffToFs = (fs, outputPath, currentAssets, callback) => {
}
for (const entry of /** @type {string[]} */ (entries)) {
const file = entry;
const filename = directory ? `${directory}/${file}` : file;
if (
!hasFile(directories, filename) &&
!hasFile(currentAssets, filename)
) {
// Since path.normalize("./file") === path.normalize("file"),
// return file directly when directory === "."
const filename =
directory && directory !== "." ? `${directory}/${file}` : file;
if (!directories.has(filename) && !currentAssets.has(filename)) {
diff.add(filename);
}
}
@ -518,5 +486,3 @@ class CleanPlugin {
module.exports = CleanPlugin;
module.exports._getDirectories = getDirectories;
module.exports._hasFile = hasFile;
module.exports._isEqualPath = isEqualPath;

View File

@ -1,10 +1,6 @@
"use strict";
const {
_getDirectories,
_hasFile,
_isEqualPath
} = require("../lib/CleanPlugin");
const { _getDirectories } = require("../lib/CleanPlugin");
describe("CleanPlugin", () => {
describe("_getDirectories", () => {
@ -54,29 +50,4 @@ describe("CleanPlugin", () => {
expect([...result]).toEqual([".", "./js", "./static/js", "./static"]);
});
});
describe("_isEqualPath", () => {
it("should normalize paths before comparison", () => {
expect(_isEqualPath("this", "this")).toBe(true);
expect(_isEqualPath("this", "./this")).toBe(true);
expect(_isEqualPath("this/a", "./this/a")).toBe(true);
expect(_isEqualPath("this", "this/a")).toBe(false);
});
});
describe("_hasFile", () => {
it("should find file in Set collection", () => {
const files = new Set(["this"]);
expect(_hasFile(files, "./this")).toBe(true);
expect(_hasFile(files, "this")).toBe(true);
expect(_hasFile(files, "this/a")).toBe(false);
});
it("should find file in Map collection", () => {
const files = new Map([["this", 0]]);
expect(_hasFile(files, "this")).toBe(true);
expect(_hasFile(files, "./this")).toBe(true);
expect(_hasFile(files, "this/a")).toBe(false);
});
});
});