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

[Fix/New] Node resolver: Try to use require.resolve when suitable #2957

Draft
wants to merge 1 commit 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
[Fix/New] Node resolver: Try to use require.resolve when suitable
akx committed Jun 24, 2024
commit 1592741d7a1f0be050464da6250513a2b5acf83d
13 changes: 13 additions & 0 deletions resolvers/node/index.js
Original file line number Diff line number Diff line change
@@ -54,6 +54,19 @@ exports.resolve = function (source, file, config) {
return { found: true, path: null };
}

// If this looks like a bare package name (not relative, not qualified
// with an extension) and we're on a fresh enough version of Node.js
// to have `require.resolve`, attempt that first.
if (require.resolve && source.indexOf('.') === -1) {
try {
resolvedPath = require.resolve(source);
log('Resolved to:', resolvedPath);
return { found: true, path: resolvedPath };
} catch (err) {
log('require.resolve threw error:', err);
}
}

try {
const cachedFilter = function (pkg, dir) { return packageFilter(pkg, dir, config); };
resolvedPath = resolve(source, opts(file, config, cachedFilter));