Skip to content

Commit

Permalink
fix(cache): imporove cache performance and resolve cache mistake (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaobos authored Mar 9, 2023
1 parent c41e526 commit c5a5fcc
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ ${dts}`.trim()}\n`
}

const writeConfigFilesThrottled = throttle(500, writeConfigFiles, { noLeading: false })
const writeFileThrottled = throttle(500, writeFile, { noLeading: false })

async function writeFile(filePath: string, content = '') {
await fs.mkdir(dirname(filePath), { recursive: true })
Expand Down Expand Up @@ -151,12 +152,13 @@ ${dts}`.trim()}\n`
return JSON.parse(str || '{}') as { [key: string]: Import[] }
}

async function generateCache() {
async function generateCache(): Promise<Record<string, Import[]>> {
if (!cachePath)
return
return {}

let cacheData = {}
try {
const cacheData = await getCacheData(cachePath)
cacheData = await getCacheData(cachePath)
await Promise.allSettled(Object.keys(cacheData).map(async (filePath) => {
try {
const normalizeRoot = root.replaceAll(sep, posix.sep)
Expand All @@ -171,6 +173,8 @@ ${dts}`.trim()}\n`
catch {
await writeFile(cachePath, '{}')
}

return cacheData
}

let isInitialCache = false
Expand All @@ -180,18 +184,16 @@ ${dts}`.trim()}\n`
return

isInitialCache = true
await resolveCachePromise
const cacheData = await resolveCachePromise
await unimport.modifyDynamicImports(async (imports) => {
const cacheData = await getCacheData(cachePath)

if (id && importList) {
const filePath = posix.normalize(posix.relative(root, id))
importList = importList.filter(i => (i.name ?? i.as) && i.name !== 'default')
if (importList.length)
cacheData[filePath] = importList
else
delete cacheData[filePath]
await writeFile(cachePath, JSON.stringify(cacheData, null, 2))
writeFileThrottled(cachePath, JSON.stringify(cacheData, null, 2))
return imports.concat(importList)
}

Expand All @@ -206,11 +208,11 @@ ${dts}`.trim()}\n`

const res = await unimport.injectImports(s, id)

await updateCacheImports(id, res.imports)

if (!s.hasChanged())
return

await updateCacheImports(id, res.imports)

writeConfigFilesThrottled()

return {
Expand Down

0 comments on commit c5a5fcc

Please sign in to comment.