@modern-js/main-doc 2.0.0-canary.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/_category_.json +1 -1
  3. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +62 -31
  4. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/router/router.md +174 -375
  5. package/en/docusaurus-plugin-content-docs/current/components/enable-micro-frontend.md +13 -0
  6. package/en/docusaurus-plugin-content-docs/current/components/micro-master-manifest-config.md +15 -0
  7. package/en/docusaurus-plugin-content-docs/current/components/micro-runtime-config.md +18 -0
  8. package/en/docusaurus-plugin-content-docs/current/components/router-legacy-tip.md +1 -0
  9. package/en/docusaurus-plugin-content-docs/current/configure/app/auto-load-plugin.md +62 -0
  10. package/en/docusaurus-plugin-content-docs/current/configure/app/deploy/microFrontend.md +54 -0
  11. package/en/docusaurus-plugin-content-docs/current/configure/app/output/ssg.md +226 -0
  12. package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/master-app.md +20 -39
  13. package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +17 -4
  14. package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/state.md +17 -4
  15. package/en/docusaurus-plugin-content-docs/current/configure/app/server/enable-framework-ext.md +47 -0
  16. package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/function.md +2 -2
  17. package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +6 -2
  18. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/_category_.json +4 -0
  19. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +1 -1
  20. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +0 -2
  21. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/_category_.json +5 -0
  22. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/extend.md +162 -0
  23. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook-list.md +803 -0
  24. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook.md +169 -0
  25. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/implement.md +247 -0
  26. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/introduction.md +49 -0
  27. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/plugin-api.md +116 -0
  28. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/relationship.md +118 -0
  29. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +1 -1
  30. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +3 -1
  31. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +1 -9
  32. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +2 -2
  33. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/_category_.json +4 -0
  34. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
  35. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c02-development.md +191 -0
  36. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
  37. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
  38. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/micro-frontend/c05-mixed-stack.md +24 -0
  39. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/_category_.json +4 -0
  40. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/auto-actions.md +90 -0
  41. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/computed-state.md +151 -0
  42. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/define-model.md +66 -0
  43. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/faq.md +43 -0
  44. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/manage-effects.md +259 -0
  45. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/model-communicate.md +219 -0
  46. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/performance.md +173 -0
  47. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/quick-start.md +116 -0
  48. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/redux-integration.md +21 -0
  49. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/test-model.md +43 -0
  50. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/typescript-best-practice.md +71 -0
  51. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-model.md +244 -0
  52. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/model/use-out-of-modernjs.md +51 -0
  53. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/_category_.json +5 -0
  54. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c01-start.md +99 -0
  55. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c02-component.md +56 -0
  56. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c03-css.md +324 -0
  57. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c04-routes.md +169 -0
  58. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c05-loader.md +82 -0
  59. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c06-model.md +260 -0
  60. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c07-container.md +283 -0
  61. package/en/docusaurus-plugin-content-docs/current/tutorials/first-app/c08-entries.md +137 -0
  62. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/_category_.json +1 -1
  63. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +5 -3
  64. package/package.json +4 -4
  65. package/zh/apis/app/runtime/core/use-module-apps.md +2 -0
  66. package/zh/apis/app/runtime/router/router.md +169 -371
  67. package/zh/components/micro-master-manifest-config.md +15 -0
  68. package/zh/components/router-legacy-tip.md +1 -0
  69. package/zh/configure/app/auto-load-plugin.md +62 -0
  70. package/zh/configure/app/deploy/microFrontend.md +0 -10
  71. package/zh/configure/app/output/ssg.md +1 -5
  72. package/zh/configure/app/runtime/master-app.md +4 -18
  73. package/zh/configure/app/runtime/router.md +19 -4
  74. package/zh/configure/app/runtime/state.md +7 -7
  75. package/zh/configure/app/server/enable-framework-ext.md +47 -0
  76. package/zh/configure/app/server/port.md +1 -1
  77. package/zh/configure/app/tools/_category_.json +1 -1
  78. package/zh/guides/advanced-features/eslint.md +2 -1
  79. package/zh/guides/advanced-features/ssg.md +4 -0
  80. package/zh/guides/basic-features/data-fetch.md +1 -1
  81. package/zh/guides/basic-features/env-vars.md +1 -1
  82. package/zh/guides/basic-features/routes.md +0 -3
  83. package/zh/guides/topic-detail/generator/config/module.md +3 -1
  84. package/zh/guides/topic-detail/generator/config/mwa.md +1 -9
  85. package/zh/guides/topic-detail/model/quick-start.md +1 -1
  86. package/zh/tutorials/first-app/c06-model.md +5 -1
  87. package/zh/tutorials/first-app/c08-entries.md +1 -1
  88. package/zh/tutorials/foundations/introduction.md +5 -3
  89. package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +0 -12
  90. package/en/docusaurus-plugin-content-docs/current/configure/app/bff/fetcher.md +0 -28
  91. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -31
  92. package/en/docusaurus-plugin-content-docs/current/guides/overview.md +0 -11
  93. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/basic.md +0 -8
  94. package/zh/apis/app/overview.md +0 -11
  95. package/zh/apis/monorepo/overview.md +0 -11
  96. package/zh/configure/app/bff/fetcher.md +0 -31
  97. package/zh/configure/app/dev/with-master-app.md +0 -32
  98. package/zh/guides/overview.md +0 -11
  99. package/zh/tutorials/foundations/basic.md +0 -8
