Skip to content

Commit 6e0069f

Browse files
fix(watch): purge the input cache when watching closes
A change made while no watcher runs is never reported, so a watch started again within 60 s built from cached content.
1 parent 04078f4 commit 6e0069f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎lib/node/NodeEnvironmentPlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ class NodeEnvironmentPlugin {
7676
inputFileSystem.purge();
7777
}
7878
});
79+
// Nothing reports a change made while no watcher runs, so a later `watch`
80+
// would build from cached content.
81+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => {
82+
if (
83+
compiler.inputFileSystem === inputFileSystem &&
84+
inputFileSystem.purge
85+
) {
86+
inputFileSystem.purge();
87+
}
88+
});
7989
compiler.hooks.beforeRun.tap(PLUGIN_NAME, (compiler) => {
8090
if (
8191
compiler.inputFileSystem === inputFileSystem &&

‎test/unitCases/WatchClose.unittest.js‎

Lines changed: 51 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 expectNoDeprecations = require("../helpers/expectNoDeprecations");
@@ -75,4 +76,54 @@ describe("WatchClose", () => {
7576
expect(num).toBe(1111);
7677
});
7778
});
79+
80+
describe("watching again after close", () => {
81+
const fixturePath = path.join(
82+
testDirectory,
83+
"fixtures",
84+
`temp-watch-close-${Date.now()}`
85+
);
86+
const filePath = path.join(fixturePath, "file.js");
87+
88+
beforeAll(() => {
89+
fs.mkdirSync(fixturePath, { recursive: true });
90+
fs.writeFileSync(filePath, "module.exports = 'before';", "utf8");
91+
});
92+
93+
afterAll(() => {
94+
fs.unlinkSync(filePath);
95+
fs.rmdirSync(fixturePath);
96+
});
97+
98+
it("should read a file edited while closed", async () => {
99+
const webpack = require("../../");
100+
101+
const outputPath = path.join(testDirectory, "js/WatchCloseAgain");
102+
const compiler = webpack({
103+
mode: "development",
104+
entry: filePath,
105+
output: { path: outputPath, filename: "bundle.js" }
106+
});
107+
/**
108+
* @returns {Promise<string>} the emitted bundle of the first build
109+
*/
110+
const build = () =>
111+
new Promise((resolve, reject) => {
112+
const watching = /** @type {import("../../").Watching} */ (
113+
compiler.watch({}, (err) => {
114+
if (err) return reject(err);
115+
watching.close(() =>
116+
resolve(
117+
fs.readFileSync(path.join(outputPath, "bundle.js"), "utf8")
118+
)
119+
);
120+
})
121+
);
122+
});
123+
124+
expect(await build()).toContain("before");
125+
fs.writeFileSync(filePath, "module.exports = 'after';", "utf8");
126+
expect(await build()).toContain("after");
127+
});
128+
});
78129
});

0 commit comments

Comments
 (0)