Home / Function/ toAliasedImport() — ui Function Reference

toAliasedImport() — ui Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

packages/shadcn/src/utils/updaters/update-files.ts lines 684–739

export function toAliasedImport(
  filePath: string,
  config: Config,
  projectInfo: ProjectInfo
): string | null {
  const abs = path.normalize(path.join(config.resolvedPaths.cwd, filePath))

  // 1️⃣ Find the longest matching alias root in resolvedPaths
  //    e.g. key="ui", root="/…/components/ui" beats key="components"
  const matches = Object.entries(config.resolvedPaths)
    .filter(
      ([, root]) => root && abs.startsWith(path.normalize(root + path.sep))
    )
    .sort((a, b) => b[1].length - a[1].length)

  if (matches.length === 0) {
    return null
  }
  const [aliasKey, rootDir] = matches[0]

  // 2️⃣ Compute the path UNDER that root
  let rel = path.relative(rootDir, abs)
  // force POSIX-style separators
  rel = rel.split(path.sep).join("/") // e.g. "button/index.tsx"

  // 3️⃣ Strip code-file extensions, keep others (css, json, etc.)
  const ext = path.posix.extname(rel)
  const codeExts = [".ts", ".tsx", ".js", ".jsx"]
  const keepExt = codeExts.includes(ext) ? "" : ext
  let noExt = rel.slice(0, rel.length - ext.length)

  // 4️⃣ Collapse "/index" to its directory
  if (noExt.endsWith("/index")) {
    noExt = noExt.slice(0, -"/index".length)
  }

  // 5️⃣ Build the aliased path
  //    config.aliases[aliasKey] is e.g. "@/components/ui"
  const aliasBase =
    aliasKey === "cwd"
      ? projectInfo.aliasPrefix
      : config.aliases[aliasKey as keyof typeof config.aliases]
  if (!aliasBase) {
    return null
  }
  // if noExt is empty (i.e. file was exactly at the root), we import the root
  let suffix = noExt === "" ? "" : `/${noExt}`

  // Remove /src from suffix.
  // Alias will handle this.
  suffix = suffix.replace("/src", "")

  // 6️⃣ Prepend the prefix from projectInfo (e.g. "@") if needed
  //    but usually config.aliases already include it.
  return `${aliasBase}${suffix}${keepExt}`
}

Subdomains

Called By

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free