Skip to content

Commit

Permalink
feat: package subpath extension searching for both cjs and non-export…
Browse files Browse the repository at this point in the history
…s resolution (#359)
  • Loading branch information
guybedford committed Jun 3, 2024
1 parent d2a4ac2 commit f52f463
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
37 changes: 32 additions & 5 deletions src/trace/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export class Resolver {
async finalizeResolve(
url: string,
parentIsCjs: boolean,
exportsResolution: boolean,
pkgUrl: `${string}/`
): Promise<string> {
if (parentIsCjs && url.endsWith("/")) url = url.slice(0, -1);
Expand All @@ -380,23 +381,27 @@ export class Resolver {
return this.finalizeResolve(
await legacyMainResolve.call(this, pcfg.browser, new URL(url)),
parentIsCjs,
exportsResolution,
pkgUrl
);
if (this.env.includes("module") && typeof pcfg.module === "string")
return this.finalizeResolve(
await legacyMainResolve.call(this, pcfg.module, new URL(url)),
parentIsCjs,
exportsResolution,
pkgUrl
);
if (typeof pcfg.main === "string")
return this.finalizeResolve(
await legacyMainResolve.call(this, pcfg.main, new URL(url)),
parentIsCjs,
exportsResolution,
pkgUrl
);
return this.finalizeResolve(
await legacyMainResolve.call(this, null, new URL(url)),
parentIsCjs,
exportsResolution,
pkgUrl
);
}
Expand Down Expand Up @@ -432,13 +437,22 @@ export class Resolver {
return await this.finalizeResolve(
pkgUrl + target.slice(2),
parentIsCjs,
exportsResolution,
pkgUrl
);
}
}
}
}

// give some compatibility for packages without "exports" field
if (!exportsResolution) {
if (await this.exists(url)) void 0;
else if (await this.exists(url + ".js")) return url + ".js";
else if (await this.exists(url + ".json")) return url + ".json";
else if (await this.exists(url + ".node")) return url + ".node";
}

return url;
}

Expand All @@ -464,7 +478,7 @@ export class Resolver {
for (const curTarget of targets) {
try {
if (
(await this.finalizeResolve(curTarget, false, pkgUrl)) ===
(await this.finalizeResolve(curTarget, false, true, pkgUrl)) ===
resolvedUrl
) {
return ".";
Expand All @@ -478,7 +492,7 @@ export class Resolver {
if (subpath !== ".") return null;
const url = new URL(pcfg.exports, pkgUrl).href;
try {
if ((await this.finalizeResolve(url, false, pkgUrl)) === resolvedUrl)
if ((await this.finalizeResolve(url, false, true, pkgUrl)) === resolvedUrl)
return ".";
} catch {}
return null;
Expand All @@ -491,6 +505,7 @@ export class Resolver {
(await this.finalizeResolve(
new URL(curTarget, pkgUrl).href,
false,
true,
pkgUrl
)) === resolvedUrl
)
Expand All @@ -511,6 +526,7 @@ export class Resolver {
(await this.finalizeResolve(
new URL(curTarget, pkgUrl).href,
false,
true,
pkgUrl
)) === resolvedUrl
) {
Expand Down Expand Up @@ -570,6 +586,7 @@ export class Resolver {
(await this.finalizeResolve(
new URL(subpath, new URL(pkgUrl)).href,
false,
false,
pkgUrl
)) === resolvedUrl
)
Expand All @@ -589,6 +606,7 @@ export class Resolver {
pkgUrl
),
false,
false,
pkgUrl
)) === resolvedUrl
)
Expand All @@ -605,6 +623,7 @@ export class Resolver {
pkgUrl
),
false,
false,
pkgUrl
)) === resolvedUrl
)
Expand All @@ -622,6 +641,7 @@ export class Resolver {
pkgUrl
),
false,
false,
pkgUrl
)) === resolvedUrl
)
Expand All @@ -639,6 +659,7 @@ export class Resolver {
pkgUrl
),
false,
false,
pkgUrl
)) === resolvedUrl
)
Expand Down Expand Up @@ -712,6 +733,7 @@ export class Resolver {
return this.finalizeResolve(
new URL(pcfg.exports, pkgUrl).href,
parentIsCjs,
true,
pkgUrl
);
else throwExportNotDefined();
Expand All @@ -720,7 +742,7 @@ export class Resolver {
const url = this.resolvePackageTarget(pcfg.exports, pkgUrl, cjsEnv, "", false);
if (url === null)
throwExportNotDefined();
return this.finalizeResolve(url, parentIsCjs, pkgUrl);
return this.finalizeResolve(url, parentIsCjs, true, pkgUrl);
}
else throwExportNotDefined();
} else {
Expand All @@ -747,7 +769,7 @@ export class Resolver {
false
);
if (resolved === null) throwExportNotDefined();
return this.finalizeResolve(resolved, parentIsCjs, pkgUrl);
return this.finalizeResolve(resolved, parentIsCjs, true, pkgUrl);
}
throwExportNotDefined();
}
Expand All @@ -763,6 +785,7 @@ export class Resolver {
pkgUrl
),
parentIsCjs,
false,
pkgUrl
);
if (env.includes("module") && typeof pcfg.module === "string")
Expand All @@ -775,6 +798,7 @@ export class Resolver {
pkgUrl
),
parentIsCjs,
false,
pkgUrl
);
if (typeof pcfg.main === "string")
Expand All @@ -787,6 +811,7 @@ export class Resolver {
pkgUrl
),
parentIsCjs,
false,
pkgUrl
);
return this.finalizeResolve(
Expand All @@ -798,12 +823,14 @@ export class Resolver {
pkgUrl
),
parentIsCjs,
false,
pkgUrl
);
} else {
return this.finalizeResolve(
new URL(subpath, new URL(pkgUrl)).href,
parentIsCjs,
false,
pkgUrl
);
}
Expand Down Expand Up @@ -953,7 +980,7 @@ export class Resolver {
packageUrl: string,
cjsEnv: boolean,
subpath: string,
isImport: boolean
isImport: boolean,
): string | null {
if (typeof target === "string") {
if (target === ".") {
Expand Down
6 changes: 1 addition & 5 deletions src/trace/tracemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,7 @@ export default class TraceMap {
);
const resolvedHref = resolvedUrl.href;
let finalized = await this.resolver.realPath(
await this.resolver.finalizeResolve(
resolvedHref,
parentIsCjs,
parentPkgUrl
)
await this.resolver.finalizeResolve(resolvedHref, parentIsCjs, false, parentPkgUrl)
);

// handle URL mappings
Expand Down
10 changes: 10 additions & 0 deletions test/resolve/dayjs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Generator } from "@jspm/generator";
import assert from "assert";

const generator = new Generator({
defaultProvider: 'jsdelivr'
});

await generator.install("@cubejs-client/[email protected]");
const json = generator.getMap();
assert(Object.keys(json.imports).length === 1);

0 comments on commit f52f463

Please sign in to comment.