// 打包 server:工作区包(@umt/*)以源码内联,npm 依赖保持 external 运行时从 node_modules 加载。 // external 依赖必须在本包 package.json 的 dependencies 里声明(工作区包的运行时依赖也要在此重复声明), // 否则运行时解析不到,构建直接报错。 import { readFileSync } from "node:fs"; import { builtinModules } from "node:module"; import { build } from "esbuild"; const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); const deps = new Set(Object.keys(pkg.dependencies ?? {})); const builtins = new Set(builtinModules); const pkgName = (p) => (p.startsWith("@") ? p.split("/").slice(0, 2).join("/") : p.split("/")[0]); await build({ entryPoints: ["src/main.ts"], outfile: "dist/main.js", bundle: true, platform: "node", target: "node24", format: "esm", sourcemap: true, logLevel: "info", plugins: [ { name: "external-npm", setup(b) { b.onResolve({ filter: /^[^./]/ }, (args) => { if (args.path.startsWith("node:") || builtins.has(args.path)) return { path: args.path, external: true }; const name = pkgName(args.path); if (name.startsWith("@umt/")) return undefined; if (!deps.has(name)) { return { errors: [ { text: `运行时依赖 ${name} 未在 apps/server/package.json dependencies 中声明(来自 ${args.importer})` }, ], }; } return { path: args.path, external: true }; }); }, }, ], });