Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added optional argument to specify a list of modules to patch to crea…
…teHook
  • Loading branch information
ida613 committed Mar 25, 2024
commit 878fadb5b00866235fbf7a54d273b1bfcd1c88af
80 changes: 54 additions & 26 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,66 @@ function addIitm (url) {
return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
}

function createHook (meta) {
async function resolve (specifier, context, parentResolve) {
const { parentURL = '' } = context
const newSpecifier = deleteIitm(specifier)
if (isWin && parentURL.indexOf('file:node') === 0) {
context.parentURL = ''
}
const url = await parentResolve(newSpecifier, context, parentResolve)
if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
entrypoint = url.url
return { url: url.url, format: 'commonjs' }
// moduleList is an optional Map specifiying which modules need IITM patching
// format: { moduleName : ['/file1.js', '/file2.js'] }
function createHook (meta, moduleList={}) {
async function resolve (specifier, context, parentResolve, moduleList) {
let patch = true
if (moduleList.size) {
patch = false // do not patch unless specifier is in moduleList
if (moduleList.has(specifier)) { // if specifier is a module name present in moduleList
patch = true
} else {
for (let mod of moduleList.key) {
if (specifier.includes(mod)) {
for (let path of moduleList[mod]) {
if (specifier.endsWith(mod + path) || specifier.endsWith(mod + path + '/')) {
patch = true
continue
}
}
}
}
}
}

if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}
if (patch) {
const { parentURL = '' } = context
const newSpecifier = deleteIitm(specifier)
if (isWin && parentURL.indexOf('file:node') === 0) {
context.parentURL = ''
}
const url = await parentResolve(newSpecifier, context, parentResolve)
if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
entrypoint = url.url
return { url: url.url, format: 'commonjs' } // early return, find out format
}

// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
(context.importAttributes && context.importAttributes.type === 'json')
) {
return url
}
if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}

specifiers.set(url.url, specifier)
// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
(context.importAttributes && context.importAttributes.type === 'json')
) {
return url
}

return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
specifiers.set(url.url, specifier)
return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
}
}
const newSpecifier = deleteIitm(specifier)
if (isWin && parentURL.indexOf('file:node') === 0) {
context.parentURL = ''
}
const url = await parentResolve(newSpecifier, context, parentResolve)
return { url: url.url, format: url.format }
}

const iitmURL = new URL('lib/register.js', meta.url).toString()
Expand Down