|
2 | 2 | sidebar_position: 15
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# Using Storybook |
6 |
| - |
7 |
| -[Storybook](https://storybook.js.org/) is a tool dedicated to component debugging, providing around component development. |
8 |
| - |
9 |
| -- Develop UIs that are more durable |
10 |
| -- Test UIs with less effort and no flakes |
11 |
| -- Document UI for your team to reuse |
12 |
| -- Share how the UI actually works |
13 |
| -- Automate UI workflows |
14 |
| - |
15 |
| -:::tip |
16 |
| -This tutorial is suitable for Storybook V7 users. If you are using an old version of Storybook V6 (using the @modern-js/plugin-storybook plugin), you can refer to the [Migration Guide](#migration-guide) to upgrade. |
17 |
| -::: |
18 |
| - |
19 |
| -## Quick Start |
20 |
| - |
21 |
| -Modern.js integrates Storybook debugging capabilities by default. You can directly enable the Storybook feature by using the following command: |
22 |
| - |
23 |
| -```bash |
24 |
| -$ npx modern new |
25 |
| -? Please select the operation you want: Enable features |
26 |
| -? Please select the feature name: Enable「Storybook」V7 |
27 |
| -``` |
28 |
| -
|
29 |
| -This command will create a template for Storybook, including: |
30 |
| -
|
31 |
| -- Creating a configuration folder .storybook and a default configuration file .storybook/main.ts. |
32 |
| -- Creating example story components. |
33 |
| -- Updating package.json to add dependencies @storybook/addon-essentials and @modern-js/storybook, as well as creating Storybook-related scripts. |
34 |
| -
|
35 |
| -To start, run `npm run storybook`. |
36 |
| -
|
37 |
| -## Enable Rspack build |
38 |
| -
|
39 |
| -Rspack is known for its fast build speed. To use Rspack as a build tool in Modern.js, you only need to configure it as follows: |
40 |
| -
|
41 |
| -```diff filename='.storybook/main.js' |
42 |
| -const config = { |
43 |
| - framework: { |
44 |
| - name: '@modern-js/storybook', |
45 |
| - options: { |
46 |
| -- bundler: 'webpack' |
47 |
| -+ bundler: 'rspack' |
48 |
| - }, |
49 |
| - }, |
50 |
| - typescript: { |
51 |
| -- reactDocgen: 'react-docgen-typescript' |
52 |
| -+ reactDocgen: 'react-docgen' |
53 |
| - } |
54 |
| -}; |
55 |
| -
|
56 |
| -export default config; |
57 |
| -``` |
58 |
| -
|
59 |
| -Note that in the above configuration, the reactDocgen configuration has been changed because Rspack currently does not support @storybook/react-docgen-typescript-plugin. |
60 |
| -
|
61 |
| -## Configurations |
62 |
| -
|
63 |
| -There are some configurations in `.storybook/main.js`. |
64 |
| -
|
65 |
| -### configPath |
66 |
| -
|
67 |
| -- **Type**: `string` |
68 |
| -- **Default**: `modern.config.(j|t)s` |
69 |
| -
|
70 |
| -Specify the path of Modern.js configuration. |
71 |
| -
|
72 |
| -Example: |
73 |
| -
|
74 |
| -```javascript filename='.storybook/main.js' |
75 |
| -const config = { |
76 |
| - framework: { |
77 |
| - name: '@modern-js/storybook', |
78 |
| - options: { |
79 |
| - configPath: 'modern.storybook.config.ts', |
80 |
| - }, |
81 |
| - }, |
82 |
| -}; |
83 |
| -
|
84 |
| -export default config; |
85 |
| -``` |
86 |
| -
|
87 |
| -### bundler |
88 |
| -
|
89 |
| -- **Type**: `'webpack' | 'rspack'` |
90 |
| -- **Default**: `webpack` |
91 |
| -
|
92 |
| -Specify the underlying build tool to use either Webpack or Rspack. |
93 |
| -
|
94 |
| -Example: |
95 |
| -
|
96 |
| -```javascript filename='.storybook/main.js' |
97 |
| -const config = { |
98 |
| - framework: { |
99 |
| - name: '@modern-js/storybook', |
100 |
| - options: { |
101 |
| - bundler: 'rspack', |
102 |
| - }, |
103 |
| - }, |
104 |
| -}; |
105 |
| -
|
106 |
| -export default config; |
107 |
| -``` |
108 |
| -
|
109 |
| -### builderConfig |
| 5 | +import { PackageManagerTabs } from '@theme' |
110 | 6 |
|
111 |
| -- **Type**: `BuilderConfig` |
112 |
| -- **Default**: `undefined` |
113 |
| -
|
114 |
| -To modify the configuration of build, which has a higher priority than the configuration file, you can specify the build configuration directly here if you do not want to use the configuration file. |
115 |
| -
|
116 |
| -Example: |
117 |
| -
|
118 |
| -```javascript filename='.storybook/main.js' |
119 |
| -const config = { |
120 |
| - framework: { |
121 |
| - name: '@modern-js/storybook', |
122 |
| - options: { |
123 |
| - builderConfig: { |
124 |
| - alias: { |
125 |
| - react: require.resolve('react'), |
126 |
| - 'react-dom': require.resolve('react-dom'), |
127 |
| - }, |
128 |
| - }, |
129 |
| - }, |
130 |
| - }, |
131 |
| -}; |
132 |
| -
|
133 |
| -export default config; |
134 |
| -``` |
135 |
| -
|
136 |
| -## Command Line Interface |
137 |
| -
|
138 |
| -@modern-js/storybook proxies some of the storybook cli commands. |
139 |
| -
|
140 |
| -### storybook dev |
141 |
| -
|
142 |
| -Start Storybook, more details at [storybook#dev](https://storybook.js.org/docs/react/api/cli-options#dev). |
143 |
| -
|
144 |
| -### storybook build |
145 |
| -
|
146 |
| -Build Storybook for production, more details at [storybook#build](https://storybook.js.org/docs/react/api/cli-options#build). |
147 |
| -
|
148 |
| -## Storybook addon compatibility |
149 |
| -
|
150 |
| -Due to the current version of Storybook in the document being version 7, please select the addon for Storybook V7. |
151 |
| -
|
152 |
| -When an addon does not require additional Babel or Webpack configurations, it can be used directly, such as @storybook/addon-essentials. |
153 |
| -
|
154 |
| -For some addons that require dependencies on Babel plugins and Webpack configurations, such as @storybook/addon-coverage, only @modern-js/builder-webpack-provider supports them. |
155 |
| -
|
156 |
| -## Benefits |
157 |
| -
|
158 |
| -Using @modern-js/storybook can bring you lightning-fast builds with Rspack, without the need for tedious configuration. It comes with many best practices for web development out-of-the-box, such as code splitting strategies, built-in support for CSS modules and postcss, TypeScript support, and commonly used Babel plugins. |
159 |
| -
|
160 |
| -The powerful build capabilities of Modern.js can be directly used in Storybook projects. |
161 |
| -
|
162 |
| -## Trouble Shooting |
163 |
| -
|
164 |
| -1. Modern.js won't load your other configurations like `babel.config.json`, babel config needs to be set in Modern.js config, [tools.babel](/configure/app/tools/babel). |
165 |
| - Webpack configuration can be written in either [tools.webpack](/configure/app/tools/webpack) or [tools.webpackChain](/configure/app/tools/webpack-chain). |
166 |
| -
|
167 |
| -2. If you find that the build performance is not good, please check if the Storybook automatic documentation generation feature is enabled. For optimal performance, configure it to use `react-docgen`. The difference between `react-docgen` and `react-docgen-typescript` is that the former is implemented based on Babel, while the latter is implemented based on TypeScript. The former has better performance but weaker type inference capabilities. If you are using Rspack for the build, only `react-docgen` is supported. |
168 |
| -
|
169 |
| -```javascript filename='.storybook/main.js' |
170 |
| -const config = { |
171 |
| - typescript: { |
172 |
| - reactDocgen: 'react-docgen', |
173 |
| - }, |
174 |
| -}; |
175 |
| -
|
176 |
| -export default config; |
177 |
| -``` |
178 |
| -
|
179 |
| -## Migration Guide |
180 |
| -
|
181 |
| -### Migrate from @modern-js/plugin-storybook |
182 |
| -
|
183 |
| -If you are a user migrating from V6 to V7, you can use V7 through [the above method](#quick-start) and make the following adjustments: |
184 |
| -
|
185 |
| -##### Modify configuration file |
186 |
| -
|
187 |
| -If you have made some custom configurations to Storybook in the older version, you need to move the configuration file `root/config/storybook/main.(j|t)s` to `root/.storybook/main.(j|t)s`. |
188 |
| -
|
189 |
| -And then add the following lines in `root/.storybook/main.(j|t)s`, specify framework as `@modern-js/storybook`. |
| 7 | +# Using Storybook |
190 | 8 |
|
191 |
| -```diff |
192 |
| -const config = { |
193 |
| -+ framework: { |
194 |
| -+ name: '@modern-js/storybook' |
195 |
| -+ } |
196 |
| -}; |
| 9 | +[Storybook](https://storybook.js.org/) is a tool specifically designed for component debugging. It provides: |
197 | 10 |
|
198 |
| -export default config; |
199 |
| -``` |
| 11 | +- A rich variety of debugging capabilities |
| 12 | +- Integration with some testing tools |
| 13 | +- Reusable documentation content |
| 14 | +- Sharing capabilities |
| 15 | +- Workflow automation |
200 | 16 |
|
201 |
| -##### Update Dependencies |
| 17 | +## Principle |
202 | 18 |
|
203 |
| -Update dependencies like @storybook/addon-\* to major version 7. |
| 19 | +Rsbuild provides [Storybook Rsbuild](https://storybook.rsbuild.rs/index.html), which allows any Rsbuild project to use this tool for building Storybook. |
204 | 20 |
|
205 |
| -##### Remove @modern-js/plugin-storybook |
| 21 | +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**. |
206 | 22 |
|
207 |
| -在 modern.config.(j|t)s 中删除 @modern-js/plugin-storybook 插件的注册。 |
| 23 | +:::info |
| 24 | +This document only provides the simplest usage. For more information, please refer to [Storybook Rsbuild Modern.js Integration](https://storybook.rsbuild.rs/guide/integrations/modernjs.html). |
| 25 | +::: |
208 | 26 |
|
209 |
| -##### Modify Storybook Syntax |
| 27 | +## Installation |
210 | 28 |
|
211 |
| -Follow the official Storybook documentation to make the necessary updates for some breaking changes, such as changes in story writing and MDX syntax. You can refer to the migration guide at [storybook migrate doc](https://storybook.js.org/docs/react/migration-guide). |
| 29 | +<PackageManagerTabs command="install @rsbuild/core storybook-builder-rsbuild storybook-addon-modernjs -D" /> |
212 | 30 |
|
213 |
| -### Native Storybook users |
| 31 | +You need to install `@rsbuild/core` in your project, otherwise the plugin may not work properly. |
214 | 32 |
|
215 |
| -Modern.js only support Storybook 7, so you need to upgrade from Storybook version 6 to version 7, please follow the steps outlined in the official Storybook documentation at [storybook migrate doc](https://storybook.js.org/docs/react/migration-guide). |
| 33 | +## Configure `.storybook/main.ts` |
216 | 34 |
|
217 |
| -```diff filename='.storybook/main.js' |
218 |
| -const config = { |
219 |
| -- framework: '@storybook/react-webapck5', |
220 |
| -+ framework: { |
221 |
| -+ name: '@modern-js/storybook' |
222 |
| -+ }, |
223 |
| -}; |
| 35 | +```ts |
| 36 | +import type { StorybookConfig } from 'storybook-react-rsbuild' |
224 | 37 |
|
225 |
| -export default config; |
| 38 | +const config: StorybookConfig = { |
| 39 | + stories: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'], |
| 40 | + addons: ['storybook-addon-modernjs'], |
| 41 | + framework: ['storybook-react-rsbuild'], |
| 42 | +} |
| 43 | +export default config |
226 | 44 | ```
|
227 | 45 |
|
228 |
| -The default config file path is `modern.config.(j|t)s`, for the detail config, see [modern.js config](/configure/app/usage). |
229 |
| -
|
230 |
| -If the original project includes configurations for Babel, they need to be written in the modern configuration. Most Babel configurations have already been included in Modern.js. |
| 46 | +## Example |
231 | 47 |
|
232 |
| -After installation, make the corresponding [configuration](/guides/basic-features/debug/using-storybook#configurations). |
| 48 | +You can check out the [example](https://github.com/rspack-contrib/storybook-rsbuild/tree/main/sandboxes/modernjs-react) to learn how to use Storybook in Modern.js. |
0 commit comments