Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: failOnWarnings option #3317

Merged
merged 8 commits into from Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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