@modern-js/main-doc 2.65.4 → 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 (87) hide show
  1. package/docs/en/apis/app/hooks/config/_meta.json +1 -0
  2. package/docs/en/apis/app/hooks/config/favicon.mdx +29 -0
  3. package/docs/en/apis/app/hooks/config/icon.mdx +3 -30
  4. package/docs/en/community/blog/v2-release-note.mdx +1 -1
  5. package/docs/en/configure/app/plugins.mdx +2 -2
  6. package/docs/en/configure/app/tools/esbuild.mdx +1 -1
  7. package/docs/en/configure/app/tools/swc.mdx +1 -1
  8. package/docs/en/plugin/_meta.json +8 -7
  9. package/docs/en/plugin/cli-plugins/_meta.json +1 -1
  10. package/docs/en/plugin/cli-plugins/api.mdx +617 -0
  11. package/docs/en/plugin/cli-plugins/life-cycle.mdx +139 -0
  12. package/docs/en/plugin/cli-plugins/migration.mdx +98 -0
  13. package/docs/en/plugin/introduction.mdx +119 -47
  14. package/docs/en/plugin/official/_meta.json +12 -0
  15. package/docs/en/plugin/official/cli-plugins/_meta.json +1 -0
  16. package/docs/en/plugin/official/cli-plugins.mdx +6 -0
  17. package/docs/en/plugin/official/rsbuild-plugins.mdx +3 -0
  18. package/docs/en/plugin/plugin-system.mdx +237 -0
  19. package/docs/en/plugin/runtime-plugins/_meta.json +1 -0
  20. package/docs/en/plugin/runtime-plugins/api.mdx +165 -0
  21. package/docs/en/plugin/runtime-plugins/life-cycle.mdx +29 -0
  22. package/docs/en/plugin/runtime-plugins/migration.mdx +101 -0
  23. package/docs/en/plugin/server-plugins/api.mdx +3 -0
  24. package/docs/en/plugin/server-plugins/life-cycle.mdx +3 -0
  25. package/docs/zh/apis/app/hooks/config/_meta.json +1 -0
  26. package/docs/zh/apis/app/hooks/config/favicon.mdx +29 -0
  27. package/docs/zh/apis/app/hooks/config/icon.mdx +3 -30
  28. package/docs/zh/community/blog/v2-release-note.mdx +1 -1
  29. package/docs/zh/configure/app/plugins.mdx +2 -2
  30. package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
  31. package/docs/zh/configure/app/tools/swc.mdx +1 -1
  32. package/docs/zh/plugin/_meta.json +8 -7
  33. package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
  34. package/docs/zh/plugin/cli-plugins/api.mdx +617 -0
  35. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +139 -0
  36. package/docs/zh/plugin/cli-plugins/migration.mdx +98 -0
  37. package/docs/zh/plugin/introduction.mdx +92 -20
  38. package/docs/zh/plugin/official/_meta.json +12 -0
  39. package/docs/zh/plugin/official/cli-plugins/_meta.json +1 -0
  40. package/docs/zh/plugin/official/cli-plugins.mdx +6 -0
  41. package/docs/zh/plugin/official/rsbuild-plugins.mdx +3 -0
  42. package/docs/zh/plugin/plugin-system.mdx +239 -0
  43. package/docs/zh/plugin/runtime-plugins/_meta.json +1 -0
  44. package/docs/zh/plugin/runtime-plugins/api.mdx +166 -0
  45. package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +29 -0
  46. package/docs/zh/plugin/runtime-plugins/migration.mdx +101 -0
  47. package/docs/zh/plugin/server-plugins/api.mdx +3 -0
  48. package/docs/zh/plugin/server-plugins/life-cycle.mdx +3 -0
  49. package/i18n.json +4 -0
  50. package/package.json +4 -4
  51. package/src/components/Footer/index.tsx +1 -1
  52. package/src/components/Mermaid/index.tsx +60 -0
  53. package/src/components/Mermaid/style.scss +221 -0
  54. package/docs/en/plugin/cli-plugins.mdx +0 -6
  55. package/docs/en/plugin/plugin-system/_meta.json +0 -10
  56. package/docs/en/plugin/plugin-system/extend.mdx +0 -163
  57. package/docs/en/plugin/plugin-system/hook-list.mdx +0 -711
  58. package/docs/en/plugin/plugin-system/hook.mdx +0 -188
  59. package/docs/en/plugin/plugin-system/implement.mdx +0 -243
  60. package/docs/en/plugin/plugin-system/introduction.mdx +0 -95
  61. package/docs/en/plugin/plugin-system/lifecycle.mdx +0 -16
  62. package/docs/en/plugin/plugin-system/plugin-api.mdx +0 -138
  63. package/docs/en/plugin/plugin-system/relationship.mdx +0 -119
  64. package/docs/en/plugin/rsbuild-plugins.mdx +0 -3
  65. package/docs/zh/plugin/cli-plugins.mdx +0 -6
  66. package/docs/zh/plugin/plugin-system/_meta.json +0 -10
  67. package/docs/zh/plugin/plugin-system/extend.mdx +0 -163
  68. package/docs/zh/plugin/plugin-system/hook-list.mdx +0 -715
  69. package/docs/zh/plugin/plugin-system/hook.mdx +0 -173
  70. package/docs/zh/plugin/plugin-system/implement.mdx +0 -250
  71. package/docs/zh/plugin/plugin-system/introduction.mdx +0 -94
  72. package/docs/zh/plugin/plugin-system/lifecycle.mdx +0 -16
  73. package/docs/zh/plugin/plugin-system/plugin-api.mdx +0 -138
  74. package/docs/zh/plugin/plugin-system/relationship.mdx +0 -119
  75. package/docs/zh/plugin/rsbuild-plugins.mdx +0 -4
  76. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  77. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  78. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  79. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  80. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  81. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
  82. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  83. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  84. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  85. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  86. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  87. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
