File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " shadcn " : patch
3+ ---
4+
5+ fix cn import bug in monorepo
Original file line number Diff line number Diff line change 11import { Config } from "@/src/utils/get-config"
22import { Transformer } from "@/src/utils/transformers"
33
4- const COMMON_CN_IMPORTS = {
5- "@/lib/utils" : / ^ @ \/ l i b \/ u t i l s / ,
6- "@workspace/lib/utils" : / ^ @ w o r k s p a c e \/ l i b \/ u t i l s / ,
7- }
8-
94export 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 }
Original file line number Diff line number Diff 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+ ` ;
Original file line number Diff line number Diff 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+ } )
You can’t perform that action at this time.
0 commit comments