From 852bc344a9a150cd56a45c2c7a76a090a1deb301 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 08:32:31 +0530 Subject: [PATCH 1/8] feat: failOnWarnings option --- packages/webpack-cli/src/types.ts | 1 + packages/webpack-cli/src/webpack-cli.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) 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..fc428c37c62 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: "boolean", + }, + ], + negative: false, + description: "Stop webpack-cli process on non-zero exit code", + }, ]; // Extract all the flags being exported from core. @@ -2477,6 +2487,10 @@ class WebpackCLI implements IWebpackCLI { if (printedStats) { this.logger.raw(printedStats); } + + if (options.failOnWarnings && stats.hasErrors()) { + process.exit(1); + } } }; From a4f0cc1de777a5ab1bbe212f2154c8d3e75db20d Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 08:35:42 +0530 Subject: [PATCH 2/8] feat: failOnWarnings option --- packages/webpack-cli/src/webpack-cli.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index fc428c37c62..600090eaa20 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2487,10 +2487,10 @@ class WebpackCLI implements IWebpackCLI { if (printedStats) { this.logger.raw(printedStats); } + } - if (options.failOnWarnings && stats.hasErrors()) { - process.exit(1); - } + if (options.failOnWarnings && stats.hasErrors()) { + process.exit(1); } }; From fd72db7b851b4834cbae2ed0f433f172c33d8da3 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 08:47:37 +0530 Subject: [PATCH 3/8] chore: update docs --- OPTIONS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/OPTIONS.md b/OPTIONS.md index de111ab94a7..3517b57cf9b 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -16,6 +16,8 @@ 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 on non-zero exit code + --no-fail-on-warnings Negative 'fail-on-warnings' option. --no-amd Negative 'amd' option. --bail Report the first error as a hard error instead of tolerating it. --no-bail Negative 'bail' option. From 5bfd563afe858bdef73c3a95bbfe3bd219e2f43c Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 09:03:48 +0530 Subject: [PATCH 4/8] chore: enforce exit on warnings --- OPTIONS.md | 2 +- packages/webpack-cli/src/webpack-cli.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OPTIONS.md b/OPTIONS.md index 3517b57cf9b..d2195711f7f 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -16,7 +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 on non-zero exit code + --fail-on-warnings Stop webpack-cli process with non-zero exit code on warnings from webpack --no-fail-on-warnings Negative 'fail-on-warnings' option. --no-amd Negative 'amd' option. --bail Report the first error as a hard error instead of tolerating it. diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 600090eaa20..8e2e756b750 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -984,7 +984,7 @@ class WebpackCLI implements IWebpackCLI { }, ], negative: false, - description: "Stop webpack-cli process on non-zero exit code", + description: "Stop webpack-cli process with non-zero exit code on warnings from webpack", }, ]; @@ -2489,7 +2489,7 @@ class WebpackCLI implements IWebpackCLI { } } - if (options.failOnWarnings && stats.hasErrors()) { + if (options.failOnWarnings && (stats.hasErrors() || stats.hasWarnings())) { process.exit(1); } }; From dadf7a9e7946b282f50277d7c3efab4e2f10c5f7 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 09:26:41 +0530 Subject: [PATCH 5/8] test: fail on warnings option --- test/build/build-warnings/warnings.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) 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"]); From 1dbc0be2cda0d0e939f3ec401856b6901adcc160 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 14:52:23 +0530 Subject: [PATCH 6/8] chore: change flag type --- OPTIONS.md | 1 - packages/webpack-cli/src/webpack-cli.ts | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OPTIONS.md b/OPTIONS.md index d2195711f7f..fc321c2ef0f 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -16,7 +16,6 @@ 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-fail-on-warnings Negative 'fail-on-warnings' option. --no-amd Negative 'amd' option. --bail Report the first error as a hard error instead of tolerating it. diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 8e2e756b750..edc727d44ae 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -980,10 +980,11 @@ class WebpackCLI implements IWebpackCLI { name: "fail-on-warnings", configs: [ { - type: "boolean", + type: "enum", + values: [false], }, ], - negative: false, + negative: true, description: "Stop webpack-cli process with non-zero exit code on warnings from webpack", }, ]; From e0fe1a1d55d64d38ae7d19b9ce7815da4ba6bad5 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 15:02:17 +0530 Subject: [PATCH 7/8] chore: no exit on warning and errors --- packages/webpack-cli/src/webpack-cli.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index edc727d44ae..ba65a9e8acf 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -984,7 +984,6 @@ class WebpackCLI implements IWebpackCLI { values: [false], }, ], - negative: true, description: "Stop webpack-cli process with non-zero exit code on warnings from webpack", }, ]; @@ -2427,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; } @@ -2489,10 +2488,6 @@ class WebpackCLI implements IWebpackCLI { this.logger.raw(printedStats); } } - - if (options.failOnWarnings && (stats.hasErrors() || stats.hasWarnings())) { - process.exit(1); - } }; const env = From aaa1d84b632a3add711e4e47ce9495ec48cc7ea6 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 29 Jun 2022 15:03:43 +0530 Subject: [PATCH 8/8] chore: suggestion --- OPTIONS.md | 2 +- packages/webpack-cli/src/webpack-cli.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OPTIONS.md b/OPTIONS.md index fc321c2ef0f..e3b000020b4 100644 --- a/OPTIONS.md +++ b/OPTIONS.md @@ -16,7 +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. - --no-fail-on-warnings Negative 'fail-on-warnings' option. + --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/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index ba65a9e8acf..e1a78afc1b6 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -981,7 +981,7 @@ class WebpackCLI implements IWebpackCLI { configs: [ { type: "enum", - values: [false], + values: [true], }, ], description: "Stop webpack-cli process with non-zero exit code on warnings from webpack",