Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): imporove cache performance and resolve cache mistake #344

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache data does not need alwawys read


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))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write file need add throttle

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is point one!


if (!s.hasChanged())
return

await updateCacheImports(id, res.imports)

writeConfigFilesThrottled()

return {
Expand Down