Skip to content

Commit 5e5fbc0

Browse files
authored
feat(plugin-api): add build time to environment compile hook (#6584)
1 parent 6f23494 commit 5e5fbc0

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

e2e/cases/plugin-api/plugin-hooks-environment/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ const createPlugin = () => {
5151
api.onBeforeEnvironmentCompile(({ environment }) => {
5252
names.push(`BeforeEnvironmentCompile ${environment.name}`);
5353
});
54-
api.onAfterEnvironmentCompile(({ stats, environment }) => {
54+
api.onAfterEnvironmentCompile(({ stats, environment, time }) => {
5555
expect(stats?.compilation.name).toBe(environment.name);
56+
expect(time).toBeGreaterThan(0);
5657
names.push(`AfterEnvironmentCompile ${environment.name}`);
5758
});
5859
api.onBeforeStartProdServer(() => {

packages/core/src/hooks.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,19 @@ export const registerBuildHook = ({
407407
await promise;
408408
};
409409

410-
const onEnvironmentDone = async (buildIndex: number, stats: Rspack.Stats) => {
410+
const onEnvironmentDone = async (index: number, stats: Rspack.Stats) => {
411+
const environment = environmentList[index];
412+
const time = context.buildState.time[environment.name] ?? 0;
413+
411414
await context.hooks.onAfterEnvironmentCompile.callBatch({
412-
environment: environmentList[buildIndex].name,
415+
environment: environment.name,
413416
args: [
414417
{
415418
isFirstCompile,
416419
stats,
417-
environment: environmentList[buildIndex],
420+
environment,
418421
isWatch,
422+
time,
419423
},
420424
],
421425
});
@@ -484,15 +488,19 @@ export const registerDevHook = ({
484488
await promise;
485489
};
486490

487-
const onEnvironmentDone = async (buildIndex: number, stats: Rspack.Stats) => {
491+
const onEnvironmentDone = async (index: number, stats: Rspack.Stats) => {
492+
const environment = environmentList[index];
493+
const time = context.buildState.time[environment.name] ?? 0;
494+
488495
await context.hooks.onAfterEnvironmentCompile.callBatch({
489-
environment: environmentList[buildIndex].name,
496+
environment: environment.name,
490497
args: [
491498
{
492499
isFirstCompile,
493500
stats,
494-
environment: environmentList[buildIndex],
501+
environment,
495502
isWatch: true,
503+
time,
496504
},
497505
],
498506
});

packages/core/src/types/hooks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export type OnBeforeDevCompileFn<B = 'rspack'> = (
5555
export type OnAfterEnvironmentCompileFn = (
5656
params: CompileCommonParams & {
5757
stats?: Rspack.Stats;
58+
/**
59+
* The time it takes to build the current environment in milliseconds.
60+
*/
61+
time: number;
5862
environment: EnvironmentContext;
5963
},
6064
) => MaybePromise<void>;

website/docs/en/shared/onAfterEnvironmentCompile.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function OnAfterEnvironmentCompile(
1111
isWatch: boolean;
1212
stats?: Stats;
1313
environment: EnvironmentContext;
14+
/**
15+
* The time it takes to build the current environment in milliseconds.
16+
*/
17+
time: number;
1418
}) => Promise<void> | void,
1519
): void;
1620
```

website/docs/zh/shared/onAfterEnvironmentCompile.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function OnAfterEnvironmentCompile(
1111
isWatch: boolean;
1212
stats?: Stats;
1313
environment: EnvironmentContext;
14+
/**
15+
* The time it takes to build the current environment in milliseconds.
16+
*/
17+
time: number;
1418
}) => Promise<void> | void,
1519
): void;
1620
```

0 commit comments

Comments
 (0)