vite-plugin-pretty-module-classnames
v1.3.1
Published
Adds the filename without the -module suffix to the class names of CSS modules.
Downloads
26,675
Maintainers
Readme
📦 Installation
npm install -D vite-plugin-pretty-module-classnames
🦾 Features
- Framework-agnostic
- Tested: without frameworks, React, Vue
- Potentially works with any framework. If you have any problems, write to the Issue
- Support CommonJS and ES Modules
- Support Vite 2.x
- Configurable: Added support for plugin configuration
🤔 Why Use This?
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!
⚙️ Usege
Api
// vite.config.js
import prettyModuleClassnames from "vite-plugin-pretty-module-classnames";
Example
// vite.config.js
export default defineConfig({
plugins: [prettyModuleClassnames()],
});
🔧 Configuration
lineNumber
Option
The 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.
Example
// 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:
- SomeComponent__wrapper_abcd1-1
- SomeComponent__container_abcd2-5
[!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.
🤝 Contributing
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! 🙏