Skip to content

Commit

Permalink
fix: logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 17, 2023
1 parent f614707 commit f1fbd81
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/util/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports.processExportsField = function processExportsField(
exportsField
) {
return createFieldProcessor(
buildExportsFieldPathTree(exportsField),
buildExportsField(exportsField),
assertExportsFieldRequest,
assertExportTarget
);
Expand All @@ -98,7 +98,7 @@ module.exports.processImportsField = function processImportsField(
importsField
) {
return createFieldProcessor(
buildImportsFieldPathTree(importsField),
buildImportsField(importsField),
assertImportsFieldRequest,
assertImportTarget
);
Expand Down Expand Up @@ -295,15 +295,25 @@ function findMatch(request, field) {
request.length - patternTrailer.length
);
}
} else if (
// For `./foo/` or `./`
}
// For legacy `./foo/`
else if (
key[key.length - 1] === "/" &&
request.startsWith(key) &&
patternKeyCompare(bestMatch, key) === 1
) {
bestMatch = key;
bestMatchSubpath = request.slice(key.length);
}
// For legacy `./`
else if (
key === "./" &&
patternKeyCompare(bestMatch, key) === 1 &&
request.length > 0
) {
bestMatch = key;
bestMatchSubpath = request.slice(key.length - 2);
}
}

if (bestMatch === "") return null;
Expand Down Expand Up @@ -458,7 +468,7 @@ function conditionalMapping(conditionalMapping_, conditionNames) {
* @param {ExportsField} field exports field
* @returns {ExportsField} normalized exports field
*/
function buildExportsFieldPathTree(field) {
function buildExportsField(field) {
// handle syntax sugar, if exports field is direct mapping for "."
if (typeof field === "string" || Array.isArray(field)) {
return { "": field };
Expand Down Expand Up @@ -509,7 +519,8 @@ function buildExportsFieldPathTree(field) {
);
}

newField[key.slice(2)] = field[key];
// Keep "./" for legacy `{ "./": "./" }`
newField[key.slice(2) || "./"] = field[key];
}

return newField;
Expand All @@ -519,7 +530,7 @@ function buildExportsFieldPathTree(field) {
* @param {ImportsField} field imports field
* @returns {ImportsField} normalized imports field
*/
function buildImportsFieldPathTree(field) {
function buildImportsField(field) {
const keys = Object.keys(field);
const newField = /** @type {ImportsField} */ ({});

Expand Down

0 comments on commit f1fbd81

Please sign in to comment.