TypeScript
Bun 原生支持 TypeScript。所有文件在执行前都会被 Bun 快速的原生转译器即时转译。与其它构建工具类似,Bun 不执行类型检查;它仅移除文件中的类型注解。
bun index.jsbun index.jsxbun index.tsbun index.tsxBun 的运行时行为的某些方面会受到 tsconfig.json 文件内容的影响。请参阅 Runtime > TypeScript 页面了解详情。
JSX
Bun 原生支持 .jsx 和 .tsx 文件。Bun 的内部转译器在执行前将 JSX 语法转换为纯 JavaScript。
function Component(props: {message: string}) {
return (
<body>
<h1 style={{color: 'red'}}>{props.message}</h1>
</body>
);
}
console.log(<Component message="Hello world!" />);
Bun 为 JSX 实现特殊的日志记录,以便于调试。
bun run react.tsx<Component message="Hello world!" />文本文件
文本文件可以作为字符串导入。
import text from "./text.txt";
console.log(text);
// => "Hello world!"
Hello world!
JSON、TOML 和 YAML
JSON、TOML 和 YAML 文件可以直接从源文件导入。内容将被加载并作为 JavaScript 对象返回。
import pkg from "./package.json";
import data from "./data.toml";
import config from "./config.yaml";
有关 YAML 支持的更多详细信息,请参阅 YAML API 文档。
WASI
🚧 实验性支持
Bun 对 WASI,即 WebAssembly System Interface,提供实验性支持。要使用 Bun 运行 .wasm 二进制文件
bun ./my-wasm-app.wasm# if the filename doesn't end with ".wasm"bun run ./my-wasm-app.whatever注意 — WASI 支持基于 wasi-js。目前,它仅支持使用 wasi_snapshot_preview1 或 wasi_unstable API 的 WASI 二进制文件。Bun 的实现尚未针对性能进行完全优化;随着 WASM 的普及,这将成为一个更优先考虑的事项。
SQLite
您可以直接将 sqlite 数据库导入您的代码。Bun 将自动加载数据库并返回一个 Database 对象。
import db from "./my.db" with { type: "sqlite" };
console.log(db.query("select * from users LIMIT 1").get());
这使用了 bun:sqlite。
自定义加载器
可以通过插件实现对其他文件类型的支持。有关完整文档,请参阅 Runtime > Plugins。