More verbose error messages

This commit is contained in:
Jan Nicklas 2015-07-08 11:27:53 +02:00 committed by Jan Nicklas
parent 2365ded8da
commit b0af6a3d42
1 changed files with 22 additions and 4 deletions

View File

@ -44,7 +44,14 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
compiler.plugin('make', function(compilation, callback) {
// Compile the template
compilationPromise = self.compileTemplate(self.options.template, self.options.filename, compilation);
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);
});
@ -68,6 +75,10 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
return compilationPromise;
})
.then(function(resultAsset) {
// Allow to use a custom function / string instead
if (self.options.templateContent) {
return self.options.templateContent;
}
// Once everything is compiled evaluate the html factory
// and replace it with its content
return self.evaluateCompilationResult(compilation, resultAsset);
@ -145,10 +156,13 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename,
});
// Compile and return a promise
return new Promise(function (resolve, reject) {
childCompiler.runAsChild(function(err) {
childCompiler.runAsChild(function(err, entries, childCompilation) {
// Resolve / reject the promise
if (err) {
reject(err);
if (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));
} else {
resolve(compilation.assets[outputFilename]);
}
@ -164,6 +178,10 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, co
if(!compilationResult) {
return Promise.reject('The child compilation didn\'t provide a result');
}
if(typeof source === 'string') {
// If the compilation result is already evaluted return it
return Promise.resolve(source);
}
// Strip the leading 'var '
var source = compilationResult.source().substr(3);
// Evaluate code and cast to string