From 55fcb6d939c69c0818405f7c3df78671a79add51 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Fri, 4 Sep 2015 17:46:13 +0200 Subject: [PATCH] Fix displaying loader errors. --- index.js | 20 ++++++++++---------- spec/HtmlWebpackPluginSpec.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 1d7b7ea..18ac620 100644 --- a/index.js +++ b/index.js @@ -48,13 +48,9 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { // Compile the template compilationPromise = self.compileTemplate(self.options.template, self.options.filename, compilation) .catch(function(err) { - // In case anything went wrong the promise is resolved - // with the error message and an error is logged - var errorMessage = "HtmlWebpackPlugin Error: " + err; - compilation.errors.push(new Error(errorMessage)); - return errorMessage; - }); - compilationPromise.finally(callback); + return new Error(err); + }) + .finally(callback); }); compiler.plugin('emit', function(compilation, callback) { @@ -77,6 +73,9 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { return compilationPromise; }) .then(function(resultAsset) { + if (resultAsset instanceof Error) { + return Promise.reject(resultAsset); + } // Allow to use a custom function / string instead if (self.options.templateContent) { return self.options.templateContent; @@ -99,7 +98,7 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { .catch(function(err) { // In case anything went wrong the promise is resolved // with the error message and an error is logged - var errorMessage = "HtmlWebpackPlugin Error: " + err; + var errorMessage = "HtmlWebpackPlugin " + err; compilation.errors.push(new Error(errorMessage)); return errorMessage; }) @@ -165,11 +164,12 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename, return new Promise(function (resolve, reject) { childCompiler.runAsChild(function(err, entries, childCompilation) { // Resolve / reject the promise - if (childCompilation.errors.length) { + if (childCompilation.errors && childCompilation.errors.length) { var errorDetails = childCompilation.errors.map(function(error) { return error.message + (error.error ? ':\n' + error.error: ''); }).join('\n'); - reject(new Error('Child compilation failed:\n' + errorDetails)); + + reject('Child compilation failed:\n' + errorDetails); } else { resolve(compilation.assets[outputFilename]); } diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index d80f35e..76efc41 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -502,4 +502,19 @@ describe('HtmlWebpackPlugin', function() { }, ['Error: HtmlWebpackPlugin: could not load file'], null, done, true); }); + it('shows an error when a template fails to load', function(done) { + testHtmlPlugin({ + entry: path.join(__dirname, 'fixtures/index.js'), + output: { + path: OUTPUT_DIR, + filename: 'index_bundle.js' + }, + plugins: [ + new HtmlWebpackPlugin({ + template: path.join(__dirname, 'fixtures/non-existing-template.html') + }) + ] + }, ["HtmlWebpackPlugin Error: Child compilation failed:\nEntry module not found: Error: Cannot resolve 'file' or 'directory'"], null, done, true); + }); + });