Skip to content

Commit

Permalink
fix(node-resolve): refactor handling builtins, do not log warning if …
Browse files Browse the repository at this point in the history
…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 <shellscape@users.noreply.github.com>
  • Loading branch information
tjenkinson and shellscape authored Nov 30, 2020
1 parent 23b0bf7 commit c30b2c0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ export function nodeResolve(opts = {}) {

const importeeIsBuiltin = builtins.has(importee);

if (importeeIsBuiltin) {
// The `resolve` library will not resolve packages with the same
// name as a node built-in module. If we're resolving something
// that's a builtin, and we don't prefer to find built-ins, we
// first try to look up a local module with that name. If we don't
// find anything, we resolve the builtin which just returns back
// the built-in's name.
importSpecifierList.push(`${importee}/`);
}

// TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
if (importer && importee.endsWith('.js')) {
for (const ext of ['.ts', '.tsx']) {
Expand Down Expand Up @@ -229,7 +239,7 @@ export function nodeResolve(opts = {}) {

if (hasPackageEntry) {
if (importeeIsBuiltin && preferBuiltins) {
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins) {
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
this.warn(
`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`
);
Expand Down

0 comments on commit c30b2c0

Please sign in to comment.