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

> 使用 Bun 的 JavaScript 和 TypeScript 压缩器减少包大小

# Minifier

Bun 内置了一个快速的 JavaScript 和 TypeScript 压缩器，可以根据代码库情况将包大小减少 80% 甚至更多，同时提升输出代码的运行速度。该压缩器执行了数十种优化，包括常量折叠、死代码消除和语法转换。与其他压缩器不同，Bun 的压缩器还能让 `bun build` 运行更快，因为打印的代码更少。

## CLI 用法

### 启用所有压缩

使用 `--minify` 标志启用所有压缩模式：

```bash theme={"theme":{"light":"github-light","dark":"dracula"}}
bun build ./index.ts --minify --outfile=out.js
```

`--minify` 标志会自动启用：

* 空白字符压缩
* 语法压缩
* 标识符压缩

### 生产模式

`--production` 标志会自动启用压缩：

```bash theme={"theme":{"light":"github-light","dark":"dracula"}}
bun build ./index.ts --production --outfile=out.js
```

`--production` 标志还会：

* 设置 `process.env.NODE_ENV` 为 `production`
* 启用生产模式下的 JSX 导入和转换

### 细粒度控制

你可以单独启用特定的压缩模式：

```bash theme={"theme":{"light":"github-light","dark":"dracula"}}
# 仅移除空白字符
bun build ./index.ts --minify-whitespace --outfile=out.js

# 仅压缩语法
bun build ./index.ts --minify-syntax --outfile=out.js

# 仅压缩标识符
bun build ./index.ts --minify-identifiers --outfile=out.js

# 组合特定模式
bun build ./index.ts --minify-whitespace --minify-syntax --outfile=out.js
```

## JavaScript API

使用 Bun 的打包器进行编程时，通过 `minify` 选项配置压缩：

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
await Bun.build({
  entrypoints: ["./index.ts"],
  outdir: "./out",
  minify: true, // 启用所有压缩模式
});
```

细粒度控制则传入对象：

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
await Bun.build({
  entrypoints: ["./index.ts"],
  outdir: "./out",
  minify: {
    whitespace: true,
    syntax: true,
    identifiers: true,
  },
});
```

## 压缩模式

Bun 的压缩器有三个独立模式，可单独或组合启用。

### 空白字符压缩（`--minify-whitespace`）

移除输出中的所有多余空白字符、换行和格式。

### 语法压缩（`--minify-syntax`）

将 JavaScript 语法重写为更短的等效形式，并执行常量折叠、死代码消除及其他优化。

### 标识符压缩（`--minify-identifiers`）

根据使用频率重命名局部变量和函数名为更短的标识符。

## 所有转换

### 布尔字面量缩短

**模式：** `--minify-syntax`

将布尔字面量转换为更短的表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
true
false
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
!0
!1
```

### 布尔代数优化

**模式：** `--minify-syntax`

通过逻辑规则简化布尔表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
!!x
x === true
x && true
x || false
!true
!false
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
x
x
x
!1
!0
```

### undefined 缩短

**模式：** `--minify-syntax`

用更短的等效表达式替换 `undefined`。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
undefined
let x = undefined;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
void 0
let x=void 0;
```

### undefined 相等性优化

**模式：** `--minify-syntax`

优化与 undefined 的宽松相等检查。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
x == undefined
x != undefined
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x == null
x != null
```

### Infinity 缩短

**模式：** `--minify-syntax`

将 Infinity 转换为数学表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
Infinity
-Infinity
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
1/0
-1/0
```

### typeof 优化

**模式：** `--minify-syntax`

优化 typeof 对比并求值常量 typeof 表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
typeof x === 'undefined'
typeof x !== 'undefined'
typeof require
typeof null
typeof true
typeof 123
typeof "str"
typeof 123n
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
typeof x>'u'
typeof x<'u'
"function"
"object"
"boolean"
"number"
"string"
"bigint"
```

### 数字格式化

**模式：** `--minify-syntax`

以最紧凑形式格式化数字。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
10000
100000
1000000
1.0
-42.0
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
1e4
1e5
1e6
1
-42
```

### 算术常量折叠

**模式：** `--minify-syntax`

编译时计算算术运算。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
1 + 2
10 - 5
3 * 4
10 / 2
10 % 3
2 ** 3
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
3
5
12
5
1
8
```

### 位运算常量折叠

**模式：** `--minify-syntax`

编译时计算位运算。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
5 & 3
5 | 3
5 ^ 3
8 << 2
32 >> 2
~5
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
1
7
6
32
8
-6
```

### 字符串拼接

**模式：** `--minify-syntax`

编译时合并字符串字面量。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
"a" + "b"
"x" + 123
"foo" + "bar" + "baz"
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"ab"
"x123"
"foobarbaz"
```

