Skip to content

Commit 3600d3e

Browse files
committed
address feedback
1 parent 325879f commit 3600d3e

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export const nextJSWebpackBundleAnalysisPlugin: ExtendedBAUploadPlugin<{
8585
output.assets = collectedAssets;
8686
}
8787

88+
// need to collect all possible chunk ids beforehand
89+
// this collection is done in the processChunks function
8890
const chunkIdMap = new Map<number | string, string>();
8991
if (chunks) {
9092
output.chunks = processChunks({ chunks, chunkIdMap });

packages/webpack-plugin/src/webpack-bundle-analysis/utils/__tests__/processChunks.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Chalk from "chalk";
12
import { describe, it, expect } from "vitest";
23
import { type StatsChunk } from "webpack";
4+
import { vi } from "vitest";
35

46
import { processChunks } from "../processChunks";
57

@@ -183,4 +185,53 @@ describe("processChunks", () => {
183185
]);
184186
});
185187
});
188+
189+
describe("child chunk not found in chunkMap", () => {
190+
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null);
191+
192+
it("should log an error", () => {
193+
const chunkIdMap = new Map();
194+
const chunks = [
195+
{
196+
id: "1",
197+
entry: true,
198+
initial: true,
199+
files: ["file1.js"],
200+
names: ["chunk1"],
201+
rendered: true,
202+
recorded: true,
203+
size: 1000,
204+
hash: "hash1",
205+
sizes: {},
206+
idHints: [],
207+
children: [],
208+
auxiliaryFiles: [],
209+
childrenByOrder: {},
210+
},
211+
{
212+
id: 2,
213+
entry: true,
214+
initial: true,
215+
files: ["file2.js"],
216+
names: ["chunk2"],
217+
rendered: true,
218+
recorded: true,
219+
size: 2000,
220+
hash: "hash2",
221+
sizes: {},
222+
idHints: [],
223+
children: [3],
224+
auxiliaryFiles: [],
225+
childrenByOrder: {},
226+
},
227+
] satisfies StatsChunk[];
228+
229+
processChunks({ chunks, chunkIdMap });
230+
231+
expect(consoleSpy).toHaveBeenCalled();
232+
expect(consoleSpy).toHaveBeenCalledWith(
233+
`[codecov] ${Chalk.red("Child chunk 3 not found in chunkMap")}`,
234+
);
235+
});
236+
});
186237
});

packages/webpack-plugin/src/webpack-bundle-analysis/utils/processChunks.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { red } from "@codecov/bundler-plugin-core";
12
import { type StatsChunk } from "webpack";
23

34
export interface ProcessChunksArgs {
@@ -6,24 +7,29 @@ export interface ProcessChunksArgs {
67
}
78

89
export const processChunks = ({ chunks, chunkIdMap }: ProcessChunksArgs) => {
9-
let idCounter = 0;
10-
const chunkMap = new Map<string, StatsChunk>();
10+
const chunkMap = new Map<PropertyKey, StatsChunk>();
1111

12-
// need to collect all possible chunk ids beforehand
12+
// need to collect all possible chunk ids beforehand so we can use them to
13+
// collect the dynamic imports
1314
chunks.forEach((chunk) => {
14-
chunkMap.set(chunk.id?.toString() ?? "", chunk);
15+
if (chunk.id) {
16+
chunkMap.set(chunk.id.toString(), chunk);
17+
}
1518
});
1619

17-
return chunks.map((chunk) => {
20+
return chunks.map((chunk, index) => {
1821
const chunkId = chunk.id ?? "";
19-
const uniqueId = `${idCounter}-${chunkId}`;
22+
const uniqueId = `${index}-${chunkId}`;
2023
chunkIdMap.set(chunkId, uniqueId);
21-
idCounter += 1;
2224

2325
const dynamicImports: string[] = [];
2426
chunk.children?.forEach((child) => {
25-
const childChunk = chunkMap.get(child.toString());
26-
if (childChunk?.files) {
27+
const childIdString = child.toString();
28+
const childChunk = chunkMap.get(childIdString);
29+
30+
if (!childChunk || !childChunk.files) {
31+
red(`Child chunk ${childIdString} not found in chunkMap`);
32+
} else {
2733
dynamicImports.push(...childChunk.files);
2834
}
2935
});

packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export const webpackBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({
7878
output.assets = collectedAssets;
7979
}
8080

81+
// need to collect all possible chunk ids beforehand
82+
// this collection is done in the processChunks function
8183
const chunkIdMap = new Map<number | string, string>();
8284
if (chunks) {
8385
output.chunks = processChunks({ chunks, chunkIdMap });

0 commit comments

Comments
 (0)