Storybook is a tool dedicated to component debugging, providing around component development.
Before when using Storybook, there are various problems related to configurations, Babel, Webpack, less or sass. Modern.js integrates with Storybook, which greatly simplifies configuration for us as we develop our Storybook project.
You can directly enable the Storybook feature by using the following command:
This command will create a template for Storybook, including:
In fact, Modern.js Module is implemented based on esbuild, while Storybook uses Webpack as the default build tool, and their configurations are not fully compatible. Therefore, we recommend building the components first, and then importing the component output in stories.
The way to import component outputs is very simple. Suppose your module exports a Button component, then you can use it in stories like this:
If you want to update the components, you can start the build in watch mode before starting Storybook:
In the development process, you may encounter situations where you cannot get type definitions in real time. Because only when the code is saved, the type file under the output directory will be updated. At this time:
For pnpm
projects, you can modify the package.json
as follows:
For the use of
publishConfig
in pnpm, you can read the following link.
For npm and Yarn projects, the types
value of package.json
can only be modified manually during the development stage and release stage.
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 separate configurations in .storybook/main.js
.
'webpack' | 'rspack'
webpack
Specify the underlying bundler to use either Webpack or Rspack.
Example:
BuilderConfig
undefined
The Storybook build capability of Modern.js is provided by Rsbuild, and the Rsbuild configuration can be modified through builderConfig.
@modern-js/storybook proxies some storybook cli commands
Start Storybook,detail
Build Storybook for production,detail
Our support methods for the two versions are different, so if you are migrating from V6 to V7, we hope you will also use V7 in the same way, while making the following adjustments:
root/config/storybook/main.(j|t)s
to the new root/.storybook/main.(j|t)s
.@storybook/addon-*
series dependencies (if any) to version 7 and delete @modern-js/plugin-storybook
dependencies.edenx dev storybook
and edenx build --platform
commands.
If you are used to the original pnpm run dev
usage, you can replace it with storybook dev -p 6006
and storybook build
.@modern-js/plugin-storybook
plugin.Starting from 2.40.0
version, @modern-js/plugin-storybook
will stop iterating. It is recommended to use the V7 version.
If your @modern-js/module-tools
version is lower than 2.40.0
, you can use Storybook V6 in the following ways:
You can directly use the following command to enable the Storybook feature.
This command will create commonly used templates for Storybook, including
pnpm dev storybook
.The Storybook official configures the project through a folder named .storybook
, which contains various configuration files. In Modern.js Module, you can add Storybook configuration files in the project's config/storybook
directory.
For how to use various Storybook configuration files, you can check the following link:
However, there are some restrictions when using it in module projects:
stories
configuration cannot be modified in the main.js
file.webpackFinal
and babel
configurations cannot be modified in the main.js
file.In addition to debugging the component or ordinary modules with Storybook, you can also use the following command to execute the Storybook build task.
After the build is complete, you can see the build output files in the dist/storybook-static
directory.