Skip to content

Commit

Permalink
fix: handling of circular dependencies (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram authored Feb 28, 2025
1 parent 341ada1 commit b58092e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
12 changes: 7 additions & 5 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
}
} else {
addSetter(n, `
let $${n} = _.${n}
let $${n}
try {
$${n} = _.${n} = namespace.${n}
} catch (err) {
if (!(err instanceof ReferenceError)) throw err
}
export { $${n} as ${n} }
set.${n} = (v) => {
$${n} = v
Expand Down Expand Up @@ -404,10 +409,7 @@ import { register } from '${iitmURL}'
import * as namespace from ${JSON.stringify(realUrl)}
// Mimic a Module object (https://tc39.es/ecma262/#sec-module-namespace-objects).
const _ = Object.assign(
Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } }),
namespace
)
const _ = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } })
const set = {}
const get = {}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/circular-a.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { bar } from './circular-b.mjs'

export const foo = 1

export { bar }
3 changes: 3 additions & 0 deletions test/fixtures/circular-b.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { foo } from './circular-a.mjs'

export const bar = foo + 1
17 changes: 17 additions & 0 deletions test/register/v18.19-circular.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { register } from 'module'
import Hook from '../../index.js'
import { strictEqual } from 'assert'

register('../../hook.mjs', import.meta.url)

let bar

Hook((exports, name) => {
if (name.match(/circular-b.mjs/)) {
bar = exports.bar
}
})

await import('../fixtures/circular-b.mjs')

strictEqual(bar, 2)

0 comments on commit b58092e

Please sign in to comment.