|
2 | 2 |
|
3 | 3 | require("../helpers/warmup-webpack"); |
4 | 4 |
|
| 5 | +const fs = require("fs"); |
5 | 6 | const path = require("path"); |
6 | 7 | const testDirectory = path.resolve(__dirname, ".."); |
7 | 8 | const { Volume, createFsFromVolume } = require("memfs"); |
@@ -114,4 +115,44 @@ describe("Watch", () => { |
114 | 115 | /** @type {import("../../").Watching} */ (compiler.watching).invalidate(); |
115 | 116 | }, 50); |
116 | 117 | }); |
| 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); |
117 | 158 | }); |
0 commit comments