8000 fix: add writeToDisk support for clean: true · webpack/webpack-dev-middleware@ff90a3a · GitHub
[go: up one dir, main page]

Skip to content

Commit ff90a3a

Browse files
committed
fix: add writeToDisk support for clean: true
1 parent 6f7b8da commit ff90a3a

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ function wdm(compiler, options = {}) {
183183

184184
setupHooks(context);
185185

186+
setupOutputFileSystem(context);
187+
186188
if (options.writeToDisk) {
187189
setupWriteToDisk(context);
188190
}
189191

190-
setupOutputFileSystem(context);
191-
192192
// Start watching
193193
if (/** @type {Compiler} */ (context.compiler).watching) {
194194
context.watching = /** @type {Compiler} */ (context.compiler).watching;

src/utils/setupWriteToDisk.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ function setupWriteToDisk(context) {
2121
(context.compiler).compilers || [context.compiler];
2222

2323
for (const compiler of compilers) {
24+
if (compiler.outputFileSystem && compiler.options.output.clean) {
25+
/** @type {Compiler["outputFileSystem"]} */
26+
const { unlink: originalUnlink } = compiler.outputFileSystem;
27+
if (originalUnlink) {
28+
compiler.outputFileSystem.unlink = (
29+
/** @type {String} */ originalPath,
30+
/** @type {(arg0?: null | NodeJS.ErrnoException) => void} */ originalCallback
31+
) => {
32+
fs.unlink(originalPath, () => {});
33+
originalUnlink(originalPath, originalCallback);
34+
};
35+
}
36+
}
37+
2438
compiler.hooks.emit.tap(
2539
"DevMiddleware",
2640
/**

test/utils/setupWriteToDisk.test.js

Lines changed: 44 additions & 0 deletions
5B9C
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import setupWriteToDisk from "../../src/utils/setupWriteToDisk";
55

66
const mkdirSpy = jest.spyOn(fs, "mkdir");
77
const writeFileSpy = jest.spyOn(fs, "writeFile");
8+
const unlinkFileSpy = jest.spyOn(fs, "unlink");
89

910
describe("setupWriteToDisk", () => {
1011
let context;
@@ -41,6 +42,7 @@ describe("setupWriteToDisk", () => {
4142
getPath.mockClear();
4243
mkdirSpy.mockClear();
4344
writeFileSpy.mockClear();
45+
unlinkFileSpy.mockClear();
4446
});
4547

4648
const runAssetEmitted = (...args) => {
@@ -180,4 +182,46 @@ describe("setupWriteToDisk", () => {
180182
expect(cb.mock.calls).toMatchSnapshot();
181183
});
182184
});
185+
186+
const cleanOptions = [
187+
{
188+
title: "true",
189+
value: true,
190+
},
191+
{
192+
title: "false",
193+
value: false,
194+
},
195+
{
196+
title: "an object",
197+
value: { keep: () => true },
198+
},
199+
];
200+
201+
cleanOptions.forEach((cleanOption) => {
202+
it(`calls node fs unlink when using memfs and clean is ${cleanOption.title}`, (done) => {
203+
const unlink = jest.fn((_, callback) => callback());
204+
context.compiler.outputFileSystem = { unlink };
205+
context.compiler.options = {
206+
output: {
207+
clean: cleanOption.value,
208+
},
209+
};
210+
context.options = {
211+
writeToDisk: true,
212+
};
213+
setupWriteToDisk(context);
214+
215+
context.compiler.outputFileSystem.unlink("/target/path/file", () => {
216+
// the memfs unlink should always be called
217+
expect(unlink.mock.calls.length).toEqual(1);
218+
219+
// the fs unlink should be called when the clean value is truthy
220+
const expectedCallCount = cleanOption.value ? 1 : 0;
221+
expect(unlinkFileSpy.mock.calls.length).toEqual(expectedCallCount);
222+
223+
done();
224+
});
225+
});
226+
});
183227
});

0 commit comments

Comments
 (0)
0