fix: refactor

This commit is contained in:
shadcn
2026-03-16 13:27:13 +04:00
parent b6cfe91aa6
commit ef35fd8f4c
5 changed files with 74 additions and 1 deletions

View File

@@ -92,6 +92,8 @@ export function isLocalAliasImport(
moduleSpecifier: string,
aliasPrefix: string | null
) {
// Workspace package exports such as `@workspace/ui/...` are already the final
// import specifiers we want to keep, so they are intentionally excluded here.
if (moduleSpecifier.startsWith("#")) {
return true
}

View File

@@ -156,6 +156,12 @@ function updateImportAliases(
}
function getWorkspaceAliasFromUtilsAlias(utilsAlias: string) {
// `#...` utils aliases are handled by package-import normalization and should
// not be treated as workspace package roots.
if (utilsAlias.startsWith("#")) {
return ""
}
if (utilsAlias.endsWith("/lib/utils")) {
return utilsAlias.slice(0, -"/lib/utils".length)
}

View File

@@ -3,6 +3,7 @@ import { getWorkspacePatterns } from "@/src/utils/get-monorepo-info"
import { getPackageInfo } from "@/src/utils/get-package-info"
import { type ImportEmitMode } from "@/src/utils/package-imports"
import fg from "fast-glob"
import fs from "fs-extra"
type WorkspacePackageInfo = {
packageName: string
@@ -33,6 +34,7 @@ const workspaceExportEntriesCache = new Map<
string,
WorkspacePackageExportEntry[]
>()
const workspaceRootCache = new Map<string, string | null>()
export async function resolveWorkspacePackageExport(
importPath: string,
@@ -203,12 +205,45 @@ async function loadWorkspacePackages(root: string) {
}
async function findWorkspaceRoot(cwd: string) {
let current = path.resolve(cwd)
const start = path.resolve(cwd)
const cachedRoot = workspaceRootCache.get(start)
if (cachedRoot !== undefined) {
return cachedRoot
}
let current = start
const gitRoot = await findGitRoot(start)
while (true) {
const patterns = await getWorkspacePatterns(current)
if (patterns.length) {
workspaceRootCache.set(start, current)
return current
}
if (gitRoot && current === gitRoot) {
workspaceRootCache.set(start, null)
return null
}
const parent = path.dirname(current)
if (parent === current) {
workspaceRootCache.set(start, null)
return null
}
current = parent
}
}
async function findGitRoot(cwd: string) {
let current = path.resolve(cwd)
while (true) {
if (fs.existsSync(path.resolve(current, ".git"))) {
return current
}

View File

@@ -3,6 +3,7 @@ import { loadConfig, type ConfigLoaderSuccessResult } from "tsconfig-paths"
import { describe, expect, test } from "vitest"
import {
isLocalAliasImport,
resolveImport,
resolveImportWithMetadata,
} from "../../src/utils/resolve-import"
@@ -186,4 +187,10 @@ describe("resolve workspace package exports", () => {
path.resolve(root, "packages/ui/src/lib/utils.ts")
)
})
test("does not treat workspace package exports as local alias imports", () => {
expect(isLocalAliasImport("@workspace/ui/components/button", "#")).toBe(
false
)
})
})

View File

@@ -200,6 +200,29 @@ import { cn } from "#app/lib/utils"
`)
})
test("transform import keeps exact #utils aliases", async () => {
expect(
await transform({
filename: "test.ts",
raw: `import { cn } from "@/lib/utils"
`,
config: {
tsx: true,
aliases: {
components: "#components",
utils: "#utils",
ui: "#components/ui",
lib: "#lib",
hooks: "#hooks",
},
},
})
).toMatchInlineSnapshot(`
"import { cn } from "#utils"
"
`)
})
test("transform import for monorepo", async () => {
expect(
await transform({