From a7b56eb5c8182fad3ddabe8176c72e03b96059e6 Mon Sep 17 00:00:00 2001 From: Ian Kerins Date: Mon, 2 May 2022 08:18:31 -0400 Subject: [PATCH] feat(node-resolve): support `node:` protocol (#1124) 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. --- packages/node-resolve/package.json | 2 +- packages/node-resolve/src/index.js | 5 ++--- .../node-resolve/test/fixtures/node-protocol.js | 1 + packages/node-resolve/test/prefer-builtins.js | 13 +++++++++++++ pnpm-lock.yaml | 11 +++++++++-- 5 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 packages/node-resolve/test/fixtures/node-protocol.js diff --git a/packages/node-resolve/package.json b/packages/node-resolve/package.json index a201828b9..d24a6bd00 100644 --- a/packages/node-resolve/package.json +++ b/packages/node-resolve/package.json @@ -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" }, diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index f9895be3c..74cdaf025 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -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'; @@ -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); @@ -172,7 +171,7 @@ export function nodeResolve(opts = {}) { ignoreSideEffectsForRoot }); - const importeeIsBuiltin = builtins.has(importee); + const importeeIsBuiltin = isBuiltinModule(importee); const resolved = importeeIsBuiltin && preferBuiltins ? { diff --git a/packages/node-resolve/test/fixtures/node-protocol.js b/packages/node-resolve/test/fixtures/node-protocol.js new file mode 100644 index 000000000..75bfd5a9c --- /dev/null +++ b/packages/node-resolve/test/fixtures/node-protocol.js @@ -0,0 +1 @@ +import 'node:fs'; diff --git a/packages/node-resolve/test/prefer-builtins.js b/packages/node-resolve/test/prefer-builtins.js index 5c78ea4f5..8cd3715a7 100644 --- a/packages/node-resolve/test/prefer-builtins.js +++ b/packages/node-resolve/test/prefer-builtins.js @@ -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); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 104134fdf..0cdd7fee9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -376,9 +376,9 @@ importers: '@rollup/plugin-json': ^4.1.0 '@rollup/pluginutils': ^3.1.0 '@types/resolve': 1.17.1 - builtin-modules: ^3.1.0 deepmerge: ^4.2.2 es5-ext: ^0.10.53 + is-builtin-module: ^3.1.0 is-module: ^1.0.0 resolve: ^1.19.0 rollup: ^2.67.3 @@ -387,8 +387,8 @@ importers: dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.67.3 '@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 devDependencies: @@ -5340,6 +5340,13 @@ packages: call-bind: 1.0.2 dev: true + /is-builtin-module/3.1.0: + resolution: {integrity: sha512-OV7JjAgOTfAFJmHZLvpSTb4qi0nIILDV1gWPYDnDJUTNFM5aGlRAhk4QcT8i7TuAleeEV5Fdkqn3t4mS+Q11fg==} + engines: {node: '>=6'} + dependencies: + builtin-modules: 3.1.0 + dev: false + /is-callable/1.2.2: resolution: {integrity: sha512-dnMqspv5nU3LoewK2N/y7KLtxtakvTuaCsU9FU50/QDmdbHNy/4/JuRtMHqRU22o3q+W89YQndQEeCVwK+3qrA==} engines: {node: '>= 0.4'}