diff --git a/.changeset/forty-rabbits-wonder.md b/.changeset/forty-rabbits-wonder.md new file mode 100644 index 0000000000..9db8773b39 --- /dev/null +++ b/.changeset/forty-rabbits-wonder.md @@ -0,0 +1,5 @@ +--- +"shadcn": patch +--- + +fix theme values bug diff --git a/packages/shadcn/src/utils/updaters/update-tailwind-config.ts b/packages/shadcn/src/utils/updaters/update-tailwind-config.ts index cdcffda75a..0deee35f77 100644 --- a/packages/shadcn/src/utils/updaters/update-tailwind-config.ts +++ b/packages/shadcn/src/utils/updaters/update-tailwind-config.ts @@ -196,12 +196,14 @@ async function addTailwindConfigTheme( const themeObject = await parseObjectLiteral(themeObjectString) const result = deepmerge(themeObject, theme) const resultString = objectToString(result) - .replace(/\'\"/g, "'") - .replace(/\"\'/g, "'") - .replace(/\'\[/g, "[") - .replace(/\]\'/g, "]") - .replace(/\\\'/g, "") - .replace(/\\\'/g, "") + .replace(/\'\"/g, "'") // Replace `\" with " + .replace(/\"\'/g, "'") // Replace `\" with " + .replace(/\'\[/g, "[") // Replace `[ with [ + .replace(/\]\'/g, "]") // Replace `] with ] + .replace(/\'\\\'/g, "'") // Replace `\' with ' + .replace(/\\\'/g, "'") // Replace \' with ' + .replace(/\\\'\'/g, "'") + .replace(/\'\'/g, "'") themeInitializer.replaceWithText(resultString) } diff --git a/packages/shadcn/test/utils/updaters/__snapshots__/update-tailwind-config.test.ts.snap b/packages/shadcn/test/utils/updaters/__snapshots__/update-tailwind-config.test.ts.snap index 2de3deeb8d..9bcb300ea5 100644 --- a/packages/shadcn/test/utils/updaters/__snapshots__/update-tailwind-config.test.ts.snap +++ b/packages/shadcn/test/utils/updaters/__snapshots__/update-tailwind-config.test.ts.snap @@ -369,6 +369,41 @@ export default config " `; +exports[`transformTailwindConfig -> theme > should keep quotes in strings 1`] = ` +"import type { Config } from 'tailwindcss' + +const config: Config = { + darkMode: ["class"], + content: [ + "./pages/**/*.{js,ts,jsx,tsx,mdx}", + "./components/**/*.{js,ts,jsx,tsx,mdx}", + "./app/**/*.{js,ts,jsx,tsx,mdx}", + ], + theme: { + extend: { + fontFamily: { + sans: ['Figtree', ...defaultTheme.fontFamily.sans] + }, + colors: { + ...defaultColors, + background: 'hsl(var(--background))', + foreground: 'hsl(var(--foreground))', + primary: { + DEFAULT: 'hsl(var(--primary))', + foreground: 'hsl(var(--primary-foreground))' + }, + card: { + DEFAULT: 'hsl(var(--card))', + foreground: 'hsl(var(--card-foreground))' + } + } + } + }, +} +export default config + " +`; + exports[`transformTailwindConfig -> theme > should keep spread assignments 1`] = ` "import type { Config } from 'tailwindcss' diff --git a/packages/shadcn/test/utils/updaters/update-tailwind-config.test.ts b/packages/shadcn/test/utils/updaters/update-tailwind-config.test.ts index e433005e42..c7e4350c79 100644 --- a/packages/shadcn/test/utils/updaters/update-tailwind-config.test.ts +++ b/packages/shadcn/test/utils/updaters/update-tailwind-config.test.ts @@ -760,6 +760,55 @@ export default config expect(output3).toBe(output1) expect(output3).toBe(output2) }) + + test("should keep quotes in strings", async () => { + expect( + await transformTailwindConfig( + `import type { Config } from 'tailwindcss' + +const config: Config = { + content: [ + "./pages/**/*.{js,ts,jsx,tsx,mdx}", + "./components/**/*.{js,ts,jsx,tsx,mdx}", + "./app/**/*.{js,ts,jsx,tsx,mdx}", + ], + theme: { + extend: { + fontFamily: { + sans: ['Figtree', ...defaultTheme.fontFamily.sans], + }, + colors: { + ...defaultColors, + background: "hsl(var(--background))", + foreground: "hsl(var(--foreground))", + }, + }, + }, +} +export default config + `, + { + theme: { + extend: { + colors: { + primary: { + DEFAULT: "hsl(var(--primary))", + foreground: "hsl(var(--primary-foreground))", + }, + card: { + DEFAULT: "hsl(var(--card))", + foreground: "hsl(var(--card-foreground))", + }, + }, + }, + }, + }, + { + config: SHARED_CONFIG, + } + ) + ).toMatchSnapshot() + }) }) describe("nestSpreadProperties", () => {