Skip to content

Commit

Permalink
feat: failOnWarnings option (#3317)
Browse files Browse the repository at this point in the history
* feat: failOnWarnings option

* feat: failOnWarnings option

* chore: update docs

* chore: enforce exit on warnings

* test: fail on warnings option

* chore: change flag type

* chore: no exit on warning and errors

* chore: suggestion
  • Loading branch information
rishabh3112 committed Jun 30, 2022
1 parent 788c596 commit c48c848
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions OPTIONS.md
Expand Up @@ -16,6 +16,7 @@ Options:
--progress [value] Print compilation progress during build.
--prefetch <value> 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.
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/src/types.ts
Expand Up @@ -210,6 +210,7 @@ interface WebpackRunOptions extends WebpackOptionsNormalized {
json?: boolean;
argv?: Argv;
env: Env;
failOnWarnings?: boolean;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/webpack-cli/src/webpack-cli.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions test/build/build-warnings/warnings.test.js
Expand Up @@ -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"]);

Expand Down

0 comments on commit c48c848

Please sign in to comment.