Skip to content

Commit 077aa1f

Browse files
authored
fix: don't count modules inside found directories twice in search total (#743)
Fixes #251
1 parent 0ebe1e5 commit 077aa1f

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-bundle-analyzer": patch
3+
---
4+
5+
Fix the search results total size counting modules twice when both a directory and modules inside it match the search.

‎client/store.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,17 @@ export class Store {
230230
}
231231

232232
get foundModulesSize() {
233-
return this.foundModules.reduce(
234-
(summ, module) => summ + module[this.activeSize],
233+
const foundModules = new Set(this.foundModules);
234+
const { activeSize } = this;
235+
// Modules inside a found directory are already included in its size.
236+
const sumFound = (modules) =>
237+
modules.reduce((summ, module) => {
238+
if (foundModules.has(module)) return summ + module[activeSize];
239+
return module.groups ? summ + sumFound(module.groups) : summ;
240+
}, 0);
241+
242+
return this.foundModulesByChunk.reduce(
243+
(summ, { chunk }) => summ + sumFound(chunk.groups),
235244
0,
236245
);
237246
}

‎test/store.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
globalThis.matchMedia = () => ({ matches: false });
2+
3+
const { Store } = require("../client/store");
4+
5+
function createStore() {
6+
const store = new Store();
7+
8+
store.setModules([
9+
{
10+
label: "main.js",
11+
statSize: 35,
12+
groups: [
13+
{
14+
label: "lodash",
15+
path: "./node_modules/lodash",
16+
statSize: 30,
17+
groups: [
18+
{
19+
label: "find.js",
20+
path: "./node_modules/lodash/find.js",
21+
statSize: 10,
22+
},
23+
{
24+
label: "map.js",
25+
path: "./node_modules/lodash/map.js",
26+
statSize: 20,
27+
},
28+
],
29+
},
30+
{ label: "index.js", path: "./src/index.js", statSize: 5 },
31+
],
32+
},
33+
]);
34+
35+
return store;
36+
}
37+
38+
describe("Store", () => {
39+
it.each([
40+
["lodash", 30],
41+
["find", 10],
42+
["map|index", 25],
43+
["lodash|find", 30],
44+
])(
45+
"should not count modules inside a found directory twice (%s)",
46+
(query, expectedSize) => {
47+
const store = createStore();
48+
store.setSearchQuery(query);
49+
50+
expect(store.foundModulesSize).toBe(expectedSize);
51+
},
52+
);
53+
});

0 commit comments

Comments
 (0)