diff --git a/lib/util/entrypoints.js b/lib/util/entrypoints.js index 85f1f7c9..4b8f5a0e 100644 --- a/lib/util/entrypoints.js +++ b/lib/util/entrypoints.js @@ -84,7 +84,7 @@ module.exports.processExportsField = function processExportsField( exportsField ) { return createFieldProcessor( - buildExportsFieldPathTree(exportsField), + buildExportsField(exportsField), assertExportsFieldRequest, assertExportTarget ); @@ -98,7 +98,7 @@ module.exports.processImportsField = function processImportsField( importsField ) { return createFieldProcessor( - buildImportsFieldPathTree(importsField), + buildImportsField(importsField), assertImportsFieldRequest, assertImportTarget ); @@ -295,8 +295,9 @@ 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 @@ -304,6 +305,15 @@ function findMatch(request, field) { 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; @@ -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 }; @@ -509,7 +519,8 @@ function buildExportsFieldPathTree(field) { ); } - newField[key.slice(2)] = field[key]; + // Keep "./" for legacy `{ "./": "./" }` + newField[key.slice(2) || "./"] = field[key]; } return newField; @@ -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} */ ({});