Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: onBeforeBuild hook should before onBeforeEnvironmentCompile hook #3196

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/cases/plugin-api/plugin-hooks/environments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ rspackOnlyTest(
'ModifyBundlerConfig web',
'BeforeCreateCompiler',
'AfterCreateCompiler',
'BeforeEnvironmentCompile web',
'BeforeBuild',
'BeforeEnvironmentCompile web',
'ModifyHTMLTags web',
'AfterEnvironmentCompile web',
'AfterBuild',
Expand All @@ -115,8 +115,8 @@ rspackOnlyTest(
'ModifyBundlerConfig node',
'BeforeCreateCompiler',
'AfterCreateCompiler',
'BeforeEnvironmentCompile node',
'BeforeBuild',
'BeforeEnvironmentCompile node',
'ModifyHTMLTags node',
'AfterEnvironmentCompile node',
'AfterBuild',
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/plugin-api/plugin-hooks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ rspackOnlyTest(
'ModifyBundlerConfig',
'BeforeCreateCompiler',
'AfterCreateCompiler',
'BeforeEnvironmentCompile',
'BeforeBuild',
'BeforeEnvironmentCompile',
'ModifyHTMLTags',
'AfterEnvironmentCompile',
'AfterBuild',
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './App.cs';
import './App.css';

const App = () => {
return (
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ const onBeforeCompile = ({

let doneCompilers = 0;

let waitBeforeCompileDone: Promise<void> | undefined;

for (let index = 0; index < compilers.length; index++) {
const compiler = compilers[index];
let compilerDone = false;
Expand All @@ -230,15 +232,16 @@ const onBeforeCompile = ({
compilerDone = true;
doneCompilers++;
}
// ensure only last compiler done will trigger beforeCompile hook
// avoid other compiler done triggers when executing async beforeEnvironmentCompiler hook
const lastCompilerDone = doneCompilers === compilers.length;

await beforeEnvironmentCompiler(index);
const isFirstTrigger = doneCompilers === 1;

if (lastCompilerDone) {
await beforeCompile?.();
if (isFirstTrigger) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we do not need a isFirstTrigger variable:

Suggested change
if (isFirstTrigger) {
if (!waitBeforeCompileDone) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doneCompilers is not reliable as it can decrease in some cases.

waitBeforeCompileDone = beforeCompile?.();
}

// beforeCompile hook should done before beforeEnvironmentCompiler run
await waitBeforeCompileDone;

await beforeEnvironmentCompiler(index);
},
);

Expand All @@ -253,8 +256,8 @@ const onBeforeCompile = ({
(isWatch ? compiler.hooks.watchRun : compiler.hooks.run).tapPromise(
name,
async () => {
await beforeEnvironmentCompiler(0);
await beforeCompile?.();
await beforeEnvironmentCompiler(0);
},
);
}
Expand Down
6 changes: 3 additions & 3 deletions website/docs/en/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ When the `rsbuild build` command or `rsbuild.build()` method is executed, Rsbuil
- [modifyRspackConfig](#modifyrspackconfig)
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
- [onAfterCreateCompiler](#onaftercreatecompiler)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [onBeforeBuild](#onbeforebuild)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [modifyHTMLTags](#modifyhtmltags)
- [onAfterEnvironmentCompile](#onafterenvironmentcompile)
- [onAfterBuild](#onafterbuild)
- [onExit](#onexit)

When rebuilding, the following hooks will be triggered again:

- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [onBeforeBuild](#onbeforebuild)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [modifyHTMLTags](#modifyhtmltags)
- [onAfterBuild](#onafterbuild)
- [onAfterEnvironmentCompile](#onafterenvironmentcompile)
- [onAfterBuild](#onafterbuild)

### Preview Hooks

Expand Down
4 changes: 2 additions & 2 deletions website/docs/zh/plugins/dev/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@
- [modifyRspackConfig](#modifyrspackconfig)
- [onBeforeCreateCompiler](#onbeforecreatecompiler)
- [onAfterCreateCompiler](#onaftercreatecompiler)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [onBeforeBuild](#onbeforebuild)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [modifyHTMLTags](#modifyhtmltags)
- [onAfterEnvironmentCompile](#onafterenvironmentcompile)
- [onAfterBuild](#onafterbuild)
- [onExit](#onexit)

当 rebuild 时,以下 hooks 会再次触发:

- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [onBeforeBuild](#onbeforebuild)
- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile)
- [modifyHTMLTags](#modifyhtmltags)
- [onAfterEnvironmentCompile](#onafterenvironmentcompile)
- [onAfterBuild](#onafterbuild)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/shared/onBeforeEnvironmentCompile.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
`onBeforeEnvironmentCompile` 是在执行单个 environment 的构建前触发的回调函数。

你可以通过 `bundlerConfig` 参数获取到当前 environment 对应的 Rspack 配置https://rspack.dev/config/
你可以通过 `bundlerConfig` 参数获取到当前 environment 对应的 [Rspack 配置](https://rspack.dev/config/)

另外,你可以通过 `isWatch` 判断是否是 dev 或者 build watch 模式,并在 watch 模式下通过 `isFirstCompile` 来判断是否为首次构建。

Expand Down
Loading