Skip to content

Commit 5c65934

Browse files
author
yourtion
committed
feat: add @codecov/rspack-plugin package
This commit adds the official Codecov Rspack plugin that provides bundle analysis support for Rspack projects. Features: - Bundle analysis and upload to Codecov - Support for GitHub OIDC authentication - Support for tokenless uploads for public repos - Dry run mode for local testing - Comprehensive test coverage
1 parent 866e31a commit 5c65934

File tree

17 files changed

+1058
-0
lines changed

17 files changed

+1058
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2025-01-01
4+
5+
### Added
6+
7+
- Initial release of @codecov/rspack-plugin
8+
- Bundle analysis support for Rspack projects
9+
- Support for uploading bundle analysis to Codecov
10+
- Support for dry run mode
11+
- Support for GitHub OIDC authentication
12+
- Support for tokenless uploads for public repositories

packages/rspack-plugin/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Codecov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# 本地安装测试指南
2+
3+
## 方法一:使用 pnpm link(推荐)
4+
5+
### 1. 在 rspack-plugin 目录创建全局链接
6+
7+
```bash
8+
cd /Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/rspack-plugin
9+
pnpm link --global
10+
```
11+
12+
### 2. 同时链接 bundler-plugin-core(重要!)
13+
14+
```bash
15+
cd /Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/bundler-plugin-core
16+
pnpm link --global
17+
```
18+
19+
### 3. 在目标项目中链接
20+
21+
```bash
22+
cd /path/to/your/test-project
23+
pnpm link --global @codecov/rspack-plugin
24+
pnpm link --global @codecov/bundler-plugin-core
25+
```
26+
27+
## 方法二:使用 file: 协议(需要同时链接两个包)
28+
29+
**注意**:由于 rspack-plugin 依赖 bundler-plugin-core,你需要同时链接两个包。
30+
31+
在你想要测试的项目的 `package.json` 中添加:
32+
33+
```json
34+
{
35+
"devDependencies": {
36+
"@codecov/bundler-plugin-core": "file:/Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/bundler-plugin-core",
37+
"@codecov/rspack-plugin": "file:/Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/rspack-plugin"
38+
}
39+
}
40+
```
41+
42+
然后运行:
43+
44+
```bash
45+
pnpm install
46+
```
47+
48+
## 方法三:在 workspace 中测试(如果是 monorepo)
49+
50+
如果你的测试项目也是这个 monorepo 的一部分,可以在根目录的 `package.json` 中添加:
51+
52+
```json
53+
{
54+
"pnpm": {
55+
"overrides": {
56+
"@codecov/rspack-plugin": "workspace:*"
57+
}
58+
}
59+
}
60+
```
61+
62+
## 验证安装
63+
64+
安装后,你可以运行以下命令验证:
65+
66+
```bash
67+
# 在测试项目中
68+
pnpm list @codecov/rspack-plugin
69+
```
70+
71+
应该会显示类似:
72+
73+
```
74+
@codecov/rspack-plugin 1.0.0 -> symlink:/Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/rspack-plugin
75+
```
76+
77+
## 使用示例
78+
79+
在你的 Rspack 配置文件中:
80+
81+
```javascript
82+
// rspack.config.js
83+
const { codecovRspackPlugin } = require("@codecov/rspack-plugin");
84+
85+
module.exports = {
86+
plugins: [
87+
codecovRspackPlugin({
88+
enableBundleAnalysis: true,
89+
bundleName: "test-bundle",
90+
dryRun: true, // 本地测试建议开启 dryRun
91+
debug: true,
92+
}),
93+
],
94+
};
95+
```
96+
97+
## 注意事项
98+
99+
1. **修改后重新构建**:每次修改 rspack-plugin 的代码后,需要重新构建:
100+
101+
```bash
102+
cd /Users/yourtion/codes/open/codecov-javascript-bundler-plugins/packages/rspack-plugin
103+
pnpm build
104+
```
105+
106+
2. **热重载**:使用 file: 协议链接的包通常不支持热重载,需要重启测试项目的 dev server
107+
108+
3. **调试**:开启 `debug: true``dryRun: true` 选项可以方便本地调试

