diff --git a/.changeset/fix-search-total-size.md b/.changeset/fix-search-total-size.md new file mode 100644 index 00000000..ea62fa4b --- /dev/null +++ b/.changeset/fix-search-total-size.md @@ -0,0 +1,5 @@ +--- +"webpack-bundle-analyzer": patch +--- + +Fix the search results total size counting modules twice when both a directory and modules inside it match the search. diff --git a/client/store.js b/client/store.js index 01899544..2783eeca 100644 --- a/client/store.js +++ b/client/store.js @@ -230,8 +230,17 @@ export class Store { } get foundModulesSize() { - return this.foundModules.reduce( - (summ, module) => summ + module[this.activeSize], + const foundModules = new Set(this.foundModules); + const { activeSize } = this; + // Modules inside a found directory are already included in its size. + const sumFound = (modules) => + modules.reduce((summ, module) => { + if (foundModules.has(module)) return summ + module[activeSize]; + return module.groups ? summ + sumFound(module.groups) : summ; + }, 0); + + return this.foundModulesByChunk.reduce( + (summ, { chunk }) => summ + sumFound(chunk.groups), 0, ); } diff --git a/test/store.js b/test/store.js new file mode 100644 index 00000000..842e9b89 --- /dev/null +++ b/test/store.js @@ -0,0 +1,53 @@ +globalThis.matchMedia = () => ({ matches: false }); + +const { Store } = require("../client/store"); + +function createStore() { + const store = new Store(); + + store.setModules([ + { + label: "main.js", + statSize: 35, + groups: [ + { + label: "lodash", + path: "./node_modules/lodash", + statSize: 30, + groups: [ + { + label: "find.js", + path: "./node_modules/lodash/find.js", + statSize: 10, + }, + { + label: "map.js", + path: "./node_modules/lodash/map.js", + statSize: 20, + }, + ], + }, + { label: "index.js", path: "./src/index.js", statSize: 5 }, + ], + }, + ]); + + return store; +} + +describe("Store", () => { + it.each([ + ["lodash", 30], + ["find", 10], + ["map|index", 25], + ["lodash|find", 30], + ])( + "should not count modules inside a found directory twice (%s)", + (query, expectedSize) => { + const store = createStore(); + store.setSearchQuery(query); + + expect(store.foundModulesSize).toBe(expectedSize); + }, + ); +});