Skip to content

Commit 3e459b1

Browse files
fix: preserve entrypoint metadata for appended child compilations
1 parent a5ab3a2 commit 3e459b1

2 files changed

Lines changed: 106 additions & 5 deletions

File tree

‎src/analyzer.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function getAssetModulesByChunk(statsAsset, modulesByChunk) {
197197
/** @typedef {Record<string, Record<string, boolean>>} ChunkToInitialByEntrypoint */
198198

199199
/**
200-
* @param {StatsCompilation} bundleStats bundle stats
200+
* @param {StatsCompilation | undefined} bundleStats bundle stats
201201
* @returns {ChunkToInitialByEntrypoint} chunk to initial by entrypoint
202202
*/
203203
function getChunkToInitialByEntrypoint(bundleStats) {
@@ -419,11 +419,16 @@ function getViewerData(bundleStats, bundleDir, opts) {
419419
}
420420
}
421421

422-
/** @typedef {{ size: number, parsedSize?: number, gzipSize?: number, brotliSize?: number, zstdSize?: number, modules: StatsModule[], tree: Folder }} Asset */
422+
const rootChunkToInitialByEntrypoint =
423+
getChunkToInitialByEntrypoint(bundleStats);
424+
425+
/** @typedef {{ size: number, parsedSize?: number, gzipSize?: number, brotliSize?: number, zstdSize?: number, modules: StatsModule[], tree: Folder, isInitialByEntrypoint: Record<string, boolean> }} Asset */
423426

424427
const assets = bundleStats.assets.reduce((result, statAsset) => {
425428
/** @type {StatsModule[]} */
426429
let assetModules;
430+
/** @type {Record<string, boolean>} */
431+
let isInitialByEntrypoint;
427432

428433
if (statAsset.isChild) {
429434
// Preserve child-compilation matching because child assets use a different module list.
@@ -435,10 +440,14 @@ function getViewerData(bundleStats, bundleDir, opts) {
435440
assetModules = modules.filter((statModule) =>
436441
assetHasModule(statAsset, statModule),
437442
);
443+
isInitialByEntrypoint =
444+
getChunkToInitialByEntrypoint(assetBundles)[statAsset.name] ?? {};
438445
} else {
439446
assetModules = /** @type {StatsModule[]} */ (
440447
rootAssetModules.get(statAsset) || []
441448
);
449+
isInitialByEntrypoint =
450+
rootChunkToInitialByEntrypoint[statAsset.name] ?? {};
442451
}
443452

444453
// A module that is a part of more than one asset is one stats object. Every asset that
@@ -449,6 +458,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
449458

450459
const asset = (result[statAsset.name] = /** @type {Asset} */ ({
451460
size: statAsset.size,
461+
isInitialByEntrypoint,
452462
}));
453463
const assetSources =
454464
bundlesSources && Object.hasOwn(bundlesSources, statAsset.name)
@@ -526,8 +536,6 @@ function getViewerData(bundleStats, bundleDir, opts) {
526536
return result;
527537
}, /** @type {Record<string, Asset>} */ ({}));
528538

529-
const chunkToInitialByEntrypoint = getChunkToInitialByEntrypoint(bundleStats);
530-
531539
return Object.entries(assets).map(([filename, asset]) => ({
532540
label: filename,
533541
isAsset: true,
@@ -541,7 +549,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
541549
brotliSize: asset.brotliSize,
542550
zstdSize: asset.zstdSize,
543551
groups: Object.values(asset.tree.children).map((i) => i.toChartData()),
544-
isInitialByEntrypoint: chunkToInitialByEntrypoint[filename] ?? {},
552+
isInitialByEntrypoint: asset.isInitialByEntrypoint,
545553
}));
546554
}
547555

‎test/analyzerUtils.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ describe("getViewerData", () => {
186186
chunks: [1],
187187
},
188188
],
189+
entrypoints: {
190+
firstEntry: {
191+
name: "firstEntry",
192+
assets: [{ name: "first.js" }],
193+
},
194+
},
189195
chunks: [
190196
{
191197
id: 1,
@@ -209,6 +215,12 @@ describe("getViewerData", () => {
209215
chunks: [2],
210216
},
211217
],
218+
entrypoints: {
219+
secondEntry: {
220+
name: "secondEntry",
221+
assets: [{ name: "second.js" }],
222+
},
223+
},
212224
chunks: [
213225
{
214226
id: 2,
@@ -230,7 +242,9 @@ describe("getViewerData", () => {
230242
const chartData = getViewerData(stats);
231243
expect(chartData).toHaveLength(2);
232244
expect(chartData[0].label).toBe("first.js");
245+
expect(chartData[0].isInitialByEntrypoint).toEqual({ firstEntry: true });
233246
expect(chartData[1].label).toBe("second.js");
247+
expect(chartData[1].isInitialByEntrypoint).toEqual({ secondEntry: true });
234248
});
235249

236250
it("maps modules for child compilation assets correctly", () => {
@@ -495,4 +509,83 @@ describe("getViewerData", () => {
495509
expect(chartData[0].label).toBe("chunk-asset.js");
496510
expect(chartData[0].groups[0].label).toBe("chunk-mod.js");
497511
});
512+
513+
it("preserves entrypoints for both root and child compilations when root also has assets", () => {
514+
const stats = {
515+
assets: [
516+
{
517+
name: "root.js",
518+
size: 100,
519+
chunks: [1],
520+
},
521+
{
522+
name: "unmatched-child.js",
523+
size: 50,
524+
chunks: [99],
525+
isChild: true,
526+
},
527+
],
528+
entrypoints: {
529+
rootEntry: {
530+
name: "rootEntry",
531+
assets: [{ name: "root.js" }],
532+
},
533+
},
534+
chunks: [
535+
{
536+
id: 1,
537+
modules: [
538+
{
539+
id: 1,
540+
identifier: "./root.js",
541+
name: "./root.js",
542+
size: 50,
543+
chunks: [1],
544+
},
545+
],
546+
},
547+
],
548+
children: [
549+
{
550+
name: "worker-child",
551+
assets: [
552+
{
553+
name: "worker.js",
554+
size: 150,
555+
chunks: [2],
556+
},
557+
],
558+
entrypoints: {
559+
workerEntry: {
560+
name: "workerEntry",
561+
assets: [{ name: "worker.js" }],
562+
},
563+
},
564+
chunks: [
565+
{
566+
id: 2,
567+
modules: [
568+
{
569+
id: 2,
570+
identifier: "./worker.js",
571+
name: "./worker.js",
572+
size: 75,
573+
chunks: [2],
574+
},
575+
],
576+
},
577+
],
578+
},
579+
],
580+
};
581+
582+
const chartData = getViewerData(stats);
583+
expect(chartData).toHaveLength(3);
584+
expect(chartData[0].label).toBe("root.js");
585+
expect(chartData[0].isInitialByEntrypoint).toEqual({ rootEntry: true });
586+
expect(chartData[1].label).toBe("unmatched-child.js");
587+
expect(chartData[1].isInitialByEntrypoint).toEqual({});
588+
expect(chartData[2].label).toBe("worker.js");
589+
expect(chartData[2].isInitialByEntrypoint).toEqual({ workerEntry: true });
590+
});
498591
});

0 commit comments

Comments
 (0)