From 42daf55d3b8c442b31769865279fd06b11d9c814 Mon Sep 17 00:00:00 2001 From: Natsu <784487301@qq.com> Date: Sat, 26 Jul 2025 19:03:53 +0800 Subject: [PATCH] fix: fix potential performance issue in CleanPlugin (#19735) --- lib/CleanPlugin.js | 44 ++++-------------------------------- test/CleanPlugin.unittest.js | 31 +------------------------ 2 files changed, 6 insertions(+), 69 deletions(-) diff --git a/lib/CleanPlugin.js b/lib/CleanPlugin.js index 219e23dca..5a43a7dc3 100644 --- a/lib/CleanPlugin.js +++ b/lib/CleanPlugin.js @@ -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|Set} 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} 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; diff --git a/test/CleanPlugin.unittest.js b/test/CleanPlugin.unittest.js index 64dfb17b5..48d521d8d 100644 --- a/test/CleanPlugin.unittest.js +++ b/test/CleanPlugin.unittest.js @@ -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); - }); - }); });