CSS is a first-class citizen in Rspack, and Rspack has several built-in features to support CSS bundling.
You can choose from the following options:
Rspack provides the experiment.css option, an experimental feature introduced in webpack 5 to enable native CSS support. Rspack has improved this feature and plans to enable it by default in Rspack 2.0.
If you create a new project based on Rspack, it is recommended to use this method.
After enabling experiment.css
, Rspack will support the following three module types, which you can set on the rule
using type
:
css
: Used to handle normal CSS files.css/module
: Used to handle CSS Modules.css/auto
: Automatically determines whether a file is a normal CSS file or CSS Modules based on the file extension. Files ending with *.module.css
are treated as CSS Modules.For files ending in *.css
, Rspack will treat them as type: 'css/auto'
by default. You can also configure type: 'css/auto'
to customize which files are treated as CSS files. For example, treat .less
files as CSS files:
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.
Refer to the migration guide to learn how to migrate from webpack.
CssExtractRspackPlugin cannot be used with type: 'css'
, type: 'css/auto'
, or type: 'css/module'
as these types are provided by experiments.css
.
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.
style-loader cannot be used with type: 'css'
, type: 'css/auto'
, or type: 'css/module'
as these types are provided by experiments.css
.
By default, Rspack treats files with a *.module.css
extension as CSS Modules. 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.config.js
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:
For more on CSS Modules configuration, please refer to module.parser.css.
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.
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.
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 is a utility-first CSS framework packed with classes that can be composed to build any design, directly in your markup.
Installing Tailwind CSS as a PostCSS plugin is the most seamless way to integrate it with Rspack.
Please install tailwindcss,autoprefixer,postcss and postcss-loader in your project.
Once installed, you need to configure postcss-loader
in rspack.config.js
to handle CSS files, and then add tailwindcss
to postcssOptions.plugins
.
Here is an example configuration for handling .css
files, if you need to handle .scss
or .less
files, you can refer to this example for modifications.
At this point, you have completed the build configuration required to use Tailwind CSS in Rspack.
Next you can follow the steps in the Tailwind CSS Documentation to add the required configuration and code for Tailwind CSS and start using it.