Make your scoped CSS module class names clear and readable — this plugin automatically adds the module filename and other useful info to class names for easier development.
npm install vite-plugin-readable-classnames
docs
·
github
·
codeberg
·
npm
·
yarn
·
fund this package
English • Russian
Make your scoped CSS module class names clear and readable — this plugin automatically adds the module filename and other useful info to class names for easier development.
Included in the Awesome Vite.js list
- Installation
- Features
- Problem
- Documentation
- Usage
- lineNumber option
- separator option
- Migration from vite-plugin-pretty-module-classnames
- Contributing
- License
``bash`
npm install -D vite-plugin-readable-classnamesor
yarn add -D vite-plugin-readable-classnamesor
pnpm add -D vite-plugin-readable-classnamesor
bun add -d vite-plugin-readable-classnames
- Framework-agnostic:
- Tested on projects with !VanillaJS !React !Vue !Astro
- Potentially works with any framework. If you have any problems, write to the Issue
- Universal:
- Compatible with both CommonJS and ES ModulesVite 2.x
- Support
- Customizable: Flexible configuration through options object
In React with CSS modules, we're used to class names like SomeComponent__classname_hash. By default, Vite generates names in the format __classname_hash, omitting the component name, which makes debugging more difficult.
If you try to add the component name through the generateScopedName: '[name]__[local]_[hash:base64:5]' configuration, React will add an extra -module suffix, and in Vue, such configuration may lead to build errors.
This plugin solves these problems and ensures predictable, readable class naming in the format ComponentName__classname_hash regardless of the framework.
> [!TIP]
> What are generated class names and where are they visible?
>
> When you use CSS modules, the resulting class names are automatically generated by the bundler (e.g., Vite or Webpack). These names are visible:
>
> - in the HTML code of the page (through browser DevTools);
> - in the final CSS file;
> - when debugging in the browser as you search for the right element or style.
>
> Readable class names that include the component name help you quickly understand where a style comes from, making debugging and project maintenance easier.
For full documentation, visit vite-plugin-readable-classnames.js.org.
`js
import readableClassnames from 'vite-plugin-readable-classnames'
export default defineConfig({
plugins: [
readableClassnames()
]
})
`
The lineNumber option adds the line number where the class is declared in the source file to the class name.
`js
import readableClassnames from 'vite-plugin-readable-classnames'
export default defineConfig({
plugins: [readableClassnames({ lineNumber: true })],
})
`
If your CSS file looks like this:
`css [SomeComponent.module.css]`
1 .wrapper {
2 / styles /
3 }
4
5 .container {
6 / styles /
7 }
The resulting class names will be:
- SomeComponent__wrapper_abcd1-1SomeComponent__container_abcd2-5
-
> [!IMPORTANT] Please note:
> The lineNumber option works the same way as Vite's standard class name generation when using preprocessors (Sass, Less, Stylus). The line number is taken from the generated CSS, where empty lines and comments are usually removed. Therefore, the line numbers in class names may not match the line numbers in the source files..vue
>
> In files, line counting always starts from the