44
55const { URL } = require ( 'url' )
66const { inspect } = require ( 'util' )
7+ const { builtinModules } = require ( 'module' )
78const specifiers = new Map ( )
89const isWin = process . platform === 'win32'
910
@@ -116,6 +117,11 @@ function isBareSpecifier (specifier) {
116117 }
117118}
118119
120+ /**
121+ * Determines whether the input is a bare specifier, file URL or a regular expression.
122+ *
123+ * - node: prefixed URL strings are considered bare specifiers in this context.
124+ */
119125function isBareSpecifierFileUrlOrRegex ( input ) {
120126 if ( input instanceof RegExp ) {
121127 return true
@@ -131,13 +137,21 @@ function isBareSpecifierFileUrlOrRegex (input) {
131137 try {
132138 // eslint-disable-next-line no-new
133139 const url = new URL ( input )
134- return url . protocol === 'file:'
140+ // We consider node: URLs bare specifiers in this context
141+ return url . protocol === 'file:' || url . protocol === 'node:'
135142 } catch ( err ) {
136143 // Anything that fails parsing is a bare specifier
137144 return true
138145 }
139146}
140147
148+ /**
149+ * Ensure an array only contains bare specifiers, file URLs or regular expressions.
150+ *
151+ * - We consider node: prefixed URL string as bare specifiers in this context.
152+ * - For node built-in modules, we add additional node: prefixed modules to the
153+ * output array.
154+ */
141155function ensureArrayWithBareSpecifiersFileUrlsAndRegex ( array , type ) {
142156 if ( ! Array . isArray ( array ) ) {
143157 return undefined
@@ -149,6 +163,14 @@ function ensureArrayWithBareSpecifiersFileUrlsAndRegex (array, type) {
149163 throw new Error ( `'${ type } ' option only supports bare specifiers, file URLs or regular expressions. Invalid entries: ${ inspect ( invalid ) } ` )
150164 }
151165
166+ // Rather than evaluate whether we have a node: scoped built-in-module for
167+ // every call to resolve, we just add them to include/exclude now.
168+ for ( const each of array ) {
169+ if ( typeof each === 'string' && ! each . startsWith ( 'node:' ) && builtinModules . includes ( each ) ) {
170+ array . push ( `node:${ each } ` )
171+ }
172+ }
173+
152174 return array
153175}
154176
0 commit comments