Home / Function/ tryOptimizedResolve() — vite Function Reference

tryOptimizedResolve() — vite Function Reference

Architecture documentation for the tryOptimizedResolve() function in resolve.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  a2e55a70_b7b6_bce2_e066_aea4c516313e["tryOptimizedResolve()"]
  dcff87b0_a8ea_57a2_3b29_a7b8f19986f3["resolve.ts"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|defined in| dcff87b0_a8ea_57a2_3b29_a7b8f19986f3
  200356af_4305_31b8_b090_47f38784e182["preAliasPlugin()"]
  200356af_4305_31b8_b090_47f38784e182 -->|calls| a2e55a70_b7b6_bce2_e066_aea4c516313e
  7afe7894_efd7_32c4_3b59_4a2cdc5ba6a2["optimizerResolvePlugin()"]
  7afe7894_efd7_32c4_3b59_4a2cdc5ba6a2 -->|calls| a2e55a70_b7b6_bce2_e066_aea4c516313e
  4063924c_27d1_3250_6b83_3c254baaafd5["optimizedDepInfoFromId()"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|calls| 4063924c_27d1_3250_6b83_3c254baaafd5
  cb293321_1174_554d_b5ce_f153b06a2fb4["getNpmPackageName()"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|calls| cb293321_1174_554d_b5ce_f153b06a2fb4
  170ceb82_4bf1_2290_1f2e_bbdec11e5184["resolvePackageData()"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|calls| 170ceb82_4bf1_2290_1f2e_bbdec11e5184
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  1a3bec7b_1a11_316f_5831_a0535b829bbf["withTrailingSlash()"]
  a2e55a70_b7b6_bce2_e066_aea4c516313e -->|calls| 1a3bec7b_1a11_316f_5831_a0535b829bbf
  style a2e55a70_b7b6_bce2_e066_aea4c516313e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/resolve.ts lines 848–901

export async function tryOptimizedResolve(
  depsOptimizer: DepsOptimizer,
  id: string,
  importer?: string,
  preserveSymlinks?: boolean,
  packageCache?: PackageCache,
): Promise<string | undefined> {
  // TODO: we need to wait until scanning is done here as this function
  // is used in the preAliasPlugin to decide if an aliased dep is optimized,
  // and avoid replacing the bare import with the resolved path.
  // We should be able to remove this in the future
  await depsOptimizer.scanProcessing

  const metadata = depsOptimizer.metadata

  const depInfo = optimizedDepInfoFromId(metadata, id)
  if (depInfo) {
    return depsOptimizer.getOptimizedDepId(depInfo)
  }

  if (!importer) return

  // further check if id is imported by nested dependency
  let idPkgDir: string | undefined
  const nestedIdMatch = `> ${id}`

  for (const optimizedData of metadata.depInfoList) {
    if (!optimizedData.src) continue // Ignore chunks

    // check where "foo" is nested in "my-lib > foo"
    if (!optimizedData.id.endsWith(nestedIdMatch)) continue

    // lazily initialize idPkgDir
    if (idPkgDir == null) {
      const pkgName = getNpmPackageName(id)
      if (!pkgName) break
      idPkgDir = resolvePackageData(
        pkgName,
        importer,
        preserveSymlinks,
        packageCache,
      )?.dir
      // if still null, it likely means that this id isn't a dep for importer.
      // break to bail early
      if (idPkgDir == null) break
      idPkgDir = normalizePath(idPkgDir)
    }

    // match by src to correctly identify if id belongs to nested dependency
    if (optimizedData.src.startsWith(withTrailingSlash(idPkgDir))) {
      return depsOptimizer.getOptimizedDepId(optimizedData)
    }
  }
}

Domain

Subdomains

Frequently Asked Questions

What does tryOptimizedResolve() do?
tryOptimizedResolve() is a function in the vite codebase, defined in packages/vite/src/node/plugins/resolve.ts.
Where is tryOptimizedResolve() defined?
tryOptimizedResolve() is defined in packages/vite/src/node/plugins/resolve.ts at line 848.
What does tryOptimizedResolve() call?
tryOptimizedResolve() calls 5 function(s): getNpmPackageName, normalizePath, optimizedDepInfoFromId, resolvePackageData, withTrailingSlash.
What calls tryOptimizedResolve()?
tryOptimizedResolve() is called by 2 function(s): optimizerResolvePlugin, preAliasPlugin.

Analyze Your Own Codebase

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

Try Supermodel Free