diff --git a/integration-tests/fixtures/generate-bundle-stats/rollup-plugin.test.ts b/integration-tests/fixtures/generate-bundle-stats/rollup-plugin.test.ts index fdc074d4..0a970927 100644 --- a/integration-tests/fixtures/generate-bundle-stats/rollup-plugin.test.ts +++ b/integration-tests/fixtures/generate-bundle-stats/rollup-plugin.test.ts @@ -52,7 +52,11 @@ describe("Generating rollup stats", () => { // eslint-disable-next-line @typescript-eslint/no-unsafe-call resolve(), commonjs(), - codecovRollupPlugin({ enableBundleAnalysis: true, dryRun: true }), + codecovRollupPlugin({ + enableBundleAnalysis: true, + dryRun: true, + bundleName: "rollup-test", + }), ], }).then((bundle) => bundle.write({ @@ -89,4 +93,8 @@ describe("Generating rollup stats", () => { it("sets the correct bundler information", () => { expect(stats.bundler).toStrictEqual(expectedStats.bundler); }); + + it("sets the correct bundle name", () => { + expect(stats.bundleName).toStrictEqual("rollup-test-es"); + }); }); diff --git a/integration-tests/fixtures/generate-bundle-stats/vite-plugin.test.ts b/integration-tests/fixtures/generate-bundle-stats/vite-plugin.test.ts index 49960a10..7f6c7bc0 100644 --- a/integration-tests/fixtures/generate-bundle-stats/vite-plugin.test.ts +++ b/integration-tests/fixtures/generate-bundle-stats/vite-plugin.test.ts @@ -56,7 +56,11 @@ describe("Generating vite stats", () => { }, }, plugins: [ - codecovVitePlugin({ enableBundleAnalysis: true, dryRun: true }), + codecovVitePlugin({ + enableBundleAnalysis: true, + dryRun: true, + bundleName: "vite-test", + }), ], }); @@ -88,4 +92,8 @@ describe("Generating vite stats", () => { it("sets the correct bundler information", () => { expect(stats.bundler).toStrictEqual(expectedStats.bundler); }); + + it("sets the correct bundle name", () => { + expect(stats.bundleName).toStrictEqual("vite-test-cjs"); + }); }); diff --git a/integration-tests/fixtures/generate-bundle-stats/webpack-plugin.test.ts b/integration-tests/fixtures/generate-bundle-stats/webpack-plugin.test.ts index 76c94873..c23b9b78 100644 --- a/integration-tests/fixtures/generate-bundle-stats/webpack-plugin.test.ts +++ b/integration-tests/fixtures/generate-bundle-stats/webpack-plugin.test.ts @@ -54,7 +54,11 @@ describe("Generating webpack stats", () => { }, mode: "production", plugins: [ - codecovWebpackPlugin({ enableBundleAnalysis: true, dryRun: true }), + codecovWebpackPlugin({ + enableBundleAnalysis: true, + dryRun: true, + bundleName: "webpack-test", + }), ], }, (err) => { @@ -95,4 +99,8 @@ describe("Generating webpack stats", () => { it("sets the correct bundler information", () => { expect(stats.bundler).toStrictEqual(expectedStats.bundler); }); + + it("sets the correct bundle name", () => { + expect(stats.bundleName).toStrictEqual("webpack-test-array-push"); + }); }); diff --git a/packages/bundler-plugin-core/src/bundle-analysis/__tests__/bundleAnalysisPluginFactory.test.ts b/packages/bundler-plugin-core/src/bundle-analysis/__tests__/bundleAnalysisPluginFactory.test.ts index 8bf70d2a..4d2474b7 100644 --- a/packages/bundler-plugin-core/src/bundle-analysis/__tests__/bundleAnalysisPluginFactory.test.ts +++ b/packages/bundler-plugin-core/src/bundle-analysis/__tests__/bundleAnalysisPluginFactory.test.ts @@ -3,7 +3,7 @@ import { bundleAnalysisPluginFactory } from "../bundleAnalysisPluginFactory"; describe("bundleAnalysisPluginFactory", () => { it("returns a build start function", () => { const plugin = bundleAnalysisPluginFactory({ - userOptions: {}, + userOptions: { bundleName: "test" }, bundleAnalysisUploadPlugin: () => ({ version: "1", name: "plugin-name", @@ -16,7 +16,7 @@ describe("bundleAnalysisPluginFactory", () => { it("returns a build end function", () => { const plugin = bundleAnalysisPluginFactory({ - userOptions: {}, + userOptions: { bundleName: "test" }, bundleAnalysisUploadPlugin: () => ({ version: "1", name: "plugin-name", @@ -29,7 +29,7 @@ describe("bundleAnalysisPluginFactory", () => { it("returns a write bundle function", () => { const plugin = bundleAnalysisPluginFactory({ - userOptions: {}, + userOptions: { bundleName: "test" }, bundleAnalysisUploadPlugin: () => ({ version: "1", name: "plugin-name", diff --git a/packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts b/packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts index eb5dff2c..5d9880b8 100644 --- a/packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts +++ b/packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts @@ -19,9 +19,9 @@ export const bundleAnalysisPluginFactory = ({ userOptions, bundleAnalysisUploadPlugin, }: BundleAnalysisUploadPluginArgs): UnpluginOptions => { - // const dryRun = userOptions?.dryRun ?? false; const output: Output = { version: "1", + bundleName: userOptions.bundleName ?? "", }; const { pluginVersion, version, ...pluginOpts } = bundleAnalysisUploadPlugin({ @@ -47,6 +47,9 @@ export const bundleAnalysisPluginFactory = ({ // don't need to do anything here if dryRun is true if (userOptions?.dryRun) return; + // don't need to do anything if the bundle name is not present or empty + if (!userOptions.bundleName || userOptions.bundleName === "") return; + const args: UploadOverrides = userOptions.uploaderOverrides ?? {}; const envs = process.env; const inputs: ProviderUtilInputs = { envs, args }; diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 120b7525..56415ab8 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -21,7 +21,7 @@ interface CodecovUnpluginFactoryOptions { bundleAnalysisUploadPlugin: BundleAnalysisUploadPlugin; } -export function codecovUnpluginFactory({ +function codecovUnpluginFactory({ bundleAnalysisUploadPlugin, }: CodecovUnpluginFactoryOptions) { return createUnplugin((userOptions, unpluginMetaContext) => { @@ -48,6 +48,8 @@ export function codecovUnpluginFactory({ }); } +export { red, codecovUnpluginFactory }; + export type { BundleAnalysisUploadPlugin, Asset, diff --git a/packages/bundler-plugin-core/src/types.ts b/packages/bundler-plugin-core/src/types.ts index a369977e..e3e005e1 100644 --- a/packages/bundler-plugin-core/src/types.ts +++ b/packages/bundler-plugin-core/src/types.ts @@ -28,6 +28,7 @@ export interface Module { export interface Output { version?: string; + bundleName: string; bundler?: { name: string; version: string; @@ -92,6 +93,15 @@ export interface Options { * Defaults to `false` */ dryRun?: boolean; + + /** + * The name for the bundle being built. + * + * Required for uploading bundle analysis information. + * + * Example: `rollup-package` + */ + bundleName?: string; } export type BundleAnalysisUploadPlugin = ( diff --git a/packages/rollup-plugin/src/rollup-bundle-analysis/__tests__/rollupBundleAnalysisPlugin.test.ts b/packages/rollup-plugin/src/rollup-bundle-analysis/__tests__/rollupBundleAnalysisPlugin.test.ts index b746299b..c2ac3b5c 100644 --- a/packages/rollup-plugin/src/rollup-bundle-analysis/__tests__/rollupBundleAnalysisPlugin.test.ts +++ b/packages/rollup-plugin/src/rollup-bundle-analysis/__tests__/rollupBundleAnalysisPlugin.test.ts @@ -4,8 +4,12 @@ describe("rollupBundleAnalysisPlugin", () => { describe("when called", () => { it("returns a plugin object", () => { const plugin = rollupBundleAnalysisPlugin({ - output: {}, - userOptions: {}, + output: { + bundleName: "test", + }, + userOptions: { + bundleName: "test", + }, }); expect(plugin.version).toEqual("1"); diff --git a/packages/rollup-plugin/src/rollup-bundle-analysis/rollupBundleAnalysisPlugin.ts b/packages/rollup-plugin/src/rollup-bundle-analysis/rollupBundleAnalysisPlugin.ts index ab4eef48..4e9a351b 100644 --- a/packages/rollup-plugin/src/rollup-bundle-analysis/rollupBundleAnalysisPlugin.ts +++ b/packages/rollup-plugin/src/rollup-bundle-analysis/rollupBundleAnalysisPlugin.ts @@ -4,6 +4,7 @@ import { type Chunk, type Module, type BundleAnalysisUploadPlugin, + red, } from "@codecov/bundler-plugin-core"; const PLUGIN_NAME = "codecov-rollup-bundle-analysis-plugin"; @@ -17,6 +18,19 @@ export const rollupBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({ pluginVersion: "1.0.0", rollup: { generateBundle(this, options, bundle) { + // don't need to do anything if the bundle name is not present or empty + if (!userOptions.bundleName || userOptions.bundleName === "") { + red("Bundle name is not present or empty. Skipping upload."); + return; + } + + // append bundle output format to bundle name + output.bundleName = `${userOptions.bundleName}-${options.format}`; + + if (options.name && options.name !== "") { + output.bundleName = `${userOptions.bundleName}-${options.name}`; + } + const customOptions = { moduleOriginalSize: false, ...options, diff --git a/packages/vite-plugin/src/vite-bundle-analysis/__tests__/viteBundleAnalysisPlugin.test.ts b/packages/vite-plugin/src/vite-bundle-analysis/__tests__/viteBundleAnalysisPlugin.test.ts index 4331948d..1590ec34 100644 --- a/packages/vite-plugin/src/vite-bundle-analysis/__tests__/viteBundleAnalysisPlugin.test.ts +++ b/packages/vite-plugin/src/vite-bundle-analysis/__tests__/viteBundleAnalysisPlugin.test.ts @@ -4,8 +4,12 @@ describe("viteBundleAnalysisPlugin", () => { describe("when called", () => { it("returns a plugin object", () => { const plugin = viteBundleAnalysisPlugin({ - output: {}, - userOptions: {}, + output: { + bundleName: "test", + }, + userOptions: { + bundleName: "test", + }, }); expect(plugin.version).toEqual("1"); diff --git a/packages/vite-plugin/src/vite-bundle-analysis/viteBundleAnalysisPlugin.ts b/packages/vite-plugin/src/vite-bundle-analysis/viteBundleAnalysisPlugin.ts index f26d102d..f5ac4d6a 100644 --- a/packages/vite-plugin/src/vite-bundle-analysis/viteBundleAnalysisPlugin.ts +++ b/packages/vite-plugin/src/vite-bundle-analysis/viteBundleAnalysisPlugin.ts @@ -4,6 +4,7 @@ import { type Chunk, type Module, type BundleAnalysisUploadPlugin, + red, } from "@codecov/bundler-plugin-core"; const PLUGIN_NAME = "codecov-vite-bundle-analysis-plugin"; @@ -17,6 +18,20 @@ export const viteBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({ pluginVersion: "1.0.0", vite: { generateBundle(this, options, bundle) { + // don't need to do anything if the bundle name is not present or empty + if (!userOptions.bundleName || userOptions.bundleName === "") { + red("Bundle name is not present or empty. Skipping upload."); + return; + } + + // append bundle output format to bundle name + output.bundleName = `${userOptions.bundleName}-${options.format}`; + + // add in bundle name if present + if (options.name && options.name !== "") { + output.bundleName = `${userOptions.bundleName}-${options.name}`; + } + const customOptions = { moduleOriginalSize: false, ...options, diff --git a/packages/webpack-plugin/src/webpack-bundle-analysis/__tests__/webpackBundleAnalysisPlugin.test.ts b/packages/webpack-plugin/src/webpack-bundle-analysis/__tests__/webpackBundleAnalysisPlugin.test.ts index 06d2118a..619a6d83 100644 --- a/packages/webpack-plugin/src/webpack-bundle-analysis/__tests__/webpackBundleAnalysisPlugin.test.ts +++ b/packages/webpack-plugin/src/webpack-bundle-analysis/__tests__/webpackBundleAnalysisPlugin.test.ts @@ -4,8 +4,12 @@ describe("webpackBundleAnalysisPlugin", () => { describe("when called", () => { it("returns a plugin object", () => { const plugin = webpackBundleAnalysisPlugin({ - output: {}, - userOptions: {}, + output: { + bundleName: "test", + }, + userOptions: { + bundleName: "test", + }, }); expect(plugin.version).toEqual("1"); diff --git a/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts b/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts index f529f8ca..7ce1c4c1 100644 --- a/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts +++ b/packages/webpack-plugin/src/webpack-bundle-analysis/webpackBundleAnalysisPlugin.ts @@ -1,4 +1,7 @@ -import { type BundleAnalysisUploadPlugin } from "@codecov/bundler-plugin-core"; +import { + type BundleAnalysisUploadPlugin, + red, +} from "@codecov/bundler-plugin-core"; import * as webpack4or5 from "webpack"; const PLUGIN_NAME = "codecov-webpack-bundle-analysis-plugin"; @@ -18,6 +21,20 @@ export const webpackBundleAnalysisPlugin: BundleAnalysisUploadPlugin = ({ stage: webpack4or5.Compilation.PROCESS_ASSETS_STAGE_REPORT, }, () => { + // don't need to do anything if the bundle name is not present or empty + if (!userOptions.bundleName || userOptions.bundleName === "") { + red("Bundle name is not present or empty. Skipping upload."); + return; + } + + if (typeof compilation.outputOptions.chunkFormat === "string") { + output.bundleName = `${userOptions.bundleName}-${compilation.outputOptions.chunkFormat}`; + } + + if (compilation.name && compilation.name !== "") { + output.bundleName = `${userOptions.bundleName}-${compilation.name}`; + } + const compilationStats = compilation.getStats().toJson({ assets: true, chunks: true,