@@ -92,23 +92,29 @@ async function getExports (url, context, parentLoad) {
92
92
source = readFileSync ( fileURLToPath ( url ) , 'utf8' )
93
93
}
94
94
95
- if ( format === 'module' ) {
96
- return getEsmExports ( source )
97
- }
95
+ try {
96
+ if ( format === 'module' ) {
97
+ return getEsmExports ( source )
98
+ }
98
99
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
+ }
102
103
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
112
118
}
113
119
}
114
120
0 commit comments