Skip to content

Commit

Permalink
feat: add isWatch param to determine whether it is watch mode when …
Browse files Browse the repository at this point in the history
…build (#3065)
  • Loading branch information
9aoy authored Jul 30, 2024
1 parent cd6fb8a commit 253ca44
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/compat/webpack/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ 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) => {
const p = context.hooks.onAfterBuild.call({
isFirstCompile,
stats,
environments: context.environments,
isWatch: Boolean(watch),
});
isFirstCompile = false;
await p;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/provider/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export const build = async (
await context.hooks.onBeforeBuild.call({
bundlerConfigs,
environments: context.environments,
isWatch: Boolean(watch),
});

const onDone = async (stats: Stats | MultiStats) => {
const p = context.hooks.onAfterBuild.call({
isFirstCompile,
stats,
environments: context.environments,
isWatch: Boolean(watch),
});
isFirstCompile = false;
await p;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { HtmlRspackPlugin, WebpackConfig } from './thirdParty';
import type { MaybePromise, NodeEnv } from './utils';

export type OnBeforeBuildFn<B = 'rspack'> = (params: {
isWatch: boolean;
bundlerConfigs?: B extends 'rspack'
? Rspack.Configuration[]
: WebpackConfig[];
Expand All @@ -22,6 +23,7 @@ export type OnBeforeBuildFn<B = 'rspack'> = (params: {

export type OnAfterBuildFn = (params: {
isFirstCompile: boolean;
isWatch: boolean;
stats?: Stats | MultiStats;
environments: Record<string, EnvironmentContext>;
}) => MaybePromise<void>;
Expand Down
5 changes: 3 additions & 2 deletions website/docs/en/shared/onAfterBuild.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
`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:**

```ts
function OnAfterBuild(
callback: (params: {
isFirstCompile: boolean;
isWatch: boolean;
stats?: Stats | MultiStats;
environments: Record<string, EnvironmentContext>;
}) => Promise<void> | void,
Expand Down
3 changes: 3 additions & 0 deletions website/docs/en/shared/onBeforeBuild.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, EnvironmentContext>;
}) => Promise<void> | void,
Expand Down
5 changes: 3 additions & 2 deletions website/docs/zh/shared/onAfterBuild.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
`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` 来判断是否为首次构建。

- **类型:**

```ts
function OnAfterBuild(
callback: (params: {
isFirstCompile: boolean;
isWatch: boolean;
stats?: Stats | MultiStats;
environments: Record<string, EnvironmentContext>;
}) => Promise<void> | void,
Expand Down
3 changes: 3 additions & 0 deletions website/docs/zh/shared/onBeforeBuild.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, EnvironmentContext>;
}) => Promise<void> | void,
Expand Down

0 comments on commit 253ca44

Please sign in to comment.