Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create non-bundler plugin #169

Merged
merged 6 commits into from
Sep 16, 2024
Merged

feat: Create non-bundler plugin #169

merged 6 commits into from
Sep 16, 2024

Conversation

suejung-sentry
Copy link
Contributor

@suejung-sentry suejung-sentry commented Sep 9, 2024

Description

This PR expands Codecov's Bundle Analysis product offering to those who operate without a standard bundler (e.g., webpack, rollup) in their production build flow.

It offers 2 options for such users:

  1. A function that can be imported into a JavaScript script and used such as below.
import { createAndUploadReport } from "@codecov/bundle-analyzer";

const buildDirs = ["path/to/build"];
const coreOpts = { dryRun: false, ... };
const bundleAnalyzerOpts = {...};

createAndUploadReport(buildDir, coreOpts, bundleAnalyzerOpts)
  1. A CLI that can be invoked such as below.
npx bundle-analyzer ./dist --bundle-name=test-cli --upload-token=abcd --dry-run

When invoked, the operation walks through the specified file tree(s) and composes a Bundle Stats report in the expected Codecov format. It then calls existing bundler-plugin-core functions to fetch a pre-signed URL then upload the Bundle Stats report to Codecov.

It requires the same coreOptions as bundler-plugin-core and also offers a beforeReportUpload that allows users to overwrite fields in the Output as desired, before upload to Codecov. Other options include ignorePatterns which lets you specify glob pattern files to ignore and normalizeAssetsPattern which lets you specify how Codecov should create a normalized name.

Closes codecov/engineering-team#1652

Notable Changes

  • The code for the new package is all within packages/bundle-analyzer, which is built and published alongside all the existing plugins using unbuild and rollup.
    • There are unit tests with mocks for all the new bundle-analyzer package functionality.
  • Examples of usage are included in examples/bundle-analyzer for both the cli and library-import variants. For library-import there are example tests for cjs and esm environments. All examples have bundle stats reports generated and pushed to Codecov as an end-to-end test.
  • Integration tests are included in integration-tests/fixtures/generate-bundle-stats/bundle-analyzer and integration-tests/test-apps/bundle-analyzer. These run against a mock API that can run in docker.
  • Typedoc - Docs were confirmed to be formatted as expected when running pnpm run generate:typedoc from the monorepo project root (docs get generated at ./typedoc).

Copy link

codecov bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 97.42991% with 11 lines in your changes missing coverage. Please review.

Project coverage is 76.98%. Comparing base (621ecd3) to head (7ca5777).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
packages/bundler-plugin-core/src/utils/index.ts 0.00% 5 Missing ⚠️
packages/bundle-analyzer/src/cli.ts 98.05% 3 Missing ⚠️
packages/bundler-plugin-core/src/index.ts 0.00% 2 Missing ⚠️
packages/bundle-analyzer/src/version.ts 92.85% 1 Missing ⚠️
Additional details and impacted files
Components Coverage Δ
Plugin core 96.70% <50.00%> (-0.15%) ⬇️
Rollup plugin 10.81% <ø> (ø)
Vite plugin 11.02% <ø> (ø)
Webpack plugin 49.88% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codecov-notifications
Copy link

codecov-notifications bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 97.42991% with 11 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files Patch % Lines
packages/bundler-plugin-core/src/utils/index.ts 0.00% 5 Missing ⚠️
packages/bundle-analyzer/src/cli.ts 98.05% 3 Missing ⚠️
packages/bundler-plugin-core/src/index.ts 0.00% 2 Missing ⚠️
packages/bundle-analyzer/src/version.ts 92.85% 1 Missing ⚠️
Components Coverage Δ
Plugin core 96.70% <50.00%> (-0.15%) ⬇️
Rollup plugin 10.81% <ø> (ø)
Vite plugin 11.02% <ø> (ø)
Webpack plugin 49.88% <ø> (ø)

📢 Thoughts on this report? Let us know!

@codecov-staging
Copy link

codecov-staging bot commented Sep 9, 2024

Bundle Report

Changes will increase total bundle size by 448.95kB ⬆️

Bundle name Size Change
@codecov/nuxt-plugin-cjs 1.4kB 1.4kB ⬆️
@codecov/solidstart-plugin-esm 1.09kB 1.09kB ⬆️
@codecov/nuxt-plugin-esm 830 bytes 830 bytes ⬆️
@codecov/bundle-analyzer-cjs 4.33kB 4.33kB ⬆️
@codecov/remix-vite-plugin-cjs 1.31kB 1.31kB ⬆️
@codecov/bundle-analyzer-esm 3.45kB 3.45kB ⬆️
@codecov/example-next-15-app-server-cjs 359.11kB 359.11kB ⬆️
@codecov/remix-vite-plugin-esm 957 bytes 957 bytes ⬆️
@codecov/sveltekit-plugin-esm 891 bytes 891 bytes ⬆️
@codecov/nextjs-webpack-plugin-esm 1.11kB 1.11kB ⬆️
@codecov/bundler-plugin-core-esm 12.29kB 12.29kB ⬆️
@codecov/bundler-plugin-core-cjs 46.63kB 46.63kB ⬆️
@codecov/vite-plugin-cjs 2.79kB 2.79kB ⬆️
@codecov/rollup-plugin-cjs 2.81kB 2.81kB ⬆️
@codecov/vite-plugin-esm 1.24kB 1.24kB ⬆️
@codecov/nextjs-webpack-plugin-cjs 2.1kB 2.1kB ⬆️
@codecov/webpack-plugin-esm 3.36kB 3.36kB ⬆️
@codecov/rollup-plugin-esm 1.3kB 1.3kB ⬆️
@codecov/solidstart-plugin-cjs 1.33kB 1.33kB ⬆️
@codecov/sveltekit-plugin-cjs 1.32kB 1.32kB ⬆️
@codecov/example-next-app-edge-server-array-push (removed) 354 bytes ⬇️
@codecov/example-next-15-app-edge-server-array-push (removed) 356 bytes ⬇️

