Skip to content

Commit

Permalink
pref: reduce memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 18, 2023
1 parent d8176da commit 91edec1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
40 changes: 14 additions & 26 deletions lib/util/entrypoints.js
Expand Up @@ -86,7 +86,7 @@ module.exports.processExportsField = function processExportsField(
) {
return createFieldProcessor(
buildExportsField(exportsField),
"",
request => (request.length === 0 ? "." : "./" + request),
assertExportsFieldRequest,
assertExportTarget
);
Expand All @@ -101,24 +101,29 @@ module.exports.processImportsField = function processImportsField(
) {
return createFieldProcessor(
buildImportsField(importsField),
"#",
request => "#" + request,
assertImportsFieldRequest,
assertImportTarget
);
};

/**
* @param {ExportsField | ImportsField} field root
* @param {string} prefix Request prefix, for `imports` field it is `#`, for `exports` field it is `./`.
* @param {(s: string) => string} normalizeRequest Normalize request, for `imports` field it adds `#`, for `exports` field it adds `.` or `./`
* @param {(s: string) => string} assertRequest assertRequest
* @param {(s: string, f: boolean) => void} assertTarget assertTarget
* @returns {FieldProcessor} field processor
*/
function createFieldProcessor(field, prefix, assertRequest, assertTarget) {
function createFieldProcessor(
field,
normalizeRequest,
assertRequest,
assertTarget
) {
return function fieldProcessor(request, conditionNames) {
request = assertRequest(request);

const match = findMatch(prefix + request, field);
const match = findMatch(normalizeRequest(request), field);

if (match === null) return [];

Expand Down Expand Up @@ -270,10 +275,8 @@ function findMatch(request, field) {
!request.endsWith("/")
) {
const target = field[request];
const isSubpathMapping =
typeof target === "string" ? target.endsWith("/") : false;

return [target, "", isSubpathMapping, false];
return [target, "", false, false];
}

let bestMatch = "";
Expand Down Expand Up @@ -310,15 +313,6 @@ 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;
Expand Down Expand Up @@ -512,10 +506,9 @@ function conditionalMapping(conditionalMapping_, conditionNames) {
function buildExportsField(field) {
// handle syntax sugar, if exports field is direct mapping for "."
if (typeof field === "string" || Array.isArray(field)) {
return { "": field };
return { ".": field };
}

const newField = /** @type {ExportsField} */ ({});
const keys = Object.keys(field);

for (let i = 0; i < keys.length; i++) {
Expand All @@ -536,7 +529,7 @@ function buildExportsField(field) {
i++;
}

return { "": field };
return { ".": field };
}

throw new Error(
Expand All @@ -547,8 +540,6 @@ function buildExportsField(field) {
}

if (key.length === 1) {
newField[""] = field[key];

continue;
}

Expand All @@ -559,12 +550,9 @@ function buildExportsField(field) {
)})`
);
}

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

return newField;
return field;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions test/exportsField.js
Expand Up @@ -594,6 +594,56 @@ describe("Process exports field", function exportsField() {
[]
]
},
{
name: "Direct mapping #11",
expect: ["./foo.js"],
suite: [
{
"./": "./",
"./*": "./*",
"./dist/index.js": "./dist/index.js"
},
"./foo.js",
[]
]
},
{
name: "Direct mapping #12",
expect: ["./foo/bar/baz.js"],
suite: [
{
"./": "./",
"./*": "./*",
"./dist/index.js": "./dist/index.js"
},
"./foo/bar/baz.js",
[]
]
},
{
name: "Direct mapping #13",
expect: ["./foo/bar/baz.js"],
suite: [
{
"./": "./",
"./dist/index.js": "./dist/index.js"
},
"./foo/bar/baz.js",
[]
]
},
{
name: "Direct mapping #14",
expect: ["./foo/bar/baz.js"],
suite: [
{
"./*": "./*",
"./dist/index.js": "./dist/index.js"
},
"./foo/bar/baz.js",
[]
]
},
//#endregion

//#region Direct and conditional mapping
Expand Down

0 comments on commit 91edec1

Please sign in to comment.