Rsbuild provides out-of-the-box support for CSS, including PostCSS, CSS Modules, CSS preprocessors, CSS inlining, and CSS compression.
In addition, Rsbuild also provides multiple configs to customize the compile rules of style resources.
Rsbuild has built-in PostCSS to transform the CSS code. You can configure PostCSS in the following ways:
postcss.config.js
:When you configure both the postcss.config.js
file and the tools.postcss
option, both will take effect, and the tools.postcss
option will take precedence.
Rsbuild has built-in autoprefixer plugin, which is used to add vendor prefixes to CSS rules. If you need to configure the target browser, you can use browserslist.
If the autoprefixer plugin is already registered in your project, Rsbuild will not register the autoprefixer plugin again.
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.
During the production build, Rsbuild compresses static assets such as CSS and JS to provide better transmission efficiency.
Rsbuild by default uses the built-in SwcCssMinimizerRspackPlugin
plugin from Rspack to automatically compress CSS code during production builds.
You can customize the CSS minimizer by using the CSS Minimizer plugin to switch to cssnano or other tools for CSS compression.
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.
You can directly import CSS files in node_modules.