Skip to content

Commit

Permalink
chore(optimizer): debug info on cache dir handle process (#12858)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Sep 19, 2023
1 parent 4a1aab1 commit 21a62da
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export async function loadCachedDepOptimizationMetadata(
}

// Start with a fresh cache
debug?.(colors.green(`removing old cache dir ${depsCacheDir}`))
await fsp.rm(depsCacheDir, { recursive: true, force: true })
}

Expand Down Expand Up @@ -473,6 +474,7 @@ export function runOptimizeDeps(

// a hint for Node.js
// all files in the cache directory should be recognized as ES modules
debug?.(colors.green(`creating package.json in ${processingCacheDir}`))
fs.writeFileSync(
path.resolve(processingCacheDir, 'package.json'),
`{\n "type": "module"\n}\n`,
Expand All @@ -499,6 +501,7 @@ export function runOptimizeDeps(
cleaned = true
// No need to wait, we can clean up in the background because temp folders
// are unique per run
debug?.(colors.green(`removing cache dir ${processingCacheDir}`))
fsp.rm(processingCacheDir, { recursive: true, force: true }).catch(() => {
// Ignore errors
})
Expand All @@ -521,6 +524,7 @@ export function runOptimizeDeps(
// Write metadata file, then commit the processing folder to the global deps cache
// Rewire the file paths from the temporal processing dir to the final deps cache dir
const dataPath = path.join(processingCacheDir, '_metadata.json')
debug?.(colors.green(`creating _metadata.json in ${processingCacheDir}`))
fs.writeFileSync(
dataPath,
stringifyDepsOptimizerMetadata(metadata, depsCacheDir),
Expand All @@ -537,16 +541,30 @@ export function runOptimizeDeps(
const temporalPath = depsCacheDir + getTempSuffix()
const depsCacheDirPresent = fs.existsSync(depsCacheDir)
if (isWindows) {
if (depsCacheDirPresent) await safeRename(depsCacheDir, temporalPath)
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporalPath}`))
await safeRename(depsCacheDir, temporalPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
await safeRename(processingCacheDir, depsCacheDir)
} else {
if (depsCacheDirPresent) fs.renameSync(depsCacheDir, temporalPath)
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporalPath}`))
fs.renameSync(depsCacheDir, temporalPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
fs.renameSync(processingCacheDir, depsCacheDir)
}

// Delete temporal path in the background
if (depsCacheDirPresent)
if (depsCacheDirPresent) {
debug?.(colors.green(`removing cache temp dir ${temporalPath}`))
fsp.rm(temporalPath, { recursive: true, force: true })
}
},
}

Expand Down Expand Up @@ -1309,6 +1327,7 @@ export async function cleanupDepsCacheStaleDirs(
stats?.mtime &&
Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS
) {
debug?.(`removing stale cache temp dir ${tempDirPath}`)
await fsp.rm(tempDirPath, { recursive: true, force: true })
}
}
Expand Down

0 comments on commit 21a62da

Please sign in to comment.