@modern-js/main-doc 2.0.0-canary.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +62 -31
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/router/router.md +174 -375
- package/en/docusaurus-plugin-content-docs/current/components/enable-bff.md +36 -0
- package/en/docusaurus-plugin-content-docs/current/components/enable-micro-frontend.md +13 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-master-manifest-config.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/components/micro-runtime-config.md +18 -0
- package/en/docusaurus-plugin-content-docs/current/components/router-legacy-tip.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/auto-load-plugin.md +62 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/deploy/microFrontend.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/ssg.md +226 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/master-app.md +20 -39
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/state.md +17 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/server/enable-framework-ext.md +47 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/frameworks.md +2 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/function.md +10 -6
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +6 -2
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/extend.md +162 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook-list.md +803 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/implement.md +247 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/introduction.md +49 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/plugin-api.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/relationship.md +118 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +3 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +1 -9
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +2 -2
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c02-development.md +191 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c05-mixed-stack.md +24 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/auto-actions.md +90 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/computed-state.md +151 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/define-model.md +66 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/faq.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/manage-effects.md +259 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/model-communicate.md +219 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/performance.md +173 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/quick-start.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/redux-integration.md +21 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/test-model.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/typescript-best-practice.md +71 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-model.md +244 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-out-of-modernjs.md +51 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/_category_.json +5 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +99 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +324 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +169 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +82 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +260 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +283 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +137 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/_category_.json +1 -1
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +5 -3
- package/package.json +4 -4
- package/zh/apis/app/runtime/core/use-module-apps.md +2 -0
- package/zh/apis/app/runtime/router/router.md +169 -371
- package/zh/components/enable-bff.md +36 -0
- package/zh/components/micro-master-manifest-config.md +15 -0
- package/zh/components/router-legacy-tip.md +1 -0
- package/zh/configure/app/auto-load-plugin.md +62 -0
- package/zh/configure/app/deploy/microFrontend.md +0 -10
- package/zh/configure/app/output/ssg.md +1 -5
- package/zh/configure/app/runtime/master-app.md +4 -18
- package/zh/configure/app/runtime/router.md +19 -4
- package/zh/configure/app/runtime/state.md +7 -7
- package/zh/configure/app/server/enable-framework-ext.md +47 -0
- package/zh/configure/app/server/port.md +1 -1
- package/zh/configure/app/tools/_category_.json +1 -1
- package/zh/guides/advanced-features/bff/frameworks.md +2 -0
- package/zh/guides/advanced-features/bff/function.md +7 -5
- package/zh/guides/advanced-features/eslint.md +2 -1
- package/zh/guides/advanced-features/ssg.md +4 -0
- package/zh/guides/basic-features/data-fetch.md +1 -1
- package/zh/guides/basic-features/env-vars.md +1 -1
- package/zh/guides/basic-features/routes.md +0 -3
- package/zh/guides/topic-detail/generator/config/module.md +3 -1
- package/zh/guides/topic-detail/generator/config/mwa.md +1 -9
- package/zh/guides/topic-detail/model/quick-start.md +1 -1
- package/zh/tutorials/first-app/c06-model.md +5 -1
- package/zh/tutorials/first-app/c08-entries.md +1 -1
- package/zh/tutorials/foundations/introduction.md +5 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +0 -12
- package/en/docusaurus-plugin-content-docs/current/configure/app/bff/fetcher.md +0 -28
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -31
- package/en/docusaurus-plugin-content-docs/current/guides/overview.md +0 -11
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/basic.md +0 -8
- package/zh/apis/app/overview.md +0 -11
- package/zh/apis/monorepo/overview.md +0 -11
- package/zh/configure/app/bff/fetcher.md +0 -31
- package/zh/configure/app/dev/with-master-app.md +0 -32
- package/zh/guides/overview.md +0 -11
- package/zh/tutorials/foundations/basic.md +0 -8
package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md
CHANGED
@@ -47,7 +47,7 @@ Package management tool (packageManager), the options are as follows:
|
|
47
47
|
In the custom type of the generator plugin to create a project scenario, only the `packageManager` configuration is provided by default.
|
48
48
|
:::
|
49
49
|
|
50
|
-
##
|
50
|
+
## Other configurations
|
51
51
|
|
52
52
|
### noNeedInstall
|
53
53
|
|
package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md
CHANGED
@@ -14,7 +14,7 @@ The application project creation parameters are [Generator Configuration](/docs/
|
|
14
14
|
|
15
15
|
Package name (packageName), character string type.
|
16
16
|
|
17
|
-
## New
|
17
|
+
## New Command
|
18
18
|
|
19
19
|
The new command configuration in the module project can be used through the `--config` parameter configuration when executing the new command, or when enabling functions in the generator plugin.
|
20
20
|
|
@@ -37,3 +37,5 @@ Optional function name (function), supports the following options:
|
|
37
37
|
- Storybook(mwa_storybook)
|
38
38
|
|
39
39
|
- Runtime API(runtimeApi)
|
40
|
+
|
41
|
+
- Test(test)
|
package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md
CHANGED
@@ -34,20 +34,12 @@ Element name (element), supports two options:
|
|
34
34
|
|
35
35
|
- Create customized server dir(server)
|
36
36
|
|
37
|
-
|
37
|
+
The entry also need to be used in conjunction with the specific configuration, which is described as follows:
|
38
38
|
|
39
39
|
#### name
|
40
40
|
|
41
41
|
Entry name (name), character `string` type.
|
42
42
|
|
43
|
-
#### framework
|
44
|
-
|
45
|
-
Customized server runtime framework(framework),supports two options::
|
46
|
-
|
47
|
-
- Express(express)
|
48
|
-
|
49
|
-
- Koa(koa)
|
50
|
-
|
51
43
|
### function
|
52
44
|
|
53
45
|
Optional function name (function), supports the following options:
|
@@ -24,7 +24,7 @@ Use npx to get the latest version of `@modern-js/create` every time.
|
|
24
24
|
|
25
25
|
Project directory name.
|
26
26
|
|
27
|
-
When executing the above command, the
|
27
|
+
When executing the above command, the `projectDir` folder will be created in the current directory by default, and the initialization project will be in this folder. When this parameter is empty, the initialization project will be directly generated in the current directory.
|
28
28
|
|
29
29
|
### --version
|
30
30
|
|
@@ -104,7 +104,7 @@ For custom generator plugins, please refer to [Development Generator Plugin](/do
|
|
104
104
|
|
105
105
|
Specifies the microgenerator.
|
106
106
|
|
107
|
-
By default, `@modern-js/create` will execute the microgenerator Modern.js framework build-in. If you need to execute a customized microgenerator, and you need to use the posture of
|
107
|
+
By default, `@modern-js/create` will execute the microgenerator Modern.js framework build-in. If you need to execute a customized microgenerator, and you need to use the posture of `npx @modern-js/create`, you can use this parameter directly.
|
108
108
|
|
109
109
|
For custom microgenerators, please refer to [Develop Microgenerator](/docs/guides/topic-detail/generator/codesmith/introduce).
|
110
110
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
title: 微前端介绍
|
4
|
+
---
|
5
|
+
|
6
|
+
微前端是一种类似于微服务的架构,是一种由独立交付的多个前端应用组成整体的架构风格,将前端应用分解成一些更小、更简单的能够独立开发、测试、部署的应用,而在用户看来仍然是内聚的单个产品。
|
7
|
+
|
8
|
+
它主要解决了两个问题:
|
9
|
+
|
10
|
+
* 随着项目迭代应用越来越庞大,难以维护。
|
11
|
+
* 跨团队或跨部门协作开发项目导致效率低下的问题。
|
12
|
+
|
13
|
+
## 微前端关键词
|
14
|
+
|
15
|
+
在微前端研发模式中,应用会被分成 **主应用**、和 **子应用**。
|
16
|
+
|
17
|
+
- 主应用:微前端项目的基座工程,所有子应用都会由它来加载。
|
18
|
+
- 子应用:独立开发、独立部署的应用,最终会被主应用加载。
|
19
|
+
|
20
|
+
## 功能简介
|
21
|
+
|
22
|
+
* 基于 [Garfish](https://www.garfishjs.org/guide)
|
23
|
+
* 生成器支持微前端应用
|
24
|
+
* 支持 React 组件式引用微前端子应用
|
25
|
+
* 支持 loading
|
26
|
+
* 支持主应用线上、子应用线下调试模式
|
27
|
+
|
28
|
+
可以在 [体验微前端](/docs/guides/topic-detail/micro-frontend/c02-development) 一节学习如何开发微前端主子应用。
|
29
|
+
|
@@ -0,0 +1,191 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
title: 体验微前端
|
4
|
+
---
|
5
|
+
|
6
|
+
通过本章你可以了解到:
|
7
|
+
|
8
|
+
- 如何创建微前端项目的主应用、子应用。
|
9
|
+
- 微前端项目开发的基本流程。
|
10
|
+
|
11
|
+
## 创建应用
|
12
|
+
|
13
|
+
在这次的实践中,我们需要创建三个应用,分别为1个主应用,2个子应用:
|
14
|
+
|
15
|
+
- main 主应用
|
16
|
+
- dashboard 子应用
|
17
|
+
- table 子应用
|
18
|
+
|
19
|
+
### 创建 main 主应用
|
20
|
+
|
21
|
+
通过命令行工具初始化项目:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
mkdir main && cd main
|
25
|
+
npx @modern-js/create
|
26
|
+
```
|
27
|
+
|
28
|
+
import DefaultMWAGenerate from '@site-docs/components/default-mwa-generate.md';
|
29
|
+
|
30
|
+
<DefaultMWAGenerate />
|
31
|
+
|
32
|
+
完成项目创建后我们可以通过 `pnpm run new` 来开启 `微前端` 功能:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
? 请选择你想要的操作 启用可选功能
|
36
|
+
? 启用可选功能 启用「微前端」模式
|
37
|
+
```
|
38
|
+
|
39
|
+
接下来,让我们注册微前端插件并添加开启微前端主应用,并增加子应用列表:
|
40
|
+
|
41
|
+
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend.md';
|
42
|
+
|
43
|
+
<EnableMicroFrontend />
|
44
|
+
|
45
|
+
import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config.md';
|
46
|
+
|
47
|
+
<MicroRuntimeConfig />
|
48
|
+
|
49
|
+
|
50
|
+
### 创建 dashboard 子应用
|
51
|
+
|
52
|
+
通过命令行工具初始化项目:
|
53
|
+
|
54
|
+
```bash
|
55
|
+
mkdir dashboard && cd dashboard
|
56
|
+
npx @modern-js/create
|
57
|
+
```
|
58
|
+
|
59
|
+
按照如下选择,生成项目:
|
60
|
+
|
61
|
+
<DefaultMWAGenerate/>
|
62
|
+
|
63
|
+
我们执行 `pnpm run new` 来开启 `微前端` 功能:
|
64
|
+
|
65
|
+
```bash
|
66
|
+
? 请选择你想要的操作 启用可选功能
|
67
|
+
? 启用可选功能 启用「微前端」模式
|
68
|
+
```
|
69
|
+
接下来,让我们注册微前端插件并修改 `modern.config.ts`,添加微前端子应用的配置 `deploy.microFrontend`:
|
70
|
+
|
71
|
+
```javascript title="modern.config.ts"
|
72
|
+
import AppToolPlugin, { defineConfig } from '@modern-js/app-tools';
|
73
|
+
import GarfishPlugin from '@modern-js/plugin-garfish';
|
74
|
+
|
75
|
+
export default defineConfig({
|
76
|
+
runtime: {
|
77
|
+
router: true,
|
78
|
+
state: true
|
79
|
+
},
|
80
|
+
deploy: {
|
81
|
+
microFrontend: true
|
82
|
+
},
|
83
|
+
plugins: [AppToolPlugin(), GarfishPlugin()],
|
84
|
+
});
|
85
|
+
```
|
86
|
+
|
87
|
+
### 创建 table 子应用
|
88
|
+
|
89
|
+
通过命令行工具初始化项目:
|
90
|
+
|
91
|
+
```bash
|
92
|
+
mkdir table && cd table
|
93
|
+
npx @modern-js/create
|
94
|
+
```
|
95
|
+
|
96
|
+
按照如下选择,生成项目:
|
97
|
+
|
98
|
+
<DefaultMWAGenerate/>
|
99
|
+
|
100
|
+
我们执行 `pnpm run new` 来开启 `微前端`:
|
101
|
+
|
102
|
+
```bash
|
103
|
+
? 请选择你想要的操作 启用可选功能
|
104
|
+
? 启用可选功能 启用「微前端」模式
|
105
|
+
```
|
106
|
+
|
107
|
+
接下来,让我们注册微前端插件并修改 `modern.config.ts`,添加微前端子应用的配置 `deploy.microFrontend`:
|
108
|
+
|
109
|
+
```javascript title="modern.config.ts"
|
110
|
+
import AppToolPlugin, { defineConfig } from '@modern-js/app-tools';
|
111
|
+
import GarfishPlugin from '@modern-js/plugin-garfish';
|
112
|
+
|
113
|
+
export default defineConfig({
|
114
|
+
runtime: {
|
115
|
+
router: true,
|
116
|
+
state: true
|
117
|
+
},
|
118
|
+
deploy: {
|
119
|
+
microFrontend: true
|
120
|
+
},
|
121
|
+
plugins: [AppToolPlugin(), GarfishPlugin()],
|
122
|
+
});
|
123
|
+
```
|
124
|
+
|
125
|
+
## 添加代码
|
126
|
+
|
127
|
+
### main 主应用
|
128
|
+
|
129
|
+
删除 `src/routers` 目录, 创建 `src/App.tsx`,并添加如下内容:
|
130
|
+
|
131
|
+
```tsx
|
132
|
+
import { Link } from '@modern-js/runtime/router';
|
133
|
+
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
134
|
+
|
135
|
+
const App = () => {
|
136
|
+
const { DashBoard, TableList } = useModuleApps();
|
137
|
+
return (
|
138
|
+
<div>
|
139
|
+
<div>
|
140
|
+
<Link to='/dashboard'>Dashboard</Link>
|
141
|
+
<Link to='/table'>Table</Link>
|
142
|
+
</div>
|
143
|
+
<Route path='/dashboard'>
|
144
|
+
<DashBoard />
|
145
|
+
</Route>
|
146
|
+
<Route path='/table'>
|
147
|
+
<TableList />
|
148
|
+
</Route>
|
149
|
+
</div>
|
150
|
+
);
|
151
|
+
};
|
152
|
+
|
153
|
+
export default App;
|
154
|
+
```
|
155
|
+
|
156
|
+
### dashboard 子应用
|
157
|
+
|
158
|
+
删除 `src/routers` 目录, 创建 `src/App.tsx`,并添加如下内容:
|
159
|
+
|
160
|
+
```tsx
|
161
|
+
export default () => <div>Dashboard Page</div>;
|
162
|
+
```
|
163
|
+
|
164
|
+
### table 子应用
|
165
|
+
|
166
|
+
删除 `src/routers` 目录, 创建 `src/App.tsx`,并添加如下内容:
|
167
|
+
|
168
|
+
```tsx
|
169
|
+
export default () => <div>Table Page</div>;
|
170
|
+
```
|
171
|
+
|
172
|
+
## 调试
|
173
|
+
|
174
|
+
按顺序在 `main`、 `dashboard`、 `table` 目录执行 `pnpm run dev` 命令启动应用:
|
175
|
+
|
176
|
+
- main - `http://localhost:8080`
|
177
|
+
- dashboard - `http://localhost:8081`
|
178
|
+
- table - `http://localhost:8082`
|
179
|
+
|
180
|
+
访问主应用地址 `http://localhost:8080`,效果如下:
|
181
|
+
|
182
|
+

|
183
|
+
|
184
|
+
在完成了微前端整体开发流程的体验后,你可以进一步了解如何 [开发主应用](./c03-main-app.md)
|
185
|
+
|
186
|
+
|
187
|
+
## 常见问题
|
188
|
+
|
189
|
+
自查手册: https://www.garfishjs.org/issues/
|
190
|
+
|
191
|
+
|
package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c03-main-app.md
ADDED
@@ -0,0 +1,246 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 3
|
3
|
+
title: 开发主应用
|
4
|
+
---
|
5
|
+
|
6
|
+
在上一章 [体验微前端](./c02-development.md) 通过一个示例演示了如何创建和配置微前端子应用,通过本章你可以进一步了解如何开发主应用,以及它的常见配置。
|
7
|
+
|
8
|
+
在通过 `@modern-js/create` 命令创建应用工程后,可以在项目中执行 `pnpm run new`(实际执行了 `modern new` 命令),在选择了「微前端」模式后,会安装微前端依赖依赖,只需手动注册插件即可。
|
9
|
+
|
10
|
+
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend.md';
|
11
|
+
|
12
|
+
<EnableMicroFrontend />
|
13
|
+
|
14
|
+
## 注册子应用信息
|
15
|
+
|
16
|
+
当在 `masterApp` 配置上提供了信息后,将会认为该应用为主应用,目前存在两种子应用信息的配置方式,这两种方式分别应用于不同的场景。
|
17
|
+
|
18
|
+
|
19
|
+
### 直接注册子应用信息
|
20
|
+
|
21
|
+
可以直接通过配置注册子应用信息:
|
22
|
+
|
23
|
+
:::tip 提示
|
24
|
+
可以通过 API [defineConfig](/docs/apis/app/runtime/app/define-config) 在运行时进行配置。
|
25
|
+
当参数为函数时无法被序列化到产物代码,所以在涉及到函数之类的配置时请通过 defineConfig 来进行配置
|
26
|
+
:::
|
27
|
+
|
28
|
+
import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config.md';
|
29
|
+
|
30
|
+
<MicroRuntimeConfig />
|
31
|
+
|
32
|
+
|
33
|
+
### 自定义远程应用列表
|
34
|
+
|
35
|
+
通过该函数可以拉取远程的子应用列表,并将其注册至运行时框架:
|
36
|
+
|
37
|
+
```ts title="App.tsx"
|
38
|
+
defineConfig(App, {
|
39
|
+
masterApp: {
|
40
|
+
manifest: {
|
41
|
+
getAppList: async ()=> {
|
42
|
+
// 可以从其他远程接口获取
|
43
|
+
return [
|
44
|
+
{
|
45
|
+
name: 'DashBoard',
|
46
|
+
entry: 'http://127.0.0.1:8081/'
|
47
|
+
},
|
48
|
+
{
|
49
|
+
name: 'TableList',
|
50
|
+
entry: 'http://localhost:8082'
|
51
|
+
}
|
52
|
+
];
|
53
|
+
},
|
54
|
+
}
|
55
|
+
}
|
56
|
+
})
|
57
|
+
```
|
58
|
+
|
59
|
+
## 渲染子应用
|
60
|
+
|
61
|
+
在微前端中分为两种加载子应用的方式:
|
62
|
+
|
63
|
+
1. **子应用组件** 获取到每个子应用的组件,之后就可以像使用普通的 `React` 组件一样渲染微前端的子应用。
|
64
|
+
2. **集中式路由** 通过集中式的路由配置,自动根据当前页面 `pathname` 激活渲染对应的子应用。
|
65
|
+
|
66
|
+
### 子应用组件
|
67
|
+
|
68
|
+
开发者使用 `useModuleApps` 方法可以获取到各个子应用的组件。
|
69
|
+
|
70
|
+
再通过 `router` 组件的结合运用,开发者可以自控式的根据不同的路由渲染不同的子应用。
|
71
|
+
|
72
|
+
假设我们的子应用列表配置如下:
|
73
|
+
|
74
|
+
<EnableMicroFrontend />
|
75
|
+
|
76
|
+
编辑主应用 `App.tsx` 文件如下:
|
77
|
+
|
78
|
+
```tsx title=主应用:App.tsx
|
79
|
+
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
80
|
+
import { Route, Switch } from '@modern-js/runtime/router';
|
81
|
+
|
82
|
+
function App() {
|
83
|
+
const { DashBoard, TableList} = useModuleApps();
|
84
|
+
|
85
|
+
return <div>
|
86
|
+
<Route path="/dashboard">
|
87
|
+
<DashBoard
|
88
|
+
loadable={{
|
89
|
+
loading: ({ pastDelay, error }: any) => {
|
90
|
+
if (error) {
|
91
|
+
return <div>error: {error?.message}</div>;
|
92
|
+
} else if (pastDelay) {
|
93
|
+
return <div>loading</div>;
|
94
|
+
} else {
|
95
|
+
return null;
|
96
|
+
}
|
97
|
+
},
|
98
|
+
}}
|
99
|
+
/>
|
100
|
+
</Route>
|
101
|
+
<Route path="/table">
|
102
|
+
<TableList />
|
103
|
+
</Route>
|
104
|
+
</div>
|
105
|
+
}
|
106
|
+
```
|
107
|
+
|
108
|
+
这里通过 `Route` 组件自定义了 **DashBoard** 的激活路由为 **/dashboard**, **TableList** 的激活路由为 **/table**。
|
109
|
+
|
110
|
+
### 集中式路由
|
111
|
+
|
112
|
+
**集中式路由** 是将子应用的激活路由集中配置的方式。我们给子应用列表信息添加 `activeWhen` 字段来启用 **集中式路由**。
|
113
|
+
|
114
|
+
|
115
|
+
<MicroRuntimeConfig />
|
116
|
+
|
117
|
+
然后在主应用中使用 `useModuleApp` 方法获取 `MApp` 组件, 并在主应用渲染 `MApp`。
|
118
|
+
|
119
|
+
```tsx title=主应用:App.tsx
|
120
|
+
import { useModuleApp } from '@modern-js/plugin-runtime';
|
121
|
+
|
122
|
+
function App() {
|
123
|
+
const { MApp } = useModuleApps();
|
124
|
+
|
125
|
+
return <div>
|
126
|
+
<MApp />
|
127
|
+
</div>
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
这样启动应用后,访问 `/dashboard` 路由,会渲染 `Dashboard` 子应用,访问 `/table` 路由,会渲染 `TableList` 子应用。
|
132
|
+
|
133
|
+
|
134
|
+
### 两种模式混用
|
135
|
+
|
136
|
+
当然 **子应用组件** 和 **集中式路由** 是可以混合使用的。
|
137
|
+
|
138
|
+
- 一部分子应用作为 **子应用组件** 激活,另外一部分作为 **集中式路由** 激活。
|
139
|
+
- 一部分子应用既可以作为 **集中式路由** 激活,也可以作为 **子应用组件** 激活。
|
140
|
+
|
141
|
+
|
142
|
+
### 添加 loading
|
143
|
+
|
144
|
+
通过配置 `loadable` 配置,可以为「集中式路由」、「子应用」添加 loading 组件,并可以考虑错误、超时、闪烁等情况的出现,从而为用户提供更好的用户体验。该功能的设计与实现参考至 [react-loadable](https://github.com/jamiebuilds/react-loadable),基本功能较为相似。
|
145
|
+
|
146
|
+
```tsx
|
147
|
+
function Loading() {
|
148
|
+
return <div>Loading...</div>;
|
149
|
+
}
|
150
|
+
|
151
|
+
function App(){
|
152
|
+
return <>
|
153
|
+
<DashBoard
|
154
|
+
loadable={{
|
155
|
+
loading: Loading,
|
156
|
+
}}
|
157
|
+
/>
|
158
|
+
<>
|
159
|
+
}
|
160
|
+
```
|
161
|
+
|
162
|
+
#### 增加错误状态
|
163
|
+
|
164
|
+
当微前端子应用加载失败或渲染失败时,`loading component` 将会接收 `error` 参数(若没有错误时 error 是 null)
|
165
|
+
|
166
|
+
```tsx
|
167
|
+
function Loading({ error }) {
|
168
|
+
if (error) {
|
169
|
+
return <div>Error msg {error?.message}</div>;
|
170
|
+
} else {
|
171
|
+
return <div>Loading...</div>;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
```
|
175
|
+
|
176
|
+
#### 避免 loading 闪退
|
177
|
+
|
178
|
+
有时 loading 组件的显示时间可能小于 200ms,这个时候会出现 loading 组件闪退的情况。许多用户研究证明,loading 闪退的情况会导致用户感知加载内容的耗时比实际耗时更长,在 loading 小于 200ms 时,不展示内容,用户会认为它更快。
|
179
|
+
|
180
|
+
所以 loading 组件还提供了 `pastDelay` 参数,超过设置的延迟展示时才会为 true,可以通过 `delay` 参数设置延迟的时长
|
181
|
+
|
182
|
+
|
183
|
+
```tsx
|
184
|
+
function Loading({ error, pastDelay }) {
|
185
|
+
if (error) {
|
186
|
+
return <div>Error! {error?.message}</div>;
|
187
|
+
} else if (pastDelay) {
|
188
|
+
return <div>Loading...</div>;
|
189
|
+
} else {
|
190
|
+
return null;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
```
|
194
|
+
|
195
|
+
`delay` 的默认值为 `200ms`,可以通过 `loadable` 中的 `delay` 来设置延迟展示的时间
|
196
|
+
|
197
|
+
```tsx
|
198
|
+
|
199
|
+
function App(){
|
200
|
+
return <>
|
201
|
+
<DashBoard
|
202
|
+
loadable={{
|
203
|
+
loading: Loading,
|
204
|
+
delay: 300 // 0.3 seconds
|
205
|
+
}}
|
206
|
+
/>
|
207
|
+
<>
|
208
|
+
}
|
209
|
+
```
|
210
|
+
|
211
|
+
#### 增加超时状态
|
212
|
+
|
213
|
+
有时因为网络的原因,从而导致微前端子应用加载失败,从而导致一直展示 loading 的状态,这对于用户而言非常糟糕,因为他们不知道合适才会获得具体的响应,他们是否需要刷新页面,通过增加超时状态可以很好的解决该问题。
|
214
|
+
|
215
|
+
loading 组件在超时时会获得 `timeOut` 参数,当微前端应用加载超时时将会获得 `timeOut` 属性值为 true
|
216
|
+
|
217
|
+
```tsx
|
218
|
+
function Loading({ error, timeOut, pastDelay }) {
|
219
|
+
if (error) {
|
220
|
+
return <div>Error! {error?.message}</div>;
|
221
|
+
} else if (timeOut) {
|
222
|
+
return <div>Loading timed out, please refresh the page... </div>;
|
223
|
+
} else if (pastDelay) {
|
224
|
+
return <div>Loading...</div>;
|
225
|
+
} else {
|
226
|
+
return null;
|
227
|
+
}
|
228
|
+
}
|
229
|
+
```
|
230
|
+
|
231
|
+
超时状态是关闭的,可以通过在 `loadable` 中设置 `timeout` 参数开启
|
232
|
+
|
233
|
+
|
234
|
+
```tsx
|
235
|
+
|
236
|
+
function App(){
|
237
|
+
return <>
|
238
|
+
<DashBoard
|
239
|
+
loadable={{
|
240
|
+
loading: Loading,
|
241
|
+
timeOut: 10000 // 10s
|
242
|
+
}}
|
243
|
+
/>
|
244
|
+
<>
|
245
|
+
}
|
246
|
+
```
|
@@ -0,0 +1,54 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 4
|
3
|
+
title: 主子应用通信
|
4
|
+
---
|
5
|
+
|
6
|
+
## props 通信
|
7
|
+
|
8
|
+
Modern.js 中,会将子应用包裹成一个 React 组件,直接通过给 React 组件传递 `props` 即可实现主应用和子应用通信的目的。
|
9
|
+
|
10
|
+
```tsx title=主应用:App.tsx
|
11
|
+
function App() {
|
12
|
+
const { MApp } = useModuleApps();
|
13
|
+
|
14
|
+
return <div>
|
15
|
+
<MApp count={100} />
|
16
|
+
</div>;
|
17
|
+
}
|
18
|
+
```
|
19
|
+
|
20
|
+
```tsx title=子应用:App.tsx
|
21
|
+
function App(props) {
|
22
|
+
console.log(props);
|
23
|
+
return ...
|
24
|
+
}
|
25
|
+
```
|
26
|
+
|
27
|
+
子应用将会打印 `{count: 0}`,目前尚未支持子应用渲染响应式,及时在主应用上更改 `count` 的数据也不会触发视图更新
|
28
|
+
|
29
|
+
## channel 通信
|
30
|
+
|
31
|
+
[Garfish.channel](https://www.garfishjs.org/api/channel) 用于应用间的通信。它是 `EventEmitter2` 的实例
|
32
|
+
|
33
|
+
```ts
|
34
|
+
// 子应用监听登录事件
|
35
|
+
const App = () => {
|
36
|
+
const handleLogin = (userInfo) => {
|
37
|
+
console.log(`${userInfo.name} has login`);
|
38
|
+
};
|
39
|
+
|
40
|
+
useEffect(() => {
|
41
|
+
window?.Garfish.channel.on('login', handleLogin);
|
42
|
+
return () => {
|
43
|
+
window?.Garfish.channel.removeListener('login', handleLogin);
|
44
|
+
};
|
45
|
+
});
|
46
|
+
};
|
47
|
+
|
48
|
+
// 主应用触发监听事件
|
49
|
+
api.getLoginInfo.then((res) => {
|
50
|
+
if (res.code === 0) {
|
51
|
+
window.Garfish.channel.emit('login', res.data);
|
52
|
+
}
|
53
|
+
});
|
54
|
+
```
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 5
|
3
|
+
title: 混合技术栈
|
4
|
+
---
|
5
|
+
|
6
|
+
Modern.js 微前端方案是基于 [Garfish](https://garfishjs.org/) 封装的,提供了一些更开箱即用的使用方式。
|
7
|
+
|
8
|
+
当你的主应用和子应用不全是 Modern.js 应用的时候,可以参考这片文档。
|
9
|
+
|
10
|
+
1. 子应用是 **Modern.js**,主应用使用的原生 Garfish 微前端。
|
11
|
+
2. 主应用是 **Modern.js**,子应用有的是其它技术栈。
|
12
|
+
|
13
|
+
## 子应用是 Modern.js
|
14
|
+
|
15
|
+
**Modern.js** 子应用编译后会生成标准的 [Garfish 子应用导出](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0)。
|
16
|
+
所以可以直接接入标准的微前端主应用。
|
17
|
+
|
18
|
+
:::info 注
|
19
|
+
子应用是 **Modern.js**,主应用使用的原生 Garfish 微前端时,**子应用调试模式** 不可用。
|
20
|
+
:::
|
21
|
+
|
22
|
+
## 主应用是 Modern.js
|
23
|
+
|
24
|
+
主应用是 **Modern.js**,子应用用的其它技术栈。子应用按照 [Garfish 子应用标准](https://www.garfishjs.org/guide/demo/react) 开发即可。
|