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 d5dab80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 13 additions & 9 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1291,19 +1291,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 d5dab80

Please sign in to comment.