CSS Modules allows you to write CSS in a modular way, and import these styles in JavaScript files. CSS Modules automatically generates unique class names, isolating styles between modules and avoiding class name conflicts.
Rsbuild supports CSS Modules by default without additional configuration. Our convention is to use the [name].module.css
filename to enable CSS Modules.
The following style files are considered CSS Modules:
*.module.css
*.module.less
*.module.sass
*.module.scss
*.module.styl
*.module.stylus
Write styles in a *.module.css
file:
Import styles in a JavaScript file:
After compilation, CSS Modules class names are automatically appended with a hash value to prevent class name conflicts:
See Custom Class Names to modify the class name generation rules.
If you prefer to use named imports in CSS Modules, you can enable it through the output.cssModules.namedExport config.
If enabled, you can reference class names using named imports:
By default, only files ending with *.module.css
are recognized as CSS Modules.
If you want to treat other CSS files as CSS Modules, you can configure output.cssModules.auto.
For example:
After this configuration, the following two files will be recognized as CSS Modules:
Customizing class names generated by CSS Modules is a commonly used feature. You can configure this using output.cssModules.localIdentName.
If you need to customize other configs of CSS Modules, you can set them via output.cssModules.
In some cases, you may need to use global styles in CSS Modules, such as overriding the styles of third-party libraries or setting global styles for specific elements.
CSS Modules provides the :global()
pseudo-class selector for this purpose. Selectors inside :global()
retain their original class names, allowing them to correctly match global elements.
You can also nest :global()
:
When you import CSS Modules in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, you need to add a type declaration file for CSS Modules. Create a src/env.d.ts
file and add the corresponding type declaration.
@rsbuild/core
package is installed, you can reference the preset types provided by @rsbuild/core
:After adding the type declaration, if the type error persists, try restarting your IDE or adjusting the directory where env.d.ts
is located to ensure TypeScript can correctly identify the type definition.
The above method provides types for CSS Modules, but cannot accurately indicate which class names are exported by a specific CSS file.
Rsbuild supports generating accurate type declarations for CSS Modules. Register the @rsbuild/plugin-typed-css-modules plugin and run the build to generate type declaration files for all CSS Modules.
For example, create two files named src/index.ts
and src/index.module.css
:
After building, Rsbuild will generate a src/index.module.css.d.ts
type declaration file:
Now when you open the src/index.ts
file, you'll see that the styles
object has accurate types.