@@ -10,7 +10,7 @@ const formattedImport = async file => {
10
10
// the location of the syntax error in the error thrown.
11
11
// This is problematic because the user can't see what file has the problem,
12
12
// so we add the file location to the error.
13
- // This `if` should be removed once Node.js fixes the problem.
13
+ // TODO: remove once Node.js fixes the problem.
14
14
if (
15
15
err instanceof SyntaxError &&
16
16
err . message &&
@@ -30,64 +30,52 @@ const formattedImport = async file => {
30
30
return import ( file ) ;
31
31
} ;
32
32
33
- const hasStableEsmImplementation = ( ( ) => {
34
- const [ major , minor ] = process . version . split ( '.' ) ;
35
- // ESM is stable from v12.22.0 onward
36
- // https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules
37
- const majorNumber = parseInt ( major . slice ( 1 ) , 10 ) ;
38
- const minorNumber = parseInt ( minor , 10 ) ;
39
- return majorNumber > 12 || ( majorNumber === 12 && minorNumber >= 22 ) ;
40
- } ) ( ) ;
41
-
42
- exports . requireOrImport = hasStableEsmImplementation
43
- ? async file => {
44
- if ( path . extname ( file ) === '.mjs' ) {
45
- return formattedImport ( file ) ;
46
- }
33
+ exports . requireOrImport = async file => {
34
+ if ( path . extname ( file ) === '.mjs' ) {
35
+ return formattedImport ( file ) ;
36
+ }
37
+ try {
38
+ return dealWithExports ( await formattedImport ( file ) ) ;
39
+ } catch ( err ) {
40
+ if (
41
+ err . code === 'ERR_MODULE_NOT_FOUND' ||
42
+ err . code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
43
+ err . code === 'ERR_UNSUPPORTED_DIR_IMPORT'
44
+ ) {
47
45
try {
48
- return dealWithExports ( await formattedImport ( file ) ) ;
49
- } catch ( err ) {
46
+ // Importing a file usually works, but the resolution of `import` is the ESM
47
+ // resolution algorithm, and not the CJS resolution algorithm. We may have
48
+ // failed because we tried the ESM resolution, so we try to `require` it.
49
+ return require ( file ) ;
50
+ } catch ( requireErr ) {
50
51
if (
51
- err . code === 'ERR_MODULE_NOT_FOUND' ||
52
- err . code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
53
- err . code === 'ERR_UNSUPPORTED_DIR_IMPORT'
52
+ requireErr . code === 'ERR_REQUIRE_ESM' ||
53
+ ( requireErr instanceof SyntaxError &&
54
+ requireErr
55
+ . toString ( )
56
+ . includes ( 'Cannot use import statement outside a module' ) )
54
57
) {
55
- try {
56
- // Importing a file usually works, but the resolution of `import` is the ESM
57
- // resolution algorithm, and not the CJS resolution algorithm. So in this case
58
- // if we fail, we may have failed because we tried the ESM resolution and failed
59
- // So we try to `require` it
60
- return require ( file ) ;
61
- } catch ( requireErr ) {
62
- if (
63
- requireErr . code === 'ERR_REQUIRE_ESM' ||
64
- ( requireErr instanceof SyntaxError &&
65
- requireErr
66
- . toString ( )
67
- . includes ( 'Cannot use import statement outside a module' ) )
68
- ) {
69
- // ERR_REQUIRE_ESM happens when the test file is a JS file, but via type:module is actually ESM,
70
- // AND has an import to a file that doesn't exist.
71
- // This throws an `ERR_MODULE_NOT_FOUND` error above,
72
- // and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
73
- // What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
74
- // and not the `ERR_REQUIRE_ESM` error, which is a red herring.
75
- //
76
- // SyntaxError happens when in an edge case: when we're using an ESM loader that loads
77
- // a `test.ts` file (i.e. unrecognized extension), and that file includes an unknown
78
- // import (which thows an ERR_MODULE_NOT_FOUND). require-ing it will throw the
79
- // syntax error, because we cannot require a file that has import-s.
80
- throw err ;
81
- } else {
82
- throw requireErr ;
83
- }
84
- }
85
- } else {
58
+ // ERR_REQUIRE_ESM happens when the test file is a JS file, but via type:module is actually ESM,
59
+ // AND has an import to a file that doesn't exist.
60
+ // This throws an `ERR_MODULE_NOT_FOUND` error above,
61
+ // and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
62
+ // What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
63
+ // and not the `ERR_REQUIRE_ESM` error, which is a red herring.
64
+ //
65
+ // SyntaxError happens when in an edge case: when we're using an ESM loader that loads
66
+ // a `test.ts` file (i.e. unrecognized extension), and that file includes an unknown
67
+ // import (which throws an ERR_MODULE_NOT_FOUND). `require`-ing it will throw the
68
+ // syntax error, because we cannot require a file that has `import`-s.
86
69
throw err ;
70
+ } else {
71
+ throw requireErr ;
87
72
}
88
73
}
74
+ } else {
75
+ throw err ;
89
76
}
90
- : implementationOfRequireOrImportForUnstableEsm ;
77
+ }
78
+ } ;
91
79
92
80
function dealWithExports ( module ) {
93
81
if ( module . default ) {
@@ -104,21 +92,3 @@ exports.loadFilesAsync = async (files, preLoadFunc, postLoadFunc) => {
104
92
postLoadFunc ( file , result ) ;
105
93
}
106
94
} ;
107
-
108
- /* istanbul ignore next */
109
- async function implementationOfRequireOrImportForUnstableEsm ( file ) {
110
- if ( path . extname ( file ) === '.mjs' ) {
111
- return formattedImport ( file ) ;
112
- }
113
- // This is currently the only known way of figuring out whether a file is CJS or ESM in
114
- // Node.js that doesn't necessitate calling `import` first.
115
- try {
116
- return require ( file ) ;
117
- } catch ( err ) {
118
- if ( err . code === 'ERR_REQUIRE_ESM' ) {
119
- return formattedImport ( file ) ;
120
- } else {
121
- throw err ;
122
- }
123
- }
124
- }
0 commit comments