@@ -188,22 +188,31 @@ export class ViteNodeRunner {
188
188
if ( importee )
189
189
mod . importers . add ( importee )
190
190
191
- // the callstack reference itself circularly
192
- if ( callstack . includes ( fsPath ) && mod . exports )
193
- return mod . exports
191
+ const getStack = ( ) => `stack:\n${ [ ...callstack , fsPath ] . reverse ( ) . map ( p => ` - ${ p } ` ) . join ( '\n' ) } `
194
192
195
- // cached module
196
- if ( mod . promise )
197
- return mod . promise
193
+ // check circular dependency
194
+ if ( callstack . includes ( fsPath ) || callstack . some ( c => this . moduleCache . get ( c ) . importers ?. has ( fsPath ) ) ) {
195
+ if ( mod . exports )
196
+ return mod . exports
197
+ }
198
198
199
- const promise = this . directRequest ( id , fsPath , callstack )
200
- Object . assign ( mod , { promise, evaluated : false } )
199
+ let debugTimer : any
200
+ if ( this . debug )
201
+ debugTimer = setTimeout ( ( ) => console . warn ( `[vite-node] module ${ fsPath } takes over 2s to load.\n${ getStack ( ) } ` ) , 2000 )
201
202
202
203
try {
204
+ // cached module
205
+ if ( mod . promise )
206
+ return await mod . promise
207
+
208
+ const promise = this . directRequest ( id , fsPath , callstack )
209
+ Object . assign ( mod , { promise, evaluated : false } )
203
210
return await promise
204
211
}
205
212
finally {
206
213
mod . evaluated = true
214
+ if ( debugTimer )
215
+ clearTimeout ( debugTimer )
207
216
}
208
217
}
209
218
@@ -245,28 +254,7 @@ export class ViteNodeRunner {
245
254
246
255
/** @internal */
247
256
async dependencyRequest ( id : string , fsPath : string , callstack : string [ ] ) {
248
- const getStack = ( ) => {
249
- return `stack:\n${ [ ...callstack , fsPath ] . reverse ( ) . map ( p => `- ${ p } ` ) . join ( '\n' ) } `
250
- }
251
-
252
- let debugTimer : any
253
- if ( this . debug )
254
- debugTimer = setTimeout ( ( ) => console . warn ( ( ) => `module ${ fsPath } takes over 2s to load.\n${ getStack ( ) } ` ) , 2000 )
255
-
256
- try {
257
- if ( callstack . includes ( fsPath ) ) {
258
- const depExports = this . moduleCache . get ( fsPath ) ?. exports
259
- if ( depExports )
260
- return depExports
261
- throw new Error ( `[vite-node] Failed to resolve circular dependency, ${ getStack ( ) } ` )
262
- }
263
-
264
- return await this . cachedRequest ( id , fsPath , callstack )
265
- }
266
- finally {
267
- if ( debugTimer )
268
- clearTimeout ( debugTimer )
269
- }
257
+ return await this . cachedRequest ( id , fsPath , callstack )
270
258
}
271
259
272
260
/** @internal */
0 commit comments