@@ -1,19 +1,20 @@
1
1
  [
2
2
  "introduction",
3
+ "plugin-system",
3
4
  {
4
5
  "type": "dir",
5
- "name": "plugin-system",
6
- "label": "plugin-system",
7
- "collapsed": true
6
+ "name": "cli-plugins",
7
+ "label": "cli-plugins"
8
8
  },
9
9
  {
10
10
  "type": "dir",
11
- "name": "cli-plugins",
12
- "label": "cli-plugins"
11
+ "name": "runtime-plugins",
12
+ "label": "runtime-plugins"
13
13
  },
14
14
  {
15
15
  "type": "dir",
16
- "name": "rsbuild-plugins",
17
- "label": "rsbuild-plugins"
16
+ "name": "official",
17
+ "label": "official-plugins",
18
+ "collapsed": true
18
19
  }
19
20
  ]
@@ -1 +1 @@
1
- ["plugin-tailwind", "plugin-bff", "plugin-ssg", "plugin-swc"]
1
+ ["api", "life-cycle", "migration"]
@@ -0,0 +1,617 @@
1
+ # 插件 API
2
+
3
+ 本文档详细介绍了 Modern.js CLI 插件的 API。CLI 插件允许您在 Modern.js 项目的构建和开发过程中扩展和定制功能。
4
+
5
+ ## 插件基础结构
6
+
7
+ 一个典型的 CLI 插件结构如下:
8
+
9
+ ```typescript
10
+ import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
11
+
12
+ const myCliPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
13
+ name: '@my-org/my-plugin', // 插件名称,确保唯一性
14
+ setup: (api) => {
15
+ // 在这里使用 API 注册钩子、添加命令等
16
+ api.onBeforeBuild(() => {
17
+ console.log('构建即将开始...');
18
+ });
19
+ },
20
+ });
21
+
22
+ export default myCliPlugin;
23
+ ```
24
+
25
+ `setup` 函数接收一个 `api` 对象,该对象提供了所有可用的 CLI 插件 API。
26
+
27
+ ## 信息获取
28
+
29
+ #### `api.getAppContext`
30
+
31
+ 获取 Modern.js 应用的上下文信息。
32
+
33
+ - **返回值:** `AppContext` 对象,包含以下字段:
34
+
35
+ | 字段名 | 类型 | 描述 | 何时可用 |
36
+ | -------------------- | -------------------- | --------------------------------------------------------------------- | -------------------------------------- |
37
+ | `command` | `string` | 当前执行的命令 (e.g., `dev`, `build`, `deploy`) | - |
38
+ | `port` | `number` | 开发服务器端口号 | `onPrepare` 之后 |
39
+ | `configFile` | `string` | 配置文件的绝对路径 | - |
40
+ | `isProd` | `boolean` | 是否为生产模式 | - |
41
+ | `appDirectory` | `string` | 项目根目录的绝对路径 | - |
42
+ | `srcDirectory` | `string` | 项目源码目录的绝对路径 | - |
43
+ | `distDirectory` | `string` | 项目产物输出目录的绝对路径 | `modifyResolvedConfig` 之后 |
44
+ | `sharedDirectory` | `string` | 公共模块目录的绝对路径 | - |
45
+ | `nodeModulesDirectory` | `string` | `node_modules` 目录的绝对路径 | - |
46
+ | `ip` | `string` | 当前机器的 IPv4 地址 | - |
47
+ | `packageName` | `string` | 项目 `package.json` 中的 `name` 字段 | - |
48
+ | `plugins` | `object[]` | 当前已注册的插件列表 | - |
49
+ | `entrypoints` | `object[]` | 页面入口信息 | - |
50
+ | `serverRoutes` | `object[]` | 服务端路由信息 | - |
51
+ | `bundlerType` | `webpack \| rspack` | 当前使用的打包工具类型 (`webpack` 或 `rspack`) | `onPrepare` 之后 |
52
+ | `metaName` | `string` | 框架内部名称 | - |
53
+ | `apiDirectory` | `string` | API 模块目录的绝对路径 (BFF 使用) | - |
54
+ | `lambdaDirectory` | `string` | Lambda 模块目录的绝对路径 (BFF 使用) | - |
55
+ | `runtimeConfigFile` | `string` | 运行时配置文件的名称 | - |
56
+ | `serverConfigFile` | `string` | 服务器配置文件的名称 | - |
57
+ | `checkedEntries` | `string[]` | 指定的入口信息 | - |
58
+ | `apiOnly` | `boolean` | 是否为 `apiOnly` 模式 | - |
59
+
60
+ - **示例:**
61
+
62
+ ```typescript
63
+ api.onPrepare(() => {
64
+ const appContext = api.getAppContext();
65
+ console.log(`当前项目运行在 ${appContext.isProd ? '生产' : '开发'} 模式`);
66
+ console.log(`打包工具: ${appContext.bundlerType}`);
67
+ });
68
+ ```
69
+
70
+ :::info
71
+ `getAppContext` 返回的上下文信息是只读的,无法直接进行修改。
72
+ :::
73
+
74
+ ---
75
+
76
+ #### `api.getConfig`
77
+
78
+ 获取用户在 `modern.config.ts` 文件中定义的配置。
79
+
80
+ - **返回值:** 用户定义的配置对象。
81
+ - **示例:**
82
+
83
+ ```typescript
84
+ api.onPrepare(() => {
85
+ const userConfig = api.getConfig();
86
+ if (userConfig.output) {
87
+ console.log('用户自定义了 output 配置');
88
+ }
89
+ });
90
+
91
+ ```
92
+
93
+ ---
94
+
95
+ #### `api.getNormalizedConfig`
96
+
97
+ 获取经过 Modern.js 内部处理和插件修改后的最终配置(归一化配置)。
98
+
99
+ - **返回值:** 归一化后的配置对象。
100
+ - **何时可用:** 必须在 `modifyResolvedConfig` 钩子之后使用。
101
+ - **示例:**
102
+
103
+ ```typescript
104
+ api.modifyResolvedConfig(resolvedConfig => {
105
+ // ... 修改配置 ...
106
+ return resolvedConfig;
107
+ });
108
+
109
+ api.onBeforeBuild(() => {
110
+ const finalConfig = api.getNormalizedConfig();
111
+ console.log('最终构建配置:', finalConfig);
112
+ });
113
+ ```
114
+
115
+ ---
116
+
117
+ #### `api.isPluginExists`
118
+
119
+ 检查指定的插件是否已注册。
120
+
121
+ - **参数:**
122
+ - `pluginName: string`: 要检查的插件名称。
123
+ - **返回值:** `boolean` 值,表示插件是否存在。
124
+ - **示例:**
125
+
126
+ ```typescript
127
+ if (api.isPluginExists('@modern-js/plugin-tailwind')) {
128
+ console.log('Tailwind CSS 插件已启用');
129
+ }
130
+ ```
131
+
132
+ ---
133
+
134
+ #### `api.getHooks`
135
+
136
+ 获取所有已注册的钩子函数。
137
+
138
+ - **返回值:** 包含所有钩子函数的对象。
139
+ - **示例:**
140
+
141
+ ```typescript
142
+ const hooks = api.getHooks();
143
+ // 手动触发 onPrepare 钩子
144
+ hooks.onPrepare.call();
145
+ ```
146
+
147
+ :::warning
148
+ 在自定义插件中,只能手动调用对应插件注册的钩子,不能调用官方钩子,以免影响正常应用的执行顺序。
149
+ :::
150
+
151
+ ---
152
+
153
+ ## 配置修改
154
+
155
+ #### `api.config`
156
+
157
+ 修改 Modern.js 的初始配置。
158
+
159
+ - **类型:** `api.config(configFn: () => UserConfig | Promise<UserConfig>)`
160
+ - **参数:**
161
+ - `configFn`: 一个返回配置对象或 Promise 的函数。
162
+ - **执行阶段:** 解析完 `modern.config.ts` 中的配置之后。
163
+ - **示例:**
164
+
165
+ ```typescript
166
+ api.config(() => {
167
+ return {
168
+ output: {
169
+ disableTsChecker: true, // 关闭 TypeScript 类型检查
170
+ },
171
+ };
172
+ });
173
+ ```
174
+
175
+ **配置合并优先级**(从高到低):
176
+
177
+ 1. 用户在 `modern.config.*` 文件中定义的配置。
178
+ 2. 插件通过 `api.config()` 注册的配置。
179
+ 3. Modern.js 默认配置。
180
+
181
+ ---
182
+
183
+ #### `api.modifyBundlerChain`
184
+
185
+ 使用 chain API 修改 webpack 或者 Rspack 配置。
186
+
187
+ - **类型:** `api.modifyBundlerChain(modifyFn: (chain: WebpackChain | RspackChain, utils: WebpackUtils | RspackUtils) => void | Promise<void>)`
188
+ - **参数:**
189
+ - `modifyFn`: 修改函数,接收 `webpack-chain` 或 `RspackChain` 实例和实用工具作为参数。
190
+ - **执行阶段:** 在生成最终的 Webpack 或 Rspack 配置时。
191
+ - **对应 Rsbuild Hook**: [modifybundlerchain](https://rsbuild.dev/zh/plugins/dev/hooks#modifybundlerchain)
192
+ - **示例:**
193
+
194
+ ```typescript
195
+ api.modifyBundlerChain((chain, utils) => {
196
+ if (utils.env === 'development') {
197
+ chain.devtool('eval');
198
+ }
199
+ chain.plugin('bundle-analyze').use(BundleAnalyzerPlugin);
200
+ });
201
+ ```
202
+
203
+ #### `api.modifyRsbuildConfig`
204
+
205
+ 修改 Rsbuild 的配置。
206
+
207
+ - **类型:** `api.modifyRsbuildConfig(modifyFn: (config: RsbuildConfig, utils: RsbuildUtils) => RsbuildConfig | Promise<RsbuildConfig> | void)`
208
+ - **参数:**
209
+ - `modifyFn`: 修改函数,接收 Rsbuild 配置对象和实用工具作为参数,可以返回修改后的配置对象、Promise 或不返回(直接修改原对象)。
210
+ - **执行阶段:** 在生成最终的 Rsbuild 配置时。
211
+ - **对应 Rsbuild Hook**: [modifyRsbuildConfig](https://rsbuild.dev/zh/plugins/dev/hooks#modifyrsbuildconfig)
212
+ - **示例:**
213
+
214
+ ```typescript
215
+ api.modifyRsbuildConfig((config, utils) => {
216
+ // 添加一个自定义的 Rsbuild 插件
217
+ config.plugins.push(myCustomRsbuildPlugin());
218
+ });
219
+
220
+ ```
221
+ ---
222
+
223
+ #### `api.modifyRspackConfig`
224
+
225
+ 修改 Rspack 的配置。(当使用 Rspack 作为打包工具时)
226
+
227
+ - **类型:** `api.modifyRspackConfig(modifyFn: (config: RspackConfig, utils: RspackUtils) => RspackConfig | Promise<RspackConfig> | void)`
228
+ - **参数:**
229
+ - `modifyFn`: 修改函数,接收 Rspack 配置对象和实用工具作为参数,可以返回修改后的配置对象、Promise 或不返回(直接修改原对象)。
230
+ - **执行阶段:** 在生成最终的 Rspack 配置时。
231
+ - **对应 Rsbuild Hook**: [modifyRspackConfig](https://rsbuild.dev/zh/plugins/dev/hooks#modifyrspackconfig)
232
+ - **示例:**
233
+
234
+ ```typescript
235
+ api.modifyRspackConfig((config, utils) => {
236
+ config.builtins.minify = {
237
+ enable: true,
238
+ implementation: utils.rspack.SwcJsMinimizerRspackPlugin,
239
+ }
240
+ });
241
+ ```
242
+
243
+ ---
244
+
245
+ #### `api.modifyWebpackChain`
246
+
247
+ 使用 [webpack-chain](https://github.com/neutrinojs/webpack-chain) 修改 Webpack 配置。(当使用 Webpack 作为打包工具时)
248
+
249
+ - **类型:** `api.modifyWebpackChain(modifyFn: (chain: WebpackChain, utils: WebpackUtils) => void | Promise<void>)`
250
+ - **参数:**
251
+ - `modifyFn`: 修改函数,接收 `webpack-chain` 实例和实用工具作为参数。
252
+ - **执行阶段:** 在生成最终的 Webpack 配置时。
253
+ - **示例:**
254
+
255
+ ```typescript
256
+ api.modifyWebpackChain((chain, utils) => {
257
+ // 添加一个自定义的 Webpack loader
258
+ chain.module
259
+ .rule('my-loader')
260
+ .test(/\.my-ext$/)
261
+ .use('my-loader')
262
+ .loader(require.resolve('./my-loader'));
263
+ });
264
+ ```
265
+
266
+ ---
267
+
268
+ #### `api.modifyWebpackConfig`
269
+
270
+ 直接修改 Webpack 配置对象。(当使用 Webpack 作为打包工具时)
271
+
272
+ - **类型:** `api.modifyWebpackConfig(modifyFn: (config: WebpackConfig, utils: WebpackUtils) => WebpackConfig | Promise<WebpackConfig> | void)`
273
+ - **参数:**
274
+ - `modifyFn`: 修改函数,接收 Webpack 配置对象和实用工具作为参数,可以返回修改后的配置对象、Promise 或不返回(直接修改原对象)。
275
+ - **执行阶段:** 在生成最终的 Webpack 配置时。
276
+ - **示例:**
277
+
278
+ ```typescript
279
+ api.modifyWebpackConfig((config, utils) => {
280
+ // 禁用 source map
281
+ config.devtool = false;
282
+ });
283
+
284
+ ```
285
+
286
+ **构建配置修改顺序**
287
+
288
+ - **使用 Rspack 构建**:
289
+ ```
290
+ modifyRsbuildConfig
291
+ modifyBundlerChain
292
+ tools.bundlerChain
293
+ modifyRspackConfig
294
+ tools.rspack
295
+ ```
296
+ - **使用 Webpack 构建**:
297
+ ```
298
+ modifyBundlerChain
299
+ tools.bundlerChain
300
+ modifyWebpackChain
301
+ tools.webpackChain
302
+ modifyWebpackConfig
303
+ tools.webpack
304
+ ```
305
+
306
+ ---
307
+
308
+ #### `api.modifyServerRoutes`
309
+
310
+ 修改服务器路由配置。
311
+
312
+ - **类型** `api.modifyServerRoutes(transformFn: (routes: ServerRoute[]) => ServerRoute[])`
313
+ - **参数:**
314
+ - `transformFn`: 转换函数,接收当前服务器路由数组作为参数,返回修改后的数组。
315
+ - **执行阶段:** 在生成服务器路由文件之前 ( `prepare` 阶段)。
316
+ - **示例:**
317
+
318
+ ```typescript
319
+ api.modifyServerRoutes(routes => {
320
+ // 添加一个新的 API 路由
321
+ routes.concat({
322
+ urlPath: '/api',
323
+ isApi: true,
324
+ entryPath: '',
325
+ isSPA: false,
326
+ isSSR: false,
327
+ });
328
+ return routes;
329
+ });
330
+ ```
331
+
332
+ ---
333
+
334
+ #### `api.modifyHtmlPartials`
335
+
336
+ 修改 HTML 模板片段。
337
+
338
+ - **类型** `api.modifyHtmlPartials(modifyFn: (partials: HtmlPartials, entrypoint: Entrypoint) => void)`
339
+ - **参数:**
340
+ - `modifyFn`: 修改函数,接收 HTML 模板片段对象和当前入口点信息作为参数。
341
+ - `partials`: 包含 `top`, `head`, `body` 三个部分,每个部分都有`append`, `prepend`, `replace`三个方法。
342
+ - **执行阶段:** 在生成 HTML 文件之前 ( `prepare` 阶段)。
343
+ - **示例:**
344
+
345
+ ```typescript
346
+ api.modifyHtmlPartials(({ entrypoint, partials }) => {
347
+ // 在所有页面的 <head> 标签中添加一个 meta 标签
348
+ if (partials.head && partials.head.append) {
349
+ partials.head.append(`<meta name="my-custom-meta" content="value">`);
350
+ }
351
+ });
352
+ ```
353
+
354
+ :::warning
355
+ 当使用[完全自定义模板](/guides/basic-features/html.html#完全自定义-html-模板)时,该钩子函数将不会执行。
356
+ :::
357
+
358
+ ---
359
+
360
+ ## 构建流程控制
361
+
362
+ #### `api.onPrepare`
363
+
364
+ 在 Modern.js 准备阶段添加额外的逻辑。
365
+
366
+ - **类型** `api.onPrepare(prepareFn: () => void | Promise<void>)`
367
+ - **参数:**
368
+ - `prepareFn`: 准备函数,无参数,可异步。
369
+ - **执行阶段:** 在 Modern.js 完成配置校验之后。
370
+ - **示例:**
371
+
372
+ ```typescript
373
+ api.onPrepare(async () => {
374
+ // 执行一些初始化操作,例如检查环境、下载依赖等
375
+ await prepareMyCustomEnvironment();
376
+ });
377
+ ```
378
+
379
+ ---
380
+
381
+ #### `api.addCommand`
382
+
383
+ 添加自定义的 CLI 命令。
384
+
385
+ - **类型** `api.addCommand(commandFn: ({ program: Command }) => void)`
386
+ - **参数:**
387
+ - `commandFn`: 接收 `commander` 的 `program` 对象作为参数,用于定义新的命令。
388
+ - **执行阶段:** `prepare` 钩子运行完成后。
389
+ - **示例:**
390
+
391
+ ```typescript
392
+ api.addCommand(({ program }) => {
393
+ program
394
+ .command('my-command')
395
+ .description('我的自定义命令')
396
+ .action(async () => {
397
+ // 执行命令逻辑
398
+ console.log('执行自定义命令...');
399
+ });
400
+ });
401
+ ```
402
+
403
+ ---
404
+
405
+ #### `api.addWatchFiles`
406
+
407
+ 添加额外的文件监听列表(用于开发模式)。
408
+
409
+ - **类型** `api.addWatchFiles(watchFn: () => string[] | { files: string[]; isPrivate: boolean; })`
410
+ - **参数:**
411
+ - `watchFn`: 返回一个包含文件路径的数组,或一个包含 `files` 和 `isPrivate` 属性的对象。
412
+ - `files`: 要监听的文件路径数组。
413
+ - `isPrivate`: 是否为框架内部文件(影响文件变更时的行为)。
414
+ - **执行阶段:** `addCommand` 钩子运行完成之后。
415
+ - **示例:**
416
+
417
+ ```typescript
418
+ api.addWatchFiles(() => {
419
+ // 监听项目根目录下的 .env 文件
420
+ return [path.resolve(api.getAppContext().appDirectory, '.env')];
421
+ });
422
+ ```
423
+
424
+ ---
425
+
426
+ #### `api.onFileChanged`
427
+
428
+ 在监听文件发生变化时添加额外的逻辑(用于开发模式)。
429
+
430
+ - **类型** `api.onFileChanged(changeFn: (params: { filename: string; eventType: 'add' | 'change' | 'unlink'; isPrivate: boolean; }) => void)`
431
+ - **参数:**
432
+ - `changeFn`: 文件变化处理函数,接收以下参数:
433
+ - `filename`: 发生变化的文件路径。
434
+ - `eventType`: 变化类型 (`add`, `change`, `unlink`)。
435
+ - `isPrivate`: 是否为框架内部文件。
436
+ - **执行阶段:** 监听文件变化之后。
437
+ - **示例:**
438
+
439
+ ```typescript
440
+ api.onFileChanged(({ filename, eventType }) => {
441
+ if (eventType === 'change' && filename.endsWith('.ts')) {
442
+ console.log('TypeScript 文件发生变化,可能需要重新编译...');
443
+ }
444
+ });
445
+ ```
446
+
447
+ ---
448
+
449
+ #### `api.onBeforeBuild`
450
+
451
+ 在构建开始之前添加额外的逻辑。
452
+
453
+ - **类型** `api.onBeforeBuild(buildFn: () => void | Promise<void>)`
454
+ - **参数:**
455
+ - `buildFn`: 构建前执行的函数,无参数,可异步。
456
+ - **执行阶段:** 在执行构建流程之前。
457
+ - **对应 Rsbuild Hook**: [onBeforeBuild](https://rsbuild.dev/zh/plugins/dev/hooks#onbeforebuild)
458
+ - **示例:**
459
+
460
+ ```typescript
461
+ api.onBeforeBuild(()=>{
462
+ // 构建前做一些环境检查
463
+ })
464
+ ```
465
+ ---
466
+
467
+ #### `api.onAfterBuild`
468
+
469
+ 在构建完成后添加额外的逻辑。
470
+
471
+ - **类型:** `api.onAfterBuild(buildFn: () => void | Promise<void>)`
472
+ - **参数:**
473
+ - `buildFn`: 构建后执行的函数,无参数,可异步。
474
+ - **执行阶段:** 在执行构建流程之后。
475
+ - **对应 Rsbuild Hook**: [onAfterBuild](https://rsbuild.dev/zh/plugins/dev/hooks#onafterbuild)
476
+ - **示例:**
477
+
478
+ ```typescript
479
+ api.onAfterBuild(()=>{
480
+ // 构建后上传sourceMap
481
+ })
482
+ ```
483
+
484
+ ---
485
+
486
+ #### `api.onDevCompileDone`
487
+ 在开发服务器编译完成后添加额外的逻辑。
488
+
489
+ - **类型:** `api.onDevCompileDone(compileFn: () => void | Promise<void>)`
490
+ - **参数:**
491
+ - `compileFn`: 编译完成后执行的函数。
492
+ - **执行阶段:** 开发服务器启动,且首次编译完成后。
493
+ - **对应 Rsbuild Hook**: [onDevCompileDone](https://rsbuild.dev/zh/plugins/dev/hooks#ondevcompiledone)
494
+ - **示例:**
495
+
496
+ ```typescript
497
+ api.onDevCompileDone(() => {
498
+ // 首次编译完成,打开浏览器
499
+ });
500
+ ```
501
+ ---
502
+
503
+ #### `api.onBeforeCreateCompiler`
504
+
505
+ 在创建编译器实例之前添加额外的逻辑。
506
+
507
+ - **类型:** `api.onBeforeCreateCompiler(createFn: () => void | Promise<void>)`
508
+ - **参数:**
509
+ - `createFn`: 创建前执行的函数,无参数,可异步。
510
+ - **执行阶段:** 在创建 Webpack 或 Rspack 编译器实例之前。
511
+ - **对应 Rsbuild Hook**: [onBeforeCreateCompiler](https://rsbuild.dev/zh/plugins/dev/hooks#onbeforecreatecompiler)
512
+ - **示例:**
513
+
514
+ ```typescript
515
+ api.onBeforeCreateCompiler(() => {
516
+ // 可以获取 compiler 相关配置
517
+ });
518
+ ```
519
+
520
+ ---
521
+
522
+ #### `api.onAfterCreateCompiler`
523
+ 在创建编译器实例之后添加额外的逻辑。
524
+
525
+ - **类型:** `api.onAfterCreateCompiler(createFn: () => void | Promise<void>)`
526
+ - **参数:**
527
+ - `createFn`: 创建后执行的函数,无参数,可异步。
528
+ - **执行阶段:** 在创建 Webpack 或 Rspack 编译器实例之后。
529
+ - **对应 Rsbuild Hook**: [onAfterCreateCompiler](https://rsbuild.dev/zh/plugins/dev/hooks#onaftercreatecompiler)
530
+ - **示例:**
531
+
532
+ ```typescript
533
+ api.onAfterCreateCompiler(() => {
534
+ // 可以获取 compiler 实例
535
+ });
536
+ ```
537
+
538
+ ---
539
+
540
+ #### `api.onBeforeDev`
541
+
542
+ 在开发服务器启动前添加额外的逻辑。
543
+
544
+ - **类型:** `api.onBeforeDev(devFn: () => void | Promise<void>)`
545
+ - **参数:**
546
+ - `devFn`: 开发服务器启动前执行的函数,无参数,可异步。
547
+ - **执行阶段:** 在执行 `dev` 命令启动开发服务器之前。
548
+ - **示例:**
549
+
550
+ ```typescript
551
+ api.onBeforeDev(async () => {
552
+ // 检查端口是否被占用
553
+ await checkPortAvailability(3000);
554
+ });
555
+ ```
556
+
557
+ ---
558
+
559
+ #### `api.onAfterDev`
560
+
561
+ 在开发服务器启动后添加额外的逻辑。
562
+
563
+ - **类型:** `api.onAfterDev(devFn: () => void | Promise<void>)`
564
+ - **参数:**
565
+ - `devFn`: 开发服务器启动后执行的函数。
566
+ - **执行阶段:** 开发服务器成功启动后。
567
+ - **对应 Rsbuild Hook**: [onAfterStartDevServer](https://rsbuild.dev/zh/plugins/dev/hooks#onafterstartdevserver)
568
+ - **示例:**
569
+
570
+ ```typescript
571
+ api.onAfterDev(()=>{
572
+ // 上报 dev 相关信息
573
+ })
574
+ ```
575
+
576
+ ---
577
+
578
+ #### `api.onBeforeExit`
579
+
580
+ 在进程退出前添加额外的逻辑。
581
+
582
+ - **类型:** `api.onBeforeExit(exitFn: () => void | Promise<void>)`
583
+ - **参数:**
584
+ - `exitFn`: 进程退出前执行的函数,无参数,可异步。
585
+ - **执行阶段:** 在 Modern.js 进程即将退出时(例如,用户按下 Ctrl+C)。
586
+ - **示例:**
587
+
588
+ ```typescript
589
+ api.onBeforeExit(async () => {
590
+ // 执行一些清理操作,例如关闭数据库连接、删除临时文件等
591
+ await cleanupMyResources();
592
+ });
593
+ ```
594
+
595
+ ---
596
+
597
+ #### `api.onBeforePrintInstructions`
598
+
599
+ 在打印成功信息前添加额外的逻辑。
600
+
601
+ - **类型:** `api.onBeforePrintInstructions(printFn: ({instructions: string}) => {instructions: string} | Promise<{instructions: string}>)`
602
+ - **参数:**
603
+ - `printFn`: 修改打印信息的函数, 返回修改后的信息。
604
+ - **执行阶段:** 打印成功信息前。
605
+ - **示例:**
606
+
607
+ ```typescript
608
+ api.onBeforePrintInstructions(({ instructions }) => {
609
+ // do something
610
+ return { instructions }
611
+ });
612
+ ```
613
+
614
+ ## 其他说明
615
+
616
+ - 可以参考 [CLI 插件生命周期](./life-cycle.mdx) 了解插件钩子的执行顺序。
617
+ - 可以参考 [CLI 插件迁移指南](./migration.mdx) 了解如何从旧版插件迁移到新版。