CSSModules
For custom CSS Modules configuration.
The CSS Modules feature of Rsbuild is based on the modules
option of css-loader. You can refer to css-loader - modules to learn more.
The auto
configuration option allows CSS Modules to be automatically enabled based on their filenames.
true
Type description:
true
: enable CSS Modules for all files matching /\.module\.\w+$/i.test(filename)
regexp.false
: disable CSS Modules.RegExp
: enable CSS Modules for all files matching /RegExp/i.test(filename)
regexp.function
: enable CSS Modules for files based on the filename satisfying your filter function check.Style of exported class names.
'camelCase'
Type description:
asIs
: Class names will be exported as is.camelCase
: Class names will be camelized, the original class name will be exported.camelCaseOnly
: Class names will be camelized, the original class name will not be exported.dashes
: Only dashes in class names will be camelized, the original class name will be exported.dashesOnly
: Dashes in class names will be camelized, the original class name will not be exported.boolean
false
Allows exporting names from global class names, so you can use them via import.
Set the exportGlobals
to true
:
Use :global()
in CSS Modules:
Then you can import the class name wrapped with :global()
:
string
Sets the format of the class names generated by CSS Modules after compilation.
localIdentName
has different default values in development and production.
In a production, Rsbuild will generate shorter class names to reduce the bundle size.
You can use the following template strings in localIdentName
:
[name]
- the basename of the asset.[local]
- original class.[hash]
- the hash of the string.[folder]
- the folder relative path.[path]
- the relative path.[file]
- filename and path.[ext]
- extension with leading dot.[hash:<hashDigest>:<hashDigestLength>]
: hash with hash settings.Set localIdentName
to other value:
'local'
Controls the mode of compilation applied to the CSS Modules.
cssModules.mode
can take one of the following values:
'local'
(default): This enables the CSS Modules specification for scoping CSS locally. Class and ID selectors are rewritten to be module-scoped, and @value
bindings are injected.'global'
: This opts-out of the CSS Modules behavior, disabling both local scoping and injecting @value
bindings. Global selectors are preserved as-is.'pure'
: This enables dead-code elimination by removing any unused local classnames and values from the final CSS. It still performs local scoping and @value
injection.'icss'
: This compiles to the low-level Interoperable CSS format, which provides a syntax for declaring :import
and :export
dependencies between CSS and other languages. It does not perform any scoping or @value
injection.The 'local'
mode is the most common use case for CSS Modules, enabling modular and locally-scoped styles within components. The other modes may be used in specific scenarios.
For example:
You can also pass a function to modules.mode
that determines the mode based on the resource path, query, or fragment. This allows you to use different modes for different files.
For example, to use local scoping for .module.css
files and global styles for other files:
boolean
false
Whether to enable ES modules named export for class names.
namedExport: false
:namedExport: true
: