From 3600d3ecf3fe74d5e5487b11795db6ed13c9e46c Mon Sep 17 00:00:00 2001 From: nicholas-codecov Date: Tue, 3 Dec 2024 09:21:20 -0400 Subject: [PATCH] address feedback --- .../nextJSWebpackBundleAnalysisPlugin.ts | 2 + .../utils/__tests__/processChunks.test.ts | 51 +++++++++++++++++++ .../utils/processChunks.ts | 24 +++++---- .../webpackBundleAnalysisPlugin.ts | 2 + 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts b/packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts index 8e4f35cf..c6ad3305 100644 --- a/packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts +++ b/packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts @@ -85,6 +85,8 @@ export const nextJSWebpackBundleAnalysisPlugin: ExtendedBAUploadPlugin<{ output.assets = collectedAssets; } + // need to collect all possible chunk ids beforehand + // this collection is done in the processChunks function const chunkIdMap = new Map(); if (chunks) { output.chunks = processChunks({ chunks, chunkIdMap }); diff --git a/packages/webpack-plugin/src/webpack-bundle-analysis/utils/__tests__/processChunks.test.ts b/packages/webpack-plugin/src/webpack-bundle-analysis/utils/__tests__/processChunks.test.ts index e544d89b..3f30f183 100644 --- a/packages/webpack-plugin/src/webpack-bundle-analysis/utils/__tests__/processChunks.test.ts +++ b/packages/webpack-plugin/src/webpack-bundle-analysis/utils/__tests__/processChunks.test.ts @@ -1,5 +1,7 @@ +import Chalk from "chalk"; import { describe, it, expect } from "vitest"; import { type StatsChunk } from "webpack"; +import { vi } from "vitest"; import { processChunks } from "../processChunks"; @@ -183,4 +185,53 @@ describe("processChunks", () => { ]); }); }); + + describe("child chunk not found in chunkMap", () => { + const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => null); + + it("should log an error", () => { + const chunkIdMap = new Map(); + const chunks = [ + { + id: "1", + entry: true, + initial: true, + files: ["file1.js"], + names: ["chunk1"], + rendered: true, + recorded: true, + size: 1000, + hash: "hash1", + sizes: {}, + idHints: [], + children: [], + auxiliaryFiles: [], + childrenByOrder: {}, + }, + { + id: 2, + entry: true, + initial: true, + files: ["file2.js"], + names: ["chunk2"], + rendered: true, + recorded: true, + size: 2000, + hash: "hash2", + sizes: {}, + idHints: [], + children: [3], + auxiliaryFiles: [], + childrenByOrder: {}, + }, + ] satisfies StatsChunk[]; + + processChunks({ chunks, chunkIdMap }); + + expect(consoleSpy).toHaveBeenCalled(); + expect(consoleSpy).toHaveBeenCalledWith( + `[codecov] ${Chalk.red("Child chunk 3 not found in chunkMap")}`, + ); + }); + }); }); diff --git a/packages/webpack-plugin/src/webpack-bundle-analysis/utils/processChunks.ts b/packages/webpack-plugin/src/webpack-bundle-analysis/utils/processChunks.ts index 89207503..3c20c2dd 100644 --- a/packages/webpack-plugin/src/webpack-bundle-analysis/utils/processChunks.ts +++ b/packages/webpack-plugin/src/webpack-bundle-analysis/utils/processChunks.ts @@ -1,3 +1,4 @@ +import { red } from "@codecov/bundler-plugin-core"; import { type StatsChunk } from "webpack"; export interface ProcessChunksArgs { @@ -6,24 +7,29 @@ export interface ProcessChunksArgs { } export const processChunks = ({ chunks, chunkIdMap }: ProcessChunksArgs) => { - let idCounter = 0; - const chunkMap = new Map(); + const chunkMap = new Map(); - // need to collect all possible chunk ids beforehand + // need to collect all possible chunk ids beforehand so we can use them to + // collect the dynamic imports chunks.forEach((chunk) => { - chunkMap.set(chunk.id?.toString() ?? "", chunk); + if (chunk.id) { + chunkMap.set(chunk.id.toString(), chunk); + } }); - return chunks.map((chunk) => { + return chunks.map((chunk, index) => { const chunkId = chunk.id ?? ""; - const uniqueId = `${idCounter}-${chunkId}`; + const uniqueId = `${index}-${chunkId}`; chunkIdMap.set(chunkId, uniqueId); - idCounter += 1; const dynamicImports: string[] = []; chunk.children?.forEach((child) => { - const childChunk = chunkMap.get(child.toString()); - if (childChunk?.files) { + const childIdString = child.toString(); + const childChunk = chunkMap.get(childIdString); + + if (!childChunk || !childChunk.files) { + red(`Child chunk ${childIdString} not found in chunkMap`); + } else { dynamicImports.push(...childChunk.files); } }); diff --git a/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts b/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts index 87f4b473..ab60fec0 100644 --- a/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts +++ b/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts @@ -78,6 +78,8 @@ export const webpackBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({ output.assets = collectedAssets; } + // need to collect all possible chunk ids beforehand + // this collection is done in the processChunks function const chunkIdMap = new Map(); if (chunks) { output.chunks = processChunks({ chunks, chunkIdMap });