Skip to content

Commit 28e40dd

Browse files
refactor: 用 Bun 原生 define 替换 cli.tsx 中的 globalThis 注入
- 删除 cli.tsx 顶部的 globalThis.MACRO / BUILD_* / feature polyfill - 新增 scripts/defines.ts 作为 MACRO define 映射的单一来源 - 新增 scripts/dev.ts,通过 bun run -d 在转译时注入 MACRO 常量 - build.ts 引用 getMacroDefines() 实现构建时内联 - 清理 global.d.ts (移除 BUILD_*, MACRO 函数声明) - 55 个 MACRO 消费文件零改动 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 21ac9e4 commit 28e40dd

7 files changed

Lines changed: 46 additions & 28 deletions

File tree

build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readdir, readFile, writeFile } from "fs/promises";
22
import { join } from "path";
3+
import { getMacroDefines } from "./scripts/defines.ts";
34

45
const outdir = "dist";
56

@@ -13,6 +14,7 @@ const result = await Bun.build({
1314
outdir,
1415
target: "bun",
1516
splitting: true,
17+
define: getMacroDefines(),
1618
});
1719

1820
if (!result.success) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
],
3737
"scripts": {
3838
"build": "bun run build.ts",
39-
"dev": "bun run src/entrypoints/cli.tsx",
39+
"dev": "bun run scripts/dev.ts",
4040
"prepublishOnly": "bun run build",
4141
"lint": "biome lint src/",
4242
"lint:fix": "biome lint --fix src/",

scripts/defines.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Shared MACRO define map used by both dev.ts (runtime -d flags)
3+
* and build.ts (Bun.build define option).
4+
*
5+
* Each value is a JSON-stringified expression that replaces the
6+
* corresponding MACRO.* identifier at transpile / bundle time.
7+
*/
8+
export function getMacroDefines(): Record<string, string> {
9+
return {
10+
"MACRO.VERSION": JSON.stringify("2.1.888"),
11+
"MACRO.BUILD_TIME": JSON.stringify(new Date().toISOString()),
12+
"MACRO.FEEDBACK_CHANNEL": JSON.stringify(""),
13+
"MACRO.ISSUES_EXPLAINER": JSON.stringify(""),
14+
"MACRO.NATIVE_PACKAGE_URL": JSON.stringify(""),
15+
"MACRO.PACKAGE_URL": JSON.stringify(""),
16+
"MACRO.VERSION_CHANGELOG": JSON.stringify(""),
17+
};
18+
}

scripts/dev.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bun
2+
/**
3+
* Dev entrypoint — launches cli.tsx with MACRO.* defines injected
4+
* via Bun's -d flag (bunfig.toml [define] doesn't propagate to
5+
* dynamically imported modules at runtime).
6+
*/
7+
import { getMacroDefines } from "./defines.ts";
8+
9+
const defines = getMacroDefines();
10+
11+
const defineArgs = Object.entries(defines).flatMap(([k, v]) => [
12+
"-d",
13+
`${k}:${v}`,
14+
]);
15+
16+
const result = Bun.spawnSync(
17+
["bun", "run", ...defineArgs, "src/entrypoints/cli.tsx", ...process.argv.slice(2)],
18+
{ stdio: ["inherit", "inherit", "inherit"] },
19+
);
20+
21+
process.exit(result.exitCode ?? 0);

src/entrypoints/cli.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
#!/usr/bin/env bun
2-
// Runtime polyfill for bun:bundle (build-time macros)
3-
const feature = (_name: string) => false;
4-
if (typeof globalThis.MACRO === "undefined") {
5-
(globalThis as any).MACRO = {
6-
VERSION: "2.1.888",
7-
BUILD_TIME: new Date().toISOString(),
8-
FEEDBACK_CHANNEL: "",
9-
ISSUES_EXPLAINER: "",
10-
NATIVE_PACKAGE_URL: "",
11-
PACKAGE_URL: "",
12-
VERSION_CHANGELOG: "",
13-
};
14-
}
15-
// Build-time constants — normally replaced by Bun bundler at compile time
16-
(globalThis as any).BUILD_TARGET = "external";
17-
(globalThis as any).BUILD_ENV = "production";
18-
(globalThis as any).INTERFACE_TYPE = "stdio";
2+
import { feature } from 'bun:bundle'
193

204
// Bugfix for corepack auto-pinning, which adds yarnpkg to peoples' package.jsons
215
// eslint-disable-next-line custom-rules/no-top-level-side-effects

src/types/global.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
*/
55

66
// ============================================================================
7-
// MACRO — Bun compile-time macro function (from bun:bundle)
8-
// Expands the function body at build time and removes the call in production.
9-
// Also supports property access like MACRO.VERSION (compile-time constants).
7+
// MACRO — Bun compile-time constants injected via bunfig.toml [define] (dev)
8+
// and Bun.build({ define }) (production). See bunfig.toml & build.ts.
109
declare namespace MACRO {
1110
export const VERSION: string
1211
export const BUILD_TIME: string
@@ -16,7 +15,6 @@ declare namespace MACRO {
1615
export const PACKAGE_URL: string
1716
export const VERSION_CHANGELOG: string
1817
}
19-
declare function MACRO<T>(fn: () => T): T
2018

2119
// ============================================================================
2220
// Internal Anthropic-only identifiers (dead-code eliminated in open-source)
@@ -62,11 +60,7 @@ declare type T = unknown
6260
declare function TungstenPill(props?: { key?: string; selected?: boolean }): JSX.Element | null
6361

6462
// ============================================================================
65-
// Build-time constants — replaced by Bun bundler, polyfilled at runtime
66-
// Using `string` (not literal types) so comparisons don't produce TS2367
67-
declare const BUILD_TARGET: string
68-
declare const BUILD_ENV: string
69-
declare const INTERFACE_TYPE: string
63+
// Build-time constants BUILD_TARGET/BUILD_ENV/INTERFACE_TYPE — removed (zero runtime usage)
7064

7165
// ============================================================================
7266
// Ink custom JSX intrinsic elements — see src/types/ink-jsx.d.ts

src/types/internal-modules.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// ============================================================================
1010
declare module "bun:bundle" {
1111
export function feature(name: string): boolean;
12-
export function MACRO<T>(fn: () => T): T;
1312
}
1413

1514
declare module "bun:ffi" {

0 commit comments

Comments
 (0)