Skip to content
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ As of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means i

## Options

### Using with Tailwind CSS v4.0

When using Tailwind CSS v4 you must specify what CSS file we should use that contains your theme, custom utilities, etc… To do this use the `tailwindStylesheet` option in your Prettier configuration.

Note that paths are resolved relative to the Prettier configuration file.

```json5
// .prettierrc
{
"tailwindStylesheet": "./resources/css/app.css"
}
```

Note: this option was previously named `tailwindEntryPoint` it has been renamed to better reflect its purpose.

### Customizing your Tailwind config path

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`).
Expand Down
27 changes: 26 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ let prettierConfigCache = expiringMap<string, string | null>(10_000)
export async function getTailwindConfig(
options: ParserOptions,
): Promise<ContextContainer> {
let key = `${options.filepath}:${options.tailwindConfig ?? ''}:${options.tailwindEntryPoint ?? ''}`
let key = [
options.filepath,
options.tailwindStylesheet ?? '',
options.tailwindEntryPoint ?? '',
options.tailwindConfig ?? '',
].join(':')
let baseDir = await getBaseDir(options)

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

function getConfigPath(options: ParserOptions, baseDir: string): string | null {
if (options.tailwindConfig) {
if (options.tailwindConfig.endsWith('.css')) {
return null
}

return path.resolve(baseDir, options.tailwindConfig)
}

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

function getEntryPoint(options: ParserOptions, baseDir: string): string | null {
if (options.tailwindStylesheet) {
return path.resolve(baseDir, options.tailwindStylesheet)
}

if (options.tailwindEntryPoint) {
console.warn(
'Use the `tailwindStylesheet` option for v4 projects instead of `tailwindEntryPoint`.',
)

return path.resolve(baseDir, options.tailwindEntryPoint)
}

if (options.tailwindConfig && options.tailwindConfig.endsWith('.css')) {
console.warn(
'Use the `tailwindStylesheet` option for v4 projects instead of `tailwindConfig`.',
)

return path.resolve(baseDir, options.tailwindConfig)
}

return null
}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,14 @@ export interface PluginOptions {
tailwindConfig?: string

/**
* Path to the Tailwind entry point (v4+)
* Path to the CSS stylesheet used by Tailwind CSS (v4+)
*/
tailwindStylesheet?: string

/**
* Path to the CSS stylesheet used by Tailwind CSS (v4+)
*
* @deprecated Use `tailwindStylesheet` instead
*/
tailwindEntryPoint?: string

Expand Down
9 changes: 9 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export const options: Record<string, SupportOption> = {
type: 'string',
category: 'Tailwind CSS',
description: 'Path to the CSS entrypoint in your Tailwind project (v4+)',

// Can't include this otherwise the option is not passed to parsers
// deprecated: "This option is deprecated. Use 'tailwindStylesheet' instead.",
},

tailwindStylesheet: {
type: 'string',
category: 'Tailwind CSS',
description: 'Path to the CSS stylesheet in your Tailwind project (v4+)',
},

tailwindAttributes: {
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/v4/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/fixtures/v4/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dependencies": {
"tailwindcss": "^4.0.0-alpha.23"
"tailwindcss": "^4.0.0-alpha.34"
},
"prettier": {
"tailwindEntryPoint": "./app.css"
"tailwindStylesheet": "./app.css"
}
}
8 changes: 4 additions & 4 deletions tests/fixtures/v4/configs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/fixtures/v4/configs/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dependencies": {
"tailwindcss": "^4.0.0-alpha.23"
"tailwindcss": "^4.0.0-alpha.34"
},
"prettier": {
"tailwindEntryPoint": "./app.css"
"tailwindStylesheet": "./app.css"
}
}
Loading