💅 Adds the filename without the -module suffix to the class names of CSS modules
MIT License
npm install -D vite-plugin-pretty-module-classnames
VanillaJS
, React
, Vue
CommonJS
and ES Modules
Vite 2.x
As utilizing CSS modules in React, we're accustomed to seeing class names formatted as SomeComponent__classname_hash
. However, with Vite, the naming convention for modular class names appears slightly different, resembling __classname_hash
or SomeComponent-module__classname_hash
the latter occurring if generateScopedName: '[name]__[local]_[hash:base64:5]'
is specified in vite.config.js. This addition of -module
post-component name can be cumbersome to work around.
Fortunately, the vite-plugin-pretty-module-classnames
provides a solution to this issue!
// vite.config.js
import prettyModuleClassnames from "vite-plugin-pretty-module-classnames";
// vite.config.js
export default defineConfig({
plugins: [prettyModuleClassnames()],
});
lineNumber
OptionThe lineNumber
option is a boolean that, when set to true
, appends the line number where the CSS class is defined in the source file to the generated class name.
// vite.config.js
import prettyModuleClassnames from "vite-plugin-pretty-module-classnames";
export default defineConfig({
plugins: [prettyModuleClassnames({ lineNumber: true })],
});
With this configuration, if your CSS file contains:
/* SomeComponent.module.css */
1 .wrapper {
2 /* styles */
3 }
4
5 .container {
6 /* styles */
7 }
The generated class names will look like:
[!WARNING] Please note that the lineNumber option may not work correctly with preprocessors like Sass, Less, or Stylus. The line number is calculated based on the compiled CSS, where empty lines between selectors and comments are typically removed. This can lead to discrepancies between the line numbers in the source files and the compiled output, potentially resulting in inaccurate line numbers in the generated class names.
Additionally, in Vue files, the line count always starts from the
<style module>
tag, regardless of where it is placed within the file. This means that the line numbers in generated class names will be relative to the position of the<style module>
tag, not the beginning of the file.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Before you contribute to the development of the project, read the rules.
Thanks again for your support, it is much appreciated! 🙏