@modern-js/main-doc 2.46.1 → 2.47.0
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/commands.mdx +9 -9
- package/docs/en/configure/app/auto-load-plugin.mdx +3 -3
- package/docs/en/configure/app/builder-plugins.mdx +13 -13
- package/docs/en/configure/app/output/disable-node-polyfill.mdx +1 -1
- package/docs/en/configure/app/source/enable-async-entry.mdx +1 -1
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +7 -7
- package/docs/en/guides/advanced-features/rsbuild-plugin.mdx +51 -0
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -3
- package/docs/en/guides/advanced-features/using-storybook.mdx +202 -20
- package/docs/en/guides/basic-features/css.mdx +2 -2
- package/docs/en/guides/concept/builder.mdx +23 -25
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +35 -4
- package/docs/zh/apis/app/commands.mdx +9 -9
- package/docs/zh/configure/app/auto-load-plugin.mdx +3 -3
- package/docs/zh/configure/app/builder-plugins.mdx +14 -14
- package/docs/zh/configure/app/output/disable-node-polyfill.mdx +1 -1
- package/docs/zh/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +8 -8
- package/docs/zh/guides/advanced-features/rsbuild-plugin.mdx +51 -0
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/zh/guides/advanced-features/using-storybook.mdx +204 -21
- package/docs/zh/guides/basic-features/css.mdx +2 -2
- package/docs/zh/guides/concept/builder.mdx +20 -22
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +35 -6
- package/package.json +7 -7
- package/docs/en/configure/app/source/compile-js-data-uri.mdx +0 -9
- package/docs/zh/configure/app/source/compile-js-data-uri.mdx +0 -9
@@ -89,7 +89,7 @@ Modern.js supports enabling ["Tailwind CSS"](/en/guides/basic-features/css.html#
|
|
89
89
|
Modern.js supports three CSS preprocessors: [Sass](https://sass-lang.com/), [Less](https://lesscss.org/), and [Stylus](https://stylus-lang.com/):
|
90
90
|
|
91
91
|
- Sass and Less are supported by default and ready to use.
|
92
|
-
- Stylus is optional and can be used by referring to the ["Stylus Plugin"](https://
|
92
|
+
- Stylus is optional and can be used by referring to the ["Stylus Plugin"](https://rsbuild.dev/plugins/list/plugin-stylus).
|
93
93
|
|
94
94
|
---
|
95
95
|
|
@@ -10,7 +10,7 @@ Modern.js offers a comprehensive plugin system with a complete lifecycle. Plugin
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[
|
13
|
+
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[plugins](/configure/app/plugins.html)` field:
|
14
14
|
|
15
15
|
```ts title="edenx.config.ts"
|
16
16
|
// an example for bff
|
@@ -12,14 +12,15 @@ Through this chapter you can learn:
|
|
12
12
|
|
13
13
|
## Create an app
|
14
14
|
|
15
|
-
|
15
|
+
The routing modes of the current project are divided into the following three types:
|
16
16
|
|
17
|
-
-
|
18
|
-
-
|
17
|
+
- File-based routing (`router: true` and file-based)
|
18
|
+
- Self-Controlled routing (`router: true` and create `BrowserRouter`)
|
19
|
+
- Other (`router: false` and independent installation of `react-router-dom` in the project)
|
19
20
|
|
20
21
|
First, clarify the routing mode of the main application [create a file-based routing main App](#file-based-routing-main-app) or [create a self-controlled main App](#self-controlled-main-app)
|
21
22
|
|
22
|
-
In this
|
23
|
+
In this tutorial we will create two sub-applications Table and Dashboard for the main application (Table is reduced routing, Dashboard is self-controlled routing)
|
23
24
|
|
24
25
|
### File-based Routing Main App
|
25
26
|
|
@@ -139,6 +140,36 @@ import CustomRouterMicroFrontend from '@site-docs-en/components/custom-router-mi
|
|
139
140
|
|
140
141
|
<CustomRouterMicroFrontend />
|
141
142
|
|
143
|
+
### Other Main App
|
144
|
+
|
145
|
+
Install and use `react-router-dom` independently in the project. The only difference from self-controlled routing is that after setting `router: false`, plugin-garfish cannot obtain `useLocation`, `useHref` and other hooks to assist in calculating basename and main and child applications. Route synchronization
|
146
|
+
|
147
|
+
import { Tab, Tabs } from 'rspress/theme';
|
148
|
+
|
149
|
+
<Tabs>
|
150
|
+
<Tab label="react-router-dom@6">
|
151
|
+
```tsx
|
152
|
+
import { useLocation, useHref } from 'react-router-dom';
|
153
|
+
const App = () => {
|
154
|
+
const basename = useHref('/table');
|
155
|
+
const { Table } = useModuleApps();
|
156
|
+
return <Table useLocation={useLocation} basename={basename} />;
|
157
|
+
};
|
158
|
+
```
|
159
|
+
</Tab>
|
160
|
+
<Tab label="react-router-dom@5">
|
161
|
+
```tsx
|
162
|
+
import { useLocation, useHistory } from 'react-router-dom';
|
163
|
+
const App = () => {
|
164
|
+
const history = useHistory();
|
165
|
+
const basename = history.createHref({ pathname: '/table' });
|
166
|
+
const { Table } = useModuleApps();
|
167
|
+
return <Table useLocation={useLocation} basename={basename} />;
|
168
|
+
};
|
169
|
+
```
|
170
|
+
</Tab>
|
171
|
+
</Tabs>
|
172
|
+
|
142
173
|
### File-based sub-app
|
143
174
|
|
144
175
|
Initialize the project with a command line:
|
@@ -195,7 +195,7 @@ Options:
|
|
195
195
|
|
196
196
|
## modern inspect
|
197
197
|
|
198
|
-
`modern inspect` 命令用于查看项目的 [
|
198
|
+
`modern inspect` 命令用于查看项目的 [Rsbuild 配置](https://rsbuild.dev/zh/config/index) 以及 webpack 或 Rspack 配置。
|
199
199
|
|
200
200
|
```bash
|
201
201
|
Usage: modern inspect [options]
|
@@ -210,16 +210,16 @@ Options:
|
|
210
210
|
|
211
211
|
在项目根目录下执行命令 `npx modern inspect` 后,会在项目的 `dist` 目录生成以下文件:
|
212
212
|
|
213
|
-
- `
|
214
|
-
- `webpack.config.web.
|
213
|
+
- `rsbuild.config.mjs`: 表示在构建时使用的 Rsbuild 配置。
|
214
|
+
- `webpack.config.web.mjs`: 表示在构建时使用的 webpack 配置。
|
215
215
|
|
216
216
|
```bash
|
217
217
|
➜ npx modern inspect
|
218
218
|
|
219
219
|
Inspect config succeed, open following files to view the content:
|
220
220
|
|
221
|
-
-
|
222
|
-
- Webpack Config (web): /root/my-project/dist/webpack.config.web.
|
221
|
+
- Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
|
222
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
|
223
223
|
```
|
224
224
|
|
225
225
|
### 指定环境
|
@@ -240,16 +240,16 @@ modern inspect --verbose
|
|
240
240
|
|
241
241
|
### SSR 构建配置
|
242
242
|
|
243
|
-
如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `webpack.config.node.
|
243
|
+
如果项目开启了 SSR 能力,则在 `dist` 目录会另外生成一份 `webpack.config.node.mjs` 文件,对应 SSR 构建时的 webpack 配置。
|
244
244
|
|
245
245
|
```bash
|
246
246
|
➜ npx modern inspect
|
247
247
|
|
248
248
|
Inspect config succeed, open following files to view the content:
|
249
249
|
|
250
|
-
-
|
251
|
-
- Webpack Config (web): /root/my-project/dist/webpack.config.web.
|
252
|
-
- Webpack Config (node): /root/my-project/dist/webpack.config.node.
|
250
|
+
- Rsbuild Config: /root/my-project/dist/rsbuild.config.mjs
|
251
|
+
- Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
|
252
|
+
- Webpack Config (node): /root/my-project/dist/webpack.config.node.mjs
|
253
253
|
```
|
254
254
|
|
255
255
|
## modern lint
|
@@ -39,14 +39,14 @@ Modern.js 将通过以下几个步骤帮你自动注册插件
|
|
39
39
|
1. Modern.js 在内部维护一份官方插件列表。
|
40
40
|
|
41
41
|
```js
|
42
|
-
const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-
|
42
|
+
const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-tailwindcss', ...];
|
43
43
|
```
|
44
44
|
|
45
45
|
2. Modern.js 将读取你的 `package.json` 文件,收集依赖信息。
|
46
46
|
|
47
47
|
```json title="package.json"
|
48
48
|
"dependencies": {
|
49
|
-
"@modern-js/plugin-
|
49
|
+
"@modern-js/plugin-tailwindcss": "x.x.x"
|
50
50
|
...
|
51
51
|
},
|
52
52
|
"devDependencies": {
|
@@ -55,7 +55,7 @@ const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-i18n', ...];
|
|
55
55
|
}
|
56
56
|
```
|
57
57
|
|
58
|
-
3. Modern.js 观察到你安装了 `@modern-js/plugin-
|
58
|
+
3. Modern.js 观察到你安装了 `@modern-js/plugin-tailwindcss` 和 `@modern-js/app-tools` 等依赖后,将会引入插件自动注册。
|
59
59
|
|
60
60
|
可以注意到这种方式相对黑盒,你甚至对加载插件的过程是无感知的。我们希望更多的细节暴露给开发者,能让开发者去控制这一过程。
|
61
61
|
|
@@ -4,18 +4,18 @@ sidebar_position: 21
|
|
4
4
|
|
5
5
|
# builderPlugins 构建插件
|
6
6
|
|
7
|
-
- **类型:** `
|
7
|
+
- **类型:** `RsbuildPlugin[]`
|
8
8
|
- **默认值:** `[]`
|
9
9
|
|
10
|
-
用于配置
|
10
|
+
用于配置 Rsbuild 构建插件。
|
11
11
|
|
12
|
-
|
12
|
+
Rsbuild 是 Modern.js 底层的构建工具,请阅读 [构建能力](/guides/concept/builder) 了解相关背景。
|
13
13
|
|
14
|
-
如果你想了解
|
14
|
+
如果你想了解 Rsbuild 插件的编写方式,可以参考 [Rsbuild - 插件系统](https://rsbuild.dev/zh/plugins/dev/index)。
|
15
15
|
|
16
16
|
## 注意事项
|
17
17
|
|
18
|
-
该选项**用于配置
|
18
|
+
该选项**用于配置 Rsbuild 构建插件**,如果你需要配置其他类型的插件,请选择对应的配置方式:
|
19
19
|
|
20
20
|
- 配置 Modern.js 框架插件,请使用 [plugins](/configure/app/plugins) 配置项。
|
21
21
|
- 配置 Rspack 或 webpack 插件,请使用 [tools.bundlerChain](/configure/app/tools/bundler-chain) 配置项。
|
@@ -23,23 +23,23 @@ Modern.js Builder 是 Modern.js 底层的构建工具,请阅读 [构建能力]
|
|
23
23
|
|
24
24
|
## 何时使用
|
25
25
|
|
26
|
-
大部分场景下,我们推荐你使用 Modern.js 框架插件,框架插件可以通过 [plugins](/configure/app/plugins) 字段进行注册。因为框架插件提供的 API 更丰富、能力更强,而
|
26
|
+
大部分场景下,我们推荐你使用 Modern.js 框架插件,框架插件可以通过 [plugins](/configure/app/plugins) 字段进行注册。因为框架插件提供的 API 更丰富、能力更强,而 Rsbuild 插件提供的 API 只能用于构建场景。
|
27
27
|
|
28
|
-
当你需要引用一些现有的
|
28
|
+
当你需要引用一些现有的 Rsbuild 插件(并且在 Modern.js 中没有相关能力),或是在不同的框架之间复用 Rsbuild 插件时,你可以使用 `builderPlugins` 字段进行注册。
|
29
29
|
|
30
30
|
## 示例
|
31
31
|
|
32
|
-
下面是
|
32
|
+
下面是 Rsbuild 插件的使用示例。
|
33
33
|
|
34
34
|
### 使用 npm 上的插件
|
35
35
|
|
36
36
|
使用 npm 上的插件,需要通过包管理器安装插件,并通过 import 引入。
|
37
37
|
|
38
38
|
```ts title="modern.config.ts"
|
39
|
-
import {
|
39
|
+
import { myRsbuildPlugin } from 'my-rsbuild-plugin';
|
40
40
|
|
41
41
|
export default defineConfig({
|
42
|
-
builderPlugins: [
|
42
|
+
builderPlugins: [myRsbuildPlugin()],
|
43
43
|
});
|
44
44
|
```
|
45
45
|
|
@@ -48,10 +48,10 @@ export default defineConfig({
|
|
48
48
|
使用本地代码仓库中的插件,直接通过相对路径 import 引入即可。
|
49
49
|
|
50
50
|
```ts title="modern.config.ts"
|
51
|
-
import {
|
51
|
+
import { myRsbuildPlugin } from './plugin/myRsbuildPlugin';
|
52
52
|
|
53
53
|
export default defineConfig({
|
54
|
-
builderPlugins: [
|
54
|
+
builderPlugins: [myRsbuildPlugin()],
|
55
55
|
});
|
56
56
|
```
|
57
57
|
|
@@ -60,11 +60,11 @@ export default defineConfig({
|
|
60
60
|
如果插件提供了一些自定义的配置项,可以通过插件函数的参数传入配置。
|
61
61
|
|
62
62
|
```ts title="modern.config.ts"
|
63
|
-
import {
|
63
|
+
import { myRsbuildPlugin } from 'my-rsbuild-plugin';
|
64
64
|
|
65
65
|
export default defineConfig({
|
66
66
|
builderPlugins: [
|
67
|
-
|
67
|
+
myRsbuildPlugin({
|
68
68
|
foo: 1,
|
69
69
|
bar: 2,
|
70
70
|
}),
|
@@ -19,4 +19,4 @@ export default defineConfig({
|
|
19
19
|
});
|
20
20
|
```
|
21
21
|
|
22
|
-
该配置项基于
|
22
|
+
该配置项基于 Rsbuild 的 Node Polyfill 插件实现,你可以阅读 [Rsbuild - Node Polyfill 插件](https://rsbuild.dev/zh/plugins/list/plugin-node-polyfill) 文档来了解详细信息。
|
@@ -44,9 +44,9 @@ BUNDLE_ANALYZE=true pnpm build
|
|
44
44
|
|
45
45
|
## 提升 Browserslist 范围
|
46
46
|
|
47
|
-
|
47
|
+
Modern.js 会根据项目的 Browserslist 配置范围进行代码编译,并注入相应的 Polyfill。如果项目不需要兼容旧版浏览器,可以根据实际情况来提升 Browserslist 范围,从而减少在语法和 Polyfill 上的编译开销。
|
48
48
|
|
49
|
-
|
49
|
+
Modern.js 默认的 Browserslist 配置为:
|
50
50
|
|
51
51
|
```js
|
52
52
|
['> 0.01%', 'not dead', 'not op_mini all'];
|
@@ -59,14 +59,14 @@ Builder 默认的 Browserslist 配置为:
|
|
59
59
|
```
|
60
60
|
|
61
61
|
:::tip
|
62
|
-
请阅读 [设置浏览器范围](https://
|
62
|
+
请阅读 [设置浏览器范围](https://rsbuild.dev/zh/guide/advanced/browserslist) 章节来了解更多关于 Browserslist 的用法。
|
63
63
|
:::
|
64
64
|
|
65
65
|
## 按需引入 polyfill
|
66
66
|
|
67
67
|
在明确第三方依赖不需要额外 polyfill 的情况下,你可以将 [output.polyfill](/configure/app/output/polyfill.html) 设置为 `usage`。
|
68
68
|
|
69
|
-
在 `usage` 模式下,
|
69
|
+
在 `usage` 模式下,Modern.js 会分析源代码中使用的语法,按需注入所需的 polyfill 代码,从而减少 polyfill 的代码量。
|
70
70
|
|
71
71
|
```js
|
72
72
|
export default {
|
@@ -85,20 +85,20 @@ export default {
|
|
85
85
|
在一般的前端项目中,图片资源的体积往往是项目产物体积的大头,因此如果能尽可能精简图片的体积,那么将会对项目的打包产物体积起到明显的优化效果。你可以在 Modern.js 中注册插件来启用图片压缩功能:
|
86
86
|
|
87
87
|
```js title="modern.config.ts"
|
88
|
-
import {
|
88
|
+
import { pluginImageCompress } from '@rsbuild/plugin-image-compress';
|
89
89
|
|
90
90
|
export default {
|
91
|
-
builderPlugins: [
|
91
|
+
builderPlugins: [pluginImageCompress()],
|
92
92
|
};
|
93
93
|
```
|
94
94
|
|
95
|
-
详见 [Image Compress 插件](https://
|
95
|
+
详见 [Image Compress 插件](https://rsbuild.dev/zh/plugins/list/plugin-image-compress)。
|
96
96
|
|
97
97
|
## 代码拆包
|
98
98
|
|
99
99
|
良好的拆包策略对于提升应用的加载性能是十分重要的,可以充分利用浏览器的缓存机制,减少请求数量,加快页面加载速度。
|
100
100
|
|
101
|
-
在 Modern.js 中内置了[多种拆包策略](https://
|
101
|
+
在 Modern.js 中内置了[多种拆包策略](https://rsbuild.dev/zh/guide/optimization/split-chunk),可以满足大部分应用的需求,你也可以根据自己的业务场景,自定义拆包配置。
|
102
102
|
|
103
103
|
比如将 node_modules 下的 `axios` 库拆分到 `axios.js` 中:
|
104
104
|
|
@@ -0,0 +1,51 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 21
|
3
|
+
title: 使用 Rsbuild 插件
|
4
|
+
---
|
5
|
+
|
6
|
+
# 使用 Rsbuild 插件
|
7
|
+
|
8
|
+
## 插件介绍
|
9
|
+
|
10
|
+
Rsbuild 是 Modern.js 底层的构建工具,可通过添加 Rsbuild 插件修改默认的构建行为并添加各类额外功能,包括但不限于:
|
11
|
+
|
12
|
+
- 修改 Rsbuild 配置
|
13
|
+
- 处理新的文件类型
|
14
|
+
- 修改或编译文件
|
15
|
+
- 部署产物
|
16
|
+
|
17
|
+
你可以在 `modern.config.ts` 中通过 `builderPlugins` 选项来注册 Rsbuild 插件,详见 [builderPlugins 构建插件](/configure/app/builder-plugins.html)。
|
18
|
+
|
19
|
+
## 官方插件
|
20
|
+
|
21
|
+
:::tip
|
22
|
+
Rsbuild 中大部分插件已内置在 Modern.js 中,如:需要注入 Node 模块的 Polyfill 时,可直接使用 [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) 配置项。
|
23
|
+
:::
|
24
|
+
|
25
|
+
### 内置插件
|
26
|
+
|
27
|
+
以下是已在 Modern.js 中内置的 Rsbuild 官方插件:
|
28
|
+
|
29
|
+
| 插件 | 介绍 | Modern.js 链接 |
|
30
|
+
| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
31
|
+
| [React 插件](https://rsbuild.dev/zh/plugins/list/plugin-react) | 提供对 React 的支持 | - |
|
32
|
+
| [SVGR 插件](https://rsbuild.dev/zh/plugins/list/plugin-svgr) | 支持将 SVG 图片转换为一个 React 组件使用 | [output.disableSvgr](/configure/app/output/disable-svgr)<br />[output.svgDefaultExport](/configure/app/output/svg-default-export) |
|
33
|
+
| [Styled Components 插件](https://rsbuild.dev/zh/plugins/list/plugin-styled-components) | 提供对 styled-components 的编译时支持 | [tools.styledComponents](/configure/app/tools/styled-components.html) |
|
34
|
+
| [Assets Retry 插件](https://rsbuild.dev/zh/plugins/list/plugin-assets-retry) | 用于在静态资源加载失败时自动发起重试请求 | [output.assetsRetry](/configure/app/output/assets-retry.html) |
|
35
|
+
| [Type Check 插件](https://rsbuild.dev/zh/plugins/list/plugin-type-check) | 用于在单独的进程中运行 TypeScript 类型检查 | [output.disableTsChecker](/configure/app/output/disable-ts-checker.html)<br />[tools.tsChecker](/configure/app/tools/ts-checker.html) |
|
36
|
+
| [Node Polyfill 插件](https://rsbuild.dev/zh/plugins/list/plugin-node-polyfill) | 用于注入 Node 核心模块在浏览器端的 polyfills | [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) |
|
37
|
+
| [Source Build 插件](https://rsbuild.dev/zh/plugins/list/plugin-source-build) | 用于 monorepo 场景,支持引用其他子目录的源代码,并完成构建和热更新 | [experiments.sourceBuild](/configure/app/experiments/source-build.html) |
|
38
|
+
| [Check Syntax 插件](https://rsbuild.dev/zh/plugins/list/plugin-check-syntax) | 用于分析产物的语法兼容性,判断是否存在导致兼容性问题的高级语法 | [security.checkSyntax](/configure/app/security/check-syntax.html) |
|
39
|
+
| [CSS Minimizer 插件](https://rsbuild.dev/zh/plugins/list/plugin-css-minimizer) | 用于自定义 CSS 压缩工具,切换到 [cssnano](https://cssnano.co/) 或其他工具进行 CSS 压缩。 | [tools.minifyCss](/configure/app/tools/minify-css.html) |
|
40
|
+
| [Pug 插件](https://rsbuild.dev/zh/plugins/list/plugin-pug) | 提供对 Pug 模板引擎的支持 | [tools.pug](/configure/app/tools/pug.html) |
|
41
|
+
| [Rem 插件](https://rsbuild.dev/zh/plugins/list/plugin-rem) | 用于实现移动端页面的 rem 自适应布局 | [output.convertToRem](/configure/app/output/convert-to-rem.html) |
|
42
|
+
| [YAML 插件](https://rsbuild.dev/zh/plugins/list/plugin-yaml) | 用于引用 YAML 文件,并将其转换为 JavaScript 对象 | [TOML 文件](/guides/basic-features/json-files.html#toml-文件) |
|
43
|
+
| [TOML 插件](https://rsbuild.dev/zh/plugins/list/plugin-toml) | 用于引用 TOML 文件,并将其转换为 JavaScript 对象 | [YAML 文件](/guides/basic-features/json-files.html#yaml-文件) |
|
44
|
+
|
45
|
+
### 未内置插件
|
46
|
+
|
47
|
+
以下是未在 Modern.js 中内置的 Rsbuild 官方插件:
|
48
|
+
|
49
|
+
- [Image Compress 插件](https://rsbuild.dev/zh/plugins/list/plugin-image-compress):将项目中用到的图片资源进行压缩处理。
|
50
|
+
- [Stylus 插件](https://rsbuild.dev/zh/plugins/list/plugin-stylus):使用 Stylus 作为 CSS 预处理器。
|
51
|
+
- [UMD 插件](https://rsbuild.dev/zh/plugins/list/plugin-umd):用于构建 UMD 格式的产物。
|
@@ -60,7 +60,7 @@ Modern.js 中 [tools.webpack](/configure/app/tools/webpack) 和 [tools.webpackCh
|
|
60
60
|
```diff
|
61
61
|
export default {
|
62
62
|
tools: {
|
63
|
-
-
|
63
|
+
- webpack: (config, { env }) => {
|
64
64
|
+ rspack: (config, { env }) => {
|
65
65
|
if (env === 'development') {
|
66
66
|
config.devtool = 'cheap-module-eval-source-map';
|
@@ -2,12 +2,7 @@
|
|
2
2
|
sidebar_position: 15
|
3
3
|
---
|
4
4
|
|
5
|
-
# 使用
|
6
|
-
|
7
|
-
:::warning
|
8
|
-
该教程是用于老版本 StorybookV6 用户,并且会在以后弃用,想要从旧版本插件升级或新用户想要尝试请查看[新文档](https://modernjs.dev/builder/guide/advanced/storybook.html)
|
9
|
-
:::
|
10
|
-
|
5
|
+
# 使用 Storybook
|
11
6
|
|
12
7
|
[Storybook](https://storybook.js.org/) 是一个专门用于组件调试的工具,它围绕着组件开发提供了:
|
13
8
|
|
@@ -17,33 +12,221 @@ sidebar_position: 15
|
|
17
12
|
- 可分享能力
|
18
13
|
- 工作流程自动化
|
19
14
|
|
20
|
-
|
15
|
+
:::tip
|
16
|
+
本教程适用于新版 Storybook V7 用户,如果你在使用老版本 Storybook V6 (有使用 @modern-js/plugin-storybook 插件),可参考 [迁移指南](#迁移指南) 进行升级。
|
17
|
+
:::
|
21
18
|
|
22
|
-
|
19
|
+
## 快速开始
|
23
20
|
|
24
|
-
|
21
|
+
Modern.js 默认集成了 Storybook 的调试能力,当我们想要对组件进行调试的时候,可以通过以下方式启用 Storybook 调试功能:
|
25
22
|
|
26
23
|
```bash
|
27
24
|
$ npx modern new
|
28
25
|
? 请选择你想要的操作 启用可选功能
|
29
|
-
? 请选择功能名称 启用「
|
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
|
66
|
+
|
67
|
+
- **类型**: `string`
|
68
|
+
- **默认值**: `modern.config.(j|t)s`
|
69
|
+
|
70
|
+
用于指定配置文件路径。
|
71
|
+
|
72
|
+
例如
|
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
|
+
- **类型**: `'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;
|
30
134
|
```
|
31
135
|
|
32
|
-
|
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。
|
33
153
|
|
34
|
-
|
35
|
-
import { appTools, defineConfig } from '@modern-js/app-tools';
|
36
|
-
import { storybookPlugin } from '@modern-js/plugin-storybook';
|
154
|
+
部分 addon 需要依赖 babel 插件和 Webpack 配置时,如 @storybook/addon-coverage,只有使用 webpack 构建才会支持。
|
37
155
|
|
38
|
-
|
39
|
-
|
40
|
-
|
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;
|
41
177
|
```
|
42
178
|
|
43
|
-
##
|
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 插件的注册。
|
208
|
+
|
209
|
+
##### 修改 Storybook 写法
|
210
|
+
|
211
|
+
按照 Storybook 官网文档,对一些 breaking change 做相应的更新,例如 stories 的写法,MDX 的写法等,参考[storybook 迁移文档](https://storybook.js.org/docs/react/migration-guide)。
|
212
|
+
|
213
|
+
### 从原生 Storybook 项目迁移
|
214
|
+
|
215
|
+
若当前 Storybook 版本还是 6,需要先按照 Storybook 官网文档升级到版本 7,参考 [storybook 迁移文档](https://storybook.js.org/docs/react/migration-guide)。
|
216
|
+
|
217
|
+
```diff filename='.storybook/main.js'
|
218
|
+
const config = {
|
219
|
+
- framework: '@storybook/react-webapck5',
|
220
|
+
+ framework: {
|
221
|
+
+ name: '@modern-js/storybook'
|
222
|
+
+ },
|
223
|
+
};
|
224
|
+
|
225
|
+
export default config;
|
226
|
+
```
|
44
227
|
|
45
|
-
Modern.js
|
228
|
+
Modern.js 的配置文件默认为 `modern.config.(j|t)s`,配置请查看 [modern.js 配置](/configure/app/usage)。
|
46
229
|
|
47
|
-
|
230
|
+
若原来项目中包含了 Babel 等配置,需要对应的写在 modern 配置中,大部分 Babel 配置已经包含进了 Modern.js。
|
48
231
|
|
49
|
-
|
232
|
+
安装完成后进行相应的[配置](/guides/advanced-features/using-storybook#配置)。
|
@@ -12,13 +12,13 @@ Modern.js 内置了社区流行的 CSS 预处理器,包括 Less 和 Sass。
|
|
12
12
|
|
13
13
|
默认情况下,你不需要对 Less 和 Sass 进行任何配置。如果你有自定义 loader 配置的需求,可以通过配置 [tools.less](/configure/app/tools/less)、[tools.sass](/configure/app/tools/sass) 来进行设置。
|
14
14
|
|
15
|
-
你也可以在 Modern.js 中使用 Stylus,只需要安装
|
15
|
+
你也可以在 Modern.js 中使用 Stylus,只需要安装 Rsbuild 提供的 Stylus 插件即可,使用方式请参考 [Stylus 插件](https://rsbuild.dev/zh/plugins/list/plugin-stylus)。
|
16
16
|
|
17
17
|
## 使用 PostCSS
|
18
18
|
|
19
19
|
Modern.js 内置了 [PostCSS](https://postcss.org/) 来转换 CSS 代码。
|
20
20
|
|
21
|
-
请阅读 [
|
21
|
+
请阅读 [Rsbuild - 使用 PostCSS](https://rsbuild.dev/zh/guide/basic/css-usage#%E4%BD%BF%E7%94%A8-postcss) 了解更多用法。
|
22
22
|
|
23
23
|
## 使用 CSS Modules
|
24
24
|
|