-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(shadcn): add src to content in tailwind config (#4787)
* feat(shadcn): handle src dir * chore: changeset
- Loading branch information
Showing
6 changed files
with
287 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"shadcn": patch | ||
--- | ||
|
||
add src to content for tailwind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/shadcn/src/utils/updaters/update-tailwind-content.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { promises as fs } from "fs" | ||
import path from "path" | ||
import { Config } from "@/src/utils/get-config" | ||
import { highlighter } from "@/src/utils/highlighter" | ||
import { spinner } from "@/src/utils/spinner" | ||
import { | ||
_createSourceFile, | ||
_getQuoteChar, | ||
} from "@/src/utils/updaters/update-tailwind-config" | ||
import { ObjectLiteralExpression, SyntaxKind } from "ts-morph" | ||
|
||
export async function updateTailwindContent( | ||
content: string[], | ||
config: Config, | ||
options: { | ||
silent?: boolean | ||
} | ||
) { | ||
if (!content) { | ||
return | ||
} | ||
|
||
options = { | ||
silent: false, | ||
...options, | ||
} | ||
|
||
const tailwindFileRelativePath = path.relative( | ||
config.resolvedPaths.cwd, | ||
config.resolvedPaths.tailwindConfig | ||
) | ||
const tailwindSpinner = spinner( | ||
`Updating ${highlighter.info(tailwindFileRelativePath)}`, | ||
{ | ||
silent: options.silent, | ||
} | ||
).start() | ||
const raw = await fs.readFile(config.resolvedPaths.tailwindConfig, "utf8") | ||
const output = await transformTailwindContent(raw, content, config) | ||
await fs.writeFile(config.resolvedPaths.tailwindConfig, output, "utf8") | ||
tailwindSpinner?.succeed() | ||
} | ||
|
||
export async function transformTailwindContent( | ||
input: string, | ||
content: string[], | ||
config: Config | ||
) { | ||
const sourceFile = await _createSourceFile(input, config) | ||
// Find the object with content property. | ||
// This is faster than traversing the default export. | ||
// TODO: maybe we do need to traverse the default export? | ||
const configObject = sourceFile | ||
.getDescendantsOfKind(SyntaxKind.ObjectLiteralExpression) | ||
.find((node) => | ||
node | ||
.getProperties() | ||
.some( | ||
(property) => | ||
property.isKind(SyntaxKind.PropertyAssignment) && | ||
property.getName() === "content" | ||
) | ||
) | ||
|
||
// We couldn't find the config object, so we return the input as is. | ||
if (!configObject) { | ||
return input | ||
} | ||
|
||
addTailwindConfigContent(configObject, content) | ||
|
||
return sourceFile.getFullText() | ||
} | ||
|
||
async function addTailwindConfigContent( | ||
configObject: ObjectLiteralExpression, | ||
content: string[] | ||
) { | ||
const quoteChar = _getQuoteChar(configObject) | ||
|
||
const existingProperty = configObject.getProperty("content") | ||
|
||
if (!existingProperty) { | ||
const newProperty = { | ||
name: "content", | ||
initializer: `[${quoteChar}${content.join( | ||
`${quoteChar}, ${quoteChar}` | ||
)}${quoteChar}]`, | ||
} | ||
configObject.addPropertyAssignment(newProperty) | ||
|
||
return configObject | ||
} | ||
|
||
if (existingProperty.isKind(SyntaxKind.PropertyAssignment)) { | ||
const initializer = existingProperty.getInitializer() | ||
|
||
// If property is an array, append. | ||
if (initializer?.isKind(SyntaxKind.ArrayLiteralExpression)) { | ||
for (const contentItem of content) { | ||
const newValue = `${quoteChar}${contentItem}${quoteChar}` | ||
|
||
// Check if the array already contains the value. | ||
if ( | ||
initializer | ||
.getElements() | ||
.map((element) => element.getText()) | ||
.includes(newValue) | ||
) { | ||
continue | ||
} | ||
|
||
initializer.addElement(newValue) | ||
} | ||
} | ||
|
||
return configObject | ||
} | ||
|
||
return configObject | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/shadcn/test/utils/updaters/__snapshots__/update-tailwind-content.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`transformTailwindContent -> content property > should NOT add content property if already in config 1`] = ` | ||
"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}", | ||
"./bar/**/*.{js,ts,jsx,tsx,mdx}" | ||
], | ||
theme: { | ||
extend: { | ||
backgroundImage: { | ||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))", | ||
"gradient-conic": | ||
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))", | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
export default config | ||
" | ||
`; | ||
|
||
exports[`transformTailwindContent -> content property > should add content property if not in config 1`] = ` | ||
"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}", | ||
"./foo/**/*.{js,ts,jsx,tsx,mdx}", | ||
"./bar/**/*.{js,ts,jsx,tsx,mdx}" | ||
], | ||
theme: { | ||
extend: { | ||
backgroundImage: { | ||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))", | ||
"gradient-conic": | ||
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))", | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
export default config | ||
" | ||
`; |
94 changes: 94 additions & 0 deletions
94
packages/shadcn/test/utils/updaters/update-tailwind-content.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { describe, expect, test } from "vitest" | ||
|
||
import { transformTailwindContent } from "../../../src/utils/updaters/update-tailwind-content" | ||
|
||
const SHARED_CONFIG = { | ||
$schema: "https://ui.shadcn.com/schema.json", | ||
style: "new-york", | ||
rsc: true, | ||
tsx: true, | ||
tailwind: { | ||
config: "tailwind.config.ts", | ||
css: "app/globals.css", | ||
baseColor: "slate", | ||
cssVariables: true, | ||
}, | ||
aliases: { | ||
components: "@/components", | ||
utils: "@/lib/utils", | ||
}, | ||
resolvedPaths: { | ||
cwd: ".", | ||
tailwindConfig: "tailwind.config.ts", | ||
tailwindCss: "app/globals.css", | ||
components: "./components", | ||
utils: "./lib/utils", | ||
ui: "./components/ui", | ||
}, | ||
} | ||
|
||
describe("transformTailwindContent -> content property", () => { | ||
test("should add content property if not in config", async () => { | ||
expect( | ||
await transformTailwindContent( | ||
`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: { | ||
backgroundImage: { | ||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))", | ||
"gradient-conic": | ||
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))", | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
export default config | ||
`, | ||
["./foo/**/*.{js,ts,jsx,tsx,mdx}", "./bar/**/*.{js,ts,jsx,tsx,mdx}"], | ||
{ | ||
config: SHARED_CONFIG, | ||
} | ||
) | ||
).toMatchSnapshot() | ||
}) | ||
|
||
test("should NOT add content property if already in config", async () => { | ||
expect( | ||
await transformTailwindContent( | ||
`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: { | ||
backgroundImage: { | ||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))", | ||
"gradient-conic": | ||
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))", | ||
}, | ||
}, | ||
}, | ||
plugins: [], | ||
} | ||
export default config | ||
`, | ||
["./app/**/*.{js,ts,jsx,tsx,mdx}", "./bar/**/*.{js,ts,jsx,tsx,mdx}"], | ||
{ | ||
config: SHARED_CONFIG, | ||
} | ||
) | ||
).toMatchSnapshot() | ||
}) | ||
}) |