runtime-dependency-closure.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { existsSync, readFileSync } from "node:fs";
  2. import { dirname, parse, resolve } from "node:path";
  3. import { createRequire } from "node:module";
  4. function findPackageRoot(entryPath) {
  5. let currentDir = dirname(entryPath);
  6. const root = parse(currentDir).root;
  7. while (currentDir !== root) {
  8. const packageJsonPath = resolve(currentDir, "package.json");
  9. if (existsSync(packageJsonPath)) {
  10. return currentDir;
  11. }
  12. currentDir = dirname(currentDir);
  13. }
  14. return null;
  15. }
  16. function readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath = null) {
  17. if (parentPackagePath) {
  18. try {
  19. const requireFromParent = createRequire(parentPackagePath);
  20. const entryPath = requireFromParent.resolve(moduleName);
  21. const packageRoot = findPackageRoot(entryPath);
  22. if (packageRoot) {
  23. const packageJsonPath = resolve(packageRoot, "package.json");
  24. return {
  25. packageJson: JSON.parse(readFileSync(packageJsonPath, "utf8")),
  26. packageJsonPath,
  27. packageRoot,
  28. };
  29. }
  30. } catch {
  31. // 父包相对解析失败时,继续走 workspace lookup roots 兜底。
  32. }
  33. }
  34. for (const lookupRoot of moduleLookupRoots) {
  35. const packageJsonPath = resolve(lookupRoot, "node_modules", moduleName, "package.json");
  36. if (!existsSync(packageJsonPath)) {
  37. continue;
  38. }
  39. return {
  40. packageJson: JSON.parse(readFileSync(packageJsonPath, "utf8")),
  41. packageJsonPath,
  42. packageRoot: dirname(packageJsonPath),
  43. };
  44. }
  45. return null;
  46. }
  47. export function collectRuntimeModuleClosure(moduleNames, moduleLookupRoots) {
  48. return collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots).map(
  49. (entry) => entry.moduleName,
  50. );
  51. }
  52. export function collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots) {
  53. const collected = [];
  54. const visited = new Set();
  55. function visit(moduleName, optional = false, parentPackagePath = null) {
  56. if (visited.has(moduleName)) {
  57. return;
  58. }
  59. const runtimePackage = readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath);
  60. if (!runtimePackage && optional) {
  61. return;
  62. }
  63. visited.add(moduleName);
  64. collected.push({
  65. moduleName,
  66. sourceModulePath: runtimePackage?.packageRoot ?? null,
  67. packageJsonPath: runtimePackage?.packageJsonPath ?? null,
  68. });
  69. if (!runtimePackage) {
  70. return;
  71. }
  72. const { packageJson, packageJsonPath } = runtimePackage;
  73. const dependencies = packageJson.dependencies ?? {};
  74. const optionalDependencies = packageJson.optionalDependencies ?? {};
  75. const dependencyEntries = [
  76. ...Object.keys(dependencies).map((dependencyName) => [dependencyName, false]),
  77. ...Object.keys(optionalDependencies).map((dependencyName) => [dependencyName, true]),
  78. ];
  79. for (const [dependencyName, isOptional] of dependencyEntries.sort(([left], [right]) =>
  80. left.localeCompare(right),
  81. )) {
  82. // 运行时外置包进了 app.asar 时,它的 hoisted 子依赖不会自动跟着进包。
  83. // 递归收集 dependencies,让打包注入和产物校验覆盖完整运行时解析链。
  84. // pnpm 多版本同名依赖下,不能每层都从固定 lookup roots 取第一个目录。
  85. // 例如 yazl 需要 buffer-crc32@1.x,而 desktop 测试依赖里还有 0.2.x;
  86. // 必须从父包 package.json 相对解析,才能复制到真实运行时会加载的版本。
  87. visit(dependencyName, isOptional, packageJsonPath);
  88. }
  89. }
  90. for (const moduleName of moduleNames) {
  91. visit(moduleName);
  92. }
  93. return collected;
  94. }