Skip to content

Commit 78966dc

Browse files
authored
feat: Add in bundle name config option (#26)
Add in bundle name configuration option, as well as appending any name from the bundler, as well as the format being bundled to (esm, cjs, etc.)
1 parent 7fc27b3 commit 78966dc

File tree

13 files changed

+112
-15
lines changed

13 files changed

+112
-15
lines changed

integration-tests/fixtures/generate-bundle-stats/rollup-plugin.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ describe("Generating rollup stats", () => {
5252
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
5353
resolve(),
5454
commonjs(),
55-
codecovRollupPlugin({ enableBundleAnalysis: true, dryRun: true }),
55+
codecovRollupPlugin({
56+
enableBundleAnalysis: true,
57+
dryRun: true,
58+
bundleName: "rollup-test",
59+
}),
5660
],
5761
}).then((bundle) =>
5862
bundle.write({
@@ -89,4 +93,8 @@ describe("Generating rollup stats", () => {
8993
it("sets the correct bundler information", () => {
9094
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
9195
});
96+
97+
it("sets the correct bundle name", () => {
98+
expect(stats.bundleName).toStrictEqual("rollup-test-es");
99+
});
92100
});

integration-tests/fixtures/generate-bundle-stats/vite-plugin.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ describe("Generating vite stats", () => {
5656
},
5757
},
5858
plugins: [
59-
codecovVitePlugin({ enableBundleAnalysis: true, dryRun: true }),
59+
codecovVitePlugin({
60+
enableBundleAnalysis: true,
61+
dryRun: true,
62+
bundleName: "vite-test",
63+
}),
6064
],
6165
});
6266

@@ -88,4 +92,8 @@ describe("Generating vite stats", () => {
8892
it("sets the correct bundler information", () => {
8993
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
9094
});
95+
96+
it("sets the correct bundle name", () => {
97+
expect(stats.bundleName).toStrictEqual("vite-test-cjs");
98+
});
9199
});

integration-tests/fixtures/generate-bundle-stats/webpack-plugin.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ describe("Generating webpack stats", () => {
5454
},
5555
mode: "production",
5656
plugins: [
57-
codecovWebpackPlugin({ enableBundleAnalysis: true, dryRun: true }),
57+
codecovWebpackPlugin({
58+
enableBundleAnalysis: true,
59+
dryRun: true,
60+
bundleName: "webpack-test",
61+
}),
5862
],
5963
},
6064
(err) => {
@@ -95,4 +99,8 @@ describe("Generating webpack stats", () => {
9599
it("sets the correct bundler information", () => {
96100
expect(stats.bundler).toStrictEqual(expectedStats.bundler);
97101
});
102+
103+
it("sets the correct bundle name", () => {
104+
expect(stats.bundleName).toStrictEqual("webpack-test-array-push");
105+
});
98106
});

packages/bundler-plugin-core/src/bundle-analysis/__tests__/bundleAnalysisPluginFactory.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { bundleAnalysisPluginFactory } from "../bundleAnalysisPluginFactory";
33
describe("bundleAnalysisPluginFactory", () => {
44
it("returns a build start function", () => {
55
const plugin = bundleAnalysisPluginFactory({
6-
userOptions: {},
6+
userOptions: { bundleName: "test" },
77
bundleAnalysisUploadPlugin: () => ({
88
version: "1",
99
name: "plugin-name",
@@ -16,7 +16,7 @@ describe("bundleAnalysisPluginFactory", () => {
1616

1717
it("returns a build end function", () => {
1818
const plugin = bundleAnalysisPluginFactory({
19-
userOptions: {},
19+
userOptions: { bundleName: "test" },
2020
bundleAnalysisUploadPlugin: () => ({
2121
version: "1",
2222
name: "plugin-name",
@@ -29,7 +29,7 @@ describe("bundleAnalysisPluginFactory", () => {
2929

3030
it("returns a write bundle function", () => {
3131
const plugin = bundleAnalysisPluginFactory({
32-
userOptions: {},
32+
userOptions: { bundleName: "test" },
3333
bundleAnalysisUploadPlugin: () => ({
3434
version: "1",
3535
name: "plugin-name",

packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export const bundleAnalysisPluginFactory = ({
1919
userOptions,
2020
bundleAnalysisUploadPlugin,
2121
}: BundleAnalysisUploadPluginArgs): UnpluginOptions => {
22-
// const dryRun = userOptions?.dryRun ?? false;
2322
const output: Output = {
2423
version: "1",
24+
bundleName: userOptions.bundleName ?? "",
2525
};
2626

2727
const { pluginVersion, version, ...pluginOpts } = bundleAnalysisUploadPlugin({
@@ -47,6 +47,9 @@ export const bundleAnalysisPluginFactory = ({
4747
// don't need to do anything here if dryRun is true
4848
if (userOptions?.dryRun) return;
4949

50+
// don't need to do anything if the bundle name is not present or empty
51+
if (!userOptions.bundleName || userOptions.bundleName === "") return;
52+
5053
const args: UploadOverrides = userOptions.uploaderOverrides ?? {};
5154
const envs = process.env;
5255
const inputs: ProviderUtilInputs = { envs, args };

packages/bundler-plugin-core/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface CodecovUnpluginFactoryOptions {
2121
bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin;
2222
}
2323

24-
export function codecovUnpluginFactory({
24+
function codecovUnpluginFactory({
2525
bundleAnalysisUploadPlugin,
2626
}: CodecovUnpluginFactoryOptions) {
2727
return createUnplugin<Options, true>((userOptions, unpluginMetaContext) => {
@@ -48,6 +48,8 @@ export function codecovUnpluginFactory({
4848
});
4949
}
5050

51+
export { red, codecovUnpluginFactory };
52+
5153
export type {
5254
BundleAnalysisUploadPlugin,
5355
Asset,

packages/bundler-plugin-core/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface Module {
2828

2929
export interface Output {
3030
version?: string;
31+
bundleName: string;
3132
bundler?: {
3233
name: string;
3334
version: string;
@@ -92,6 +93,15 @@ export interface Options {
9293
* Defaults to `false`
9394
*/
9495
dryRun?: boolean;
96+
97+
/**
98+
* The name for the bundle being built.
99+
*
100+
* Required for uploading bundle analysis information.
101+
*
102+
* Example: `rollup-package`
103+
*/
104+
bundleName?: string;
95105
}
96106

97107
export type BundleAnalysisUploadPlugin = (

packages/rollup-plugin/src/rollup-bundle-analysis/__tests__/rollupBundleAnalysisPlugin.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ describe("rollupBundleAnalysisPlugin", () => {
44
describe("when called", () => {
55
it("returns a plugin object", () => {
66
const plugin = rollupBundleAnalysisPlugin({
7-
output: {},
8-
userOptions: {},
7+
output: {
8+
bundleName: "test",
9+
},
10+
userOptions: {
11+
bundleName: "test",
12+
},
913
});
1014

1115
expect(plugin.version).toEqual("1");

packages/rollup-plugin/src/rollup-bundle-analysis/rollupBundleAnalysisPlugin.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type Chunk,
55
type Module,
66
type BundleAnalysisUploadPlugin,
7+
red,
78
} from "@codecov/bundler-plugin-core";
89

910
const PLUGIN_NAME = "codecov-rollup-bundle-analysis-plugin";
@@ -17,6 +18,19 @@ export const rollupBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({
1718
pluginVersion: "1.0.0",
1819
rollup: {
1920
generateBundle(this, options, bundle) {
21+
// don't need to do anything if the bundle name is not present or empty
22+
if (!userOptions.bundleName || userOptions.bundleName === "") {
23+
red("Bundle name is not present or empty. Skipping upload.");
24+
return;
25+
}
26+
27+
// append bundle output format to bundle name
28+
output.bundleName = `${userOptions.bundleName}-${options.format}`;
29+
30+
if (options.name && options.name !== "") {
31+
output.bundleName = `${userOptions.bundleName}-${options.name}`;
32+
}
33+
2034
const customOptions = {
2135
moduleOriginalSize: false,
2236
...options,

packages/vite-plugin/src/vite-bundle-analysis/__tests__/viteBundleAnalysisPlugin.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ describe("viteBundleAnalysisPlugin", () => {
44
describe("when called", () => {
55
it("returns a plugin object", () => {
66
const plugin = viteBundleAnalysisPlugin({
7-
output: {},
8-
userOptions: {},
7+
output: {
8+
bundleName: "test",
9+
},
10+
userOptions: {
11+
bundleName: "test",
12+
},
913
});
1014

1115
expect(plugin.version).toEqual("1");

0 commit comments

Comments
 (0)