Home / Function/ runOptimizeDeps() — vite Function Reference

runOptimizeDeps() — vite Function Reference

Architecture documentation for the runOptimizeDeps() function in index.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  b53437fa_d2e4_a560_0d14_dfc84f740176["runOptimizeDeps()"]
  2f328851_91ee_fb05_63f0_4f466b9d6250["index.ts"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|defined in| 2f328851_91ee_fb05_63f0_4f466b9d6250
  d04f1f71_b435_f5c3_dc33_d847fd576eaf["optimizeDeps()"]
  d04f1f71_b435_f5c3_dc33_d847fd576eaf -->|calls| b53437fa_d2e4_a560_0d14_dfc84f740176
  9810a1ae_08e1_202d_3fb0_3cc8b579cb87["optimizeExplicitEnvironmentDeps()"]
  9810a1ae_08e1_202d_3fb0_3cc8b579cb87 -->|calls| b53437fa_d2e4_a560_0d14_dfc84f740176
  d40c38f0_b6c1_b93f_a735_b3f1e17bea5b["createDepsOptimizer()"]
  d40c38f0_b6c1_b93f_a735_b3f1e17bea5b -->|calls| b53437fa_d2e4_a560_0d14_dfc84f740176
  178d0f68_5aa9_9f89_6fb4_a6b2acfb2c07["getDepsCacheDir()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 178d0f68_5aa9_9f89_6fb4_a6b2acfb2c07
  219e2afa_89d3_f956_ec3c_dc5f331bf93b["getProcessingDepsCacheDir()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 219e2afa_89d3_f956_ec3c_dc5f331bf93b
  54f1d371_b3ac_a02d_d56e_00234dedc459["initDepsOptimizerMetadata()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 54f1d371_b3ac_a02d_d56e_00234dedc459
  b4bab689_2525_9144_6dd0_9a61aa65ad6a["getOptimizedBrowserHash()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| b4bab689_2525_9144_6dd0_9a61aa65ad6a
  66cd079f_6ff8_40a5_1497_cfdafb7791b1["depsFromOptimizedDepInfo()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 66cd079f_6ff8_40a5_1497_cfdafb7791b1
  ad1ec3b0_3e16_129e_0edb_116fa393a5d0["stringifyDepsOptimizerMetadata()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| ad1ec3b0_3e16_129e_0edb_116fa393a5d0
  3f085f78_71b1_a901_2b8a_6a8e5283a870["getTempSuffix()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 3f085f78_71b1_a901_2b8a_6a8e5283a870
  a001ac60_921b_cd0e_e2f0_cd371881facf["safeRename()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| a001ac60_921b_cd0e_e2f0_cd371881facf
  27cb44a2_ca79_d2a4_e536_a5e4bc22ffbf["prepareRolldownOptimizerRun()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 27cb44a2_ca79_d2a4_e536_a5e4bc22ffbf
  1cc52ea9_1097_389c_806c_c1207629afcc["flattenId()"]
  b53437fa_d2e4_a560_0d14_dfc84f740176 -->|calls| 1cc52ea9_1097_389c_806c_c1207629afcc
  style b53437fa_d2e4_a560_0d14_dfc84f740176 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/optimizer/index.ts lines 511–749

export function runOptimizeDeps(
  environment: Environment,
  depsInfo: Record<string, OptimizedDepInfo>,
): {
  cancel: () => Promise<void>
  result: Promise<DepOptimizationResult>
} {
  const optimizerContext = { cancelled: false }

  const depsCacheDir = getDepsCacheDir(environment)
  const processingCacheDir = getProcessingDepsCacheDir(environment)

  // Create a temporary directory so we don't need to delete optimized deps
  // until they have been processed. This also avoids leaving the deps cache
  // directory in a corrupted state if there is an error
  fs.mkdirSync(processingCacheDir, { recursive: true })

  // 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`,
  )

  const metadata = initDepsOptimizerMetadata(environment)

  metadata.browserHash = getOptimizedBrowserHash(
    metadata.hash,
    depsFromOptimizedDepInfo(depsInfo),
  )

  // We prebundle dependencies with esbuild and cache them, but there is no need
  // to wait here. Code that needs to access the cached deps needs to await
  // the optimizedDepInfo.processing promise for each dep

  const qualifiedIds = Object.keys(depsInfo)
  let cleaned = false
  let committed = false
  const cleanUp = () => {
    // If commit was already called, ignore the clean up even if a cancel was requested
    // This minimizes the chances of leaving the deps cache in a corrupted state
    if (!cleaned && !committed) {
      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}`))
      try {
        // When exiting the process, `fsp.rm` may not take effect, so we use `fs.rmSync`
        fs.rmSync(processingCacheDir, { recursive: true, force: true })
      } catch {
        // Ignore errors
      }
    }
  }

  const successfulResult: DepOptimizationResult = {
    metadata,
    cancel: cleanUp,
    commit: async () => {
      if (cleaned) {
        throw new Error(
          'Can not commit a Deps Optimization run as it was cancelled',
        )
      }
      // Ignore clean up requests after this point so the temp folder isn't deleted before
      // we finish committing the new deps cache files to the deps folder
      committed = true

      // Write metadata file, then commit the processing folder to the global deps cache
      // Rewire the file paths from the temporary processing dir to the final deps cache dir
      const dataPath = path.join(processingCacheDir, METADATA_FILENAME)
      debug?.(
        colors.green(`creating ${METADATA_FILENAME} in ${processingCacheDir}`),
      )
      fs.writeFileSync(
        dataPath,
        stringifyDepsOptimizerMetadata(metadata, depsCacheDir),
      )

      // In order to minimize the time where the deps folder isn't in a consistent state,

Subdomains

Frequently Asked Questions

What does runOptimizeDeps() do?
runOptimizeDeps() is a function in the vite codebase, defined in packages/vite/src/node/optimizer/index.ts.
Where is runOptimizeDeps() defined?
runOptimizeDeps() is defined in packages/vite/src/node/optimizer/index.ts at line 511.
What does runOptimizeDeps() call?
runOptimizeDeps() calls 15 function(s): addOptimizedDepInfo, depsFromOptimizedDepInfo, findOptimizedDepInfoInRecord, flattenId, getDepsCacheDir, getHash, getOptimizedBrowserHash, getProcessingDepsCacheDir, and 7 more.
What calls runOptimizeDeps()?
runOptimizeDeps() is called by 3 function(s): createDepsOptimizer, optimizeDeps, optimizeExplicitEnvironmentDeps.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free