From 620968bbfefdca45e7e63c49afcb8c448e5eccad Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 13 Aug 2024 14:23:32 +0800 Subject: [PATCH 1/3] fix: onBeforeBuild hook should before all onBeforeEnvironmentCompile hook --- .../plugin-hooks/environments.test.ts | 4 ++-- .../plugin-api/plugin-hooks/index.test.ts | 2 +- examples/react/src/App.tsx | 2 +- packages/core/src/hooks.ts | 19 +++++++++++-------- website/docs/en/plugins/dev/hooks.mdx | 6 +++--- website/docs/zh/plugins/dev/hooks.mdx | 4 ++-- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/e2e/cases/plugin-api/plugin-hooks/environments.test.ts b/e2e/cases/plugin-api/plugin-hooks/environments.test.ts index 9f18b7b700..f0444872c6 100644 --- a/e2e/cases/plugin-api/plugin-hooks/environments.test.ts +++ b/e2e/cases/plugin-api/plugin-hooks/environments.test.ts @@ -101,8 +101,8 @@ rspackOnlyTest( 'ModifyBundlerConfig web', 'BeforeCreateCompiler', 'AfterCreateCompiler', - 'BeforeEnvironmentCompile web', 'BeforeBuild', + 'BeforeEnvironmentCompile web', 'ModifyHTMLTags web', 'AfterEnvironmentCompile web', 'AfterBuild', @@ -115,8 +115,8 @@ rspackOnlyTest( 'ModifyBundlerConfig node', 'BeforeCreateCompiler', 'AfterCreateCompiler', - 'BeforeEnvironmentCompile node', 'BeforeBuild', + 'BeforeEnvironmentCompile node', 'ModifyHTMLTags node', 'AfterEnvironmentCompile node', 'AfterBuild', diff --git a/e2e/cases/plugin-api/plugin-hooks/index.test.ts b/e2e/cases/plugin-api/plugin-hooks/index.test.ts index b380038b74..b824be1dd2 100644 --- a/e2e/cases/plugin-api/plugin-hooks/index.test.ts +++ b/e2e/cases/plugin-api/plugin-hooks/index.test.ts @@ -89,8 +89,8 @@ rspackOnlyTest( 'ModifyBundlerConfig', 'BeforeCreateCompiler', 'AfterCreateCompiler', - 'BeforeEnvironmentCompile', 'BeforeBuild', + 'BeforeEnvironmentCompile', 'ModifyHTMLTags', 'AfterEnvironmentCompile', 'AfterBuild', diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx index 49a8b5b686..dff1751231 100644 --- a/examples/react/src/App.tsx +++ b/examples/react/src/App.tsx @@ -1,4 +1,4 @@ -import './App.cs'; +import './App.css'; const App = () => { return ( diff --git a/packages/core/src/hooks.ts b/packages/core/src/hooks.ts index ef82276765..ef08632a15 100644 --- a/packages/core/src/hooks.ts +++ b/packages/core/src/hooks.ts @@ -219,6 +219,8 @@ const onBeforeCompile = ({ let doneCompilers = 0; + let waitBeforeCompileDone: Promise | undefined; + for (let index = 0; index < compilers.length; index++) { const compiler = compilers[index]; let compilerDone = false; @@ -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) { + waitBeforeCompileDone = beforeCompile?.(); } + + // beforeCompile hook should done before beforeEnvironmentCompiler run + await waitBeforeCompileDone; + + await beforeEnvironmentCompiler(index); }, ); @@ -253,8 +256,8 @@ const onBeforeCompile = ({ (isWatch ? compiler.hooks.watchRun : compiler.hooks.run).tapPromise( name, async () => { - await beforeEnvironmentCompiler(0); await beforeCompile?.(); + await beforeEnvironmentCompiler(0); }, ); } diff --git a/website/docs/en/plugins/dev/hooks.mdx b/website/docs/en/plugins/dev/hooks.mdx index d5be73b355..fbb44e628e 100644 --- a/website/docs/en/plugins/dev/hooks.mdx +++ b/website/docs/en/plugins/dev/hooks.mdx @@ -78,8 +78,8 @@ 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) @@ -87,11 +87,11 @@ When the `rsbuild build` command or `rsbuild.build()` method is executed, Rsbuil 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 diff --git a/website/docs/zh/plugins/dev/hooks.mdx b/website/docs/zh/plugins/dev/hooks.mdx index 07c99a45fd..98be0dff74 100644 --- a/website/docs/zh/plugins/dev/hooks.mdx +++ b/website/docs/zh/plugins/dev/hooks.mdx @@ -78,8 +78,8 @@ - [modifyRspackConfig](#modifyrspackconfig) - [onBeforeCreateCompiler](#onbeforecreatecompiler) - [onAfterCreateCompiler](#onaftercreatecompiler) -- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile) - [onBeforeBuild](#onbeforebuild) +- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile) - [modifyHTMLTags](#modifyhtmltags) - [onAfterEnvironmentCompile](#onafterenvironmentcompile) - [onAfterBuild](#onafterbuild) @@ -87,8 +87,8 @@ 当 rebuild 时,以下 hooks 会再次触发: -- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile) - [onBeforeBuild](#onbeforebuild) +- [onBeforeEnvironmentCompile](#onbeforeenvironmentcompile) - [modifyHTMLTags](#modifyhtmltags) - [onAfterEnvironmentCompile](#onafterenvironmentcompile) - [onAfterBuild](#onafterbuild) From 1e4757800f18a04ce0ed0baf22a2578bf0d827ee Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 13 Aug 2024 14:24:39 +0800 Subject: [PATCH 2/3] docs: fix --- website/docs/zh/shared/onBeforeEnvironmentCompile.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/zh/shared/onBeforeEnvironmentCompile.mdx b/website/docs/zh/shared/onBeforeEnvironmentCompile.mdx index 43425ee5f5..ff0c4921db 100644 --- a/website/docs/zh/shared/onBeforeEnvironmentCompile.mdx +++ b/website/docs/zh/shared/onBeforeEnvironmentCompile.mdx @@ -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` 来判断是否为首次构建。 From f942089d54d0a0012483c3384b696f8f1e7a2ecd Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 13 Aug 2024 14:56:26 +0800 Subject: [PATCH 3/3] fix: remove isFirstTrigger --- packages/core/src/hooks.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/src/hooks.ts b/packages/core/src/hooks.ts index ef08632a15..a297debdab 100644 --- a/packages/core/src/hooks.ts +++ b/packages/core/src/hooks.ts @@ -232,9 +232,8 @@ const onBeforeCompile = ({ compilerDone = true; doneCompilers++; } - const isFirstTrigger = doneCompilers === 1; - if (isFirstTrigger) { + if (!waitBeforeCompileDone) { waitBeforeCompileDone = beforeCompile?.(); } @@ -250,6 +249,10 @@ const onBeforeCompile = ({ compilerDone = false; doneCompilers--; } + + if (doneCompilers <= 0) { + waitBeforeCompileDone = undefined; + } }); } } else {