@@ -58,10 +58,10 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
58
58
/**
59
59
* Assign partial data to the map
60
60
*/
61
- update ( fsPath : string , mod : Partial < ModuleCache > ) {
61
+ update ( fsPath : string , mod : ModuleCache ) {
62
62
fsPath = this . normalizePath ( fsPath )
63
63
if ( ! super . has ( fsPath ) )
64
- super . set ( fsPath , mod )
64
+ this . setByModuleId ( fsPath , mod )
65
65
else
66
66
Object . assign ( super . get ( fsPath ) as ModuleCache , mod )
67
67
return this
@@ -75,13 +75,21 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
75
75
return this . setByModuleId ( this . normalizePath ( fsPath ) , mod )
76
76
}
77
77
78
- getByModuleId ( modulePath : string ) : ModuleCache {
78
+ getByModuleId ( modulePath : string ) {
79
79
if ( ! super . has ( modulePath ) )
80
- super . set ( modulePath , { } )
81
- return super . get ( modulePath ) !
80
+ this . setByModuleId ( modulePath , { } )
81
+
82
+ const mod = super . get ( modulePath ) !
83
+ if ( ! mod . imports ) {
84
+ Object . assign ( mod , {
85
+ imports : new Set ( ) ,
86
+ importers : new Set ( ) ,
87
+ } )
88
+ }
89
+ return mod as ModuleCache & Required < Pick < ModuleCache , 'imports' | 'importers' > >
82
90
}
83
91
84
- get ( fsPath : string ) : ModuleCache {
92
+ get ( fsPath : string ) {
85
93
return this . getByModuleId ( this . normalizePath ( fsPath ) )
86
94
}
87
95
@@ -99,6 +107,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
99
107
delete mod . promise
100
108
delete mod . exports
101
109
mod . importers ?. clear ( )
110
+ mod . imports ?. clear ( )
102
111
return true
103
112
}
104
113
@@ -185,16 +194,15 @@ export class ViteNodeRunner {
185
194
const importee = callstack [ callstack . length - 1 ]
186
195
187
196
const mod = this . moduleCache . get ( fsPath )
197
+ const { imports, importers } = mod
188
198
189
- if ( ! mod . importers )
190
- mod . importers = new Set ( )
191
199
if ( importee )
192
- mod . importers . add ( importee )
200
+ importers . add ( importee )
193
201
194
202
const getStack = ( ) => `stack:\n${ [ ...callstack , fsPath ] . reverse ( ) . map ( p => ` - ${ p } ` ) . join ( '\n' ) } `
195
203
196
204
// check circular dependency
197
- if ( callstack . includes ( fsPath ) || callstack . some ( c => this . moduleCache . get ( c ) . importers ? .has ( fsPath ) ) ) {
205
+ if ( callstack . includes ( fsPath ) || Array . from ( imports . values ( ) ) . some ( i => importers . has ( i ) ) ) {
198
206
if ( mod . exports )
199
207
return mod . exports
200
208
}
@@ -269,6 +277,10 @@ export class ViteNodeRunner {
269
277
270
278
const request = async ( dep : string ) => {
271
279
const [ id , depFsPath ] = await this . resolveUrl ( `${ dep } ` , fsPath )
280
+ const depMod = this . moduleCache . getByModuleId ( depFsPath )
281
+ depMod . importers . add ( moduleId )
282
+ mod . imports . add ( depFsPath )
283
+
272
284
return this . dependencyRequest ( id , depFsPath , callstack )
273
285
}
274
286
0 commit comments