close
logologo
指南
配置
插件
API
社区
版本
更新日志
Rsbuild 0.x 文档
English
简体中文
指南
配置
插件
API
社区
更新日志
Rsbuild 0.x 文档
English
简体中文
logologo

开始

介绍
快速上手
功能导航
名词解释

框架

React
Vue
Preact
Svelte
Solid

基础

命令行工具
开发服务器
构建产物
静态资源
HTML
JSON
Wasm
TypeScript
Web Workers
部署静态站点
升级 Rsbuild

配置

配置 Rspack
配置 Rsbuild
配置 SWC

样式

CSS
CSS Modules
CSS-in-JS
Tailwind CSS v4
Tailwind CSS v3
UnoCSS

进阶

路径别名
环境变量
模块热更新
浏览器范围
浏览器兼容性
模块联邦
多环境构建
服务端渲染(SSR)
测试

优化

代码拆分
产物体积优化
提升构建性能
静态资源内联

迁移

从 Rsbuild 0.x 迁移
webpack
Create React App
Vue CLI
Vite
Vite 插件
Modern.js Builder

调试

开启调试模式
构建性能分析
使用 Rsdoctor

常见问题

通用类问题
功能类问题
异常类问题
热更新问题
📝 在 GitHub 上编辑此页
上一页CSS Modules
下一页Tailwind CSS v4

#CSS-in-JS

本文档列出了在 Rsbuild 中使用常见 CSS-in-JS 库的方法。

虽然示例基于 React,但部分 CSS-in-JS 库(如 vanilla-extract)也适用于其他框架。

#Emotion

Rsbuild 支持编译 Emotion,你可以添加以下配置来使用:

  • swcReactOptions.importSource
  • @swc/plugin-emotion
rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
  plugins: [
    pluginReact({
      swcReactOptions: {
        importSource: '@emotion/react',
      },
    }),
  ],
  tools: {
    swc: {
      jsc: {
        experimental: {
          plugins: [['@swc/plugin-emotion', {}]],
        },
      },
    },
  },
});

参考示例:emotion。

#styled-jsx

你可以通过 @swc/plugin-styled-jsx 来使用 styled-jsx:

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
  plugins: [pluginReact()],
  tools: {
    swc: {
      jsc: {
        experimental: {
          plugins: [['@swc/plugin-styled-jsx', {}]],
        },
      },
    },
  },
});

请注意,你需要选择和当前 @swc/core 版本匹配的 SWC 插件,才能使 SWC 正常执行,详见 tools.swc。

参考示例:styled-jsx。

#vanilla-extract

Rsbuild 支持使用 @vanilla-extract/webpack-plugin 插件,你可以添加以下配置来使用 vanilla-extract:

目前,Rspack 在同时使用 splitChunks 和 @vanilla-extract/webpack-plugin 时存在一个 HMR 问题。在开发模式下,你可以使用特殊的 splitChunks 配置来规避这个问题。

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import { VanillaExtractPlugin } from '@vanilla-extract/webpack-plugin';

export default defineConfig({
  plugins: [
    pluginReact({
      reactRefreshOptions: {
        exclude: [/\.css\.ts$/],
      },
    }),
  ],
  performance: {
    chunkSplit: {
      override: {
        cacheGroups: {
          vanilla: {
            test: /@vanilla-extract\/webpack-plugin/,
            // 确保在开发模式下 @vanilla-extract/webpack-plugin 产生虚拟模块所在的 Chunk id 稳定以避免 HMR 问题
            name: process.env.NODE_ENV === 'development' && 'vanilla',
            chunks: 'all',
          },
        },
      },
    },
  },
  tools: {
    rspack: {
      plugins: [new VanillaExtractPlugin()],
    },
  },
});

参考示例:vanilla-extract。

#静态资源

当导入静态资源时,使用 import 语法:

src/App.css.ts
import { style } from '@vanilla-extract/css';
import logoUrl from './logo.png';

export const containerStyle = style({
  backgroundImage: `url(${logoUrl})`,
});

由于 logoUrl 已经是一个指向 dist 目录的解析路径,css-loader 不需要再次处理它。通过 tools.cssLoader.url 禁用 CSS URL 处理,以避免遇到模块解析错误:

rsbuild.config.ts
export default defineConfig({
  // ... 其他配置
  tools: {
    cssLoader: {
      url: false,
    },
  },
});

参考:#6215。

#StyleX

你可以通过 unplugin-stylex 来使用 StyleX:

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import stylexPlugin from 'unplugin-stylex/rspack';

export default defineConfig({
  plugins: [pluginReact()],
  tools: {
    rspack: {
      plugins: [stylexPlugin()],
    },
  },
});

参考示例:stylex。

#styled-components

styled-components 是一个运行时库,默认情况下你可以直接使用它,无需任何额外配置。

Rsbuild 支持编译 styled-components,用于优化调试体验,并对 styled-components 添加服务器端渲染支持。

如果你需要使用 styled-components,推荐使用 @rsbuild/plugin-styled-components。

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';
import { pluginStyledComponents } from '@rsbuild/plugin-styled-components';

export default defineConfig({
  plugins: [pluginStyledComponents()],
});

参考示例:styled-components。

TIP

styled-components 不再推荐用于新项目,因为它已经处于 维护模式。