@@ -18,22 +18,26 @@ interface DumpOptions {
1818 readFromDump ?: boolean
1919}
2020
21+ export interface VitestFetchFunction {
22+ (
23+ url : string ,
24+ importer : string | undefined ,
25+ environment : DevEnvironment ,
26+ cacheFs : boolean ,
27+ options ?: FetchFunctionOptions
28+ ) : Promise < FetchResult | FetchCachedFileSystemResult >
29+ }
30+
2131export function createFetchModuleFunction (
2232 resolver : VitestResolver ,
23- cacheFs : boolean = false ,
2433 tmpDir : string = join ( tmpdir ( ) , nanoid ( ) ) ,
2534 dump ?: DumpOptions ,
26- ) : (
27- url : string ,
28- importer : string | undefined ,
29- environment : DevEnvironment ,
30- options ?: FetchFunctionOptions
31- ) => Promise < FetchResult | FetchCachedFileSystemResult > {
32- const cachedFsResults = new Map < string , string > ( )
35+ ) : VitestFetchFunction {
3336 return async (
3437 url ,
3538 importer ,
3639 environment ,
40+ cacheFs ,
3741 options ,
3842 ) => {
3943 // We are copy pasting Vite's externalization logic from `fetchModule` because
@@ -117,10 +121,14 @@ export function createFetchModuleFunction(
117121 }
118122
119123 const code = result . code
124+ const transformResult = result . transformResult !
125+ if ( ! transformResult ) {
126+ throw new Error ( `"transformResult" in not defined. This is a bug in Vitest.` )
127+ }
120128 // to avoid serialising large chunks of code,
121129 // we store them in a tmp file and read in the test thread
122- if ( cachedFsResults . has ( result . id ) ) {
123- return getCachedResult ( result , cachedFsResults )
130+ if ( '_vitestTmp' in transformResult ) {
131+ return getCachedResult ( result , Reflect . get ( transformResult as any , '_vitestTmp' ) )
124132 }
125133 const dir = join ( tmpDir , environment . name )
126134 const name = hash ( 'sha1' , result . id , 'hex' )
@@ -131,8 +139,8 @@ export function createFetchModuleFunction(
131139 }
132140 if ( promises . has ( tmp ) ) {
133141 await promises . get ( tmp )
134- cachedFsResults . set ( result . id , tmp )
135- return getCachedResult ( result , cachedFsResults )
142+ Reflect . set ( transformResult , '_vitestTmp' , tmp )
143+ return getCachedResult ( result , tmp )
136144 }
137145 promises . set (
138146 tmp ,
@@ -143,8 +151,7 @@ export function createFetchModuleFunction(
143151 . finally ( ( ) => promises . delete ( tmp ) ) ,
144152 )
145153 await promises . get ( tmp )
146- cachedFsResults . set ( result . id , tmp )
147- return getCachedResult ( result , cachedFsResults )
154+ return getCachedResult ( result , tmp )
148155 }
149156}
150157
@@ -153,7 +160,9 @@ SOURCEMAPPING_URL += 'ppingURL'
153160
154161const MODULE_RUNNER_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-generated'
155162
156- function processResultSource ( environment : DevEnvironment , result : FetchResult ) : FetchResult {
163+ function processResultSource ( environment : DevEnvironment , result : FetchResult ) : FetchResult & {
164+ transformResult ?: TransformResult | null
165+ } {
157166 if ( ! ( 'code' in result ) ) {
158167 return result
159168 }
@@ -168,6 +177,7 @@ function processResultSource(environment: DevEnvironment, result: FetchResult):
168177 return {
169178 ...result ,
170179 code : node ?. transformResult ?. code || result . code ,
180+ transformResult : node ?. transformResult ,
171181 }
172182}
173183
@@ -220,11 +230,7 @@ function genSourceMapUrl(map: Rollup.SourceMap | string): string {
220230 return `data:application/json;base64,${ Buffer . from ( map ) . toString ( 'base64' ) } `
221231}
222232
223- function getCachedResult ( result : Extract < FetchResult , { code : string } > , cachedFsResults : Map < string , string > ) : FetchCachedFileSystemResult {
224- const tmp = cachedFsResults . get ( result . id )
225- if ( ! tmp ) {
226- throw new Error ( `The cached result was returned too early for ${ result . id } .` )
227- }
233+ function getCachedResult ( result : Extract < FetchResult , { code : string } > , tmp : string ) : FetchCachedFileSystemResult {
228234 return {
229235 cached : true as const ,
230236 file : result . file ,
0 commit comments