Skip to content

Commit 7ea1578

Browse files
committed
build: add codecov plugin to tsdown
1 parent 7a456b6 commit 7ea1578

5 files changed

Lines changed: 308 additions & 4 deletions

File tree

doc/building.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Building Ghostbird
22

3-
[`tools/tsdown_config.ts`](../tools/tsdown_config.ts) serves as the build script of Ghostbird. It makes heavy use of custom plugins, which would have resulted in very long comments, so I felt they deserved separate documentation.
3+
[`tools/tsdown_config.ts`](../tools/tsdown_config.ts), which is a configuration file for tsdown, serves as the build script of Ghostbird. It makes heavy use of custom plugins, which would have resulted in very long comments, so I felt they deserved separate documentation.
4+
5+
[tsdown](https://tsdown.dev) is a descendant of the Rollup bundler that combines [Rolldown](https://rolldown.rs/) and [TypeScript](https://www.typescriptlang.org/) to compile TypeScript code into JavaScript.
46

57
## Inputs and Outputs
68

@@ -22,13 +24,15 @@ subgraph "tsdown"
2224
tsdown_builtin[["copy plugin<br>(tsdown built-in)"]]
2325
generate_locale_messages[[generate_locale_messages.ts]]
2426
generate_manifest[[generate_manifest.ts]]
27+
codecov[[codecov.ts]]
2528
end
2629
2730
index_ts --o barrelsby
2831
index_ts --> ts
2932
ts --> test_sanity & tsc --> fail((build failure))
3033
tsc --> tsc_cache["tsc cache<br>build/"]
3134
ts --> rolldown --> js["Bundled js files<br>dist/ext/js/*.js"]
35+
js --> codecov
3236
ext_files --> tsdown_builtin --> copied_ext_files["Assets<br>dist/ext/*"]
3337
locales --> generate_locale_messages --> messages["Localized messages<br>dist/ext/_locales/*/messages.json"]
3438
manifest_template & version --> generate_manifest --> manifest["MailExtension Manifest<br>dist/ext/manifest.json"]
@@ -80,6 +84,8 @@ The build script uses several custom plugins, each serving a different purpose:
8084
- This is skipped in a release build since a full `yarn check-type` is run as a pre-build step.
8185
1. [`tools/generate_manifest.ts`](../tools/generate_manifest.ts) prepares `manifest.json` by filling in placeholders in [`manifest_template.json`](../manifest_template.json).
8286
1. [`tools/generate_locale_messages.ts`](../tools/generate_locale_messages.ts) generates [message files](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization) from the definition in [`locales.toml`](../locales.toml).
87+
1. [`tools/codecov.ts`](../tools/codecov.ts) uploads bundle size information to Codecov for analysis.
88+
- This is only active during a release build and an API key is available.
8389

8490
### Version number calculation in `generate_manifest.ts`
8591

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
},
7070
"devDependencies": {
7171
"@biomejs/biome": "^2.2.5",
72+
"@codecov/rollup-plugin": "^1.9.1",
7273
"@types/node": "^24.3.0",
7374
"@types/thunderbird-webext-browser": "^127.0.0",
7475
"@vitest/coverage-v8": "^3.2.4",

tools/codecov.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Plugin } from "rolldown"
2+
3+
export type CodecovOptions = {
4+
/** Name of the bundle */
5+
bundleName: string
6+
/** Environment variables which may contain the Codecov API token */
7+
env: Record<string, string | undefined>
8+
}
9+
10+
/** Uploads bundle size information to Codecov if the API token is available in the environment */
11+
export async function codecov({ bundleName, env: { CODECOV_TOKEN: uploadToken } }: CodecovOptions): Promise<Plugin[]> {
12+
if (!bundleName || !uploadToken) {
13+
return []
14+
}
15+
16+
// lazy load the plugin to avoid loading it except when needed
17+
let { codecovRollupPlugin } = await import("@codecov/rollup-plugin")
18+
19+
return codecovRollupPlugin({
20+
enableBundleAnalysis: true,
21+
bundleName,
22+
uploadToken,
23+
})
24+
}

tools/tsdown_config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { join } from "node:path"
2626
import { env } from "node:process"
2727
import type { Options, UserConfig } from "tsdown"
2828
import { barrelsby } from "./barrelsby"
29+
import { codecov } from "./codecov"
2930
import { generateLocaleMessages } from "./generate_locale_messages"
3031
import { generateManifest } from "./generate_manifest"
3132
import { testSanity } from "./test_sanity"
@@ -99,6 +100,8 @@ const esmConfig = {
99100
generateManifest({ env: isRelease ? (env as Record<string, string>) : {} }),
100101
// Generate _locales/*/messages.json
101102
generateLocaleMessages({ path: "./locales.toml" }),
103+
// Upload bundle size info to Codecov
104+
isRelease && codecov({ env, bundleName: "@ghostbird/background-and-options.js" }),
102105
],
103106
} satisfies Options
104107

@@ -110,6 +113,10 @@ const iifeConfig = {
110113
"src/root/compose.ts",
111114
],
112115
format: "iife",
116+
plugins: [
117+
// Upload bundle size info to Codecov
118+
isRelease && codecov({ env, bundleName: "@ghostbird/compose.js" }),
119+
],
113120
} satisfies Options
114121

115122
// Check that the keys are disjoint

0 commit comments

Comments
 (0)