fix: should emit assets even if invalidation occurs again (#19756)

This commit is contained in:
hai-x 2025-08-04 23:09:45 +08:00 committed by GitHub
parent 07a9d20a4c
commit 9a3c4f9f8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 2 deletions

View File

@ -187,8 +187,6 @@ class Watching {
const compilation = /** @type {Compilation} */ (_compilation); const compilation = /** @type {Compilation} */ (_compilation);
if (this.invalid) return this._done(null, compilation);
if (this.compiler.hooks.shouldEmit.call(compilation) === false) { if (this.compiler.hooks.shouldEmit.call(compilation) === false) {
return this._done(null, compilation); return this._done(null, compilation);
} }

View File

@ -58,4 +58,47 @@ describe("Watch", () => {
compiler.close(done); compiler.close(done);
}, 5000); }, 5000);
}); });
it("should correctly emit asset when invalidation occurs again", (done) => {
function handleError(err) {
if (err) done(err);
}
let calls = 0;
const compiler = webpack({
mode: "development",
context: path.resolve(__dirname, "fixtures/watch"),
plugins: [
(c) => {
// Ensure the second invalidation can occur during compiler running
let once = false;
c.hooks.afterCompile.tapAsync("LongTask", (_, cb) => {
if (once) cb();
once = true;
setTimeout(() => {
cb();
}, 1000);
});
},
(c) => {
c.hooks.done.tap("Test", () => {
// Should emit assets twice, instead of once
expect(calls).toBe(2);
done();
});
}
]
});
compiler.watch({}, handleError);
compiler.hooks.emit.tap("Test", () => {
calls++;
});
// First invalidation
compiler.watching.invalidate();
// Second invalidation while compiler is still running
setTimeout(() => {
compiler.watching.invalidate();
}, 50);
});
}); });