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: don't use memory-fs when writeToDisk is true #1537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/utils/setupOutputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,31 @@ function setupOutputFileSystem(context) {
const { outputFileSystem: outputFileSystemFromOptions } = context.options;

outputFileSystem = outputFileSystemFromOptions;
} else {
}
// Don't use `memfs` when developer wants to write everything to a disk, because it doesn't make sense.
else if (context.options.writeToDisk !== true) {
outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
} else {
const isMultiCompiler =
/** @type {MultiCompiler} */
(context.compiler).compilers;

if (isMultiCompiler) {
// Prefer compiler with `devServer` option or fallback on the first
// TODO we need to support webpack-dev-server as a plugin or revisit it
const compiler =
/** @type {MultiCompiler} */
(context.compiler).compilers.filter((item) =>
Object.prototype.hasOwnProperty.call(item.options, "devServer")
);

({ outputFileSystem } =
compiler[0] ||
/** @type {MultiCompiler} */
(context.compiler).compilers[0]);
} else {
({ outputFileSystem } = context.compiler);
}
}

const compilers =
Expand Down
78 changes: 78 additions & 0 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,84 @@ describe.each([
});
});

describe('should work with "true" value when the `output.clean` is `true`', () => {
const outputPath = path.resolve(
__dirname,
"./outputs/write-to-disk-true-with-clean"
);

let compiler;

beforeAll((done) => {
compiler = getCompiler({
...webpackConfig,
output: {
clean: true,
filename: "bundle.js",
path: outputPath,
},
});

instance = middleware(compiler, { writeToDisk: true });

fs.mkdirSync(outputPath, {
recursive: true,
});
fs.writeFileSync(path.resolve(outputPath, "test.json"), "{}");

app = framework();
app.use(instance);

listen = listenShorthand(done);

req = request(app);
});

afterAll((done) => {
del.sync(outputPath);

close(done);
});

it("should find the bundle file on disk", (done) => {
request(app)
.get("/bundle.js")
.expect(200, (error) => {
if (error) {
return done(error);
}

const bundlePath = path.resolve(outputPath, "bundle.js");

expect(fs.existsSync(path.resolve(outputPath, "test.json"))).toBe(
false
);

expect(
compiler.hooks.assetEmitted.taps.filter(
(hook) => hook.name === "DevMiddleware"
).length
).toBe(1);
expect(fs.existsSync(bundlePath)).toBe(true);

instance.invalidate();

return compiler.hooks.done.tap(
"DevMiddlewareWriteToDiskTest",
() => {
expect(
compiler.hooks.assetEmitted.taps.filter(
(hook) => hook.name === "DevMiddleware"
).length
).toBe(1);

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

describe('should work with "false" value', () => {
let compiler;

Expand Down