Rsbuild provides out-of-the-box support for CSS, including PostCSS, CSS Modules, CSS preprocessors, CSS inlining, and CSS compression.
Rsbuild also provides several configurations that allow customization of the processing rules for CSS files.
Lightning CSS is a high performance CSS parser, transformer and minifier written in Rust. It supports parsing and transforming many modern CSS features into syntax supported by target browsers, and also provides a better compression ratio.
Rsbuild uses Rspack's built-in lightningcss-loader to transform CSS code. It automatically reads the project's browserslist config and converts the CSS code to syntax supported by target browsers.
-webkit-
, -moz-
, -ms-
, etc., so you don't need to manually add prefixes or use the autoprefixer plugin.lightningcss-loader
.If Lightning CSS does not meet your needs, you can disable Lightning CSS and use PostCSS to transform your CSS code.
Steps:
false
to disable the Lightning CSS loader.When building for production, Rsbuild enables Rspack's built-in LightningCssMinimizerRspackPlugin plugin to minify CSS assets for better transmission efficiency.
LightningCssMinimizerRspackPlugin
.Rsbuild supports transforming CSS code through PostCSS. You can configure PostCSS in the following ways:
postcss.config.js
:postcss.config.js
file and the tools.postcss
option, both will take effect, and the tools.postcss
option will take precedence.postcss.config.js
file in the project and the tools.postcss
option is not configured, Rsbuild will not register postcss-loader
.Rsbuild supports CSS Modules by default, please read the Using CSS Modules chapter for the complete usage of CSS Modules.
Rsbuild supports popular CSS preprocessors through plugins, including Sass, Less and Stylus. See how to use them:
Please read the React - CSS-in-JS section to learn how to use common CSS-in-JS libraries in Rsbuild.
By default, Rsbuild will extract CSS into a separate .css
file and output it to the dist directory.
If you want to inline styles into your JS file, you can set output.injectStyles to true to disable CSS extraction logic. When the JS file is requested by the browser, JS dynamically inserts the <style>
tag into the Html to load the CSS styles.
This will increase the size of your JS Bundle, so it is usually not recommended to disable the CSS extraction.
Rsbuild supports importing CSS files in node_modules
.
In Sass and Less files, it is also allowed to add the ~
prefix to resolve style files in node_modules
. However, this is a deprecated feature and it is recommended to remove the ~
prefix from the code.