Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting with external plugins like Svelte, Astro, etc… not working when using Prettier v3 #186

Closed
GeoGeorgeous opened this issue Jul 19, 2023 · 29 comments
Assignees

Comments

@GeoGeorgeous
Copy link

GeoGeorgeous commented Jul 19, 2023

What version of prettier-plugin-tailwindcss are you using?
v0.4.1

What version of Tailwind CSS are you using?
v3.3.3

What version of Node.js are you using?
v18.16.1

What package manager are you using?
npm

What operating system are you using?
Windows

package.json:

"devDependencies": {
    "@sveltejs/adapter-auto": "^2.0.0",
    "@sveltejs/adapter-static": "^2.0.2",
    "@sveltejs/kit": "^1.22.3",
    "@tailwindcss/forms": "^0.5.4",
    "autoprefixer": "^10.4.14",
    "eslint": "^8.45.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-svelte": "^2.31.1",
    "postcss": "^8.4.26",
    "prettier": "^3.0.0",
    "prettier-plugin-svelte": "^3.0.0",
    "prettier-plugin-tailwindcss": "^0.4.1",
    "svelte": "^4.0.5",
    "tailwindcss": "^3.3.3",
    "vite": "^4.4.4"
  },

settings.json:

{
  <...>
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "prettier.documentSelectors": ["**/*.svelte"],
  "prettier.requireConfig": true,
  "[svelte]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

.prettierrc:

{
  "useTabs": false,
  "singleQuote": true,
  "semi": true,
  "trailingComma": "all",
  "arrowParens": "avoid",
  "printWidth": 100,
  "plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
  "pluginSearchDirs": false,
  "overrides": [
    {
      "files": "**/*.svelte",
      "options": {
        "parser": "svelte"
      }
    }
  ]
}

tailwind.config.js:

/** @type {import('tailwindcss').Config} */
const defaultTheme = require('tailwindcss/defaultTheme');

export default {
  content: ['./src/**/*.{html,js,svelte,ts}'],
  theme: {
    extend: {
      maxWidth: {
        '8xl': '120rem',
      },
      fontFamily: {
        sans: ['Inter var', ...defaultTheme.fontFamily.sans],
      },
    },
  },
  plugins: [require('@tailwindcss/forms')],
};

Hi! I'm encountering an issue while setting up the prettier-plugin-tailwindcss and prettier-plugin-svelte plugins for my Svelte app. The problem is that the tailwind classes are not being sorted, even though the prettier-plugin-svelte is functioning correctly for other formatting aspects like tabs, quotes, and empty lines.

  • Tailwind config is located in in the root folder, along with the rest of the configs,
  • Prettier debug output does not give explicit reasons.
  • I have tried to reinstall dependencies, restart the VSCode,
  • Both the "format on save" option and the npx prettier --write . command produce the same result, with no sorting of tailwind classes.

Any guidance or advice on resolving this issue would be greatly appreciated.

@juanpujol
Copy link

juanpujol commented Jul 19, 2023

@GeoGeorgeous I will recommend to stick to "prettier": "2.8.8" for now. Plugins are being upgraded to v3, documentation is not clear and there's an issue where you have to pass plugin options to the CLI commands even if configured on the pretierrc. I think this migration needs more time to mature.

I will wait a few more weeks and try to update to v3 again.

@TristanBrotherton
Copy link

I also have this issue, which surfaced after upgrading everything to Prettier v3. Thanks for all your hard work!

@juanpujol
Copy link

This is what I did in case someone else wants to give it a try:

  1. Update prettier to v3
  2. Update all the plugins prettier-plugin-svelte, prettier-plugin-organize-imports and prettier-plugin-tailwindcss to their latest (prettier v3 compatible) versions
  3. Remove "pluginSearchDirs": false from the prettier config
  4. Change the package.json script to "format": "prettier --plugin=prettier-plugin-svelte --plugin=prettier-plugin-organize-imports --plugin=prettier-plugin-tailwindcss --write ."

Then when I run the format command it works for svelte files but it does not run the other plugins on svelte files. Imports and tailwind are not updating on my tests.

@thecrypticace
Copy link
Contributor

Hi! When we released v3 Svelte, Astro, and all suppored plugins from v2 (as far as I know) had not yet updated. We temporarily disabled support for external plugins because of this — when using v3 only (because there's no way we would be able to shim v2 -> v3 for external plugins).

I'm taking a look at all our previously supported plugins on Monday and will try to get a release out Monday/Tuesday with any that have updated.

@thecrypticace
Copy link
Contributor

Short update here:

I've got a prototype version of the v3 plugin with support Astro, Svelte, and other plugins. We've had to switch to using ESM for v3 because we have to use top-level await to dynamically load plugins that we have compatibility with. This is because some of them have switched to ESM which means dynamic imports return promises (… ESM really should have had a sync dynamic import primitive — at least for node :/).

I'm currently running into issues with the vscode prettier extension loading the wrong version of the plugin so it doesn't look like I'll get a release out today — sorry. But it is being worked on! :)

@TristanBrotherton
Copy link

Is anyone aware if there is a manual work-around for now to be able to select class strings in VsCode and use a command to sort the classes?

@thecrypticace
Copy link
Contributor

thecrypticace commented Jul 27, 2023

@TristanBrotherton you can use Prettier v2 with v0.4 of this plugin and everything should work fine. Note that you'll need to use an earlier version of prettier-plugin-svelte as well. It's the prettier v3 compatibility including external plugins that's the complication at the moment.

@TristanBrotherton
Copy link

@thecrypticace thanks so much for the response. Unfortunately I'm locked in with Prettier V3 - its not a big deal though. I can just sort the classes retrospectively. I really appreciate your work on this, thank you from all of us!

@thecrypticace thecrypticace changed the title Classes not sorted with prettier-plugin-tailwindcss and prettier-plugin-svelte Sorting with external plugins like Svelte, Astro, etc… not working when using Prettier v3 Jul 28, 2023
@thecrypticace
Copy link
Contributor

thecrypticace commented Aug 9, 2023

Hey all! I pushed out an insiders build that enables support for plugins!

There are two important things to note:

  • It supports Prettier v3+ only — ultimately we couldn't find a way to support both v2 and v3 within the same plugin and have it work with some external tools
  • Most but not all plugins that were supported in v2 work. Marko and Twig support have been removed as they don't support v3 currently. If they get updates to support Prettier v3 in the future we'll look into it.

I've done a good bit of testing myself and things appear to work but it would be great if you all could test our insiders build in whatever tooling you're using and report back! Also, if you're using VSCode, be sure to reload VSCode after upgrading any prettier plugins.

npm install prettier-plugin-tailwindcss@insiders

@eminaliyevx
Copy link

Hey all! I pushed out an insiders build that enables support for plugins!

There are two important things to note:

  • It supports Prettier v3+ only — ultimately we couldn't find a way to support both v2 and v3 within the same plugin and have it work with some external tools
  • Most but not all plugins that were supported in v2 work. Marko and Twig support have been removed as they don't support v3 currently. If they get updates to support Prettier v3 in the future we'll look into it.

I've done a good bit of testing myself and things appear to work but it would be great if you all could test our insiders build in whatever tooling you're using and report back! Also, if you're using VSCode, be sure to reload VSCode after upgrading any prettier plugins.

npm install prettier-plugin-tailwindcss@insiders

Thank you for your work. It managed to sort Tailwind classes after upgrading to the aforementioned build. By the way, I had the following Prettier config.

{ "plugins": [ "prettier-plugin-astro", "prettier-plugin-organize-imports", "prettier-plugin-tailwindcss" ], "tailwindFunctions": ["cva"] }

@wiew
Copy link

wiew commented Aug 9, 2023

it's working for me after upgrading to insiders version, vscode restart was needed. i'm using it in .astro files.
thanks for your work @thecrypticace!

@baresi687
Copy link

baresi687 commented Aug 9, 2023

@thecrypticace

It's working with Svelte.
In both ESM and JSON.

package.json

"prettier": "^3.0.1",
"prettier-plugin-svelte": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.0.0-insiders.ac70a66",

.prettierrc.mjs/js

/** @type {import("prettier").Options} */
export default {
	useTabs: true,
	singleQuote: true,
	trailingComma: 'none',
	printWidth: 100,
	plugins: ['prettier-plugin-svelte', 'prettier-plugin-tailwindcss'],
	overrides: [{ files: '*.svelte', options: { parser: 'svelte' } }]
};

@delight
Copy link

delight commented Aug 9, 2023

I can also comfirm that it is working in VSCode after restart with

		"prettier": "^3.0.1",
		"prettier-plugin-svelte": "^3.0.3",
		"prettier-plugin-tailwindcss": "0.0.0-insiders.ac70a66",

and

	"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
	"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]

in .prettierrc

Also changed the format run script to:
"format": "prettier --plugin=prettier-plugin-svelte --plugin=prettier-plugin-tailwindcss --write .",

@baresi687
Copy link

baresi687 commented Aug 9, 2023

@delight

I also had to change the format script to:
"format": "prettier --write . --plugin=prettier-plugin-svelte --plugin=prettier-plugin-tailwindcss"
https://prettier.io/docs/en/plugins

I had this previously:
"format": "prettier --plugin-search-dir . --write .",
This does not work with .svelte files.

@thecrypticace
Copy link
Contributor

@delight @baresi687 Prettier autoloading was removed with Prettier v3 meaning that you must specify all plugins explicitly. The --plugin-search-dir / pluginSearchDirs options no longer exist in Prettier.

@evgrekov
Copy link

Thanks a lot for your work. However, not working for me, tried on a clean Astro setup.

My route:
npm create astro@latest

npx astro add tailwind

npm i prettier-plugin-astro

npm install prettier-plugin-tailwindcss@insiders

.prettierrc
{"plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"]}

Prettier is not working at all, no formatting and no tailwind class sort.

Maybe I do something wrong?

@mvllow
Copy link

mvllow commented Aug 10, 2023

@evgrekov you need to install prettier, not just the plugins

@Serator
Copy link

Serator commented Aug 10, 2023

Works with Svelte, but only when modifying the format run script as mentioned in #186 (comment).

@thecrypticace
Copy link
Contributor

@Serator Do you have a prettier config file? If so, and you still have to modify the format script, can you provide a repro? This is likely to be an issue with Prettier itself.

@Serator
Copy link

Serator commented Aug 10, 2023

@thecrypticace

.prettierrc

{
  "bracketSpacing": false,
  "overrides": [{"files": "*.svelte", "options": {"parser": "svelte"}}],
  "plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
  "printWidth": 128,
  "semi": false,
  "singleQuote": true,
  "tailwindAttributes": ["iconClass", "imageClass", "rootClass", "titleClass"]
}

npm run script

"format": "prettier --plugin=prettier-plugin-svelte --plugin=prettier-plugin-tailwindcss --write .",

I'll be able to add a repository with an example a little later.

@thecrypticace
Copy link
Contributor

Hey all I've published v0.5.0 which mirrors the insiders build published yesterday — as such I'm closing this issue given that things seem to be working for everyone in most cases! Thanks for giving it a test — much appreciated!

@thecrypticace
Copy link
Contributor

@Serator I'll still be looking for the update here — but like I said I suspect this is actually a Prettier problem rather than prettier-plugin-tailwindcss problem 👍

@Serator
Copy link

Serator commented Aug 10, 2023

@thecrypticace It looks like my issue is described here along with an example to reproduce and it is indeed a problem with Prettier itself.

@evgrekov
Copy link

@mvllow thanks it's working now :)

@Vaiterius
Copy link

Thanks a lot for your work. However, not working for me, tried on a clean Astro setup.

My route: npm create astro@latest

npx astro add tailwind

npm i prettier-plugin-astro

npm install prettier-plugin-tailwindcss@insiders

.prettierrc {"plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"]}

