Skip to content

Commit

Permalink
fix: Include source url for parsing failures (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Jun 29, 2024
1 parent a3778ac commit 49d69ba
Showing 1 changed file with 21 additions and 15 deletions.
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

0 comments on commit 49d69ba

Please sign in to comment.