@modern-js/main-doc 2.0.0-beta.3 → 2.0.0-beta.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/dev.md +8 -3
  3. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/inspect.md +31 -10
  4. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/serve.md +32 -0
  5. package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/server.md +31 -0
  6. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +4 -3
  7. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/create-app.md +2 -3
  8. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +1 -1
  9. package/en/docusaurus-plugin-content-docs/current/components/init-app.md +42 -0
  10. package/en/docusaurus-plugin-content-docs/current/configure/app/builder-plugins.md +70 -0
  11. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -1
  12. package/en/docusaurus-plugin-content-docs/current/configure/app/plugins.md +11 -5
  13. package/en/docusaurus-plugin-content-docs/current/configure/app/server/routes.md +2 -4
  14. package/en/docusaurus-plugin-content-docs/current/configure/app/source/disable-entry-dirs.md +38 -0
  15. package/en/docusaurus-plugin-content-docs/current/configure/app/source/entries.md +66 -2
  16. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/esbuild.md +16 -39
  17. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-in-js.md +38 -0
  18. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-modules.md +86 -0
  19. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/less-sass.md +17 -0
  20. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/postcss.md +81 -0
  21. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +95 -0
  22. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +66 -0
  23. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +270 -0
  24. package/en/docusaurus-plugin-content-docs/current/guides/concept/entries.md +116 -0
  25. package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +162 -0
  26. package/en/docusaurus-plugin-content-docs/current/guides/get-started/upgrade.md +78 -0
  27. package/{zh/tutorials/first-app → en/docusaurus-plugin-content-docs/current/guides}/overview.md +4 -4
  28. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/_category_.json +4 -0
  29. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/lifecycle.md +15 -0
  30. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +1 -1
  31. package/en/docusaurus-plugin-content-docs/current.json +11 -11
  32. package/package.json +4 -4
  33. package/zh/apis/app/commands/dev.md +9 -4
  34. package/zh/apis/app/commands/inspect.md +32 -11
  35. package/zh/apis/app/commands/new.md +1 -1
  36. package/zh/apis/app/commands/{start.md → serve.md} +3 -3
  37. package/zh/apis/app/hooks/src/index_.md +5 -4
  38. package/zh/apis/app/hooks/src/server.md +31 -0
  39. package/zh/apis/app/runtime/core/bootstrap.md +3 -4
  40. package/zh/apis/app/runtime/core/create-app.md +1 -18
  41. package/zh/apis/app/runtime/core/use-module-apps.md +64 -33
  42. package/zh/apis/app/runtime/web-server/hook.md +1 -1
  43. package/zh/apis/app/runtime/web-server/middleware.md +1 -0
  44. package/zh/components/debug-app.md +18 -0
  45. package/zh/components/default-mwa-generate.md +5 -0
  46. package/zh/components/deploy.md +1 -0
  47. package/zh/components/enable-micro-frontend.md +13 -0
  48. package/zh/components/global-proxy.md +28 -0
  49. package/zh/components/init-app.md +44 -0
  50. package/zh/components/micro-runtime-config.md +18 -0
  51. package/zh/components/prerequisites.md +19 -0
  52. package/zh/components/release-note.md +1 -0
  53. package/zh/configure/app/builder-plugins.md +72 -0
  54. package/zh/configure/app/deploy/_category_.json +4 -0
  55. package/zh/configure/app/deploy/microFrontend.md +64 -0
  56. package/zh/configure/app/dev/with-master-app.md +0 -2
  57. package/zh/configure/app/plugins.md +10 -4
  58. package/zh/configure/app/runtime/master-app.md +33 -36
  59. package/zh/configure/app/server/routes.md +2 -4
  60. package/zh/configure/app/source/disable-entry-dirs.md +37 -0
  61. package/zh/configure/app/source/entries-dir.md +0 -3
  62. package/zh/configure/app/source/entries.md +66 -3
  63. package/zh/configure/app/tools/esbuild.md +16 -39
  64. package/zh/guides/advanced-features/bff/bff-proxy.md +1 -1
  65. package/zh/guides/advanced-features/compatibility.md +14 -39
  66. package/zh/guides/advanced-features/eslint.md +21 -21
  67. package/zh/guides/advanced-features/ssg.md +20 -9
  68. package/zh/guides/advanced-features/ssr.md +95 -52
  69. package/zh/guides/advanced-features/testing.md +44 -1
  70. package/zh/guides/advanced-features/web-server.md +14 -3
  71. package/zh/guides/basic-features/css/tailwindcss.md +13 -6
  72. package/zh/guides/basic-features/data-fetch.md +398 -5
  73. package/zh/guides/basic-features/html.md +182 -0
  74. package/zh/guides/basic-features/mock.md +3 -9
  75. package/zh/guides/basic-features/proxy.md +2 -27
  76. package/zh/guides/basic-features/routes.md +35 -3
  77. package/zh/guides/concept/entries.md +108 -19
  78. package/zh/guides/get-started/quick-start.md +14 -83
  79. package/zh/guides/get-started/upgrade.md +11 -9
  80. package/zh/guides/{concept → topic-detail/framework-plugin}/lifecycle.md +0 -0
  81. package/zh/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
  82. package/zh/guides/topic-detail/micro-frontend/c02-development.md +191 -0
  83. package/zh/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
  84. package/zh/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
  85. package/zh/guides/topic-detail/micro-frontend/{mixed-stack.md → c05-mixed-stack.md} +3 -3
  86. package/zh/guides/topic-detail/model/quick-start.md +1 -1
  87. package/zh/guides/topic-detail/model/test-model.md +2 -2
  88. package/zh/guides/topic-detail/monorepo/create-sub-project.md +2 -2
  89. package/zh/guides/topic-detail/monorepo/intro.md +1 -1
  90. package/zh/guides/troubleshooting/dependencies.md +0 -69
  91. package/zh/tutorials/first-app/_category_.json +1 -1
  92. package/zh/tutorials/first-app/c01-start.md +99 -0
  93. package/zh/tutorials/first-app/{c05-component/5.1-use-ui-library.md → c02-component.md} +13 -15
  94. package/zh/tutorials/first-app/c03-css.md +324 -0
  95. package/zh/tutorials/first-app/{c08-client-side-routing/8.1-code-based-routing.md → c04-routes.md} +52 -39
  96. package/zh/tutorials/first-app/c05-loader.md +82 -0
  97. package/zh/tutorials/first-app/c06-model.md +256 -0
  98. package/zh/tutorials/first-app/c07-container.md +283 -0
  99. package/zh/tutorials/first-app/c08-entries.md +137 -0
  100. package/zh/tutorials/foundations/introduction.md +1 -1
  101. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/start.md +0 -32
  102. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-modern-mode.md +0 -34
  103. package/zh/apis/generator/overview.md +0 -32
  104. package/zh/configure/app/output/enable-modern-mode.md +0 -34
  105. package/zh/guides/advanced-features/custom-app.md +0 -72
  106. package/zh/guides/topic-detail/micro-frontend/communicate.md +0 -39
  107. package/zh/guides/topic-detail/micro-frontend/debugging.md +0 -168
  108. package/zh/guides/topic-detail/micro-frontend/introduction.md +0 -13
  109. package/zh/guides/topic-detail/micro-frontend/route-mode.md +0 -110
  110. package/zh/guides/topic-detail/monorepo/deploy.md +0 -43
  111. package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +0 -25
  112. package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +0 -118
  113. package/zh/tutorials/first-app/c01-getting-started/1.3-dev-command.md +0 -29
  114. package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +0 -47
  115. package/zh/tutorials/first-app/c01-getting-started/1.5-start-command.md +0 -18
  116. package/zh/tutorials/first-app/c01-getting-started/1.6-create-repo.md +0 -31
  117. package/zh/tutorials/first-app/c01-getting-started/_category_.json +0 -3
  118. package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -79
  119. package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +0 -34
  120. package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +0 -19
  121. package/zh/tutorials/first-app/c02-generator-and-studio/_category_.json +0 -3
  122. package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +0 -55
  123. package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +0 -60
  124. package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +0 -11
  125. package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +0 -63
  126. package/zh/tutorials/first-app/c03-ide/_category_.json +0 -3
  127. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +0 -54
  128. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +0 -135
  129. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.3-compatibility.md +0 -67
  130. package/zh/tutorials/first-app/c04-es6-plus-and-ts/_category_.json +0 -3
  131. package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +0 -72
  132. package/zh/tutorials/first-app/c05-component/_category_.json +0 -3
  133. package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +0 -110
  134. package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +0 -143
  135. package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +0 -84
  136. package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +0 -83
  137. package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +0 -77
  138. package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +0 -104
  139. package/zh/tutorials/first-app/c06-css-and-component/_category_.json +0 -3
  140. package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +0 -69
  141. package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +0 -100
  142. package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +0 -69
  143. package/zh/tutorials/first-app/c07-app-entry/_category_.json +0 -3
  144. package/zh/tutorials/first-app/c08-client-side-routing/_category_.json +0 -3
  145. package/zh/tutorials/first-app/c09-bff/9.1-serverless.md +0 -30
  146. package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +0 -95
  147. package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +0 -131
  148. package/zh/tutorials/first-app/c09-bff/_category_.json +0 -3
  149. package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +0 -21
  150. package/zh/tutorials/first-app/c10-model/10.2-add-model.md +0 -185
  151. package/zh/tutorials/first-app/c10-model/10.3-use-model.md +0 -55
  152. package/zh/tutorials/first-app/c10-model/10.4-testing.md +0 -69
  153. package/zh/tutorials/first-app/c10-model/_category_.json +0 -3
  154. package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +0 -240
  155. package/zh/tutorials/first-app/c11-container/11.2-add-container.md +0 -109
  156. package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +0 -63
  157. package/zh/tutorials/first-app/c11-container/11.4-testing.md +0 -56
  158. package/zh/tutorials/first-app/c11-container/_category_.json +0 -3
