prettier-plugin-tailwindcss

repository·main·Indexed 27 days ago

https://github.com/tailwindlabs/prettier-plugin-tailwindcss

A Prettier plugin that automatically sorts Tailwind CSS classes according to the recommended order. It supports Tailwind CSS v3 and v4 across various frameworks including HTML, JavaScript, TypeScript, Svelte, Astro, Vue, and Angular. The plugin can be configured to sort classes in attributes, function calls (like clsx or cva), and template literals, and provides a programmatic `createSorter` API for custom sorting logic.

Tokens
4K
Snippets
14
Records
29
Agent score
88%

What's inside prettier-plugin-tailwindcss

  1. Configure IntelliSense for JavaScript Prettier configs

    main

    If you are using a JavaScript-based Prettier configuration (e.g., prettier.config.js), you can import types for IntelliSense support.

    // prettier.config.js
    
    /** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
    export default {
      plugins: ["prettier-plugin-tailwindcss"],
    }
  2. Ensure compatibility with other Prettier plugins

    main

    Because this plugin uses specific Prettier APIs, it must be loaded last in your plugins array to maintain compatibility with other plugins (like prettier-plugin-svelte or prettier-plugin-organize-imports).

    // .prettierrc
    {
      "plugins": [
        "prettier-plugin-svelte",
        "prettier-plugin-organize-imports",
        "prettier-plugin-tailwindcss" // MUST come last
      ]
    }
  3. Supported Parsers and Frameworks

    main

    The prettier-plugin-tailwindcss plugin supports class sorting across a wide variety of parsers and frameworks. It automatically detects and applies transformations for:

    • HTML/Web: HTML, Angular, Vue, LWC, Astro, Glimmer, Marko, Pug, Twig, and Svelte.
    • JavaScript/TypeScript: Babel, Babel-flow, Babel-ts, TypeScript, Meriyah, Acorn, Flow, OXC, OXC-TS, and Hermes.
    • CSS: CSS, SCSS, and Less.

    For many frameworks, the plugin handles both static attributes (e.g., class) and dynamic attributes (e.g., :class in Vue, [ngClass] in Angular, or class:list in Astro).

  4. Sort non-standard attributes with tailwindAttributes

    main

    By default, the plugin sorts class, className, :class, [ngClass], and @apply. To sort additional attributes, provide an array of attribute names to tailwindAttributes. You can also use regular expressions enclosed in forward slashes (e.g., /data-.*/) to match multiple attributes.

    // .prettierrc
    {
      "tailwindAttributes": ["myClassList", "/data-.*/"]
    }
  5. Specify Tailwind CSS v4 stylesheet path

    main

    When using Tailwind CSS v4, you must specify your CSS file entry point (which contains your theme and custom utilities) using the tailwindStylesheet option. Paths are resolved relative to the Prettier configuration file.

    // .prettierrc
    {
      "tailwindStylesheet": "./resources/css/app.css"
    }
  6. Specify Tailwind CSS v3 JavaScript config path

    main

    To ensure class sorting accounts for project customizations, specify your tailwind.config.js path using the tailwindConfig option. If not specified, the plugin looks in the same directory as the Prettier config. Paths are resolved relative to the Prettier configuration file.

    // .prettierrc
    {
      "tailwindConfig": "./styles/tailwind.config.js"
    }
  7. Use the Tailwind class sorter API

    main

    You can use the Tailwind sorting logic programmatically by importing createSorter from prettier-plugin-tailwindcss/sorter. This is useful for sorting HTML class attributes (space-separated strings) or class lists (arrays of strings).

    import { createSorter } from 'prettier-plugin-tailwindcss/sorter'
    
    let sorter = await createSorter({
      base: '/path/to/project',
      stylesheetPath: './app.css',
    })
    
    // Sort HTML class attributes (space-separated strings)
    let sorted = sorter.sortClassAttributes([
      'sm:bg-tomato bg-red-500',
      'p-4 m-2'
    ])
    // Returns: ['bg-red-500 sm:bg-tomato', 'm-2 p-4']
    
    // Sort class lists (arrays of class names)
    let sortedLists = sorter.sortClassLists([
      ['sm:bg-tomato', 'bg-red-500'],
      ['p-4', 'm-2']
    ])
    // Returns: [['bg-red-500', 'sm:bg-tomato'], ['m-2 p-4']]
  8. Configure TransformOptions for the Tailwind Prettier Plugin

    main

    When extending or interacting with the plugin's transformation logic, you can use the TransformOptions interface to define how Tailwind classes are identified and sorted across different parsers.

    Key configuration properties include:

    • staticAttrs: A list of static attributes (e.g., class, className) supported by default.
    • dynamicAttrs: A list of dynamic or expression-based attributes supported by default.
    • load: An array of PluginLoad objects (either a standard Prettier Plugin or a LazyPluginLoad containing a name and an importer function) to be used for parsers and printers.
    • compatible: A list of compatible, third-party plugin names. These are loaded lazily during the parse call to avoid errors.
    • parsers: A record mapping parser names to specific configurations, allowing you to override load, staticAttrs, or dynamicAttrs for specific parsers.
    • printers: A record of supported printer names.
    • transform: An optional function to transform entire ASTs, receiving the ast and a TransformerEnv.
    • reprint: An optional function to transform ASTs via an AstPath, receiving the path and TransformerEnv.