@@ -42,7 +42,7 @@ const saveCachePromises = new Map<
4242> ( )
4343const readFilePromises = new Map < string , Promise < string | null > > ( )
4444
45- class ModuleFetcher {
45+ export class ModuleTransformService {
4646 private tmpDirectories = new Set < string > ( )
4747 private fsCacheEnabled : boolean
4848 // the module type is only needed by the evaluator to decide if CJS
@@ -54,13 +54,48 @@ class ModuleFetcher {
5454 private resolver : VitestResolver ,
5555 private config : ResolvedConfig ,
5656 private fsCache : FileSystemModuleCache ,
57+ private traces : Traces ,
5758 private tmpProjectDir : string ,
5859 ) {
5960 this . fsCacheEnabled = config . fsModuleCache === true
6061 this . detectModuleType = config . injectCjsGlobals === false
6162 }
6263
6364 async fetch (
65+ url : string ,
66+ importer : string | undefined ,
67+ environment : DevEnvironment ,
68+ makeTmpCopies ?: boolean ,
69+ options ?: FetchFunctionOptions ,
70+ otelCarrier ?: OTELCarrier ,
71+ ) : Promise < FetchResult | FetchCachedFileSystemResult > {
72+ await this . traces . waitInit ( )
73+ const context = otelCarrier ? this . traces . getContextFromCarrier ( otelCarrier ) : undefined
74+ return this . traces . $ ( 'vitest.module.transform' , context ? { context } : { } , ( span ) =>
75+ this . fetchInSpan ( span , url , importer , environment , makeTmpCopies , options ) ,
76+ )
77+ }
78+
79+ /**
80+ * Transforms the module like `environment.transformRequest`, but reads and
81+ * populates the `fsModuleCache` when it is enabled.
82+ */
83+ async transform ( id : string , environment : DevEnvironment ) : Promise < TransformResult | null > {
84+ await this . traces . waitInit ( )
85+ const result = await this . traces . $ ( 'vitest.module.transform' , ( span ) =>
86+ this . fetchInSpan ( span , id , undefined , environment ) ,
87+ )
88+ if ( 'id' in result ) {
89+ const transformResult = environment . moduleGraph . getModuleById ( result . id ) ?. transformResult
90+ if ( transformResult ) {
91+ return transformResult
92+ }
93+ }
94+ // externalized modules are never transformed by `fetch`
95+ return environment . transformRequest ( id )
96+ }
97+
98+ private async fetchInSpan (
6499 trace : Span ,
65100 url : string ,
66101 importer : string | undefined ,
@@ -531,34 +566,6 @@ function getSafeModulePaths(environment: DevEnvironment): Set<string> | undefine
531566 return config . safeModulePaths
532567}
533568
534- export interface VitestFetchFunction {
535- (
536- url : string ,
537- importer : string | undefined ,
538- environment : DevEnvironment ,
539- cacheFs ?: boolean ,
540- options ?: FetchFunctionOptions ,
541- otelCarrier ?: OTELCarrier ,
542- ) : Promise < FetchResult | FetchCachedFileSystemResult >
543- }
544-
545- export function createFetchModuleFunction (
546- resolver : VitestResolver ,
547- config : ResolvedConfig ,
548- fsCache : FileSystemModuleCache ,
549- traces : Traces ,
550- tmpProjectDir : string ,
551- ) : VitestFetchFunction {
552- const fetcher = new ModuleFetcher ( resolver , config , fsCache , tmpProjectDir )
553- return async ( url , importer , environment , cacheFs , options , otelCarrier ) => {
554- await traces . waitInit ( )
555- const context = otelCarrier ? traces . getContextFromCarrier ( otelCarrier ) : undefined
556- return traces . $ ( 'vitest.module.transform' , context ? { context } : { } , ( span ) =>
557- fetcher . fetch ( span , url , importer , environment , cacheFs , options ) ,
558- )
559- }
560- }
561-
562569let SOURCEMAPPING_URL = 'sourceMa'
563570SOURCEMAPPING_URL += 'ppingURL'
564571
0 commit comments