@modern-js/main-doc 2.65.5 → 2.66.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/docs/en/community/blog/v2-release-note.mdx +1 -1
  2. package/docs/en/configure/app/plugins.mdx +2 -2
  3. package/docs/en/configure/app/tools/esbuild.mdx +1 -1
  4. package/docs/en/configure/app/tools/swc.mdx +1 -1
  5. package/docs/en/plugin/_meta.json +8 -7
  6. package/docs/en/plugin/cli-plugins/_meta.json +1 -1
  7. package/docs/en/plugin/cli-plugins/api.mdx +617 -0
  8. package/docs/en/plugin/cli-plugins/life-cycle.mdx +139 -0
  9. package/docs/en/plugin/cli-plugins/migration.mdx +98 -0
  10. package/docs/en/plugin/introduction.mdx +119 -47
  11. package/docs/en/plugin/official/_meta.json +12 -0
  12. package/docs/en/plugin/official/cli-plugins/_meta.json +1 -0
  13. package/docs/en/plugin/official/cli-plugins.mdx +6 -0
  14. package/docs/en/plugin/official/rsbuild-plugins.mdx +3 -0
  15. package/docs/en/plugin/plugin-system.mdx +237 -0
  16. package/docs/en/plugin/runtime-plugins/_meta.json +1 -0
  17. package/docs/en/plugin/runtime-plugins/api.mdx +165 -0
  18. package/docs/en/plugin/runtime-plugins/life-cycle.mdx +29 -0
  19. package/docs/en/plugin/runtime-plugins/migration.mdx +101 -0
  20. package/docs/en/plugin/server-plugins/api.mdx +3 -0
  21. package/docs/en/plugin/server-plugins/life-cycle.mdx +3 -0
  22. package/docs/zh/community/blog/v2-release-note.mdx +1 -1
  23. package/docs/zh/configure/app/plugins.mdx +2 -2
  24. package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
  25. package/docs/zh/configure/app/tools/swc.mdx +1 -1
  26. package/docs/zh/plugin/_meta.json +8 -7
  27. package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
  28. package/docs/zh/plugin/cli-plugins/api.mdx +617 -0
  29. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +139 -0
  30. package/docs/zh/plugin/cli-plugins/migration.mdx +98 -0
  31. package/docs/zh/plugin/introduction.mdx +92 -20
  32. package/docs/zh/plugin/official/_meta.json +12 -0
  33. package/docs/zh/plugin/official/cli-plugins/_meta.json +1 -0
  34. package/docs/zh/plugin/official/cli-plugins.mdx +6 -0
  35. package/docs/zh/plugin/official/rsbuild-plugins.mdx +3 -0
  36. package/docs/zh/plugin/plugin-system.mdx +239 -0
  37. package/docs/zh/plugin/runtime-plugins/_meta.json +1 -0
  38. package/docs/zh/plugin/runtime-plugins/api.mdx +166 -0
  39. package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +29 -0
  40. package/docs/zh/plugin/runtime-plugins/migration.mdx +101 -0
  41. package/docs/zh/plugin/server-plugins/api.mdx +3 -0
  42. package/docs/zh/plugin/server-plugins/life-cycle.mdx +3 -0
  43. package/i18n.json +4 -0
  44. package/package.json +3 -2
  45. package/src/components/Footer/index.tsx +1 -1
  46. package/src/components/Mermaid/index.tsx +60 -0
  47. package/src/components/Mermaid/style.scss +221 -0
  48. package/docs/en/plugin/cli-plugins.mdx +0 -6
  49. package/docs/en/plugin/plugin-system/_meta.json +0 -10
  50. package/docs/en/plugin/plugin-system/extend.mdx +0 -163
  51. package/docs/en/plugin/plugin-system/hook-list.mdx +0 -711
  52. package/docs/en/plugin/plugin-system/hook.mdx +0 -188
  53. package/docs/en/plugin/plugin-system/implement.mdx +0 -243
  54. package/docs/en/plugin/plugin-system/introduction.mdx +0 -95
  55. package/docs/en/plugin/plugin-system/lifecycle.mdx +0 -16
  56. package/docs/en/plugin/plugin-system/plugin-api.mdx +0 -138
  57. package/docs/en/plugin/plugin-system/relationship.mdx +0 -119
  58. package/docs/en/plugin/rsbuild-plugins.mdx +0 -3
  59. package/docs/zh/plugin/cli-plugins.mdx +0 -6
  60. package/docs/zh/plugin/plugin-system/_meta.json +0 -10
  61. package/docs/zh/plugin/plugin-system/extend.mdx +0 -163
  62. package/docs/zh/plugin/plugin-system/hook-list.mdx +0 -715
  63. package/docs/zh/plugin/plugin-system/hook.mdx +0 -173
  64. package/docs/zh/plugin/plugin-system/implement.mdx +0 -250
  65. package/docs/zh/plugin/plugin-system/introduction.mdx +0 -94
  66. package/docs/zh/plugin/plugin-system/lifecycle.mdx +0 -16
  67. package/docs/zh/plugin/plugin-system/plugin-api.mdx +0 -138
  68. package/docs/zh/plugin/plugin-system/relationship.mdx +0 -119
  69. package/docs/zh/plugin/rsbuild-plugins.mdx +0 -4
  70. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  71. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  72. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  73. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  74. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  75. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
  76. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  77. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  78. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  79. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  80. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  81. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
