Skip to content

Commit c30b2c0

Browse files
fix(node-resolve): refactor handling builtins, do not log warning if no local version (#637)
* refactor handling builtins * remove duplicate code block * do not warn when using a builtin when no local version * add not about warnings to readme Co-authored-by: Andrew Powell <[email protected]>
1 parent 23b0bf7 commit c30b2c0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/node-resolve/src/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ export function nodeResolve(opts = {}) {
163163

164164
const importeeIsBuiltin = builtins.has(importee);
165165

166+
if (importeeIsBuiltin) {
167+
// The `resolve` library will not resolve packages with the same
168+
// name as a node built-in module. If we're resolving something
169+
// that's a builtin, and we don't prefer to find built-ins, we
170+
// first try to look up a local module with that name. If we don't
171+
// find anything, we resolve the builtin which just returns back
172+
// the built-in's name.
173+
importSpecifierList.push(`${importee}/`);
174+
}
175+
166176
// TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
167177
if (importer && importee.endsWith('.js')) {
168178
for (const ext of ['.ts', '.tsx']) {
@@ -229,7 +239,7 @@ export function nodeResolve(opts = {}) {
229239

230240
if (hasPackageEntry) {
231241
if (importeeIsBuiltin && preferBuiltins) {
232-
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins) {
242+
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
233243
this.warn(
234244
`preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
235245
);

0 commit comments

Comments
 (0)