tsup.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { pathToFileURL } from "node:url";
  4. import { defineConfig } from "tsup";
  5. // tsup 配置自身会被打包,构建工具需保留原始文件位置,不能被内联后重定位。
  6. const { loadBuiltinProviderConfig } = await import(
  7. pathToFileURL(resolve(import.meta.dirname, "../../scripts/builtin-provider-config.mjs")).href
  8. );
  9. const { stageThirdPartyNotices } = await import(
  10. pathToFileURL(resolve(import.meta.dirname, "../../scripts/third-party-notices.mjs")).href
  11. );
  12. // tsup config 可能从不同 cwd 加载,基于配置文件自身目录解析仓库根 package.json。
  13. const rootPackageJsonPath = resolve(import.meta.dirname, "../../package.json");
  14. const { version } = JSON.parse(readFileSync(rootPackageJsonPath, "utf-8"));
  15. const { environment: zcodeEnv, content: zcodeBuiltinProviderConfigJson } =
  16. await loadBuiltinProviderConfig();
  17. export const SERVER_HTTP_DEFINES = {
  18. __ZCODE_VERSION__: JSON.stringify(version),
  19. __ZCODE_ENV__: JSON.stringify(zcodeEnv),
  20. __ZCODE_BUILTIN_PROVIDER_CONFIG_JSON__: JSON.stringify(zcodeBuiltinProviderConfigJson),
  21. };
  22. function createSharedDefines() {
  23. return SERVER_HTTP_DEFINES;
  24. }
  25. export const SERVER_HTTP_EXTERNAL_DEPENDENCIES = [
  26. "ssh2",
  27. "node-pty",
  28. "undici",
  29. "axios",
  30. "form-data",
  31. "combined-stream",
  32. "proxy-from-env",
  33. "follow-redirects",
  34. "yazl",
  35. // Bugfix: node-forge 内部用动态 require("crypto"),内联进 server ESM bundle 后 Node 会报
  36. // Dynamic require of "crypto" is not supported;这里和 desktop main/host 构建保持同一外置策略。
  37. "node-forge",
  38. "yaml",
  39. // services 的反馈日志压缩链路引入 CJS 包 yazl,被内联进 ESM bundle 后
  40. // 运行时命中 require("fs") 动态 require,entry-http 启动即崩;保留外部依赖交给 Node 原生加载。
  41. "yazl",
  42. // Bugfix: node-forge(appCaCert 自签 CA 链路)内部动态 require("crypto"),
  43. // 与 desktop tsup.config 的处理一致,必须外置交给 Node 原生加载。
  44. "node-forge",
  45. // 修复:云内容 ZIP 解包链路(services/cloud-content)引入 CJS 包 yauzl,内联进 ESM bundle 后
  46. // entry-http.js 加载即报 Dynamic require of "fs" is not supported;与 desktop 相同,外置交给 Node 原生加载。
  47. "yauzl",
  48. ];
  49. export default defineConfig({
  50. // HTTP server 产物外置并内联第三方代码,dist 旁必须附带完整声明(Web/统一分发包直接复制 dist)。
  51. onSuccess: async () => {
  52. await stageThirdPartyNotices(resolve(import.meta.dirname, "dist"));
  53. },
  54. entry: { "entry-http": "src/entry-http.ts" },
  55. outDir: "dist",
  56. format: "esm",
  57. platform: "node",
  58. target: "node22",
  59. // workspace 包的 exports 指向 .ts 源码,node 运行时无法直接加载,需要 bundle 进来
  60. noExternal: [
  61. "@zcode/shared",
  62. "@zcode/rpc",
  63. "@zcode/services",
  64. "@zcode/services/node",
  65. "@zcode/client",
  66. ],
  67. // ssh2 / node-pty 含 .node native addon,不能被 esbuild 处理。
  68. // undici / axios 这类 CJS 依赖被内联进 ESM bundle 后,运行时会走到
  69. // require("assert") / require("util") / require("url") 等动态 require,Node 的 ESM wrapper 下会直接报 Dynamic require not supported。
  70. // HTTP server 场景保留为外部依赖,交给 Node 原生加载;remote 单文件 bundle 仍由 build-remote.ts 负责内联。
  71. external: SERVER_HTTP_EXTERNAL_DEPENDENCIES,
  72. define: createSharedDefines(),
  73. // esbuild 不认识 tsconfig.json 里的 es2025,用专门的 tsconfig.build.json 消除 warning
  74. tsconfig: "tsconfig.build.json",
  75. });