Skip to content

Commit 8a72a8a

Browse files
fix: fails when failOnError or failOnWarning enabled (#72)
* fix: fails when `failOnError` or `failOnWarning` enabled * fix: failOnError and failOnWarning
1 parent 4daddf5 commit 8a72a8a

File tree

8 files changed

+23
-20
lines changed

8 files changed

+23
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ The warnings found will always be emitted, to disable set to `false`.
188188
#### `failOnError`
189189

190190
- Type: `Boolean`
191-
- Default: `false`
191+
- Default: `true`
192192

193-
Will cause the module build to fail if there are any errors, if set to `true`.
193+
Will cause the module build to fail if there are any errors, to disable set to `false`.
194194

195195
#### `failOnWarning`
196196

src/ESLintError.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// @ts-ignore
2-
import WebpackError from 'webpack/lib/WebpackError';
3-
4-
export default class ESLintError extends WebpackError {
1+
class ESLintError extends Error {
52
/**
63
* @param {string=} messages
74
*/
@@ -11,3 +8,5 @@ export default class ESLintError extends WebpackError {
118
this.stack = '';
129
}
1310
}
11+
12+
export default ESLintError;

src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,25 @@ class ESLintWebpackPlugin {
109109

110110
// Gather Files to lint
111111
compilation.hooks.succeedModule.tap(ESLINT_PLUGIN, processModule);
112+
112113
// await and interpret results
113-
compilation.hooks.afterSeal.tapPromise(ESLINT_PLUGIN, processResults);
114+
compilation.hooks.additionalAssets.tapPromise(
115+
ESLINT_PLUGIN,
116+
processResults
117+
);
114118

115119
async function processResults() {
116120
const { errors, warnings, generateReportAsset } = await report();
117121

118-
if (warnings) {
122+
if (warnings && !options.failOnWarning) {
119123
// @ts-ignore
120124
compilation.warnings.push(warnings);
125+
} else if (warnings && options.failOnWarning) {
126+
// @ts-ignore
127+
compilation.errors.push(warnings);
121128
}
122129

123-
if (errors) {
130+
if (errors && options.failOnError) {
124131
// @ts-ignore
125132
compilation.errors.push(errors);
126133
}

src/linter.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ export default function linter(key, options, compilation) {
9494
parseResults(options, results)
9595
);
9696

97-
if (options.failOnError && errors) {
98-
throw errors;
99-
} else if (options.failOnWarning && warnings) {
100-
throw warnings;
101-
}
102-
10397
return {
10498
errors,
10599
warnings,

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function getOptions(pluginOptions) {
5050
extensions: 'js',
5151
emitError: true,
5252
emitWarning: true,
53+
failOnError: true,
5354
...pluginOptions,
5455
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
5556
};

src/options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"anyOf": [{ "type": "string" }, { "type": "array" }]
2424
},
2525
"failOnError": {
26-
"description": "Will cause the module build to fail if there are any errors, if set to `true`.",
26+
"description": "Will cause the module build to fail if there are any errors, to disable set to `false`.",
2727
"type": "boolean"
2828
},
2929
"failOnWarning": {

test/fail-on-error.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ describe('fail on error', () => {
44
it('should emits errors', (done) => {
55
const compiler = pack('error', { failOnError: true });
66

7-
compiler.run((err) => {
8-
expect(err.message).toContain('error.js');
7+
compiler.run((err, stats) => {
8+
expect(err).toBeNull();
9+
expect(stats.hasErrors()).toBe(true);
910
done();
1011
});
1112
});

test/fail-on-warning.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ describe('fail on warning', () => {
44
it('should emits errors', (done) => {
55
const compiler = pack('warn', { failOnWarning: true });
66

7-
compiler.run((err) => {
8-
expect(err.message).toContain('warn.js');
7+
compiler.run((err, stats) => {
8+
expect(err).toBeNull();
9+
expect(stats.hasErrors()).toBe(true);
910
done();
1011
});
1112
});

0 commit comments

Comments
 (0)