Skip to content

Commit 475ccbc

Browse files
committed
feat: sort nodes and children by bytes for improved pruning
1 parent fbe2139 commit 475ccbc

File tree

1 file changed

+7
-0
lines changed
  • packages/plugin-bundle-stats/src/lib/runner/audits/details

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ function pruneTree(
190190
options: Required<PruningOptions>,
191191
): TreeNode {
192192
const { maxChildren, maxDepth, startDepth = 0 } = options;
193+
194+
// Sort children by bytes in descending order before pruning
195+
node.children.sort((a, b) => b.bytes - a.bytes);
196+
193197
if (node.children.length > maxChildren) {
194198
const remainingChildren = node.children.slice(maxChildren);
195199
const remainingBytes = remainingChildren.reduce(
@@ -282,6 +286,9 @@ export function createTree(
282286
const { title, groups, pruning } = options;
283287
let nodes = convertToTree(statsSlice);
284288
nodes = applyGrouping(nodes, groups);
289+
290+
// Sort nodes by bytes in descending order at the root level
291+
nodes.sort((a, b) => b.bytes - a.bytes);
285292

286293
const tempRoot: TreeNode = {
287294
name: 'temp-root',

0 commit comments

Comments
 (0)