@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
@@ -132,7 +132,7 @@ Modern.js 可以划分为三个核心部分:**CLI 工具、服务端和运行
132
132
 
133
133
  在字节跳动内部,我们借助这些插件 API,结合公司内的基建和平台,封装出内部的企业级框架。如果你需要对 Modern.js 框架进行深度定制,也可以借助这些插件 API 来完成。
134
134
 
135
- > 如果你对 Modern.js 的插件系统感兴趣,请阅读 [「Modern.js - 自定义插件」](https://modernjs.dev/plugin/plugin-system/introduction.html)文档。
135
+ > 如果你对 Modern.js 的插件系统感兴趣,请阅读 [「Modern.js - 自定义插件」](https://modernjs.dev/plugin/plugin-system.html)文档。
136
136
 
137
137
  ### 嵌套路由
138
138
 
@@ -9,7 +9,7 @@ sidebar_position: 9
9
9
 
10
10
  Used to configure custom Modern.js framework plugins.
11
11
 
12
- Refer to [How to Develop Plugins](/plugin/plugin-system/implement) for how to write custom plugins.
12
+ Refer to [How to Develop Plugins](/plugin/plugin-system) for how to write custom plugins.
13
13
 
14
14
  ## Note
15
15
 
@@ -33,7 +33,7 @@ Currently, Modern.js has opened up the ability to customize CLI plugins, and Ser
33
33
 
34
34
  By default, custom plugins are executed in the order of the `plugins` array, and the execution time of built-in Modern.js plugins is earlier than that of custom plugins.
35
35
 
36
- When the plugin sets options that control the order, such as `pre` and `post`, the execution order will be adjusted based on the declared fields. Refer to [Relationship between plugins](/plugin/plugin-system/relationship) for more information.
36
+ When the plugin sets options that control the order, such as `pre` and `post`, the execution order will be adjusted based on the declared fields. Refer to [Plugins Structure](/plugin/plugin-system) for more information.
37
37
 
38
38
  ## Example
39
39
 
@@ -41,4 +41,4 @@ export default defineConfig({
41
41
  });
42
42
  ```
43
43
 
44
- For config details, please refer to [Esbuild Plugin Configuration](/plugin/rsbuild-plugins/plugin-esbuild.html#config).
44
+ For config details, please refer to [Esbuild Plugin Configuration](/plugin/official/rsbuild-plugins/plugin-esbuild.html#config).
@@ -108,4 +108,4 @@ export default defineConfig({
108
108
  });
109
109
  ```
110
110
 
111
- For config details, please refer to [SWC Plugin Configuration](/plugin/cli-plugins/plugin-swc.html#config).
111
+ For config details, please refer to [SWC Plugin Configuration](/plugin/official/cli-plugins/plugin-swc.html#config).
@@ -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
+ # CLI Plugin API
2
+
3
+ This document details the API for Modern.js CLI plugins. CLI plugins allow you to extend and customize the functionality of Modern.js projects during the build and development process.
4
+
5
+ ## Plugin Basic Structure
6
+
7
+ A typical CLI plugin structure is as follows:
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', // Plugin name, ensure uniqueness
14
+ setup: (api) => {
15
+ // Use the API here to register hooks, add commands, etc.
16
+ api.onBeforeBuild(() => {
17
+ console.log('Build is about to start...');
18
+ });
19
+ },
20
+ });
21
+
22
+ export default myCliPlugin;
23
+ ```
24
+
25
+ The `setup` function receives an `api` object, which provides all available CLI plugin APIs.
26
+
27
+ ## Information Retrieval
28
+
29
+ #### `api.getAppContext`
30
+
31
+ Gets the context information of the Modern.js application.
32
+
33
+ - **Returns:** An `AppContext` object containing the following fields:
34
+
35
+ | Field Name | Type | Description | When Available |
36
+ | ---------------------- | -------------------- | ----------------------------------------------------------------------------- | -------------------------------------- |
37
+ | `command` | `string` | The currently executing command (e.g., `dev`, `build`, `deploy`) | - |
38
+ | `port` | `number` | The development server port number | After `onPrepare` |
39
+ | `configFile` | `string` | The absolute path to the configuration file | - |
40
+ | `isProd` | `boolean` | Whether it is in production mode | - |
41
+ | `appDirectory` | `string` | The absolute path to the project root directory | - |
42
+ | `srcDirectory` | `string` | The absolute path to the project source code directory | - |
43
+ | `distDirectory` | `string` | The absolute path to the project output directory | After `modifyResolvedConfig` |
44
+ | `sharedDirectory` | `string` | The absolute path to the shared modules directory | - |
45
+ | `nodeModulesDirectory` | `string` | The absolute path to the `node_modules` directory | - |
46
+ | `ip` | `string` | The IPv4 address of the current machine | - |
47
+ | `packageName` | `string` | The `name` field in the project's `package.json` | - |
48
+ | `plugins` | `object[]` | The list of currently registered plugins | - |
49
+ | `entrypoints` | `object[]` | Information about page entry points | - |
50
+ | `serverRoutes` | `object[]` | Server-side routing information | - |
51
+ | `bundlerType` | `webpack \| rspack` | The type of bundler currently in use (`webpack` or `rspack`) | After `onPrepare` |
52
+ | `metaName` | `string` | The internal name of the framework | - |
53
+ | `apiDirectory` | `string` | The absolute path to the API module directory (used by BFF) | - |
54
+ | `lambdaDirectory` | `string` | The absolute path to the Lambda module directory (used by BFF) | - |
55
+ | `runtimeConfigFile` | `string` | The name of the runtime configuration file | - |
56
+ | `serverConfigFile` | `string` | The name of the server configuration file | - |
57
+ | `checkedEntries` | `string[]` | Specified entry information | - |
58
+ | `apiOnly` | `boolean` | Whether it is in `apiOnly` mode | - |
59
+
60
+ - **Example:**
61
+
62
+ ```typescript
63
+ api.onPrepare(() => {
64
+ const appContext = api.getAppContext();
65
+ console.log(`The current project is running in ${appContext.isProd ? 'production' : 'development'} mode`);
66
+ console.log(`Bundler: ${appContext.bundlerType}`);
67
+ });
68
+ ```
69
+
70
+ :::info
71
+ The context information returned by `getAppContext` is read-only and cannot be modified directly.
72
+ :::
73
+
74
+ ---
75
+
76
+ #### `api.getConfig`
77
+
78
+ Gets the user-defined configuration from the `modern.config.ts` file.
79
+
80
+ - **Returns:** The user-defined configuration object.
81
+ - **Example:**
82
+
83
+ ```typescript
84
+ api.onPrepare(() => {
85
+ const userConfig = api.getConfig();
86
+ if (userConfig.output) {
87
+ console.log('User has customized the output configuration');
88
+ }
89
+ });
90
+
91
+ ```
92
+
93
+ ---
94
+
95
+ #### `api.getNormalizedConfig`
96
+
97
+ Gets the final configuration after internal processing by Modern.js and modifications by plugins (normalized configuration).
98
+
99
+ - **Returns:** The normalized configuration object.
100
+ - **When Available:** Must be used after the `modifyResolvedConfig` hook.
101
+ - **Example:**
102
+
103
+ ```typescript
104
+ api.modifyResolvedConfig(resolvedConfig => {
105
+ // ... Modify the configuration ...
106
+ return resolvedConfig;
107
+ });
108
+
109
+ api.onBeforeBuild(() => {
110
+ const finalConfig = api.getNormalizedConfig();
111
+ console.log('Final build configuration:', finalConfig);
112
+ });
113
+ ```
114
+
115
+ ---
116
+
117
+ #### `api.isPluginExists`
118
+
119
+ Checks if a specified plugin is registered.
120
+
121
+ - **Parameters:**
122
+ - `pluginName: string`: The name of the plugin to check.
123
+ - **Returns:** A `boolean` value indicating whether the plugin exists.
124
+ - **Example:**
125
+
126
+ ```typescript
127
+ if (api.isPluginExists('@modern-js/plugin-tailwind')) {
128
+ console.log('Tailwind CSS plugin is enabled');
129
+ }
130
+ ```
131
+
132
+ ---
133
+
134
+ #### `api.getHooks`
135
+
136
+ Gets all registered hook functions.
137
+
138
+ - **Returns:** An object containing all hook functions.
139
+ - **Example:**
140
+
141
+ ```typescript
142
+ const hooks = api.getHooks();
143
+ // Manually trigger the onPrepare hook
144
+ hooks.onPrepare.call();
145
+ ```
146
+
147
+ :::warning
148
+ In custom plugins, you can only manually call the hooks registered by the corresponding plugin and cannot call official hooks to avoid affecting the normal execution order of the application.
149
+ :::
150
+
151
+ ---
152
+
153
+ ## Configuration Modification
154
+
155
+ #### `api.config`
156
+
157
+ Modify the initial configuration of Modern.js.
158
+
159
+ - **Type:** `api.config(configFn: () => UserConfig | Promise<UserConfig>)`
160
+ - **Parameters:**
161
+ - `configFn`: A function that returns a configuration object or a Promise.
162
+ - **Execution Phase:** After parsing the configuration in `modern.config.ts`.
163
+ - **Example:**
164
+
165
+ ```typescript
166
+ api.config(() => {
167
+ return {
168
+ output: {
169
+ disableTsChecker: true, // Disable TypeScript type checking
170
+ },
171
+ };
172
+ });
173
+ ```
174
+
175
+ **Configuration Merging Priority** (from highest to lowest):
176
+
177
+ 1. Configuration defined by the user in the `modern.config.*` file.
178
+ 2. Configuration registered by plugins via `api.config()`.
179
+ 3. Default Modern.js configuration.
180
+
181
+ ---
182
+
183
+ #### `api.modifyBundlerChain`
184
+
185
+ Modify Webpack or Rspack configuration using the chain API.
186
+
187
+ - **Type:** `api.modifyBundlerChain(modifyFn: (chain: WebpackChain | RspackChain, utils: WebpackUtils | RspackUtils) => void | Promise<void>)`
188
+ - **Parameters:**
189
+ - `modifyFn`: A modification function that receives a `webpack-chain` or `RspackChain` instance and utility functions as parameters.
190
+ - **Execution Phase:** When generating the final Webpack or Rspack configuration.
191
+ - **Corresponding Rsbuild Hook:** [modifyBundlerChain](https://rsbuild.dev/en/plugins/dev/hooks#modifybundlerchain)
192
+ - **Example:**
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
+ Modify the Rsbuild configuration.
206
+
207
+ - **Type:** `api.modifyRsbuildConfig(modifyFn: (config: RsbuildConfig, utils: RsbuildUtils) => RsbuildConfig | Promise<RsbuildConfig> | void)`
208
+ - **Parameters:**
209
+ - `modifyFn`: A modification function that receives the Rsbuild configuration object and utility functions as parameters. It can return the modified configuration object, a Promise, or nothing (modifying the original object directly).
210
+ - **Execution Phase:** When generating the final Rsbuild configuration.
211
+ - **Corresponding Rsbuild Hook:** [modifyRsbuildConfig](https://rsbuild.dev/en/plugins/dev/hooks#modifyrsbuildconfig)
212
+ - **Example:**
213
+
214
+ ```typescript
215
+ api.modifyRsbuildConfig((config, utils) => {
216
+ // Add a custom Rsbuild plugin
217
+ config.plugins.push(myCustomRsbuildPlugin());
218
+ });
219
+
220
+ ```
221
+ ---
222
+
223
+ #### `api.modifyRspackConfig`
224
+
225
+ Modify the Rspack configuration (when using Rspack as the bundler).
226
+
227
+ - **Type:** `api.modifyRspackConfig(modifyFn: (config: RspackConfig, utils: RspackUtils) => RspackConfig | Promise<RspackConfig> | void)`
228
+ - **Parameters:**
229
+ - `modifyFn`: A modification function that receives the Rspack configuration object and utility functions as parameters. It can return the modified configuration object, a Promise, or nothing (modifying the original object directly).
230
+ - **Execution Phase:** When generating the final Rspack configuration.
231
+ - **Corresponding Rsbuild Hook:** [modifyRspackConfig](https://rsbuild.dev/en/plugins/dev/hooks#modifyrspackconfig)
232
+ - **Example:**
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
+ Modify the Webpack configuration using [webpack-chain](https://github.com/neutrinojs/webpack-chain) (when using Webpack as the bundler).
248
+
249
+ - **Type:** `api.modifyWebpackChain(modifyFn: (chain: WebpackChain, utils: WebpackUtils) => void | Promise<void>)`
250
+ - **Parameters:**
251
+ - `modifyFn`: A modification function that receives a `webpack-chain` instance and utility functions as parameters.
252
+ - **Execution Phase:** When generating the final Webpack configuration.
253
+ - **Example:**
254
+
255
+ ```typescript
256
+ api.modifyWebpackChain((chain, utils) => {
257
+ // Add a custom 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
+ Directly modify the Webpack configuration object (when using Webpack as the bundler).
271
+
272
+ - **Type:** `api.modifyWebpackConfig(modifyFn: (config: WebpackConfig, utils: WebpackUtils) => WebpackConfig | Promise<WebpackConfig> | void)`
273
+ - **Parameters:**
274
+ - `modifyFn`: A modification function that receives the Webpack configuration object and utility functions as parameters. It can return the modified configuration object, a Promise, or nothing (modifying the original object directly).
275
+ - **Execution Phase:** When generating the final Webpack configuration.
276
+ - **Example:**
277
+
278
+ ```typescript
279
+ api.modifyWebpackConfig((config, utils) => {
280
+ // Disable source map
281
+ config.devtool = false;
282
+ });
283
+
284
+ ```
285
+
286
+ **Build Configuration Modification Order**
287
+
288
+ - **Building with Rspack:**
289
+ ```
290
+ modifyRsbuildConfig
291
+ modifyBundlerChain
292
+ tools.bundlerChain
293
+ modifyRspackConfig
294
+ tools.rspack
295
+ ```
296
+ - **Building with 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
+ Modify the server routing configuration.
311
+
312
+ - **Type:** `api.modifyServerRoutes(transformFn: (routes: ServerRoute[]) => ServerRoute[])`
313
+ - **Parameters:**
314
+ - `transformFn`: A transformation function that receives the current server routes array as a parameter and returns the modified array.
315
+ - **Execution Phase:** Before generating the server route file (during the `prepare` phase).
316
+ - **Example:**
317
+
318
+ ```typescript
319
+ api.modifyServerRoutes(routes => {
320
+ // Add a new API route
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
+ Modify HTML template partials.
337
+
338
+ - **Type:** `api.modifyHtmlPartials(modifyFn: (partials: HtmlPartials, entrypoint: Entrypoint) => void)`
339
+ - **Parameters:**
340
+ - `modifyFn`: A modification function that receives the HTML template partials object and the current entry point information as parameters.
341
+ - `partials`: Contains `top`, `head`, and `body` sections, each with `append`, `prepend`, and `replace` methods.
342
+ - **Execution Phase:** Before generating the HTML file (during the `prepare` phase).
343
+ - **Example:**
344
+
345
+ ```typescript
346
+ api.modifyHtmlPartials(({ entrypoint, partials }) => {
347
+ // Add a meta tag to the <head> section of all pages
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
+ This hook function will not be executed when using a [completely custom HTML template](/guides/basic-features/html.html#completely-custom-html-template).
356
+ :::
357
+
358
+ ---
359
+
360
+ ## Build Process Control
361
+
362
+ #### `api.onPrepare`
363
+
364
+ Add additional logic during the Modern.js preparation phase.
365
+
366
+ - **Type:** `api.onPrepare(prepareFn: () => void | Promise<void>)`
367
+ - **Parameters:**
368
+ - `prepareFn`: A preparation function, without parameters, can be asynchronous.
369
+ - **Execution Phase:** After Modern.js has completed configuration validation.
370
+ - **Example:**
371
+
372
+ ```typescript
373
+ api.onPrepare(async () => {
374
+ // Perform some initialization operations, such as checking the environment, downloading dependencies, etc.
375
+ await prepareMyCustomEnvironment();
376
+ });
377
+ ```
378
+
379
+ ---
380
+
381
+ #### `api.addCommand`
382
+
383
+ Add a custom CLI command.
384
+
385
+ - **Type:** `api.addCommand(commandFn: ({ program: Command }) => void)`
386
+ - **Parameters:**
387
+ - `commandFn`: Receives the `program` object from `commander` as a parameter, used to define new commands.
388
+ - **Execution Phase:** After the `prepare` hook has completed.
389
+ - **Example:**
390
+
391
+ ```typescript
392
+ api.addCommand(({ program }) => {
393
+ program
394
+ .command('my-command')
395
+ .description('My custom command')
396
+ .action(async () => {
397
+ // Execute command logic
398
+ console.log('Executing custom command...');
399
+ });
400
+ });
401
+ ```
402
+
403
+ ---
404
+
405
+ #### `api.addWatchFiles`
406
+
407
+ Add additional files to the watch list (for development mode).
408
+
409
+ - **Type:** `api.addWatchFiles(watchFn: () => string[] | { files: string[]; isPrivate: boolean; })`
410
+ - **Parameters:**
411
+ - `watchFn`: Returns an array of file paths or an object containing `files` and `isPrivate` properties.
412
+ - `files`: An array of file paths to watch.
413
+ - `isPrivate`: Whether the files are framework-internal (affects behavior when files change).
414
+ - **Execution Phase:** After the `addCommand` hook has completed.
415
+ - **Example:**
416
+
417
+ ```typescript
418
+ api.addWatchFiles(() => {
419
+ // Watch the .env file in the project root directory
420
+ return [path.resolve(api.getAppContext().appDirectory, '.env')];
421
+ });
422
+ ```
423
+
424
+ ---
425
+
426
+ #### `api.onFileChanged`
427
+
428
+ Add additional logic when a watched file changes (for development mode).
429
+
430
+ - **Type:** `api.onFileChanged(changeFn: (params: { filename: string; eventType: 'add' | 'change' | 'unlink'; isPrivate: boolean; }) => void)`
431
+ - **Parameters:**
432
+ - `changeFn`: A file change handler function that receives the following parameters:
433
+ - `filename`: The path of the changed file.
434
+ - `eventType`: The type of change (`add`, `change`, `unlink`).
435
+ - `isPrivate`: Whether the file is framework-internal.
436
+ - **Execution Phase:** After a watched file changes.
437
+ - **Example:**
438
+
439
+ ```typescript
440
+ api.onFileChanged(({ filename, eventType }) => {
441
+ if (eventType === 'change' && filename.endsWith('.ts')) {
442
+ console.log('TypeScript file changed, may need to recompile...');
443
+ }
444
+ });
445
+ ```
446
+
447
+ ---
448
+
449
+ #### `api.onBeforeBuild`
450
+
451
+ Add additional logic before the build starts.
452
+
453
+ - **Type:** `api.onBeforeBuild(buildFn: () => void | Promise<void>)`
454
+ - **Parameters:**
455
+ - `buildFn`: A function to be executed before the build, without parameters, can be asynchronous.
456
+ - **Execution Phase:** Before executing the build process.
457
+ - **Corresponding Rsbuild Hook:** [onBeforeBuild](https://rsbuild.dev/en/plugins/dev/hooks#onbeforebuild)
458
+ - **Example:**
459
+
460
+ ```typescript
461
+ api.onBeforeBuild(()=>{
462
+ // Perform some environment checks before building
463
+ })
464
+ ```
465
+ ---
466
+
467
+ #### `api.onAfterBuild`
468
+
469
+ Add additional logic after the build is complete.
470
+
471
+ - **Type:** `api.onAfterBuild(buildFn: () => void | Promise<void>)`
472
+ - **Parameters:**
473
+ - `buildFn`: A function to be executed after the build, without parameters, can be asynchronous.
474
+ - **Execution Phase:** After executing the build process.
475
+ - **Corresponding Rsbuild Hook:** [onAfterBuild](https://rsbuild.dev/en/plugins/dev/hooks#onafterbuild)
476
+ - **Example:**
477
+
478
+ ```typescript
479
+ api.onAfterBuild(()=>{
480
+ // Upload sourceMap after building
481
+ })
482
+ ```
483
+
484
+ ---
485
+
486
+ #### `api.onDevCompileDone`
487
+ Add additional logic after the development server compilation is complete.
488
+
489
+ - **Type:** `api.onDevCompileDone(compileFn: () => void | Promise<void>)`
490
+ - **Parameters:**
491
+ - `compileFn`: A function to be executed after compilation is complete.
492
+ - **Execution Phase:** After the development server starts and the initial compilation is complete.
493
+ - **Corresponding Rsbuild Hook:** [onDevCompileDone](https://rsbuild.dev/en/plugins/dev/hooks#ondevcompiledone)
494
+ - **Example:**
495
+
496
+ ```typescript
497
+ api.onDevCompileDone(() => {
498
+ // Open the browser after the initial compilation is complete
499
+ });
500
+ ```
501
+ ---
502
+
503
+ #### `api.onBeforeCreateCompiler`
504
+
505
+ Add additional logic before creating the compiler instance.
506
+
507
+ - **Type:** `api.onBeforeCreateCompiler(createFn: () => void | Promise<void>)`
508
+ - **Parameters:**
509
+ - `createFn`: A function to be executed before creation, without parameters, can be asynchronous.
510
+ - **Execution Phase:** Before creating the Webpack or Rspack compiler instance.
511
+ - **Corresponding Rsbuild Hook:** [onBeforeCreateCompiler](https://rsbuild.dev/en/plugins/dev/hooks#onbeforecreatecompiler)
512
+ - **Example:**
513
+
514
+ ```typescript
515
+ api.onBeforeCreateCompiler(() => {
516
+ // Can get compiler related configuration
517
+ });
518
+ ```
519
+
520
+ ---
521
+
522
+ #### `api.onAfterCreateCompiler`
523
+ Add additional logic after creating the compiler instance.
524
+
525
+ - **Type:** `api.onAfterCreateCompiler(createFn: () => void | Promise<void>)`
526
+ - **Parameters:**
527
+ - `createFn`: A function to be executed after creation, without parameters, can be asynchronous.
528
+ - **Execution Phase:** After creating the Webpack or Rspack compiler instance.
529
+ - **Corresponding Rsbuild Hook:** [onAfterCreateCompiler](https://rsbuild.dev/en/plugins/dev/hooks#onaftercreatecompiler)
530
+ - **Example:**
531
+
532
+ ```typescript
533
+ api.onAfterCreateCompiler(() => {
534
+ // Can get the compiler instance
535
+ });
536
+ ```
537
+
538
+ ---
539
+
540
+ #### `api.onBeforeDev`
541
+
542
+ Add additional logic before starting the development server.
543
+
544
+ - **Type:** `api.onBeforeDev(devFn: () => void | Promise<void>)`
545
+ - **Parameters:**
546
+ - `devFn`: A function to be executed before starting the development server, without parameters, can be asynchronous.
547
+ - **Execution Phase:** Before executing the `dev` command to start the development server.
548
+ - **Example:**
549
+
550
+ ```typescript
551
+ api.onBeforeDev(async () => {
552
+ // Check if the port is occupied
553
+ await checkPortAvailability(3000);
554
+ });
555
+ ```
556
+
557
+ ---
558
+
559
+ #### `api.onAfterDev`
560
+
561
+ Add additional logic after starting the development server.
562
+
563
+ - **Type:** `api.onAfterDev(devFn: () => void | Promise<void>)`
564
+ - **Parameters:**
565
+ - `devFn`: A function to be executed after the development server starts.
566
+ - **Execution Phase:** After the development server has successfully started.
567
+ - **Corresponding Rsbuild Hook:** [onAfterStartDevServer](https://rsbuild.dev/en/plugins/dev/hooks#onafterstartdevserver)
568
+ - **Example:**
569
+
570
+ ```typescript
571
+ api.onAfterDev(()=>{
572
+ // Report dev related information
573
+ })
574
+ ```
575
+
576
+ ---
577
+
578
+ #### `api.onBeforeExit`
579
+
580
+ Add additional logic before the process exits.
581
+
582
+ - **Type:** `api.onBeforeExit(exitFn: () => void | Promise<void>)`
583
+ - **Parameters:**
584
+ - `exitFn`: A function to be executed before the process exits, without parameters, can be asynchronous.
585
+ - **Execution Phase:** When the Modern.js process is about to exit (for example, when the user presses Ctrl+C).
586
+ - **Example:**
587
+
588
+ ```typescript
589
+ api.onBeforeExit(async () => {
590
+ // Perform some cleanup operations, such as closing database connections, deleting temporary files, etc.
591
+ await cleanupMyResources();
592
+ });
593
+ ```
594
+
595
+ ---
596
+
597
+ #### `api.onBeforePrintInstructions`
598
+
599
+ Add additional logic before printing success information.
600
+
601
+ - **Type:** `api.onBeforePrintInstructions(printFn: ({instructions: string}) => {instructions: string} | Promise<{instructions: string}>)`
602
+ - **Parameters:**
603
+ - `printFn`: Function to modify the printed information, returns the modified information.
604
+ - **Execution Phase:** Before printing success information.
605
+ - **Example:**
606
+
607
+ ```typescript
608
+ api.onBeforePrintInstructions(({ instructions }) => {
609
+ // do something
610
+ return { instructions }
611
+ });
612
+ ```
613
+
614
+ ## Other Notes
615
+
616
+ - Refer to [CLI Plugin Lifecycle](./life-cycle.mdx) to understand the execution order of plugin hooks.
617
+ - Refer to [CLI Plugin Migration Guide](./migration.mdx) to learn how to migrate from the old version of plugins to the new version.