Skip to content

Commit d615902

Browse files
zwarunekshadcn
andauthored
fix(cli): monorepo cn import (#6530)
* fix monorepo cn import * chore: changeset * style(shadcn): format --------- Co-authored-by: shadcn <m@shadcn.com>
1 parent b0774b0 commit d615902

4 files changed

Lines changed: 85 additions & 12 deletions

File tree

.changeset/orange-papayas-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": patch
3+
---
4+
5+
fix cn import bug in monorepo

packages/shadcn/src/utils/transformers/transform-import.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Config } from "@/src/utils/get-config"
22
import { Transformer } from "@/src/utils/transformers"
33

4-
const COMMON_CN_IMPORTS = {
5-
"@/lib/utils": /^@\/lib\/utils/,
6-
"@workspace/lib/utils": /^@workspace\/lib\/utils/,
7-
}
8-
94
export const transformImport: Transformer = async ({
105
sourceFile,
116
config,
127
isRemote,
138
}) => {
9+
const workspaceAlias = config.aliases?.utils?.split("/")[0]?.slice(1)
10+
const utilsImport = `@${workspaceAlias}/lib/utils`
11+
1412
const importDeclarations = sourceFile.getImportDeclarations()
1513

1614
for (const importDeclaration of importDeclarations) {
@@ -23,17 +21,12 @@ export const transformImport: Transformer = async ({
2321
importDeclaration.setModuleSpecifier(moduleSpecifier)
2422

2523
// Replace `import { cn } from "@/lib/utils"`
26-
if (COMMON_CN_IMPORTS[moduleSpecifier as keyof typeof COMMON_CN_IMPORTS]) {
24+
if (utilsImport === moduleSpecifier) {
2725
const namedImports = importDeclaration.getNamedImports()
2826
const cnImport = namedImports.find((i) => i.getName() === "cn")
2927
if (cnImport) {
3028
importDeclaration.setModuleSpecifier(
31-
moduleSpecifier.replace(
32-
COMMON_CN_IMPORTS[
33-
moduleSpecifier as keyof typeof COMMON_CN_IMPORTS
34-
],
35-
config.aliases.utils
36-
)
29+
moduleSpecifier.replace(utilsImport, config.aliases.utils)
3730
)
3831
}
3932
}

packages/shadcn/test/utils/__snapshots__/transform-import.test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,25 @@ import { Foo } from "bar"
6969
import { cn } from "@custom-alias/lib/utils"
7070
"
7171
`;
72+
73+
exports[`transform import for monorepo 1`] = `
74+
"import * as React from "react"
75+
import { Foo } from "bar"
76+
import { Button } from "@workspace/ui/components/ui/button"
77+
import { Label} from "ui/label"
78+
import { Box } from "@workspace/ui/components/box"
79+
80+
import { cn } from "@workspace/ui/lib/utils"
81+
"
82+
`;
83+
84+
exports[`transform import for monorepo 2`] = `
85+
"import * as React from "react"
86+
import { Foo } from "bar"
87+
import { Button } from "@repo/ui/components/ui/button"
88+
import { Label} from "ui/label"
89+
import { Box } from "@repo/ui/components/box"
90+
91+
import { cn } from "@repo/ui/lib/utils"
92+
"
93+
`;

packages/shadcn/test/utils/transform-import.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,56 @@ import { Foo } from "bar"
143143
})
144144
).toMatchSnapshot()
145145
})
146+
147+
148+
test("transform import for monorepo", async () => {
149+
expect(
150+
await transform({
151+
filename: "test.ts",
152+
raw: `import * as React from "react"
153+
import { Foo } from "bar"
154+
import { Button } from "@/registry/new-york/ui/button"
155+
import { Label} from "ui/label"
156+
import { Box } from "@/registry/new-york/box"
157+
158+
import { cn } from "@/lib/utils"
159+
`,
160+
config: {
161+
tsx: true,
162+
tailwind: {
163+
baseColor: "neutral",
164+
cssVariables: true,
165+
},
166+
aliases: {
167+
components: "@workspace/ui/components",
168+
utils: "@workspace/ui/lib/utils",
169+
},
170+
},
171+
})
172+
).toMatchSnapshot()
173+
174+
expect(
175+
await transform({
176+
filename: "test.ts",
177+
raw: `import * as React from "react"
178+
import { Foo } from "bar"
179+
import { Button } from "@/registry/new-york/ui/button"
180+
import { Label} from "ui/label"
181+
import { Box } from "@/registry/new-york/box"
182+
183+
import { cn } from "@/lib/utils"
184+
`,
185+
config: {
186+
tsx: true,
187+
tailwind: {
188+
baseColor: "neutral",
189+
cssVariables: true,
190+
},
191+
aliases: {
192+
components: "@repo/ui/components",
193+
utils: "@repo/ui/lib/utils",
194+
},
195+
},
196+
})
197+
).toMatchSnapshot()
198+
})

0 commit comments

Comments
 (0)