diff --git a/.changeset/friendly-mangos-fetch.md b/.changeset/friendly-mangos-fetch.md new file mode 100644 index 00000000000..588f127d5e3 --- /dev/null +++ b/.changeset/friendly-mangos-fetch.md @@ -0,0 +1,5 @@ +--- +"shadcn": patch +--- + +fix transofmrRsc for handling use client diff --git a/packages/shadcn/src/utils/transformers/transform-rsc.ts b/packages/shadcn/src/utils/transformers/transform-rsc.ts index b7d24a0709e..1090093aba0 100644 --- a/packages/shadcn/src/utils/transformers/transform-rsc.ts +++ b/packages/shadcn/src/utils/transformers/transform-rsc.ts @@ -1,6 +1,8 @@ import { Transformer } from "@/src/utils/transformers" import { SyntaxKind } from "ts-morph" +const directiveRegex = /^["']use client["']$/g + export const transformRsc: Transformer = async ({ sourceFile, config }) => { if (config.rsc) { return sourceFile @@ -8,7 +10,7 @@ export const transformRsc: Transformer = async ({ sourceFile, config }) => { // Remove "use client" from the top of the file. const first = sourceFile.getFirstChildByKind(SyntaxKind.ExpressionStatement) - if (first?.getText() === `"use client"`) { + if (first && directiveRegex.test(first.getText())) { first.remove() } diff --git a/packages/shadcn/test/utils/__snapshots__/transform-rsc.test.ts.snap b/packages/shadcn/test/utils/__snapshots__/transform-rsc.test.ts.snap index c0d1e446bd3..9f378a737df 100644 --- a/packages/shadcn/test/utils/__snapshots__/transform-rsc.test.ts.snap +++ b/packages/shadcn/test/utils/__snapshots__/transform-rsc.test.ts.snap @@ -29,3 +29,27 @@ import { Foo } from "bar" "use client" " `; + +exports[`transform rsc 5`] = ` +"'use client' + + import * as React from 'react' +import { Foo } from 'bar' + " +`; + +exports[`transform rsc 6`] = ` +"import * as React from 'react' +import { Foo } from 'bar' + " +`; + +exports[`transform rsc 7`] = ` +"'use foo' + + import * as React from 'react' +import { Foo } from 'bar' + +'use client' + " +`; diff --git a/packages/shadcn/test/utils/transform-rsc.test.ts b/packages/shadcn/test/utils/transform-rsc.test.ts index 73e0105d75d..79b0d0b4ad3 100644 --- a/packages/shadcn/test/utils/transform-rsc.test.ts +++ b/packages/shadcn/test/utils/transform-rsc.test.ts @@ -62,4 +62,52 @@ import { Foo } from "bar" }, }) ).toMatchSnapshot() + + + expect( + await transform({ + filename: "test.ts", + raw: `'use client' + + import * as React from 'react' +import { Foo } from 'bar' + `, + config: { + tsx: true, + rsc: true, + }, + }) + ).toMatchSnapshot() + + expect( + await transform({ + filename: "test.ts", + raw: `'use client' + + import * as React from 'react' +import { Foo } from 'bar' + `, + config: { + tsx: true, + rsc: false, + }, + }) + ).toMatchSnapshot() + + expect( + await transform({ + filename: "test.ts", + raw: `'use foo' + + import * as React from 'react' +import { Foo } from 'bar' + +'use client' + `, + config: { + tsx: true, + rsc: false, + }, + }) + ).toMatchSnapshot() })