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

esm: improve packageResolve #34269

Closed
wants to merge 6 commits into from
Closed
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
22 changes: 8 additions & 14 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ function getPackageScopeConfig(resolved, base) {
const packageJSONPath = packageJSONUrl.pathname;
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json'))
break;
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base);
if (packageConfig.exists) return packageConfig;
const filePath = fileURLToPath(packageJSONUrl);
const packageConfig = getPackageConfig(filePath, base);
if (packageConfig.exists) return { path: filePath, packageConfig };

const lastPackageJSONUrl = packageJSONUrl;
packageJSONUrl = new URL('../package.json', packageJSONUrl);
Expand All @@ -151,7 +152,7 @@ function getPackageScopeConfig(resolved, base) {
exports: undefined
};
packageJSONCache.set(fileURLToPath(packageJSONUrl), packageConfig);
return packageConfig;
return { packageConfig };
}

/*
Expand Down Expand Up @@ -488,7 +489,7 @@ function packageExportsResolve(
}

function getPackageType(url) {
const packageConfig = getPackageScopeConfig(url, url);
const { packageConfig } = getPackageScopeConfig(url, url);
return packageConfig.type;
}

Expand Down Expand Up @@ -533,17 +534,10 @@ function packageResolve(specifier, base, conditions) {
'' : '.' + StringPrototypeSlice(specifier, separatorIndex);

// ResolveSelf
const packageConfig = getPackageScopeConfig(base, base);
const { path, packageConfig } = getPackageScopeConfig(base, base);
if (packageConfig.exists) {
// TODO(jkrems): Find a way to forward the pair/iterator already generated
// while executing GetPackageScopeConfig
let packageJSONUrl;
for (const [ filename, packageConfigCandidate ] of packageJSONCache) {
if (packageConfig === packageConfigCandidate) {
packageJSONUrl = pathToFileURL(filename);
break;
}
}
// If path not exist, then packageConfig.exists is false too
const packageJSONUrl = pathToFileURL(path);
if (packageJSONUrl !== undefined &&
packageConfig.name === packageName &&
packageConfig.exports !== undefined) {
Expand Down