A Prettier plugin for Tailwind CSS v3.0+ that automatically sorts classes based on our recommended class order.
Installation
To get started, just install prettier-plugin-tailwindcss
as a dev-dependency:
npm install -D prettier prettier-plugin-tailwindcss
This plugin follows Prettier’s autoloading convention, so as long as you’ve got Prettier set up in your project, it’ll start working automatically as soon as it’s installed.
Note that plugin autoloading is not supported when using certain package managers, such as pnpm or Yarn PnP. In this case you may need to add the plugin to your Prettier config explicitly:
// prettier.config.js
module.exports = {
plugins: [require('prettier-plugin-tailwindcss')],
}
Resolving your Tailwind configuration
To ensure that the class sorting is taking into consideration any of your project's Tailwind customizations, it needs access to your Tailwind configuration file (tailwind.config.js
).
By default the plugin will look for this file in the same directory as your Prettier configuration file. However, if your Tailwind configuration is somewhere else, you can specify this using the tailwindConfig
option in your Prettier configuration.
Note that paths are resolved relative to the Prettier configuration file.
// prettier.config.js
module.exports = {
tailwindConfig: './styles/tailwind.config.js',
}
If a local configuration file cannot be found the plugin will fallback to the default Tailwind configuration.
Compatibility with other Prettier plugins
To make this plugin work we had to use private Prettier APIs that can only be used by a single plugin at once. This means this plugin is incompatible with other Prettier plugins that are using the same APIs.
The most popular example we know of is prettier-plugin-svelte, which can't be installed at the same time as the Tailwind CSS plugin.
To work around this, we've bundled prettier-plugin-svelte
directly into prettier-plugin-tailwindcss
, so if you'd like to use this plugin with Svelte, just uninstall prettier-plugin-svelte
and everything should work as expected.
If you discover any other incompatibilities, please share them in this issue and hopefully we can figure out a way to make it work.
Incompatibility with other Prettier plugins
To make this plugin work we had to use private Prettier APIs that can only be used by a single plugin at once. This means this plugin is incompatible with other Prettier plugins that are using the same APIs.
This GitHub issue will serve as a place to keep track of which Prettier plugins are incompatible — and hopefully we'll eventually find some workarounds, or even a proper long term solution. 👍
Known incompatibilities
@trivago/prettier-plugin-sort-imports
prettier-plugin-organize-imports
prettier-plugin-svelte
(see below)prettier-plugin-twig-melody
prettier-plugin-svelte
We've bundled the
prettier-plugin-svelte
directly intoprettier-plugin-tailwindcss
, so if you'd like to use this plugin with Svelte, just uninstallprettier-plugin-svelte
and everything should work as expected.Workarounds
While I have not tested it yet, @Mattinton provided one possible workaround in this comment.
Unable to get working VSCode formatOnSave
Apologies if this is a naive question, not had much experience with how prettier & plugins work...
I am trying to get this to work in a Svelte (+TS) setup, and it seems to run fine if I run the
pnpm run format
script across all files, but the plugin is not running on save withformatOnSave
enabled in VSCode.Is there something I need to set to enable plugins when running within VSCode
formatOnSave
? Or is it because theformatOnSave
uses the Prettier extension, so doesn't see the project installed plugin and would only work if a Tailwind Prettier extension appeared?VSCode settings.json
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
Current set up...
.prettierrc
tailwind.config.cjs
in the root of application (same as.prettierrc
).vscode/settings.json
Any guidance would be great!
Incompatible with `prettier-plugin-twig-melody`
Hello,
Regarding this part of the README.
This plugin does not work with
*.twig
files, with prettier-plugin-twig-melodyprettier version: 2.5.1 prettier-plugin-twig-melody version: 0.4.6 tailwindcss version: 2.2.19
My .prettierrc.js
Only the settings from the twig plugin are applied when running prettier, class names stay in the same order.
Add support for formatting `.astro` files
Hello!
I'm Erika from the Astro team. We love Tailwind and would love to have support for Astro files inside this plugin. As such, well, here's a PR that adds it 😄
The support is pretty good, since we use other parsers for the different parts of the files, including expressions:
I hope we can get this to work, don't hesitate if there's anything needed from us to get this over the line!
Thank you
[WIP] React Native "style" support
Overview
This PR is a quick stab at expanding the JSX attributes to include
style
for use in React Native. The use-case behind this is TailwindCSS is great and this plugin automates anotherstandard
that we would otherwise have to debate at some point. But, it doesn't yet support react native.Hoping you're open to the idea and thanks for the killer 💯 plugin 🔌
Will not format classes within Blade files.
I'm using VS Code and I have everything installed as required,
format on save
is on.When I save, I get an error on the bottom that says
Extension 'Prettier - code formatter' is configured as formatter but can not format 'Blade' files.
I have this in my .prettierrc as noted in another issue, but it's not affecting anything:
If I run
npx prettier --check resources/views
it states everything is formatted properly (which it's not)Interestingly, if I change a blade file to be a plain HTML file,
format on save
still does not do anything, but runningnpx prettier
will then find the issues.What am I doing wrong?
Is there no support for the formatter in blade files within a Laravel project?
Error formatting document
Hey there! I'm getting this error in the VS code output console when trying to format a rather large .vue file. (SFC with
script setup
). I tried a bunch of files and this one isn't formatting properly, all others work super smooth. I can't share the exact file but I can provide some more details if necessary.prettier
version: 2.5.1prettier-plugin-tailwindcss
version: 0.1.3tailwindcss
version: 3.0.16feat: support properties and variable declarators
This PR adds support for object properties and variable declarators within JavaScript-based languages, supporting either of the following syntaxes and their combinations:
Not working on HTML within .php files
Hey guys,
I installed the plugin and it's working like a charm on .html files (VSCode, Prettier extension + prettier php plugin).
However, doesn't seem to apply the same changes if file's extension is .php.
Am I missing something or currently HTML in PHP files is not supported?
seems to have no effect on class lists in template literals
as an example:
plugin seems to not operate on these strings, flattening them into a single line seems to not have any effect either
I have also tried randomly shuffling the class list to see if they were already sorted
Mismatch with Github Action using Prettier
I'm using a Github action to format any code that my team pushes up using prettier. It's incredibly useful but I also have a local instance of Prettier installed via my node package.json and in VScode.
There seems to be a mismatch in how the two versions of prettier handle a custom font class.
VSCode returns this:
My github action returns this:
Is there any reason for this discrepancy?
Actually, I just realized it is more than just the font classes. There are more classes where whatever ordering the styles aren't in sync. I do have this plugin in my dev dependencies so I t should be getting installed. Adding the commit log for this to show all the changes: https://github.com/fairdataihub/SODA-for-COVID-19-Research/commit/d4b3dc6642115bbb3f0ce3ba7c47038c2edcb563
Add prettier plugins to peer dependencies
Fixes #112
I tested it by publishing it to a private npm registry. You can test it for yourself (the yarn cache is checked into the repo): https://github.com/notpeelz/bug-repro-prettier-tw-sort-imports/tree/fixed
prettier-plugin-tailwindcss doesn't work with vscode
prettier-plugin-tailwindcs: v0.2.1 Tailwind CSS: v3.2.4 Node.js: v18.12.1 Package Manager: pnpm v7.21.0 Operating System: Windows 11
Describe your issue prettier-plugin-tailwindcss doesn't work with vscode and it's plugin
Svelte for VS Code
andPrettier - Code formatter
; it won't format the *.svelte files (but it formats the html files);And with the svelte files, the "prettier" has taken effect, cause the
prettier-plugin-organize-imports
andprettier-plugin-css-order
works fine,exceptprettier-plugin-tailwindcss
...,so i think Is there anything that prevents tailwind formatting?...here is my
.prettierrc
Vue `` classes are not sorted
For the Vue components, only the attributes
class
and:class
are sorted.https://github.com/tailwindlabs/prettier-plugin-tailwindcss/blob/01fe07a29aeefcc7933e5cec22a840ab07fd3918/src/index.js#L543
However the
Transition
component in Vue has many other attributes that contain class namesCan support be added to lint the order of classes inside these attributes as well?