Skip to content

Commit

Permalink
fix: node: prefixed build-in modules with include/exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 27, 2024
1 parent 79340b8 commit 75c2103
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const { URL } = require('url')
const { inspect } = require('util')
const { builtinModules } = require('module')
const specifiers = new Map()
const isWin = process.platform === 'win32'

Expand Down Expand Up @@ -131,7 +132,8 @@ function isBareSpecifierFileUrlOrRegex (input) {
try {
// eslint-disable-next-line no-new
const url = new URL(input)
return url.protocol === 'file:'
// We consider node: URLs bare specifiers in this context
return url.protocol === 'file:' || url.protocol === 'node:'
} catch (err) {
// Anything that fails parsing is a bare specifier
return true
Expand All @@ -149,6 +151,14 @@ function ensureArrayWithBareSpecifiersFileUrlsAndRegex (array, type) {
throw new Error(`'${type}' option only supports bare specifiers, file URLs or regular expressions. Invalid entries: ${inspect(invalid)}`)
}

// Rather than evaluate whether we have a node: scoped built-in-module for
// every call to resolve, we just add them to include/exclude now.
for (const each of array) {
if (typeof each === 'string' && !each.startsWith('node:') && builtinModules.includes(each)) {
array.push(`node:${each}`)
}
}

return array
}

Expand Down
20 changes: 20 additions & 0 deletions test/register/v18.19-include-builtin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { register } from 'module'
import Hook from '../../index.js'
import { strictEqual } from 'assert'

register('../../hook.mjs', import.meta.url, { data: { include: ['node:util', 'os'] } })

const hooked = []

Hook((exports, name) => {
hooked.push(name)
})

await import('util')
await import('node:os')
await import('fs')
await import('path')

strictEqual(hooked.length, 2)
strictEqual(hooked[0], 'util')
strictEqual(hooked[1], 'os')

0 comments on commit 75c2103

Please sign in to comment.