Copy link

codecov bot commented Sep 9, 2024

Bundle Report

Changes will increase total bundle size by 9.65kB (0.17%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@codecov/bundler-plugin-core-cjs 46.63kB 33 bytes ⬆️
@codecov/bundler-plugin-core-esm 12.29kB 43 bytes ⬆️
@codecov/rollup-plugin-esm 1.3kB 1.02kB ⬇️
@codecov/webpack-plugin-esm 3.36kB 6 bytes ⬆️
@codecov/example-solidstart-app-client-esm 47.8kB 193 bytes ⬇️
@codecov/example-oidc-app-esm 150.57kB 4 bytes ⬆️
@codecov/example-bundle-analyzer-cli 511 bytes 511 bytes ⬆️
@codecov/bundle-analyzer-esm 3.11kB 3.11kB ⬆️
@codecov/bundle-analyzer-cjs 4.33kB 4.33kB ⬆️
@codecov/example-bundle-analyzer-cjs 1.41kB 1.41kB ⬆️
@codecov/example-bundle-analyzer-esm 1.41kB 1.41kB ⬆️

@suejung-sentry suejung-sentry marked this pull request as ready for review September 10, 2024 17:20
Comment on lines 1 to 3
---
"@codecov/bundle-analyzer": minor
---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you include all the other plugins in here, that way everything gets bumped at the same time and stays on the same version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Comment on lines +5 to +19
const coreOpts = {
dryRun: false,
uploadToken: process.env.BUNDLE_ANALYZER_UPLOAD_TOKEN,
retryCount: 3,
apiUrl: "https://api.codecov.io",
bundleName: "@codecov/example-bundle-analyzer-cjs",
enableBundleAnalysis: true,
debug: true,
};

const bundleAnalyzerOpts = {
beforeReportUpload: async (original) => original,
ignorePatterns: ["*.map"],
normalizeAssetsPattern: "[name]-[hash].js",
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future, it would be nice to provide some types here that people can import and use with the satisfies keyword, so they know they're correct fields and types for those fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice. I went through and exported the relevant types in bundle-analyzer package and converted the ESM example to ts so people can see what that'd look like.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Hrrm, iirc these trailing commas are against the JSON spec, can we quickly remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah sorry that was my linter on save autorun (previously had a change in this file since removed). Our eslint/prettier project settings seem to make it so I have to do git commit --no-verify to override the linter and get this to come back to the way matching main 😂. Spent 5min but couldn't get the trailingComma / comma-dangle settings to match main - will try again later!

Comment on lines 9 to 10
> [!WARNING]
> This bundle-analyzer package is subject to change.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this comment, with the move the 1.0.0 this isn't as true as it used to be. This was more of a warning while in alpha/beta that we could break things without notice, which we're not doing anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

pnpm add @codecov/bundle-analyzer --save-dev
```

## Example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update this to be something like Example - Custom Analysis Script and add a heading for the CLI version something like Example - CLI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

@@ -0,0 +1,80 @@
{
"name": "@codecov/bundle-analyzer",
"version": "0.0.1-beta.12",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version i think needs to be set to 1.0.1 so that it aligns with what we have currently. Once this is merged and we prepare a publish it will be bumped to 1.0.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Comment on lines 7 to 12
"keywords": [
"Codecov",
"bundle-analyzer",
"bundler",
"analyzer"
],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add cli, and script to the keywords 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Comment on lines 34 to 74
.options({
"dry-run": {
alias: "d",
type: "boolean",
description: "Perform a dry run without uploading",
default: false,
},
"upload-token": {
alias: "t",
type: "string",
description: "Specify the upload token for authentication",
},
"api-url": {
alias: "u",
type: "string",
description: "Set the API URL",
default: "https://api.codecov.io",
},
"bundle-name": {
alias: "n",
type: "string",
description: "Set the bundle identifier in Codecov",
demandOption: true,
},
debug: {
alias: "v",
type: "boolean",
description: "Enable debug mode for additional logging",
default: false,
},
"ignore-patterns": {
alias: "i",
type: "array",
description: "Specify file patterns to ignore during the analysis",
},
"normalize-assets-pattern": {
alias: "p",
type: "string",
description: "Pattern to normalize asset names, e.g., '[name]-[hash].js'",
},
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry for this, but I totally missed that we need to provide the ability for user to override the upload values. We need to do this in case we don't support the CI environment or they want to customize what is being sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. in my pass I had intentionally only exposed some of the many options in the CLI version to keep it simple.
It's true it'll be a more useful tool if there's not a big disparity in the CLI's exposed functionality.
I added the option of a json config file to throw in all the less common configuration options

@suejung-sentry suejung-sentry merged commit 875be0b into main Sep 16, 2024
63 checks passed
@suejung-sentry suejung-sentry deleted the prerelease branch September 16, 2024 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support non-bundler setups
2 participants