( environment: Environment, force: boolean = environment.config.optimizeDeps.force ?? false, asCommand = false, )
| 381 | * if it exists and pre-bundling isn't forced |
| 382 | */ |
| 383 | export async function loadCachedDepOptimizationMetadata( |
| 384 | environment: Environment, |
| 385 | force: boolean = environment.config.optimizeDeps.force ?? false, |
| 386 | asCommand = false, |
| 387 | ): Promise<DepOptimizationMetadata | undefined> { |
| 388 | const log = asCommand ? environment.logger.info : debug |
| 389 | |
| 390 | if (firstLoadCachedDepOptimizationMetadata) { |
| 391 | firstLoadCachedDepOptimizationMetadata = false |
| 392 | // Fire up a clean up of stale processing deps dirs if older process exited early |
| 393 | setTimeout( |
| 394 | () => cleanupDepsCacheStaleDirs(environment.getTopLevelConfig()), |
| 395 | 0, |
| 396 | ).unref() |
| 397 | } |
| 398 | |
| 399 | const depsCacheDir = getDepsCacheDir(environment) |
| 400 | |
| 401 | // When run inside Vite Task, the dep optimizer cache is both read and |
| 402 | // written under this directory (metadata + pre-bundled deps). Tell Vite |
| 403 | // Task to treat it as neither a build input nor a build output: the |
| 404 | // lockfile hash stored in the metadata already drives re-optimization, |
| 405 | // and the cache is process-local scratch space, not a build artifact. |
| 406 | ignoreInput(depsCacheDir) |
| 407 | ignoreOutput(depsCacheDir) |
| 408 | |
| 409 | if (!force) { |
| 410 | let cachedMetadata: DepOptimizationMetadata | undefined |
| 411 | try { |
| 412 | const cachedMetadataPath = path.join(depsCacheDir, METADATA_FILENAME) |
| 413 | cachedMetadata = parseDepsOptimizerMetadata( |
| 414 | await fsp.readFile(cachedMetadataPath, 'utf-8'), |
| 415 | depsCacheDir, |
| 416 | ) |
| 417 | } catch {} |
| 418 | // hash is consistent, no need to re-bundle |
| 419 | if (cachedMetadata) { |
| 420 | if (cachedMetadata.lockfileHash !== getLockfileHash(environment)) { |
| 421 | environment.logger.info( |
| 422 | 'Re-optimizing dependencies because lockfile has changed', |
| 423 | { |
| 424 | timestamp: true, |
| 425 | }, |
| 426 | ) |
| 427 | } else if (cachedMetadata.configHash !== getConfigHash(environment)) { |
| 428 | environment.logger.info( |
| 429 | 'Re-optimizing dependencies because vite config has changed', |
| 430 | { |
| 431 | timestamp: true, |
| 432 | }, |
| 433 | ) |
| 434 | } else { |
| 435 | log?.( |
| 436 | `(${environment.name}) Hash is consistent. Skipping. Use --force to override.`, |
| 437 | ) |
| 438 | // Nothing to commit or cancel as we are using the cache, we only |
| 439 | // need to resolve the processing promise so requests can move on |
| 440 | return cachedMetadata |
no test coverage detected