From 1f935a7361f9c1831fc8abb4e97371cae2bcb8b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Mon, 27 Mar 2023 17:15:35 +0200 Subject: [PATCH] feat(core): optimize npm pruning path elevation function (#15880) --- packages/nx/src/lock-file/npm-parser.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/nx/src/lock-file/npm-parser.ts b/packages/nx/src/lock-file/npm-parser.ts index 7000c79c07..f094f4020b 100644 --- a/packages/nx/src/lock-file/npm-parser.ts +++ b/packages/nx/src/lock-file/npm-parser.ts @@ -568,10 +568,11 @@ function elevateNestedPaths( sortedPaths.forEach((path) => { const segments = path.split('/node_modules/'); + const mappedPackage = remappedPackages.get(path); // we keep hoisted packages intact if (segments.length === 1) { - result.set(path, remappedPackages.get(path)); + result.set(path, mappedPackage); return; } @@ -581,13 +582,12 @@ function elevateNestedPaths( // check if grandparent has the same package const shouldElevate = (segs: string[]) => { - const newPath = getNewPath(segs.slice(0, -1)); - if (result.has(newPath)) { - const match = result.get(newPath); - const source = remappedPackages.get(path); + const elevatedPath = getNewPath(segs.slice(0, -1)); + if (result.has(elevatedPath)) { + const match = result.get(elevatedPath); return ( - match.valueV1?.version === source.valueV1?.version && - match.valueV3?.version === source.valueV3?.version + match.valueV1?.version === mappedPackage.valueV1?.version && + match.valueV3?.version === mappedPackage.valueV3?.version ); } return true; @@ -598,12 +598,12 @@ function elevateNestedPaths( } const newPath = getNewPath(segments); if (path !== newPath) { - result.set(newPath, { - ...remappedPackages.get(path), - path: newPath, - }); + if (!result.has(newPath)) { + mappedPackage.path = newPath; + result.set(newPath, mappedPackage); + } } else { - result.set(path, remappedPackages.get(path)); + result.set(path, mappedPackage); } });