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.
Modern.js implements storybook-addon-modernjs based on this tool. This plugin loads the Modern.js configuration file and converts it into a configuration usable by Storybook Rsbuild.
This document only provides the simplest usage. For more information, please refer to Storybook Rsbuild Modern.js Integration.
Installation
You need to install @rsbuild/core in your project, otherwise the plugin may not work properly.
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.