Home / Function/ optimizedDepsPlugin() — vite Function Reference

optimizedDepsPlugin() — vite Function Reference

Architecture documentation for the optimizedDepsPlugin() function in optimizedDeps.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950["optimizedDepsPlugin()"]
  1ad5c135_fc65_cc8f_2918_6f109d7fd644["optimizedDeps.ts"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|defined in| 1ad5c135_fc65_cc8f_2918_6f109d7fd644
  b1b40b5b_3e43_2197_dea0_d36389d312a1["resolvePlugins()"]
  b1b40b5b_3e43_2197_dea0_d36389d312a1 -->|calls| daaf9cbf_e2ae_6c47_2a70_91fb6babc950
  cf8cccc0_fa2a_e8ac_480a_47c54435825c["isDepOptimizationDisabled()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| cf8cccc0_fa2a_e8ac_480a_47c54435825c
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  0e7dedc8_f980_705d_9b47_79e0785d8853["optimizedDepInfoFromFile()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| 0e7dedc8_f980_705d_9b47_79e0785d8853
  c21bd4cb_400a_15f5_15eb_1de2be5b3418["throwOutdatedRequest()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| c21bd4cb_400a_15f5_15eb_1de2be5b3418
  71bb2f3f_3aad_f3d5_db95_883bd60c383b["throwProcessingError()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| 71bb2f3f_3aad_f3d5_db95_883bd60c383b
  5e82bdfe_ad8a_9cc3_2afe_850f2b0c448a["throwFileNotFoundInOptimizedDep()"]
  daaf9cbf_e2ae_6c47_2a70_91fb6babc950 -->|calls| 5e82bdfe_ad8a_9cc3_2afe_850f2b0c448a
  style daaf9cbf_e2ae_6c47_2a70_91fb6babc950 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/optimizedDeps.ts lines 20–99

export function optimizedDepsPlugin(): Plugin {
  return {
    name: 'vite:optimized-deps',

    applyToEnvironment(environment) {
      return !isDepOptimizationDisabled(environment.config.optimizeDeps)
    },

    resolveId(id) {
      const environment = this.environment as DevEnvironment
      if (environment.depsOptimizer?.isOptimizedDepFile(id)) {
        return id
      }
    },

    // this.load({ id }) isn't implemented in PluginContainer
    // The logic to register an id to wait until it is processed
    // is in importAnalysis, see call to delayDepsOptimizerUntil

    async load(id) {
      const environment = this.environment as DevEnvironment
      const depsOptimizer = environment.depsOptimizer
      if (depsOptimizer?.isOptimizedDepFile(id)) {
        const metadata = depsOptimizer.metadata
        const file = cleanUrl(id)
        const versionMatch = DEP_VERSION_RE.exec(id)
        const browserHash = versionMatch
          ? versionMatch[1].split('=')[1]
          : undefined

        // Search in both the currently optimized and newly discovered deps
        const info = optimizedDepInfoFromFile(metadata, file)
        if (info) {
          if (
            browserHash &&
            info.browserHash !== browserHash &&
            !environment.config.optimizeDeps.ignoreOutdatedRequests
          ) {
            throwOutdatedRequest(id)
          }
          try {
            // This is an entry point, it may still not be bundled
            await info.processing
          } catch {
            // If the refresh has not happened after timeout, Vite considers
            // something unexpected has happened. In this case, Vite
            // returns an empty response that will error.
            throwProcessingError(id)
          }
          const newMetadata = depsOptimizer.metadata
          if (metadata !== newMetadata) {
            const currentInfo = optimizedDepInfoFromFile(newMetadata!, file)
            if (
              info.browserHash !== currentInfo?.browserHash &&
              !environment.config.optimizeDeps.ignoreOutdatedRequests
            ) {
              throwOutdatedRequest(id)
            }
          }
        }
        debug?.(`load ${colors.cyan(file)}`)
        // Load the file from the cache instead of waiting for other plugin
        // load hooks to avoid race conditions, once processing is resolved,
        // we are sure that the file has been properly save to disk
        try {
          return await fsp.readFile(file, 'utf-8')
        } catch {
          if (
            browserHash &&
            !environment.config.optimizeDeps.ignoreOutdatedRequests
          ) {
            // Outdated optimized files loaded after a rerun
            throwOutdatedRequest(id)
          }
          throwFileNotFoundInOptimizedDep(id)
        }
      }
    },
  }
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does optimizedDepsPlugin() do?
optimizedDepsPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/optimizedDeps.ts.
Where is optimizedDepsPlugin() defined?
optimizedDepsPlugin() is defined in packages/vite/src/node/plugins/optimizedDeps.ts at line 20.
What does optimizedDepsPlugin() call?
optimizedDepsPlugin() calls 6 function(s): cleanUrl, isDepOptimizationDisabled, optimizedDepInfoFromFile, throwFileNotFoundInOptimizedDep, throwOutdatedRequest, throwProcessingError.
What calls optimizedDepsPlugin()?
optimizedDepsPlugin() is called by 1 function(s): resolvePlugins.

Analyze Your Own Codebase

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

Try Supermodel Free