{"id":13632289,"url":"https://github.com/tailwindlabs/prettier-plugin-tailwindcss","last_synced_at":"2025-09-09T20:03:49.681Z","repository":{"id":40303073,"uuid":"400148747","full_name":"tailwindlabs/prettier-plugin-tailwindcss","owner":"tailwindlabs","description":"A Prettier plugin for Tailwind CSS that automatically sorts classes based on our recommended class order.","archived":false,"fork":false,"pushed_at":"2025-02-27T14:02:06.000Z","size":863,"stargazers_count":6273,"open_issues_count":44,"forks_count":159,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-05-13T11:07:00.507Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tailwindlabs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-26T11:45:14.000Z","updated_at":"2025-05-13T09:54:28.000Z","dependencies_parsed_at":"2023-12-20T08:58:18.315Z","dependency_job_id":"afbee03f-4bb2-4d8b-81d4-720a465a77b1","html_url":"https://github.com/tailwindlabs/prettier-plugin-tailwindcss","commit_stats":{"total_commits":278,"total_committers":31,"mean_commits":8.96774193548387,"dds":"0.47482014388489213","last_synced_commit":"15e8009e5112ee7453618786a8f7d96c9c0e5b1a"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailwindlabs%2Fprettier-plugin-tailwindcss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailwindlabs%2Fprettier-plugin-tailwindcss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailwindlabs%2Fprettier-plugin-tailwindcss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailwindlabs%2Fprettier-plugin-tailwindcss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tailwindlabs","download_url":"https://codeload.github.com/tailwindlabs/prettier-plugin-tailwindcss/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253929364,"owners_count":21985802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T22:02:58.910Z","updated_at":"2025-09-09T20:03:49.665Z","avatar_url":"https://github.com/tailwindlabs.png","language":"TypeScript","readme":"\u003cimg src=\"https://raw.githubusercontent.com/tailwindlabs/prettier-plugin-tailwindcss/main/.github/banner.jpg\" alt=\"prettier-plugin-tailwindcss\" /\u003e\n\nA [Prettier v3+](https://prettier.io/) plugin for Tailwind CSS v3.0+ that automatically sorts classes based on [our recommended class order](https://tailwindcss.com/blog/automatic-class-sorting-with-prettier#how-classes-are-sorted).\n\n## Installation\n\nTo get started, install `prettier-plugin-tailwindcss` as a dev-dependency:\n\n```sh\nnpm install -D prettier prettier-plugin-tailwindcss\n```\n\nThen add the plugin to your [Prettier configuration](https://prettier.io/docs/en/configuration.html):\n\n```json5\n// .prettierrc\n{\n  \"plugins\": [\"prettier-plugin-tailwindcss\"]\n}\n```\n\nWhen using a JavaScript config, you can import the types for IntelliSense:\n\n```js\n// prettier.config.js\n\n/** @type {import('prettier').Config \u0026 import('prettier-plugin-tailwindcss').PluginOptions} */\nexport default {\n  plugins: [\"prettier-plugin-tailwindcss\"],\n}\n```\n\n## Upgrading to v0.5.x\n\nAs of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means it cannot be loaded via `require()`. For more information see our [upgrade guide](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/issues/207#issuecomment-1698071122).\n\n## Options\n\n### Specifying your Tailwind stylesheet path (Tailwind CSS v4+)\n\nWhen 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.\n\nNote that paths are resolved relative to the Prettier configuration file.\n\n```json5\n// .prettierrc\n{\n  \"tailwindStylesheet\": \"./resources/css/app.css\"\n}\n```\n\n### Specifying your Tailwind JavaScript config path (Tailwind CSS v3)\n\nTo 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`).\n\nBy 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.\n\nNote that paths are resolved relative to the Prettier configuration file.\n\n```json5\n// .prettierrc\n{\n  \"tailwindConfig\": \"./styles/tailwind.config.js\"\n}\n```\n\nIf a local configuration file cannot be found the plugin will fallback to the default Tailwind configuration.\n\n## Sorting non-standard attributes\n\nBy default this plugin sorts classes in the `class` attribute, any framework-specific equivalents like `className`, `:class`, `[ngClass]`, and any Tailwind `@apply` directives.\n\nYou can sort additional attributes using the `tailwindAttributes` option, which takes an array of attribute names:\n\n```json5\n// .prettierrc\n{\n  \"tailwindAttributes\": [\"myClassList\"]\n}\n```\n\nWith this configuration, any classes found in the `myClassList` attribute will be sorted:\n\n```jsx\nfunction MyButton({ children }) {\n  return (\n    \u003cbutton myClassList=\"rounded bg-blue-500 px-4 py-2 text-base text-white\"\u003e\n      {children}\n    \u003c/button\u003e\n  );\n}\n```\n\n## Sorting classes in function calls\n\nIn addition to sorting classes in attributes, you can also sort classes in strings provided to function calls. This is useful when working with libraries like [clsx](https://github.com/lukeed/clsx) or [cva](https://cva.style/).\n\nYou can sort classes in function calls using the `tailwindFunctions` option, which takes a list of function names:\n\n```json5\n// .prettierrc\n{\n  \"tailwindFunctions\": [\"clsx\"]\n}\n```\n\nWith this configuration, any classes in `clsx()` function calls will be sorted:\n\n```jsx\nimport clsx from 'clsx'\n\nfunction MyButton({ isHovering, children }) {\n  let classes = clsx(\n    'rounded bg-blue-500 px-4 py-2 text-base text-white',\n    {\n      'bg-blue-700 text-gray-100': isHovering,\n    },\n  )\n\n  return (\n    \u003cbutton className={classes}\u003e\n      {children}\n    \u003c/button\u003e\n  )\n}\n```\n\n## Sorting classes in template literals\n\nThis plugin also enables sorting of classes in tagged template literals.\n\nYou can sort classes in template literals using the `tailwindFunctions` option, which takes a list of function names:\n\n```json5\n// .prettierrc\n{\n  \"tailwindFunctions\": [\"tw\"],\n}\n```\n\nWith this configuration, any classes in template literals tagged with `tw` will automatically be sorted:\n\n```jsx\nimport { View, Text } from 'react-native'\nimport tw from 'twrnc'\n\nfunction MyScreen() {\n  return (\n    \u003cView style={tw`bg-white p-4 dark:bg-black`}\u003e\n      \u003cText style={tw`text-md text-black dark:text-white`}\u003eHello World\u003c/Text\u003e\n    \u003c/View\u003e\n  )\n}\n```\n\nThis feature can be used with third-party libraries like `twrnc` or you can create your own tagged template by defining this \"identity\" function:\n\n```js\nconst tw = (strings, ...values) =\u003e String.raw({ raw: strings }, ...values)\n```\n\nOnce added, tag your strings with the function and the plugin will sort them:\n\n```js\nconst mySortedClasses = tw`bg-white p-4 dark:bg-black`\n```\n\n## Preserving whitespace\n\nThis plugin automatically removes unnecessary whitespace between classes to ensure consistent formatting. If you prefer to preserve whitespace, you can use the `tailwindPreserveWhitespace` option:\n\n```json5\n// .prettierrc\n{\n  \"tailwindPreserveWhitespace\": true,\n}\n```\n\nWith this configuration, any whitespace surrounding classes will be preserved:\n\n```jsx\nimport clsx from 'clsx'\n\nfunction MyButton({ isHovering, children }) {\n  return (\n    \u003cbutton className=\" rounded  bg-blue-500 px-4  py-2     text-base text-white \"\u003e\n      {children}\n    \u003c/button\u003e\n  )\n}\n```\n\n## Preserving duplicate classes\n\nThis plugin automatically removes duplicate classes from your class lists. However, this can cause issues in some templating languages, like Fluid or Blade, where we can't distinguish between classes and the templating syntax.\n\nIf removing duplicate classes is causing issues in your project, you can use the `tailwindPreserveDuplicates` option to disable this behavior:\n\n```json5\n// .prettierrc\n{\n  \"tailwindPreserveDuplicates\": true,\n}\n```\n\nWith this configuration, anything we perceive as duplicate classes will be preserved:\n\n```html\n\u003cdiv\n  class=\"\n    {f:if(condition: isCompact, then: 'grid-cols-3', else: 'grid-cols-5')}\n    {f:if(condition: isDark, then: 'bg-black/50', else: 'bg-white/50')}\n    grid gap-4 p-4\n  \"\n\u003e\n\u003c/div\u003e\n```\n\n## Compatibility with other Prettier plugins\n\nThis plugin uses Prettier APIs that can only be used by one plugin at a time, making it incompatible with other Prettier plugins implemented the same way. To solve this we've added explicit per-plugin workarounds that enable compatibility with the following Prettier plugins:\n\n- `@ianvs/prettier-plugin-sort-imports`\n- `@prettier/plugin-pug`\n- `@shopify/prettier-plugin-liquid`\n- `@trivago/prettier-plugin-sort-imports`\n- `prettier-plugin-astro`\n- `prettier-plugin-css-order`\n- `prettier-plugin-jsdoc`\n- `prettier-plugin-multiline-arrays`\n- `prettier-plugin-organize-attributes`\n- `prettier-plugin-organize-imports`\n- `prettier-plugin-svelte`\n- `prettier-plugin-sort-imports`\n\nOne limitation with this approach is that `prettier-plugin-tailwindcss` *must* be loaded last.\n\n```json5\n// .prettierrc\n{\n  // ..\n  \"plugins\": [\n    \"prettier-plugin-svelte\",\n    \"prettier-plugin-organize-imports\",\n    \"prettier-plugin-tailwindcss\" // MUST come last\n  ]\n}\n```\n","funding_links":[],"categories":["TypeScript","JavaScript","Tools","前端","Uncategorized"],"sub_categories":["文本处理","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftailwindlabs%2Fprettier-plugin-tailwindcss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftailwindlabs%2Fprettier-plugin-tailwindcss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftailwindlabs%2Fprettier-plugin-tailwindcss/lists"}