-
Notifications
You must be signed in to change notification settings - Fork 2
/
mismatched-format.mjs
36 lines (29 loc) · 1015 Bytes
/
mismatched-format.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import containsCJS from './containsCJS.mjs';
import { getFilenameExt } from './parse-filename.mjs';
/**
* This loader attempts to detect and override misconfigured packages, such as those that declare
* themselves as ESM but are actually CJS, and vice versa.
*/
export async function load(url, ctx, next) {
if (!exts.has(getFilenameExt(url))) return next(url);
// Ensure the ESMLoader is used to read the contents.
// It may throw, in which case we'll probably get a telling error we can use to know it was CJS.
const nextResult = await next(url, { ...ctx, format: 'module' })
.then((result) => {
if (containsCJS(''+result.source)) { throw new Error('CommonJS'); }
return result;
})
.catch(async (err) => {
if (
(err?.message.includes('require') && err.includes('import'))
|| err?.message.includes('CommonJS')
) {
return { format: 'commonjs' };
}
throw err;
});
return nextResult;
}
const exts = new Set([
'.js',
]);