@@ -97,6 +97,7 @@ ${dts}`.trim()}\n`
97
97
}
98
98
99
99
const writeConfigFilesThrottled = throttle ( 500 , writeConfigFiles , { noLeading : false } )
100
+ const writeFileThrottled = throttle ( 500 , writeFile , { noLeading : false } )
100
101
101
102
async function writeFile ( filePath : string , content = '' ) {
102
103
await fs . mkdir ( dirname ( filePath ) , { recursive : true } )
@@ -151,12 +152,13 @@ ${dts}`.trim()}\n`
151
152
return JSON . parse ( str || '{}' ) as { [ key : string ] : Import [ ] }
152
153
}
153
154
154
- async function generateCache ( ) {
155
+ async function generateCache ( ) : Promise < Record < string , Import [ ] > > {
155
156
if ( ! cachePath )
156
- return
157
+ return { }
157
158
159
+ let cacheData = { }
158
160
try {
159
- const cacheData = await getCacheData ( cachePath )
161
+ cacheData = await getCacheData ( cachePath )
160
162
await Promise . allSettled ( Object . keys ( cacheData ) . map ( async ( filePath ) => {
161
163
try {
162
164
const normalizeRoot = root . replaceAll ( sep , posix . sep )
@@ -171,6 +173,8 @@ ${dts}`.trim()}\n`
171
173
catch {
172
174
await writeFile ( cachePath , '{}' )
173
175
}
176
+
177
+ return cacheData
174
178
}
175
179
176
180
let isInitialCache = false
@@ -180,18 +184,16 @@ ${dts}`.trim()}\n`
180
184
return
181
185
182
186
isInitialCache = true
183
- await resolveCachePromise
187
+ const cacheData = await resolveCachePromise
184
188
await unimport . modifyDynamicImports ( async ( imports ) => {
185
- const cacheData = await getCacheData ( cachePath )
186
-
187
189
if ( id && importList ) {
188
190
const filePath = posix . normalize ( posix . relative ( root , id ) )
189
191
importList = importList . filter ( i => ( i . name ?? i . as ) && i . name !== 'default' )
190
192
if ( importList . length )
191
193
cacheData [ filePath ] = importList
192
194
else
193
195
delete cacheData [ filePath ]
194
- await writeFile ( cachePath , JSON . stringify ( cacheData , null , 2 ) )
196
+ writeFileThrottled ( cachePath , JSON . stringify ( cacheData , null , 2 ) )
195
197
return imports . concat ( importList )
196
198
}
197
199
@@ -206,11 +208,11 @@ ${dts}`.trim()}\n`
206
208
207
209
const res = await unimport . injectImports ( s , id )
208
210
211
+ await updateCacheImports ( id , res . imports )
212
+
209
213
if ( ! s . hasChanged ( ) )
210
214
return
211
215
212
- await updateCacheImports ( id , res . imports )
213
-
214
216
writeConfigFilesThrottled ( )
215
217
216
218
return {
0 commit comments