Skip to content

Commit 75c2f66

Browse files
committed
refactor(vite): refactor optimizer
1 parent ec16a5e commit 75c2f66

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

packages/vite/src/node/optimizer/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,17 @@ export function runOptimizeDeps(
547547
// is safer than a delete-rename operation.
548548
const temporaryPath = depsCacheDir + getTempSuffix()
549549
const depsCacheDirPresent = fs.existsSync(depsCacheDir)
550+
if (depsCacheDirPresent) {
551+
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
552+
await safeRename(depsCacheDir, temporaryPath)
553+
}
554+
550555
if (isWindows) {
551-
if (depsCacheDirPresent) {
552-
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
553-
await safeRename(depsCacheDir, temporaryPath)
554-
}
555556
debug?.(
556557
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
557558
)
558559
await safeRename(processingCacheDir, depsCacheDir)
559560
} else {
560-
if (depsCacheDirPresent) {
561-
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
562-
fs.renameSync(depsCacheDir, temporaryPath)
563-
}
564561
debug?.(
565562
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
566563
)
@@ -1291,19 +1288,23 @@ export async function cleanupDepsCacheStaleDirs(
12911288
const cacheDir = path.resolve(config.cacheDir)
12921289
if (fs.existsSync(cacheDir)) {
12931290
const dirents = await fsp.readdir(cacheDir, { withFileTypes: true })
1291+
const promises = []
12941292
for (const dirent of dirents) {
12951293
if (dirent.isDirectory() && dirent.name.includes('_temp_')) {
1296-
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
1297-
const stats = await fsp.stat(tempDirPath).catch((_) => null)
1298-
if (
1299-
stats?.mtime &&
1300-
Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS
1301-
) {
1302-
debug?.(`removing stale cache temp dir ${tempDirPath}`)
1303-
await fsp.rm(tempDirPath, { recursive: true, force: true })
1304-
}
1294+
promises.push(async () => {
1295+
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
1296+
const stats = await fsp.stat(tempDirPath).catch((_) => null)
1297+
if (
1298+
stats?.mtime &&
1299+
Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS
1300+
) {
1301+
debug?.(`removing stale cache temp dir ${tempDirPath}`)
1302+
await fsp.rm(tempDirPath, { recursive: true, force: true })
1303+
}
1304+
})
13051305
}
13061306
}
1307+
if (promises.length) await Promise.all(promises)
13071308
}
13081309
} catch (err) {
13091310
config.logger.error(err)

packages/vite/src/node/optimizer/optimizer.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,15 @@ function findInteropMismatches(
790790
const needsInteropMismatch = []
791791
for (const dep in discovered) {
792792
const discoveredDepInfo = discovered[dep]
793+
if (discoveredDepInfo.needsInterop === undefined) continue
794+
793795
const depInfo = optimized[dep]
794-
if (depInfo) {
795-
if (
796-
discoveredDepInfo.needsInterop !== undefined &&
797-
depInfo.needsInterop !== discoveredDepInfo.needsInterop
798-
) {
799-
// This only happens when a discovered dependency has mixed ESM and CJS syntax
800-
// and it hasn't been manually added to optimizeDeps.needsInterop
801-
needsInteropMismatch.push(dep)
802-
debug?.(colors.cyan(`✨ needsInterop mismatch detected for ${dep}`))
803-
}
796+
797+
if (depInfo.needsInterop !== discoveredDepInfo.needsInterop) {
798+
// This only happens when a discovered dependency has mixed ESM and CJS syntax
799+
// and it hasn't been manually added to optimizeDeps.needsInterop
800+
needsInteropMismatch.push(dep)
801+
debug?.(colors.cyan(`✨ needsInterop mismatch detected for ${dep}`))
804802
}
805803
}
806804
return needsInteropMismatch

0 commit comments

Comments
 (0)