Skip to content

Commit 4a0937f

Browse files
authored
Merge pull request #15578 from webpack/feat/catch-error-in-run-as-child
catch error in runAsChild callback
2 parents c3f5897 + 3929e68 commit 4a0937f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/Compiler.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,21 @@ class Compiler {
545545
*/
546546
runAsChild(callback) {
547547
const startTime = Date.now();
548+
549+
const finalCallback = (err, entries, compilation) => {
550+
try {
551+
callback(err, entries, compilation);
552+
} catch (e) {
553+
const err = new WebpackError(
554+
`compiler.runAsChild callback error: ${e}`
555+
);
556+
err.details = e.stack;
557+
this.parentCompilation.errors.push(err);
558+
}
559+
};
560+
548561
this.compile((err, compilation) => {
549-
if (err) return callback(err);
562+
if (err) return finalCallback(err);
550563

551564
this.parentCompilation.children.push(compilation);
552565
for (const { name, source, info } of compilation.getAssets()) {
@@ -561,7 +574,7 @@ class Compiler {
561574
compilation.startTime = startTime;
562575
compilation.endTime = Date.now();
563576

564-
return callback(null, entries, compilation);
577+
return finalCallback(null, entries, compilation);
565578
});
566579
}
567580

0 commit comments

Comments
 (0)