Skip to content

Commit 26a4754

Browse files
Introduce tailwindStylesheet option to replace tailwindEntryPoint (#330)
* Refactor * Introduce `tailwindStylesheet` option to replace `tailwindEntryPoint` The old option will still work but the new one is much better named * Allow `tailwindConfig` to be used as a stylesheet option This is a compatability feature. People previously used the `tailwindConfig` option to specify the location of their config and naturally expect it to work with a CSS file in v4. The preferred option is `tailwindStylesheet` but making this work is rather simple so it should “just work” for improved DX * Warn when using outdated config options * Update readme * Bump Tailwind version in tests * Update option in tests * Update README.md Co-authored-by: Jonathan Reinink <[email protected]> * Update README.md Co-authored-by: Jonathan Reinink <[email protected]> * Update README.md Co-authored-by: Jonathan Reinink <[email protected]> * Tweak title --------- Co-authored-by: Jonathan Reinink <[email protected]>
1 parent 64373cf commit 26a4754

File tree

8 files changed

+69
-15
lines changed

8 files changed

+69
-15
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ As of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means i
2525

2626
## Options
2727

28-
### Customizing your Tailwind config path
28+
### Specifying your Tailwind stylesheet path
29+
30+
When using Tailwind CSS v4 you must specify your CSS file entry point, which includes your theme, custom utilities, and other Tailwind configuration options. To do this, use the `tailwindStylesheet` option in your Prettier configuration.
31+
32+
Note that paths are resolved relative to the Prettier configuration file.
33+
34+
```json5
35+
// .prettierrc
36+
{
37+
"tailwindStylesheet": "./resources/css/app.css"
38+
}
39+
```
40+
41+
### Specifying your Tailwind JavaScript config path
2942

3043
To ensure that the class sorting takes into consideration any of your project's Tailwind customizations, it needs access to your [Tailwind configuration file](https://tailwindcss.com/docs/configuration) (`tailwind.config.js`).
3144

src/config.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ let prettierConfigCache = expiringMap<string, string | null>(10_000)
2828
export async function getTailwindConfig(
2929
options: ParserOptions,
3030
): Promise<ContextContainer> {
31-
let key = `${options.filepath}:${options.tailwindConfig ?? ''}:${options.tailwindEntryPoint ?? ''}`
31+
let key = [
32+
options.filepath,
33+
options.tailwindStylesheet ?? '',
34+
options.tailwindEntryPoint ?? '',
35+
options.tailwindConfig ?? '',
36+
].join(':')
3237
let baseDir = await getBaseDir(options)
3338

3439
// Map the source file to it's associated Tailwind config file
@@ -292,6 +297,10 @@ async function loadV4(
292297

293298
function getConfigPath(options: ParserOptions, baseDir: string): string | null {
294299
if (options.tailwindConfig) {
300+
if (options.tailwindConfig.endsWith('.css')) {
301+
return null
302+
}
303+
295304
return path.resolve(baseDir, options.tailwindConfig)
296305
}
297306

@@ -321,9 +330,25 @@ function getConfigPath(options: ParserOptions, baseDir: string): string | null {
321330
}
322331

323332
function getEntryPoint(options: ParserOptions, baseDir: string): string | null {
333+
if (options.tailwindStylesheet) {
334+
return path.resolve(baseDir, options.tailwindStylesheet)
335+
}
336+
324337
if (options.tailwindEntryPoint) {
338+
console.warn(
339+
'Use the `tailwindStylesheet` option for v4 projects instead of `tailwindEntryPoint`.',
340+
)
341+
325342
return path.resolve(baseDir, options.tailwindEntryPoint)
326343
}
327344

345+
if (options.tailwindConfig && options.tailwindConfig.endsWith('.css')) {
346+
console.warn(
347+
'Use the `tailwindStylesheet` option for v4 projects instead of `tailwindConfig`.',
348+
)
349+
350+
return path.resolve(baseDir, options.tailwindConfig)
351+
}
352+
328353
return null
329354
}

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,14 @@ export interface PluginOptions {
11971197
tailwindConfig?: string
11981198

11991199
/**
1200-
* Path to the Tailwind entry point (v4+)
1200+
* Path to the CSS stylesheet used by Tailwind CSS (v4+)
1201+
*/
1202+
tailwindStylesheet?: string
1203+
1204+
/**
1205+
* Path to the CSS stylesheet used by Tailwind CSS (v4+)
1206+
*
1207+
* @deprecated Use `tailwindStylesheet` instead
12011208
*/
12021209
tailwindEntryPoint?: string
12031210

src/options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ export const options: Record<string, SupportOption> = {
1313
type: 'string',
1414
category: 'Tailwind CSS',
1515
description: 'Path to the CSS entrypoint in your Tailwind project (v4+)',
16+
17+
// Can't include this otherwise the option is not passed to parsers
18+
// deprecated: "This option is deprecated. Use 'tailwindStylesheet' instead.",
19+
},
20+
21+
tailwindStylesheet: {
22+
type: 'string',
23+
category: 'Tailwind CSS',
24+
description: 'Path to the CSS stylesheet in your Tailwind project (v4+)',
1625
},
1726

1827
tailwindAttributes: {

tests/fixtures/v4/basic/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/v4/basic/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"dependencies": {
3-
"tailwindcss": "^4.0.0-alpha.23"
3+
"tailwindcss": "^4.0.0-alpha.34"
44
},
55
"prettier": {
6-
"tailwindEntryPoint": "./app.css"
6+
"tailwindStylesheet": "./app.css"
77
}
88
}

tests/fixtures/v4/configs/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"dependencies": {
3-
"tailwindcss": "^4.0.0-alpha.23"
3+
"tailwindcss": "^4.0.0-alpha.34"
44
},
55
"prettier": {
6-
"tailwindEntryPoint": "./app.css"
6+
"tailwindStylesheet": "./app.css"
77
}
88
}

0 commit comments

Comments
 (0)