> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 --define 的构建时常量

将 `--define` 传递给 `bun build` 或 `bun build --compile` 以将构建时常量注入到你的应用程序中。用它来将构建版本、时间戳或配置标志等元数据直接嵌入到编译后的可执行文件中。

```sh terminal icon="terminal" theme={null}
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`

```sh terminal icon="terminal" theme={null}
# 使用构建时常量打包
bun build --define BUILD_VERSION='"1.0.0"' --define NODE_ENV='"production"' src/index.ts --outdir ./dist
```

### 使用 `bun build --compile`

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

### JavaScript API

```ts build.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
await Bun.build({
  entrypoints: ["./src/index.ts"],
  outdir: "./dist",
  define: {
    BUILD_VERSION: '"1.0.0"',
    BUILD_TIME: '"2024-01-15T10:30:00Z"',
    DEBUG: "false",
  },
});
```

***

## 常见用例

### 版本信息

将版本和构建元数据直接嵌入到可执行文件中：

<CodeGroup>
  ```ts src/version.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
  // 这些常量在构建时被替换
  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,
    };
  }
  ```

  ```sh Build command theme={null}
  bun build --compile \
    --define BUILD_VERSION='"1.2.3"' \
    --define BUILD_TIME='"2024-01-15T10:30:00Z"' \
    --define GIT_COMMIT='"abc123"' \
    src/cli.ts --outfile mycli
  ```
</CodeGroup>

### 功能开关

使用构建时常量来启用/禁用功能：

```ts src/version.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
// 在构建时替换
declare const ENABLE_ANALYTICS: boolean;
declare const ENABLE_DEBUG: boolean;

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

if (ENABLE_DEBUG) {
  console.log("调试模式已启用");
}
```

```sh theme={null}
# 生产构建 - 分析启用，调试禁用
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
```

### 配置

在构建时替换配置对象：

```ts src/version.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
declare const CONFIG: {
  apiUrl: string;
  timeout: number;
  retries: number;
};

// CONFIG 在构建时被替换为实际对象
const response = await fetch(CONFIG.apiUrl, {
  timeout: CONFIG.timeout,
});
```

```sh theme={null}
bun build --compile --define 'CONFIG={"apiUrl":"https://api.example.com","timeout":5000,"retries":3}' src/app.ts --outfile app
```

***

## 高级模式

### 环境特定构建

为不同环境创建不同的可执行文件：

```json theme={null}
{
  "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 命令生成构建时常量：

```sh theme={null}
# 使用 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
```

### 构建自动化脚本

创建一个注入构建元数据的构建脚本：

```ts theme={null}
// 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。Bun 解析每个值并将其内联为 JavaScript 表达式：

```sh theme={null}
# ✅ 字符串必须用 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
```

### 属性键

键可以是属性访问模式，而不仅仅是简单标识符：

```sh theme={null}
# ✅ 将 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"'
```

使用此功能在构建时内联环境变量：

```ts theme={null}
// 编译前
if (process.env.NODE_ENV === "production") {
  console.log("生产模式");
}

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

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

### TypeScript 声明

对于 TypeScript 项目，声明你的常量以避免类型错误：

```ts theme={null}
// 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;
```

### 跨平台兼容性

为多个平台构建时，常量的工作方式相同：

```sh theme={null}
# 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
```

***

## 相关

* [在运行时定义常量](/guides/runtime/define-constant) - 在 `bun run` 中使用 `--define`
* [构建可执行文件](/bundler/executables) - `bun build --compile` 完整指南
* [打包器 API](/bundler) - 完整的打包器文档，包括 `define` 选项
