Bun 热重载 #
概述 #
Bun 内置热重载功能,文件修改后自动重新加载,无需手动重启服务。
基本用法 #
bash
# 使用 --hot 参数
bun --hot run server.ts
# 使用 --watch 参数
bun --watch run server.ts
hot vs watch #
| 参数 | 说明 |
|---|---|
--hot |
软重载,保留状态 |
--watch |
硬重载,完全重启 |
示例 #
typescript
// server.ts
Bun.serve({
port: 3000,
fetch() {
return new Response(`Server time: ${new Date().toISOString()}`);
},
});
console.log("Server running at http://localhost:3000");
bash
bun --hot run server.ts
修改文件后,服务器自动重载。
配置 #
toml
# bunfig.toml
[run]
# 预加载文件
preload = ["./setup.ts"]
下一步 #
现在你已经了解了 Bun 热重载,继续学习其他高级特性。
最后更新:2026-03-29