Prettier is not working at all, no formatting and no tailwind class sort.

Maybe I do something wrong?

Adding on to this, make sure "prettier-plugin-tailwindcss" comes last in the plugins list as referenced by this comment in another thread. I had these two reversed and had a rough time figuring out why they weren't being compatible 😅

@aurelienbobenrieth
Copy link

@Vaiterius
I still have conflict on a project, I posted my config here.
Do you have any tips ?

@Frost-on-Web
Copy link

Hi,

{
  "tailwindFunctions": ["cn"],
  "overrides": [
    { "files": ["*.astro"], "options": { "parser": "astro" } },
    { "files": ["*.html"], "options": { "tabWidth": 8 } }
  ],
  "tailwindConfig": "./tailwind.config.js",
  "plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"]
}

VSCode auto format (on save) doesn't apply the "prettier-plugin-tailwindcss" effects.

BUT, at least, i found that
yarn run prettier --plugin=prettier-plugin-astro --plugin=prettier-plugin-tailwindcss --write .

is working.

I tried both :
prettier-plugin-tailwindcss@0.5.7 and prettier-plugin-tailwindcss@insiders
with the same result.

So my workaround will be to have a package.json (npm/yarn) command such as :
"format": "yarn run prettier --plugin=prettier-plugin-astro --plugin=prettier-plugin-tailwindcss --write ."

I thank you for all of your work, past and future ;-) Bye

@hwk-omv
Copy link

hwk-omv commented Dec 23, 2023

BUT, at least, i found that yarn run prettier --plugin=prettier-plugin-astro --plugin=prettier-plugin-tailwindcss --write .

is working.

That is interesting. I still can not manage to achieve that in my Astro project. With none of the suggested solutions, unfortunately.

@mmartinrm97
Copy link

Thanks a lot for your work. However, not working for me, tried on a clean Astro setup.

My route: npm create astro@latest

npx astro add tailwind

npm i prettier-plugin-astro

npm install prettier-plugin-tailwindcss@insiders

.prettierrc {"plugins": ["prettier-plugin-astro", "prettier-plugin-tailwindcss"]}

Prettier is not working at all, no formatting and no tailwind class sort.

Maybe I do something wrong?

I follow this blog, and it works for me:
https://webreaper.dev/posts/astro-prettier-tailwind-setup/

this is my package.json:

 "scripts": {
    "dev": "astro dev",
    "start": "astro dev",
    "build": "astro check && astro build",
    "preview": "astro preview",
    "astro": "astro",
    "format": "prettier  --plugin=prettier-plugin-astro --plugin=prettier-plugin-tailwindcss --write ."
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests