diff --git a/apps/desktop/scripts/build-main.mjs b/apps/desktop/scripts/build-main.mjs index 3bdc56d..d58b9f4 100644 --- a/apps/desktop/scripts/build-main.mjs +++ b/apps/desktop/scripts/build-main.mjs @@ -12,7 +12,10 @@ await build({ banner: { js: [ 'import { createRequire as __mdToPdfCreateRequire } from "node:module";', - "const require = __mdToPdfCreateRequire(import.meta.url);" + 'import { dirname as __mdToPdfDirname } from "node:path";', + 'import { fileURLToPath as __mdToPdfFileURLToPath } from "node:url";', + "const require = __mdToPdfCreateRequire(import.meta.url);", + "const __dirname = __mdToPdfDirname(__mdToPdfFileURLToPath(import.meta.url));" ].join("\n") } }); diff --git a/apps/desktop/tests/release-build.test.ts b/apps/desktop/tests/release-build.test.ts index 7574b1d..c060d47 100644 --- a/apps/desktop/tests/release-build.test.ts +++ b/apps/desktop/tests/release-build.test.ts @@ -92,6 +92,23 @@ describe("桌面发行构建链", () => { expect(buildScript).toContain("dist/docx-media-filter.lua"); }); + it("为 ESM 主进程中的 CommonJS 字体转换依赖保留 __dirname", () => { + const buildScript = readFileSync( + `${desktopRoot}/scripts/build-main.mjs`, + "utf8" + ); + + expect(buildScript).toContain( + 'dirname as __mdToPdfDirname' + ); + expect(buildScript).toContain( + 'fileURLToPath as __mdToPdfFileURLToPath' + ); + expect(buildScript).toContain( + "const __dirname = __mdToPdfDirname(__mdToPdfFileURLToPath(import.meta.url));" + ); + }); + it("在 Docker Web 构建前复制样例并保留到运行镜像", () => { const dockerfile = readFileSync( `${projectRoot}/deploy/Dockerfile`,