Skip to content

Commit

Permalink
refactor(vite): refactor optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Jun 23, 2024
1 parent ec16a5e commit 75c2f66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
35 changes: 18 additions & 17 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,20 +547,17 @@ export function runOptimizeDeps(
// is safer than a delete-rename operation.
const temporaryPath = depsCacheDir + getTempSuffix()
const depsCacheDirPresent = fs.existsSync(depsCacheDir)
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
await safeRename(depsCacheDir, temporaryPath)
}

if (isWindows) {
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
await safeRename(depsCacheDir, temporaryPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
await safeRename(processingCacheDir, depsCacheDir)
} else {
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
fs.renameSync(depsCacheDir, temporaryPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
Expand Down Expand Up @@ -1291,19 +1288,23 @@ export async function cleanupDepsCacheStaleDirs(
const cacheDir = path.resolve(config.cacheDir)
if (fs.existsSync(cacheDir)) {
const dirents = await fsp.readdir(cacheDir, { withFileTypes: true })
const promises = []
for (const dirent of dirents) {
if (dirent.isDirectory() && dirent.name.includes('_temp_')) {
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
const stats = await fsp.stat(tempDirPath).catch((_) => null)
if (
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 })
}
promises.push(async () => {
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
const stats = await fsp.stat(tempDirPath).catch((_) => null)
if (
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 })
}
})
}
}
if (promises.length) await Promise.all(promises)
}
} catch (err) {
config.logger.error(err)
Expand Down
18 changes: 8 additions & 10 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,17 +790,15 @@ function findInteropMismatches(
const needsInteropMismatch = []
for (const dep in discovered) {
const discoveredDepInfo = discovered[dep]
if (discoveredDepInfo.needsInterop === undefined) continue

const depInfo = optimized[dep]
if (depInfo) {
if (
discoveredDepInfo.needsInterop !== undefined &&
depInfo.needsInterop !== discoveredDepInfo.needsInterop
) {
// This only happens when a discovered dependency has mixed ESM and CJS syntax
// and it hasn't been manually added to optimizeDeps.needsInterop
needsInteropMismatch.push(dep)
debug?.(colors.cyan(`✨ needsInterop mismatch detected for ${dep}`))
}

if (depInfo.needsInterop !== discoveredDepInfo.needsInterop) {
// This only happens when a discovered dependency has mixed ESM and CJS syntax
// and it hasn't been manually added to optimizeDeps.needsInterop
needsInteropMismatch.push(dep)
debug?.(colors.cyan(`✨ needsInterop mismatch detected for ${dep}`))
}
}
return needsInteropMismatch
Expand Down

0 comments on commit 75c2f66

Please sign in to comment.