@@ -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/runtime/garfish';
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
+ ```
@@ -3,7 +3,7 @@ sidebar_position: 5
3
3
  title: 混合技术栈
4
4
  ---
5
5
 
6
- Modern.js 微前端方案是基于 [Garfish](https://garfish.top/) 封装的,提供了一些更开箱即用的使用方式。
6
+ Modern.js 微前端方案是基于 [Garfish](https://garfishjs.org/) 封装的,提供了一些更开箱即用的使用方式。
7
7
 
8
8
  当你的主应用和子应用不全是 Modern.js 应用的时候,可以参考这片文档。
9
9
 
@@ -12,7 +12,7 @@ Modern.js 微前端方案是基于 [Garfish](https://garfish.top/) 封装的,
12
12
 
13
13
  ## 子应用是 Modern.js
14
14
 
15
- **Modern.js** 子应用编译后会生成标准的 [Garfish 子应用导出](https://garfish.top/quick-start#%E5%AD%90%E5%BA%94%E7%94%A8)。
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
16
  所以可以直接接入标准的微前端主应用。
17
17
 
18
18
  :::info 注
@@ -21,4 +21,4 @@ Modern.js 微前端方案是基于 [Garfish](https://garfish.top/) 封装的,
21
21
 
22
22
  ## 主应用是 Modern.js
23
23
 
24
- 主应用是 **Modern.js**,子应用用的其它技术栈。子应用按照 [Garfish 子应用标准](https://garfish.top/quick-start#%E5%AD%90%E5%BA%94%E7%94%A8) 开发即可。
24
+ 主应用是 **Modern.js**,子应用用的其它技术栈。子应用按照 [Garfish 子应用标准](https://www.garfishjs.org/guide/demo/react) 开发即可。
@@ -81,7 +81,7 @@ export default countModel;
81
81
 
82
82
  接下来,我们演示如何在组件中使用 Model。
83
83
 
84
- 新建一个组件 Counter,在组件内通过 `useModel` API 使用 countModel
84
+ 新建一个组件 Counter,在组件内通过 `useModel` API 使用 `countModel`:
85
85
 
86
86
  ```js