### 字符串索引

**模式：** `--minify-syntax`

编译时计算字符串字符访问。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
"foo"[2]
"hello"[0]
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"o"
"h"
```

### 模板字面量折叠

**模式：** `--minify-syntax`

编译时计算带常量表达式的模板字面量。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
`a${123}b`
`result: ${5 + 10}`
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"a123b"
"result: 15"
```

### 模板字面量转字符串

**模式：** `--minify-syntax`

将简单模板字面量转换为常规字符串。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
`Hello World`
`Line 1
Line 2`
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"Hello World"
"Line 1\nLine 2"
```

### 字符串引号优化

**模式：** `--minify-syntax`

选择最优引号以减少转义。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
"It's a string"
'He said "hello"'
`Simple string`
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"It's a string"
'He said "hello"'
"Simple string"
```

### 数组展开内联

**模式：** `--minify-syntax`

内联对常量数组的数组展开操作。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
[1, ...[2, 3], 4]
[...[a, b]]
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
[1,2,3,4]
[a,b]
```

### 数组索引

**模式：** `--minify-syntax`

编译时计算常量数组访问。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
[x][0]
['a', 'b', 'c'][1]
['a', , 'c'][1]
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
'b'
void 0
```

### 属性访问优化

**模式：** `--minify-syntax`

可用时将括号访问转换为点访问。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
obj["property"]
obj["validName"]
obj["123"]
obj["invalid-name"]
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
obj.property
obj.validName
obj["123"]
obj["invalid-name"]
```

### 比较折叠

**模式：** `--minify-syntax`

编译时计算常量比较。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
3 < 5
5 > 3
3 <= 3
5 >= 6
"a" < "b"
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
!0
!0
!0
!1
!0
```

### 逻辑操作折叠

**模式：** `--minify-syntax`

简化带常量值的逻辑操作。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
true && x
false && x
true || x
false || x
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
!1
!0
x
```

### 空值合并折叠

**模式：** `--minify-syntax`

编译时计算已知值的空值合并操作。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
null ?? x
undefined ?? x
42 ?? x
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
x
42
```

### 逗号表达式简化

**模式：** `--minify-syntax`

移除无副作用的逗号序列表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
(0, x)
(123, "str", x)
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
x
```

### 三元条件折叠

**模式：** `--minify-syntax`

编译时计算常量条件的三元表达式。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
true ? a : b
false ? a : b
x ? true : false
x ? false : true
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
a
b
x ? !0 : !1
x ? !1 : !0
```

### 一元表达式折叠

**模式：** `--minify-syntax`

简化一元操作。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
+123
+"123"
-(-x)
~~x
!!x
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
123
123
123
123
x
```

### 双重否定移除

**模式：** `--minify-syntax`

移除不必要的双重否定。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
!!x
!!!x
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x
!x
```

### if 语句优化

**模式：** `--minify-syntax`

优化常量条件的 if 语句。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
if (true) x;
if (false) x;
if (x) { a; }
if (x) {} else y;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x;
// 移除
if(x)a;
if(!x)y;
```

### 死代码消除

**模式：** `--minify-syntax`

移除不可达和无副作用代码。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
if (false) {
  unreachable();
}
function foo() {
  return x;
  deadCode();
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
function foo(){return x}
```

### 不可达分支移除

**模式：** `--minify-syntax`

