Skip to content

Commit 49d69ba

Browse files
authored
fix: Include source url for parsing failures (#109)
1 parent a3778ac commit 49d69ba

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/get-exports.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,29 @@ async function getExports (url, context, parentLoad) {
9292
source = readFileSync(fileURLToPath(url), 'utf8')
9393
}
9494

95-
if (format === 'module') {
96-
return getEsmExports(source)
97-
}
95+
try {
96+
if (format === 'module') {
97+
return getEsmExports(source)
98+
}
9899

99-
if (format === 'commonjs') {
100-
return getCjsExports(url, context, parentLoad, source)
101-
}
100+
if (format === 'commonjs') {
101+
return await getCjsExports(url, context, parentLoad, source)
102+
}
102103

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

0 commit comments

Comments
 (0)