Skip to content

Commit 83c1f40

Browse files
committed
fix: prevent WebpackLogger 'done hook' error when callback throws
- Replace try/catch with try/finally to always call callback() - Improve test to explicitly assert WebpackLogger error is not logged
1 parent 6c634ba commit 83c1f40

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/BundleAnalyzerPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BundleAnalyzerPlugin {
6969
// Making analyzer logs to be after all webpack logs in the console
7070
setImmediate(async () => {
7171
try {
72-
await Promise.all(actions.map(action => action()));
72+
await Promise.all(actions.map((action) => action()));
7373
} finally {
7474
callback();
7575
}

test/issue-499.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const BundleAnalyzerPlugin = require("../src/BundleAnalyzerPlugin");
5+
6+
describe("Issue #499", () => {
7+
it("should not cause WebpackLogger 'done hook' error when callback throws", (done) => {
8+
const compiler = webpack({
9+
mode: "development",
10+
entry: __filename,
11+
plugins: [new BundleAnalyzerPlugin({ analyzerMode: "disabled" })]
12+
});
13+
14+
let webpackLoggerError = false;
15+
// eslint-disable-next-line no-console
16+
const originalConsoleError = console.error;
17+
18+
// eslint-disable-next-line no-console
19+
console.error = function(...args) {
20+
const message = args.join(" ");
21+
if (message.includes("No such label 'done hook'")) {
22+
webpackLoggerError = true;
23+
}
24+
// eslint-disable-next-line no-console
25+
originalConsoleError.apply(console, args);
26+
};
27+
28+
compiler.run(() => {
29+
try {
30+
throw new Error("Intentional test error");
31+
} catch {
32+
// Swallow expected error
33+
}
34+
});
35+
36+
setTimeout(() => {
37+
// eslint-disable-next-line no-console
38+
console.error = originalConsoleError;
39+
40+
// ✅ Explicit assertion – satisfies jest/expect-expect
41+
expect(webpackLoggerError).toBe(false);
42+
done();
43+
}, 1000);
44+
});
45+
});

0 commit comments

Comments
 (0)