Skip to content

Commit 4c6e863

Browse files
authored
ref: Webpack plugin to export utility functions (#155)
Refactor Webpack plugin to export utility functions that will be used in the nextjs plugin.
1 parent b1967b1 commit 4c6e863

File tree

12 files changed

+511
-109
lines changed

12 files changed

+511
-109
lines changed

.changeset/beige-games-invite.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@codecov/webpack-plugin": patch
3+
"@codecov/bundler-plugin-core": patch
4+
"@codecov/nuxt-plugin": patch
5+
"@codecov/remix-vite-plugin": patch
6+
"@codecov/rollup-plugin": patch
7+
"@codecov/solidstart-plugin": patch
8+
"@codecov/sveltekit-plugin": patch
9+
"@codecov/vite-plugin": patch
10+
---
11+
12+
Refactor webpack plugin to export utility functions that will be used in the upcoming NextJS plugin

packages/webpack-plugin/src/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import {
1414

1515
import { webpackBundleAnalysisPlugin } from "./webpack-bundle-analysis/webpackBundleAnalysisPlugin";
1616

17+
import {
18+
findFilenameFormat,
19+
processAssets,
20+
processChunks,
21+
processModules,
22+
} from "./webpack-bundle-analysis/utils";
23+
1724
const codecovWebpackPluginFactory = createWebpackPlugin<Options, true>(
1825
(userOptions, unpluginMetaContext) => {
1926
if (checkNodeVersion(unpluginMetaContext)) {
@@ -87,3 +94,39 @@ export const codecovWebpackPlugin: (options: Options) => WebpackPluginInstance =
8794
*/
8895
export const _internal_webpackBundleAnalysisPlugin =
8996
webpackBundleAnalysisPlugin;
97+
98+
/**
99+
* Do not use this plugin directly. For internal use only.
100+
*
101+
* Used to find the filename format for a given compilation.
102+
*
103+
* @internal
104+
*/
105+
export const _internal_findFilenameFormat = findFilenameFormat;
106+
107+
/**
108+
* Do not use this plugin directly. For internal use only.
109+
*
110+
* Used to process webpack assets in other plugins.
111+
*
112+
* @internal
113+
*/
114+
export const _internal_processAssets = processAssets;
115+
116+
/**
117+
* Do not use this plugin directly. For internal use only.
118+
*
119+
* Used to process webpack chunks in other plugins.
120+
*
121+
* @internal
122+
*/
123+
export const _internal_processChunks = processChunks;
124+
125+
/**
126+
* Do not use this plugin directly. For internal use only.
127+
*
128+
* Used to process webpack modules in other plugins.
129+
*
130+
* @internal
131+
*/
132+
export const _internal_processModules = processModules;
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { describe, it, expect, vi } from "vitest";
2+
import { type StatsAsset, type Compilation } from "webpack";
3+
4+
import { processAssets } from "../processAssets";
5+
6+
describe("processAssets", () => {
7+
describe("there are output options", () => {
8+
it("should process assets", async () => {
9+
const assets = [
10+
{
11+
name: "file1.js",
12+
size: 1000,
13+
},
14+
{
15+
name: "file2.js",
16+
size: 2000,
17+
},
18+
] as StatsAsset[];
19+
20+
const compilation = {
21+
hooks: {},
22+
outputOptions: {
23+
filename: "filename",
24+
assetModuleFilename: "assetModuleFilename",
25+
chunkFilename: "chunkFilename",
26+
cssFilename: "cssFilename",
27+
cssChunkFilename: "cssChunkFilename",
28+
},
29+
getAsset: vi.fn().mockReturnValue({
30+
source: {
31+
source: vi.fn().mockReturnValue("source"),
32+
},
33+
}),
34+
// This is a fairly complex type to mock out, so we're just using the values that are needed for this test
35+
} as unknown as Compilation;
36+
37+
const processedAssets = await processAssets({ assets, compilation });
38+
39+
expect(processedAssets).toEqual([
40+
{
41+
name: "file1.js",
42+
normalized: "file1.js",
43+
size: 1000,
44+
gzipSize: 26,
45+
},
46+
{
47+
name: "file2.js",
48+
normalized: "file2.js",
49+
size: 2000,
50+
gzipSize: 26,
51+
},
52+
]);
53+
});
54+
});
55+
56+
describe("there are no output options", () => {
57+
it("should process assets", async () => {
58+
const assets = [
59+
{
60+
name: "file1.js",
61+
size: 1000,
62+
},
63+
{
64+
name: "file2.js",
65+
size: 2000,
66+
},
67+
] as StatsAsset[];
68+
69+
const compilation = {
70+
hooks: {},
71+
outputOptions: {},
72+
getAsset: vi.fn().mockReturnValue({
73+
source: {
74+
source: vi.fn().mockReturnValue("source"),
75+
},
76+
}),
77+
// This is a fairly complex type to mock out, so we're just using the values that are needed for this test
78+
} as unknown as Compilation;
79+
80+
const processedAssets = await processAssets({ assets, compilation });
81+
82+
expect(processedAssets).toEqual([
83+
{
84+
name: "file1.js",
85+
normalized: "file1.js",
86+
size: 1000,
87+
gzipSize: 26,
88+
},
89+
{
90+
name: "file2.js",
91+
normalized: "file2.js",
92+
size: 2000,
93+
gzipSize: 26,
94+
},
95+
]);
96+
});
97+
});
98+
99+
describe("when there are source maps", () => {
100+
it("should not process the asset", async () => {
101+
const assets = [
102+
{
103+
name: "file1.js",
104+
size: 1000,
105+
},
106+
{
107+
name: "file1.js.map",
108+
size: 2000,
109+
},
110+
] as StatsAsset[];
111+
112+
const compilation = {
113+
hooks: {},
114+
outputOptions: {
115+
filename: "filename",
116+
assetModuleFilename: "assetModuleFilename",
117+
chunkFilename: "chunkFilename",
118+
cssFilename: "cssFilename",
119+
cssChunkFilename: "cssChunkFilename",
120+
},
121+
getAsset: vi.fn().mockReturnValue({
122+
source: {
123+
source: vi.fn().mockReturnValue("source"),
124+
},
125+
}),
126+
// This is a fairly complex type to mock out, so we're just using the values that are needed for this test
127+
} as unknown as Compilation;
128+
129+
const processedAssets = await processAssets({ assets, compilation });
130+
131+
expect(processedAssets).toEqual([
132+
{
133+
name: "file1.js",
134+
normalized: "file1.js",
135+
size: 1000,
136+
gzipSize: 26,
137+
},
138+
]);
139+
});
140+
});
141+
});
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { describe, it, expect } from "vitest";
2+
import { type StatsChunk } from "webpack";
3+
4+
import { processChunks } from "../processChunks";
5+
6+
describe("processChunks", () => {
7+
describe("there are ids present", () => {
8+
it("should process chunks", () => {
9+
const chunks = [
10+
{
11+
id: 1,
12+
entry: true,
13+
initial: true,
14+
files: ["file1.js"],
15+
names: ["chunk1"],
16+
rendered: true,
17+
recorded: true,
18+
size: 1000,
19+
hash: "hash1",
20+
},
21+
{
22+
id: 2,
23+
entry: true,
24+
initial: true,
25+
files: ["file2.js"],
26+
names: ["chunk2"],
27+
rendered: true,
28+
recorded: true,
29+
size: 2000,
30+
hash: "hash2",
31+
},
32+
] satisfies StatsChunk[];
33+
34+
const chunkIdMap = new Map();
35+
36+
expect(processChunks({ chunks, chunkIdMap })).toEqual([
37+
{
38+
id: "1",
39+
uniqueId: "0-1",
40+
entry: true,
41+
initial: true,
42+
files: ["file1.js"],
43+
names: ["chunk1"],
44+
},
45+
{
46+
id: "2",
47+
uniqueId: "1-2",
48+
entry: true,
49+
initial: true,
50+
files: ["file2.js"],
51+
names: ["chunk2"],
52+
},
53+
]);
54+
});
55+
});
56+
57+
describe("there are no ids present", () => {
58+
it("should process chunks", () => {
59+
const chunks = [
60+
{
61+
entry: true,
62+
initial: true,
63+
files: ["file1.js"],
64+
names: ["chunk1"],
65+
rendered: true,
66+
recorded: true,
67+
size: 1000,
68+
hash: "hash1",
69+
},
70+
{
71+
entry: true,
72+
initial: true,
73+
files: ["file2.js"],
74+
names: ["chunk2"],
75+
rendered: true,
76+
recorded: true,
77+
size: 2000,
78+
hash: "hash2",
79+
},
80+
] satisfies StatsChunk[];
81+
82+
const chunkIdMap = new Map();
83+
84+
expect(processChunks({ chunks, chunkIdMap })).toEqual([
85+
{
86+
id: "",
87+
uniqueId: "0-",
88+
entry: true,
89+
initial: true,
90+
files: ["file1.js"],
91+
names: ["chunk1"],
92+
},
93+
{
94+
id: "",
95+
uniqueId: "1-",
96+
entry: true,
97+
initial: true,
98+
files: ["file2.js"],
99+
names: ["chunk2"],
100+
},
101+
]);
102+
});
103+
});
104+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, it, expect } from "vitest";
2+
import { type StatsModule } from "webpack";
3+
4+
import { processModules } from "../processModules";
5+
6+
describe("processModules", () => {
7+
describe("modules have chunks", () => {
8+
it("should process modules", () => {
9+
const modules = [
10+
{
11+
name: "module1",
12+
size: 1000,
13+
chunks: [1],
14+
},
15+
{
16+
name: "module2",
17+
size: 2000,
18+
chunks: [2],
19+
},
20+
] satisfies StatsModule[];
21+
22+
const chunkIdMap = new Map();
23+
chunkIdMap.set(1, "0-1");
24+
chunkIdMap.set(2, "1-2");
25+
26+
expect(processModules({ modules, chunkIdMap })).toEqual([
27+
{
28+
name: "module1",
29+
size: 1000,
30+
chunkUniqueIds: ["0-1"],
31+
},
32+
{
33+
name: "module2",
34+
size: 2000,
35+
chunkUniqueIds: ["1-2"],
36+
},
37+
]);
38+
});
39+
});
40+
41+
describe("modules have no chunks", () => {
42+
it("should process modules", () => {
43+
const modules = [
44+
{
45+
name: "module1",
46+
size: 1000,
47+
},
48+
{
49+
name: "module2",
50+
size: 2000,
51+
},
52+
] satisfies StatsModule[];
53+
54+
const chunkIdMap = new Map();
55+
56+
expect(processModules({ modules, chunkIdMap })).toEqual([
57+
{
58+
name: "module1",
59+
size: 1000,
60+
chunkUniqueIds: [],
61+
},
62+
{
63+
name: "module2",
64+
size: 2000,
65+
chunkUniqueIds: [],
66+
},
67+
]);
68+
});
69+
});
70+
});

packages/webpack-plugin/src/webpack-bundle-analysis/findFileFormat.ts renamed to packages/webpack-plugin/src/webpack-bundle-analysis/utils/findFileFormat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const STRIP_CHARS_REGEX = /(\w|\[|]|\/)/g;
22

3-
interface FindFilenameFormatArgs {
3+
export interface FindFilenameFormatArgs {
44
assetName: string;
55
filename: string;
66
assetModuleFilename: string;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { findFilenameFormat } from "./findFileFormat";
2+
export { processAssets } from "./processAssets";
3+
export { processChunks } from "./processChunks";
4+
export { processModules } from "./processModules";

0 commit comments

Comments
 (0)