Skip to content
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

fix(cli): monorepo cn import #6530

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions packages/shadcn/src/utils/transformers/transform-import.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Config } from "@/src/utils/get-config"
import { Transformer } from "@/src/utils/transformers"

const COMMON_CN_IMPORTS = {
"@/lib/utils": /^@\/lib\/utils/,
"@workspace/lib/utils": /^@workspace\/lib\/utils/,
}

export const transformImport: Transformer = async ({
sourceFile,
config,
isRemote,
}) => {
const workspaceAlias = config.aliases?.utils?.split("/")[0]?.slice(1)
const utilsImport = `@${workspaceAlias}/lib/utils`

const importDeclarations = sourceFile.getImportDeclarations()

for (const importDeclaration of importDeclarations) {
Expand All @@ -23,15 +21,13 @@ export const transformImport: Transformer = async ({
importDeclaration.setModuleSpecifier(moduleSpecifier)

// Replace `import { cn } from "@/lib/utils"`
if (COMMON_CN_IMPORTS[moduleSpecifier as keyof typeof COMMON_CN_IMPORTS]) {
if (utilsImport === moduleSpecifier) {
const namedImports = importDeclaration.getNamedImports()
const cnImport = namedImports.find((i) => i.getName() === "cn")
if (cnImport) {
importDeclaration.setModuleSpecifier(
moduleSpecifier.replace(
COMMON_CN_IMPORTS[
moduleSpecifier as keyof typeof COMMON_CN_IMPORTS
],
utilsImport,
config.aliases.utils
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,25 @@ import { Foo } from "bar"
import { cn } from "@custom-alias/lib/utils"
"
`;

exports[`transform import for monorepo 1`] = `
"import * as React from "react"
import { Foo } from "bar"
import { Button } from "@workspace/ui/components/ui/button"
import { Label} from "ui/label"
import { Box } from "@workspace/ui/components/box"

import { cn } from "@workspace/ui/lib/utils"
"
`;

exports[`transform import for monorepo 2`] = `
"import * as React from "react"
import { Foo } from "bar"
import { Button } from "@repo/ui/components/ui/button"
import { Label} from "ui/label"
import { Box } from "@repo/ui/components/box"

import { cn } from "@repo/ui/lib/utils"
"
`;
53 changes: 53 additions & 0 deletions packages/shadcn/test/utils/transform-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,56 @@ import { Foo } from "bar"
})
).toMatchSnapshot()
})


test("transform import for monorepo", async () => {
expect(
await transform({
filename: "test.ts",
raw: `import * as React from "react"
import { Foo } from "bar"
import { Button } from "@/registry/new-york/ui/button"
import { Label} from "ui/label"
import { Box } from "@/registry/new-york/box"

import { cn } from "@/lib/utils"
`,
config: {
tsx: true,
tailwind: {
baseColor: "neutral",
cssVariables: true,
},
aliases: {
components: "@workspace/ui/components",
utils: "@workspace/ui/lib/utils",
},
},
})
).toMatchSnapshot()

expect(
await transform({
filename: "test.ts",
raw: `import * as React from "react"
import { Foo } from "bar"
import { Button } from "@/registry/new-york/ui/button"
import { Label} from "ui/label"
import { Box } from "@/registry/new-york/box"

import { cn } from "@/lib/utils"
`,
config: {
tsx: true,
tailwind: {
baseColor: "neutral",
cssVariables: true,
},
aliases: {
components: "@repo/ui/components",
utils: "@repo/ui/lib/utils",
},
},
})
).toMatchSnapshot()
})