Storybook is a tool dedicated to component debugging, providing around component development.
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 to upgrade.
Modern.js integrates Storybook debugging capabilities by default. You can directly enable the Storybook feature by using the following command:
This command will create a template for Storybook, including:
To start, run npm run storybook
.
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:
Note that in the above configuration, the reactDocgen configuration has been changed because Rspack currently does not support @storybook/react-docgen-typescript-plugin.
There are some configurations in .storybook/main.js
.
string
modern.config.(j|t)s
Specify the path of Modern.js configuration.
Example:
'webpack' | 'rspack'
webpack
Specify the underlying build tool to use either Webpack or Rspack.
Example:
BuilderConfig
undefined
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.
Example:
@modern-js/storybook proxies some of the storybook cli commands.
Start Storybook, more details at storybook#dev.
Build Storybook for production, more details at storybook#build.
Due to the current version of Storybook in the document being version 7, please select the addon for Storybook V7.
When an addon does not require additional Babel or Webpack configurations, it can be used directly, such as @storybook/addon-essentials.
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.
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.
The powerful build capabilities of Modern.js can be directly used in Storybook projects.
Modern.js won't load your other configurations like babel.config.json
, babel config needs to be set in Modern.js config, tools.babel.
Webpack configuration can be written in either tools.webpack or tools.webpackChain.
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.
If you are a user migrating from V6 to V7, you can use V7 through the above method and make the following adjustments:
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
.
And then add the following lines in root/.storybook/main.(j|t)s
, specify framework as @modern-js/storybook
.
Update dependencies like @storybook/addon-* to major version 7.
在 modern.config.(j|t)s 中删除 @modern-js/plugin-storybook 插件的注册。
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.
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.
The default config file path is modern.config.(j|t)s
, for the detail config, see modern.js config.
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.
After installation, make the corresponding configuration.