Skip to content

fix: Update file extension handling in toAliasedImport function #7549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/shadcn/src/utils/updaters/update-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,16 @@ export function toAliasedImport(
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)
// We use `lastIndexOf('.')` so that multi-dot filenames (e.g. "message-demo.const.tsx") are split correctly on the final dot.
const lastDotIndex = rel.lastIndexOf(".")
let ext = ""
let noExt = rel
if (lastDotIndex !== -1) {
ext = rel.slice(lastDotIndex) // e.g. ".tsx"
noExt = rel.slice(0, lastDotIndex) // e.g. "message-demo.const"
}
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")) {
Expand Down