@@ -1,715 +0,0 @@
1
- ---
2
- sidebar_position: 8
3
- ---
4
-
5
- # Hook 列表
6
-
7
- Modern.js 工程体系中包含三类插件:CLI、Runtime、Server,每一类插件可以使用不同的 Hooks。
8
-
9
- 在本章节中,罗列了所有可用的 Hooks,你可以根据自己的需求来使用对应的 Hook。
10
-
11
- ## CLI Common Hooks
12
-
13
- 以下是通用的 CLI Hooks,可以在 Modern.js Framework 以及 Modern.js Module 中使用。
14
-
15
- ### `config`
16
-
17
- - 功能:收集配置
18
- - 执行阶段:解析完 `modern.config.ts` 中的配置之后
19
- - Hook 模型:`ParallelWorkflow`
20
- - 类型:`ParallelWorkflow<void, unknown>`
21
- - 使用示例:
22
-
23
- ```ts
24
- import type { CliPlugin } from '@modern-js/core';
25
-
26
- export const myPlugin = (): CliPlugin => ({
27
- setup(api) {
28
- return {
29
- config: () => {
30
- return {
31
- /** some config */
32
- };
33
- },
34
- };
35
- },
36
- });
37
- ```
38
-
39
- 如果你需要设置 Modern.js Framework 的配置,请使用 `@modern-js/app-tools` 导出的 `CliPlugin<AppTools>` 类型:
40
-
41
- ```ts
42
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
43
-
44
- export const myPlugin = (): CliPlugin => ({
45
- setup(api) {
46
- return {
47
- config: () => {
48
- return {
49
- output: {
50
- polyfill: 'usage',
51
- },
52
- };
53
- },
54
- };
55
- },
56
- });
57
- ```
58
-
59
- 插件在 `config` hook 中返回的配置信息,会被 Modern.js 统一收集与合并,最终生成一份 `NormalizedConfig`。在进行配置合并时,优先级由高到低依次为:
60
-
61
- 1. 用户在 `modern.config.*` 文件里定义的配置
62
- 2. 插件通过 `config` hook 定义的配置
63
- 3. Modern.js 默认配置。
64
-
65
- ### `prepare`
66
-
67
- - 功能:运行主流程的前置准备流程
68
- - 执行阶段:校验完配置之后
69
- - Hook 模型:`AsyncWorkflow`
70
- - 类型:`AsyncWorkflow<void, void>`
71
- - 使用示例:
72
-
73
- ```ts
74
- import type { CliPlugin } from '@modern-js/core';
75
-
76
- export const myPlugin = (): CliPlugin => ({
77
- setup(api) {
78
- return {
79
- prepare: () => {
80
- // do something
81
- },
82
- };
83
- },
84
- });
85
- ```
86
-
87
- ### `afterPrepare`
88
-
89
- - 功能:运行前置准备流程的之后的任务
90
- - 执行阶段:前置准备流程之后
91
- - Hook 模型:`AsyncWorkflow`
92
- - 类型:`AsyncWorkflow<void, void>`
93
- - 使用示例:
94
-
95
- ```ts
96
- import type { CliPlugin } from '@modern-js/core';
97
-
98
- export const myPlugin = (): CliPlugin => ({
99
- setup(api) {
100
- return {
101
- afterPrepare: () => {
102
- // do something
103
- },
104
- };
105
- },
106
- });
107
- ```
108
-
109
- ### `commands`
110
-
111
- - 功能:为 commander 添加新的 CLI 命令
112
- - 执行阶段:`prepare` Hook 运行完成之后
113
- - Hook 模型:`AsyncWorkflow`
114
- - 类型:`AsyncWorkflow<{ program: Command; }, void>`
115
- - 使用示例:
116
-
117
- ```ts
118
- import type { CliPlugin } from '@modern-js/core';
119
-
120
- export const myPlugin = (): CliPlugin => ({
121
- setup(api) {
122
- return {
123
- commands: ({ program }) => {
124
- program.command('foo').action(async () => {
125
- // do something
126
- console.log('foo');
127
- });
128
- },
129
- };
130
- },
131
- });
132
- ```
133
-
134
- 将上面这个插件添加到 `modern.config.ts` 中:
135
-
136
- ```ts title="modern.config.ts"
137
- import myPlugin from './config/plugin/myPlugin';
138
-
139
- export const myPlugin = defineConfig({
140
- plugins: [myPlugin()],
141
- });
142
- ```
143
-
144
- 运行 `modern foo` 就可以看到控制台输出:
145
-
146
- ```sh
147
- $ modern foo
148
- foo
149
- ```
150
-
151
- ### `beforeExit`
152
-
153
- - 功能:在退出进程前,重置一些文件状态
154
- - 执行阶段:进程退出之前
155
- - Hook 模型:`Workflow`
156
- - 类型:`Workflow<void, void>`
157
- - 使用示例:
158
-
159
- ```ts
160
- import type { CliPlugin } from '@modern-js/core';
161
-
162
- export const myPlugin = (): CliPlugin => ({
163
- setup(api) {
164
- return {
165
- beforeExit: () => {
166
- // do something
167
- },
168
- };
169
- },
170
- });
171
- ```
172
-
173
- :::tip
174
- 由于 Node.js 中退出进程时的回调函数是同步的,所以 `beforeExit` Hook 的类型是 `Workflow`,不能执行异步操作。
175
- :::
176
-
177
- ## CLI Framework Hooks
178
-
179
- 以下是框架的 CLI Hooks,只能在 Modern.js Framework 中使用,不能在 Modern.js Module 中使用。
180
-
181
- 你需要从 `@modern-js/app-tools` 中导入 `CliPlugin` 和 `AppTools` 类型,以获得准确的 Hooks 类型提示。
182
-
183
- ### `beforeDev`
184
-
185
- - 功能:运行 dev 主流程的之前的任务
186
- - 执行阶段:`dev` 命令运行时,项目开始启动前执行
187
- - Hook 模型:`AsyncWorkflow`
188
- - 类型:`AsyncWorkflow<void, unknown>`
189
- - 使用示例:
190
-
191
- ```ts
192
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
193
-
194
- export const myPlugin = (): CliPlugin<AppTools> => ({
195
- setup(api) {
196
- return {
197
- beforeDev: () => {
198
- // do something
199
- },
200
- };
201
- },
202
- });
203
- ```
204
-
205
- ### `afterDev`
206
-
207
- - 功能:运行 dev 主流程的之后的任务
208
- - 执行阶段:运行 `dev` 命令时,每一次编译完成后执行
209
- - Hook 模型:`AsyncWorkflow`
210
- - 类型:`AsyncWorkflow<{ isFirstCompile: boolean }, unknown>`
211
- - 使用示例:
212
-
213
- ```ts
214
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
215
-
216
- export const myPlugin = (): CliPlugin<AppTools> => ({
217
- setup(api) {
218
- return {
219
- afterDev: () => {
220
- // do something
221
- },
222
- };
223
- },
224
- });
225
- ```
226
-
227
- `afterDev` 会在每一次编译完成后执行,你可以通过 `isFirstCompile` 参数来判断是否为首次编译:
228
-
229
- ```ts
230
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
231
-
232
- export const myPlugin = (): CliPlugin<AppTools> => ({
233
- setup(api) {
234
- return {
235
- afterDev: ({ isFirstCompile }) => {
236
- if (isFirstCompile) {
237
- // do something
238
- }
239
- },
240
- };
241
- },
242
- });
243
- ```
244
-
245
- ### `beforeBuild`
246
-
247
- - 功能:在执行生产环境构建前触发的回调函数,你可以通过 `bundlerConfigs` 参数获取到底层打包工具的最终配置数组。
248
- - 如果当前打包工具为 webpack,则获取到的是 webpack 配置数组。
249
- - 如果当前打包工具为 Rspack,则获取到的是 Rspack 配置数组。
250
- - 配置数组中可能包含一份或多份配置,这取决于你是否开启了 SSR 等功能。
251
- - 执行阶段:运行 `build` 命令后,在开始构建前执行
252
- - Hook 模型:`AsyncWorkflow`
253
- - 类型:
254
-
255
- ```ts
256
- type BeforeBuild = AsyncWorkflow<{
257
- bundlerConfigs: WebpackConfig[] | RspackConfig[];
258
- }>;
259
- ```
260
-
261
- - 使用示例:
262
-
263
- ```ts
264
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
265
-
266
- export const myPlugin = (): CliPlugin<AppTools> => ({
267
- setup(api) {
268
- return {
269
- beforeBuild: ({ bundlerConfigs }) => {
270
- console.log('Before build.');
271
- console.log(bundlerConfigs);
272
- },
273
- };
274
- },
275
- });
276
- ```
277
-
278
- ### `afterBuild`
279
-
280
- - 功能:在执行生产环境构建后触发的回调函数,你可以通过 `stats` 参数获取到构建结果信息。
281
- - 如果当前打包工具为 webpack,则获取到的是 webpack Stats。
282
- - 如果当前打包工具为 Rspack,则获取到的是 Rspack Stats。
283
- - 执行阶段:运行 `build` 命令运行后,在项目构建完成之后执行
284
- - Hook 模型:`AsyncWorkflow`
285
- - 类型:
286
-
287
- ```ts
288
- type AfterBuild = AsyncWorkflow<{
289
- stats?: Stats | MultiStats;
290
- }>;
291
- ```
292
-
293
- - 使用示例:
294
-
295
- ```ts
296
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
297
-
298
- export const myPlugin = (): CliPlugin<AppTools> => ({
299
- setup(api) {
300
- return {
301
- afterBuild: ({ stats }) => {
302
- console.log('After build.');
303
- console.log(stats);
304
- },
305
- };
306
- },
307
- });
308
- ```
309
-
310
- ### `beforeCreateCompiler`
311
-
312
- - 功能:在创建底层 Compiler 实例前触发的回调函数,并且你可以通过 `bundlerConfigs` 参数获取到底层打包工具的最终配置数组:
313
- - 如果当前打包工具为 webpack,则获取到的是 webpack 配置数组。
314
- - 如果当前打包工具为 Rspack,则获取到的是 Rspack 配置数组。
315
- - 配置数组中可能包含一份或多份配置,这取决于你是否开启了 SSR 等功能。
316
- - 执行阶段:在执行 dev 或 build 命令时,创建 Compiler 实例之前执行
317
- - Hook 模型:`AsyncWorkflow`
318
- - 类型:
319
-
320
- ```ts
321
- type BeforeCreateCompiler = AsyncWorkflow<
322
- { bundlerConfigs: Configuration[] },
323
- unknown
324
- >;
325
- ```
326
-
327
- - 使用示例:
328
-
329
- ```ts
330
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
331
-
332
- export const myPlugin = (): CliPlugin<AppTools> => ({
333
- setup(api) {
334
- return {
335
- beforeCreateCompiler: ({ bundlerConfigs }) => {
336
- console.log('Before create compiler.');
337
- console.log(bundlerConfigs);
338
- },
339
- };
340
- },
341
- });
342
- ```
343
-
344
- ### `afterCreateCompiler`
345
-
346
- - 功能:在创建 Compiler 实例后、执行构建前触发的回调函数,并且你可以通过 `compiler` 参数获取到 Compiler 实例对象:
347
- - 如果当前打包工具为 webpack,则获取到的是 webpack Compiler 对象。
348
- - 如果当前打包工具为 Rspack,则获取到的是 Rspack Compiler 对象。
349
- - 执行阶段:创建 Compiler 对象之后执行
350
- - Hook 模型:`AsyncWorkflow`
351
- - 类型:
352
-
353
- ```ts
354
- type AfterCreateCompiler = AsyncWorkflow<
355
- { compiler: Compiler | MultiCompiler | undefined },
356
- unknown
357
- >;
358
- ```
359
-
360
- - 使用示例:
361
-
362
- ```ts
363
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
364
-
365
- export const myPlugin = (): CliPlugin<AppTools> => ({
366
- setup(api) {
367
- return {
368
- afterCreateCompiler: ({ compiler }) => {
369
- console.log('After create compiler.');
370
- console.log(compiler);
371
- },
372
- };
373
- },
374
- });
375
- ```
376
-
377
- ### `beforePrintInstructions`
378
-
379
- - 功能:在中间件函数中可以拿到即将打印的日志信息,并对其进行修改
380
- - 执行阶段:打印日志信息之前执行
381
- - Hook 模型:`AsyncWaterfall`
382
- - 类型:`AsyncWaterfall<{ instructions: string }>`
383
- - 使用示例:
384
-
385
- ```ts
386
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
387
-
388
- export const myPlugin = (): CliPlugin<AppTools> => ({
389
- setup(api) {
390
- return {
391
- beforePrintInstructions: ({ instructions }) => {
392
- // do something
393
- return {
394
- instructions: [...instructions, 'some new message'],
395
- };
396
- },
397
- };
398
- },
399
- });
400
- ```
401
-
402
- {/*
403
- ### `checkEntryPoint`
404
-
405
- - 功能:增加入口类型
406
- - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
407
- - Hook 模型:`AsyncWaterfall`
408
- - 类型:`AsyncWaterfall<{ path: string; entry: false | string; }>`
409
- - 使用示例:
410
-
411
- ```ts
412
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
413
-
414
- export const myPlugin = (): CliPlugin<AppTools> => ({
415
- setup(api) {
416
- return {
417
- checkEntryPoint({ path, entry }) {
418
- return { path, entry: entry || isNewEntry(path) };
419
- },
420
- };
421
- },
422
- });
423
- ```
424
-
425
- ### `modifyEntrypoints`
426
-
427
- - 功能:用于修改入口信息,针对于插件新增的入口,可以通过该钩子修改对应的 entrypoint 信息
428
- - 执行阶段:生成入口文件之前,[`prepare`](#prepare) 阶段触发
429
- - Hook 模型:`AsyncWaterfall`
430
- - 类型:`AsyncWaterfall<{ entrypoints: Entrypoint[]; }>`
431
- - 使用示例:
432
-
433
- ```ts
434
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
435
-
436
- export const myPlugin = (): CliPlugin<AppTools> => ({
437
- setup(api) {
438
- return {
439
- async modifyEntrypoints({ entrypoints }) {
440
- const newEntryPoints = entrypoints.map(entrypoint => {
441
- ...
442
- });
443
- return { entrypoints: newEntryPoints };
444
- },
445
- };
446
- },
447
- });
448
- ```
449
-
450
- ### `generateEntryCode`
451
-
452
- - 功能:用于修改生成的入口文件
453
- - 执行阶段:生成入口文件之后,创建 builder 之前
454
- - Hook 模型:`AsyncWaterfall`
455
- - 类型:`AsyncWorkflow<{ entrypoints: Entrypoint[] }, void>`
456
- - 使用示例:
457
-
458
- ```ts
459
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
460
-
461
- export const myPlugin = (): CliPlugin<AppTools> => ({
462
- setup(api) {
463
- return {
464
- async generateEntryCode({ entrypoints }) {
465
- await Promise.all(
466
- entrypoints.map(async entrypoint => {
467
- ...
468
- })
469
- );
470
- },
471
- };
472
- },
473
- });
474
- ```
475
- */}
476
- ### `modifyFileSystemRoutes`
477
-
478
- - 功能:用于修改生成前端页面路由文件中的内容,内容都是需要可序列化的
479
- - 执行阶段:生成前端路由文件之前,[`prepare`](#prepare) 阶段触发
480
- - Hook 模型:`AsyncWaterfall`
481
- - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; routes: Route[]; }>`
482
- - 使用示例:
483
-
484
- ```tsx
485
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
486
-
487
- export const myPlugin = (): CliPlugin<AppTools> => ({
488
- setup(api) {
489
- return {
490
- modifyFileSystemRoutes({ entrypoint, routes }) {
491
- return {
492
- entrypoint,
493
- routes: [
494
- ...routes,
495
- {
496
- path: '/custom_page',
497
- component: require.resolve('./Component'),
498
- exact: true,
499
- },
500
- ],
501
- };
502
- },
503
- };
504
- },
505
- });
506
- ```
507
-
508
- 这样就为前端新增了一个页面路由。
509
-
510
- ### `modifyServerRoutes`
511
-
512
- - 功能:用于修改生成服务器路由中的内容
513
- - 执行阶段:生成 Server 路由文件之前,[`prepare`](#prepare) 阶段触发
514
- - Hook 模型:`AsyncWaterfall`
515
- - 类型:`AsyncWaterfall<{ routes: ServerRoute[]; }>`
516
- - 使用示例:
517
-
518
- ```ts
519
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
520
-
521
- export const myPlugin = (): CliPlugin<AppTools> => ({
522
- setup(api) {
523
- return {
524
- modifyServerRoutes({ routes }) {
525
- return {
526
- routes: [
527
- ...routes,
528
- {
529
- urlPath: '/api/foo',
530
- isApi: true,
531
- entryPath: '',
532
- isSPA: false,
533
- isSSR: false,
534
- },
535
- ],
536
- };
537
- },
538
- };
539
- },
540
- });
541
- ```
542
-
543
- ### `htmlPartials`
544
-
545
- - 功能:用于定制生成的 HTML 页面模版
546
- - 执行阶段:[`prepare`](#prepare) 阶段触发
547
- - Hook 模型:`AsyncWaterfall`
548
- - 类型:`AsyncWaterfall<{ entrypoint: Entrypoint; partials: HtmlPartials; }>`
549
- - 使用示例:
550
-
551
- ```ts
552
- import type { CliPlugin, AppTools } from '@modern-js/app-tools';
553
-
554
- export const myPlugin = (): CliPlugin<AppTools> => ({
555
- setup(api) {
556
- return {
557
- async htmlPartials({ entrypoint, partials }) {
558
- partials.head.push('<script>console.log("test")</script>');
559
- return {
560
- entrypoint,
561
- partials,
562
- };
563
- },
564
- };
565
- },
566
- });
567
- ```
568
-
569
- 这样就为 HTML 模版中新增了一个 Script 标签。
570
-
571
- {/* ## Server
572
-
573
- :::note
574
- 目前 Server 插件还未完全开放,API 不保证稳定,使用需谨慎。
575
-
576
- :::
577
-
578
- Modern.js 工程中的 Server 部分也支持了插件。其中的 Hook 将会提供一些特定阶段调用和特殊功能的 Hook。
579
-
580
- ### `create`
581
-
582
- - 功能:在中间件函数中会拿到 Server 初始化用到的指标测量工具配置 `measureOptions` 和日志工具配置 `loggerOptions`,并返回自定义的指标测量工具 `measure` 和日志工具配置 `logger`
583
- - 执行阶段:Server 初始化
584
- - Hook 模型:`AsyncPipeline`
585
- - 类型:`AsyncPipeline<ServerInitInput, InitExtension>`
586
- - 使用示例:
587
-
588
- ```ts
589
- import type { ServerPlugin } from '@modern-js/server-core';
590
-
591
- export const myPlugin = (): ServerPlugin => ({
592
- setup(api) {
593
- return {
594
- create: ({ measureOptions, loggerOptions }) => {
595
- // do something
596
- },
597
- };
598
- },
599
- });
600
- ```
601
-
602
- ### `prepareWebServer`
603
-
604
- - 功能:设置 Web 路由的处理函数,在中间件函数中可以拿到 Web Server 的前置中间件
605
- - 执行阶段:在请求到达的时候
606
- - Hook 模型:`AsyncPipeline`
607
- - 类型:`AsyncPipeline<WebServerStartInput, Adapter>`
608
- - 使用示例:
609
-
610
- ```ts
611
- import type { ServerPlugin } from '@modern-js/server-core';
612
-
613
- export const myPlugin = (): ServerPlugin => ({
614
- setup(api) {
615
- return {
616
- prepareWebServer: ({ middleware }) => {
617
- // do something
618
-
619
- return (req, res) => {
620
- // do response
621
- };
622
- },
623
- };
624
- },
625
- });
626
- ```
627
-
628
- ### `prepareApiServer`
629
-
630
- - 功能:设置 API 路由的处理函数,在中间件函数中可以拿到 API Server 的前置中间件
631
- - 执行阶段:请求到达并且 match bff basename 之后执行
632
- - Hook 模型:`AsyncPipeline`
633
- - 类型:`AsyncPipeline<APIServerStartInput, Adapter>`
634
- - 使用示例:
635
-
636
- ```ts
637
- import type { ServerPlugin } from '@modern-js/server-core';
638
-
639
- export const myPlugin = (): ServerPlugin => ({
640
- setup(api) {
641
- return {
642
- prepareApiServer: ({ middleware }) => {
643
- // do something
644
-
645
- return (req, res) => {
646
- // do response
647
- };
648
- },
649
- };
650
- },
651
- });
652
- ```*/}
653
-
654
- ## Runtime
655
-
656
- :::note
657
- 目前 Runtime 插件还未完全开放,API 不保证稳定,使用需谨慎。
658
-
659
- :::
660
-
661
- Runtime 插件主要用于开发者自定义应用渲染前行为和修改需要渲染的组件。
662
-
663
- ### `beforeRender`
664
-
665
- - 功能:在应用渲染之前增加行为
666
- - 执行阶段:渲染(SSR/CSR)
667
- - Hook 模型:`AsyncWorkflow`
668
- - 类型:`AsyncWorkflow<RuntimeContext, void>`
669
- - 使用示例:
670
-
671
- ```ts
672
- import type { Plugin } from '@modern-js/runtime';
673
-
674
- export const myPlugin = (): Plugin => ({
675
- setup(api) {
676
- return {
677
- beforeRender(context) {
678
- // do something
679
- },
680
- };
681
- },
682
- });
683
- ```
684
-
685
- ### `wrapRoot`
686
-
687
- - 功能:修改需要渲染的组件
688
- - 执行阶段:渲染(SSR/CSR)
689
- - Hook 模型:`Waterfall`
690
- - 类型:`Waterfall<React.ComponentType<any>>`
691
- - 使用示例:
692
-
693
- ```ts
694
- import { createContext } from 'react';
695
- import type { Plugin } from '@modern-js/runtime';
696
-
697
- export const myPlugin = (): Plugin => ({
698
- setup(api) {
699
- const FooContext = createContext('');
700
- return {
701
- wrapRoot(App) {
702
- const AppWrapper = (props: any) => {
703
- return (
704
- <FooContext.Provider store={'test'}>
705
- <App {...props} />
706
- </FooContext.Provider>
707
- );
708
- };
709
- return AppWrapper
710
- },
711
- };
712
- },
713
- });
714
- ```
715
-