CSS
Rspack has built-in support for CSS and provides several features to support CSS bundling.
Enabling CSS support
You can choose from the following options:
Built-in CSS support
Rspack supports the following four CSS Modules types which you can configure manually:
css/auto: Automatically determines whether a file is a normal CSS file or CSS Modules based on the file extension. Files ending with*.module.cssor*.modules.cssare treated as CSS Modules.css: Used to handle normal CSS files without CSS Modules features.css/global: Used to handle CSS Modules in global scope mode.css/module: Used to handle CSS Modules.
The difference between css/auto, css/global, and css/module is how CSS Modules scope affects the generated CSS:
css/autochooses the behavior by filename.style.cssis handled as plain CSS, whilestyle.module.cssandstyle.modules.cssare handled likecss/module.css/moduleenables CSS Modules with local scope by default. A selector like.buttonis renamed in the generated CSS. Use:global(.button)when you want a selector to stay global.css/globalenables CSS Modules with global scope by default. A selector like.buttonstays global. Use:local(.button)when you want a selector to be renamed in the generated CSS.
For example:
With type: 'css', the file is emitted as plain CSS without applying CSS Modules scope transforms.
With type: 'css/module', .button and .title are local selectors and are renamed in the generated CSS. Use :global(.button) if .button should stay global.
With type: 'css/global', .button stays global, while .title is renamed because it is wrapped in :local(...).
The generated CSS would look like this:
To enable CSS support, add CSS rules to your configuration:
You can also configure type: 'css/auto' to customize which files are treated as CSS files. For example, treat .less files as CSS files:
Using CssExtractRspackPlugin
Rspack supports using css-loader and CssExtractRspackPlugin to generate standalone CSS files.
If you are migrating a webpack project that uses mini-css-extract-plugin, it is recommended to replace it with CssExtractRspackPlugin. Their functionality and options are basically the same.
- Install css-loader:
- Add configuration:
Refer to the migration guide to learn how to migrate from webpack.
CssExtractRspackPlugin cannot be used with type: 'css', type: 'css/auto', type: 'css/global', or type: 'css/module' as these types are provided by native css support.
Using style-loader
Rspack supports using css-loader and style-loader to inject CSS via <style> tags. This method does not generate standalone CSS files but inline the CSS content into JS files.
- Install css-loader and style-loader:
- Add configuration:
style-loader cannot be used with type: 'css', type: 'css/auto', type: 'css/global', or type: 'css/module' as these types are provided by native css support.
CSS Modules
CSS Modules is a way of organizing CSS files that localizes the scope of CSS by automatically generating unique identifiers for class names. This allows you to use the same class name in different files without worrying about collisions.
Built-in support
If you enable Built-in CSS support, Rspack will treat files with a *.module.css extension as CSS Modules by default. You can import them into your JavaScript files, and then access each class defined in the CSS file as an export from the module.
You can use namespace import:
You can also use named import:
To enable default imports in Rspack, you need to set namedExports to false in your Rspack configuration file. This allows you, when using CSS Modules, to import the entire style module by default import, in addition to namespace imports and named imports:
Now you can use default import:
Rspack provides options to customize the parsing and generation of CSS Modules:
- module.parser.css: Configure the parsing of CSS Modules.
- module.generator["css/auto"]: Configure generation for
css/automodules. - module.generator["css/global"]: Configure generation for
css/globalmodules. - module.generator["css/module"]: Configure generation for
css/modulemodules.
Using css-loader
If you do not enable Rspack's built-in CSS support, you can use css-loader to provide CSS Modules support.
Enable the modules option of css-loader:
For more usage, please refer to css-loader - modules.
PostCSS
Rspack supports postcss-loader, which you can configure like this:
The above configuration will have all *.css files processed by postcss-loader. The output will be passed to Rspack for CSS post-processing.
Sass
Rspack supports sass-loader, which you can configure like this:
The above configuration runs all *.sass and *.scss files through the sass-loader and passes the resulting results to Rspack for CSS post-processing.
Less
Rspack supports less-loader, which you can configure like this:
The above configuration runs all *.less files through the less-loader and passes the generated results to Rspack for CSS post-processing.
Tailwind CSS
Tailwind CSS is a CSS framework and design system based on utility class, which can quickly add common styles to components, and support flexible extension of theme styles.
Tailwind CSS documentation provides integration guides for Rspack, please refer to:

