Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov committed Dec 3, 2024
1 parent 325879f commit 3600d3e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 89 in packages/nextjs-webpack-plugin/src/nextjs-webpack-bundle-analysis/nextJSWebpackBundleAnalysisPlugin.ts

View check run for this annotation

Codecov Notifications / codecov/patch

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

Added lines #L88 - L89 were not covered by tests
const chunkIdMap = new Map<number | string, string>();
if (chunks) {
output.chunks = processChunks({ chunks, chunkIdMap });
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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")}`,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { red } from "@codecov/bundler-plugin-core";
import { type StatsChunk } from "webpack";

export interface ProcessChunksArgs {
Expand All @@ -6,24 +7,29 @@ export interface ProcessChunksArgs {
}

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

// 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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 82 in packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts

View check run for this annotation

Codecov Notifications / codecov/patch

packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts#L81-L82

Added lines #L81 - L82 were not covered by tests
const chunkIdMap = new Map<number | string, string>();
if (chunks) {
output.chunks = processChunks({ chunks, chunkIdMap });
Expand Down

0 comments on commit 3600d3e

Please sign in to comment.