Bun

一体化 JavaScript 工具包 - 超快运行时、打包器、测试器、包管理器合一

TL;DR

是什么:一体化 JavaScript 运行时、打包器、测试运行器和包管理器。

为什么用:比 Node.js 快 4 倍、原生 TypeScript、内置打包器、可替代 Node。

Quick Start

安装

# macOS/Linux
curl -fsSL https://bun.sh/install | bash

# Windows
powershell -c "irm bun.sh/install.ps1 | iex"

# 检查版本
bun --version

运行脚本

bun run index.ts     # 直接运行 TypeScript
bun run index.js     # 运行 JavaScript
bun run dev          # 运行 package.json 脚本

Cheatsheet

命令描述
bun run file运行文件
bun install安装依赖
bun add pkg添加依赖
bun remove pkg移除包
bun test运行测试
bun build生产打包
bun init创建新项目

Gotchas

包管理

# 初始化项目
bun init

# 安装依赖
bun install

# 添加包
bun add express

# 添加开发依赖
bun add -d typescript

# 移除包
bun remove lodash

# 更新包
bun update

运行脚本

# 直接运行 TypeScript
bun run app.ts

# 监听模式运行
bun --watch run app.ts

# 运行 package.json 脚本
bun run dev

# 运行脚本(简写)
bun dev

HTTP 服务器

// server.ts
const server = Bun.serve({
  port: 3000,
  fetch(request) {
    const url = new URL(request.url);

    if (url.pathname === "/") {
      return new Response("Hello Bun!");
    }

    if (url.pathname === "/json") {
      return Response.json({ message: "Hello" });
    }

    return new Response("Not Found", { status: 404 });
  },
});

console.log(`Listening on http://localhost:${server.port}`);

文件 I/O

// 读取文件
const file = Bun.file("./data.json");
const text = await file.text();
const json = await file.json();

// 写入文件
await Bun.write("./output.txt", "Hello World");
await Bun.write("./data.json", JSON.stringify({ key: "value" }));

// 检查是否存在
const exists = await Bun.file("./data.json").exists();

测试

// math.test.ts
import { describe, expect, test } from "bun:test";

describe("math", () => {
  test("2 + 2", () => {
    expect(2 + 2).toBe(4);
  });

  test("async test", async () => {
    const result = await Promise.resolve(42);
    expect(result).toBe(42);
  });
});
bun test
bun test --watch

打包器

# 为浏览器打包
bun build ./src/index.ts --outdir ./dist

# 带压缩
bun build ./src/index.ts --outdir ./dist --minify

# 为 Node.js
bun build ./src/index.ts --outdir ./dist --target node
// 编程式打包
const result = await Bun.build({
  entrypoints: ['./src/index.ts'],
  outdir: './dist',
  minify: true,
  splitting: true,
});

Next Steps