Skip to content

Commit ed1eebe

Browse files
committed
fix: handle minimal stats and undefined chunks/modules
1 parent 4ddb2d4 commit ed1eebe

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/analyzer.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,23 @@ function getViewerData(bundleStats, bundleDir, opts) {
2626

2727
const isAssetIncluded = createAssetsFilter(excludeAssets);
2828

29-
// Sometimes all the information is located in `children` array (e.g. problem in #10)
30-
if (
31-
(bundleStats.assets == null || bundleStats.assets.length === 0)
32-
&& bundleStats.children && bundleStats.children.length > 0
33-
) {
29+
// Handle minimal stats format that only has assetsByChunkName but no assets array
30+
if ((bundleStats.assets == null || bundleStats.assets.length === 0) && bundleStats.assetsByChunkName) {
31+
// Convert assetsByChunkName to assets array for minimal stats
32+
bundleStats.assets = [];
33+
Object.entries(bundleStats.assetsByChunkName).forEach(([chunkName, assetNames]) => {
34+
assetNames.forEach(assetName => {
35+
bundleStats.assets.push({
36+
name: assetName,
37+
chunks: [chunkName],
38+
size: 0 // Default size for minimal stats
39+
});
40+
});
41+
});
42+
}
43+
44+
// Sometimes all the information is located in `children` array (e.g. problem in #10)
45+
if ((bundleStats.assets == null || bundleStats.assets.length === 0) && bundleStats.children && bundleStats.children.length > 0) {
3446
const {children} = bundleStats;
3547
bundleStats = bundleStats.children[0];
3648
// Sometimes if there are additional child chunks produced add them as child assets,
@@ -189,13 +201,21 @@ function getChildAssetBundles(bundleStats, assetName) {
189201
}
190202

191203
function getBundleModules(bundleStats) {
204+
// Handle case where bundleStats is undefined or has no modules/chunks
205+
if (!bundleStats) {
206+
return [];
207+
}
208+
192209
const seenIds = new Set();
193-
194-
return flatten(
195-
((bundleStats.chunks?.map(chunk => chunk.modules)) || [])
196-
.concat(bundleStats.modules)
197-
.filter(Boolean)
198-
).filter(mod => {
210+
211+
// Safely handle chunks and modules that might be undefined
212+
const chunksModules = bundleStats.chunks ?
213+
bundleStats.chunks.map(chunk => chunk.modules || []) :
214+
[];
215+
216+
const statsModules = bundleStats.modules || [];
217+
218+
return flatten(chunksModules.concat(statsModules).filter(Boolean)).filter(mod => {
199219
// Filtering out Webpack's runtime modules as they don't have ids and can't be parsed (introduced in Webpack 5)
200220
if (isRuntimeModule(mod)) {
201221
return false;

test/analyzer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ describe('Analyzer', function () {
9696

9797
it('should handle stats with minimal configuration', async function () {
9898
generateReportFrom('minimal-stats/stats.json');
99-
await expectValidReport();
99+
await expectValidReport({
100+
bundleLabel: 'viewer.js',
101+
statSize: 0
100102
});
103+
});
101104

102105
it.skip("should not filter out modules that we couldn't find during parsing", async function () {
103106
generateReportFrom('with-missing-parsed-module/stats.json');

0 commit comments

Comments
 (0)