Included in the Awesome Vite.js list
npm install -D vite-plugin-pretty-module-classnames
- Framework-agnostic
- Tested:
VanillaJS
,React
,Vue
- Potentially works with any framework. If you have any problems, write to the Issue
- Tested:
- Support
CommonJS
andES Modules
- Support
Vite 2.x
- Configurable: Added support for plugin configuration
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()],
});
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.
// 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.
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! 🙏
MIT License © 2024 teplostanski donate.teplostanski.dev