From 222853208ff94bc67d61df7a9dacd55114d4e1c6 Mon Sep 17 00:00:00 2001 From: Alexander Nanberg Date: Sat, 1 Feb 2020 19:17:48 +0100 Subject: [PATCH] feat: log error on fail hook --- src/friendly-errors-plugin.js | 8 ++++++++ test/fixtures/error/index.js | 1 + test/fixtures/error/webpack.config.js | 19 +++++++++++++++++++ test/integration.spec.js | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 test/fixtures/error/index.js create mode 100644 test/fixtures/error/webpack.config.js diff --git a/src/friendly-errors-plugin.js b/src/friendly-errors-plugin.js index 57005ef..c109746 100644 --- a/src/friendly-errors-plugin.js +++ b/src/friendly-errors-plugin.js @@ -63,11 +63,19 @@ class FriendlyErrorsWebpackPlugin { output.title('info', 'WAIT', 'Compiling...'); }; + const failedFn = (error) => { + this.clearConsole(); + this.displayErrors([error], 'error'); + } + if (compiler.hooks) { const plugin = { name: 'FriendlyErrorsWebpackPlugin' }; compiler.hooks.done.tap(plugin, doneFn); compiler.hooks.invalid.tap(plugin, invalidFn); + if (compiler.hooks.failed) { + compiler.hooks.failed.tap(plugin, failedFn) + } } else { compiler.plugin('done', doneFn); compiler.plugin('invalid', invalidFn); diff --git a/test/fixtures/error/index.js b/test/fixtures/error/index.js new file mode 100644 index 0000000..46252bb --- /dev/null +++ b/test/fixtures/error/index.js @@ -0,0 +1 @@ +module.exports = "I am an entry point"; diff --git a/test/fixtures/error/webpack.config.js b/test/fixtures/error/webpack.config.js new file mode 100644 index 0000000..0680da2 --- /dev/null +++ b/test/fixtures/error/webpack.config.js @@ -0,0 +1,19 @@ +const FriendlyErrorsWebpackPlugin = require("../../../index"); + +class WebpackPluginWithError { + apply(compiler) { + compiler.hooks.run.tap("WebpackPluginWithError", compilation => { + throw new Error("Error"); + }); + } +} + +module.exports = { + mode: "development", + entry: __dirname + "/index.js", + output: { + path: __dirname + "/dist", + filename: "bundle.js" + }, + plugins: [new FriendlyErrorsWebpackPlugin(), new WebpackPluginWithError()] +}; diff --git a/test/integration.spec.js b/test/integration.spec.js index b107742..5e163d7 100644 --- a/test/integration.spec.js +++ b/test/integration.spec.js @@ -221,4 +221,23 @@ Warning (3:2) grid-auto-flow works only if grid-template-rows and grid-template-columns are present in the same rule`, '' ]); +}); + +it('integration : error', (done) => { + const compiler = webpack(require('./fixtures/error/webpack.config')); + compiler.outputFileSystem = new MemoryFileSystem(); + + output.capture(); + compiler.run(() => { + expect(output.capturedMessages).toEqual([ + 'ERROR Failed to compile with 1 errors', + '', + 'error', + '', + 'Error', + '' + ]); + output.endCapture(); + done(); + }) }); \ No newline at end of file