Skip to content

Commit 0c870a0

Browse files
alexander-akaitclaude
authored andcommitted
fix(watch): rebuild when a file is removed while a build is running
Regression test for the watchpack fix (webpack/watchpack#345); it passes once watchpack is bumped to the release carrying it.
1 parent be52143 commit 0c870a0

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Rebuild in watch mode when a file is deleted while a build is running.

‎test/unitCases/Watch.unittest.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require("../helpers/warmup-webpack");
44

5+
const fs = require("fs");
56
const path = require("path");
67
const testDirectory = path.resolve(__dirname, "..");
78
const { Volume, createFsFromVolume } = require("memfs");
@@ -114,4 +115,44 @@ describe("Watch", () => {
114115
/** @type {import("../../").Watching} */ (compiler.watching).invalidate();
115116
}, 50);
116117
});
118+
119+
it("should rebuild when a file is removed while a build is running", (done) => {
120+
const context = path.join(testDirectory, "js", "Watch", "removed-while-building");
121+
const removedFile = path.join(context, "b.js");
122+
fs.rmSync(context, { recursive: true, force: true });
123+
fs.mkdirSync(context, { recursive: true });
124+
fs.writeFileSync(path.join(context, "index.js"), 'require("./a");require("./b");');
125+
fs.writeFileSync(path.join(context, "a.js"), "module.exports = 1;");
126+
fs.writeFileSync(removedFile, "module.exports = 2;");
127+
let changed = false;
128+
const compiler = webpack({
129+
mode: "development",
130+
context,
131+
entry: "./index.js",
132+
plugins: [
133+
(c) => {
134+
// the paused watcher sees the removal before the next build replaces it
135+
c.hooks.afterCompile.tapAsync("RemoveWhileBuilding", (_, cb) => {
136+
if (!changed || !fs.existsSync(removedFile)) return cb();
137+
fs.unlinkSync(removedFile);
138+
setTimeout(cb, 500);
139+
});
140+
}
141+
]
142+
});
143+
compiler.outputFileSystem = /** @type {import("../../").OutputFileSystem} */ (
144+
/** @type {unknown} */ (createFsFromVolume(new Volume()))
145+
);
146+
compiler.watch({ aggregateTimeout: 50 }, (err) => {
147+
if (err) return done(err);
148+
if (!changed) {
149+
changed = true;
150+
setTimeout(() => {
151+
fs.writeFileSync(path.join(context, "a.js"), "module.exports = 3;");
152+
}, 300);
153+
} else if (compiler.removedFiles && compiler.removedFiles.has(removedFile)) {
154+
compiler.close(done);
155+
}
156+
});
157+
}, 10000);
117158
});

0 commit comments

Comments
 (0)