Skip to main content
--define 标志可以与 bun buildbun build --compile 一起使用,将构建时常量注入到您的应用程序中。这对于将构建版本、时间戳或配置标志等元数据直接嵌入编译后的可执行文件尤其有用。
terminal
bun build --compile --define BUILD_VERSION='"1.2.3"' --define BUILD_TIME='"2024-01-15T10:30:00Z"' src/index.ts --outfile myapp

为什么使用构建时常量?

构建时常量直接嵌入到编译后的代码中,因此它们具有:
  • 零运行时开销 – 不需要环境变量查找或文件读取
  • 不可变 – 值在编译时烘焙进二进制文件
  • 可优化 – 未使用的分支可以被死代码消除
  • 安全 – 无需管理外部依赖或配置文件
这类似于 C/C++ 中的 gcc -D#define,但适用于 JavaScript/TypeScript。

基本用法

使用 bun build

terminal
# 带构建时常量打包
bun build --define BUILD_VERSION='"1.0.0"' --define NODE_ENV='"production"' src/index.ts --outdir ./dist

使用 bun build --compile

terminal
# 带构建时常量编译为可执行文件
bun build --compile --define BUILD_VERSION='"1.0.0"' --define BUILD_TIME='"2024-01-15T10:30:00Z"' src/cli.ts --outfile mycli

JavaScript API

https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79build.ts
await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  define: {
    BUILD_VERSION: '"1.0.0"',
    BUILD_TIME: '"2024-01-15T10:30:00Z"',
    DEBUG: "false",
  },
});

常见用例

版本信息

将版本和构建元数据直接嵌入可执行文件:
// 这些常量在构建时被替换
declare const BUILD_VERSION: string;
declare const BUILD_TIME: string;
declare const GIT_COMMIT: string;

export function getVersion() {
  return {
    version: BUILD_VERSION,
    buildTime: BUILD_TIME,
    commit: GIT_COMMIT,
  };
}

功能开关

使用构建时常量启用或禁用功能:
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79src/version.ts
// 在构建时被替换
declare const ENABLE_ANALYTICS: boolean;
declare const ENABLE_DEBUG: boolean;

function trackEvent(event: string) {
  if (ENABLE_ANALYTICS) {
    // 如果 ENABLE_ANALYTICS 为 false,整个代码块将被移除
    console.log("Tracking:", event);
  }
}

if (ENABLE_DEBUG) {
  console.log("调试模式已启用");
}
# 生产环境构建 - 启用分析,关闭调试
bun build --compile --define ENABLE_ANALYTICS=true --define ENABLE_DEBUG=false src/app.ts --outfile app-prod

# 开发环境构建 - 都启用
bun build --compile --define ENABLE_ANALYTICS=false --define ENABLE_DEBUG=true src/app.ts --outfile app-dev

配置

在构建时替换配置对象:
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79src/version.ts
declare const CONFIG: {
  apiUrl: string;
  timeout: number;
  retries: number;
};

// CONFIG 在构建时被替换为实际对象
const response = await fetch(CONFIG.apiUrl, {
  timeout: CONFIG.timeout,
});
bun build --compile --define 'CONFIG={"apiUrl":"https://api.example.com","timeout":5000,"retries":3}' src/app.ts --outfile app

高级用法

不同环境的构建

为不同环境创建不同的可执行文件:
{
  "scripts": {
    "build:dev": "bun build --compile --define NODE_ENV='\"development\"' --define API_URL='\"http://localhost:3000\"' src/app.ts --outfile app-dev",
    "build:staging": "bun build --compile --define NODE_ENV='\"staging\"' --define API_URL='\"https://staging.example.com\"' src/app.ts --outfile app-staging",
    "build:prod": "bun build --compile --define NODE_ENV='\"production\"' --define API_URL='\"https://api.example.com\"' src/app.ts --outfile app-prod"
  }
}

使用 Shell 命令生成动态值

通过 Shell 命令生成构建时常量:
# 使用 git 获取当前提交号和时间戳
bun build --compile \
  --define BUILD_VERSION="\"$(git describe --tags --always)\"" \
  --define BUILD_TIME="\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"" \
  --define GIT_COMMIT="\"$(git rev-parse HEAD)\"" \
  src/cli.ts --outfile mycli

构建自动化脚本

创建构建脚本自动注入构建元数据:
// build.ts
import { $ } from "bun";

const version = await $`git describe --tags --always`.text();
const buildTime = new Date().toISOString();
const gitCommit = await $`git rev-parse HEAD`.text();

await Bun.build({
  entrypoints: ["./src/cli.ts"],
  outdir: "./dist",
  define: {
    BUILD_VERSION: JSON.stringify(version.trim()),
    BUILD_TIME: JSON.stringify(buildTime),
    GIT_COMMIT: JSON.stringify(gitCommit.trim()),
  },
});

console.log(`构建版本为 ${version.trim()}`);

重要注意事项

值的格式

值必须是有效的 JSON,将被解析并内联为 JavaScript 表达式:
# ✅ 字符串必须用 JSON 引号包裹
--define VERSION='"1.0.0"'

# ✅ 数字是 JSON 字面量
--define PORT=3000

# ✅ 布尔值是 JSON 字面量
--define DEBUG=true

# ✅ 对象和数组(使用单引号包裹 JSON)
--define 'CONFIG={"host":"localhost","port":3000}'

# ✅ 数组也可以
--define 'FEATURES=["auth","billing","analytics"]'

# ❌ 以下写法无效 - 字符串缺少引号
--define VERSION=1.0.0

属性键名

你可以使用属性访问模式作为键,不仅限于简单标识符:
# ✅ 替换 process.env.NODE_ENV 为 "production"
--define 'process.env.NODE_ENV="production"'

# ✅ 替换 process.env.API_KEY 为实际的密钥
--define 'process.env.API_KEY="abc123"'

# ✅ 替换嵌套属性
--define 'window.myApp.version="1.0.0"'

# ✅ 替换数组访问
--define 'process.argv[2]="--production"'
这对于环境变量特别有用:
// 编译前
if (process.env.NODE_ENV === "production") {
  console.log("生产模式");
}

// 使用 --define 'process.env.NODE_ENV="production"' 编译后
if ("production" === "production") {
  console.log("生产模式");
}

// 优化后
console.log("生产模式");

TypeScript 声明

对于 TypeScript 项目,声明常量以避免类型错误:
// types/build-constants.d.ts
declare const BUILD_VERSION: string;
declare const BUILD_TIME: string;
declare const NODE_ENV: "development" | "staging" | "production";
declare const DEBUG: boolean;

跨平台兼容性

针对多个平台构建时,常量的用法相同:
# Linux
bun build --compile --target=bun-linux-x64 --define PLATFORM='"linux"' src/app.ts --outfile app-linux

# macOS
bun build --compile --target=bun-darwin-x64 --define PLATFORM='"darwin"' src/app.ts --outfile app-macos

# Windows
bun build --compile --target=bun-windows-x64 --define PLATFORM='"windows"' src/app.ts --outfile app-windows.exe

相关链接