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 6 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.
--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.
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
15 changes: 15 additions & 0 deletions packages/webpack-cli/src/webpack-cli.ts
Expand Up @@ -976,6 +976,17 @@ 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: [false],
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
},
],
negative: true,
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2478,6 +2489,10 @@ class WebpackCLI implements IWebpackCLI {
this.logger.raw(printedStats);
}
}

if (options.failOnWarnings && (stats.hasErrors() || stats.hasWarnings())) {
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
process.exit(1);
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
}
};

const env =
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