Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cjs resolution when falling back to parentLoad #106

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 5 additions & 17 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,20 @@ function createHook (meta) {
}
}

async function getSource (url, context, parentGetSource) {
async function load (url, context, parentLoad) {
if (hasIitm(url)) {
const realUrl = deleteIitm(url)

try {
const setters = await processModule({
srcUrl: realUrl,
context,
parentGetSource,
parentGetSource: parentLoad,
parentResolve: cachedResolve
})
return {
shortCircuit: true,
format: 'module',
source: `
import { register } from '${iitmURL}'
import * as namespace from ${JSON.stringify(realUrl)}
Expand Down Expand Up @@ -310,20 +312,6 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
}
}

return parentGetSource(url, context, parentGetSource)
}

// For Node.js 16.12.0 and higher.
async function load (url, context, parentLoad) {
if (hasIitm(url)) {
const { source } = await getSource(url, context, parentLoad)
return {
source,
shortCircuit: true,
format: 'module'
}
}

return parentLoad(url, context, parentLoad)
}

Expand All @@ -333,7 +321,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
return {
load,
resolve,
getSource,
getSource: load,
getFormat (url, context, parentGetFormat) {
if (hasIitm(url)) {
return {
Expand Down
36 changes: 21 additions & 15 deletions lib/get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,29 @@ async function getExports (url, context, parentLoad) {
source = readFileSync(fileURLToPath(url), 'utf8')
}

if (format === 'module') {
return getEsmExports(source)
}
try {
if (format === 'module') {
return getEsmExports(source)
}

if (format === 'commonjs') {
return getCjsExports(url, context, parentLoad, source)
}
if (format === 'commonjs') {
return await getCjsExports(url, context, parentLoad, source)
}

// At this point our `format` is either undefined or not known by us. Fall
// back to parsing as ESM/CJS.
const esmExports = getEsmExports(source)
if (!esmExports.length) {
// TODO(bengl) it's might be possible to get here if somehow the format
// isn't set at first and yet we have an ESM module with no exports.
// I couldn't construct an example that would do this, so maybe it's
// impossible?
return getCjsExports(url, context, parentLoad, source)
// At this point our `format` is either undefined or not known by us. Fall
// back to parsing as ESM/CJS.
const esmExports = getEsmExports(source)
if (!esmExports.length) {
// TODO(bengl) it's might be possible to get here if somehow the format
// isn't set at first and yet we have an ESM module with no exports.
// I couldn't construct an example that would do this, so maybe it's
// impossible?
return await getCjsExports(url, context, parentLoad, source)
}
} catch (cause) {
const err = new Error(`Failed to parse '${url}'`)
err.cause = cause
throw err
}
}

Expand Down