To enable CSS Modules language features in your editor, you need to install @css-modules-kit/ts-plugin (ts-plugin). The installation method varies by editor.
- For VS Code:
- Install the CSS Modules Kit extension
- For Neovim:
- Install
@css-modules-kit/ts-pluginand set up the configuration
- Install
- For Emacs:
- Not yet supported
- For Zed:
- For WebStorm:
- Not yet supported
- For StackBlitz Codeflow:
- Install the CSS Modules Kit extension
The ts-plugin provides type definitions for styles as { foo: string, bar: string }. However, these types are not applied during type-checking with the tsc command.
To ensure tsc properly type-checks, you need to generate *.module.css.d.ts files. This is handled by codegen.
To install codegen, run the following command:
npm i -D @css-modules-kit/codegenConfigure npm-script to run cmk command before building and type checking. This command generates *.module.css.d.ts files in generated directory.
{
"scripts": {
"gen": "cmk",
"build": "run-s -c gen build:*",
"build:vite": "vite build",
"lint": "run-s -c gen lint:*",
"lint:eslint": "eslint .",
"lint:tsc": "tsc --noEmit",
"lint:prettier": "prettier --check ."
}
}Finally, you need to configure your tsconfig.json so that css-modules-kit works correctly.
- Set
cmkOptions.enabledtotrueto enable css-modules-kit. - Omit the
includeoptions or ensure that*.module.cssfiles are included when specifying them explicitly.- ✅ Good cases:
- Omit
include(equivalent to["**/*"]) - Use patterns like
["src"],["src/**/*"]
- Omit
- ❌ Bad cases:
["src/**/*.ts"]["src/index.ts"](excludes*.module.cssfiles)
- ✅ Good cases:
- Set the
rootDirsoption to include both the directory containingtsconfig.jsonand thegenerateddirectory.- Example:
[".", "generated"]
- Example:
Below is an example configuration:
We provide linter plugin for CSS Modules. Currently, we support the following linters:
All linter plugins offer the same set of rules. So please choose and install one.
You can customize the behavior of codegen by adding the cmkOptions option to your tsconfig.json. For more details, please refer to Configuration.
{ "compilerOptions": { "rootDirs": [".", "generated"], // ... }, "cmkOptions": { "enabled": true, }, }