Skip to content

Commit d6aed02

Browse files
authored
feat(app-tools): add isFirstCompile param to afterDev hook (#3748)
1 parent 55d37e9 commit d6aed02

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

.changeset/loud-pears-argue.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
---
4+
5+
feat(app-tools): add isFirstCompile param to afterDev hook
6+
7+
feat(app-tools): 为 afterDev 钩子增加 isFirstCompile 参数

packages/document/main-doc/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ export default (): CliPlugin => ({
260260
### `afterDev`
261261
262262
- Function: Tasks to be executed after the main process of `dev` command
263-
- Execution Stage: Executed after the project is started when running the `dev` command
263+
- Execution Stage: It is executed after each compilation is completed when running the `dev` command
264264
- Hook Model: AsyncWorkflow
265-
- Type: `AsyncWorkflow<void, unknown>`
265+
- Type: `AsyncWorkflow<{ isFirstCompile: boolean }, unknown>`
266266
- Usage Example:
267267
268268
```ts
@@ -279,6 +279,24 @@ export default (): CliPlugin => ({
279279
});
280280
```
281281
282+
`afterDev` will be executed after each compilation is completed, you can use the `isFirstCompile` param to determine whether it is the first compilation:
283+
284+
```ts
285+
import type { CliPlugin } from '@modern-js/core';
286+
287+
export default (): CliPlugin => ({
288+
setup(api) {
289+
return {
290+
afterDev: ({ isFirstCompile }) => {
291+
if (isFirstCompile) {
292+
// do something
293+
}
294+
},
295+
};
296+
},
297+
});
298+
```
299+
282300
### `beforeCreateCompiler`
283301
284302
- Function: Provides access to the Webpack configuration used to create the Webpack Compiler within middleware functions.
@@ -776,7 +794,7 @@ export default (): Plugin => ({
776794
);
777795
};
778796
return next({
779-
App: hoistNonReactStatics(AppWrapper, App)
797+
App: hoistNonReactStatics(AppWrapper, App),
780798
});
781799
},
782800
};

packages/document/main-doc/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Hook 列表
33
sidebar_position: 8
44
---
5+
56
# Hook 列表
67

78
在 Modern.js 中暴露了三类插件:CLI、Runtime、Server。下面列举下各类中的 Hook:
@@ -166,7 +167,6 @@ export default (): CliPlugin => ({
166167
});
167168
```
168169
169-
170170
### `commands`
171171
172172
- 功能:为 command 添加新的命令
@@ -260,9 +260,9 @@ export default (): CliPlugin => ({
260260
### `afterDev`
261261
262262
- 功能:运行 dev 主流程的之后的任务
263-
- 执行阶段:`dev` 命令运行时,项目启动完成之后执行
263+
- 执行阶段:运行 `dev` 命令时,每一次编译完成后执行
264264
- Hook 模型:AsyncWorkflow
265-
- 类型:`AsyncWorkflow<void, unknown>`
265+
- 类型:`AsyncWorkflow<{ isFirstCompile: boolean }, unknown>`
266266
- 使用示例:
267267
268268
```ts
@@ -279,6 +279,24 @@ export default (): CliPlugin => ({
279279
});
280280
```
281281
282+
`afterDev` 会在每一次编译完成后执行,你可以通过 `isFirstCompile` 参数来判断是否为首次编译:
283+
284+
```ts
285+
import type { CliPlugin } from '@modern-js/core';
286+
287+
export default (): CliPlugin => ({
288+
setup(api) {
289+
return {
290+
afterDev: ({ isFirstCompile }) => {
291+
if (isFirstCompile) {
292+
// do something
293+
}
294+
},
295+
};
296+
},
297+
});
298+
```
299+
282300
### `beforeCreateCompiler`
283301
284302
- 功能:在中间件函数中可以拿到创建 Webpack Compiler 的 Webpack 配置
@@ -778,7 +796,7 @@ export default (): Plugin => ({
778796
);
779797
};
780798
return next({
781-
App: hoistNonReactStatics(AppWrapper, App)
799+
App: hoistNonReactStatics(AppWrapper, App),
782800
});
783801
},
784802
};

packages/solutions/app-tools/src/analyze/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export default ({
179179
async onDevCompileDone({ isFirstCompile }) {
180180
const hookRunners = api.useHookRunners();
181181
if (process.stdout.isTTY || isFirstCompile) {
182-
hookRunners.afterDev();
182+
hookRunners.afterDev({ isFirstCompile });
183183

184184
if (isFirstCompile) {
185185
printInstructions(hookRunners, appContext, normalizedConfig);

packages/solutions/app-tools/src/types/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export type AppToolsHooks<B extends Bundler = 'webpack'> = {
7676

7777
// beforeCreateBuilder
7878
beforeDev: AsyncWorkflow<void, unknown>;
79-
afterDev: AsyncWorkflow<void, unknown>;
79+
afterDev: AsyncWorkflow<{ isFirstCompile: boolean }, unknown>;
8080
beforeCreateCompiler: AsyncWorkflow<
8181
{
8282
bundlerConfigs?: B extends 'rspack'

0 commit comments

Comments
 (0)