From a0994a824f65605038e602645b1deb9314daecb1 Mon Sep 17 00:00:00 2001 From: SoonIter Date: Fri, 17 Mar 2023 13:33:00 +0800 Subject: [PATCH] fix(cli): rspack build --watch not work (#2280) (#2316) --- .changeset/shaggy-hairs-float.md | 6 ++++++ packages/rspack-cli/src/commands/build.ts | 19 +++++++++++++---- packages/rspack-cli/src/rspack-cli.ts | 25 ++++++++++++++++++++--- packages/rspack/src/index.ts | 1 + packages/rspack/src/multiStats.ts | 2 ++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 .changeset/shaggy-hairs-float.md diff --git a/.changeset/shaggy-hairs-float.md b/.changeset/shaggy-hairs-float.md new file mode 100644 index 00000000000..69bb19688c8 --- /dev/null +++ b/.changeset/shaggy-hairs-float.md @@ -0,0 +1,6 @@ +--- +"@rspack/core": patch +"@rspack/cli": patch +--- + +fix(cli): rspack build --watch not work ([#2280](https://github.com/web-infra-dev/rspack/issues/2280)) diff --git a/packages/rspack-cli/src/commands/build.ts b/packages/rspack-cli/src/commands/build.ts index bfa1d228b5c..02f640ccdb8 100644 --- a/packages/rspack-cli/src/commands/build.ts +++ b/packages/rspack-cli/src/commands/build.ts @@ -3,7 +3,7 @@ import type { RspackCLI } from "../rspack-cli"; import { RspackCommand } from "../types"; import { commonOptions } from "../utils/options"; import { Stats } from "@rspack/core/src/stats"; -import { Compiler } from "@rspack/core"; +import { Compiler, MultiCompiler } from "@rspack/core"; import MultiStats from "@rspack/core/src/multiStats"; export class BuildCommand implements RspackCommand { @@ -86,11 +86,22 @@ export class BuildCommand implements RspackCommand { console.time("build"); let rspackOptions = { ...options, argv: { ...options } }; - const compiler = await cli.createCompiler(rspackOptions, "production"); - compiler.run((err, Stats) => { + const errorHandler = (err, Stats) => { callback(err, Stats); console.timeEnd("build"); - }); + }; + + const compiler = await cli.createCompiler( + rspackOptions, + "production", + errorHandler + ); + + if (cli.isWatch(compiler)) { + return; + } else { + compiler.run(errorHandler); + } } ); } diff --git a/packages/rspack-cli/src/rspack-cli.ts b/packages/rspack-cli/src/rspack-cli.ts index d4b94f54c38..10aca4580a2 100644 --- a/packages/rspack-cli/src/rspack-cli.ts +++ b/packages/rspack-cli/src/rspack-cli.ts @@ -9,7 +9,9 @@ import { MultiCompiler, Compiler, rspack, - MultiRspackOptions + MultiRspackOptions, + Stats, + MultiStats } from "@rspack/core"; import { normalizeEnv } from "./utils/options"; import { loadRspackConfig } from "./utils/loadConfig"; @@ -25,7 +27,8 @@ export class RspackCLI { } async createCompiler( options: RspackCLIOptions, - rspackEnv: RspackEnv + rspackEnv: RspackEnv, + callback?: (e: Error, res?: Stats | MultiStats) => void ): Promise { process.env.RSPACK_CONFIG_VALIDATE = "loose"; let nodeEnv = process?.env?.NODE_ENV; @@ -36,8 +39,13 @@ export class RspackCLI { } let config = await this.loadConfig(options); config = await this.buildConfig(config, options, rspackEnv); + + const isWatch = Array.isArray(config) + ? (config as MultiRspackOptions).some(i => i.watch) + : (config as RspackOptions).watch; + // @ts-ignore - const compiler = rspack(config); + const compiler = rspack(config, isWatch ? callback : undefined); return compiler; } createColors(useColor?: boolean): RspackCLIColors { @@ -102,6 +110,10 @@ export class RspackCLI { } }); } + // cli --watch overrides the watch config + if (options.watch) { + item.watch = options.watch; + } // auto set default mode if user config don't set it if (!item.mode) { item.mode = rspackEnv ?? "none"; @@ -228,6 +240,13 @@ export class RspackCLI { ): compiler is MultiCompiler { return Boolean((compiler as MultiCompiler).compilers); } + isWatch(compiler: Compiler | MultiCompiler): boolean { + return Boolean( + this.isMultipleCompiler(compiler) + ? compiler.compilers.some(compiler => compiler.options.watch) + : compiler.options.watch + ); + } } export function defineConfig(config: RspackOptions): RspackOptions { diff --git a/packages/rspack/src/index.ts b/packages/rspack/src/index.ts index f7d999d598b..0ac554992ca 100644 --- a/packages/rspack/src/index.ts +++ b/packages/rspack/src/index.ts @@ -4,4 +4,5 @@ export * from "./compilation"; export * from "./config"; export * from "./rspack"; export * from "./stats"; +export * from "./multiStats"; export * from "./chunk_group"; diff --git a/packages/rspack/src/multiStats.ts b/packages/rspack/src/multiStats.ts index 16cb626f533..5803d992552 100644 --- a/packages/rspack/src/multiStats.ts +++ b/packages/rspack/src/multiStats.ts @@ -148,3 +148,5 @@ export default class MultiStats { return results.filter(Boolean).join("\n\n"); } } + +export { MultiStats };