Bun 常用命令速查清单(TypeScript 编译篇)
Bun 是一款集运行时、包管理器、打包工具、测试框架于一体的全栈 JavaScript/TypeScript 工具链。它内置了 TypeScript 编译器,无需额外安装 tsc 即可直接运行 .ts 和 .tsx 文件。以下是常用命令速查清单,重点整理 TypeScript 编译相关命令。
一、安装与版本管理
| 命令 | 说明 |
|---|---|
| `curl -fsSL https://bun.sh/install | bash` |
npm install -g bun |
通过 npm 全局安装 |
bun --version |
查看 Bun 版本 |
bun upgrade |
升级 Bun 到最新版 |
二、项目初始化与 TypeScript 配置
bun init
初始化新项目,自动生成 package.json 和推荐的 tsconfig.json。
bash
bun init
运行后会自动生成 tsconfig.json,包含 Bun 推荐的 TypeScript 编译选项:
json
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true
}
}
关键选项说明:
noEmit: true--- 不让 TypeScript 输出 JS 文件,因为 Bun 在运行时会直接转译moduleResolution: bundler--- 使用 Bundler 解析算法,支持带扩展名的.ts导入allowImportingTsExtensions: true--- 允许import "./foo.ts"语法
安装 TypeScript 类型定义
bash
bun add -d @types/bun # 安装 Bun 内置 API 的类型定义
bun add -d @types/node # 如需 Node.js 类型
三、运行 TypeScript(无需编译)
Bun 可直接运行 .ts 和 .tsx 文件,不需要预先编译。
| 命令 | 说明 |
|---|---|
bun run index.ts |
运行 TypeScript 文件 |
bun index.ts |
简写形式,效果相同 |
bun run --watch index.ts |
监听模式,文件变化时自动重启 |
bun run --hot index.ts |
热重载模式 |
bun run --inspect index.ts |
开启调试模式 |
注意:Bun 的转译器只移除类型注解,不做类型检查 。如需类型检查,应单独运行 tsc --noEmit。
四、构建打包(TypeScript 编译)
bun build --- 基础打包命令
将 TypeScript 代码打包为 JavaScript 输出:
bash
# 打包单个入口文件
bun build ./src/index.ts --outdir ./dist
# 指定输出文件名
bun build ./src/index.ts --outfile ./dist/bundle.js
# 同时打包多个入口
bun build ./src/a.ts ./src/b.ts --outdir ./dist
常用选项
| 选项 | 说明 |
|---|---|
--outdir <dir> |
输出目录 |
--outfile <file> |
输出文件路径 |
--minify |
压缩输出代码 |
--sourcemap |
生成 Source Map |
| `--target browser | bun |
--splitting |
开启代码拆分(仅支持 ESM) |
--tsconfig-override <path> |
指定自定义 tsconfig.json 路径 |
--external <pkg> |
将指定包标记为外部依赖(不打包) |
--define <key=value> |
定义编译时常量替换 |
五、编译为独立可执行文件(重点)
bun build --compile --- 生成二进制可执行文件
这是 Bun 最强大的 TypeScript 编译特性之一:将 .ts 文件编译为无需运行时环境的独立二进制可执行文件。
bash
# 编译 TS 为当前平台的可执行文件
bun build ./cli.ts --compile --outfile mycli
生成的 mycli 可以直接运行,不需要安装 Node.js 或 Bun。
六、包管理与脚本执行
| 命令 | 别名 | 说明 |
|---|---|---|
bun install |
bun i |
安装全部依赖 |
bun add <pkg> |
bun a |
添加依赖(生产) |
bun add -d <pkg> |
--- | 添加开发依赖 |
bun remove <pkg> |
bun rm |
移除依赖 |
bun update <pkg> |
bun up |
更新依赖 |
bun run <script> |
bun <script> |
运行 package.json 中的脚本 |
bunx <pkg> |
bun x |
临时执行 npm 包(类似 npx) |
bun --bun <cmd> |
--- | 强制使用 Bun 运行时而非 Node.js |
七、测试
| 命令 | 说明 |
|---|---|
bun test |
运行所有测试文件(*.test.ts) |
bun test --watch |
监听模式运行测试 |
bun test --coverage |
生成测试覆盖率报告 |
八、常用组合示例
bash
# 1. 初始化 TypeScript 项目
bun init
bun add -d @types/bun
# 2. 开发时直接运行 TS 并监听变化
bun --watch src/index.ts
# 3. 构建生产版本(打包 + 压缩 + Source Map)
bun build src/index.ts --outdir dist --minify --sourcemap
# 4. 编译为 Linux 服务器可执行文件
bun build src/cli.ts --compile --target=bun-linux-x64 --outfile cli-linux
# 5. 类型检查(单独执行)
tsc --noEmit
# 6. 一次性构建 + 类型检查
tsc --noEmit && bun build src/index.ts --outdir dist
小贴士
- 类型检查 :Bun 运行时不检查类型,建议在 CI 或提交前运行
tsc --noEmit。 - 环境一致性 :提交
bun.lockb到 Git,配合bun install --frozen-lockfile确保依赖锁定。 - bunx :类似
npx,用于临时执行 npm 包命令,无需全局安装。 - 查看完整帮助 :运行
bun --help或bun build --help获取最新命令详情。