Skip to content

Commit 44be1ec

Browse files
committed
feat: prepend a header to each console log
Add a preprocessing build step that prepends `[Ghostbird]` to each log output. Also, strip all `console.log()` calls in release builds.
1 parent 0836ca7 commit 44be1ec

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

doc/building.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ subgraph "tsdown"
2121
test_sanity[[test_sanity.ts]]
2222
tsc[[typecheck_with_tsc.ts]]
2323
rolldown[["Rolldown<br>(tsdown built-in)"]]
24-
tsdown_builtin[["copy plugin<br>(tsdown built-in)"]]
24+
replace_plugin[["replace plugin<br>(Rolldown built-in)"]]
25+
copy[["copy plugin<br>(tsdown built-in)"]]
2526
generate_locale_messages[[generate_locale_messages.ts]]
2627
generate_manifest[[generate_manifest.ts]]
2728
codecov[[codecov.ts]]
@@ -31,9 +32,9 @@ index_ts --o barrelsby
3132
index_ts --> ts
3233
ts --> test_sanity & tsc -.-> fail((build failure))
3334
tsc --> tsc_cache["tsc cache<br>build/"]
34-
ts --> rolldown --> js["Bundled js files<br>dist/ext/js/*.js"]
35+
ts --> rolldown & replace_plugin --> js["Bundled js files<br>dist/ext/js/*.js"]
3536
ts --> codecov -.-> codecovio((Web service<br>codecov.io))
36-
ext_files --> tsdown_builtin --> copied_ext_files["Assets<br>dist/ext/*"]
37+
ext_files --> copy --> copied_ext_files["Assets<br>dist/ext/*"]
3738
locales --> generate_locale_messages --> messages["Localized messages<br>dist/ext/_locales/*/messages.json"]
3839
manifest_template & version --> generate_manifest --> manifest["MailExtension Manifest<br>dist/ext/manifest.json"]
3940
@@ -43,14 +44,15 @@ js & copied_ext_files & messages & manifest --> web_ext[["web-ext"]] --> xpi["Ma
4344
## Overview
4445

4546
- Running `yarn build-js` starts [the `tsdown` tool](https://tsdown.dev/), which performs several tasks.
46-
- Generating `index.ts` (barrel exports).
47-
- Running sanity tests.
48-
- Performing a type check.
49-
- Generating `manifest.json` from the template file.
50-
- Generating localized `messages.json` files.
51-
- Adjusting source maps.
52-
- Copying static assets from `ext/` into `dist/ext/`.
53-
- Compiling bundled JavaScript files into `dist/ext/`.
47+
- Generating `index.ts` (barrel exports).
48+
- Running sanity tests.
49+
- Performing a type check.
50+
- Generating `manifest.json` from the template file.
51+
- Generating localized `messages.json` files.
52+
- Preprocessing source files.
53+
- Adjusting source maps.
54+
- Copying static assets from `ext/` into `dist/ext/`.
55+
- Compiling bundled JavaScript files into `dist/ext/`.
5456
- Running `yarn build-xpi` creates a zip archive named `dist/ghostbird-{version}.xpi`.
5557
- [The `web-ext` tool](https://github.com/mozilla/web-ext#web-ext) does the actual work.
5658
- The usual `yarn build`, which is used by the CI, runs both of the above after running the linter and tests.
@@ -67,11 +69,20 @@ js & copied_ext_files & messages & manifest --> web_ext[["web-ext"]] --> xpi["Ma
6769

6870
### Output differences
6971

70-
The output of development and release builds is identical except for the source maps and version number calculation.
72+
The outputs of development and release builds are different in a few ways:
73+
74+
#### Source maps
7175

7276
- In a development build, source maps have absolute paths to source files to make debuggers happy.
7377
- In a release build, source maps have relative paths to source files that share a common parent directory so that they look organized in the Thunderbird debugger.
7478

79+
#### Log output
80+
81+
- In a release build, `console.log()` are removed.
82+
- In a development build, `console.log()` are left in place.
83+
84+
Other log levels like `console.info` are not removed in either case.
85+
7586
## Rollup plugins
7687

7788
The build script uses several custom plugins, each serving a different purpose:
@@ -87,6 +98,13 @@ The build script uses several custom plugins, each serving a different purpose:
8798
1. [`tools/codecov.ts`](../tools/codecov.ts) uploads bundle size information to Codecov for analysis.
8899
- This is only active during a release build and an API key is available.
89100

101+
### Built-in plugins
102+
103+
It also uses some functionality that come with `tsdown` or `rolldown`:
104+
105+
1. The `copy` option copies non-TypeScript files from `ext/` to `dist/ext/`.
106+
2. The `replacePlugin` is used to modify logging.
107+
90108
### Version number calculation in `generate_manifest.ts`
91109

92110
The version number included in `manifest.json` is calculated in `generate_manifest.ts`.

tools/tsdown_config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import { existsSync } from "node:fs"
2525
import { join } from "node:path"
2626
import { env } from "node:process"
27+
import { replacePlugin } from "rolldown/experimental"
2728
import type { Options, UserConfig } from "tsdown"
2829
import { barrelsby } from "./barrelsby"
2930
import { codecov } from "./codecov"
@@ -66,6 +67,21 @@ const commonConfig = {
6667
intro: "'use strict';",
6768
sourcemapPathTransform: isRelease ? makeRelativeSourcePath : makeAbsoluteSourcePath,
6869
chunkFileNames,
70+
plugins: [
71+
// Adjust console logs
72+
replacePlugin(
73+
{
74+
"console.log(": isRelease ? ";(" : 'console.log("[Ghostbird]",',
75+
"console.debug(": 'console.debug("[Ghostbird]",',
76+
"console.info(": 'console.info("[Ghostbird]",',
77+
"console.warn(": 'console.warn("[Ghostbird]",',
78+
"console.error(": 'console.error("[Ghostbird]",',
79+
},
80+
{
81+
delimiters: ["\\b", '(?!"\\[Ghostbird\\])'],
82+
},
83+
),
84+
],
6985
advancedChunks: {
7086
groups: [
7187
// Separate library files

0 commit comments

Comments
 (0)