Skip to content

Commit 240af2c

Browse files
committed
refactor: resolvePrivateRequest
1 parent a0de5e1 commit 240af2c

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

.pnp.cjs

Lines changed: 21 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/hook.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/loader/makeApi.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,29 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
869869
}
870870
}
871871

872+
function resolvePrivateRequest(request: PortablePath, issuer: PortablePath | null, opts: ResolveRequestOptions) {
873+
if (!issuer)
874+
throw new Error(`Assertion failed: An issuer is required to resolve private import mappings`);
875+
876+
const resolved = packageImportsResolve({
877+
name: request,
878+
base: pathToFileURL(npath.fromPortablePath(issuer)),
879+
conditions: opts.conditions ?? defaultExportsConditions,
880+
readFileSyncFn: tryReadFile,
881+
});
882+
883+
if (resolved instanceof URL) {
884+
return resolveUnqualified(npath.toPortablePath(fileURLToPath(resolved)), {extensions: opts.extensions});
885+
} else {
886+
if (resolved.startsWith(`#`))
887+
// Node behaves interestingly by default so just block the request for now.
888+
// https://github.com/nodejs/node/issues/40579
889+
throw new Error(`Mapping from one private import to another isn't allowed`);
890+
891+
return resolveRequest(resolved as PortablePath, issuer, opts);
892+
}
893+
}
894+
872895
/**
873896
* Transforms a request into a fully qualified path.
874897
*
@@ -877,30 +900,12 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
877900
* imports won't be computed correctly (they'll get resolved relative to "/tmp/" instead of "/tmp/foo/").
878901
*/
879902

880-
function resolveRequest(request: PortablePath, issuer: PortablePath | null, {considerBuiltins, extensions, conditions}: ResolveRequestOptions = {}): PortablePath | null {
903+
function resolveRequest(request: PortablePath, issuer: PortablePath | null, opts: ResolveRequestOptions = {}): PortablePath | null {
881904
try {
882-
if (request.startsWith(`#`)) {
883-
if (!issuer)
884-
throw new Error(`Assertion failed: An issuer is required to resolve private import mappings`);
885-
886-
const resolved = packageImportsResolve({
887-
name: request,
888-
base: pathToFileURL(npath.fromPortablePath(issuer)),
889-
conditions: conditions ?? defaultExportsConditions,
890-
readFileSyncFn: tryReadFile,
891-
});
892-
893-
if (resolved instanceof URL) {
894-
return resolveUnqualified(npath.toPortablePath(fileURLToPath(resolved)), {extensions});
895-
} else {
896-
if (resolved.startsWith(`#`))
897-
// Node behaves interestingly by default so just block the request for now.
898-
// https://github.com/nodejs/node/issues/40579
899-
throw new Error(`Mapping from one private import to another isn't allowed`);
905+
if (request.startsWith(`#`))
906+
return resolvePrivateRequest(request, issuer, opts);
900907

901-
return resolveRequest(resolved as PortablePath, issuer, {conditions, considerBuiltins, extensions});
902-
}
903-
}
908+
const {considerBuiltins, extensions, conditions} = opts;
904909

905910
const unqualifiedPath = resolveToUnqualified(request, issuer, {considerBuiltins});
906911

0 commit comments

Comments
 (0)