gpt4 book ai didi

typescript - 使用 'bun dev' 进行热重载 (HMR)

转载 作者:行者123 更新时间:2023-12-05 09:01:26 24 4
gpt4 key购买 nike

我正在尝试新的 bun平台 (v0.1.6) 与 Hono .

我遵循的步骤:

bun create hono test-api
cd test-api
bun dev

然后服务器显示这条信息:

$ bun dev
[1.00ms] bun!! v0.1.6


Link: http://localhost:3000

当我修改任何文件时,服务器会检测到它,然后重新加载应用程序,但我不知道如何调用我的应用程序 REST API。

如果我执行:curl localhost:3000 响应是一个转译的 JS 代码:

import {
__require
} from "http://localhost:3000/bun:wrap";
import {
__HMRClient as Bun
} from "http://localhost:3000/bun:wrap";
Bun.activate(false);
import {
__HMRModule as HMR
} from "http://localhost:3000/bun:wrap";
import * as $9121e9 from "http://localhost:3000/node_modules/hono/dist/index.js";
var { Hono} = __require($9121e9);
var hmr = new HMR(2320229645, "src/index.ts"), exports = hmr.exports;
(hmr._load = function() {
const app = new Hono;
const port = parseInt(process.env.PORT) || 3000;
const home = app.get("/", (c) => {
return c.json({ message: "Hello World!" });
});
console.log(`Running at http://localhost:${port}`);
var src_default = {
port,
fetch: home.fetch
};
hmr.exportAll({
default: () => src_default
});
})();
var $$hmr_default = hmr.exports.default;
hmr._update = function(exports) {
$$hmr_default = exports.default;
};

export {
$$hmr_default as default
};

//# sourceMappingURL=http://localhost:3000/.map

index.ts 中原始生成的代码是:

import { Hono } from "hono";

const app = new Hono();

const port = parseInt(process.env.PORT) || 3000;

const home = app.get("/", (c) => {
return c.json({ message: "Hello World!" });
});

console.log(`Running at http://localhost:${port}`);

export default {
port,
fetch: home.fetch,
};

我没有在 bun README.md 中找到关于 bun dev 的文档但是当创建应用程序时,它会出现一条消息,只执行“bun dev”,所以我可能遗漏了一些明显的东西。

如何调用运行 bun dev 的 hono API Hello-Word?

另一方面,如果我执行:bun src/index.ts,应用程序会按预期运行,但不会进行热重载。

最佳答案

对于当前版本(从 0.2.0 开始),请参阅已接受的答案。

在当前版本 (v 0.1.6) 中,命令 bun dev 仅适用于前端项目,不适用于后端(REST API...)。根据 bun 提交者在 Bun Discord server 中的回答

但是,我们可以使用 nodemon 获得类似的结果工具:

bun add -d nodemon

将文件 nodemon.json 添加到您的项目根目录,内容如下:

{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "bun ./src/index.ts"
}

然后,使用此命令执行您的项目:

bun run nodemon

nodemon output

当您更改源文件时,该命令将自动重新启动 bun 解释器

关于typescript - 使用 'bun dev' 进行热重载 (HMR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73208846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com