@@ -33,6 +33,56 @@ export const supportsESM = semver.satisfies(
3333 "^12.17 || >=13.2" ,
3434) ;
3535
36+ const LOADING_CJS_FILES = new Set ( ) ;
37+
38+ function loadCjsDefault ( filepath : string ) {
39+ // The `require()` call below can make this code reentrant if a require hook
40+ // like @babel /register has been loaded into the system. That would cause
41+ // Babel to attempt to compile the `.babelrc.js` file as it loads below. To
42+ // cover this case, we auto-ignore re-entrant config processing. ESM loaders
43+ // do not have this problem, because loaders do not apply to themselves.
44+ if ( LOADING_CJS_FILES . has ( filepath ) ) {
45+ debug ( "Auto-ignoring usage of config %o." , filepath ) ;
46+ return { } ;
47+ }
48+
49+ let module ;
50+ try {
51+ LOADING_CJS_FILES . add ( filepath ) ;
52+ module = endHiddenCallStack ( require ) ( filepath ) ;
53+ } finally {
54+ LOADING_CJS_FILES . delete ( filepath ) ;
55+ }
56+
57+ if ( process . env . BABEL_8_BREAKING ) {
58+ return module ?. __esModule ? module . default : module ;
59+ } else {
60+ return module ?. __esModule
61+ ? module . default ||
62+ /* fallbackToTranspiledModule */ ( arguments [ 1 ] ? module : undefined )
63+ : module ;
64+ }
65+ }
66+
67+ const loadMjsDefault = endHiddenCallStack ( async function loadMjsDefault (
68+ filepath : string ,
69+ ) {
70+ const url = pathToFileURL ( filepath ) . toString ( ) ;
71+
72+ if ( process . env . BABEL_8_BREAKING ) {
73+ return ( await import ( url ) ) . default ;
74+ } else {
75+ if ( ! import_ ) {
76+ throw new ConfigError (
77+ "Internal error: Native ECMAScript modules aren't supported by this platform.\n" ,
78+ filepath ,
79+ ) ;
80+ }
81+
82+ return ( await import_ ( url ) ) . default ;
83+ }
84+ } ) ;
85+
3686export default function * loadCodeDefault (
3787 filepath : string ,
3888 asyncError : string ,
@@ -68,7 +118,6 @@ export default function* loadCodeDefault(
68118 }
69119 }
70120 if ( yield * isAsync ( ) ) {
71- // eslint-disable-next-line @typescript-eslint/no-use-before-define
72121 return yield * waitFor ( loadMjsDefault ( filepath ) ) ;
73122 }
74123 throw new ConfigError ( asyncError , filepath ) ;
@@ -145,56 +194,6 @@ function loadCtsDefault(filepath: string) {
145194 }
146195}
147196
148- const LOADING_CJS_FILES = new Set ( ) ;
149-
150- function loadCjsDefault ( filepath : string ) {
151- // The `require()` call below can make this code reentrant if a require hook
152- // like @babel /register has been loaded into the system. That would cause
153- // Babel to attempt to compile the `.babelrc.js` file as it loads below. To
154- // cover this case, we auto-ignore re-entrant config processing. ESM loaders
155- // do not have this problem, because loaders do not apply to themselves.
156- if ( LOADING_CJS_FILES . has ( filepath ) ) {
157- debug ( "Auto-ignoring usage of config %o." , filepath ) ;
158- return { } ;
159- }
160-
161- let module ;
162- try {
163- LOADING_CJS_FILES . add ( filepath ) ;
164- module = endHiddenCallStack ( require ) ( filepath ) ;
165- } finally {
166- LOADING_CJS_FILES . delete ( filepath ) ;
167- }
168-
169- if ( process . env . BABEL_8_BREAKING ) {
170- return module ?. __esModule ? module . default : module ;
171- } else {
172- return module ?. __esModule
173- ? module . default ||
174- /* fallbackToTranspiledModule */ ( arguments [ 1 ] ? module : undefined )
175- : module ;
176- }
177- }
178-
179- const loadMjsDefault = endHiddenCallStack ( async function loadMjsDefault (
180- filepath : string ,
181- ) {
182- const url = pathToFileURL ( filepath ) . toString ( ) ;
183-
184- if ( process . env . BABEL_8_BREAKING ) {
185- return ( await import ( url ) ) . default ;
186- } else {
187- if ( ! import_ ) {
188- throw new ConfigError (
189- "Internal error: Native ECMAScript modules aren't supported by this platform.\n" ,
190- filepath ,
191- ) ;
192- }
193-
194- return ( await import_ ( url ) ) . default ;
195- }
196- } ) ;
197-
198197function getTSPreset ( filepath : string ) {
199198 try {
200199 // eslint-disable-next-line import/no-extraneous-dependencies
0 commit comments