@@ -19,6 +19,8 @@ const cacheCommentLength = cacheComment.length
1919
2020const METADATA_FILE = '_metadata.json'
2121
22+ const parallelFsCacheRead = new Map < string , Promise < { code : string ; meta : CachedInlineModuleMeta } | undefined > > ( )
23+
2224/**
2325 * @experimental
2426 */
@@ -68,28 +70,38 @@ export class FileSystemModuleCache {
6870 }
6971 }
7072
73+ private readCachedFileConcurrently ( cachedFilePath : string ) {
74+ if ( ! parallelFsCacheRead . has ( cachedFilePath ) ) {
75+ parallelFsCacheRead . set ( cachedFilePath , readFile ( cachedFilePath , 'utf-8' ) . then ( ( code ) => {
76+ const matchIndex = code . lastIndexOf ( cacheComment )
77+ if ( matchIndex === - 1 ) {
78+ debugFs ?.( `${ c . red ( '[empty]' ) } ${ cachedFilePath } exists, but doesn't have a ${ cacheComment } comment, transforming by vite instead` )
79+ return
80+ }
81+
82+ return { code, meta : this . fromBase64 ( code . slice ( matchIndex + cacheCommentLength ) ) }
83+ } ) . finally ( ( ) => {
84+ parallelFsCacheRead . delete ( cachedFilePath )
85+ } ) )
86+ }
87+ return parallelFsCacheRead . get ( cachedFilePath ) !
88+ }
89+
7190 async getCachedModule ( cachedFilePath : string ) : Promise <
7291 CachedInlineModuleMeta
73- | Extract < FetchResult , { externalize : string } >
7492 | undefined
7593 > {
7694 if ( ! existsSync ( cachedFilePath ) ) {
7795 debugFs ?.( `${ c . red ( '[empty]' ) } ${ cachedFilePath } doesn't exist, transforming by vite instead` )
7896 return
7997 }
8098
81- const code = await readFile ( cachedFilePath , 'utf-8' )
82- const matchIndex = code . lastIndexOf ( cacheComment )
83- if ( matchIndex === - 1 ) {
84- debugFs ?.( `${ c . red ( '[empty]' ) } ${ cachedFilePath } exists, but doesn't have a ${ cacheComment } comment, transforming by vite instead` )
99+ const fileResult = await this . readCachedFileConcurrently ( cachedFilePath )
100+ if ( ! fileResult ) {
85101 return
86102 }
103+ const { code, meta } = fileResult
87104
88- const meta = this . fromBase64 ( code . slice ( matchIndex + cacheCommentLength ) )
89- if ( meta . externalize ) {
90- debugFs ?.( `${ c . green ( '[read]' ) } ${ meta . externalize } is externalized inside ${ cachedFilePath } ` )
91- return { externalize : meta . externalize , type : meta . type }
92- }
93105 debugFs ?.( `${ c . green ( '[read]' ) } ${ meta . id } is cached in ${ cachedFilePath } ` )
94106
95107 return {
@@ -108,11 +120,7 @@ export class FileSystemModuleCache {
108120 importers : string [ ] = [ ] ,
109121 mappings : boolean = false ,
110122 ) : Promise < void > {
111- if ( 'externalize' in fetchResult ) {
112- debugFs ?.( `${ c . yellow ( '[write]' ) } ${ fetchResult . externalize } is externalized inside ${ cachedFilePath } ` )
113- await atomicWriteFile ( cachedFilePath , `${ cacheComment } ${ this . toBase64 ( fetchResult ) } ` )
114- }
115- else if ( 'code' in fetchResult ) {
123+ if ( 'code' in fetchResult ) {
116124 const result = {
117125 file : fetchResult . file ,
118126 id : fetchResult . id ,
@@ -169,8 +177,6 @@ export class FileSystemModuleCache {
169177 id : string ,
170178 fileContent : string ,
171179 ) : string | null {
172- let hashString = ''
173-
174180 // bail out if file has import.meta.glob because it depends on other files
175181 // TODO: figure out a way to still support it
176182 if ( fileContent . includes ( 'import.meta.glob(' ) ) {
@@ -179,6 +185,8 @@ export class FileSystemModuleCache {
179185 return null
180186 }
181187
188+ let hashString = ''
189+
182190 for ( const generator of this . fsCacheKeyGenerators ) {
183191 const result = generator ( { environment, id, sourceCode : fileContent } )
184192 if ( typeof result === 'string' ) {
@@ -214,13 +222,6 @@ export class FileSystemModuleCache {
214222 environment : environment . name ,
215223 // this affects Vitest CSS plugin
216224 css : vitestConfig . css ,
217- // this affect externalization
218- resolver : {
219- inline : resolver . options . inline ,
220- external : resolver . options . external ,
221- inlineFiles : resolver . options . inlineFiles ,
222- moduleDirectories : resolver . options . moduleDirectories ,
223- } ,
224225 } ,
225226 ( _ , value ) => {
226227 if ( typeof value === 'function' || value instanceof RegExp ) {
0 commit comments