Using Storybook
Storybook is a tool specifically designed for component debugging. It provides:
- A rich variety of debugging capabilities
- Integration with some testing tools
- Reusable documentation content
- Sharing capabilities
- Workflow automation
Principle
Rsbuild provides Storybook Rsbuild, which allows any Rsbuild project to use this tool for building Storybook.
In Modern.js v3 projects, @modern-js/storybook-addon uses the application config resolver and merges the shared and client configurations into the Storybook preview. Storybook Rsbuild manages the preview entry, output paths and HMR client.
Installation
Use the same @rsbuild/core version as your project’s @modern-js/app-tools. Projects on older Modern.js versions can continue using storybook-addon-modernjs.
Configure .storybook/main.ts
BFF Integrated Calls in Storybook
When you want to call BFF APIs directly in Storybook components (for example /api/**), start the BFF service and configure a proxy in Storybook's dev server to avoid CORS issues.
1. Add scripts
Add the following scripts to package.json to start the BFF server and Storybook separately. The BFF_PROXY environment variable ensures the proxy is only enabled during Storybook build:
2. Configure proxy
Add the following config in modern.config.ts to proxy requests to the BFF service only during Storybook build:
Proxy path and port follow this logic:
- If
dev.server.proxyis configured, use that configuration - If not configured, the proxy path falls back to
bff.prefix, otherwise/api - If not configured, the target port falls back to
server.port, otherwise8080 - The final target is
http://localhost:<port>
3. Start
Now you can request the path configured in bff.prefix; if not configured, use /api/**.
Example
You can check out the example to learn how to use Storybook in Modern.js.