Skip to content

Commit

Permalink
refactor: resolvePrivateRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Sep 19, 2022
1 parent a0de5e1 commit 240af2c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
39 changes: 21 additions & 18 deletions .pnp.cjs

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

2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

49 changes: 27 additions & 22 deletions packages/yarnpkg-pnp/sources/loader/makeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,29 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
}
}

function resolvePrivateRequest(request: PortablePath, issuer: PortablePath | null, opts: ResolveRequestOptions) {
if (!issuer)
throw new Error(`Assertion failed: An issuer is required to resolve private import mappings`);

const resolved = packageImportsResolve({
name: request,
base: pathToFileURL(npath.fromPortablePath(issuer)),
conditions: opts.conditions ?? defaultExportsConditions,
readFileSyncFn: tryReadFile,
});

if (resolved instanceof URL) {
return resolveUnqualified(npath.toPortablePath(fileURLToPath(resolved)), {extensions: opts.extensions});
} else {
if (resolved.startsWith(`#`))
// Node behaves interestingly by default so just block the request for now.
// https://github.com/nodejs/node/issues/40579
throw new Error(`Mapping from one private import to another isn't allowed`);

return resolveRequest(resolved as PortablePath, issuer, opts);
}
}

/**
* Transforms a request into a fully qualified path.
*
Expand All @@ -877,30 +900,12 @@ export function makeApi(runtimeState: RuntimeState, opts: MakeApiOptions): PnpAp
* imports won't be computed correctly (they'll get resolved relative to "/tmp/" instead of "/tmp/foo/").
*/

function resolveRequest(request: PortablePath, issuer: PortablePath | null, {considerBuiltins, extensions, conditions}: ResolveRequestOptions = {}): PortablePath | null {
function resolveRequest(request: PortablePath, issuer: PortablePath | null, opts: ResolveRequestOptions = {}): PortablePath | null {
try {
if (request.startsWith(`#`)) {
if (!issuer)
throw new Error(`Assertion failed: An issuer is required to resolve private import mappings`);

const resolved = packageImportsResolve({
name: request,
base: pathToFileURL(npath.fromPortablePath(issuer)),
conditions: conditions ?? defaultExportsConditions,
readFileSyncFn: tryReadFile,
});

if (resolved instanceof URL) {
return resolveUnqualified(npath.toPortablePath(fileURLToPath(resolved)), {extensions});
} else {
if (resolved.startsWith(`#`))
// Node behaves interestingly by default so just block the request for now.
// https://github.com/nodejs/node/issues/40579
throw new Error(`Mapping from one private import to another isn't allowed`);
if (request.startsWith(`#`))
return resolvePrivateRequest(request, issuer, opts);

return resolveRequest(resolved as PortablePath, issuer, {conditions, considerBuiltins, extensions});
}
}
const {considerBuiltins, extensions, conditions} = opts;

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

Expand Down

0 comments on commit 240af2c

Please sign in to comment.