From 09b71b61f8d0d339ef9302de15a50a3800c9d140 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Mon, 1 Feb 2021 10:26:59 -0300 Subject: [PATCH 1/2] fix: fails when `failOnError` or `failOnWarning` enabled --- README.md | 4 ++-- src/ESLintError.js | 7 +++---- src/index.js | 3 ++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b6c0072..48e9111 100644 --- a/README.md +++ b/README.md @@ -190,14 +190,14 @@ Will always return warnings, if set to `true`. - Type: `Boolean` - Default: `false` -Will cause the module build to fail if there are any errors, if set to `true`. +Will cause the module build to fail and not emitted files if there are any errors, if set to `true`. #### `failOnWarning` - Type: `Boolean` - Default: `false` -Will cause the module build to fail if there are any warnings, if set to `true`. +Will cause the module build to fail and not emitted files if there are any warnings, if set to `true`. #### `quiet` diff --git a/src/ESLintError.js b/src/ESLintError.js index 1d4054c..171bd8d 100644 --- a/src/ESLintError.js +++ b/src/ESLintError.js @@ -1,7 +1,4 @@ -// @ts-ignore -import WebpackError from 'webpack/lib/WebpackError'; - -export default class ESLintError extends WebpackError { +class ESLintError extends Error { /** * @param {string=} messages */ @@ -11,3 +8,5 @@ export default class ESLintError extends WebpackError { this.stack = ''; } } + +export default ESLintError; diff --git a/src/index.js b/src/index.js index 94dc444..06d5fcc 100644 --- a/src/index.js +++ b/src/index.js @@ -112,8 +112,9 @@ export class ESLintWebpackPlugin { // Gather Files to lint compilation.hooks.succeedModule.tap(ESLINT_PLUGIN, processModule); + // await and interpret results - compilation.hooks.afterSeal.tapPromise(ESLINT_PLUGIN, processResults); + compilation.compiler.hooks.emit.tapPromise(ESLINT_PLUGIN, processResults); async function processResults() { const { errors, warnings, generateReportAsset } = await report(); From b059ef3386f4533392e93c4e958a084d4dcae458 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Mon, 1 Feb 2021 11:51:22 -0300 Subject: [PATCH 2/2] fix: failOnError and failOnWarning --- README.md | 6 +++--- src/index.js | 12 +++++++++--- src/linter.js | 6 ------ src/options.js | 1 + src/options.json | 2 +- test/fail-on-error.test.js | 5 +++-- test/fail-on-warning.test.js | 5 +++-- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 48e9111..1b3db50 100644 --- a/README.md +++ b/README.md @@ -188,16 +188,16 @@ Will always return warnings, if set to `true`. #### `failOnError` - Type: `Boolean` -- Default: `false` +- Default: `true` -Will cause the module build to fail and not emitted files if there are any errors, if set to `true`. +Will cause the module build to fail if there are any errors, to disable set to `false`. #### `failOnWarning` - Type: `Boolean` - Default: `false` -Will cause the module build to fail and not emitted files if there are any warnings, if set to `true`. +Will cause the module build to fail if there are any warnings, if set to `true`. #### `quiet` diff --git a/src/index.js b/src/index.js index 06d5fcc..e68bbcc 100644 --- a/src/index.js +++ b/src/index.js @@ -114,17 +114,23 @@ export class ESLintWebpackPlugin { compilation.hooks.succeedModule.tap(ESLINT_PLUGIN, processModule); // await and interpret results - compilation.compiler.hooks.emit.tapPromise(ESLINT_PLUGIN, processResults); + compilation.hooks.additionalAssets.tapPromise( + ESLINT_PLUGIN, + processResults + ); async function processResults() { const { errors, warnings, generateReportAsset } = await report(); - if (warnings) { + if (warnings && !options.failOnWarning) { // @ts-ignore compilation.warnings.push(warnings); + } else if (warnings && options.failOnWarning) { + // @ts-ignore + compilation.errors.push(warnings); } - if (errors) { + if (errors && options.failOnError) { // @ts-ignore compilation.errors.push(errors); } diff --git a/src/linter.js b/src/linter.js index e4605b6..f8d7967 100644 --- a/src/linter.js +++ b/src/linter.js @@ -94,12 +94,6 @@ export default function linter(key, options, compilation) { parseResults(options, results) ); - if (options.failOnError && errors) { - throw errors; - } else if (options.failOnWarning && warnings) { - throw warnings; - } - return { errors, warnings, diff --git a/src/options.js b/src/options.js index a42e6ac..b235ad6 100644 --- a/src/options.js +++ b/src/options.js @@ -47,6 +47,7 @@ import schema from './options.json'; export function getOptions(pluginOptions) { const options = { extensions: 'js', + failOnError: true, ...pluginOptions, ...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}), }; diff --git a/src/options.json b/src/options.json index c104396..68f77fc 100644 --- a/src/options.json +++ b/src/options.json @@ -23,7 +23,7 @@ "anyOf": [{ "type": "string" }, { "type": "array" }] }, "failOnError": { - "description": "Will cause the module build to fail if there are any errors, if set to `true`.", + "description": "Will cause the module build to fail if there are any errors, to disable set to `false`.", "type": "boolean" }, "failOnWarning": { diff --git a/test/fail-on-error.test.js b/test/fail-on-error.test.js index a670a40..711cbf7 100644 --- a/test/fail-on-error.test.js +++ b/test/fail-on-error.test.js @@ -4,8 +4,9 @@ describe('fail on error', () => { it('should emits errors', (done) => { const compiler = pack('error', { failOnError: true }); - compiler.run((err) => { - expect(err.message).toContain('error.js'); + compiler.run((err, stats) => { + expect(err).toBeNull(); + expect(stats.hasErrors()).toBe(true); done(); }); }); diff --git a/test/fail-on-warning.test.js b/test/fail-on-warning.test.js index 28c1673..e654541 100644 --- a/test/fail-on-warning.test.js +++ b/test/fail-on-warning.test.js @@ -4,8 +4,9 @@ describe('fail on warning', () => { it('should emits errors', (done) => { const compiler = pack('warn', { failOnWarning: true }); - compiler.run((err) => { - expect(err.message).toContain('warn.js'); + compiler.run((err, stats) => { + expect(err).toBeNull(); + expect(stats.hasErrors()).toBe(true); done(); }); });