Skip to content

Commit

Permalink
update impl and test
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent 0872a0f commit a76d6bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
28 changes: 15 additions & 13 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,25 @@ function _findSubpath(subpath: string, exports: PackageJson["exports"]) {
return subpath;
}

const flattenedExports = _flattenExports(exports);
const [foundPath] =
// eslint-disable-next-line @typescript-eslint/no-unused-vars
flattenedExports.find(([_, resolved]) => resolved === subpath) || [];

return foundPath;
const _flatExports = _flattenExports(exports);
console.log(subpath, "in", _flatExports);
return _flatExports.find((p) => p.fsPath === subpath)?.subpath;
}

function _flattenExports(
exports: Exclude<PackageJson["exports"], string>,
path?: string,
) {
parentSubpath = "./",
): { subpath: string; fsPath: string; condition?: string }[] {
return Object.entries(exports).flatMap(([key, value]) => {
const subpath =
path ?? (key.startsWith(".") ? key : "./"); /* key is export condition */
return typeof value === "string"
? [[subpath, value]]
: _flattenExports(value, subpath);
const [subpath, condition] = key.startsWith(".")
? [key.slice(1), undefined]
: [undefined, key];
const _subPath = joinURL(parentSubpath, subpath);
// eslint-disable-next-line unicorn/prefer-ternary
if (typeof value === "string") {
return [{ subpath: _subPath, fsPath: value, condition }];
} else {
return _flattenExports(value, _subPath);
}
});
}
7 changes: 6 additions & 1 deletion test/fixture/package/node_modules/subpaths/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 21 additions & 13 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,36 +108,44 @@ describe("lookupNodeModuleSubpath", () => {
const r = (p: string) => new URL(p, import.meta.url).toString();

const tests = [
// Resolve with exports
{
name: "resolves with exports field",
input: r("fixture/package/node_modules/subpaths/lib/subpath.mjs"),
name: "resolves with exports field (root)",
input: r("fixture/package/node_modules/subpaths/dist/index.mjs"),
output: "./",
},
{
name: "resolves with exports field (subpath)",
input: r("fixture/package/node_modules/subpaths/dist/subpath.mjs"),
output: "./subpath",
},
{
name: "resolves with fallback subpath guess (non resolved module)",
name: "resolves with exports field (with conditions)",
input: r("fixture/package/node_modules/postgres/src/index.js"),
output: "./",
},
// Fallbacks
{
name: "resolves with fallback (non resolved module)",
input: r("fixture/package/node_modules/alien/lib/subpath.json5"),
output: "./lib/subpath.json5",
},
{
name: "resolves with fallback subpath guess (non existing file)",
input: r("fixture/package/node_modules/subpaths/foo/bar.mjs"),
output: "./foo/bar.mjs",
},
// Invalid
{
name: "ignores invalid paths",
input: r("/foo/bar/lib/subpath.mjs"),
output: undefined,
},
{
name: "resolves with fallback subpath guess (mjs)",
input: r("fixture/package/node_modules/subpaths/foo/bar.mjs"),
output: "./foo/bar.mjs",
},
{
name: "resolves main export",
input: r("fixture/package/node_modules/subpaths/"),
output: "./",
},
{
name: "resolves main export (top level export conditions)",
input: r("fixture/package/node_modules/posgres/src/index.js"),
output: "./src/index.js", // TODO: Should be "./"
},
];

for (const t of tests) {
Expand Down

0 comments on commit a76d6bc

Please sign in to comment.