移除永远不会执行的分支。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
while (false) {
  neverRuns();
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
// 完全移除
```

### 空块移除

**模式：** `--minify-syntax`

移除空块和多余的大括号。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
{ }
if (x) { }
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
;
// 移除
```

### 单语句块展开

**模式：** `--minify-syntax`

移除单语句块多余的大括号。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
if (condition) {
  doSomething();
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
if(condition)doSomething();
```

### TypeScript 枚举内联

**模式：** `--minify-syntax`

编译时内联 TypeScript 枚举值。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
enum Color { Red, Green, Blue }
const x = Color.Red;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
const x=0;
```

### 纯净标注支持

**模式：** 始终生效

尊重 `/*@__PURE__*/` 标注以支持摇树优化。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
const x = /*@__PURE__*/ expensive();
// 如果 x 未使用...
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
// 完全移除
```

### 标识符重命名

**模式：** `--minify-identifiers`

根据使用频率重命名局部变量为更短的名称。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
function calculateSum(firstNumber, secondNumber) {
  const result = firstNumber + secondNumber;
  return result;
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
function a(b,c){const d=b+c;return d}
```

**命名策略：**

* 使用频率最高的标识符获得最短名称（a, b, c…）
* 单字母：a-z（共 26 个）
* 双字母：aa-zz（676 个）
* 必要时使用三字母及以上

**保留标识符：**

* JavaScript 关键字和保留字
* 全局标识符
* 命名导出（保持 API 稳定）
* CommonJS 名称：`exports`、`module`

### 空白字符移除

**模式：** `--minify-whitespace`

移除所有多余空白。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
function add(a, b) {
    return a + b;
}
let x = 10;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
function add(a,b){return a+b;}let x=10;
```

### 分号优化

**模式：** `--minify-whitespace`

仅在必要时插入分号。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
let a = 1;
let b = 2;
return a + b;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
let a=1;let b=2;return a+b
```

### 操作符空格移除

**模式：** `--minify-whitespace`

移除操作符周围空格。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
a + b
x = y * z
foo && bar || baz
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
a+b
x=y*z
foo&&bar||baz
```

### 注释移除

**模式：** `--minify-whitespace`

移除注释，但保留重要的许可证注释。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
// 该注释被移除
/* 这个也是 */
/*! 但该许可证注释被保留 */
function test() { /* 内联注释 */ }
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
/*! 但该许可证注释被保留 */
function test(){}
```

### 对象和数组格式化

**模式：** `--minify-whitespace`

移除对象和数组字面量内空白。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
const obj = {
    name: "John",
    age: 30
};
const arr = [1, 2, 3];
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
const obj={name:"John",age:30};const arr=[1,2,3];
```

### 控制流格式化

**模式：** `--minify-whitespace`

移除控制结构内空白。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
if (condition) {
    doSomething();
}
for (let i = 0; i < 10; i++) {
    console.log(i);
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
if(condition)doSomething();for(let i=0;i<10;i++)console.log(i);
```

### 函数格式化

**模式：** `--minify-whitespace`

移除函数声明内空白。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
function myFunction(param1, param2) {
    return param1 + param2;
}
const arrow = (a, b) => a + b;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
function myFunction(a,b){return a+b}const arrow=(a,b)=>a+b;
```

### 括号最小化

**模式：** 始终生效

仅在优先级需要时添加括号。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
(a + b) * c
a + (b * c)
((x))
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
(a+b)*c
a+b*c
x
```

### 属性名重整

**模式：** `--minify-identifiers`（需配置）

启用时，将对象属性名重命名为更短名称。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
obj.longPropertyName
```

```js Output（启用属性名重整） theme={"theme":{"light":"github-light","dark":"dracula"}}
obj.a
```

### 模板字面量值折叠

**模式：** `--minify-syntax`

将非字符串插值值转为字符串并折叠进模板。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
`hello ${123}`
`value: ${true}`
`result: ${null}`
`status: ${undefined}`
`big: ${10n}`
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"hello 123"
"value: true"
"result: null"
"status: undefined"
"big: 10"
```

### 字符串长度常量折叠

**模式：** `--minify-syntax`

编译时计算字符串字面量的 `.length` 属性。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
"hello world".length
"test".length
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
11
4
```

### 构造函数调用简化

**模式：** `--minify-syntax`

简化内置类型的构造函数调用。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
new Object()
new Object(null)
new Object({a: 1})
new Array()
new Array(x, y)
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
{}
{}
{a:1}
[]
[x,y]
```

### 单属性对象内联

**模式：** `--minify-syntax`

内联只有单个属性的对象的属性访问。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
({fn: () => console.log('hi')}).fn()
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
(() => console.log('hi'))()
```

### 字符串 charCodeAt 常量折叠

**模式：** 始终生效

对 ASCII 字符串字面量计算 `charCodeAt()`。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
"hello".charCodeAt(1)
"A".charCodeAt(0)
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
101
65
```

### void 0 等价 null 相等转换

**模式：** `--minify-syntax`

将与 `void 0` 的宽松相等转换为 `null`，因为等价。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
x == void 0
x != void 0
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
x == null
x != null
```

### 取反操作符优化

**模式：** `--minify-syntax`

将取反操作符移至逗号表达式内。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
-(a, b)
-(x, y, z)
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
a,-b
x,y,-z
```

### Import.meta 属性内联

**模式：** 打包模式下

构建时内联已知的 `import.meta` 属性值。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
import.meta.dir
import.meta.file
import.meta.path
import.meta.url
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
"/path/to/directory"
"filename.js"
"/full/path/to/file.js"
"file:///full/path/to/file.js"
```

### 变量声明合并

**模式：** `--minify-syntax`

合并相邻的相同类型变量声明。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
let a = 1;
let b = 2;
const c = 3;
const d = 4;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
let a=1,b=2;
const c=3,d=4;
```

### 表达式语句合并

**模式：** `--minify-syntax`

使用逗号运算符合并相邻表达式语句。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log(1);
console.log(2);
console.log(3);
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log(1),console.log(2),console.log(3);
```

### return 语句合并

**模式：** `--minify-syntax`

合并 return 前的表达式，使用逗号运算符。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log(x);
return y;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
return console.log(x),y;
```

### throw 语句合并

**模式：** `--minify-syntax`

合并 throw 前的表达式，使用逗号运算符。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log(x);
throw new Error();
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
throw(console.log(x),new Error());
```

### TypeScript 枚举跨模块内联

**模式：** `--minify-syntax`（打包模式）

跨模块边界内联枚举值。

```ts Input  theme={"theme":{"light":"github-light","dark":"dracula"}}
// lib.ts
export enum Color { Red, Green, Blue }

// 输入（main.ts）
import { Color } from './lib';
const x = Color.Red;
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
const x=0;
```

### 计算属性枚举内联

**模式：** `--minify-syntax`

内联用作计算属性对象属性的枚举值。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
enum Keys { FOO = 'foo' }
const obj = { [Keys.FOO]: value }
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
const obj={foo:value}
```

### 字符串数字转数字索引

**模式：** `--minify-syntax`

将字符串数字属性访问转换为数字索引。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
obj["0"]
arr["5"]
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
obj[0]
arr[5]
```

### 箭头函数主体简写

**模式：** 始终生效

当箭头函数仅返回值时，使用表达式主体语法。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
() => { return x; }
(a) => { return a + 1; }
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
() => x
a => a + 1
```

### 对象属性简写

**模式：** 始终生效

当属性名与值的标识符相同时，使用简写语法。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
{ x: x, y: y }
{ name: name, age: age }
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
{ x, y }
{ name, age }
```

### 方法简写

**模式：** 始终生效

在对象字面量中使用方法简写语法。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
{
  foo: function() {},
  bar: async function() {}
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
{
  foo() {},
  async bar() {}
}
```

### 移除 debugger 语句

**模式：** `--drop=debugger`

移除代码中的 `debugger` 语句。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
function test() {
  debugger;
  return x;
}
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
function test(){return x}
```

### 移除 console 调用

**模式：** `--drop=console`

移除所有 `console.*` 方法调用。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
console.log("debug");
console.warn("warning");
x = console.error("error");
```

```js Output theme={"theme":{"light":"github-light","dark":"dracula"}}
void 0;
void 0;
x=void 0;
```

### 移除自定义函数调用

**模式：** `--drop=<name>`

移除指定全局函数或方法的调用。

```ts Input theme={"theme":{"light":"github-light","dark":"dracula"}}
assert(condition);
obj.assert(test);
```

```js Output（使用 --drop=assert） theme={"theme":{"light":"github-light","dark":"dracula"}}
void 0;
void 0;
```

## 保留名称

压缩标识符时，如果想保留原始函数和类名以便调试，可以使用 `--keep-names` 标志：

```bash theme={"theme":{"light":"github-light","dark":"dracula"}}
bun build ./index.ts --minify --keep-names --outfile=out.js
```

或者在 JavaScript API 中：

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
await Bun.build({
  entrypoints: ["./index.ts"],
  outdir: "./out",
  minify: {
    identifiers: true,
    keepNames: true,
  },
});
```

这会保留函数和类的 `.name` 属性，同时仍压缩实际的标识符名称。

## 组合示例

同时使用三种压缩模式：

```ts input.ts (158 bytes) theme={"theme":{"light":"github-light","dark":"dracula"}}
const myVariable = 42;

const myFunction = () => {
  const isValid = true;
  const result = undefined;
  return isValid ? myVariable : result;
};

const output = myFunction();
```

```js output.js theme={"theme":{"light":"github-light","dark":"dracula"}}
// 使用 --minify 输出（49 字节，减少 69%）
const a=42,b=()=>{const c=!0,d=void 0;return c?a:d},e=b();
```

## 何时使用压缩

**适用 `--minify` 的场景：**

* 生产环境包
* 降低 CDN 带宽成本
* 提升页面加载速度

**适用单独模式的场景：**

* **`--minify-whitespace`：** 快速减小尺寸，不改变语义
* **`--minify-syntax`：** 输出更小，同时保留可读标识符，便于调试
* **`--minify-identifiers`：** 最大尺寸减小（结合 `--keep-names` 可得更好的堆栈信息）

**避免压缩的场景：**

* 开发构建（调试更困难）
* 需要可读错误信息时
* 代码库供消费者阅读源码的库
