




























@@ -241,12 +241,14 @@ function collectInstalledRuntimeDependencyRoots(
241241rootNodeModulesDir,
242242dependencySpecs,
243243directDependencyPackageRoot = null,
244+optionalDependencyNames = new Set(),
244245) {
245246const packageCache = new Map();
246247const directRoots = [];
247248const allRoots = [];
248249const queue = Object.entries(dependencySpecs).map(([depName, spec]) => ({
249250 depName,
251+optional: optionalDependencyNames.has(depName),
250252 spec,
251253parentPackageRoot: directDependencyPackageRoot,
252254direct: true,
@@ -263,6 +265,9 @@ function collectInstalledRuntimeDependencyRoots(
263265 rootNodeModulesDir,
264266});
265267if (depRoot === null) {
268+if (current.optional) {
269+continue;
270+}
266271return null;
267272}
268273const canonicalDepRoot = fs.realpathSync(depRoot);
@@ -285,6 +290,7 @@ function collectInstalledRuntimeDependencyRoots(
285290for (const [childName, childSpec] of Object.entries(packageJson.dependencies ?? {})) {
286291queue.push({
287292depName: childName,
293+optional: false,
288294spec: childSpec,
289295parentPackageRoot: depRoot,
290296direct: false,
@@ -293,6 +299,7 @@ function collectInstalledRuntimeDependencyRoots(
293299for (const [childName, childSpec] of Object.entries(packageJson.optionalDependencies ?? {})) {
294300queue.push({
295301depName: childName,
302+optional: true,
296303spec: childSpec,
297304parentPackageRoot: depRoot,
298305direct: false,
@@ -391,6 +398,7 @@ function resolveInstalledDirectDependencyNames(
391398rootNodeModulesDir,
392399dependencySpecs,
393400directDependencyPackageRoot = null,
401+optionalDependencyNames = new Set(),
394402) {
395403const directDependencyNames = [];
396404for (const [depName, spec] of Object.entries(dependencySpecs)) {
@@ -401,6 +409,9 @@ function resolveInstalledDirectDependencyNames(
401409 rootNodeModulesDir,
402410});
403411if (depRoot === null) {
412+if (optionalDependencyNames.has(depName)) {
413+continue;
414+}
404415return null;
405416}
406417const installedVersion = readInstalledDependencyVersionFromRoot(depRoot);
@@ -463,6 +474,7 @@ function resolveInstalledRuntimeClosureFingerprint(params) {
463474params.rootNodeModulesDir,
464475dependencySpecs,
465476params.directDependencyPackageRoot,
477+new Set(Object.keys(params.packageJson.optionalDependencies ?? {})),
466478);
467479if (resolution === null) {
468480return null;
@@ -890,6 +902,7 @@ function stageInstalledRootRuntimeDeps(params) {
890902 ...packageJson.dependencies,
891903 ...packageJson.optionalDependencies,
892904};
905+const optionalDependencyNames = new Set(Object.keys(packageJson.optionalDependencies ?? {}));
893906const rootNodeModulesDir = path.join(repoRoot, "node_modules");
894907if (Object.keys(dependencySpecs).length === 0 || !fs.existsSync(rootNodeModulesDir)) {
895908return false;
@@ -899,6 +912,7 @@ function stageInstalledRootRuntimeDeps(params) {
899912rootNodeModulesDir,
900913dependencySpecs,
901914directDependencyPackageRoot,
915+optionalDependencyNames,
902916);
903917if (directDependencyNames === null) {
904918return false;
@@ -907,15 +921,25 @@ function stageInstalledRootRuntimeDeps(params) {
907921rootNodeModulesDir,
908922dependencySpecs,
909923directDependencyPackageRoot,
924+optionalDependencyNames,
910925);
911926if (resolution === null) {
912927return false;
913928}
914929const rootsToCopy = selectRuntimeDependencyRootsToCopy(resolution);
915-const allowedRealRoots = rootsToCopy.map((record) => record.realRoot);
916-917930const nodeModulesDir = path.join(pluginDir, "node_modules");
918931const stampPath = resolveRuntimeDepsStampPath(pluginDir);
932+if (rootsToCopy.length === 0) {
933+assertPathIsNotSymlink(nodeModulesDir, "remove runtime deps");
934+removePathIfExists(nodeModulesDir);
935+writeJsonAtomically(stampPath, {
936+ fingerprint,
937+generatedAt: new Date().toISOString(),
938+});
939+return true;
940+}
941+const allowedRealRoots = rootsToCopy.map((record) => record.realRoot);
942+919943const stagedNodeModulesDir = path.join(
920944makePluginOwnedTempDir(pluginDir, "stage"),
921945"node_modules",
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。