@modern-js/main-doc 2.65.5 → 2.66.1-alpha.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.
- package/docs/en/apis/app/hooks/src/app.mdx +20 -34
- package/docs/en/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/en/apis/app/runtime/app/define-config.mdx +6 -0
- package/docs/en/community/blog/v2-release-note.mdx +1 -1
- package/docs/en/components/enable-micro-frontend.mdx +20 -3
- package/docs/en/components/micro-runtime-config.mdx +12 -13
- package/docs/en/components/reduck-notify.mdx +27 -0
- package/docs/en/configure/app/plugins.mdx +2 -2
- package/docs/en/configure/app/runtime/0-intro.mdx +63 -91
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/configure/app/usage.mdx +93 -69
- package/docs/en/guides/basic-features/render/_meta.json +1 -1
- package/docs/en/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/en/guides/basic-features/routes.mdx +0 -95
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/en/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/en/guides/topic-detail/model/computed-state.mdx +0 -5
- package/docs/en/guides/topic-detail/model/define-model.mdx +0 -4
- package/docs/en/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/en/guides/topic-detail/model/manage-effects.mdx +0 -5
- package/docs/en/guides/topic-detail/model/model-communicate.mdx +0 -5
- package/docs/en/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/en/guides/topic-detail/model/quick-start.mdx +5 -7
- package/docs/en/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/en/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/en/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- package/docs/en/plugin/_meta.json +8 -7
- package/docs/en/plugin/cli-plugins/_meta.json +1 -1
- package/docs/en/plugin/cli-plugins/api.mdx +617 -0
- package/docs/en/plugin/cli-plugins/life-cycle.mdx +139 -0
- package/docs/en/plugin/cli-plugins/migration.mdx +98 -0
- package/docs/en/plugin/introduction.mdx +119 -47
- package/docs/en/plugin/official/_meta.json +12 -0
- package/docs/en/plugin/official/cli-plugins/_meta.json +1 -0
- package/docs/en/plugin/official/cli-plugins.mdx +6 -0
- package/docs/en/plugin/official/rsbuild-plugins.mdx +3 -0
- package/docs/en/plugin/plugin-system.mdx +237 -0
- package/docs/en/plugin/runtime-plugins/_meta.json +1 -0
- package/docs/en/plugin/runtime-plugins/api.mdx +165 -0
- package/docs/en/plugin/runtime-plugins/life-cycle.mdx +29 -0
- package/docs/en/plugin/runtime-plugins/migration.mdx +101 -0
- package/docs/en/plugin/server-plugins/api.mdx +3 -0
- package/docs/en/plugin/server-plugins/life-cycle.mdx +3 -0
- package/docs/zh/apis/app/hooks/src/app.mdx +18 -26
- package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +9 -0
- package/docs/zh/apis/app/runtime/app/define-config.mdx +5 -0
- package/docs/zh/community/blog/v2-release-note.mdx +1 -1
- package/docs/zh/components/enable-micro-frontend.mdx +19 -12
- package/docs/zh/components/micro-runtime-config.mdx +3 -3
- package/docs/zh/components/reduck-notify.mdx +27 -0
- package/docs/zh/configure/app/plugins.mdx +2 -2
- package/docs/zh/configure/app/runtime/0-intro.mdx +67 -86
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/configure/app/usage.mdx +44 -21
- package/docs/zh/guides/basic-features/render/_meta.json +1 -1
- package/docs/zh/guides/basic-features/render/before-render.mdx +115 -0
- package/docs/zh/guides/basic-features/routes.mdx +0 -95
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
- package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
- package/docs/zh/guides/topic-detail/model/auto-actions.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/computed-state.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/define-model.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/faq.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/manage-effects.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/model-communicate.mdx +1 -4
- package/docs/zh/guides/topic-detail/model/performance.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/quick-start.mdx +7 -8
- package/docs/zh/guides/topic-detail/model/redux-integration.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
- package/docs/zh/guides/topic-detail/model/use-model.mdx +0 -5
- package/docs/zh/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
- package/docs/zh/plugin/_meta.json +8 -7
- package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
- package/docs/zh/plugin/cli-plugins/api.mdx +617 -0
- package/docs/zh/plugin/cli-plugins/life-cycle.mdx +139 -0
- package/docs/zh/plugin/cli-plugins/migration.mdx +98 -0
- package/docs/zh/plugin/introduction.mdx +92 -20
- package/docs/zh/plugin/official/_meta.json +12 -0
- package/docs/zh/plugin/official/cli-plugins/_meta.json +1 -0
- package/docs/zh/plugin/official/cli-plugins.mdx +6 -0
- package/docs/zh/plugin/official/rsbuild-plugins.mdx +3 -0
- package/docs/zh/plugin/plugin-system.mdx +239 -0
- package/docs/zh/plugin/runtime-plugins/_meta.json +1 -0
- package/docs/zh/plugin/runtime-plugins/api.mdx +166 -0
- package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +29 -0
- package/docs/zh/plugin/runtime-plugins/migration.mdx +101 -0
- package/docs/zh/plugin/server-plugins/api.mdx +3 -0
- package/docs/zh/plugin/server-plugins/life-cycle.mdx +3 -0
- package/i18n.json +4 -0
- package/package.json +3 -2
- package/src/components/Footer/index.tsx +1 -1
- package/src/components/Mermaid/index.tsx +60 -0
- package/src/components/Mermaid/style.scss +221 -0
- package/docs/en/plugin/cli-plugins.mdx +0 -6
- package/docs/en/plugin/plugin-system/_meta.json +0 -10
- package/docs/en/plugin/plugin-system/extend.mdx +0 -163
- package/docs/en/plugin/plugin-system/hook-list.mdx +0 -711
- package/docs/en/plugin/plugin-system/hook.mdx +0 -188
- package/docs/en/plugin/plugin-system/implement.mdx +0 -243
- package/docs/en/plugin/plugin-system/introduction.mdx +0 -95
- package/docs/en/plugin/plugin-system/lifecycle.mdx +0 -16
- package/docs/en/plugin/plugin-system/plugin-api.mdx +0 -138
- package/docs/en/plugin/plugin-system/relationship.mdx +0 -119
- package/docs/en/plugin/rsbuild-plugins.mdx +0 -3
- package/docs/zh/plugin/cli-plugins.mdx +0 -6
- package/docs/zh/plugin/plugin-system/_meta.json +0 -10
- package/docs/zh/plugin/plugin-system/extend.mdx +0 -163
- package/docs/zh/plugin/plugin-system/hook-list.mdx +0 -715
- package/docs/zh/plugin/plugin-system/hook.mdx +0 -173
- package/docs/zh/plugin/plugin-system/implement.mdx +0 -250
- package/docs/zh/plugin/plugin-system/introduction.mdx +0 -94
- package/docs/zh/plugin/plugin-system/lifecycle.mdx +0 -16
- package/docs/zh/plugin/plugin-system/plugin-api.mdx +0 -138
- package/docs/zh/plugin/plugin-system/relationship.mdx +0 -119
- package/docs/zh/plugin/rsbuild-plugins.mdx +0 -4
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
- /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
- /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
- /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
- /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
- /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
- /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
@@ -1,711 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 8
|
3
|
-
---
|
4
|
-
|
5
|
-
# Hook List
|
6
|
-
|
7
|
-
In the Modern.js engineering system, there are three types of plugins: CLI, Runtime, and Server. Each type of plugin can utilize different Hooks.
|
8
|
-
|
9
|
-
In this chapter, all available Hooks are listed, and you can use the corresponding Hook based on your needs.
|
10
|
-
|
11
|
-
## CLI Common Hooks
|
12
|
-
|
13
|
-
The following are the common CLI Hooks that can be used in both Modern.js Framework and Modern.js Module.
|
14
|
-
|
15
|
-
### `config`
|
16
|
-
|
17
|
-
- Functionality: Collect configuration
|
18
|
-
- Execution phase: After parsing the configuration in `modern.config.ts`
|
19
|
-
- Hook model: `ParallelWorkflow`
|
20
|
-
- Type: `ParallelWorkflow<void, unknown>`
|
21
|
-
- Example:
|
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
|
-
If you need to set the configuration of the Modern.js Framework, please use the `CliPlugin<AppTools>` type exported by `@modern-js/app-tools`:
|
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
|
-
The configuration returned by the `config` hook will be collected and merged by Modern.js, resulting in a `NormalizedConfig`. When merging configurations, the priority is as follows, from high to low:
|
60
|
-
|
61
|
-
1. Configuration defined by the user in the `modern.config.*` file.
|
62
|
-
2. Configuration defined by the plugin through the `config` hook.
|
63
|
-
3. Default configuration of Modern.js.
|
64
|
-
|
65
|
-
### `prepare`
|
66
|
-
|
67
|
-
- Functionality: Preparatory process for running the main process.
|
68
|
-
- Execution phase: After configuration validation.
|
69
|
-
- Hook model: `AsyncWorkflow`
|
70
|
-
- Type: `AsyncWorkflow<void, void>`
|
71
|
-
- Example:
|
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
|
-
- function: Running tasks after the prepare process
|
90
|
-
- Execution Phase: After the prepare process
|
91
|
-
- Hook model: `AsyncWorkflow`
|
92
|
-
- type: `AsyncWorkflow<void, void>`
|
93
|
-
- Usage:
|
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
|
-
- Functionality: Add new CLI commands for the commander.
|
112
|
-
- Execution phase: After the `prepare` Hook has run.
|
113
|
-
- Hook model: `AsyncWorkflow`
|
114
|
-
- Type: `AsyncWorkflow<{ program: Command; }, void>`
|
115
|
-
- Example:
|
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
|
-
Move the plugin to `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
|
-
run `modern foo`:
|
145
|
-
|
146
|
-
```sh
|
147
|
-
$ modern foo
|
148
|
-
foo
|
149
|
-
```
|
150
|
-
|
151
|
-
### `beforeExit`
|
152
|
-
|
153
|
-
- Functionality: Reset some file states before exiting the process.
|
154
|
-
- Execution phase: Before the process exits.
|
155
|
-
- Hook model: `Workflow`
|
156
|
-
- Type: `Workflow<void, void>`
|
157
|
-
- Example:
|
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
|
-
Since the callback function when exiting the process in Node.js is synchronous, the type of `beforeExit` Hook is `Workflow` and cannot perform asynchronous operations.
|
175
|
-
:::
|
176
|
-
|
177
|
-
## CLI Framework Hooks
|
178
|
-
|
179
|
-
The following are the CLI Hooks of the framework, which can only be used in Modern.js Framework and cannot be used in Modern.js Module.
|
180
|
-
|
181
|
-
You need to import the `CliPlugin` and `AppTools` types from `@modern-js/app-tools` to get accurate type hints for Hooks.
|
182
|
-
|
183
|
-
### `beforeDev`
|
184
|
-
|
185
|
-
- Functionality: Tasks before running the main dev process.
|
186
|
-
- Execution phase: Before the project starts when the `dev` command is run.
|
187
|
-
- Hook model: `AsyncWorkflow`
|
188
|
-
- Type: `AsyncWorkflow<void, unknown>`
|
189
|
-
- Example:
|
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
|
-
- Function: Tasks to be executed after the main process of `dev` command
|
208
|
-
- Execution stage: It is executed after each compilation is completed when running the `dev` command
|
209
|
-
- Hook model: `AsyncWorkflow`
|
210
|
-
- Type: `AsyncWorkflow<{ isFirstCompile: boolean }, unknown>`
|
211
|
-
- Example:
|
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` will be executed after each compilation is completed, you can use the `isFirstCompile` param to determine whether it is the first compilation:
|
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
|
-
- Function: A callback function triggered before executing production environment builds. You can access the final configuration array of the underlying bundler through the `bundlerConfigs` parameter.
|
248
|
-
- If the current bundler is webpack, you will get the webpack Compiler object.
|
249
|
-
- If the current bundler is Rspack, you will get the Rspack Compiler object.
|
250
|
-
- The configuration array may contain one or multiple configurations, depending on whether you have enabled features such as SSR.
|
251
|
-
- Execution stage: Executed after running the `build` command and before the actual build process begins.
|
252
|
-
- Hook model: `AsyncWorkflow`.
|
253
|
-
- Type:
|
254
|
-
|
255
|
-
```ts
|
256
|
-
type BeforeBuild = AsyncWorkflow<{
|
257
|
-
bundlerConfigs: WebpackConfig[] | RspackConfig[];
|
258
|
-
}>;
|
259
|
-
```
|
260
|
-
|
261
|
-
- Example:
|
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
|
-
- Function: A callback function triggered after running the production build. You can access the build result information through the `stats` parameter.
|
281
|
-
- If the current bundler is webpack, you will get webpack Stats.
|
282
|
-
- If the current bundler is Rspack, you will get Rspack Stats.
|
283
|
-
- Execution stage: It is executed after running the `build` command and completing the build.
|
284
|
-
- Hook model: `AsyncWorkflow`.
|
285
|
-
- Type:
|
286
|
-
|
287
|
-
```ts
|
288
|
-
type AfterBuild = AsyncWorkflow<{
|
289
|
-
stats?: Stats | MultiStats;
|
290
|
-
}>;
|
291
|
-
```
|
292
|
-
|
293
|
-
- Example:
|
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
|
-
- Function: A callback function triggered before creating the underlying Compiler instance, and you can get the final configuration array of the underlying bundler through the `bundlerConfigs` parameter:
|
313
|
-
- If the current bundler is webpack, you will get the webpack Compiler object.
|
314
|
-
- If the current bundler is Rspack, you will get the Rspack Compiler object.
|
315
|
-
- The configuration array may contain one or multiple configurations, depending on whether you have enabled features such as SSR.
|
316
|
-
- Execution stage: Executed before creating the Compiler instance when running the `dev` or `build` command.
|
317
|
-
- Hook model: `AsyncWorkflow`.
|
318
|
-
- Type:
|
319
|
-
|
320
|
-
```ts
|
321
|
-
type BeforeCreateCompiler = AsyncWorkflow<
|
322
|
-
{ bundlerConfigs: Configuration[] },
|
323
|
-
unknown
|
324
|
-
>;
|
325
|
-
```
|
326
|
-
|
327
|
-
- Example:
|
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
|
-
- Function: A callback function triggered after creating a Compiler instance and before executing the build. You can access the Compiler instance object through the `compiler` parameter:
|
347
|
-
- If the current bundler is webpack, you will get the webpack Compiler object.
|
348
|
-
- If the current bundler is Rspack, you will get the Rspack Compiler object.
|
349
|
-
- Execution stage: Executed after creating the Compiler object.
|
350
|
-
- Hook model: `AsyncWorkflow`.
|
351
|
-
- Type:
|
352
|
-
|
353
|
-
```ts
|
354
|
-
type AfterCreateCompiler = AsyncWorkflow<
|
355
|
-
{ compiler: Compiler | MultiCompiler | undefined },
|
356
|
-
unknown
|
357
|
-
>;
|
358
|
-
```
|
359
|
-
|
360
|
-
- Example:
|
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
|
-
- Function: Provides access to the log information that will be printed within middleware functions and allows modification of the log information.
|
380
|
-
- Execution stage: Executed before printing the log information.
|
381
|
-
- Hook model: `AsyncWaterfall`
|
382
|
-
- Type: `AsyncWaterfall<{ instructions: string }>`
|
383
|
-
- Example:
|
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
|
-
### `checkEntryPoint`
|
403
|
-
|
404
|
-
- Function: Used for increasing entry types.
|
405
|
-
- Execution stage: Executed before generating the entry files, triggered during the [`prepare`](#prepare) stage.
|
406
|
-
- Hook model: `AsyncWaterfall`
|
407
|
-
- Type: `AsyncWaterfall<{ path: string; entry: false | string; }>`
|
408
|
-
- Example:
|
409
|
-
|
410
|
-
```ts
|
411
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
412
|
-
|
413
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
414
|
-
setup(api) {
|
415
|
-
return {
|
416
|
-
checkEntryPoint({ path, entry }) {
|
417
|
-
return { path, entry: entry || isNewEntry(path) };
|
418
|
-
}
|
419
|
-
};
|
420
|
-
},
|
421
|
-
});
|
422
|
-
```
|
423
|
-
|
424
|
-
### `modifyEntrypoints`
|
425
|
-
|
426
|
-
- Function: Used for modifying the entry information, for the newly added entries of the plugin, the corresponding entrypoint information can be modified through this hook.
|
427
|
-
- Execution stage: Before the entry file is generated, the [`prepare`](#prepare) phase triggers
|
428
|
-
- Hook model: `AsyncWaterfall`
|
429
|
-
- Type: `AsyncWaterfall<{ entrypoints: Entrypoint[]; }>`
|
430
|
-
- Example:
|
431
|
-
|
432
|
-
```ts
|
433
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
434
|
-
|
435
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
436
|
-
setup(api) {
|
437
|
-
return {
|
438
|
-
async modifyEntrypoints({ entrypoints }) {
|
439
|
-
const newEntryPoints = entrypoints.map(entrypoint => {
|
440
|
-
...
|
441
|
-
});
|
442
|
-
return { entrypoints: newEntryPoints };
|
443
|
-
},
|
444
|
-
};
|
445
|
-
},
|
446
|
-
});
|
447
|
-
```
|
448
|
-
|
449
|
-
### `generateEntryCode`
|
450
|
-
|
451
|
-
- Function: Used for modifying the generated entry file
|
452
|
-
- Execution stage: After generating the entry file and before creating the builder.
|
453
|
-
- Hook model: `AsyncWaterfall`
|
454
|
-
- Type: `AsyncWorkflow<{ entrypoints: Entrypoint[] }, void>`
|
455
|
-
- Example:
|
456
|
-
|
457
|
-
```ts
|
458
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
459
|
-
|
460
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
461
|
-
setup(api) {
|
462
|
-
return {
|
463
|
-
async generateEntryCode({ entrypoints }) {
|
464
|
-
await Promise.all(
|
465
|
-
entrypoints.map(async entrypoint => {
|
466
|
-
...
|
467
|
-
})
|
468
|
-
);
|
469
|
-
},
|
470
|
-
};
|
471
|
-
},
|
472
|
-
});
|
473
|
-
```
|
474
|
-
*/}
|
475
|
-
### `modifyFileSystemRoutes`
|
476
|
-
|
477
|
-
- Function: Used for modifying the content of the generated front-end page routing files, which must be serializable.
|
478
|
-
- Execution stage: Executed before generating the front-end routing files, triggered during the [`prepare`](#prepare) stage.
|
479
|
-
- Hook model: `AsyncWaterfall`
|
480
|
-
- Type: `AsyncWaterfall<{ entrypoint: Entrypoint; routes: Route[]; }>`
|
481
|
-
- Example:
|
482
|
-
|
483
|
-
```tsx
|
484
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
485
|
-
|
486
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
487
|
-
setup(api) {
|
488
|
-
return {
|
489
|
-
modifyFileSystemRoutes({ entrypoint, routes }) {
|
490
|
-
return {
|
491
|
-
entrypoint,
|
492
|
-
routes: [
|
493
|
-
...routes,
|
494
|
-
{
|
495
|
-
path: '/custom_page',
|
496
|
-
component: require.resolve('./Component'),
|
497
|
-
exact: true,
|
498
|
-
},
|
499
|
-
],
|
500
|
-
};
|
501
|
-
},
|
502
|
-
};
|
503
|
-
},
|
504
|
-
});
|
505
|
-
```
|
506
|
-
|
507
|
-
This adds a new page route for the front-end.
|
508
|
-
|
509
|
-
### `modifyServerRoutes`
|
510
|
-
|
511
|
-
- Function: Used for modifying the content of the generated server routes.
|
512
|
-
- Execution stage: Executed before generating the server routing files, triggered during the [`prepare`](#prepare) stage.
|
513
|
-
- Hook model: `AsyncWaterfall`
|
514
|
-
- Type: `AsyncWaterfall<{ routes: ServerRoute[]; }>`
|
515
|
-
- Example:
|
516
|
-
|
517
|
-
```ts
|
518
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
519
|
-
|
520
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
521
|
-
setup(api) {
|
522
|
-
return {
|
523
|
-
modifyServerRoutes({ routes }) {
|
524
|
-
return {
|
525
|
-
routes: [
|
526
|
-
...routes,
|
527
|
-
{
|
528
|
-
urlPath: '/api/foo',
|
529
|
-
isApi: true,
|
530
|
-
entryPath: '',
|
531
|
-
isSPA: false,
|
532
|
-
isSSR: false,
|
533
|
-
},
|
534
|
-
],
|
535
|
-
};
|
536
|
-
},
|
537
|
-
};
|
538
|
-
},
|
539
|
-
});
|
540
|
-
```
|
541
|
-
|
542
|
-
### `htmlPartials`
|
543
|
-
|
544
|
-
- Function: Used for customizing the generated HTML page template.
|
545
|
-
- Execution stage: Triggered during the [`prepare`](#prepare) stage.
|
546
|
-
- Hook model: `AsyncWaterfall`
|
547
|
-
- Type: `AsyncWaterfall<{ entrypoint: Entrypoint; partials: HtmlPartials; }>`
|
548
|
-
- Example:
|
549
|
-
|
550
|
-
```ts
|
551
|
-
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
552
|
-
|
553
|
-
export const myPlugin = (): CliPlugin<AppTools> => ({
|
554
|
-
setup(api) {
|
555
|
-
return {
|
556
|
-
async htmlPartials({ entrypoint, partials }) {
|
557
|
-
partials.head.push('<script>console.log("test")</script>');
|
558
|
-
return {
|
559
|
-
entrypoint,
|
560
|
-
partials,
|
561
|
-
};
|
562
|
-
},
|
563
|
-
};
|
564
|
-
},
|
565
|
-
});
|
566
|
-
```
|
567
|
-
|
568
|
-
This adds a new Script tag to the HTML template.
|
569
|
-
|
570
|
-
{/* ## Server
|
571
|
-
|
572
|
-
:::note
|
573
|
-
The Server plugin is currently not fully opened, and the API is not guaranteed to be stable. Use with caution.
|
574
|
-
:::
|
575
|
-
|
576
|
-
Plugins are also supported in the Server section of the application project. The hook will provide hooks for specific stage calls and special functionality.
|
577
|
-
|
578
|
-
### `create`
|
579
|
-
|
580
|
-
- Function: In the middleware function, you will get the measurement tool configuration `measureOptions` and log tool configuration `loggerOptions` used for Server initialization, and return a custom measurement tool `measure` and log tool configuration `logger`.
|
581
|
-
- Execution stage: Server initialization.
|
582
|
-
- Hook model: `AsyncPipeline`
|
583
|
-
- Type: `AsyncPipeline<ServerInitInput, InitExtension>`
|
584
|
-
- Example:
|
585
|
-
|
586
|
-
```ts
|
587
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
588
|
-
|
589
|
-
export const myPlugin = (): ServerPlugin => ({
|
590
|
-
setup(api) {
|
591
|
-
return {
|
592
|
-
create: ({ measureOptions, loggerOptions }) => {
|
593
|
-
// do something
|
594
|
-
},
|
595
|
-
};
|
596
|
-
},
|
597
|
-
});
|
598
|
-
```
|
599
|
-
|
600
|
-
### `prepareWebServer`
|
601
|
-
|
602
|
-
- Function: Sets the handling function for the Web route. In the middleware function, you can get the front-end middleware of the Web server.
|
603
|
-
- Execution stage: When the request arrives.
|
604
|
-
- Hook model: `AsyncPipeline`
|
605
|
-
- Type: `AsyncPipeline<WebServerStartInput, Adapter>`
|
606
|
-
- Example:
|
607
|
-
|
608
|
-
```ts
|
609
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
610
|
-
|
611
|
-
export const myPlugin = (): ServerPlugin => ({
|
612
|
-
setup(api) {
|
613
|
-
return {
|
614
|
-
prepareWebServer: ({ middleware }) => {
|
615
|
-
// do something
|
616
|
-
|
617
|
-
return (req, res) => {
|
618
|
-
// do response
|
619
|
-
};
|
620
|
-
},
|
621
|
-
};
|
622
|
-
},
|
623
|
-
});
|
624
|
-
```
|
625
|
-
|
626
|
-
### `prepareApiServer`
|
627
|
-
|
628
|
-
- Function: Sets the handling function for the API route. In the middleware function, you can get the front-end middleware of the API server.
|
629
|
-
- Execution stage: When the request arrives and matches the BFF basename.
|
630
|
-
- Hook model: `AsyncPipeline`
|
631
|
-
- Type: `AsyncPipeline<APIServerStartInput, Adapter>`
|
632
|
-
- Example:
|
633
|
-
|
634
|
-
```ts
|
635
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
636
|
-
|
637
|
-
export const myPlugin = (): ServerPlugin => ({
|
638
|
-
setup(api) {
|
639
|
-
return {
|
640
|
-
prepareApiServer: ({ middleware }) => {
|
641
|
-
// do something
|
642
|
-
|
643
|
-
return (req, res) => {
|
644
|
-
// do response
|
645
|
-
};
|
646
|
-
},
|
647
|
-
};
|
648
|
-
},
|
649
|
-
});
|
650
|
-
```*/}
|
651
|
-
|
652
|
-
## Runtime
|
653
|
-
|
654
|
-
:::note
|
655
|
-
Currently, the Runtime plugin is not fully open, and the API does not guarantee stability. Use with caution.
|
656
|
-
:::
|
657
|
-
|
658
|
-
The Runtime plugin is mainly used by developers to add behaviors before application rendering and to modify components that need to be rendered.
|
659
|
-
|
660
|
-
### `beforeRender`
|
661
|
-
|
662
|
-
- Function: Add behaviors before application rendering
|
663
|
-
- Execution stage: Rendering (SSR/CSR).
|
664
|
-
- Hook model: `AsyncWorkflow`
|
665
|
-
- Type: `AsyncWorkflow<RuntimeContext, void>`
|
666
|
-
- Example:
|
667
|
-
|
668
|
-
```ts
|
669
|
-
import type { Plugin } from '@modern-js/runtime';
|
670
|
-
|
671
|
-
export const myPlugin = (): Plugin => ({
|
672
|
-
setup(api) {
|
673
|
-
return {
|
674
|
-
beforeRender(context) {
|
675
|
-
// do something
|
676
|
-
},
|
677
|
-
};
|
678
|
-
},
|
679
|
-
});
|
680
|
-
```
|
681
|
-
|
682
|
-
### `wrapRoot`
|
683
|
-
|
684
|
-
- Function: Modify components that need to be rendered
|
685
|
-
- Execution stage: Rendering (SSR/CSR).
|
686
|
-
- Hook model: `Waterfall`
|
687
|
-
- Type: `Waterfall<React.ComponentType<any>>`
|
688
|
-
- Example:
|
689
|
-
|
690
|
-
```ts
|
691
|
-
import { createContext } from 'react';
|
692
|
-
import type { Plugin } from '@modern-js/runtime';
|
693
|
-
|
694
|
-
export const myPlugin = (): Plugin => ({
|
695
|
-
setup(api) {
|
696
|
-
const FooContext = createContext('');
|
697
|
-
return {
|
698
|
-
wrapRoot(App) {
|
699
|
-
const AppWrapper = (props: any) => {
|
700
|
-
return (
|
701
|
-
<FooContext.Provider value={'test'}>
|
702
|
-
<App {...props} />
|
703
|
-
</FooContext.Provider>
|
704
|
-
);
|
705
|
-
};
|
706
|
-
return AppWrapper;
|
707
|
-
},
|
708
|
-
};
|
709
|
-
},
|
710
|
-
});
|
711
|
-
```
|