Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add safe Yarn PnP support #270

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ function _tryModuleResolve(
}
}

function safeImportMetaResolve(path) {
try {
// Yarn PnP support -- resolve the path to Yarn's virtual file system
if (typeof import.meta.resolve === "function") {
const resolvedPath = import.meta.resolve(path);

if (typeof resolvedPath === "string") {
// Older implementations of resolve use a promise; it's a hassle to
// support this, and they're old anyway, so support only the modern
// synchronous resolve
return resolvedPath;
}
}

// If there's no resolve, then we can't be using Yarn PnP, so nothing to
// do anyway
return path;
} catch (_error) {
return path;
}
}

function _resolve(id: string | URL, options: ResolveOptions = {}): string {
if (typeof id !== "string") {
if (id instanceof URL) {
Expand Down Expand Up @@ -118,6 +140,11 @@ function _resolve(id: string | URL, options: ResolveOptions = {}): string {
if (resolved) {
break;
}
// Try Yarn PnP support
resolved = _tryModuleResolve(safeImportMetaResolve(id), url, conditionsSet);
if (resolved) {
break;
}
// Try other extensions if not found
for (const prefix of ["", "/index"]) {
for (const extension of options.extensions || DEFAULT_EXTENSIONS) {
Expand Down