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>
This commit is contained in:
parent
21ac9e441f
commit
28e40ddc67
2
build.ts
2
build.ts
@ -1,5 +1,6 @@
|
|||||||
import { readdir, readFile, writeFile } from "fs/promises";
|
import { readdir, readFile, writeFile } from "fs/promises";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
|
import { getMacroDefines } from "./scripts/defines.ts";
|
||||||
|
|
||||||
const outdir = "dist";
|
const outdir = "dist";
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ const result = await Bun.build({
|
|||||||
outdir,
|
outdir,
|
||||||
target: "bun",
|
target: "bun",
|
||||||
splitting: true,
|
splitting: true,
|
||||||
|
define: getMacroDefines(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@ -36,7 +36,7 @@
|
|||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "bun run build.ts",
|
"build": "bun run build.ts",
|
||||||
"dev": "bun run src/entrypoints/cli.tsx",
|
"dev": "bun run scripts/dev.ts",
|
||||||
"prepublishOnly": "bun run build",
|
"prepublishOnly": "bun run build",
|
||||||
"lint": "biome lint src/",
|
"lint": "biome lint src/",
|
||||||
"lint:fix": "biome lint --fix src/",
|
"lint:fix": "biome lint --fix src/",
|
||||||
|
|||||||
18
scripts/defines.ts
Normal file
18
scripts/defines.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Shared MACRO define map used by both dev.ts (runtime -d flags)
|
||||||
|
* and build.ts (Bun.build define option).
|
||||||
|
*
|
||||||
|
* Each value is a JSON-stringified expression that replaces the
|
||||||
|
* corresponding MACRO.* identifier at transpile / bundle time.
|
||||||
|
*/
|
||||||
|
export function getMacroDefines(): Record<string, string> {
|
||||||
|
return {
|
||||||
|
"MACRO.VERSION": JSON.stringify("2.1.888"),
|
||||||
|
"MACRO.BUILD_TIME": JSON.stringify(new Date().toISOString()),
|
||||||
|
"MACRO.FEEDBACK_CHANNEL": JSON.stringify(""),
|
||||||
|
"MACRO.ISSUES_EXPLAINER": JSON.stringify(""),
|
||||||
|
"MACRO.NATIVE_PACKAGE_URL": JSON.stringify(""),
|
||||||
|
"MACRO.PACKAGE_URL": JSON.stringify(""),
|
||||||
|
"MACRO.VERSION_CHANGELOG": JSON.stringify(""),
|
||||||
|
};
|
||||||
|
}
|
||||||
21
scripts/dev.ts
Normal file
21
scripts/dev.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bun
|
||||||
|
/**
|
||||||
|
* Dev entrypoint — launches cli.tsx with MACRO.* defines injected
|
||||||
|
* via Bun's -d flag (bunfig.toml [define] doesn't propagate to
|
||||||
|
* dynamically imported modules at runtime).
|
||||||
|
*/
|
||||||
|
import { getMacroDefines } from "./defines.ts";
|
||||||
|
|
||||||
|
const defines = getMacroDefines();
|
||||||
|
|
||||||
|
const defineArgs = Object.entries(defines).flatMap(([k, v]) => [
|
||||||
|
"-d",
|
||||||
|
`${k}:${v}`,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const result = Bun.spawnSync(
|
||||||
|
["bun", "run", ...defineArgs, "src/entrypoints/cli.tsx", ...process.argv.slice(2)],
|
||||||
|
{ stdio: ["inherit", "inherit", "inherit"] },
|
||||||
|
);
|
||||||
|
|
||||||
|
process.exit(result.exitCode ?? 0);
|
||||||
@ -1,21 +1,5 @@
|
|||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
// Runtime polyfill for bun:bundle (build-time macros)
|
import { feature } from 'bun:bundle'
|
||||||
const feature = (_name: string) => false;
|
|
||||||
if (typeof globalThis.MACRO === "undefined") {
|
|
||||||
(globalThis as any).MACRO = {
|
|
||||||
VERSION: "2.1.888",
|
|
||||||
BUILD_TIME: new Date().toISOString(),
|
|
||||||
FEEDBACK_CHANNEL: "",
|
|
||||||
ISSUES_EXPLAINER: "",
|
|
||||||
NATIVE_PACKAGE_URL: "",
|
|
||||||
PACKAGE_URL: "",
|
|
||||||
VERSION_CHANGELOG: "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Build-time constants — normally replaced by Bun bundler at compile time
|
|
||||||
(globalThis as any).BUILD_TARGET = "external";
|
|
||||||
(globalThis as any).BUILD_ENV = "production";
|
|
||||||
(globalThis as any).INTERFACE_TYPE = "stdio";
|
|
||||||
|
|
||||||
// Bugfix for corepack auto-pinning, which adds yarnpkg to peoples' package.jsons
|
// Bugfix for corepack auto-pinning, which adds yarnpkg to peoples' package.jsons
|
||||||
// eslint-disable-next-line custom-rules/no-top-level-side-effects
|
// eslint-disable-next-line custom-rules/no-top-level-side-effects
|
||||||
|
|||||||
12
src/types/global.d.ts
vendored
12
src/types/global.d.ts
vendored
@ -4,9 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// MACRO — Bun compile-time macro function (from bun:bundle)
|
// MACRO — Bun compile-time constants injected via bunfig.toml [define] (dev)
|
||||||
// Expands the function body at build time and removes the call in production.
|
// and Bun.build({ define }) (production). See bunfig.toml & build.ts.
|
||||||
// Also supports property access like MACRO.VERSION (compile-time constants).
|
|
||||||
declare namespace MACRO {
|
declare namespace MACRO {
|
||||||
export const VERSION: string
|
export const VERSION: string
|
||||||
export const BUILD_TIME: string
|
export const BUILD_TIME: string
|
||||||
@ -16,7 +15,6 @@ declare namespace MACRO {
|
|||||||
export const PACKAGE_URL: string
|
export const PACKAGE_URL: string
|
||||||
export const VERSION_CHANGELOG: string
|
export const VERSION_CHANGELOG: string
|
||||||
}
|
}
|
||||||
declare function MACRO<T>(fn: () => T): T
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Internal Anthropic-only identifiers (dead-code eliminated in open-source)
|
// Internal Anthropic-only identifiers (dead-code eliminated in open-source)
|
||||||
@ -62,11 +60,7 @@ declare type T = unknown
|
|||||||
declare function TungstenPill(props?: { key?: string; selected?: boolean }): JSX.Element | null
|
declare function TungstenPill(props?: { key?: string; selected?: boolean }): JSX.Element | null
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Build-time constants — replaced by Bun bundler, polyfilled at runtime
|
// Build-time constants BUILD_TARGET/BUILD_ENV/INTERFACE_TYPE — removed (zero runtime usage)
|
||||||
// Using `string` (not literal types) so comparisons don't produce TS2367
|
|
||||||
declare const BUILD_TARGET: string
|
|
||||||
declare const BUILD_ENV: string
|
|
||||||
declare const INTERFACE_TYPE: string
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Ink custom JSX intrinsic elements — see src/types/ink-jsx.d.ts
|
// Ink custom JSX intrinsic elements — see src/types/ink-jsx.d.ts
|
||||||
|
|||||||
1
src/types/internal-modules.d.ts
vendored
1
src/types/internal-modules.d.ts
vendored
@ -9,7 +9,6 @@
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
declare module "bun:bundle" {
|
declare module "bun:bundle" {
|
||||||
export function feature(name: string): boolean;
|
export function feature(name: string): boolean;
|
||||||
export function MACRO<T>(fn: () => T): T;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module "bun:ffi" {
|
declare module "bun:ffi" {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user