JSPM

eslint-plugin-tailwindcss

4.0.0-alpha.7
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1513787
  • Score
    100M100P100Q194359F
  • License MIT

Rules enforcing best practices while using Tailwind CSS

Package Exports

  • eslint-plugin-tailwindcss
  • eslint-plugin-tailwindcss/package.json

Readme

eslint-plugin-tailwindcss
![npm latest version](https://img.shields.io/npm/v/eslint-plugin-tailwindcss?style=for-the-badge) ![license](https://img.shields.io/npm/l/eslint-plugin-tailwindcss?style=for-the-badge) ![downloads](https://img.shields.io/npm/dt/eslint-plugin-tailwindcss?style=for-the-badge)

eslint-plugin-tailwindcss

This project needs your help

Support eslint-plugin-tailwindcss

I spent countless days working on this plugin, and it is available to all for free.
If you benefits from my work and if you want to help me keep the project alive, consider becoming a sponsor.

Premium sponsors Current sponsors
Sent.dm @codecov @getsentry

GitHub Sponsors | thanks.dev Sponsors

Rules

💞 Configurations enabled in.
⚠ïļ Configurations set to warn in.
✅ Set in the recommended configuration.
🔧 Automatically fixable by the --fix CLI option.
ðŸ’Ą Manually fixable by editor suggestions.

Name Description 💞 ⚠ïļ ðŸ”§ ðŸ’Ą
classnames-order Enforces a consistent order for the Tailwind CSS classnames, based on the compiler. ✅ 🔧
enforces-negative-arbitrary-values Warns about - prefixed classnames using arbitrary values. ✅ 🔧
enforces-shorthand Avoid using multiple Tailwind CSS classnames when not required. ✅ 🔧
no-arbitrary-value Forbid using arbitrary values in classnames.
no-contradicting-classname Avoid contradicting Tailwind CSS classnames. ✅ ðŸ’Ą
no-custom-classname Detects classnames which do not belong to Tailwind CSS. ✅ ðŸ’Ą
no-unnecessary-arbitrary-value Avoid unjustified arbitrary classnames. ✅ 🔧 ðŸ’Ą

Getting started

1. Install the plugin

npm i -D eslint-plugin-tailwindcss

  • As it is a dev dependency, it will not affected your final bundle size ðŸŠķ
  • It can be installed with other package managers such as pnpm
  • We support ESLint v10

If you are still using Tailwind CSS v3 and/or older ESLint versions, you can use eslint-plugin-tailwindcss@3.x.x

2. Edit your eslint.config file

Here is a very basic example:

// 1. import the plugin
import eslintPluginTailwindcss from "eslint-plugin-tailwindcss";
import { defineConfig } from "eslint/config";

export default defineConfig([
  {
    // 2. Optional: extend an existing config preset
    extends: [eslintPluginTailwindcss.configs.recommended],
    settings: {
      // 3. Define the tailwindcss settings with the MANDATORY cssConfigPath
      tailwindcss: {
        cssConfigPath: "./src/styles/tailwind.css",
      },
    },
    // 4. Optional: customize the rules to your needs
    rules: {
      "tailwindcss/classnames-order": "warn",
      "tailwindcss/no-arbitrary-value": "warn",
      "tailwindcss/no-custom-classname": [
        "warn",
        { whitelist: ["custom\\-*"] },
      ],
      "tailwindcss/no-contradicting-classname": "warn",
    },
  },
]);

cssConfigPath can be an absolute or a relative path. If you provide a relative path, the plugin will attempt to convert it into an absolute path.

3. Going further

You can also:

  • run the linting process inside a pipeline of your hosted repository.
    • I would recommend to run it on your merge requests
    • Not on every single (pre)commit
  • autoformat on save in your favorite IDE

Settings

Most of the rules solely use the shared settings (shared across all the plugin rules). Learn about eslint shared settings from the official documentation.

Here is a fully detailled example of shared settings:

// eslint.config.mjs
{
  settings: {
    tailwindcss: {
      // Attributes/props that could contain Tailwind CSS classes...
      // Optional, default values: ["class", "className", "ngClass", "@apply"]
      attributes: ["class"],
      // The (absolute or relative) path pointing to you main Tailwind CSS v4 config file.
      // It must be a `.css` file (v4), not a `.js` file (v3)
      // REQUIRED, default value may not help
      cssConfigPath: "./styles/tailwind.css",
      // Functions/tagFunctions that will be parsed by the plugin.
      // Optional, default values: ["classnames", "classNames", "clsx", "ctl", "cva", "tv", "tw", "twMerge", "twJoin"]
      functions: ["twClasses"],
      // Keys to be ignored in object expressions
      // Optional, default values: ["defaultVariants", "compoundVariants"]
      ignoredKeys: ["defaultVariants", "compoundVariants", "foo"],
      // Max size of the Set or Map objects used for caching
      // Optional, default value: 250_000
      cacheMaxSize: 150_000,
      // Max lifetime of the cache set in ms
      // Optional, default value: 10 * 60 * 1000 (10 minutes)
      cacheMaxAge: 60 * 1000,
    },
  }
}

The default settings are exported via the DEFAULT_SETTINGS.

Contributing

The project is open to all developers, you can contribute the eslint-plugin-tailwindcss.

However, make sure to discuss the issue or the feature you wish to implement prior to getting to work unless you feel adventurous.