From 6c79eeb4abfd907f95653d4b9d265a255904115f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Sat, 15 Aug 2026 16:24:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(build):=20fs=20=E9=80=82=E9=85=8D=E5=B1=82?= =?UTF-8?q?=E6=94=B9=E9=9D=99=E6=80=81=20import=20=E5=88=86=E5=8F=91?= =?UTF-8?q?=E2=80=94=E2=80=94=E4=BF=AE=E5=A4=8D=20top-level=20await=20?= =?UTF-8?q?=E4=B8=8E=20CJS=20=E6=9E=84=E5=BB=BA=E4=B8=8D=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=EF=BC=88=E4=B8=8A=E4=B8=AA=E6=8F=90=E4=BA=A4=E9=81=97=E7=95=99?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app.ts | 14 +++++++------- backend/server/llrt_lr.ts | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 backend/server/llrt_lr.ts diff --git a/backend/app.ts b/backend/app.ts index 84b1043..c35b62c 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -1,10 +1,10 @@ -/** 首先加载 fs 适配层,必须在 main() 之前完成!按运行时选择实现(top-level await 保证注册先于一切使用) */ -if (typeof __RUNTIME__ !== "undefined" && __RUNTIME__ === "llrt") { - await import("./server/llrt"); -} else { - /** Node.js(tsx 开发 / tsdown define "node" 构建)走 node 适配器 */ - await import("./server/node"); -} +/** + * 首先加载 fs 适配层,必须在所有其他 import 之前! + * 静态 import 依赖 ESM hoisting:无论源码书写顺序,llrt/node 适配器都会最先执行 + * (implInterface 直接给 interface.ts 的导出变量赋值,注册必须先于任何使用)。 + * 不能用 top-level await import —— tsdown 的 CJS 产物不支持。 + */ +import "./server/llrt_lr"; import { mimeTypes } from "./server/mime_type"; import type { cMiddleware } from "./server/req_res"; diff --git a/backend/server/llrt_lr.ts b/backend/server/llrt_lr.ts new file mode 100644 index 0000000..dc48ce3 --- /dev/null +++ b/backend/server/llrt_lr.ts @@ -0,0 +1,21 @@ +/** + * fs 适配层分发器。 + * + * 按编译期 __RUNTIME__ 常量选择实现 —— tsdown 会把未命中分支 tree-shake 掉: + * - LLRT 构建(__RUNTIME__="llrt"):llrt.ts 生效 + * - Node 构建(__RUNTIME__="node")与 dev(tsx,__RUNTIME__ 未定义):node.ts 生效 + * + * 两个适配器都在模块顶层调用 implInterface 完成注册(副作用 import), + * ESM import hoisting 保证分发器先于 app.ts 其余 import 执行。 + * 注意:不能用 top-level await import —— tsdown 的 CJS 产物不支持; + * 条件 require 在打包时同样会被静态分析,未命中分支整块消除。 + */ + +/** 编译期常量:tsdown define 注入,dev 下未定义 */ +declare const __RUNTIME__: string | undefined; + +if (typeof __RUNTIME__ !== "undefined" && __RUNTIME__ === "llrt") { + require("./llrt"); +} else { + require("./node"); +}