diff --git a/packages/compat/webpack/src/build.ts b/packages/compat/webpack/src/build.ts index 8e5f70c0ff..aa2567ebe1 100644 --- a/packages/compat/webpack/src/build.ts +++ b/packages/compat/webpack/src/build.ts @@ -36,6 +36,7 @@ export const build = async ( await context.hooks.onBeforeBuild.call({ bundlerConfigs: bundlerConfigs as Rspack.Configuration[], environments: context.environments, + isWatch: Boolean(watch), }); const onDone = async (stats: Rspack.Stats | Rspack.MultiStats) => { @@ -43,6 +44,7 @@ export const build = async ( isFirstCompile, stats, environments: context.environments, + isWatch: Boolean(watch), }); isFirstCompile = false; await p; diff --git a/packages/core/src/provider/build.ts b/packages/core/src/provider/build.ts index 7cdb46f010..e7d97ecf22 100644 --- a/packages/core/src/provider/build.ts +++ b/packages/core/src/provider/build.ts @@ -39,6 +39,7 @@ export const build = async ( await context.hooks.onBeforeBuild.call({ bundlerConfigs, environments: context.environments, + isWatch: Boolean(watch), }); const onDone = async (stats: Stats | MultiStats) => { @@ -46,6 +47,7 @@ export const build = async ( isFirstCompile, stats, environments: context.environments, + isWatch: Boolean(watch), }); isFirstCompile = false; await p; diff --git a/packages/core/src/types/hooks.ts b/packages/core/src/types/hooks.ts index 2c64060b2a..c0002ad7ff 100644 --- a/packages/core/src/types/hooks.ts +++ b/packages/core/src/types/hooks.ts @@ -14,6 +14,7 @@ import type { HtmlRspackPlugin, WebpackConfig } from './thirdParty'; import type { MaybePromise, NodeEnv } from './utils'; export type OnBeforeBuildFn = (params: { + isWatch: boolean; bundlerConfigs?: B extends 'rspack' ? Rspack.Configuration[] : WebpackConfig[]; @@ -22,6 +23,7 @@ export type OnBeforeBuildFn = (params: { export type OnAfterBuildFn = (params: { isFirstCompile: boolean; + isWatch: boolean; stats?: Stats | MultiStats; environments: Record; }) => MaybePromise; diff --git a/website/docs/en/shared/onAfterBuild.mdx b/website/docs/en/shared/onAfterBuild.mdx index 2b5c77287a..3e62322c01 100644 --- a/website/docs/en/shared/onAfterBuild.mdx +++ b/website/docs/en/shared/onAfterBuild.mdx @@ -1,6 +1,6 @@ -`onAfterBuild` is a callback function that is triggered after running the production build. You can access the build result information via the [stats](https://webpack.js.org/api/node/#stats-object) parameter: +`onAfterBuild` is a callback function that is triggered after running the production build. You can access the build result information via the [stats](https://webpack.js.org/api/node/#stats-object) parameter. -Moreover, you can use `isFirstCompile` to determine whether it is the first build on watch mode. +Moreover, you can use `isWatch` to determine whether it is watch mode, and use `isFirstCompile` to determine whether it is the first build on watch mode. - **Type:** @@ -8,6 +8,7 @@ Moreover, you can use `isFirstCompile` to determine whether it is the first buil function OnAfterBuild( callback: (params: { isFirstCompile: boolean; + isWatch: boolean; stats?: Stats | MultiStats; environments: Record; }) => Promise | void, diff --git a/website/docs/en/shared/onBeforeBuild.mdx b/website/docs/en/shared/onBeforeBuild.mdx index 611d5bd3bb..4f9bb75350 100644 --- a/website/docs/en/shared/onBeforeBuild.mdx +++ b/website/docs/en/shared/onBeforeBuild.mdx @@ -2,11 +2,14 @@ You can access the Rspack configuration array through the `bundlerConfigs` parameter. The array may contain one or more [Rspack configurations](https://rspack.dev/config/). It depends on whether multiple [environments](/config/environments) are configured. +Moreover, you can use `isWatch` to determine whether it is watch mode. + - **Type:** ```ts function OnBeforeBuild( callback: (params: { + isWatch: boolean; bundlerConfigs?: WebpackConfig[] | RspackConfig[]; environments: Record; }) => Promise | void, diff --git a/website/docs/zh/shared/onAfterBuild.mdx b/website/docs/zh/shared/onAfterBuild.mdx index e0907e94e6..c4607ad256 100644 --- a/website/docs/zh/shared/onAfterBuild.mdx +++ b/website/docs/zh/shared/onAfterBuild.mdx @@ -1,6 +1,6 @@ -`onAfterBuild` 是在执行生产环境构建后触发的回调函数,你可以通过 [stats](https://webpack.js.org/api/node/#stats-object) 参数获取到构建结果信息: +`onAfterBuild` 是在执行生产环境构建后触发的回调函数,你可以通过 [stats](https://webpack.js.org/api/node/#stats-object) 参数获取到构建结果信息。 -另外,在 watch 模式下你可以通过 `isFirstCompile` 来判断是否为首次构建。 +另外,你可以通过 `isWatch` 判断是否是 watch 模式,并在 watch 模式下通过 `isFirstCompile` 来判断是否为首次构建。 - **类型:** @@ -8,6 +8,7 @@ function OnAfterBuild( callback: (params: { isFirstCompile: boolean; + isWatch: boolean; stats?: Stats | MultiStats; environments: Record; }) => Promise | void, diff --git a/website/docs/zh/shared/onBeforeBuild.mdx b/website/docs/zh/shared/onBeforeBuild.mdx index 0ff5b142c1..08f0d9bbf6 100644 --- a/website/docs/zh/shared/onBeforeBuild.mdx +++ b/website/docs/zh/shared/onBeforeBuild.mdx @@ -2,11 +2,14 @@ 你可以通过 `bundlerConfigs` 参数获取到 Rspack 配置数组,数组中可能包含一份或多份 [Rspack 配置](https://rspack.dev/config/),这取决于是否配置了多个 [environments](/config/environments)。 +另外,你可以通过 `isWatch` 判断是否是 watch 模式。 + - **类型:** ```ts function OnBeforeBuild( callback: (params: { + isWatch: boolean; bundlerConfigs?: WebpackConfig[] | RspackConfig[]; environments: Record; }) => Promise | void,