Skip to content

Commit

Permalink
changed how we filter for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ida613 committed Mar 28, 2024
1 parent 6cbb2c0 commit 2e930f9
Showing 1 changed file with 28 additions and 53 deletions.
81 changes: 28 additions & 53 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,69 +228,44 @@ function addIitm (url) {
return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
}

// moduleList is an optional Map specifiying which modules need IITM patching
// format: { moduleName : ['/file1.js', '/file2.js'] }
function createHook (meta, moduleList = {}) {
// moduleList is an optional Set specifiying which modules need IITM patching
// if moduleList is not set, IITM patches everything
function createHook (meta, moduleList = new Set()) {
async function resolve (specifier, context, parentResolve) {
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 (const mod of moduleList.key) { // if specifier is a file path
if (specifier.includes(mod)) {
for (const path of moduleList[mod]) {
if (specifier.endsWith(mod + path) || specifier.endsWith(mod + path + '/')) {
patch = true
continue
}
}
}
}
}
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' }
}
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' }
}

if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}

// 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)
return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
}
// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
(context.importAttributes && context.importAttributes.type === 'json')
) {
return url
}
const newSpecifier = deleteIitm(specifier)
if (isWin) {
context.parentURL = ''

specifiers.set(url.url, specifier)
return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
}
const url = await parentResolve(newSpecifier, context, parentResolve)
return { url: url.url, format: url.format }
}

const iitmURL = new URL('lib/register.js', meta.url).toString()
async function getSource (url, context, parentGetSource) {
if (moduleList.size && !moduleList.has(url)) return parentGetSource(url, context, parentGetSource)
if (hasIitm(url)) {
const realUrl = deleteIitm(url)
const { imports, namespaces, setters: mapSetters } = await processModule({
Expand Down

0 comments on commit 2e930f9

Please sign in to comment.