87
87
  import { useModel } from '@modern-js/runtime/model';
@@ -8,8 +8,8 @@ title: 测试 Model
8
8
  使用测试功能,需要先开启该功能。在项目根目录下,执行 `pnpm run new`,进行如下选择:
9
9
 
10
10
  ```bash
11
- ? 请选择你想要的操作: 启用可选功能
12
- ? 启用可选功能: 启用「单元测试 / 集成测试」功能
11
+ ? 请选择你想要的操作 启用可选功能
12
+ ? 启用可选功能 启用「单元测试 / 集成测试」功能
13
13
  ```
14
14
 
15
15
  即可开启测试功能支持。
@@ -57,8 +57,8 @@ pnpm run new
57
57
  [INFO] 创建成功!
58
58
  可在新项目的目录下运行以下命令:
59
59
  pnpm run dev # 按开发环境的要求,运行和调试项目
60
- pnpm run build # 按产品环境的要求,构建项目
61
- pnpm run start # 按产品环境的要求,运行项目
60
+ pnpm run build # 按生产环境的要求,构建项目
61
+ pnpm run serve # 按生产环境的要求,运行项目
62
62
  pnpm run lint # 检查和修复所有代码
63
63
  pnpm run new # 继续创建更多项目要素,比如应用入口
64
64
  ```
@@ -2,7 +2,7 @@
2
2
  sidebar_position: 1
3
3
  ---
4
4
 
5
- # 简介
5
+ # Monorepo 工程介绍
6
6
 
7
7
  Modern.js 提供了对于 Monorepo 工程方案的支持,其主要通过 `@modern-js/monorepo-tools` 来提供功能。
8
8
 
@@ -4,75 +4,6 @@ sidebar_position: 1
4
4
 
5
5
  # 依赖安装问题
6
6
 
7
- ### 如何升级项目中的 Modern.js 到最新版本?
8
-
9
- 对于使用 yarn 的项目,可以执行:
10
-
11
- ```bash
12
- yarn upgrade --scope @modern-js
13
- ```
14
-
15
- 对于使用 pnpm 的项目,可以执行:
16
-
17
- ```bash
18
- pnpm update "@modern-js/*" -r
19
- ```
20
-
21
- 如果需要了解不同版本之间的更新内容,可以查看:
22
-
23
- - Modern.js 发版日志:[Modern.js/releases](https://github.com/modern-js-dev/modern.js/releases)。
24
- - 各个 package 下的 `CHANGELOG.md` 文件。
25
-
26
- ---
27
-
28
- ### 如何锁定项目中的某个依赖版本?
29
-
30
- 如果需要锁定项目中的某个依赖的版本,可以通过以下方式进行操作:
31
-
32
- **yarn**
33
-
34
- 对于使用 yarn 的项目,请在**项目根目录**的 `package.json` 中添加以下配置,然后重新执行 `yarn`:
35
-
36
- ```json
37
- {
38
- "resolutions": {
39
- "package-name": "^1.0.0"
40
- }
41
- }
42
- ```
43
-
44
- **pnpm**
45
-
46
- 对于使用 pnpm 的项目,请在**项目根目录**的 `package.json` 中添加以下配置,然后重新执行 `pnpm i`:
47
-
48
- ```json
49
- {
50
- "pnpm": {
51
- "overrides": {
52
- "package-name": "^1.0.0"
53
- }
54
- }
55
- }
56
- ```
57
-
58
- **npm**
59
-
60
- 对于使用 npm 的项目,请在**项目根目录**的 `package.json` 中添加以下配置,然后重新执行 `npm i`:
61
-
62
- ```json
63
- {
64
- "overrides": {
65
- "package-name": "^1.0.0"
66
- }
67
- }
68
- ```
69
-
70
- :::info
71
- 对于 monorepo 场景,只能在项目根目录的 `package.json` 中锁定依赖版本,并且会影响 monorepo 中的所有 package。
72
- :::
73
-
74
- ---
75
-
76
7
  ### 如何查看项目里某个依赖实际安装的版本?
77
8
 
78
9
  可以使用包管理器自带的 `ls` 命令来查看项目里依赖的版本。
@@ -1,5 +1,5 @@
1
1
  {
2
- "label": "创建第一个应用",
2
+ "label": "联系人列表应用",
3
3
  "position": 2,
4
4
  "collapsed": false
5
5
  }
@@ -0,0 +1,99 @@
1
+ ---
2
+ title: 创建项目
3
+ ---
4
+
5
+ 从这一章节开始,我们将进入实战教程部分。在实战教程中,我们将会从环境准备开始,从简单到复杂,一步一步搭建一个真实的项目。
6
+
7
+ ## 环境准备
8
+
9
+ import Prerequisites from '@site-docs/components/prerequisites.md'
10
+
11
+ <Prerequisites />
12
+
13
+ ## 初始化项目
14
+
15
+ 我们创建新的目录,通过命令行工具初始化项目:
16
+
17
+ ```bash
18
+ mkdir myapp && cd myapp
19
+ npx @modern-js/create
20
+ ```
21
+
22
+ import InitApp from '@site-docs/components/init-app.md'
23
+
24
+ <InitApp />
25
+
26
+ ## 调试项目
27
+
28
+ import DebugApp from '@site-docs/components/debug-app.md'
29
+
30
+ <DebugApp />
31
+
32
+ ## 修改代码
33
+
34
+ 我们将原本的示例代码删除,替换成一个简单的联系人列表:
35
+
36
+ ```tsx title="src/routes/page.tsx"
37
+ const getAvatar = (users: Array<{ name: string; email: string }>) =>
38
+ users.map(user => ({
39
+ ...user,
40
+ avatar: `https://avatars.dicebear.com/v2/identicon/${user.name}.svg`,
41
+ }));
42
+
43
+ const mockData = getAvatar([
44
+ { name: 'Thomas', email: 'w.kccip@bllmfbgv.dm' },
45
+ { name: 'Chow', email: 'f.lfqljnlk@ywoefljhc.af' },
46
+ { name: 'Bradley', email: 'd.wfovsqyo@gpkcjwjgb.fr' },
47
+ { name: 'Davis', email: '"t.kqkoj@utlkwnpwk.nu' },
48
+ ]);
49
+
50
+ function App() {
51
+ return (
52
+ <ul>
53
+ {mockData.map(({ name, avatar, email }) => (
54
+ <li key={name}>
55
+ <img src={avatar} width={60} height={60} /> ---
56
+ <span>{name}</span> ---
57
+ <span>{email}</span>
58
+ </li>
59
+ ))}
60
+ </ul>
61
+ );
62
+ }
63
+
64
+ export default App;
65
+ ```
66
+
67
+ 删除多余的 css 文件,保持目录没有多余的文件:
68
+
69
+ ```bash
70
+ rm src/routes/index.css
71
+ ```
72
+
73
+ 因为框架默认支持 [HMR](https://webpack.js.org/concepts/hot-module-replacement/),可以看到 `http://localhost:8080/` 里的内容会自动更新为:
74
+
75
+ ![result](https://lf3-static.bytednsdoc.com/obj/eden-cn/zq-uylkvt/ljhwZthlaukjlkulzlp/screenshot-20221214-141909.png)
76
+
77
+ 此刻的页面还没有样式。下一章节将展开这部分的内容。
78
+
79
+ ## 开启 SSR
80
+
81
+ 接下来,我们修改项目中的 `modern.config.ts`,开启 SSR 能力:
82
+
83
+ ```ts
84
+ import AppToolsPlugin, { defineConfig } from '@modern-js/app-tools';
85
+
86
+ // https://modernjs.dev/docs/apis/app/config
87
+ export default defineConfig({
88
+ runtime: {
89
+ router: true,
90
+ state: true,
91
+ },
92
+ server: {
93
+ ssr: true,
94
+ },
95
+ plugins: [AppToolsPlugin()],
96
+ });
97
+ ```
98
+
99
+ 重新执行 `pnpm run dev`,可以发现项目已经在服务端完成了页面渲染。
@@ -1,29 +1,27 @@
1
1
  ---
2
- title: 使用主流组件库
2
+ title: 编写 UI 组件
3
3
  ---
4
4
 
5
- 上一章节中,我们学习了如何使用 ES6+ 与 TypeScript 语法编写 Modern.js 应用。
5
+ 上一章节中,我们学习了如何初始化项目,并使用配置修改 Modern.js 的默认行为。
6
6
 
7
- 这一章节中,我们继续沿用上一章节的项目代码(注意是 TS 代码,不是 ES6+ 代码),继续完善联系人列表。
7
+ 这一章节中,我们继续沿用上一章节的项目代码,继续完善联系人列表。
8
8
 
9
- 为了做更好的 UI 展示和交互,我们引入主流的组件库 Ant Design 来开发,使用 List 组件来代替原始的列表。
10
-
11
- 先添加依赖:
9
+ 为了做更好的 UI 展示和交互,我们引入组件库 [Antd](https://ant.design/index-cn) 来开发,使用 `<List>` 组件来代替原始的列表。先添加依赖:
12
10
 
13
11
  ```bash
14
12
  pnpm add antd
15
13
  ```
16
14
 
17
- 修改 `src/routes/page.tsx`,在顶部导入组件:
15
+ 修改 `src/routes/page.tsx`,在顶部导入组件:
18
16
 
19
- ```js
17
+ ```ts
20
18
  import { List } from 'antd';
21
19
  ```
22
20
 
23
- 修改 Index 组件的实现:
21
+ 修改 `<App>` 组件的实现:
24
22
 
25
- ```ts
26
- function Index() {
23
+ ```tsx
24
+ function App() {
27
25
  return (
28
26
  <div>
29
27
  <List
@@ -45,7 +43,7 @@ function Index() {
45
43
 
46
44
  执行 `pnpm run dev`,查看运行结果:
47
45
 
48
- ![result](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/05/result.png)
46
+ ![result](https://lf3-static.bytednsdoc.com/obj/eden-cn/nuvjhpqnuvr/modern-website/tutorials/c02-antd-result.png)
49
47
 
50
48
  可以看到 Ant Design 导出的组件,已经具备了完整的样式。
51
49
 
@@ -53,6 +51,6 @@ function Index() {
53
51
  Modern.js 会[自动按需导入 Ant Design 组件需要的 CSS](https://github.com/ant-design/babel-plugin-import)。
54
52
  :::
55
53
 
56
- ---
57
-
58
- > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c05/hello-modern)。
54
+ :::note
55
+ 我们也可以使用其他组件库来实现同样的功能,例如 [Arco Design](https://arco.design/)。
56
+ :::