diff --git a/lib/Compiler.js b/lib/Compiler.js index bd7474d8f..2bdd15b99 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -545,8 +545,21 @@ class Compiler { */ runAsChild(callback) { const startTime = Date.now(); + + const finalCallback = (err, entries, compilation) => { + try { + callback(err, entries, compilation); + } catch (e) { + const err = new WebpackError( + `compiler.runAsChild callback error: ${e}` + ); + err.details = e.stack; + this.parentCompilation.errors.push(err); + } + }; + this.compile((err, compilation) => { - if (err) return callback(err); + if (err) return finalCallback(err); this.parentCompilation.children.push(compilation); for (const { name, source, info } of compilation.getAssets()) { @@ -561,7 +574,7 @@ class Compiler { compilation.startTime = startTime; compilation.endTime = Date.now(); - return callback(null, entries, compilation); + return finalCallback(null, entries, compilation); }); }