Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add writeToDisk support for clean: true #1472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ function wdm(compiler, options = {}) {

setupHooks(context);

setupOutputFileSystem(context);

if (options.writeToDisk) {
setupWriteToDisk(context);
}

setupOutputFileSystem(context);

// Start watching
if (/** @type {Compiler} */ (context.compiler).watching) {
context.watching = /** @type {Compiler} */ (context.compiler).watching;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/setupWriteToDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ function setupWriteToDisk(context) {
(context.compiler).compilers || [context.compiler];

for (const compiler of compilers) {
if (compiler.outputFileSystem && compiler.options.output.clean) {
/** @type {Compiler["outputFileSystem"]} */
const { unlink: originalUnlink } = compiler.outputFileSystem;
if (originalUnlink) {
compiler.outputFileSystem.unlink = (
/** @type {String} */ originalPath,
/** @type {(arg0?: null | NodeJS.ErrnoException) => void} */ originalCallback
) => {
fs.unlink(originalPath, () => {});
Copy link
Member

@alexander-akait alexander-akait Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, there is racing you should wait the first operation finished, also using fs.unlink is not safe, because some developer use custom outputFileSystem to avoid file removing, but we remove them and break application, we nee to think deeply how we can handle such case. In theiry we can apply clean plugin before watch and it will with original fs

originalUnlink(originalPath, originalCallback);
};
}
}

compiler.hooks.emit.tap(
"DevMiddleware",
/**
Expand Down
44 changes: 44 additions & 0 deletions test/utils/setupWriteToDisk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import setupWriteToDisk from "../../src/utils/setupWriteToDisk";

const mkdirSpy = jest.spyOn(fs, "mkdir");
const writeFileSpy = jest.spyOn(fs, "writeFile");
const unlinkFileSpy = jest.spyOn(fs, "unlink");

describe("setupWriteToDisk", () => {
let context;
Expand Down Expand Up @@ -41,6 +42,7 @@ describe("setupWriteToDisk", () => {
getPath.mockClear();
mkdirSpy.mockClear();
writeFileSpy.mockClear();
unlinkFileSpy.mockClear();
});

const runAssetEmitted = (...args) => {
Expand Down Expand Up @@ -180,4 +182,46 @@ describe("setupWriteToDisk", () => {
expect(cb.mock.calls).toMatchSnapshot();
});
});

const cleanOptions = [
{
title: "true",
value: true,
},
{
title: "false",
value: false,
},
{
title: "an object",
value: { keep: () => true },
},
];

cleanOptions.forEach((cleanOption) => {
it(`calls node fs unlink when using memfs and clean is ${cleanOption.title}`, (done) => {
const unlink = jest.fn((_, callback) => callback());
context.compiler.outputFileSystem = { unlink };
context.compiler.options = {
output: {
clean: cleanOption.value,
},
};
context.options = {
writeToDisk: true,
};
setupWriteToDisk(context);

context.compiler.outputFileSystem.unlink("/target/path/file", () => {
// the memfs unlink should always be called
expect(unlink.mock.calls.length).toEqual(1);

// the fs unlink should be called when the clean value is truthy
const expectedCallCount = cleanOption.value ? 1 : 0;
expect(unlinkFileSpy.mock.calls.length).toEqual(expectedCallCount);

done();
});
});
});
});