|
@@ -2,12 +2,31 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
import { dirname, parse, resolve } from "node:path";
|
|
import { dirname, parse, resolve } from "node:path";
|
|
|
import { createRequire } from "node:module";
|
|
import { createRequire } from "node:module";
|
|
|
|
|
|
|
|
|
|
+const NODE_MODULES_DIR = "node_modules";
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 读取 package.json 的 name 字段。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 修复点:`long` 这类包的 main 指向子目录(`umd/index.js`),而该子目录下另有一个
|
|
|
|
|
+ * 只声明 `{"type":"commonjs"}` 的标记型 package.json(没有 name)。
|
|
|
|
|
+ * 旧的 findPackageRoot 遇到第一个 package.json 就返回,会把 `long/umd` 当成包根,
|
|
|
|
|
+ * 注入到 app.asar 后 `require("long")` 直接解析失败。这里要求候选包根必须声明 name。
|
|
|
|
|
+ */
|
|
|
|
|
+function readPackageName(packageJsonPath) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const parsed = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
|
|
|
+ return typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : null;
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function findPackageRoot(entryPath) {
|
|
function findPackageRoot(entryPath) {
|
|
|
let currentDir = dirname(entryPath);
|
|
let currentDir = dirname(entryPath);
|
|
|
const root = parse(currentDir).root;
|
|
const root = parse(currentDir).root;
|
|
|
while (currentDir !== root) {
|
|
while (currentDir !== root) {
|
|
|
const packageJsonPath = resolve(currentDir, "package.json");
|
|
const packageJsonPath = resolve(currentDir, "package.json");
|
|
|
- if (existsSync(packageJsonPath)) {
|
|
|
|
|
|
|
+ if (existsSync(packageJsonPath) && readPackageName(packageJsonPath)) {
|
|
|
return currentDir;
|
|
return currentDir;
|
|
|
}
|
|
}
|
|
|
currentDir = dirname(currentDir);
|
|
currentDir = dirname(currentDir);
|
|
@@ -19,14 +38,28 @@ function readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath = n
|
|
|
if (parentPackagePath) {
|
|
if (parentPackagePath) {
|
|
|
try {
|
|
try {
|
|
|
const requireFromParent = createRequire(parentPackagePath);
|
|
const requireFromParent = createRequire(parentPackagePath);
|
|
|
- const entryPath = requireFromParent.resolve(moduleName);
|
|
|
|
|
- const packageRoot = findPackageRoot(entryPath);
|
|
|
|
|
- if (packageRoot) {
|
|
|
|
|
- const packageJsonPath = resolve(packageRoot, "package.json");
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 优先直接解析 `<module>/package.json`:这是 Node 定位包根的官方方式,
|
|
|
|
|
+ // 不受 main/exports 指向子目录的影响(见上面 long 的说明)。
|
|
|
|
|
+ // 少数包用 exports 屏蔽了 `./package.json`,这时再退回按入口文件向上找。
|
|
|
|
|
+ let packageJsonPath = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ packageJsonPath = requireFromParent.resolve(`${moduleName}/package.json`);
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ packageJsonPath = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!packageJsonPath) {
|
|
|
|
|
+ const packageRoot = findPackageRoot(requireFromParent.resolve(moduleName));
|
|
|
|
|
+ if (packageRoot) {
|
|
|
|
|
+ packageJsonPath = resolve(packageRoot, "package.json");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (packageJsonPath && existsSync(packageJsonPath)) {
|
|
|
return {
|
|
return {
|
|
|
packageJson: JSON.parse(readFileSync(packageJsonPath, "utf8")),
|
|
packageJson: JSON.parse(readFileSync(packageJsonPath, "utf8")),
|
|
|
packageJsonPath,
|
|
packageJsonPath,
|
|
|
- packageRoot,
|
|
|
|
|
|
|
+ packageRoot: dirname(packageJsonPath),
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
} catch {
|
|
} catch {
|
|
@@ -49,60 +82,191 @@ function readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath = n
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function collectRuntimeModuleClosure(moduleNames, moduleLookupRoots) {
|
|
|
|
|
- return collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots).map(
|
|
|
|
|
- (entry) => entry.moduleName,
|
|
|
|
|
- );
|
|
|
|
|
|
|
+function listDependencyEntries(packageJson) {
|
|
|
|
|
+ const dependencies = packageJson.dependencies ?? {};
|
|
|
|
|
+ const optionalDependencies = packageJson.optionalDependencies ?? {};
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ ...Object.keys(dependencies).map((dependencyName) => [dependencyName, false]),
|
|
|
|
|
+ ...Object.keys(optionalDependencies).map((dependencyName) => [dependencyName, true]),
|
|
|
|
|
+ ].sort(([left], [right]) => left.localeCompare(right));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** 路径里 `node_modules` 段的个数越多,说明这份副本嵌套得越深。 */
|
|
|
|
|
+function nestingDepth(packageRoot) {
|
|
|
|
|
+ return packageRoot.split(/[\\/]/).filter((segment) => segment === NODE_MODULES_DIR).length;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 收集运行时依赖闭包,并给出每一项在 app.asar 内应该落到的相对路径。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 返回的每一项形如:
|
|
|
|
|
+ * { moduleName, sourceModulePath, packageJsonPath, installRelativePath }
|
|
|
|
|
+ * 其中 installRelativePath 以 `node_modules/` 开头、使用正斜杠、相对 app.asar 根。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 修复点(产物启动即 Cannot find module 的根因):
|
|
|
|
|
+ * 1. 去重键从「模块名」改成「解析出的包根」。旧实现用 visited.has(moduleName),
|
|
|
|
|
+ * 会把同名不同版本压成一条:yazl 需要 buffer-crc32@^1.0.0、yauzl 需要 ~0.2.3,
|
|
|
|
|
+ * 结果只保留先访问到的那个版本,另一个永远不会被注入。
|
|
|
|
|
+ * 2. 注入时不能把每个包都拍平到 node_modules/<name>。多版本必须按依赖它的父包
|
|
|
|
|
+ * 就近落成 node_modules/<parent>/node_modules/<name>,否则后者会覆盖前者,
|
|
|
|
|
+ * Node 解析链仍然拿不到正确版本。
|
|
|
|
|
+ */
|
|
|
export function collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots) {
|
|
export function collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots) {
|
|
|
- const collected = [];
|
|
|
|
|
- const visited = new Set();
|
|
|
|
|
|
|
+ const nodes = new Map(); // packageRoot -> { moduleName, sourceModulePath, packageJsonPath }
|
|
|
|
|
+ const childByParent = new Map(); // parentPackageRoot -> Map<childPackageRoot, dependencyName>
|
|
|
|
|
+ const rootsOfName = new Map(); // moduleName -> Set<packageRoot>
|
|
|
|
|
+ const unresolvedEntries = [];
|
|
|
|
|
|
|
|
- function visit(moduleName, optional = false, parentPackagePath = null) {
|
|
|
|
|
- if (visited.has(moduleName)) {
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ const registerName = (moduleName, packageRoot) => {
|
|
|
|
|
+ if (!rootsOfName.has(moduleName)) {
|
|
|
|
|
+ rootsOfName.set(moduleName, new Set());
|
|
|
}
|
|
}
|
|
|
|
|
+ rootsOfName.get(moduleName).add(packageRoot);
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
|
|
+ const queue = moduleNames.map((moduleName) => ({
|
|
|
|
|
+ moduleName,
|
|
|
|
|
+ parentPackagePath: null,
|
|
|
|
|
+ parentPackageRoot: null,
|
|
|
|
|
+ optional: false,
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ while (queue.length > 0) {
|
|
|
|
|
+ const { moduleName, parentPackagePath, parentPackageRoot, optional } = queue.shift();
|
|
|
const runtimePackage = readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath);
|
|
const runtimePackage = readRuntimePackage(moduleLookupRoots, moduleName, parentPackagePath);
|
|
|
- if (!runtimePackage && optional) {
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (!runtimePackage) {
|
|
|
|
|
+ if (!optional) {
|
|
|
|
|
+ // 保持旧行为:不可解析的非可选依赖仍产出一条 sourceModulePath=null 的记录,
|
|
|
|
|
+ // 交给调用方告警并跳过,而不是在这里中断整个平台的出包。
|
|
|
|
|
+ unresolvedEntries.push({
|
|
|
|
|
+ moduleName,
|
|
|
|
|
+ sourceModulePath: null,
|
|
|
|
|
+ packageJsonPath: null,
|
|
|
|
|
+ installRelativePath: `${NODE_MODULES_DIR}/${moduleName}`,
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- visited.add(moduleName);
|
|
|
|
|
- collected.push({
|
|
|
|
|
|
|
+ const packageRoot = runtimePackage.packageRoot;
|
|
|
|
|
+ registerName(moduleName, packageRoot);
|
|
|
|
|
+
|
|
|
|
|
+ if (parentPackageRoot !== null) {
|
|
|
|
|
+ if (!childByParent.has(parentPackageRoot)) {
|
|
|
|
|
+ childByParent.set(parentPackageRoot, new Map());
|
|
|
|
|
+ }
|
|
|
|
|
+ childByParent.get(parentPackageRoot).set(packageRoot, moduleName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (nodes.has(packageRoot)) {
|
|
|
|
|
+ // 同一个物理包只展开一次依赖,避免环状依赖无限递归。
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ nodes.set(packageRoot, {
|
|
|
moduleName,
|
|
moduleName,
|
|
|
- sourceModulePath: runtimePackage?.packageRoot ?? null,
|
|
|
|
|
- packageJsonPath: runtimePackage?.packageJsonPath ?? null,
|
|
|
|
|
|
|
+ sourceModulePath: packageRoot,
|
|
|
|
|
+ packageJsonPath: runtimePackage.packageJsonPath,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- if (!runtimePackage) {
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ for (const [dependencyName, isOptional] of listDependencyEntries(runtimePackage.packageJson)) {
|
|
|
|
|
+ queue.push({
|
|
|
|
|
+ moduleName: dependencyName,
|
|
|
|
|
+ parentPackagePath: runtimePackage.packageJsonPath,
|
|
|
|
|
+ parentPackageRoot: packageRoot,
|
|
|
|
|
+ optional: isOptional,
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 决定哪些包「提升」到 node_modules/<name>:
|
|
|
|
|
+ // - 同名只解析出一个物理版本 → 提升(与旧行为一致,落位不变)。
|
|
|
|
|
+ // - 同名解析出多个版本 → 只提升嵌套最浅的那个,其余版本按父包就近嵌套。
|
|
|
|
|
+ const placements = new Map(); // packageRoot -> Set<installRelativePath>
|
|
|
|
|
+ const hoistedRoots = new Set();
|
|
|
|
|
|
|
|
- const { packageJson, packageJsonPath } = runtimePackage;
|
|
|
|
|
- const dependencies = packageJson.dependencies ?? {};
|
|
|
|
|
- const optionalDependencies = packageJson.optionalDependencies ?? {};
|
|
|
|
|
- const dependencyEntries = [
|
|
|
|
|
- ...Object.keys(dependencies).map((dependencyName) => [dependencyName, false]),
|
|
|
|
|
- ...Object.keys(optionalDependencies).map((dependencyName) => [dependencyName, true]),
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- for (const [dependencyName, isOptional] of dependencyEntries.sort(([left], [right]) =>
|
|
|
|
|
- left.localeCompare(right),
|
|
|
|
|
- )) {
|
|
|
|
|
- // 运行时外置包进了 app.asar 时,它的 hoisted 子依赖不会自动跟着进包。
|
|
|
|
|
- // 递归收集 dependencies,让打包注入和产物校验覆盖完整运行时解析链。
|
|
|
|
|
- // pnpm 多版本同名依赖下,不能每层都从固定 lookup roots 取第一个目录。
|
|
|
|
|
- // 例如 yazl 需要 buffer-crc32@1.x,而 desktop 测试依赖里还有 0.2.x;
|
|
|
|
|
- // 必须从父包 package.json 相对解析,才能复制到真实运行时会加载的版本。
|
|
|
|
|
- visit(dependencyName, isOptional, packageJsonPath);
|
|
|
|
|
|
|
+ const addPlacement = (packageRoot, installRelativePath) => {
|
|
|
|
|
+ if (!placements.has(packageRoot)) {
|
|
|
|
|
+ placements.set(packageRoot, new Set());
|
|
|
}
|
|
}
|
|
|
|
|
+ placements.get(packageRoot).add(installRelativePath);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ for (const [moduleName, packageRoots] of rootsOfName) {
|
|
|
|
|
+ const sorted = [...packageRoots].sort(
|
|
|
|
|
+ (left, right) => nestingDepth(left) - nestingDepth(right) || left.localeCompare(right),
|
|
|
|
|
+ );
|
|
|
|
|
+ const hoistedRoot = sorted[0];
|
|
|
|
|
+ hoistedRoots.add(hoistedRoot);
|
|
|
|
|
+ addPlacement(hoistedRoot, `${NODE_MODULES_DIR}/${moduleName}`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- for (const moduleName of moduleNames) {
|
|
|
|
|
- visit(moduleName);
|
|
|
|
|
|
|
+ // 从提升节点出发,沿依赖边给未提升的包推导嵌套落位。
|
|
|
|
|
+ const pending = [...hoistedRoots].map((packageRoot) => ({
|
|
|
|
|
+ packageRoot,
|
|
|
|
|
+ installRelativePath: [...placements.get(packageRoot)][0],
|
|
|
|
|
+ }));
|
|
|
|
|
+ const visitedPlacements = new Set();
|
|
|
|
|
+
|
|
|
|
|
+ while (pending.length > 0) {
|
|
|
|
|
+ const { packageRoot, installRelativePath } = pending.shift();
|
|
|
|
|
+ const visitKey = `${packageRoot}\u0000${installRelativePath}`;
|
|
|
|
|
+ if (visitedPlacements.has(visitKey)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ visitedPlacements.add(visitKey);
|
|
|
|
|
+
|
|
|
|
|
+ const children = childByParent.get(packageRoot);
|
|
|
|
|
+ if (!children) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (const [childPackageRoot, dependencyName] of children) {
|
|
|
|
|
+ const childNestedPath = `${installRelativePath}/${NODE_MODULES_DIR}/${dependencyName}`;
|
|
|
|
|
+ const childInstallPath = hoistedRoots.has(childPackageRoot)
|
|
|
|
|
+ ? [...placements.get(childPackageRoot)][0]
|
|
|
|
|
+ : childNestedPath;
|
|
|
|
|
+ if (!hoistedRoots.has(childPackageRoot)) {
|
|
|
|
|
+ addPlacement(childPackageRoot, childNestedPath);
|
|
|
|
|
+ }
|
|
|
|
|
+ pending.push({ packageRoot: childPackageRoot, installRelativePath: childInstallPath });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return collected;
|
|
|
|
|
|
|
+ const entries = [];
|
|
|
|
|
+ for (const [packageRoot, node] of nodes) {
|
|
|
|
|
+ const packagePlacements = placements.get(packageRoot);
|
|
|
|
|
+ if (!packagePlacements || packagePlacements.size === 0) {
|
|
|
|
|
+ // 兜底:理论上不该发生(每个节点都能沿父链回溯到某个提升节点),
|
|
|
|
|
+ // 真遇到就退回旧的拍平落位,保证不会静默漏包。
|
|
|
|
|
+ entries.push({ ...node, installRelativePath: `${NODE_MODULES_DIR}/${node.moduleName}` });
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (const installRelativePath of packagePlacements) {
|
|
|
|
|
+ entries.push({ ...node, installRelativePath });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ entries.sort(
|
|
|
|
|
+ (left, right) =>
|
|
|
|
|
+ left.moduleName.localeCompare(right.moduleName) ||
|
|
|
|
|
+ left.installRelativePath.localeCompare(right.installRelativePath),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ return [...entries, ...unresolvedEntries];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function collectRuntimeModuleClosure(moduleNames, moduleLookupRoots) {
|
|
|
|
|
+ const seen = new Set();
|
|
|
|
|
+ const names = [];
|
|
|
|
|
+ for (const entry of collectRuntimeModuleClosureEntries(moduleNames, moduleLookupRoots)) {
|
|
|
|
|
+ if (seen.has(entry.moduleName)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ seen.add(entry.moduleName);
|
|
|
|
|
+ names.push(entry.moduleName);
|
|
|
|
|
+ }
|
|
|
|
|
+ return names;
|
|
|
}
|
|
}
|