From 7b233e2a5b4ffc841dda645aeb79268905a5de9c Mon Sep 17 00:00:00 2001 From: Deepak kudi Date: Thu, 4 Jun 2026 12:01:42 +0530 Subject: [PATCH] fix: handle child stats without root assets --- src/analyzer.js | 1 + test/analyzer-get-viewer-data.js | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/analyzer-get-viewer-data.js diff --git a/src/analyzer.js b/src/analyzer.js index f0634b2b..dfbb4075 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -230,6 +230,7 @@ function getViewerData(bundleStats, bundleDir, opts) { ) { const { children } = bundleStats; [bundleStats] = bundleStats.children; + bundleStats.assets ||= []; // Sometimes if there are additional child chunks produced add them as child assets, // leave the 1st one as that is considered the 'root' asset. for (let i = 1; i < children.length; i++) { diff --git a/test/analyzer-get-viewer-data.js b/test/analyzer-get-viewer-data.js new file mode 100644 index 00000000..858465cb --- /dev/null +++ b/test/analyzer-get-viewer-data.js @@ -0,0 +1,48 @@ +const analyzer = require("../src/analyzer"); + +describe("getViewerData", () => { + it("handles child stats when the root child has no assets", () => { + const chartData = analyzer.getViewerData( + { + assets: [], + children: [ + { + chunks: [], + modules: [], + }, + { + assets: [ + { + chunks: [1], + name: "child.js", + size: 12, + }, + ], + chunks: [ + { + id: 1, + modules: [ + { + chunks: [1], + id: 1, + identifier: "./child.js", + name: "./child.js", + size: 12, + }, + ], + }, + ], + }, + ], + }, + null, + ); + + expect(chartData).toMatchObject([ + { + label: "child.js", + statSize: 12, + }, + ]); + }); +});