|
1 | | -const getNodeVersion = () => { |
2 | | - const version = process.version.replace('v', ''); |
3 | | - |
4 | | - return parseInt(version, 10); |
| 1 | +const mergeDefaultAndNamedExports = (module) => { |
| 2 | + const _default = module.default || {}; |
| 3 | + return Object.keys(module).reduce((prev, val) => { |
| 4 | + if (val !== 'default') { |
| 5 | + prev[val] = module[val]; |
| 6 | + } |
| 7 | + |
| 8 | + return prev; |
| 9 | + }, _default); |
5 | 10 | } |
6 | 11 |
|
| 12 | +/** |
| 13 | + * Requires a module, supporting both CommonJS and ES6 modules. |
| 14 | + * |
| 15 | + * @param {string} fullpath - The full path to the module to require. |
| 16 | + * @returns {any|Promise<any>} - The required module or a promise that resolves to the required module. |
| 17 | + */ |
7 | 18 | module.exports = function (fullpath) { |
8 | 19 | let exported; |
9 | 20 | try { |
10 | 21 | exported = require(fullpath); |
11 | 22 | } catch (err) { |
12 | | - if (err.message === 'Cannot use import statement outside a module' || err.message === 'Unexpected token \'export\'') { |
| 23 | + const isEsmSyntaxError = err.message === 'Cannot use import statement outside a module' || |
| 24 | + err.message.includes('Unexpected token \'export\''); |
| 25 | + const isMjsFile = fullpath.endsWith('.mjs'); |
| 26 | + |
| 27 | + // calling require() on a .mjs file on Node.js < 20 throws ERR_REQUIRE_ESM. |
| 28 | + // calling require() on a .mjs file on Node.js >= 20 passes through without |
| 29 | + // any error, but if ts-node is activated, it throws ERR_REQUIRE_ESM in normal |
| 30 | + // cases (it still relies on the old CommonJS loader) and isEsmSyntaxError if |
| 31 | + // v8-compile-cache-lib is used (see PR #4437). |
| 32 | + // Use dynamic import() in both cases. |
| 33 | + if (err.code === 'ERR_REQUIRE_ESM' || (isMjsFile && isEsmSyntaxError)) { |
| 34 | + const { pathToFileURL } = require('node:url'); |
| 35 | + |
| 36 | + return import(pathToFileURL(fullpath).href).then(mergeDefaultAndNamedExports); |
| 37 | + } |
| 38 | + |
| 39 | + if (isEsmSyntaxError) { |
13 | 40 | err.detailedErr = err.message; |
14 | 41 | err.help = ['Using ES6 import/export syntax? - make sure to specify "type=module" in your package.json or use .mjs extension.']; |
15 | 42 | err.link = 'https://nodejs.org/api/esm.html'; |
16 | | - |
17 | | - throw err; |
18 | 43 | } |
19 | 44 |
|
20 | | - if (err.code !== 'ERR_REQUIRE_ESM') { |
21 | | - throw err; |
22 | | - } |
23 | | - |
24 | | - if (getNodeVersion() >= 14) { |
25 | | - const {pathToFileURL} = require('node:url'); |
26 | | - |
27 | | - return import(pathToFileURL(fullpath).href).then(result => { |
28 | | - const _default = result.default || {}; |
29 | | - |
30 | | - return Object.keys(result).reduce((prev, val) => { |
31 | | - if (val !== 'default') { |
32 | | - prev[val] = result[val]; |
33 | | - } |
| 45 | + throw err; |
| 46 | + } |
34 | 47 |
|
35 | | - return prev; |
36 | | - }, _default); |
37 | | - }); |
| 48 | + if (exported && Object.prototype.hasOwnProperty.call(exported, 'default')) { |
| 49 | + const keys = Object.keys(exported); |
| 50 | + if (keys.length === 1 || (keys.length === 2 && exported.__esModule === true)) { |
| 51 | + return exported.default; |
38 | 52 | } |
39 | | - |
40 | | - return import(fullpath).then(result => (result.default || {})); |
41 | 53 | } |
42 | 54 |
|
43 | | - if (exported && Object.prototype.hasOwnProperty.call(exported, 'default') && Object.keys(exported).length === 1) { |
44 | | - return exported.default; |
| 55 | + if (exported?.__esModule === true) { |
| 56 | + // ESM module with named exports present (with or without default export). |
| 57 | + return mergeDefaultAndNamedExports(exported); |
45 | 58 | } |
46 | 59 |
|
47 | 60 | return exported; |
|