diff --git a/packages/rspack-cli/src/cli.ts b/packages/rspack-cli/src/cli.ts index 4dab69bff2b5..0b1d6e69015b 100644 --- a/packages/rspack-cli/src/cli.ts +++ b/packages/rspack-cli/src/cli.ts @@ -70,7 +70,11 @@ export class RspackCLI { this.getLogger().error(e.message); process.exit(2); } else if (e instanceof Error) { - callback?.(e); + if (typeof callback === "function") { + callback(e); + } else { + this.getLogger().error(e); + } return null; } throw e; diff --git a/packages/rspack-cli/tests/serve/plugin-apply-error/rspack.config.js b/packages/rspack-cli/tests/serve/plugin-apply-error/rspack.config.js new file mode 100644 index 000000000000..e92bd88bf264 --- /dev/null +++ b/packages/rspack-cli/tests/serve/plugin-apply-error/rspack.config.js @@ -0,0 +1,13 @@ +module.exports = { + mode: "development", + devtool: false, + plugins: [ + { + name: "test-plugin", + apply(compiler) { + throw new Error("error in plugin"); + } + } + ], + devServer: {} +}; diff --git a/packages/rspack-cli/tests/serve/plugin-apply-error/serve-plugin-apply-error.test.ts b/packages/rspack-cli/tests/serve/plugin-apply-error/serve-plugin-apply-error.test.ts new file mode 100644 index 000000000000..7aa953cfea71 --- /dev/null +++ b/packages/rspack-cli/tests/serve/plugin-apply-error/serve-plugin-apply-error.test.ts @@ -0,0 +1,11 @@ +import { normalizeStderr, runWatch } from "../../utils/test-utils"; + +describe("should display plugin error", () => { + it("display error", async () => { + const { stderr } = await runWatch(__dirname, ["serve"], { + killString: /Error: / + }); + + expect(normalizeStderr(stderr)).toContain("error in plugin"); + }); +});