Skip to content

Commit

Permalink
fix(cli): rspack build --watch not work (#2280) (#2316)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter authored Mar 17, 2023
1 parent 6d482a7 commit a0994a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/shaggy-hairs-float.md
Original file line number Diff line number Diff line change
@@ -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))
19 changes: 15 additions & 4 deletions packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
);
}
Expand Down
25 changes: 22 additions & 3 deletions packages/rspack-cli/src/rspack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,7 +27,8 @@ export class RspackCLI {
}
async createCompiler(
options: RspackCLIOptions,
rspackEnv: RspackEnv
rspackEnv: RspackEnv,
callback?: (e: Error, res?: Stats | MultiStats) => void
): Promise<Compiler | MultiCompiler> {
process.env.RSPACK_CONFIG_VALIDATE = "loose";
let nodeEnv = process?.env?.NODE_ENV;
Expand All @@ -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 {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./compilation";
export * from "./config";
export * from "./rspack";
export * from "./stats";
export * from "./multiStats";
export * from "./chunk_group";
2 changes: 2 additions & 0 deletions packages/rspack/src/multiStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ export default class MultiStats {
return results.filter(Boolean).join("\n\n");
}
}

export { MultiStats };

0 comments on commit a0994a8

Please sign in to comment.