Skip to content

Commit f89a7c1

Browse files
committed
test: output.clean keeps the last HMR update regardless of elapsed time
CleanPlugin keeps *.hot-update.* of the previous compilation for a fixed 10 seconds. A compilation that takes longer than that removes them on emit while a client that received the previous "done" may still be fetching them (requests queued by webpack-dev-middleware until the build finishes). The test builds three times, waits past the 10s window and builds a fourth time. The update emitted by the third build is expected to survive, since no newer update existed while the fourth build ran. Fails on main.
1 parent e95be18 commit f89a7c1

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

‎test/unitCases/HotModuleReplacementPluginCompilation.unittest.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,85 @@ describe("HotModuleReplacementPlugin", () => {
188188
compiler.run(callback);
189189
}, 20000);
190190

191+
it("output.clean=true should keep the last update until a newer one is emitted, regardless of elapsed time", (done) => {
192+
const outputPath = path.join(testDirectory, "js", "HotModuleReplacementPlugin");
193+
const entryFile = path.join(outputPath, "entry.js");
194+
const recordsFile = path.join(outputPath, "records.json");
195+
let step = 0;
196+
try {
197+
fs.mkdirSync(outputPath, { recursive: true });
198+
} catch (_err) {
199+
// empty
200+
}
201+
fs.writeFileSync(entryFile, `${++step}`, "utf8");
202+
/** @type {string[]} */
203+
const updates = [];
204+
const hasFile = (/** @type {string} */ file) => {
205+
try {
206+
fs.statSync(path.join(outputPath, file));
207+
return true;
208+
} catch (_err) {
209+
return false;
210+
}
211+
};
212+
const compiler = webpack({
213+
mode: "development",
214+
cache: false,
215+
entry: {
216+
0: entryFile
217+
},
218+
recordsPath: recordsFile,
219+
output: {
220+
path: outputPath,
221+
clean: true
222+
},
223+
plugins: [new webpack.HotModuleReplacementPlugin()]
224+
});
225+
const callback = (
226+
/** @type {Error | null} */ err,
227+
/** @type {import("../../").Stats | undefined} */ _stats
228+
) => {
229+
if (err) return done(err);
230+
const stats = /** @type {import("../../").Stats} */ (_stats);
231+
const hash = stats.toJson().hash;
232+
// the update emitted by the NEXT compilation is named after this hash
233+
updates.push(`0.${hash}.hot-update.json`, `0.${hash}.hot-update.js`);
234+
235+
if (step < 3) {
236+
fs.writeFileSync(entryFile, `${++step}`, "utf8");
237+
return compiler.run(callback);
238+
}
239+
240+
// step 3 emitted the update from hash(2) -> hash(3): updates[2], updates[3]
241+
for (const file of updates.slice(0, 4)) {
242+
expect(hasFile(file)).toBe(true);
243+
}
244+
245+
// A client that received the "done" of step 3 fetches that update while
246+
// step 4 compiles. When step 4 takes longer than 10s its emit removes
247+
// the update files although nothing newer existed while it compiled.
248+
return setTimeout(() => {
249+
fs.writeFileSync(entryFile, `${++step}`, "utf8");
250+
compiler.run((err) => {
251+
if (err) return done(err);
252+
try {
253+
// the update older than the last one may go
254+
expect(hasFile(updates[0])).toBe(false);
255+
expect(hasFile(updates[1])).toBe(false);
256+
// the last update must survive until a newer one exists
257+
expect(hasFile(updates[2])).toBe(true);
258+
expect(hasFile(updates[3])).toBe(true);
259+
} catch (err) {
260+
return done(err);
261+
}
262+
done();
263+
});
264+
}, 10100);
265+
};
266+
267+
compiler.run(callback);
268+
}, 20000);
269+
191270
it("should correct working when entry is Object and key is a number", (done) => {
192271
const outputPath = path.join(testDirectory, "js", "HotModuleReplacementPlugin");
193272
const entryFile = path.join(outputPath, "entry.js");

0 commit comments

Comments
 (0)