@@ -0,0 +1,803 @@
1
+ ---
2
+ title: Hook 列表
3
+ sidebar_position: 8
4
+ ---
5
+
6
+ 在 Modern.js 中暴露了三类插件:CLI、Runtime、Server。下面列举下各类中的 Hook:
7
+
8
+ ## CLI
9
+
10
+ ### `config`
11
+
12
+ - 功能:收集配置
13
+ - 执行阶段:解析完 `modern.config.ts` 中的配置之后
14
+ - Hook 模型:ParallelWorkflow
15
+ - 类型:`ParallelWorkflow<void, unknown>`
16
+ - 使用示例:
17
+
18
+ ```ts
19
+ import type { CliPlugin } from '@modern-js/core';
20
+
21
+ export default (): CliPlugin => ({
22
+ setup(api) {
23
+ return {
24
+ config: () => {
25
+ return {
26
+ /** some config */
27
+ };
28
+ },
29
+ };
30
+ },
31
+ });
32
+ ```
33
+
34
+ 这里返回的配置信息,会被收集和统一处理合并。
35
+
36
+ ### `validateSchema`
37
+
38
+ - 功能:收集各个插件中配置的用来校验用户配置的 [JSON Schema](https://json-schema.org/)
39
+ - 执行阶段:`config` Hook 运行完之后。
40
+ - Hook 模型:ParallelWorkflow
41
+ - 类型:`ParallelWorkflow<void, unknown>`
42
+ - 使用示例:
43
+
44
+ ```ts
45
+ import type { CliPlugin } from '@modern-js/core';
46
+
47
+ export default (): CliPlugin => ({
48
+ setup(api) {
49
+ return {
50
+ validateSchema: () => {
51
+ return {
52
+ // target is field
53
+ target: 'foo',
54
+ schema: {
55
+ type: 'string',
56
+ },
57
+ };
58
+ },
59
+ };
60
+ },
61
+ });
62
+ ```
63
+
64
+ 这里返回的 JSON Schema 会用来校验 `modern.config.js` 中的配置信息。
65
+
66
+ 比如这里返回:
67
+
68
+ ```json
69
+ {
70
+ "target": "foo",
71
+ "schema": {
72
+ "type": "string"
73
+ }
74
+ }
75
+ ```
76
+
77
+ 就可以在 `modern.config.ts` 中这样配置:
78
+
79
+ ```ts title="modern.config.ts"
80
+ export default defineConfig({
81
+ foo: 'test',
82
+ });
83
+ ```
84
+
85
+ 如果是别的类型,校验就不通过会报错,比如这样:
86
+
87
+ ```ts title="modern.config.ts"
88
+ export default defineConfig({
89
+ foo: {},
90
+ });
91
+ ```
92
+
93
+ 就会报错:
94
+
95
+ ```sh
96
+ $ modern dev
97
+ 1 | {
98
+ > 2 | "foo": {},
99
+ | ^^^^^ Property foo is not expected to be here
100
+ ```
101
+
102
+ ### `prepare`
103
+
104
+ - 功能:运行主流程的前置准备流程
105
+ - 执行阶段:校验完配置之后
106
+ - Hook 模型:AsyncWorkflow
107
+ - 类型:`AsyncWorkflow<void, void>`
108
+ - 使用示例:
109
+
110
+ ```ts
111
+ import type { CliPlugin } from '@modern-js/core';
112
+
113
+ export default (): CliPlugin => ({
114
+ setup(api) {
115
+ return {
116
+ prepare: () => {
117
+ // do something
118
+ },
119
+ };
120
+ },
121
+ });
122
+ ```
123
+
124
+ ### `commands`
125
+
126
+ - 功能:为 command 添加新的命令
127
+ - 执行阶段:`prepare` Hook 运行完之后
128
+ - Hook 模型:AsyncWorkflow
129
+ - 类型:`AsyncWorkflow<{ program: Command; }, void>`
130
+ - 使用示例:
131
+
132
+ ```ts
133
+ import type { CliPlugin } from '@modern-js/core';
134
+
135
+ export default (): CliPlugin => ({
136
+ setup(api) {
137
+ return {
138
+ commands: ({ program }) => {
139
+ program.command('foo').action(async () => {
140
+ // do something
141
+ console.log('foo');
142
+ });
143
+ },
144
+ };
145
+ },
146
+ });
147
+ ```
148
+
149
+ 将上面这个插件添加到 `modern.config.ts` 中:
150
+
151
+ ```ts title="modern.config.ts"
152
+ import MyPlugin from './config/plugin/MyPlugin';
153
+
154
+ export default defineConfig({
155
+ plugins: [MyPlugin()],
156
+ });
157
+ ```
158
+
159
+ 运行 `modern foo` 就可以看到控制台输出:
160
+
161
+ ```sh
162
+ $ modern foo
163
+ foo
164
+ ```
165
+
166
+ ### `beforeExit`
167
+
168
+ - 功能:在退出进程前,重置一些文件状态
169
+ - 执行阶段:进程退出之前
170
+ - Hook 模型:AsyncWorkflow
171
+ - 类型:`AsyncWorkflow<void, void>`
172
+ - 使用示例:
173
+
174
+ ```ts
175
+ import type { CliPlugin } from '@modern-js/core';
176
+
177
+ export default (): CliPlugin => ({
178
+ setup(api) {
179
+ return {
180
+ beforeExit: () => {
181
+ // do something
182
+ },
183
+ };
184
+ },
185
+ });
186
+ ```
187
+
188
+ ### `beforeDev`
189
+
190
+ - 功能:运行 dev 主流程的之前的任务
191
+ - 执行阶段:`dev` 命令运行时,项目开始启动前执行
192
+ - Hook 模型:AsyncWorkflow
193
+ - 类型:`AsyncWorkflow<void, unknown>`
194
+ - 使用示例:
195
+
196
+ ```ts
197
+ import type { CliPlugin } from '@modern-js/core';
198
+
199
+ export default (): CliPlugin => ({
200
+ setup(api) {
201
+ return {
202
+ beforeDev: () => {
203
+ // do something
204
+ },
205
+ };
206
+ },
207
+ });
208
+ ```
209
+
210
+ ### `afterDev`
211
+
212
+ - 功能:运行 dev 主流程的之后的任务
213
+ - 执行阶段:`dev` 命令运行时,项目启动完成之后执行
214
+ - Hook 模型:AsyncWorkflow
215
+ - 类型:`AsyncWorkflow<void, unknown>`
216
+ - 使用示例:
217
+
218
+ ```ts
219
+ import type { CliPlugin } from '@modern-js/core';
220
+
221
+ export default (): CliPlugin => ({
222
+ setup(api) {
223
+ return {
224
+ afterDev: () => {
225
+ // do something
226
+ },
227
+ };
228
+ },
229
+ });
230
+ ```
231
+
232
+ ### `beforeCreateCompiler`
233
+
234
+ - 功能:在中间件函数中可以拿到创建 Webpack Compiler 的 Webpack 配置
235
+ - 执行阶段:创建 Webpack Compiler 之前执行
236
+ - Hook 模型:AsyncWorkflow
237
+ - 类型:`AsyncWorkflow<{ webpackConfigs: Configuration[];}, unknown>`
238
+ - 使用示例:
239
+
240
+ ```ts
241
+ import type { CliPlugin } from '@modern-js/core';
242
+
243
+ export default (): CliPlugin => ({
244
+ setup(api) {
245
+ return {
246
+ beforeCreateCompiler: ({ webpackConfigs }) => {
247
+ // do something
248
+ },
249
+ };
250
+ },
251
+ });
252
+ ```
253
+
254
+ ### `afterCreateCompiler`
255
+
256
+ - 功能:在中间件函数中可以拿到创建的 Webpack Compiler
257
+ - 执行阶段:创建 Webpack Compiler 之后执行
258
+ - Hook 模型:AsyncWorkflow
259
+ - 类型:`AsyncWorkflow<{ compiler: Compiler | MultiCompiler | undefined; }, unknown>`
260
+ - 使用示例:
261
+
262
+ ```ts
263
+ import type { CliPlugin } from '@modern-js/core';
264
+
265
+ export default (): CliPlugin => ({
266
+ setup(api) {
267
+ return {
268
+ afterCreateCompiler: ({ compiler }) => {
269
+ // do something
270
+ },
271
+ };
272
+ },
273
+ });
274
+ ```
275
+
276
+ ### `beforePrintInstructions`
277
+
278
+ - 功能:在中间件函数中可以拿到即将打印的日志信息,并对其进行修改
279
+ - 执行阶段:打印日志信息之前执行
280
+ - Hook 模型:AsyncWaterfall
281
+ - 类型:`AsyncWaterfall<{ instructions: string }>`
282
+ - 使用示例:
283
+
284
+ ```ts
285
+ import type { CliPlugin } from '@modern-js/core';
286
+
287
+ export default (): CliPlugin => ({
288
+ setup(api) {
289
+ return {
290
+ beforePrintInstructions: ({ instructions }) => {
291
+ // do something
292
+ return {
293
+ instructions: [...instructions, 'some new message'],
294
+ };
295
+ },
296
+ };
297
+ },
298
+ });
299
+ ```
300
+
301
+ ### `beforeBuild`
302
+
303
+ - 功能:运行 build 主流程的之前的任务,可以拿到构建的 Webpack 配置
304
+ - 执行阶段:`build` 命令运行时,项目构建启动前执行
305
+ - Hook 模型:AsyncWorkflow
306
+ - 类型:`AsyncWorkflow<{ webpackConfigs: Configuration[]; }>`
307
+ - 使用示例:
308
+
309
+ ```ts
310
+ import type { CliPlugin } from '@modern-js/core';
311
+
312
+ export default (): CliPlugin => ({
313
+ setup(api) {
314
+ return {
315
+ beforeBuild: () => {
316
+ // do something
317
+ },
318
+ };
319
+ },
320
+ });
321
+ ```
322
+
323
+ ### `afterBuild`
324
+
325
+ - 功能:运行 build 主流程的之后的任务
326
+ - 执行阶段:`build` 命令运行时,项目构建完成之后执行
327
+ - Hook 模型:AsyncWorkflow
328
+ - 类型:`AsyncWorkflow<void, unknown>`
329
+ - 使用示例:
330
+
331
+ ```ts
332
+ import type { CliPlugin } from '@modern-js/core';
333
+
334
+ export default (): CliPlugin => ({
335
+ setup(api) {
336
+ return {
337
+ afterBuild: () => {
338
+ // do something
339
+ },
340
+ };
341
+ },
342
+ });
343
+ ```
344
+
345
+ ### `modifyEntryImports`
346
+
347
+ - 功能:用于修改、添加生成入口文件中的 `import` 语句
348
+ - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
349
+ - Hook 模型:AsyncWaterfall
350
+ - 类型:`AsyncWaterfall<{ imports: ImportStatement[]; entrypoint: Entrypoint; }>`
351
+ - 使用示例:
352
+
353
+ ```ts
354
+ import type { CliPlugin } from '@modern-js/core';
355
+
356
+ export default (): CliPlugin => ({
357
+ setup(api) {
358
+ return {
359
+ modifyEntryImports({ entrypoint, imports }) {
360
+ // 添加 `import React from 'React'`
361
+ imports.push({
362
+ value: 'react',
363
+ specifiers: [
364
+ {
365
+ imported: 'unmountComponentAtNode',
366
+ },
367
+ ],
368
+ });
369
+
370
+ return { entrypoint, imports };
371
+ },
372
+ };
373
+ },
374
+ });
375
+ ```
376
+
377
+ ### `modifyEntryExport`
378
+
379
+ - 功能:用于修改生成入口文件中的 `export` 语句
380
+ - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
381
+ - Hook 模型:AsyncWaterfall
382
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; exportStatement: string; }>`
383
+ - 使用示例:
384
+
385
+ ```ts
386
+ import type { CliPlugin } from '@modern-js/core';
387
+
388
+ export default (): CliPlugin => ({
389
+ setup(api) {
390
+ return {
391
+ modifyEntryImports({ entrypoint, exportStatement }) {
392
+ return {
393
+ entrypoint,
394
+ exportStatement: [`export const foo = 'test'`, exportStatement].join(
395
+ '\n',
396
+ ),
397
+ };
398
+ },
399
+ };
400
+ },
401
+ });
402
+ ```
403
+
404
+ ### `modifyEntryRuntimePlugins`
405
+
406
+ - 功能:用于添加、修改生成入口文件中的 [Runtime 插件](#Runtime)
407
+ - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
408
+ - Hook 模型:AsyncWaterfall
409
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; plugins: RuntimePlugin[]; }>`
410
+ - 使用示例:
411
+
412
+ ```ts
413
+ import type { CliPlugin } from '@modern-js/core';
414
+
415
+ export default (): CliPlugin => ({
416
+ setup(api) {
417
+ return {
418
+ modifyEntryRuntimePlugins({ entrypoint, plugins }) {
419
+ const name = 'customPlugin';
420
+ const options = {
421
+ /** 可序列化的内容 */
422
+ };
423
+
424
+ return {
425
+ plugins: [
426
+ ...plugins,
427
+ {
428
+ name,
429
+ options: JSON.stringify(options),
430
+ },
431
+ ],
432
+ };
433
+ },
434
+ };
435
+ },
436
+ });
437
+ ```
438
+
439
+ ### `modifyEntryRenderFunction`
440
+
441
+ - 功能:用于修改生成入口文件中 `render` 函数
442
+ - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
443
+ - Hook 模型:AsyncWaterfall
444
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; code: string; }>`
445
+ - 使用示例:
446
+
447
+ ```ts
448
+ import type { CliPlugin } from '@modern-js/core';
449
+
450
+ export default (): CliPlugin => ({
451
+ setup(api) {
452
+ return {
453
+ modifyEntryRenderFunction({ entrypoint, code }) {
454
+ const customRender = `/** render function body */`;
455
+ return {
456
+ entrypoint,
457
+ code: customRender,
458
+ };
459
+ },
460
+ };
461
+ },
462
+ });
463
+ ```
464
+
465
+ ### `modifyFileSystemRoutes`
466
+
467
+ - 功能:用于修改生成前端页面路由文件中的内容,内容都是需要可序列化的
468
+ - 执行阶段:生成前端路由文件之前,[`prepare`](#prepare) 阶段触发
469
+ - Hook 模型:AsyncWaterfall
470
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; routes: Route[]; }>`
471
+ - 使用示例:
472
+
473
+ ```tsx
474
+ import type { CliPlugin } from '@modern-js/core';
475
+
476
+ export default (): CliPlugin => ({
477
+ setup(api) {
478
+ return {
479
+ modifyFileSystemRoutes({ entrypoint, routes }) {
480
+ return {
481
+ entrypoint,
482
+ routes: [
483
+ ...routes,
484
+ {
485
+ path: '/custom_page',
486
+ component: require.resolve('./Component'),
487
+ exact: true,
488
+ },
489
+ ],
490
+ };
491
+ },
492
+ };
493
+ },
494
+ });
495
+ ```
496
+
497
+ 这样就为前端新增了一个页面路由。
498
+
499
+ ### `modifyServerRoutes`
500
+
501
+ - 功能:用于修改生成服务器路由中的内容
502
+ - 执行阶段:生成 Server 路由文件之前,[`prepare`](#prepare) 阶段触发
503
+ - Hook 模型:AsyncWaterfall
504
+ - 类型:`AsyncWaterfall<{ routes: ServerRoute[]; }>`
505
+ - 使用示例:
506
+
507
+ ```ts
508
+ import type { CliPlugin } from '@modern-js/core';
509
+
510
+ export default (): CliPlugin => ({
511
+ setup(api) {
512
+ return {
513
+ modifyServerRoutes({ routes }) {
514
+ return {
515
+ routes: [
516
+ ...routes,
517
+ {
518
+ urlPath: '/api/foo',
519
+ isApi: true,
520
+ entryPath: '',
521
+ isSPA: false,
522
+ isSSR: false,
523
+ },
524
+ ],
525
+ };
526
+ },
527
+ };
528
+ },
529
+ });
530
+ ```
531
+
532
+ ### `modifyAsyncEntry`
533
+
534
+ - 功能:用于修改包裹入口文件的异步模块,参见 [source.enableAsyncEntry](/docs/configure/app/source/enable-async-entry)
535
+ - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
536
+ - Hook 模型:AsyncWaterfall
537
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; code: string; }>`
538
+ - 使用示例:
539
+
540
+ ```ts
541
+ import type { CliPlugin } from '@modern-js/core';
542
+
543
+ export default (): CliPlugin => ({
544
+ setup(api) {
545
+ return {
546
+ modifyAsyncEntry({ entrypoint, code }) {
547
+ const customCode = `console.log('hello');`;
548
+ return {
549
+ entrypoint,
550
+ code: `${customCode}${code}`,
551
+ };
552
+ },
553
+ };
554
+ },
555
+ });
556
+ ```
557
+
558
+ ### `htmlPartials`
559
+
560
+ - 功能:用于定制生成的 HTML 页面模版
561
+ - 执行阶段:[`prepare`](#prepare) 阶段触发
562
+ - Hook 模型:AsyncWaterfall
563
+ - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; partials: HtmlPartials; }>`
564
+ - 使用示例:
565
+
566
+ ```ts
567
+ import type { CliPlugin } from '@modern-js/core';
568
+
569
+ export default (): CliPlugin => ({
570
+ setup(api) {
571
+ return {
572
+ async htmlPartials({ entrypoint, partials }) {
573
+ partials.head.push('<script>console.log("test")</script>');
574
+ return {
575
+ entrypoint,
576
+ partials,
577
+ };
578
+ },
579
+ };
580
+ },
581
+ });
582
+ ```
583
+
584
+ 这样就为 HTML 模版中新增了一个 Script 标签。
585
+
586
+ ## Server
587
+
588
+ :::note
589
+ 目前 Server 插件还未完全开放,API 不保证稳定,使用需谨慎。
590
+ :::
591
+
592
+ 应用工程中的 Server 部分也支持了插件。其中的 Hook 将会提供一些特定阶段调用和特殊功能的 Hook。
593
+
594
+ ### `create`
595
+
596
+ - 功能:在中间件函数中会拿到 Server 初始化用到的指标测量工具配置 `measureOptions` 和日志工具配置 `loggerOptions`,并返回自定义的指标测量工具 `measure` 和日志工具配置 `logger`
597
+ - 执行阶段:Server 初始化
598
+ - Hook 模型:AsyncPipeline
599
+ - 类型:`AsyncPipeline<ServerInitInput, InitExtension>`
600
+ - 使用示例:
601
+
602
+ ```ts
603
+ import type { ServerPlugin } from '@modern-js/server-core';
604
+
605
+ export default (): ServerPlugin => ({
606
+ setup(api) {
607
+ return {
608
+ create: ({ measureOptions, loggerOptions }) => {
609
+ // do something
610
+ },
611
+ };
612
+ },
613
+ });
614
+ ```
615
+
616
+ ### `prepareWebServer`
617
+
618
+ - 功能:设置 Web 路由的处理函数,在中间件函数中可以拿到 Web Server 的前置中间件
619
+ - 执行阶段:在请求到达的时候
620
+ - Hook 模型:AsyncPipeline
621
+ - 类型:`AsyncPipeline<WebServerStartInput, Adapter>`
622
+ - 使用示例:
623
+
624
+ ```ts
625
+ import type { ServerPlugin } from '@modern-js/server-core';
626
+
627
+ export default (): ServerPlugin => ({
628
+ setup(api) {
629
+ return {
630
+ prepareWebServer: ({ middleware }) => {
631
+ // do something
632
+
633
+ return (req, res) => {
634
+ // do response
635
+ };
636
+ },
637
+ };
638
+ },
639
+ });
640
+ ```
641
+
642
+ ### `prepareApiServer`
643
+
644
+ - 功能:设置 API 路由的处理函数,在中间件函数中可以拿到 API Server 的前置中间件
645
+ - 执行阶段:请求到达并且 match bff basename 之后执行
646
+ - Hook 模型:AsyncPipeline
647
+ - 类型:`AsyncPipeline<APIServerStartInput, Adapter>`
648
+ - 使用示例:
649
+
650
+ ```ts
651
+ import type { ServerPlugin } from '@modern-js/server-core';
652
+
653
+ export default (): ServerPlugin => ({
654
+ setup(api) {
655
+ return {
656
+ prepareApiServer: ({ middleware }) => {
657
+ // do something
658
+
659
+ return (req, res) => {
660
+ // do response
661
+ };
662
+ },
663
+ };
664
+ },
665
+ });
666
+ ```
667
+
668
+ ## Runtime
669
+
670
+ :::note
671
+ 目前 Runtime 插件还未完全开放,API 不保证稳定,使用需谨慎。
672
+ :::
673
+
674
+ Runtime 插件主要用于开发者修改需要渲染的组件与 Element 和定制服务器端、客户端的渲染过程。
675
+
676
+ ### `init`
677
+
678
+ - 功能:执行 `App.init`
679
+ - 执行阶段:渲染(SSR/CSR)
680
+ - Hook 模型:AsyncPipeline
681
+ - 类型:`AsyncPipeline<{ context: RuntimeContext; }, unknown>`
682
+ - 使用示例:
683
+
684
+ ```ts
685
+ import type { Plugin } from '@modern-js/runtime';
686
+
687
+ export default (): Plugin => ({
688
+ setup(api) {
689
+ return {
690
+ init({ context }, next) {
691
+ // do something
692
+ return next({ context });
693
+ },
694
+ };
695
+ },
696
+ });
697
+ ```
698
+
699
+ ### `hoc`
700
+
701
+ - 功能:修改需要渲染的组件
702
+ - 执行阶段:渲染(SSR/CSR)
703
+ - Hook 模型:Pipeline
704
+ - 类型:`Pipeline<{ App: React.ComponentType<any>; }, React.ComponentType<any>>`
705
+ - 使用示例:
706
+
707
+ ```ts
708
+ import { createContext } from 'react';
709
+ import type { Plugin } from '@modern-js/runtime';
710
+
711
+ export default (): Plugin => ({
712
+ setup(api) {
713
+ const FooContext = createContext('');
714
+ return {
715
+ hoc({ App }, next) {
716
+ return next({
717
+ App: (props: any) => {
718
+ return (
719
+ <FooContext.Provider store={'test'}>
720
+ <App {...props} />
721
+ </FooContext.Provider>
722
+ );
723
+ },
724
+ });
725
+ },
726
+ };
727
+ },
728
+ });
729
+ ```
730
+
731
+ ### `provide`
732
+
733
+ - 功能:修改需要渲染的 Element
734
+ - 执行阶段:渲染(SSR/CSR)
735
+ - Hook 模型:Pipeline
736
+ - 类型:`Pipeline<{ element: JSX.Element; props: AppProps; context: RuntimeContext }, JSX.Element>`
737
+ - 使用示例:
738
+
739
+ ```ts
740
+ import { createContext } from 'react';
741
+ import type { Plugin } from '@modern-js/runtime';
742
+
743
+ export default (): Plugin => ({
744
+ setup(api) {
745
+ const FooContext = createContext('');
746
+
747
+ return {
748
+ provide: ({ element }) => <div>{element}</div>,
749
+ };
750
+ },
751
+ });
752
+ ```
753
+
754
+ ### `client`
755
+
756
+ - 功能:定制客户端渲染流程
757
+ - 执行阶段:在浏览器客户端渲染
758
+ - Hook 模型:AsyncPipeline
759
+ - 类型:`AsyncPipeline<{ App: React.ComponentType<any>; context?: RuntimeContext; rootElement: HTMLElement; }, void>`
760
+ - 使用示例:
761
+
762
+ ```ts
763
+ import ReactDOM from 'react-dom';
764
+ import type { Plugin } from '@modern-js/runtime';
765
+
766
+ export default (): Plugin => ({
767
+ setup(api) {
768
+ return {
769
+ client: async ({ App, rootElement }) => {
770
+ ReactDOM.render(
771
+ React.createElement(App, { context: { foo: 'test' } }),
772
+ rootElement,
773
+ );
774
+ },
775
+ };
776
+ },
777
+ });
778
+ ```
779
+
780
+ ### `server`
781
+
782
+ - 功能:定制服务器端渲染流程
783
+ - 执行阶段:SSR
784
+ - Hook 模型:AsyncPipeline
785
+ - 类型:`AsyncPipeline<{ App: React.ComponentType<any>; context?: RuntimeContext; }, string>`
786
+ - 使用示例:
787
+
788
+ ```ts
789
+ import ReactDomServer from 'react-dom/server';
790
+ import type { Plugin } from '@modern-js/runtime';
791
+
792
+ export default (): Plugin => ({
793
+ setup(api) {
794
+ return {
795
+ server({ App, context }) {
796
+ return ReactDomServer.renderToString(
797
+ React.createElement(App, { context: { foo: 'test' } }),
798
+ );
799
+ },
800
+ };
801
+ },
802
+ });
803
+ ```