packages/rspack-plugin/README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<p align="center">
2+
<a href="https://about.codecov.io" target="_blank">
3+
<img src="https://about.codecov.io/wp-content/themes/codecov/assets/brand/sentry-cobranding/logos/codecov-by-sentry-logo.svg" alt="Codecov by Sentry logo" width="280" height="84">
4+
</a>
5+
</p>
6+
7+
# Codecov Rspack Plugin
8+
9+
A Rspack plugin that provides bundle analysis support for Codecov.
10+
11+
> [!NOTE]
12+
> The plugin does not support code coverage, see our [docs](https://docs.codecov.com/docs/quick-start) to set up coverage today!
13+
14+
## Installation
15+
16+
Using npm:
17+
18+
```bash
19+
npm install @codecov/rspack-plugin --save-dev
20+
```
21+
22+
Using yarn:
23+
24+
```bash
25+
yarn add @codecov/rspack-plugin --dev
26+
```
27+
28+
Using pnpm:
29+
30+
```bash
31+
pnpm add @codecov/rspack-plugin --save-dev
32+
```
33+
34+
## Public Repo Example - GitHub Actions
35+
36+
This configuration will automatically upload the bundle analysis to Codecov for public repositories. When an internal PR is created it will use the Codecov token set in your secrets, and if running from a forked PR, it will use the tokenless setting automatically. For setups not using GitHub Actions see the following [example](#public-repo-example---non-github-actions). For private repositories see the following [example](#private-repo-example).
37+
38+
```js
39+
// rspack.config.js
40+
const path = require("path");
41+
const { codecovRspackPlugin } = require("@codecov/rspack-plugin");
42+
43+
module.exports = {
44+
entry: "./src/index.js",
45+
mode: "production",
46+
output: {
47+
filename: "main.js",
48+
path: path.resolve(__dirname, "dist"),
49+
},
50+
plugins: [
51+
// Put the Codecov rspack plugin after all other plugins
52+
codecovRspackPlugin({
53+
enableBundleAnalysis: true,
54+
bundleName: "example-rspack-bundle",
55+
uploadToken: process.env.CODECOV_TOKEN,
56+
gitService: "github",
57+
}),
58+
],
59+
};
60+
```
61+
62+
## Public Repo Example - Non-GitHub Actions
63+
64+
This setup is for public repositories that are not using GitHub Actions, this configuration will automatically upload the bundle analysis to Codecov. You will need to configure it similar to the GitHub Actions example, however you will need to provide a branch override, and ensure that it will pass the correct branch name, and with forks including the fork-owner i.e. `fork-owner:branch`.
65+
66+
```js
67+
// rspack.config.js
68+
const path = require("path");
69+
const { codecovRspackPlugin } = require("@codecov/rspack-plugin");
70+
71+
module.exports = {
72+
entry: "./src/index.js",
73+
mode: "production",
74+
output: {
75+
filename: "main.js",
76+
path: path.resolve(__dirname, "dist"),
77+
},
78+
plugins: [
79+
// Put the Codecov rspack plugin after all other plugins
80+
codecovRspackPlugin({
81+
enableBundleAnalysis: true,
82+
bundleName: "example-rspack-bundle",
83+
uploadToken: process.env.CODECOV_TOKEN,
84+
gitService: "github",
85+
uploadOverrides: {
86+
branch: "<branch value>",
87+
},
88+
}),
89+
],
90+
};
91+
```
92+
93+
## Private Repo Example
94+
95+
This is the required way to use the plugin for private repositories. This configuration will automatically upload the bundle analysis to Codecov.
96+
97+
```js
98+
// rspack.config.js
99+
const path = require("path");
100+
const { codecovRspackPlugin } = require("@codecov/rspack-plugin");
101+
102+
module.exports = {
103+
entry: "./src/index.js",
104+
mode: "production",
105+
output: {
106+
filename: "main.js",
107+
path: path.resolve(__dirname, "dist"),
108+
},
109+
plugins: [
110+
// Put the Codecov rspack plugin after all other plugins
111+
codecovRspackPlugin({
112+
enableBundleAnalysis: true,
113+
bundleName: "example-rspack-bundle",
114+
uploadToken: process.env.CODECOV_TOKEN,
115+
gitService: "github",
116+
}),
117+
],
118+
};
119+
```
120+
121+
## GitHub OIDC
122+
123+
For GitHub Actions users, you can use OIDC instead of a upload token. This is the recommended approach for GitHub Actions.
124+
125+
```js
126+
// rspack.config.js
127+
const path = require("path");
128+
const { codecovRspackPlugin } = require("@codecov/rspack-plugin");
129+
130+
module.exports = {
131+
entry: "./src/index.js",
132+
mode: "production",
133+
output: {
134+
filename: "main.js",
135+
path: path.resolve(__dirname, "dist"),
136+
},
137+
plugins: [
138+
// Put the Codecov rspack plugin after all other plugins
139+
codecovRspackPlugin({
140+
enableBundleAnalysis: true,
141+
bundleName: "example-rspack-bundle",
142+
oidc: {
143+
useGitHubOIDC: true,
144+
},
145+
}),
146+
],
147+
};
148+
```
149+
150+
See the [full documentation](https://docs.codecov.com/docs/bundle-analysis) for more details.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { defineBuildConfig } from "unbuild";
2+
import { codecovRollupPlugin } from "codecovProdRollupPlugin";
3+
import packageJson from "./package.json";
4+
5+
export default defineBuildConfig({
6+
entries: ["./src/index"],
7+
outDir: "dist",
8+
declaration: "compatible",
9+
sourcemap: true,
10+
rollup: {
11+
dts: {
12+
compilerOptions: {
13+
removeComments: false,
14+
},
15+
},
16+
emitCJS: true,
17+
replace: {
18+
preventAssignment: true,
19+
values: {
20+
__PACKAGE_VERSION__: JSON.stringify(packageJson.version),
21+
__PACKAGE_NAME__: JSON.stringify(packageJson.name),
22+
},
23+
},
24+
},
25+
hooks: {
26+
"rollup:options": (_ctx, opts) => {
27+
if (process.env.PLUGIN_CODECOV_TOKEN && Array.isArray(opts.plugins)) {
28+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-floating-promises
29+
opts.plugins = [
30+
// @ts-expect-error - using rollup plugin
31+
...opts.plugins,
32+
// @ts-expect-error - using rollup plugin
33+
codecovRollupPlugin({
34+
enableBundleAnalysis:
35+
typeof process.env.PLUGIN_CODECOV_TOKEN === "string",
36+
bundleName: packageJson.name,
37+
uploadToken: process.env.PLUGIN_CODECOV_TOKEN,
38+
apiUrl: process.env.PLUGIN_CODECOV_API_URL,
39+
}),
40+
];
41+
}
42+
},
43+
},
44+
});

0 commit comments

Comments
 (0)