feat(core): optimize npm pruning path elevation function (#15880)

This commit is contained in:
Miroslav Jonaš 2023-03-27 17:15:35 +02:00 committed by GitHub
parent 15e29b2a3e
commit 1f935a7361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -568,10 +568,11 @@ function elevateNestedPaths(
sortedPaths.forEach((path) => { sortedPaths.forEach((path) => {
const segments = path.split('/node_modules/'); const segments = path.split('/node_modules/');
const mappedPackage = remappedPackages.get(path);
// we keep hoisted packages intact // we keep hoisted packages intact
if (segments.length === 1) { if (segments.length === 1) {
result.set(path, remappedPackages.get(path)); result.set(path, mappedPackage);
return; return;
} }
@ -581,13 +582,12 @@ function elevateNestedPaths(
// check if grandparent has the same package // check if grandparent has the same package
const shouldElevate = (segs: string[]) => { const shouldElevate = (segs: string[]) => {
const newPath = getNewPath(segs.slice(0, -1)); const elevatedPath = getNewPath(segs.slice(0, -1));
if (result.has(newPath)) { if (result.has(elevatedPath)) {
const match = result.get(newPath); const match = result.get(elevatedPath);
const source = remappedPackages.get(path);
return ( return (
match.valueV1?.version === source.valueV1?.version && match.valueV1?.version === mappedPackage.valueV1?.version &&
match.valueV3?.version === source.valueV3?.version match.valueV3?.version === mappedPackage.valueV3?.version
); );
} }
return true; return true;
@ -598,12 +598,12 @@ function elevateNestedPaths(
} }
const newPath = getNewPath(segments); const newPath = getNewPath(segments);
if (path !== newPath) { if (path !== newPath) {
result.set(newPath, { if (!result.has(newPath)) {
...remappedPackages.get(path), mappedPackage.path = newPath;
path: newPath, result.set(newPath, mappedPackage);
}); }
} else { } else {
result.set(path, remappedPackages.get(path)); result.set(path, mappedPackage);
} }
}); });