sea-runtime-package-resolution.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { createRequire } from "node:module";
  2. import { access, readFile } from "node:fs/promises";
  3. import { dirname, posix, resolve } from "node:path";
  4. export function placeRuntimePackage({ packageName, packageDirectory, fromAssetPath, placements }) {
  5. // 原收集器仅按包名去重,把 contracts 的 Zod 3 和 shared 的 Zod 4 压成同一个包。
  6. // 按消费者的 Node 查找顺序复用同一物理包;冲突版本放入消费者自己的 node_modules。
  7. let directory = fromAssetPath;
  8. while (true) {
  9. const candidate = posix.join(directory, "node_modules", packageName);
  10. const existing = placements.get(candidate);
  11. if (existing) {
  12. if (existing === packageDirectory) return undefined;
  13. break;
  14. }
  15. if (directory === ".") break;
  16. directory = posix.dirname(directory);
  17. }
  18. const rootPath = posix.join("node_modules", packageName);
  19. const assetPath = placements.has(rootPath)
  20. ? posix.join(fromAssetPath, "node_modules", packageName)
  21. : rootPath;
  22. placements.set(assetPath, packageDirectory);
  23. return assetPath;
  24. }
  25. export const resolveRuntimePackageDirectory = async ({
  26. fromDirectory,
  27. packageName,
  28. root,
  29. workspacePackageDirectories,
  30. }) => {
  31. const workspacePackageDirectory = workspacePackageDirectories.get(packageName);
  32. if (workspacePackageDirectory) {
  33. const directory = workspacePackageDirectory;
  34. await assertPackageDirectory(packageName, directory);
  35. // 原检查要求 dist/index.js。shared 依赖 session-control 后,session-control 进入 TUI 闭包;
  36. // 但它只有子路径导出,不会生成 dist/index.js,build:sea 因此必然失败。
  37. // SEA 运行时只加载 @zcode/tui/dist/index.js(esbuild 已内联全部 @zcode/* 源码),
  38. // 闭包中其他 workspace 包的 dist 不会被加载,所以这里只确认包已构建,不校验具体入口。
  39. if (!(await exists(resolve(directory, "dist")))) {
  40. throw new Error(
  41. `Missing ${packageName} dist files. Run \`pnpm build:sea:prepare\` from the repository root before \`pnpm sea\`.`,
  42. );
  43. }
  44. return directory;
  45. }
  46. const require = createRequire(resolve(fromDirectory ?? root, "package.json"));
  47. try {
  48. try {
  49. return dirname(require.resolve(`${packageName}/package.json`));
  50. } catch (error) {
  51. if (error?.code !== "ERR_PACKAGE_PATH_NOT_EXPORTED") {
  52. throw error;
  53. }
  54. return await findPackageRoot(require.resolve(packageName), packageName);
  55. }
  56. } catch (error) {
  57. const packageDirectory = await resolvePackageRootFromNodeModules({
  58. fromDirectory,
  59. packageName,
  60. root,
  61. });
  62. if (packageDirectory) {
  63. return packageDirectory;
  64. }
  65. throw new Error(
  66. `Missing SEA TUI runtime package ${packageName}. Run \`pnpm install\` and try again.`,
  67. {
  68. cause: error,
  69. },
  70. );
  71. }
  72. };
  73. const resolvePackageRootFromNodeModules = async ({ fromDirectory, packageName, root }) => {
  74. const packagePathSegments = packageName.split("/");
  75. for (const directory of ancestorDirectories(resolve(fromDirectory ?? root))) {
  76. const packageDirectory = resolve(directory, "node_modules", ...packagePathSegments);
  77. if (await exists(resolve(packageDirectory, "package.json"))) {
  78. return packageDirectory;
  79. }
  80. }
  81. return undefined;
  82. };
  83. function* ancestorDirectories(startDirectory) {
  84. let directory = startDirectory;
  85. while (true) {
  86. yield directory;
  87. const parentDirectory = dirname(directory);
  88. if (parentDirectory === directory) {
  89. return;
  90. }
  91. directory = parentDirectory;
  92. }
  93. }
  94. const findPackageRoot = async (entryPath, packageName) => {
  95. let directory = dirname(entryPath);
  96. while (directory !== dirname(directory)) {
  97. const packageJsonPath = resolve(directory, "package.json");
  98. if (await exists(packageJsonPath)) {
  99. const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
  100. if (packageJson.name === packageName) {
  101. return directory;
  102. }
  103. }
  104. directory = dirname(directory);
  105. }
  106. throw new Error(`Could not find package root for ${packageName} from ${entryPath}`);
  107. };
  108. const assertPackageDirectory = async (packageName, directory) => {
  109. if (!(await exists(resolve(directory, "package.json")))) {
  110. throw new Error(`Missing package.json for ${packageName} at ${directory}`);
  111. }
  112. };
  113. async function exists(path) {
  114. try {
  115. await access(path);
  116. return true;
  117. } catch (error) {
  118. if (error.code === "ENOENT") return false;
  119. throw error;
  120. }
  121. }