Home / Function/ tryCleanFsResolve() — vite Function Reference

tryCleanFsResolve() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  8ac17738_0641_9534_aa4e_c559f410561a["tryCleanFsResolve()"]
  dcff87b0_a8ea_57a2_3b29_a7b8f19986f3["resolve.ts"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|defined in| dcff87b0_a8ea_57a2_3b29_a7b8f19986f3
  2c262e8e_4e5d_80f3_98ab_102647c58f78["tryFsResolve()"]
  2c262e8e_4e5d_80f3_98ab_102647c58f78 -->|calls| 8ac17738_0641_9534_aa4e_c559f410561a
  04eba3ee_b103_fb12_64a9_27459b1e87c1["tryResolveRealFileOrType()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| 04eba3ee_b103_fb12_64a9_27459b1e87c1
  3cf85747_d347_b7de_1ce8_1e4ff859a58e["isPossibleTsOutput()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| 3cf85747_d347_b7de_1ce8_1e4ff859a58e
  66405bb2_893b_16d8_47a4_76b06e9ca1bc["isDirectory()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| 66405bb2_893b_16d8_47a4_76b06e9ca1bc
  1c922666_7528_3264_0a1f_191c477449fe["tryResolveRealFile()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| 1c922666_7528_3264_0a1f_191c477449fe
  94631252_f262_9f37_c9ed_e7b54e49e902["tryResolveRealFileWithExtensions()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| 94631252_f262_9f37_c9ed_e7b54e49e902
  c2d70c8c_c901_2d4f_df10_d0fa8607751f["loadPackageData()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| c2d70c8c_c901_2d4f_df10_d0fa8607751f
  ff420344_db87_ddaa_de80_eb7cf9a4644d["resolvePackageEntry()"]
  8ac17738_0641_9534_aa4e_c559f410561a -->|calls| ff420344_db87_ddaa_de80_eb7cf9a4644d
  style 8ac17738_0641_9534_aa4e_c559f410561a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/resolve.ts lines 578–685

function tryCleanFsResolve(
  file: string,
  options: InternalResolveOptions,
  tryIndex = true,
  skipPackageJson = false,
): string | undefined {
  const { tryPrefix, extensions, preserveSymlinks } = options

  // Optimization to get the real type or file type (directory, file, other)
  const fileResult = tryResolveRealFileOrType(file, options.preserveSymlinks)

  if (fileResult?.path) return fileResult.path

  let res: string | undefined

  // If path.dirname is a valid directory, try extensions and ts resolution logic
  const possibleJsToTs = isPossibleTsOutput(file)
  if (possibleJsToTs || options.extensions.length || tryPrefix) {
    const dirPath = path.dirname(file)
    if (isDirectory(dirPath)) {
      if (possibleJsToTs) {
        // try resolve .js, .mjs, .cjs or .jsx import to typescript file
        const fileExt = path.extname(file)
        const fileName = file.slice(0, -fileExt.length)
        if (
          (res = tryResolveRealFile(
            fileName + fileExt.replace('js', 'ts'),
            preserveSymlinks,
          ))
        )
          return res
        // for .js, also try .tsx
        if (
          fileExt === '.js' &&
          (res = tryResolveRealFile(fileName + '.tsx', preserveSymlinks))
        )
          return res
      }

      if (
        (res = tryResolveRealFileWithExtensions(
          file,
          extensions,
          preserveSymlinks,
        ))
      )
        return res

      if (tryPrefix) {
        const prefixed = `${dirPath}/${options.tryPrefix}${path.basename(file)}`

        if ((res = tryResolveRealFile(prefixed, preserveSymlinks))) return res

        if (
          (res = tryResolveRealFileWithExtensions(
            prefixed,
            extensions,
            preserveSymlinks,
          ))
        )
          return res
      }
    }
  }

  if (tryIndex && fileResult?.type === 'directory') {
    // Path points to a directory, check for package.json and entry and /index file
    const dirPath = file

    if (!skipPackageJson) {
      let pkgPath = `${dirPath}/package.json`
      try {
        if (fs.existsSync(pkgPath)) {
          if (!options.preserveSymlinks) {
            pkgPath = safeRealpathSync(pkgPath)
          }
          // path points to a node package
          const pkg = loadPackageData(pkgPath)
          return resolvePackageEntry(dirPath, pkg, options)
        }
      } catch (e) {

Domain

Subdomains

Called By

Frequently Asked Questions

What does tryCleanFsResolve() do?
tryCleanFsResolve() is a function in the vite codebase, defined in packages/vite/src/node/plugins/resolve.ts.
Where is tryCleanFsResolve() defined?
tryCleanFsResolve() is defined in packages/vite/src/node/plugins/resolve.ts at line 578.
What does tryCleanFsResolve() call?
tryCleanFsResolve() calls 7 function(s): isDirectory, isPossibleTsOutput, loadPackageData, resolvePackageEntry, tryResolveRealFile, tryResolveRealFileOrType, tryResolveRealFileWithExtensions.
What calls tryCleanFsResolve()?
tryCleanFsResolve() is called by 1 function(s): tryFsResolve.

Analyze Your Own Codebase

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

Try Supermodel Free