Skip to content

Commit

Permalink
Resolver perf 1/n: Optimise package exports flattenLegacySubpaths
Browse files Browse the repository at this point in the history
Differential Revision: D56701232
  • Loading branch information
robhogan authored and facebook-github-bot committed Aug 5, 2024
1 parent fbd16fe commit ec8cf16
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/metro-resolver/src/PackageExportsResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,23 @@ function flattenLegacySubpathValues(
exportMap: ExportMap | ExportMapWithFallbacks,
createConfigError: (reason: string) => Error,
): ExportMap {
return Object.keys(exportMap).reduce((result: ExportMap, subpath: string) => {
const value = exportMap[subpath];

// We do not support empty or nested arrays (non-standard)
if (Array.isArray(value) && (!value.length || Array.isArray(value[0]))) {
throw createConfigError(
'Could not parse non-standard array value in "exports" field.',
);
}

return {
...result,
[subpath]: Array.isArray(value) ? value[0] : value,
};
}, {});
return Object.entries(exportMap).reduce(
(result, [subpath, value]) => {
// We do not support empty or nested arrays (non-standard)
if (Array.isArray(value)) {
if (!value.length || Array.isArray(value[0])) {
throw createConfigError(
'Could not parse non-standard array value in "exports" field.',
);
}
result[subpath] = value[0];
} else {
result[subpath] = value;
}
return result;
},
{} as {[subpathOrCondition: string]: string | ExportMap | null},
);
}

/**
Expand Down

0 comments on commit ec8cf16

Please sign in to comment.