diff --git a/OPTIONS.md b/OPTIONS.md index de111ab94a7..e3b000020b4 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -16,6 +16,7 @@ Options: --progress [value] Print compilation progress during build. --prefetch Prefetch this request. -j, --json [value] Prints result as JSON or store it in a file. + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack --no-amd Negative 'amd' option. --bail Report the first error as a hard error instead of tolerating it. --no-bail Negative 'bail' option. diff --git a/packages/webpack-cli/src/types.ts b/packages/webpack-cli/src/types.ts index 3931e96987a..c1875698f19 100644 --- a/packages/webpack-cli/src/types.ts +++ b/packages/webpack-cli/src/types.ts @@ -210,6 +210,7 @@ interface WebpackRunOptions extends WebpackOptionsNormalized { json?: boolean; argv?: Argv; env: Env; + failOnWarnings?: boolean; } /** diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 9a0ffeef947..e1a78afc1b6 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -976,6 +976,16 @@ class WebpackCLI implements IWebpackCLI { description: "Stop watching when stdin stream has ended.", negatedDescription: "Do not stop watching when stdin stream has ended.", }, + { + name: "fail-on-warnings", + configs: [ + { + type: "enum", + values: [true], + }, + ], + description: "Stop webpack-cli process with non-zero exit code on warnings from webpack", + }, ]; // Extract all the flags being exported from core. @@ -2416,7 +2426,7 @@ class WebpackCLI implements IWebpackCLI { process.exit(2); } - if (stats && stats.hasErrors()) { + if (stats && (stats.hasErrors() || (options.failOnWarnings && stats.hasWarnings()))) { process.exitCode = 1; } diff --git a/test/build/build-warnings/warnings.test.js b/test/build/build-warnings/warnings.test.js index 61f03a3434c..d249218539b 100644 --- a/test/build/build-warnings/warnings.test.js +++ b/test/build/build-warnings/warnings.test.js @@ -14,6 +14,15 @@ describe("warnings", () => { expect(stdout).toMatch(/Error: Can't resolve/); }); + it("should exit with non-zero code on --fail-on-warnings flag", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, ["--fail-on-warnings"]); + + expect(exitCode).toBe(1); + expect(stderr).toBeFalsy(); + expect(stdout).toMatch(/WARNING/); + expect(stdout).toMatch(/Error: Can't resolve/); + }); + it('should output JSON with the "json" flag', async () => { const { exitCode, stderr, stdout } = await run(__dirname, ["--json"]);