Skip to content

Commit

Permalink
fix(loader): fix agressive cjs-esm transform breaks require
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 31, 2024
1 parent c9c6b3a commit 531ef2b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ class NodeLoader extends Loader {
const originalLoad: ModuleLoad = Module['_load']
Module['_load'] = ((request, parent, isMain) => {
try {
// TODO support hmr for cjs-esm interop
const result = this.internal?.resolveSync(request, pathToFileURL(parent.filename).href, {})
const job = result?.format === 'module'
? this.internal?.loadCache.get(result.url)
: undefined
if (job) return job?.module?.getNamespace()
} catch {}
return originalLoad(request, parent, isMain)
return originalLoad(request, parent, isMain)
} catch (e: any) {
if (e.code !== 'ERR_REQUIRE_ESM' || !this.internal) throw e
try {
// TODO support hmr for cjs-esm interop
const result = this.internal.resolveSync(request, pathToFileURL(parent.filename).href, {})
const job = result?.format === 'module'
? this.internal.loadCache.get(result.url)
: undefined
if (job) return job?.module?.getNamespace()
} catch {
throw e
}
}
}) as ModuleLoad

await super.start()
Expand Down

0 comments on commit 531ef2b

Please sign in to comment.