Skip to content

Commit

Permalink
feat(node-resolve): support node: protocol (#1124)
Browse files Browse the repository at this point in the history
Imports with the [`node:`
protocol](https://nodejs.org/api/esm.html#node-imports) are currently
undetected by this plugin.

Switch to using
[is-builtin-module](https://github.com/sindresorhus/is-builtin-module),
which handles this protocol and also submodule imports like
`fs/promises`.  Functionality is otherwise identical.

Resolves #1120.
  • Loading branch information
isker authored May 2, 2022
1 parent cd5b1c8 commit a7b56eb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/node-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"dependencies": {
"@rollup/pluginutils": "^3.1.0",
"@types/resolve": "1.17.1",
"builtin-modules": "^3.1.0",
"deepmerge": "^4.2.2",
"is-builtin-module": "^3.1.0",
"is-module": "^1.0.0",
"resolve": "^1.19.0"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign, no-shadow, no-undefined */
import { dirname, normalize, resolve, sep } from 'path';

import builtinList from 'builtin-modules';
import isBuiltinModule from 'is-builtin-module';
import deepMerge from 'deepmerge';
import isModule from 'is-module';

Expand All @@ -13,7 +13,6 @@ import { fileExists, readFile, realpath } from './fs';
import resolveImportSpecifiers from './resolveImportSpecifiers';
import { getMainFields, getPackageName, normalizeInput } from './util';

const builtins = new Set(builtinList);
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
const deepFreeze = (object) => {
Object.freeze(object);
Expand Down Expand Up @@ -172,7 +171,7 @@ export function nodeResolve(opts = {}) {
ignoreSideEffectsForRoot
});

const importeeIsBuiltin = builtins.has(importee);
const importeeIsBuiltin = isBuiltinModule(importee);
const resolved =
importeeIsBuiltin && preferBuiltins
? {
Expand Down
1 change: 1 addition & 0 deletions packages/node-resolve/test/fixtures/node-protocol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'node:fs';
13 changes: 13 additions & 0 deletions packages/node-resolve/test/prefer-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ test('does not warn when using a builtin module when there is no local version,

t.is(warning, null);
});

test('detects builtins imported with node: protocol', async (t) => {
const warnings = [];
await rollup({
input: 'node-protocol.js',
onwarn({ message }) {
warnings.push(message);
},
plugins: [nodeResolve()]
});

t.is(warnings.length, 0);
});
11 changes: 9 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7b56eb

Please sign in to comment.