For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/integrations/rslib.md.
close

Rslib

storybook-addon-rslib 从 rslib.config.* 加载单个 library 入口的 Rsbuild 配置,同时从 Storybook preview 构建中剥离仅用于产物输出的 library 字段。

示例:rslib-react-component、rslib-vue3-component 和 rslib-react-mf。

环境要求

依赖版本
Node.js>= 22.12.0
@rslib/core>= 0.22.0
@rsbuild/core^2.0.0
storybook^11.0.0

请同时安装配套的 Storybook Rsbuild framework 包,例如 storybook-react-rsbuild(React)或 storybook-vue3-rsbuild(Vue)。

安装与配置

安装 @rsbuild/core 和 storybook-addon-rslib:

npm
yarn
pnpm
bun
deno
npm install @rsbuild/core storybook-addon-rslib -D

在 .storybook/main.ts 中注册 storybook-addon-rslib。需要自定义选项时,请将配置嵌套在 options.rslib 下:

.storybook/main.ts
import type { 
import StorybookConfig
StorybookConfig
} from 'storybook-react-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: string
framework
: 'storybook-react-rsbuild',
stories: string[]
stories
: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: {
    name: string;
    options: {
        rslib: {
            configPath: string;
            libIndex: number;
        };
    };
}[]
addons
: [
{
name: string
name
: 'storybook-addon-rslib',
options: {
    rslib: {
        configPath: string;
        libIndex: number;
    };
}
options
: {
rslib: {
    configPath: string;
    libIndex: number;
}
rslib
: {
configPath: string
configPath
: './rslib.config.ts',
libIndex: number
libIndex
: 0,
}, }, }, ], } export default
const config: StorybookConfig
config

如果使用默认行为(libIndex: 0,并在 process.cwd() 中自动查找 rslib.config.*),直接传入字符串 'storybook-addon-rslib' 即可。

配置

在 options.rslib 下配置该 addon:

cwd

  • 类型: string
  • 默认值: undefined

传递给 @rslib/core loadConfig 的工作目录。

configPath

  • 类型: string
  • 默认值: undefined

传递给 @rslib/core loadConfig path 参数的配置文件路径。

libIndex

  • 类型: number | false
  • 默认值: 0

要合并到顶层配置中的 lib 数组索引。设为 false 时仅继承顶层配置。

当省略 lib 或 lib 不是数组时,索引 0 解析为隐式的 {}。显式配置空数组(lib: [])或 libIndex 越界时会抛出错误。转换为 Rsbuild 配置时,lib 和 environments 均会被移除。

modifyLibConfig

  • 类型: (config: LibConfig) => void
  • 默认值: undefined

实验性: 在转换为 Rsbuild 配置及剥离不兼容字段之前,就地修改选中的 LibConfig。

modifyLibRsbuildConfig

  • 类型: (config: RsbuildConfig) => void
  • 默认值: undefined

实验性: 在剥离不兼容字段之后、合并到 Storybook 之前,就地修改转换后的 Rsbuild 配置,可用于为 Storybook 显式恢复被剥离的字段。

继承的配置

转换后的配置在合并进 Storybook 之前,会经过继承的 Rsbuild 配置剥离边界。你也可以使用 rsbuildFinal 调整合并后的配置,或在 rslib.config.ts 中判断 process.env.STORYBOOK 来跳过仅用于 library 构建的 plugins。

在 Vue 3 中使用 unplugin-vue

unplugin-vue 目前不支持 Rspack 的 HMR。如果你的 rslib.config.ts 使用了 rsbuild-plugin-unplugin-vue(pluginUnpluginVue()),请在 rsbuildFinal 中移除 plugin-unplugin-vue 并添加 @rsbuild/plugin-vue:

.storybook/main.ts
import { 
import mergeRsbuildConfig
mergeRsbuildConfig
} from '@rsbuild/core'
import {
import pluginVue
pluginVue
} from '@rsbuild/plugin-vue'
import type {
import StorybookConfig
StorybookConfig
} from 'storybook-vue3-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: string
framework
: 'storybook-vue3-rsbuild',
stories: string[]
stories
: ['../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: string[]
addons
: ['storybook-addon-rslib'],
rsbuildFinal: (config: any) => any
rsbuildFinal
: (
config: any
config
) => {
const
const filteredPlugins: any
filteredPlugins
= (
config: any
config
.plugins ?? []).filter((
plugin: any
plugin
) => {
if (
plugin: any
plugin
&& 'name' in
plugin: any
plugin
) {
return
plugin: any
plugin
.name !== 'plugin-unplugin-vue'
} return true }) return
import mergeRsbuildConfig
mergeRsbuildConfig
(
{ ...
config: any
config
,
plugins: any
plugins
:
const filteredPlugins: any
filteredPlugins
},
{
plugins: any[]
plugins
: [
import pluginVue
pluginVue
()],
}, ) }, } export default
const config: StorybookConfig
config

Module Federation

storybook-addon-rslib 仅加载常规的 lib 入口(如 libIndex: 0)用于本地构建转换,本身不会提供 format: 'mf' 产物的服务。如需在 stories 中消费 Rslib Module Federation 入口,请与 Storybook 并行启动 rslib mf-dev,并注册 @module-federation/storybook-addon/preset 且配置 remotes。

请参阅 Rslib Module Federation 文档和 rslib-react-mf 示例。

.storybook/main.ts
import type { 
import StorybookConfig
StorybookConfig
} from 'storybook-react-rsbuild'
const
const config: StorybookConfig
config
:
import StorybookConfig
StorybookConfig
= {
framework: string
framework
: 'storybook-react-rsbuild',
stories: string[]
stories
: ['../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: (string | {
    name: string;
    options: {
        remotes: {
            'rslib-module': string;
        };
    };
})[]
addons
: [
'storybook-addon-rslib', {
name: string
name
: '@module-federation/storybook-addon/preset',
options: {
    remotes: {
        'rslib-module': string;
    };
}
options
: {
remotes: {
    'rslib-module': string;
}
remotes
: {
'rslib-module': 'rslib-module@http://localhost:3001/mf/mf-manifest.json', }, }, }, ], } export default
const config: StorybookConfig
config

请在 storybook dev 启动前或同时运行 rslib mf-dev,确保 http://localhost:3001/mf/mf-manifest.json 的远程 manifest 地址可访问。