The classname utility is used to isolate CSS code by applying a naming convention of prefix-moduleName-className. It is designed to support both standard CSS and CSS Modules by automatically prefixing class names and mapping them to hashed identifiers when CSS Modules are enabled.
Naming Convention
By default, the utility follows the pattern: prefix-moduleName-className.
Key Behaviors
- Prefixing: Every class name passed to the generated function is prefixed with the configured
prefix and the provided module name. - The Underscore Placeholder: Passing an underscore
'_' as an argument acts as a placeholder for the base module class (e.g., prefix-moduleName). - CSS Modules Support: If
config.cssModule is enabled, the utility looks up the generated class name within the provided style object (the object returned by css-loader) to retrieve the actual hashed class name used in the DOM.
import genaration from 'src/utils/classname'
const alertClass = genaration(require('src/styles/alert.less'), 'alert', 'shineout')
// Example usage:
const classes = alertClass('_', 'success', 'show')
// Result (Standard CSS): 'shineout-alert shineout-alert-success shineout-alert-show'
// Result (CSS Modules): 'shineout-alert-xxxxx shineout-alert-success-xxxxx shineout-alert-show-xxxxx'