bmatcuk / eslint-plugin-postcss-modules

Checks that you are using the classes exported by your css modules using postcss.
MIT License
21 stars 7 forks source link

Release Build Status codecov.io

eslint-plugin-postcss-modules

Like eslint-plugin-css-modules, this plugin helps you lint your css modules. It adds a new eslint rule that detects if you are trying to use a class that is not exported by your css module.

The major difference between this plugin and the aforementioned plugin is that this plugin uses postcss to parse the css files and determine what classes are exported. There are a couple of benefits to this:

  1. Under the hood, css-loader is also using postcss with a few plugins to determine which classes are exported. This plugin uses the same group of plugins to guarantee that the classes that are linted are the same as the classes css-loader exports.
  2. Through plugins, postcss can handle a variety of complex input css files. While eslint-plugin-css-modules has excellent support for most major css grammars (such as sass), it does not support all of the inputs that postcss can handle and probably never will. If you are using postcss for your project, chances are good that eslint-plugin-css-modules will choke on your files.

The downside is that, while postcss is very battle-tested and fast for building css, it may be slower than eslint-plugin-css-modules for linting purposes. I don't have benchmarks, but welcome them.

Installation

yarn add -D eslint-plugin-postcss-modules
npm install -D eslint-plugin-postcss-modules

Usage

In your eslint config:

{
  "extends": [
    "plugin:postcss-modules/recommended"
  ],
}

The recommended configuration will set no-undef-class to errors and no-unused-class to warnings. The recommended configuration is equivalent to:

{
  "plugins": ["postcss-modules"],
  "rules": {
    "postcss-modules/no-undef-class": "error",
    "postcss-modules/no-unused-class": "warn"
  }
}

Settings

There are a couple settings you can tweak in your eslint config. Below are examples of the options and their default values:

{
  "settings": {
    "postcss-modules": {
      "postcssConfigDir": "cwd",
      "baseDir": "cwd",
      "camelCase": false,
      "defaultScope": "local",
      "include": "**/*.css",
      "exclude": "**/node_modules/**/*"
    }
  }
}