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: Include source url for parsing failures #109

Merged
merged 3 commits into from
Jun 29, 2024
Merged
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
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
Loading