mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-01 16:44:24 +00:00
fix(shadcn): fix transformRsc to account for ' (#5518)
* fix(shadcn): fix transformRsc to account for ' * chore: add changeset --------- Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
5
.changeset/friendly-mangos-fetch.md
Normal file
5
.changeset/friendly-mangos-fetch.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"shadcn": patch
|
||||
---
|
||||
|
||||
fix transofmrRsc for handling use client
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
"
|
||||
`;
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user