11import type { Span } from '@opentelemetry/api'
2+ import type { StaticMockCall } from '@vitest/mocker/node'
23import type { DevEnvironment , EnvironmentModuleNode , Rollup , TransformResult } from 'vite'
34import type { FetchFunctionOptions , FetchResult } from 'vite/module-runner'
45import type { FetchCachedFileSystemResult , ModuleType , VitestFetchResult } from '../../types/general'
@@ -122,7 +123,7 @@ class ModuleFetcher {
122123 }
123124
124125 const tmpFile = join ( tmpDir , hash ( 'sha1' , result . id , 'hex' ) )
125- return this . cacheResult ( result , tmpFile ) . then ( ( result ) => {
126+ return this . cacheResult ( result , tmpFile , transformResult ) . then ( ( result ) => {
126127 if ( transformResult ) {
127128 transformResult . __vitestTmp = tmpFile
128129 }
@@ -148,7 +149,13 @@ class ModuleFetcher {
148149 const map = moduleGraphModule . transformResult ?. map
149150 const mappings = map && ! ( 'version' in map ) && map . mappings === ''
150151
151- const cachedResult = await this . cacheResult ( result , cachePath , importedUrls , ! ! mappings )
152+ const cachedResult = await this . cacheResult (
153+ result ,
154+ cachePath ,
155+ moduleGraphModule . transformResult ,
156+ importedUrls ,
157+ ! ! mappings ,
158+ )
152159 // remember where the code is stored on disk so that repeat fetches and the
153160 // `fetchWarmModules` snapshot can point at it in this session already, not
154161 // only after the cache is read back in the next one
@@ -287,8 +294,11 @@ class ModuleFetcher {
287294 code : cachedModule . code ,
288295 map,
289296 ssr : true ,
297+ deps : cachedModule . deps ,
298+ dynamicDeps : cachedModule . dynamicDeps ,
290299 __vitestTmp : cachePath ,
291300 __vitestModuleType : moduleType ,
301+ __vitestStaticMocks : cachedModule . staticMocks ,
292302 }
293303
294304 // we populate the module graph to make the watch mode work because it relies on importers
@@ -339,6 +349,10 @@ class ModuleFetcher {
339349 if ( 'code' in result ) {
340350 result . moduleType = await this . cachedModuleType ( result . file , result . code , moduleGraphModule . transformResult )
341351 }
352+ const transformResult = moduleGraphModule . transformResult
353+ if ( transformResult && moduleGraphModule . id ) {
354+ transformResult . __vitestStaticMocks ??= environment . pluginContainer . getModuleInfo ( moduleGraphModule . id ) ?. meta ?. vitestStaticMocks ?? null
355+ }
342356 return result
343357 }
344358
@@ -374,6 +388,7 @@ class ModuleFetcher {
374388 private async cacheResult (
375389 result : FetchResult ,
376390 cachePath : string ,
391+ transformResult : TransformResult | null ,
377392 importedUrls : string [ ] = [ ] ,
378393 mappings = false ,
379394 ) : Promise < FetchResult | FetchCachedFileSystemResult > {
@@ -386,7 +401,7 @@ class ModuleFetcher {
386401 }
387402
388403 const savePromise = this . fsCache
389- . saveCachedModule ( cachePath , result , importedUrls , mappings )
404+ . saveCachedModule ( cachePath , result , transformResult , importedUrls , mappings )
390405 . then ( ( ) => returnResult )
391406 . catch ( ( error ) => {
392407 debugFs ?.( `failed to cache ${ cachePath } , serving it inline: ${ error } ` )
@@ -588,5 +603,7 @@ declare module 'vite' {
588603 // `experimental.fsModuleCache` store or the forks pool's tmp copies
589604 __vitestTmp ?: string
590605 __vitestModuleType ?: ModuleType
606+ // set by the hoistMocks plugin; null when the file was not hoisted
607+ __vitestStaticMocks ?: StaticMockCall [ ] | null
591608 }
592609}
0 commit comments