Skip to content

Commit bef3529

Browse files
committed
feat: add minimum size filtering to tree pruning
1 parent 475ccbc commit bef3529

File tree

1 file changed

+39
-2
lines changed
  • packages/plugin-bundle-stats/src/lib/runner/audits/details

1 file changed

+39
-2
lines changed

packages/plugin-bundle-stats/src/lib/runner/audits/details/tree.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ export interface PruningOptions {
4141
maxChildren?: number;
4242
maxDepth?: number;
4343
startDepth?: number;
44+
minSize?: number;
4445
}
4546

4647
export const DEFAULT_PRUNING = {
4748
maxDepth: 3,
4849
maxChildren: 5,
50+
minSize: 0,
4951
};
5052

5153
const ARTEFACT_TYPE_ICON_MAP: Record<ArtefactType, string> = {
@@ -185,15 +187,50 @@ function applyGrouping(nodes: TreeNode[], groups: GroupingRule[]): TreeNode[] {
185187
return finalNodes;
186188
}
187189

190+
/**
191+
* Filters children by minimum size threshold and aggregates filtered entries.
192+
* Enables size-based filtering while preserving information about smaller bundles.
193+
*/
194+
function filterByMinSize(children: TreeNode[], minSize: number): TreeNode[] {
195+
const filteredChildren = children.filter(child => child.bytes >= minSize);
196+
const belowThreshold = children.filter(child => child.bytes < minSize);
197+
198+
if (belowThreshold.length > 0) {
199+
const aggregatedBytes = belowThreshold.reduce(
200+
(acc: number, child: TreeNode) => acc + child.bytes,
201+
0,
202+
);
203+
const aggregatedSources = belowThreshold.reduce(
204+
(acc: number, child: TreeNode) => acc + child.sources,
205+
0,
206+
);
207+
208+
const summaryNode: TreeNode = {
209+
name: `... ${belowThreshold.length} files more`,
210+
bytes: aggregatedBytes,
211+
sources: aggregatedSources,
212+
children: [],
213+
type: 'group',
214+
};
215+
216+
return [...filteredChildren, summaryNode];
217+
}
218+
219+
return filteredChildren;
220+
}
221+
188222
function pruneTree(
189223
node: TreeNode,
190224
options: Required<PruningOptions>,
191225
): TreeNode {
192-
const { maxChildren, maxDepth, startDepth = 0 } = options;
226+
const { maxChildren, maxDepth, startDepth = 0, minSize } = options;
193227

194228
// Sort children by bytes in descending order before pruning
195229
node.children.sort((a, b) => b.bytes - a.bytes);
196230

231+
// Apply minimum size filtering
232+
node.children = filterByMinSize(node.children, minSize);
233+
197234
if (node.children.length > maxChildren) {
198235
const remainingChildren = node.children.slice(maxChildren);
199236
const remainingBytes = remainingChildren.reduce(
@@ -222,7 +259,7 @@ function pruneTree(
222259
return childDepth <= maxDepth;
223260
})
224261
.map((child: TreeNode) =>
225-
pruneTree(child, { maxDepth, maxChildren, startDepth: startDepth + 1 }),
262+
pruneTree(child, { maxDepth, maxChildren, startDepth: startDepth + 1, minSize }),
226263
);
227264

228265
return node;

0 commit comments

Comments
 (0)