sea-runtime-package-resolution.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (!(await exists(resolve(directory, "dist", "index.js")))) {
  36. throw new Error(`Missing ${packageName} dist files. Run \`pnpm build\` before \`pnpm sea\`.`);
  37. }
  38. return directory;
  39. }
  40. const require = createRequire(resolve(fromDirectory ?? root, "package.json"));
  41. try {
  42. try {
  43. return dirname(require.resolve(`${packageName}/package.json`));
  44. } catch (error) {
  45. if (error?.code !== "ERR_PACKAGE_PATH_NOT_EXPORTED") {
  46. throw error;
  47. }
  48. return await findPackageRoot(require.resolve(packageName), packageName);
  49. }
  50. } catch (error) {
  51. const packageDirectory = await resolvePackageRootFromNodeModules({
  52. fromDirectory,
  53. packageName,
  54. root,
  55. });
  56. if (packageDirectory) {
  57. return packageDirectory;
  58. }
  59. throw new Error(
  60. `Missing SEA TUI runtime package ${packageName}. Run \`pnpm install\` and try again.`,
  61. {
  62. cause: error,
  63. },
  64. );
  65. }
  66. };
  67. const resolvePackageRootFromNodeModules = async ({ fromDirectory, packageName, root }) => {
  68. const packagePathSegments = packageName.split("/");
  69. for (const directory of ancestorDirectories(resolve(fromDirectory ?? root))) {
  70. const packageDirectory = resolve(directory, "node_modules", ...packagePathSegments);
  71. if (await exists(resolve(packageDirectory, "package.json"))) {
  72. return packageDirectory;
  73. }
  74. }
  75. return undefined;
  76. };
  77. function* ancestorDirectories(startDirectory) {
  78. let directory = startDirectory;
  79. while (true) {
  80. yield directory;
  81. const parentDirectory = dirname(directory);
  82. if (parentDirectory === directory) {
  83. return;
  84. }
  85. directory = parentDirectory;
  86. }
  87. }
  88. const findPackageRoot = async (entryPath, packageName) => {
  89. let directory = dirname(entryPath);
  90. while (directory !== dirname(directory)) {
  91. const packageJsonPath = resolve(directory, "package.json");
  92. if (await exists(packageJsonPath)) {
  93. const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
  94. if (packageJson.name === packageName) {
  95. return directory;
  96. }
  97. }
  98. directory = dirname(directory);
  99. }
  100. throw new Error(`Could not find package root for ${packageName} from ${entryPath}`);
  101. };
  102. const assertPackageDirectory = async (packageName, directory) => {
  103. if (!(await exists(resolve(directory, "package.json")))) {
  104. throw new Error(`Missing package.json for ${packageName} at ${directory}`);
  105. }
  106. };
  107. async function exists(path) {
  108. try {
  109. await access(path);
  110. return true;
  111. } catch (error) {
  112. if (error.code === "ENOENT") return false;
  113. throw error;
  114. }
  115. }