Skip to content

Commit

Permalink
Merge pull request #15578 from webpack/feat/catch-error-in-run-as-child
Browse files Browse the repository at this point in the history
catch error in runAsChild callback
  • Loading branch information
sokra committed Apr 1, 2022
2 parents c3f5897 + 3929e68 commit 4a0937f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/Compiler.js
Expand Up @@ -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()) {
Expand All @@ -561,7 +574,7 @@ class Compiler {
compilation.startTime = startTime;
compilation.endTime = Date.now();

return callback(null, entries, compilation);
return finalCallback(null, entries, compilation);
});
}

Expand Down

0 comments on commit 4a0937f

Please sign in to comment.