Home / Function/ resolveImports() — ui Function Reference

resolveImports() — ui Function Reference

Architecture documentation for the resolveImports() function in update-files.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  7dc382f5_944d_c67e_f395_05edc855da6a["resolveImports()"]
  3efb2c23_7931_d244_b4e1_85acab69298c["update-files.ts"]
  7dc382f5_944d_c67e_f395_05edc855da6a -->|defined in| 3efb2c23_7931_d244_b4e1_85acab69298c
  bc52ca78_0228_54e7_56b2_602b341e2ec4["updateFiles()"]
  bc52ca78_0228_54e7_56b2_602b341e2ec4 -->|calls| 7dc382f5_944d_c67e_f395_05edc855da6a
  5ca9d691_c347_aaf4_a263_a11c048b0d37["resolveModuleByProbablePath()"]
  7dc382f5_944d_c67e_f395_05edc855da6a -->|calls| 5ca9d691_c347_aaf4_a263_a11c048b0d37
  6f4fcb58_e322_dd7a_1912_a488aeba98bc["toAliasedImport()"]
  7dc382f5_944d_c67e_f395_05edc855da6a -->|calls| 6f4fcb58_e322_dd7a_1912_a488aeba98bc
  style 7dc382f5_944d_c67e_f395_05edc855da6a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/utils/updaters/update-files.ts lines 512–605

async function resolveImports(filePaths: string[], config: Config) {
  const project = new Project({
    compilerOptions: {},
  })
  const projectInfo = await getProjectInfo(config.resolvedPaths.cwd)
  const tsConfig = loadConfig(config.resolvedPaths.cwd)
  const updatedFiles = []

  if (!projectInfo || tsConfig.resultType === "failed") {
    return []
  }

  for (const filepath of filePaths) {
    const resolvedPath = path.resolve(config.resolvedPaths.cwd, filepath)

    // Check if the file exists.
    if (!existsSync(resolvedPath)) {
      continue
    }

    const content = await fs.readFile(resolvedPath, "utf-8")

    const dir = await fs.mkdtemp(path.join(tmpdir(), "shadcn-"))
    const sourceFile = project.createSourceFile(
      path.join(dir, basename(resolvedPath)),
      content,
      {
        scriptKind: ScriptKind.TSX,
      }
    )

    // Skip if the file extension is not one of the supported extensions.
    if (![".tsx", ".ts", ".jsx", ".js"].includes(sourceFile.getExtension())) {
      continue
    }

    const importDeclarations = sourceFile.getImportDeclarations()
    for (const importDeclaration of importDeclarations) {
      const moduleSpecifier = importDeclaration.getModuleSpecifierValue()

      // Filter out non-local imports.
      if (
        projectInfo?.aliasPrefix &&
        !moduleSpecifier.startsWith(`${projectInfo.aliasPrefix}/`)
      ) {
        continue
      }

      // Find the probable import file path.
      // This is where we expect to find the file on disk.
      const probableImportFilePath = await resolveImport(
        moduleSpecifier,
        tsConfig
      )

      if (!probableImportFilePath) {
        continue
      }

      // Find the actual import file path.
      // This is the path where the file has been installed.
      const resolvedImportFilePath = resolveModuleByProbablePath(
        probableImportFilePath,
        filePaths,
        config
      )

      if (!resolvedImportFilePath) {
        continue
      }

      // Convert the resolved import file path to an aliased import.
      const newImport = toAliasedImport(
        resolvedImportFilePath,
        config,
        projectInfo
      )

      if (!newImport || newImport === moduleSpecifier) {
        continue
      }

Subdomains

Called By

Frequently Asked Questions

What does resolveImports() do?
resolveImports() is a function in the ui codebase, defined in packages/shadcn/src/utils/updaters/update-files.ts.
Where is resolveImports() defined?
resolveImports() is defined in packages/shadcn/src/utils/updaters/update-files.ts at line 512.
What does resolveImports() call?
resolveImports() calls 2 function(s): resolveModuleByProbablePath, toAliasedImport.
What calls resolveImports()?
resolveImports() is called by 1 function(s): updateFiles.

Analyze Your Own Codebase

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

Try Supermodel Free