@modern-js/main-doc 2.67.10 → 2.68.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/runtime/web-server/hook.mdx +3 -7
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +2 -9
- package/docs/en/apis/app/runtime/web-server/unstable_middleware.mdx +7 -0
- package/docs/en/configure/app/experiments/lazy-compilation.mdx +11 -0
- package/docs/en/configure/app/source/include.mdx +34 -7
- package/docs/en/guides/basic-features/debug/using-storybook.mdx +28 -212
- package/docs/zh/apis/app/runtime/web-server/hook.mdx +5 -6
- package/docs/zh/apis/app/runtime/web-server/middleware.mdx +2 -7
- package/docs/zh/apis/app/runtime/web-server/unstable_middleware.mdx +6 -0
- package/docs/zh/configure/app/experiments/lazy-compilation.mdx +10 -0
- package/docs/zh/configure/app/source/include.mdx +34 -5
- package/docs/zh/guides/basic-features/debug/using-storybook.mdx +22 -206
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +4 -4
- package/package.json +6 -6
@@ -4,18 +4,14 @@ title: Hook
|
|
4
4
|
|
5
5
|
# Hook
|
6
6
|
|
7
|
-
Used to extend Modern.js built-in Web Server, all page requests are handled by these hooks.
|
8
|
-
|
9
7
|
:::warning
|
10
|
-
It is recommended to use [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) to handle page requests.
|
11
|
-
|
12
|
-
:::
|
13
8
|
|
14
|
-
|
15
|
-
For more detail, see [Extend Web Server](/guides/advanced-features/web-server).
|
9
|
+
The Hook API has been deprecated. Please use the [new Custom Web Server](/guides/advanced-features/web-server) to handle page requests.
|
16
10
|
|
17
11
|
:::
|
18
12
|
|
13
|
+
Used to extend Modern.js built-in Web Server, all page requests are handled by these hooks.
|
14
|
+
|
19
15
|
## Usage
|
20
16
|
|
21
17
|
```ts
|
@@ -4,21 +4,14 @@ title: Middleware
|
|
4
4
|
|
5
5
|
# Middleware
|
6
6
|
|
7
|
-
Used to extend the built-in Web Server of Modern.js, unlike [Hook](/apis/app/runtime/web-server/hook), Middleware can directly operate Node's origin request and response, and can be extended using the framework plugin.
|
8
|
-
|
9
7
|
:::warning
|
10
8
|
|
11
|
-
Middleware
|
12
|
-
|
13
|
-
It is recommended to use [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) to handle page requests.
|
9
|
+
Middleware has been deprecated. Please use the [new Custom Web Server](/guides/advanced-features/web-server) to handle page requests.
|
14
10
|
|
15
11
|
:::
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
For more detail, see [Extend Web Server](/guides/advanced-features/web-server).
|
13
|
+
Used to extend the built-in Web Server of Modern.js, unlike [Hook](/apis/app/runtime/web-server/hook), Middleware can directly operate Node's origin request and response, and can be extended using the framework plugin.
|
20
14
|
|
21
|
-
:::
|
22
15
|
|
23
16
|
## Usage
|
24
17
|
|
@@ -4,9 +4,16 @@ title: Unstable Middleware
|
|
4
4
|
|
5
5
|
# Unstable Middleware
|
6
6
|
|
7
|
+
:::warning
|
8
|
+
|
9
|
+
UnstableMiddleware has been deprecated. Please use the [new Custom Web Server](/guides/advanced-features/web-server) to handle page requests.
|
10
|
+
|
11
|
+
:::
|
12
|
+
|
7
13
|
Used to extend the built-in Web Server in Modern.js.
|
8
14
|
UnstableMiddleware will replace [Middleware](/apis/app/runtime/web-server/middleware) in the future.
|
9
15
|
|
16
|
+
|
10
17
|
## Usage
|
11
18
|
|
12
19
|
```ts title="server/index.ts"
|
@@ -25,6 +25,17 @@ Used to enable the lazy compilation (i.e. compile on demand). When this config i
|
|
25
25
|
|
26
26
|
Lazy compilation only takes effect in the development.
|
27
27
|
|
28
|
+
## Rspack
|
29
|
+
|
30
|
+
import RsbuildLink from '@site/src/components/RsbuildLink';
|
31
|
+
|
32
|
+
:::info
|
33
|
+
The usage of this configuration item is exactly the same as that of Rsbuild. For detailed information, please refer to <RsbuildLink configName="dev.lazyCompilation"/>。
|
34
|
+
In Rspack build mode,
|
35
|
+
:::
|
36
|
+
|
37
|
+
## Webapck
|
38
|
+
|
28
39
|
### Lazy Compilation for Dynamic Imports
|
29
40
|
|
30
41
|
Lazy compile async modules introduced by [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import):
|
@@ -5,21 +5,48 @@ title: include
|
|
5
5
|
# source.include
|
6
6
|
|
7
7
|
- **Type:** [RuleSetCondition[]](https://rspack.rs/config/module#condition)
|
8
|
-
- **Default:**
|
8
|
+
- **Default value:**
|
9
9
|
|
10
10
|
```ts
|
11
11
|
const defaultInclude = [
|
12
12
|
[
|
13
|
-
/node_modules
|
14
|
-
|
15
|
-
/node_modules\/@remix-run\/router/,
|
16
|
-
'/node_modules/.modern-js',
|
13
|
+
{ not: /[\\/]node_modules[\\/]/ },
|
14
|
+
/\.(?:ts|tsx|jsx|mts|cts)$/,
|
17
15
|
],
|
18
16
|
];
|
19
17
|
```
|
20
18
|
|
21
|
-
|
19
|
+
`source.include` is used to specify additional JavaScript files that need to be compiled. By default, the following files will be compiled:
|
22
20
|
|
23
|
-
|
21
|
+
- TypeScript and JSX files in any directory, with file extensions matching `.ts`, `.tsx`, `.jsx`, `.mts`, `.cts`.
|
22
|
+
- JavaScript files not in the `node_modules` directory, with file extensions matching `.js`, `.mjs`, `.cjs`.
|
23
|
+
|
24
|
+
:::tip
|
25
|
+
Before Rsbuild version 1.4, the default value of `source.include` was:
|
26
|
+
|
27
|
+
```ts
|
28
|
+
const defaultInclude = [
|
29
|
+
[
|
30
|
+
{
|
31
|
+
and: [
|
32
|
+
APP_ROOT,
|
33
|
+
{ not: /[\\/]node_modules[\\/]/ }
|
34
|
+
]
|
35
|
+
},
|
36
|
+
/\.(?:ts|tsx|jsx|mts|cts)$/,
|
37
|
+
],
|
38
|
+
];
|
39
|
+
```
|
40
|
+
|
41
|
+
The difference from the new version is that `.js`, `.mjs`, `.cjs` files only in the current directory will be compiled.
|
42
|
+
:::
|
43
|
+
|
44
|
+
## Rspack
|
45
|
+
|
46
|
+
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
24
47
|
|
25
48
|
<RsbuildConfig configName="source.include" />
|
49
|
+
|
50
|
+
## Webpack
|
51
|
+
|
52
|
+
To ensure legacy projects can run properly, when using Webpack, the default value of `source.include` is the same as before Rsbuild version 1.4.
|
@@ -2,231 +2,47 @@
|
|
2
2
|
sidebar_position: 15
|
3
3
|
---
|
4
4
|
|
5
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
17
|
+
## Principle
|
202
18
|
|
203
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
27
|
+
## Installation
|
210
28
|
|
211
|
-
|
29
|
+
<PackageManagerTabs command="install @rsbuild/core storybook-builder-rsbuild storybook-addon-modernjs -D" />
|
212
30
|
|
213
|
-
|
31
|
+
You need to install `@rsbuild/core` in your project, otherwise the plugin may not work properly.
|
214
32
|
|
215
|
-
|
33
|
+
## Configure `.storybook/main.ts`
|
216
34
|
|
217
|
-
```
|
218
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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.
|
@@ -4,16 +4,15 @@ title: Hook
|
|
4
4
|
|
5
5
|
# Hook
|
6
6
|
|
7
|
-
用于拓展 Modern.js 内置的 Web Server,所有的页面请求都会经过 Hook。
|
8
|
-
|
9
7
|
:::warning
|
10
|
-
推荐使用 [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware) 来处理页面请求。
|
11
|
-
:::
|
12
8
|
|
13
|
-
|
14
|
-
|
9
|
+
Hook API 已废弃,请使用[新版自定义 Web Server](/guides/advanced-features/web-server) 来处理页面请求。
|
10
|
+
|
15
11
|
:::
|
16
12
|
|
13
|
+
用于拓展 Modern.js 内置的 Web Server,所有的页面请求都会经过 Hook。
|
14
|
+
|
15
|
+
|
17
16
|
## 使用姿势
|
18
17
|
|
19
18
|
```ts
|
@@ -4,19 +4,14 @@ title: Middleware
|
|
4
4
|
|
5
5
|
# Middleware
|
6
6
|
|
7
|
-
用于拓展 Modern.js 内置的 Web Server,与 [Hook](/apis/app/runtime/web-server/hook) 不同的是,Middleware 可以直接操作 Node 原生的请求、响应对象,并且可以使用框架拓展。
|
8
|
-
|
9
7
|
:::warning
|
10
8
|
|
11
|
-
Middleware
|
9
|
+
Middleware 已废弃,请使用[新版自定义 Web Server](/guides/advanced-features/web-server) 来处理页面请求。
|
12
10
|
|
13
11
|
:::
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
更多内容可以查看[自定义 Web Server](/guides/advanced-features/web-server)。
|
13
|
+
用于拓展 Modern.js 内置的 Web Server,与 [Hook](/apis/app/runtime/web-server/hook) 不同的是,Middleware 可以直接操作 Node 原生的请求、响应对象,并且可以使用框架拓展。
|
18
14
|
|
19
|
-
:::
|
20
15
|
|
21
16
|
## 使用姿势
|
22
17
|
|
@@ -4,6 +4,12 @@ title: Unstable Middleware
|
|
4
4
|
|
5
5
|
# Unstable Middleware
|
6
6
|
|
7
|
+
:::warning
|
8
|
+
|
9
|
+
Unstable Middleware 已废弃,请使用[新版自定义 Web Server](/guides/advanced-features/web-server) 来处理页面请求。
|
10
|
+
|
11
|
+
:::
|
12
|
+
|
7
13
|
用于拓展 Modern.js 内置的 Web Server。 未来 UnstableMiddleware 将替代 [Middleware](/apis/app/runtime/web-server/middleware)
|
8
14
|
|
9
15
|
## 使用
|
@@ -25,6 +25,16 @@ type LazyCompilationOptions =
|
|
25
25
|
|
26
26
|
延迟编译只在开发环境下生效。
|
27
27
|
|
28
|
+
## Rspack
|
29
|
+
|
30
|
+
import RsbuildLink from '@site/src/components/RsbuildLink';
|
31
|
+
|
32
|
+
:::info
|
33
|
+
该配置项的使用方式与 Rsbuild 完全一致,详细信息可参考 <RsbuildLink configName="dev.lazyCompilation"/>。
|
34
|
+
:::
|
35
|
+
|
36
|
+
## Webpack
|
37
|
+
|
28
38
|
### 延迟编译异步模块
|
29
39
|
|
30
40
|
延迟编译 [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) 引入的异步模块:
|
@@ -10,16 +10,45 @@ title: include
|
|
10
10
|
```ts
|
11
11
|
const defaultInclude = [
|
12
12
|
[
|
13
|
-
/node_modules
|
14
|
-
|
15
|
-
/node_modules\/@remix-run\/router/,
|
16
|
-
'/node_modules/.modern-js',
|
13
|
+
{ not: /[\\/]node_modules[\\/]/ },
|
14
|
+
/\.(?:ts|tsx|jsx|mts|cts)$/,
|
17
15
|
],
|
18
16
|
];
|
19
17
|
```
|
20
18
|
|
21
|
-
`source.include` 用于指定额外需要编译的 JavaScript
|
19
|
+
`source.include` 用于指定额外需要编译的 JavaScript 文件。默认情况下,会编译以下文件:
|
20
|
+
|
21
|
+
- 任意目录下的 TypeScript 和 JSX 文件,匹配的文件后缀为 `.ts`、`.tsx`、`.jsx`、`.mts`、`.cts`。
|
22
|
+
- 非 `node_modules` 目录下的 JavaScript 文件,匹配的文件后缀为 `.js`、`.mjs`、`.cjs`。
|
23
|
+
|
24
|
+
:::tip
|
25
|
+
在 Rsbuild 1.4 版本前,`source.include` 的默认值为:
|
26
|
+
|
27
|
+
```ts
|
28
|
+
const defaultInclude = [
|
29
|
+
[
|
30
|
+
{
|
31
|
+
and: [
|
32
|
+
APP_ROOT,
|
33
|
+
{ not: /[\\/]node_modules[\\/]/ }
|
34
|
+
]
|
35
|
+
},
|
36
|
+
/\.(?:ts|tsx|jsx|mts|cts)$/,
|
37
|
+
],
|
38
|
+
];
|
39
|
+
```
|
40
|
+
|
41
|
+
与新版本的差异是,非当前项目目录的 `.js`、`.mjs`、`.cjs` 文件不会被编译。
|
42
|
+
:::
|
43
|
+
|
44
|
+
## Rspack
|
22
45
|
|
23
46
|
import RsbuildConfig from '@site-docs/components/rsbuild-config-tooltip';
|
24
47
|
|
25
48
|
<RsbuildConfig configName="source.include" />
|
49
|
+
|
50
|
+
## Webpack
|
51
|
+
|
52
|
+
为了保证历史项目可以正常运行,Webpack 编译时,`source.include` 与 Rsbuild 1.4 版本前的默认值一致。
|
53
|
+
|
54
|
+
|
@@ -2,6 +2,8 @@
|
|
2
2
|
sidebar_position: 15
|
3
3
|
---
|
4
4
|
|
5
|
+
import { PackageManagerTabs } from '@theme'
|
6
|
+
|
5
7
|
# 使用 Storybook
|
6
8
|
|
7
9
|
[Storybook](https://storybook.js.org/) 是一个专门用于组件调试的工具,它围绕着组件开发提供了:
|
@@ -12,221 +14,35 @@ sidebar_position: 15
|
|
12
14
|
- 可分享能力
|
13
15
|
- 工作流程自动化
|
14
16
|
|
15
|
-
|
16
|
-
本教程适用于新版 Storybook V7 用户,如果你在使用老版本 Storybook V6 (有使用 @modern-js/plugin-storybook 插件),可参考 [迁移指南](#迁移指南) 进行升级。
|
17
|
-
:::
|
18
|
-
|
19
|
-
## 快速开始
|
20
|
-
|
21
|
-
Modern.js 默认集成了 Storybook 的调试能力,当我们想要对组件进行调试的时候,可以通过以下方式启用 Storybook 调试功能:
|
22
|
-
|
23
|
-
```bash
|
24
|
-
$ npx modern new
|
25
|
-
? 请选择你想要的操作 启用可选功能
|
26
|
-
? 请选择功能名称 启用「Storybook」V7
|
27
|
-
```
|
28
|
-
|
29
|
-
该命令会创建好 Storybook 常用的模版,包括:
|
30
|
-
|
31
|
-
- 创建配置文件夹 `.storybook`,以及默认配置文件 `.storybook/main.ts`
|
32
|
-
- 创建 stories 组件示例
|
33
|
-
- 更新 package.json,新增依赖 @storybook/addon-essential 和 @modern-js/storybook,以及创建 storybook 相关脚本
|
34
|
-
|
35
|
-
运行 `npm run storybook` 即可启动 Storybook 预览。
|
36
|
-
|
37
|
-
## 开启 Rspack 构建
|
38
|
-
|
39
|
-
Rspack 构建速度非常快,在 Modern.js 中只需要如下配置即可使用 Rspack 作为构建工具。
|
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
|
-
注意上面配置中,更改了 reactDocgen 配置,因为 Rspack 目前还不支持 @storybook/react-docgen-typescript-plugin。
|
60
|
-
|
61
|
-
## 配置
|
62
|
-
|
63
|
-
在 `.storybook/main.js` 中包含一些配置。
|
64
|
-
|
65
|
-
### configPath
|
17
|
+
## 原理
|
66
18
|
|
67
|
-
|
68
|
-
- **默认值**: `modern.config.(j|t)s`
|
19
|
+
Rsbuild 提供了 [Storybook Rsbuild](https://storybook.rsbuild.rs/index.html),任何 Rsbuild 的项目都可以使用该工具来实现 Storybook 的构建。
|
69
20
|
|
70
|
-
|
21
|
+
Modern.js 基于此工具实现了 `storybook-addon-modernjs`,该插件会加载 Modern.js 配置文件,并转换为 **Storybook Rsbuild** 可用的配置。
|
71
22
|
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
- **类型**: `'webpack' | 'rspack'`
|
90
|
-
- **默认值**: `webpack`
|
91
|
-
|
92
|
-
指定底层构建工具使用 Webpack 还是 Rspack。
|
93
|
-
|
94
|
-
例如
|
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
|
110
|
-
|
111
|
-
- **类型**: `BuilderConfig`
|
112
|
-
- **默认值**: `undefined`
|
113
|
-
|
114
|
-
更改构建配置,该配置比配置文件拥有更高的优先级,若不想使用配置文件,也可直接在此处指定。
|
115
|
-
|
116
|
-
例如
|
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
|
-
## 命令行
|
137
|
-
|
138
|
-
@modern-js/storybook 代理了部分 storybook cli 的命令。
|
139
|
-
|
140
|
-
### storybook dev
|
141
|
-
|
142
|
-
启动 Storybook,详情请看 [storybook#dev](https://storybook.js.org/docs/react/api/cli-options#dev)
|
143
|
-
|
144
|
-
### storybook build
|
145
|
-
|
146
|
-
对 Storybook 进行生产环境构建,详情请看 [storybook#build](https://storybook.js.org/docs/react/api/cli-options#build)
|
147
|
-
|
148
|
-
## Storybook addon 兼容性
|
149
|
-
|
150
|
-
由于当前文档中的 Storybook 版本为 7,因此请选择 storybook V7 的 addon。
|
151
|
-
|
152
|
-
当 addon 不需要额外的 Babel 或 Webpack 配置时,可以直接使用,如 @storybook/addon-essentials。
|
153
|
-
|
154
|
-
部分 addon 需要依赖 babel 插件和 Webpack 配置时,如 @storybook/addon-coverage,只有使用 webpack 构建才会支持。
|
155
|
-
|
156
|
-
## 收益
|
157
|
-
|
158
|
-
使用 @modern-js/storybook 可以带给你 Rspack 超快的构建,并且完全无需繁琐配置,开箱即用。并且默认包含了许多 Web 构建中的最佳实践,例如 code splitting 策略,内置 css module 和 postcss,开箱即用的 TypeScript 支持,内置常用 Babel 插件等等。
|
159
|
-
|
160
|
-
Modern.js 的构建能力和配置都可以直接在 Storybook 项目中使用。
|
161
|
-
|
162
|
-
## Trouble Shooting
|
163
|
-
|
164
|
-
1. 使用 Modern.js 启动 storybook 时不会读取 babel.config.json 等配置文件,因此 babel 配置需要在 [tools.babel](/configure/app/tools/babel) 中进行配置。
|
165
|
-
同样的 webpack 配置需要写在 [tools.webpack](/configure/app/tools/webpack) 或 [tools.webpackChain](/configure/app/tools/webpack-chain) 中。
|
166
|
-
|
167
|
-
2. 如果发现构建速度很慢,请检查是否开启了自动文档生成功能,如果想要最高的性能,请配置为 `react-docgen`。`react-docgen` 和 `react-docgen-typescript` 的区别是,前者基于 Babel 实现,后者基于 TypeScript 实现,前者性能会更好,但类型推断能力更弱。如果使用 Rspack 构建,则只支持 `react-docgen`。
|
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
|
-
## 迁移指南
|
180
|
-
|
181
|
-
### 从 Modern.js Storybook V6 迁移
|
182
|
-
|
183
|
-
如果你是从 V6 迁移至 V7 的用户,可以通过 [上述方式](#快速开始) 使用 V7,同时做以下调整:
|
184
|
-
|
185
|
-
##### 修改配置文件
|
186
|
-
|
187
|
-
若你在旧版本对 storybook 进行了一些自定义配置,需要将配置文件 `root/config/storybook/main.(j|t)s` 移动到 `root/.storybook/main.(j|t)s`。
|
188
|
-
|
189
|
-
并在 `root/.storybook/main.(j|t)s` 中添加以下配置,指定 framework 为 @modern-js/storybook:
|
190
|
-
|
191
|
-
```diff
|
192
|
-
const config = {
|
193
|
-
+ framework: {
|
194
|
-
+ name: '@modern-js/storybook'
|
195
|
-
+ },
|
196
|
-
};
|
197
|
-
|
198
|
-
export default config;
|
199
|
-
```
|
200
|
-
|
201
|
-
##### 依赖升级
|
202
|
-
|
203
|
-
升级 @storybook/addon-\* 系列依赖,升级到 7 版本。
|
204
|
-
|
205
|
-
##### 移除 @modern-js/plugin-storybook 插件
|
206
|
-
|
207
|
-
在 modern.config.(j|t)s 中删除 @modern-js/plugin-storybook 插件的注册。
|
23
|
+
:::info
|
24
|
+
本文档仅提供最简单的使用方式,更多内容可参考 [Storybook Rsbuild Modern.js 集成](https://storybook.rsbuild.rs/guide/integrations/modernjs.html)。
|
25
|
+
:::
|
208
26
|
|
209
|
-
|
27
|
+
## 安装
|
210
28
|
|
211
|
-
|
29
|
+
<PackageManagerTabs command="install @rsbuild/core storybook-builder-rsbuild storybook-addon-modernjs -D" />
|
212
30
|
|
213
|
-
|
31
|
+
你需要在项目中安装 `@rsbuild/core`,否则该插件可能无法正常工作。
|
214
32
|
|
215
|
-
|
33
|
+
## 配置 `.storybook/main.ts`
|
216
34
|
|
217
|
-
```
|
218
|
-
|
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
|
-
|
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
|
-
|
229
|
-
|
230
|
-
若原来项目中包含了 Babel 等配置,需要对应的写在 modern 配置中,大部分 Babel 配置已经包含进了 Modern.js。
|
46
|
+
## 示例
|
231
47
|
|
232
|
-
|
48
|
+
你可以查看 [示例](https://github.com/rspack-contrib/storybook-rsbuild/tree/main/sandboxes/modernjs-react) 了解 Modern.js 中使用 Storybook 的方式。
|
@@ -304,11 +304,11 @@ export default (props: { basename: string }) => {
|
|
304
304
|
使用纯 `Garfish` API 开发微前端应用时
|
305
305
|
|
306
306
|
- 主应用:
|
307
|
-
- 安装 Garfish 依赖并使用 `Garfish.run` 注册子应用 [参考](https://www.garfishjs.org/api/run)
|
308
|
-
- 提供一个常驻的 `DOM` 节点供子应用挂载 [参考](https://www.garfishjs.org/api/registerApp
|
307
|
+
- 安装 Garfish 依赖并使用 `Garfish.run` 注册子应用 [参考](https://www.garfishjs.org/api/run.html)
|
308
|
+
- 提供一个常驻的 `DOM` 节点供子应用挂载 [参考](https://www.garfishjs.org/api/registerApp.html)
|
309
309
|
- 子应用:
|
310
|
-
- 导出 `provider` [参考](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0)
|
311
|
-
- 设置应用的 `basename` [参考](https://www.garfishjs.org/guide/start#3-%E8%AE%BE%E7%BD%AE%E5%BA%94%E7%94%A8%E8%B7%AF%E7%94%B1-basename)
|
310
|
+
- 导出 `provider` [参考](https://www.garfishjs.org/guide/quick-start/start.html#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0)
|
311
|
+
- 设置应用的 `basename` [参考](https://www.garfishjs.org/guide/quick-start/start.html#3-%E8%AE%BE%E7%BD%AE%E5%BA%94%E7%94%A8%E8%B7%AF%E7%94%B1-basename)
|
312
312
|
|
313
313
|
区别于直接使用 `Garfish` 运行时 API 开发微前端项目,`Modern.js` 的微前端方案更加开箱即用。
|
314
314
|
使用 `pnpm new` 启用微前端模式后会自动在 `Modern.js` 应用中集成 `Garfish` 插件,在 `Garfish`
|
package/package.json
CHANGED
@@ -15,28 +15,28 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.
|
18
|
+
"version": "2.68.0",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public"
|
22
22
|
},
|
23
23
|
"dependencies": {
|
24
24
|
"mermaid": "^11.4.1",
|
25
|
-
"@modern-js/sandpack-react": "2.
|
25
|
+
"@modern-js/sandpack-react": "2.68.0"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
28
|
"@rsbuild/plugin-sass": "1.3.2",
|
29
29
|
"@shikijs/transformers": "^3.4.2",
|
30
|
-
"@rspress/shared": "2.0.0-beta.
|
31
|
-
"@rspress/plugin-llms": "2.0.0-beta.
|
30
|
+
"@rspress/shared": "2.0.0-beta.16",
|
31
|
+
"@rspress/plugin-llms": "2.0.0-beta.16",
|
32
32
|
"@types/fs-extra": "9.0.13",
|
33
33
|
"@types/node": "^16",
|
34
34
|
"classnames": "^2.5.1",
|
35
35
|
"clsx": "^1.2.1",
|
36
|
-
"fs-extra": "^10",
|
36
|
+
"fs-extra": "^10.1.0",
|
37
37
|
"react": "^18.3.1",
|
38
38
|
"react-dom": "^18.3.1",
|
39
|
-
"rspress": "2.0.0-beta.
|
39
|
+
"rspress": "2.0.0-beta.16",
|
40
40
|
"ts-node": "^10.9.1",
|
41
41
|
"typescript": "^5"
|
42
42
|
},
|