mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-28 23:24:13 +00:00
fix: refactor
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user