@modern-js/main-doc 2.67.7 → 2.67.8
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.
- package/docs/en/apis/app/commands.mdx +3 -3
- package/docs/en/community/blog/v2-release-note.mdx +2 -2
- package/docs/en/community/contributing-guide.mdx +1 -1
- package/docs/en/components/builder.mdx +1 -1
- package/docs/en/components/bundler.mdx +1 -1
- package/docs/en/components/rsbuild.mdx +1 -1
- package/docs/en/components/rspack.mdx +1 -1
- package/docs/en/components/rspackPrecautions.mdx +2 -2
- package/docs/en/configure/app/builder-plugins.mdx +1 -1
- package/docs/en/configure/app/output/assets-retry.mdx +1 -1
- package/docs/en/configure/app/output/copy.mdx +1 -1
- package/docs/en/configure/app/performance/build-cache.mdx +1 -1
- package/docs/en/configure/app/security/sri.mdx +1 -1
- package/docs/en/configure/app/source/exclude.mdx +2 -2
- package/docs/en/configure/app/source/include.mdx +1 -1
- package/docs/en/configure/app/source/transform-import.mdx +2 -2
- package/docs/en/configure/app/tools/babel.mdx +5 -5
- package/docs/en/configure/app/tools/bundler-chain.mdx +2 -2
- package/docs/en/configure/app/tools/css-extract.mdx +1 -1
- package/docs/en/configure/app/tools/dev-server.mdx +1 -1
- package/docs/en/configure/app/tools/lightningcss-loader.mdx +1 -1
- package/docs/en/configure/app/tools/rspack.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +3 -3
- package/docs/en/configure/app/tools/webpack-chain.mdx +19 -19
- package/docs/en/guides/advanced-features/build-performance.mdx +1 -1
- package/docs/en/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/en/guides/advanced-features/page-performance/optimize-bundle.mdx +3 -3
- package/docs/en/guides/advanced-features/rspack-start.mdx +2 -2
- package/docs/en/guides/basic-features/css/css.mdx +4 -4
- package/docs/en/guides/basic-features/debug/rsdoctor.mdx +1 -1
- package/docs/en/guides/basic-features/routes.mdx +3 -3
- package/docs/en/guides/basic-features/static-assets.mdx +1 -1
- package/docs/en/guides/concept/builder.mdx +3 -3
- package/docs/en/guides/concept/entries.mdx +1 -1
- package/docs/en/guides/get-started/tech-stack.mdx +3 -3
- package/docs/en/guides/troubleshooting/builder.mdx +4 -5
- package/docs/en/plugin/cli-plugins/api.mdx +140 -134
- package/docs/en/plugin/introduction.mdx +27 -24
- package/docs/en/tutorials/examples/csr-auth.mdx +15 -198
- package/docs/zh/apis/app/commands.mdx +3 -3
- package/docs/zh/community/blog/v2-release-note.mdx +2 -2
- package/docs/zh/community/contributing-guide.mdx +1 -1
- package/docs/zh/components/builder.mdx +1 -1
- package/docs/zh/components/bundler.mdx +1 -2
- package/docs/zh/components/rsbuild.mdx +1 -1
- package/docs/zh/components/rspack.mdx +1 -1
- package/docs/zh/components/rspackPrecautions.mdx +2 -2
- package/docs/zh/configure/app/builder-plugins.mdx +1 -1
- package/docs/zh/configure/app/output/assets-retry.mdx +1 -1
- package/docs/zh/configure/app/output/copy.mdx +1 -1
- package/docs/zh/configure/app/performance/build-cache.mdx +1 -1
- package/docs/zh/configure/app/security/sri.mdx +1 -1
- package/docs/zh/configure/app/source/exclude.mdx +2 -2
- package/docs/zh/configure/app/source/include.mdx +1 -1
- package/docs/zh/configure/app/source/transform-import.mdx +2 -2
- package/docs/zh/configure/app/tools/babel.mdx +5 -6
- package/docs/zh/configure/app/tools/bundler-chain.mdx +2 -2
- package/docs/zh/configure/app/tools/css-extract.mdx +1 -1
- package/docs/zh/configure/app/tools/dev-server.mdx +1 -1
- package/docs/zh/configure/app/tools/lightningcss-loader.mdx +1 -1
- package/docs/zh/configure/app/tools/rspack.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +3 -3
- package/docs/zh/configure/app/tools/webpack-chain.mdx +20 -20
- package/docs/zh/guides/advanced-features/compatibility.mdx +1 -1
- package/docs/zh/guides/advanced-features/page-performance/optimize-bundle.mdx +2 -2
- package/docs/zh/guides/advanced-features/rspack-start.mdx +2 -2
- package/docs/zh/guides/basic-features/css/css.mdx +4 -4
- package/docs/zh/guides/basic-features/debug/rsdoctor.mdx +1 -1
- package/docs/zh/guides/basic-features/static-assets.mdx +2 -2
- package/docs/zh/guides/concept/builder.mdx +3 -3
- package/docs/zh/guides/concept/entries.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +3 -3
- package/docs/zh/guides/troubleshooting/builder.mdx +4 -5
- package/docs/zh/plugin/cli-plugins/api.mdx +122 -120
- package/docs/zh/plugin/introduction.mdx +18 -15
- package/docs/zh/tutorials/examples/csr-auth.mdx +15 -198
- package/package.json +6 -6
- package/rspress.config.ts +12 -1
- package/src/components/RsbuildLink/index.tsx +1 -1
- package/src/components/Sandpack/index.tsx +1 -11
- package/src/components/ShowcaseList/useShowcases.ts +1 -1
- package/src/sandbox/csr-auth/src/routes/Auth-tsx.txt +74 -0
- package/src/sandbox/csr-auth/src/routes/fakeAuth-ts.txt +16 -0
- package/src/sandbox/csr-auth/src/routes/layout-tsx.txt +21 -0
- package/src/sandbox/csr-auth/src/routes/login/page-tsx.txt +40 -0
- package/src/sandbox/csr-auth/src/routes/page-tsx.txt +17 -0
- package/src/sandbox/csr-auth/src/routes/protected/page-tsx.txt +11 -0
@@ -17,7 +17,7 @@ import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
|
|
17
17
|
|
18
18
|
const myCliPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
|
19
19
|
name: '@my-org/my-plugin', // Plugin name, ensure uniqueness
|
20
|
-
setup:
|
20
|
+
setup: api => {
|
21
21
|
// Use the API here to register hooks, add commands, etc.
|
22
22
|
api.onBeforeBuild(() => {
|
23
23
|
console.log('Build is about to start...');
|
@@ -36,39 +36,43 @@ The `setup` function receives an `api` object, which provides all available CLI
|
|
36
36
|
|
37
37
|
Gets the context information of the Modern.js application.
|
38
38
|
|
39
|
-
-
|
40
|
-
|
41
|
-
| Field Name | Type
|
42
|
-
| ---------------------- |
|
43
|
-
| `command` | `string`
|
44
|
-
| `port` | `number`
|
45
|
-
| `configFile` | `string`
|
46
|
-
| `isProd` | `boolean`
|
47
|
-
| `appDirectory` | `string`
|
48
|
-
| `srcDirectory` | `string`
|
49
|
-
| `distDirectory` | `string`
|
50
|
-
| `sharedDirectory` | `string`
|
51
|
-
| `nodeModulesDirectory` | `string`
|
52
|
-
| `ip` | `string`
|
53
|
-
| `packageName` | `string`
|
54
|
-
| `plugins` | `object[]`
|
55
|
-
| `entrypoints` | `object[]`
|
56
|
-
| `serverRoutes` | `object[]`
|
57
|
-
| `bundlerType` | `webpack \| rspack` | The type of bundler currently in use (`webpack` or `rspack`)
|
58
|
-
| `metaName` | `string`
|
59
|
-
| `apiDirectory` | `string`
|
60
|
-
| `lambdaDirectory` | `string`
|
61
|
-
| `runtimeConfigFile` | `string`
|
62
|
-
| `serverConfigFile` | `string`
|
63
|
-
| `checkedEntries` | `string[]`
|
64
|
-
| `apiOnly` | `boolean`
|
65
|
-
|
66
|
-
-
|
39
|
+
- **Returns:** An `AppContext` object containing the following fields:
|
40
|
+
|
41
|
+
| Field Name | Type | Description | When Available |
|
42
|
+
| ---------------------- | ------------------- | ---------------------------------------------------------------- | ---------------------------- |
|
43
|
+
| `command` | `string` | The currently executing command (e.g., `dev`, `build`, `deploy`) | - |
|
44
|
+
| `port` | `number` | The development server port number | After `onPrepare` |
|
45
|
+
| `configFile` | `string` | The absolute path to the configuration file | - |
|
46
|
+
| `isProd` | `boolean` | Whether it is in production mode | - |
|
47
|
+
| `appDirectory` | `string` | The absolute path to the project root directory | - |
|
48
|
+
| `srcDirectory` | `string` | The absolute path to the project source code directory | - |
|
49
|
+
| `distDirectory` | `string` | The absolute path to the project output directory | After `modifyResolvedConfig` |
|
50
|
+
| `sharedDirectory` | `string` | The absolute path to the shared modules directory | - |
|
51
|
+
| `nodeModulesDirectory` | `string` | The absolute path to the `node_modules` directory | - |
|
52
|
+
| `ip` | `string` | The IPv4 address of the current machine | - |
|
53
|
+
| `packageName` | `string` | The `name` field in the project's `package.json` | - |
|
54
|
+
| `plugins` | `object[]` | The list of currently registered plugins | - |
|
55
|
+
| `entrypoints` | `object[]` | Information about page entry points | - |
|
56
|
+
| `serverRoutes` | `object[]` | Server-side routing information | - |
|
57
|
+
| `bundlerType` | `webpack \| rspack` | The type of bundler currently in use (`webpack` or `rspack`) | After `onPrepare` |
|
58
|
+
| `metaName` | `string` | The internal name of the framework | - |
|
59
|
+
| `apiDirectory` | `string` | The absolute path to the API module directory (used by BFF) | - |
|
60
|
+
| `lambdaDirectory` | `string` | The absolute path to the Lambda module directory (used by BFF) | - |
|
61
|
+
| `runtimeConfigFile` | `string` | The name of the runtime configuration file | - |
|
62
|
+
| `serverConfigFile` | `string` | The name of the server configuration file | - |
|
63
|
+
| `checkedEntries` | `string[]` | Specified entry information | - |
|
64
|
+
| `apiOnly` | `boolean` | Whether it is in `apiOnly` mode | - |
|
65
|
+
|
66
|
+
- **Example:**
|
67
67
|
|
68
68
|
```typescript
|
69
69
|
api.onPrepare(() => {
|
70
70
|
const appContext = api.getAppContext();
|
71
|
-
console.log(
|
71
|
+
console.log(
|
72
|
+
`The current project is running in ${
|
73
|
+
appContext.isProd ? 'production' : 'development'
|
74
|
+
} mode`,
|
75
|
+
);
|
72
76
|
console.log(`Bundler: ${appContext.bundlerType}`);
|
73
77
|
});
|
74
78
|
```
|
@@ -83,17 +87,16 @@ The context information returned by `getAppContext` is read-only and cannot be m
|
|
83
87
|
|
84
88
|
Gets the user-defined configuration from the `modern.config.ts` file.
|
85
89
|
|
86
|
-
-
|
87
|
-
-
|
90
|
+
- **Returns:** The user-defined configuration object.
|
91
|
+
- **Example:**
|
88
92
|
|
89
93
|
```typescript
|
90
94
|
api.onPrepare(() => {
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
+
const userConfig = api.getConfig();
|
96
|
+
if (userConfig.output) {
|
97
|
+
console.log('User has customized the output configuration');
|
98
|
+
}
|
95
99
|
});
|
96
|
-
|
97
100
|
```
|
98
101
|
|
99
102
|
---
|
@@ -102,9 +105,9 @@ api.onPrepare(() => {
|
|
102
105
|
|
103
106
|
Gets the final configuration after internal processing by Modern.js and modifications by plugins (normalized configuration).
|
104
107
|
|
105
|
-
-
|
106
|
-
-
|
107
|
-
-
|
108
|
+
- **Returns:** The normalized configuration object.
|
109
|
+
- **When Available:** Must be used after the `modifyResolvedConfig` hook.
|
110
|
+
- **Example:**
|
108
111
|
|
109
112
|
```typescript
|
110
113
|
api.modifyResolvedConfig(resolvedConfig => {
|
@@ -124,10 +127,10 @@ api.onBeforeBuild(() => {
|
|
124
127
|
|
125
128
|
Checks if a specified plugin is registered.
|
126
129
|
|
127
|
-
-
|
128
|
-
|
129
|
-
-
|
130
|
-
-
|
130
|
+
- **Parameters:**
|
131
|
+
- `pluginName: string`: The name of the plugin to check.
|
132
|
+
- **Returns:** A `boolean` value indicating whether the plugin exists.
|
133
|
+
- **Example:**
|
131
134
|
|
132
135
|
```typescript
|
133
136
|
if (api.isPluginExists('@modern-js/plugin-tailwind')) {
|
@@ -141,8 +144,8 @@ if (api.isPluginExists('@modern-js/plugin-tailwind')) {
|
|
141
144
|
|
142
145
|
Gets all registered hook functions.
|
143
146
|
|
144
|
-
-
|
145
|
-
-
|
147
|
+
- **Returns:** An object containing all hook functions.
|
148
|
+
- **Example:**
|
146
149
|
|
147
150
|
```typescript
|
148
151
|
const hooks = api.getHooks();
|
@@ -162,7 +165,7 @@ In custom plugins, you can only manually call the hooks registered by the corres
|
|
162
165
|
|
163
166
|
Modify the initial configuration of Modern.js.
|
164
167
|
|
165
|
-
- **Type:**
|
168
|
+
- **Type:** `api.config(configFn: () => UserConfig | Promise<UserConfig>)`
|
166
169
|
- **Parameters:**
|
167
170
|
- `configFn`: A function that returns a configuration object or a Promise.
|
168
171
|
- **Execution Phase:** After parsing the configuration in `modern.config.ts`.
|
@@ -194,7 +197,7 @@ Modify Webpack or Rspack configuration using the chain API.
|
|
194
197
|
- **Parameters:**
|
195
198
|
- `modifyFn`: A modification function that receives a `webpack-chain` or `RspackChain` instance and utility functions as parameters.
|
196
199
|
- **Execution Phase:** When generating the final Webpack or Rspack configuration.
|
197
|
-
- **Corresponding Rsbuild Hook:** [modifyBundlerChain](https://rsbuild.
|
200
|
+
- **Corresponding Rsbuild Hook:** [modifyBundlerChain](https://rsbuild.rs/en/plugins/dev/hooks#modifybundlerchain)
|
198
201
|
- **Example:**
|
199
202
|
|
200
203
|
```typescript
|
@@ -210,39 +213,39 @@ api.modifyBundlerChain((chain, utils) => {
|
|
210
213
|
|
211
214
|
Modify the Rsbuild configuration.
|
212
215
|
|
213
|
-
-
|
214
|
-
-
|
215
|
-
|
216
|
-
-
|
217
|
-
-
|
218
|
-
-
|
216
|
+
- **Type:** `api.modifyRsbuildConfig(modifyFn: (config: RsbuildConfig, utils: RsbuildUtils) => RsbuildConfig | Promise<RsbuildConfig> | void)`
|
217
|
+
- **Parameters:**
|
218
|
+
- `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).
|
219
|
+
- **Execution Phase:** When generating the final Rsbuild configuration.
|
220
|
+
- **Corresponding Rsbuild Hook:** [modifyRsbuildConfig](https://rsbuild.rs/en/plugins/dev/hooks#modifyrsbuildconfig)
|
221
|
+
- **Example:**
|
219
222
|
|
220
223
|
```typescript
|
221
224
|
api.modifyRsbuildConfig((config, utils) => {
|
222
225
|
// Add a custom Rsbuild plugin
|
223
226
|
config.plugins.push(myCustomRsbuildPlugin());
|
224
227
|
});
|
225
|
-
|
226
228
|
```
|
229
|
+
|
227
230
|
---
|
228
231
|
|
229
232
|
#### `api.modifyRspackConfig`
|
230
233
|
|
231
234
|
Modify the Rspack configuration (when using Rspack as the bundler).
|
232
235
|
|
233
|
-
-
|
234
|
-
-
|
235
|
-
|
236
|
-
-
|
237
|
-
-
|
238
|
-
-
|
236
|
+
- **Type:** `api.modifyRspackConfig(modifyFn: (config: RspackConfig, utils: RspackUtils) => RspackConfig | Promise<RspackConfig> | void)`
|
237
|
+
- **Parameters:**
|
238
|
+
- `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).
|
239
|
+
- **Execution Phase:** When generating the final Rspack configuration.
|
240
|
+
- **Corresponding Rsbuild Hook:** [modifyRspackConfig](https://rsbuild.rs/en/plugins/dev/hooks#modifyrspackconfig)
|
241
|
+
- **Example:**
|
239
242
|
|
240
243
|
```typescript
|
241
244
|
api.modifyRspackConfig((config, utils) => {
|
242
245
|
config.builtins.minify = {
|
243
246
|
enable: true,
|
244
247
|
implementation: utils.rspack.SwcJsMinimizerRspackPlugin,
|
245
|
-
}
|
248
|
+
};
|
246
249
|
});
|
247
250
|
```
|
248
251
|
|
@@ -252,7 +255,7 @@ api.modifyRspackConfig((config, utils) => {
|
|
252
255
|
|
253
256
|
Modify the Webpack configuration using [webpack-chain](https://github.com/neutrinojs/webpack-chain) (when using Webpack as the bundler).
|
254
257
|
|
255
|
-
- **Type:**
|
258
|
+
- **Type:** `api.modifyWebpackChain(modifyFn: (chain: WebpackChain, utils: WebpackUtils) => void | Promise<void>)`
|
256
259
|
- **Parameters:**
|
257
260
|
- `modifyFn`: A modification function that receives a `webpack-chain` instance and utility functions as parameters.
|
258
261
|
- **Execution Phase:** When generating the final Webpack configuration.
|
@@ -275,39 +278,38 @@ api.modifyWebpackChain((chain, utils) => {
|
|
275
278
|
|
276
279
|
Directly modify the Webpack configuration object (when using Webpack as the bundler).
|
277
280
|
|
278
|
-
-
|
279
|
-
-
|
280
|
-
|
281
|
-
-
|
282
|
-
-
|
281
|
+
- **Type:** `api.modifyWebpackConfig(modifyFn: (config: WebpackConfig, utils: WebpackUtils) => WebpackConfig | Promise<WebpackConfig> | void)`
|
282
|
+
- **Parameters:**
|
283
|
+
- `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).
|
284
|
+
- **Execution Phase:** When generating the final Webpack configuration.
|
285
|
+
- **Example:**
|
283
286
|
|
284
287
|
```typescript
|
285
288
|
api.modifyWebpackConfig((config, utils) => {
|
286
289
|
// Disable source map
|
287
290
|
config.devtool = false;
|
288
291
|
});
|
289
|
-
|
290
292
|
```
|
291
293
|
|
292
294
|
**Build Configuration Modification Order**
|
293
295
|
|
294
|
-
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
296
|
+
- **Building with Rspack:**
|
297
|
+
```
|
298
|
+
modifyRsbuildConfig
|
299
|
+
modifyBundlerChain
|
300
|
+
tools.bundlerChain
|
301
|
+
modifyRspackConfig
|
302
|
+
tools.rspack
|
303
|
+
```
|
304
|
+
- **Building with Webpack:**
|
305
|
+
```
|
306
|
+
modifyBundlerChain
|
307
|
+
tools.bundlerChain
|
308
|
+
modifyWebpackChain
|
309
|
+
tools.webpackChain
|
310
|
+
modifyWebpackConfig
|
311
|
+
tools.webpack
|
312
|
+
```
|
311
313
|
|
312
314
|
---
|
313
315
|
|
@@ -352,7 +354,7 @@ Modify HTML template partials.
|
|
352
354
|
api.modifyHtmlPartials(({ entrypoint, partials }) => {
|
353
355
|
// Add a meta tag to the <head> section of all pages
|
354
356
|
if (partials.head && partials.head.append) {
|
355
|
-
|
357
|
+
partials.head.append(`<meta name="my-custom-meta" content="value">`);
|
356
358
|
}
|
357
359
|
});
|
358
360
|
```
|
@@ -460,84 +462,88 @@ Add additional logic before the build starts.
|
|
460
462
|
- **Parameters:**
|
461
463
|
- `buildFn`: A function to be executed before the build, without parameters, can be asynchronous.
|
462
464
|
- **Execution Phase:** Before executing the build process.
|
463
|
-
- **Corresponding Rsbuild Hook:** [onBeforeBuild](https://rsbuild.
|
465
|
+
- **Corresponding Rsbuild Hook:** [onBeforeBuild](https://rsbuild.rs/en/plugins/dev/hooks#onbeforebuild)
|
464
466
|
- **Example:**
|
465
467
|
|
466
468
|
```typescript
|
467
|
-
api.onBeforeBuild(()=>{
|
468
|
-
|
469
|
-
})
|
469
|
+
api.onBeforeBuild(() => {
|
470
|
+
// Perform some environment checks before building
|
471
|
+
});
|
470
472
|
```
|
473
|
+
|
471
474
|
---
|
472
475
|
|
473
476
|
#### `api.onAfterBuild`
|
474
477
|
|
475
478
|
Add additional logic after the build is complete.
|
476
479
|
|
477
|
-
-
|
478
|
-
-
|
479
|
-
|
480
|
-
-
|
481
|
-
-
|
482
|
-
-
|
480
|
+
- **Type:** `api.onAfterBuild(buildFn: () => void | Promise<void>)`
|
481
|
+
- **Parameters:**
|
482
|
+
- `buildFn`: A function to be executed after the build, without parameters, can be asynchronous.
|
483
|
+
- **Execution Phase:** After executing the build process.
|
484
|
+
- **Corresponding Rsbuild Hook:** [onAfterBuild](https://rsbuild.rs/en/plugins/dev/hooks#onafterbuild)
|
485
|
+
- **Example:**
|
483
486
|
|
484
487
|
```typescript
|
485
|
-
api.onAfterBuild(()=>{
|
486
|
-
|
487
|
-
})
|
488
|
+
api.onAfterBuild(() => {
|
489
|
+
// Upload sourceMap after building
|
490
|
+
});
|
488
491
|
```
|
489
492
|
|
490
493
|
---
|
491
494
|
|
492
495
|
#### `api.onDevCompileDone`
|
496
|
+
|
493
497
|
Add additional logic after the development server compilation is complete.
|
494
498
|
|
495
|
-
-
|
496
|
-
-
|
497
|
-
|
498
|
-
-
|
499
|
-
-
|
500
|
-
-
|
499
|
+
- **Type:** `api.onDevCompileDone(compileFn: () => void | Promise<void>)`
|
500
|
+
- **Parameters:**
|
501
|
+
- `compileFn`: A function to be executed after compilation is complete.
|
502
|
+
- **Execution Phase:** After the development server starts and the initial compilation is complete.
|
503
|
+
- **Corresponding Rsbuild Hook:** [onDevCompileDone](https://rsbuild.rs/en/plugins/dev/hooks#ondevcompiledone)
|
504
|
+
- **Example:**
|
501
505
|
|
502
506
|
```typescript
|
503
507
|
api.onDevCompileDone(() => {
|
504
|
-
|
508
|
+
// Open the browser after the initial compilation is complete
|
505
509
|
});
|
506
510
|
```
|
511
|
+
|
507
512
|
---
|
508
513
|
|
509
514
|
#### `api.onBeforeCreateCompiler`
|
510
515
|
|
511
516
|
Add additional logic before creating the compiler instance.
|
512
517
|
|
513
|
-
-
|
514
|
-
-
|
515
|
-
|
516
|
-
-
|
517
|
-
-
|
518
|
-
-
|
518
|
+
- **Type:** `api.onBeforeCreateCompiler(createFn: () => void | Promise<void>)`
|
519
|
+
- **Parameters:**
|
520
|
+
- `createFn`: A function to be executed before creation, without parameters, can be asynchronous.
|
521
|
+
- **Execution Phase:** Before creating the Webpack or Rspack compiler instance.
|
522
|
+
- **Corresponding Rsbuild Hook:** [onBeforeCreateCompiler](https://rsbuild.rs/en/plugins/dev/hooks#onbeforecreatecompiler)
|
523
|
+
- **Example:**
|
519
524
|
|
520
525
|
```typescript
|
521
526
|
api.onBeforeCreateCompiler(() => {
|
522
|
-
|
527
|
+
// Can get compiler related configuration
|
523
528
|
});
|
524
529
|
```
|
525
530
|
|
526
531
|
---
|
527
532
|
|
528
533
|
#### `api.onAfterCreateCompiler`
|
534
|
+
|
529
535
|
Add additional logic after creating the compiler instance.
|
530
536
|
|
531
|
-
-
|
532
|
-
-
|
533
|
-
|
534
|
-
-
|
535
|
-
-
|
536
|
-
-
|
537
|
+
- **Type:** `api.onAfterCreateCompiler(createFn: () => void | Promise<void>)`
|
538
|
+
- **Parameters:**
|
539
|
+
- `createFn`: A function to be executed after creation, without parameters, can be asynchronous.
|
540
|
+
- **Execution Phase:** After creating the Webpack or Rspack compiler instance.
|
541
|
+
- **Corresponding Rsbuild Hook:** [onAfterCreateCompiler](https://rsbuild.rs/en/plugins/dev/hooks#onaftercreatecompiler)
|
542
|
+
- **Example:**
|
537
543
|
|
538
544
|
```typescript
|
539
545
|
api.onAfterCreateCompiler(() => {
|
540
|
-
|
546
|
+
// Can get the compiler instance
|
541
547
|
});
|
542
548
|
```
|
543
549
|
|
@@ -568,15 +574,15 @@ Add additional logic after starting the development server.
|
|
568
574
|
|
569
575
|
- **Type:** `api.onAfterDev(devFn: () => void | Promise<void>)`
|
570
576
|
- **Parameters:**
|
571
|
-
|
577
|
+
- `devFn`: A function to be executed after the development server starts.
|
572
578
|
- **Execution Phase:** After the development server has successfully started.
|
573
|
-
- **Corresponding Rsbuild Hook:** [onAfterStartDevServer](https://rsbuild.
|
579
|
+
- **Corresponding Rsbuild Hook:** [onAfterStartDevServer](https://rsbuild.rs/en/plugins/dev/hooks#onafterstartdevserver)
|
574
580
|
- **Example:**
|
575
581
|
|
576
582
|
```typescript
|
577
|
-
api.onAfterDev(()=>{
|
578
|
-
|
579
|
-
})
|
583
|
+
api.onAfterDev(() => {
|
584
|
+
// Report dev related information
|
585
|
+
});
|
580
586
|
```
|
581
587
|
|
582
588
|
---
|
@@ -604,20 +610,20 @@ api.onBeforeExit(async () => {
|
|
604
610
|
|
605
611
|
Add additional logic before printing success information.
|
606
612
|
|
607
|
-
-
|
608
|
-
-
|
609
|
-
|
610
|
-
-
|
611
|
-
-
|
613
|
+
- **Type:** `api.onBeforePrintInstructions(printFn: ({instructions: string}) => {instructions: string} | Promise<{instructions: string}>)`
|
614
|
+
- **Parameters:**
|
615
|
+
- `printFn`: Function to modify the printed information, returns the modified information.
|
616
|
+
- **Execution Phase:** Before printing success information.
|
617
|
+
- **Example:**
|
612
618
|
|
613
619
|
```typescript
|
614
620
|
api.onBeforePrintInstructions(({ instructions }) => {
|
615
621
|
// do something
|
616
|
-
return { instructions }
|
622
|
+
return { instructions };
|
617
623
|
});
|
618
624
|
```
|
619
625
|
|
620
626
|
## Other Notes
|
621
627
|
|
622
|
-
-
|
623
|
-
-
|
628
|
+
- Refer to [CLI Plugin Lifecycle](./life-cycle.mdx) to understand the execution order of plugin hooks.
|
629
|
+
- Refer to [CLI Plugin Migration Guide](./migration.mdx) to learn how to migrate from the old version of plugins to the new version.
|
@@ -6,29 +6,30 @@ Modern.js provides a powerful plugin system that allows developers to extend the
|
|
6
6
|
|
7
7
|
In web application development, we often encounter needs that the framework itself cannot directly satisfy, such as:
|
8
8
|
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
12
|
-
-
|
13
|
-
-
|
9
|
+
- **I want to add a custom command-line tool to help me automate some tasks.**
|
10
|
+
- **I want to be able to handle a new file format, such as `.xyz`.**
|
11
|
+
- **I need to perform some initialization operations before the application starts.**
|
12
|
+
- **I want to perform special processing on the generated CSS files.**
|
13
|
+
- **I need to customize the application's routing logic or add some server-side middleware.**
|
14
14
|
|
15
|
-
Without a plugin system, these requirements might require modifying the framework's source code or resorting to cumbersome hacks.
|
15
|
+
Without a plugin system, these requirements might require modifying the framework's source code or resorting to cumbersome hacks. Modern.js's plugin system offers an elegant, flexible, and maintainable solution.
|
16
16
|
|
17
17
|
## When to Use Which Plugin?
|
18
18
|
|
19
|
-
Modern.js offers two main types of plugins: Modern.js framework plugins and Rsbuild build plugins.
|
19
|
+
Modern.js offers two main types of plugins: Modern.js framework plugins and Rsbuild build plugins. The choice of which plugin to use depends on your specific needs:
|
20
20
|
|
21
|
-
-
|
22
|
-
- Modifying Webpack/Rspack `loader` or `plugin` configurations.
|
23
|
-
- Handling new file types.
|
24
|
-
- Modifying or compiling file contents.
|
25
|
-
- Optimizing or processing build artifacts.
|
21
|
+
- **Rsbuild Build Plugins:** If your needs are closely related to the build process, especially involving modifications to Webpack or Rspack configuration, then you should choose an Rsbuild plugin. For example:
|
26
22
|
|
27
|
-
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
- Modifying Webpack/Rspack `loader` or `plugin` configurations.
|
24
|
+
- Handling new file types.
|
25
|
+
- Modifying or compiling file contents.
|
26
|
+
- Optimizing or processing build artifacts.
|
27
|
+
|
28
|
+
- **Modern.js Framework Plugins:** If your needs relate to the extension of Modern.js framework itself, runtime behavior, or server-side logic, then you should choose a Modern.js plugin. For example:
|
29
|
+
- Adding custom command-line commands.
|
30
|
+
- Modifying the application's routing configuration.
|
31
|
+
- Customizing the application's rendering process (e.g., SSR).
|
32
|
+
- Adding server-side middleware or handler functions.
|
32
33
|
|
33
34
|
In short, use Rsbuild plugins when you need to modify Webpack/Rspack configurations; use Modern.js plugins for other framework-related extensions.
|
34
35
|
|
@@ -69,13 +70,14 @@ export default defineRuntimeConfig({
|
|
69
70
|
});
|
70
71
|
```
|
71
72
|
|
73
|
+
{/* prettier-ignore */}
|
72
74
|
{/* #### Server Plugins
|
73
75
|
|
74
76
|
Server plugins are used to provide additional functionality when the application receives requests, such as adding middleware and modifying request responses.
|
75
77
|
|
76
78
|
Server plugins are configured via the `plugins` field in `server/modern.server.ts`.
|
77
79
|
|
78
|
-
|
80
|
+
````ts title="src/modern.server.ts"
|
79
81
|
import { defineConfig } from '@modern-js/app-tools/server';
|
80
82
|
import bffPlugin from '@modern-js/plugin-bff/server';
|
81
83
|
|
@@ -106,13 +108,13 @@ Rsbuild is the underlying build tool for Modern.js. By adding Rsbuild plugins, y
|
|
106
108
|
You can register Rsbuild plugins in `modern.config.ts` via the `builderPlugins` option. See [builderPlugins](/configure/app/builder-plugins.html) for details.
|
107
109
|
|
108
110
|
:::tip
|
109
|
-
Starting from `MAJOR_VERSION.46.0`, Modern.js upgraded its underlying build tool to [Rsbuild](https://rsbuild.
|
111
|
+
Starting from `MAJOR_VERSION.46.0`, Modern.js upgraded its underlying build tool to [Rsbuild](https://rsbuild.rs/).
|
110
112
|
|
111
113
|
If your current version is lower than MAJOR_VERSION.46.0, you can upgrade by running `npx modern upgrade`.
|
112
114
|
:::
|
113
115
|
|
114
116
|
:::info
|
115
|
-
You can read [Rsbuild Official Website - Plugins](https://rsbuild.
|
117
|
+
You can read [Rsbuild Official Website - Plugins](https://rsbuild.rs/plugins/list/index) to learn more about the Rsbuild plugin system.
|
116
118
|
:::
|
117
119
|
|
118
120
|
### Official Plugins
|
@@ -123,10 +125,10 @@ The following are official Rsbuild plugins that are already built into Modern.js
|
|
123
125
|
|
124
126
|
| Plugin | Description | Modern.js Link |
|
125
127
|
| -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
126
|
-
| [React Plugin](https://rsbuild.
|
127
|
-
| [SVGR Plugin](https://rsbuild.
|
128
|
+
| [React Plugin](https://rsbuild.rs/zh/plugins/list/plugin-react) | Provides support for React | - |
|
129
|
+
| [SVGR Plugin](https://rsbuild.rs/zh/plugins/list/plugin-svgr) | Supports converting SVG images into React components | [output.disableSvgr](/configure/app/output/disable-svgr)<br />[output.svgDefaultExport](/configure/app/output/svg-default-export) |
|
128
130
|
| [Styled Components Plugin](https://github.com/rspack-contrib/rsbuild-plugin-styled-components) | Provides compile-time support for styled-components | [tools.styledComponents](/configure/app/tools/styled-components.html) |
|
129
|
-
| [Assets Retry Plugin](https://rsbuild.
|
131
|
+
| [Assets Retry Plugin](https://rsbuild.rs/zh/plugins/list/plugin-assets-retry) | Automatically retries requests when static asset loading fails | [output.assetsRetry](/configure/app/output/assets-retry.html) |
|
130
132
|
| [Type Check Plugin](https://github.com/rspack-contrib/rsbuild-plugin-type-check) | Runs TypeScript type checking in a separate process | [output.disableTsChecker](/configure/app/output/disable-ts-checker.html)<br />[tools.tsChecker](/configure/app/tools/ts-checker.html) |
|
131
133
|
| [Node Polyfill Plugin](https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill) | Injects polyfills for Node core modules in the browser | [output.disableNodePolyfill](/configure/app/output/disable-node-polyfill.html) |
|
132
134
|
| [Source Build Plugin](https://github.com/rspack-contrib/rsbuild-plugin-source-build) | For monorepo scenarios, supports referencing source code from other subdirectories and completing builds and hot updates | [experiments.sourceBuild](/configure/app/experiments/source-build.html) |
|
@@ -142,9 +144,10 @@ The following are official Rsbuild plugins that are already built into Modern.js
|
|
142
144
|
The following are official Rsbuild plugins that are not built into Modern.js:
|
143
145
|
|
144
146
|
- [Image Compress Plugin](https://github.com/rspack-contrib/rsbuild-plugin-image-compress): Compresses image resources used in the project.
|
145
|
-
- [Stylus Plugin](https://rsbuild.
|
147
|
+
- [Stylus Plugin](https://rsbuild.rs/zh/plugins/list/plugin-stylus): Uses Stylus as the CSS preprocessor.
|
146
148
|
- [UMD Plugin](https://github.com/rspack-contrib/rsbuild-plugin-umd): Used to build UMD format artifacts.
|
147
149
|
|
148
150
|
import OtherPlugins from '@site-docs-en/components/other-plugins.mdx';
|
149
151
|
|
150
152
|
<OtherPlugins />
|
153
|
+
````
|