Skip to content

Commit 852245e

Browse files
fix: don't use memory-fs when writeToDisk is true (#1537)
1 parent 688ba9b commit 852245e

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/utils/setupOutputFileSystem.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,31 @@ function setupOutputFileSystem(context) {
1616
const { outputFileSystem: outputFileSystemFromOptions } = context.options;
1717

1818
outputFileSystem = outputFileSystemFromOptions;
19-
} else {
19+
}
20+
// Don't use `memfs` when developer wants to write everything to a disk, because it doesn't make sense.
21+
else if (context.options.writeToDisk !== true) {
2022
outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
23+
} else {
24+
const isMultiCompiler =
25+
/** @type {MultiCompiler} */
26+
(context.compiler).compilers;
27+
28+
if (isMultiCompiler) {
29+
// Prefer compiler with `devServer` option or fallback on the first
30+
// TODO we need to support webpack-dev-server as a plugin or revisit it
31+
const compiler =
32+
/** @type {MultiCompiler} */
33+
(context.compiler).compilers.filter((item) =>
34+
Object.prototype.hasOwnProperty.call(item.options, "devServer")
35+
);
36+
37+
({ outputFileSystem } =
38+
compiler[0] ||
39+
/** @type {MultiCompiler} */
40+
(context.compiler).compilers[0]);
41+
} else {
42+
({ outputFileSystem } = context.compiler);
43+
}
2144
}
2245

2346
const compilers =

test/middleware.test.js

+78
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,84 @@ describe.each([
23732373
});
23742374
});
23752375

2376+
describe('should work with "true" value when the `output.clean` is `true`', () => {
2377+
const outputPath = path.resolve(
2378+
__dirname,
2379+
"./outputs/write-to-disk-true-with-clean"
2380+
);
2381+
2382+
let compiler;
2383+
2384+
beforeAll((done) => {
2385+
compiler = getCompiler({
2386+
...webpackConfig,
2387+
output: {
2388+
clean: true,
2389+
filename: "bundle.js",
2390+
path: outputPath,
2391+
},
2392+
});
2393+
2394+
instance = middleware(compiler, { writeToDisk: true });
2395+
2396+
fs.mkdirSync(outputPath, {
2397+
recursive: true,
2398+
});
2399+
fs.writeFileSync(path.resolve(outputPath, "test.json"), "{}");
2400+
2401+
app = framework();
2402+
app.use(instance);
2403+
2404+
listen = listenShorthand(done);
2405+
2406+
req = request(app);
2407+
});
2408+
2409+
afterAll((done) => {
2410+
del.sync(outputPath);
2411+
2412+
close(done);
2413+
});
2414+
2415+
it("should find the bundle file on disk", (done) => {
2416+
request(app)
2417+
.get("/bundle.js")
2418+
.expect(200, (error) => {
2419+
if (error) {
2420+
return done(error);
2421+
}
2422+
2423+
const bundlePath = path.resolve(outputPath, "bundle.js");
2424+
2425+
expect(fs.existsSync(path.resolve(outputPath, "test.json"))).toBe(
2426+
false
2427+
);
2428+
2429+
expect(
2430+
compiler.hooks.assetEmitted.taps.filter(
2431+
(hook) => hook.name === "DevMiddleware"
2432+
).length
2433+
).toBe(1);
2434+
expect(fs.existsSync(bundlePath)).toBe(true);
2435+
2436+
instance.invalidate();
2437+
2438+
return compiler.hooks.done.tap(
2439+
"DevMiddlewareWriteToDiskTest",
2440+
() => {
2441+
expect(
2442+
compiler.hooks.assetEmitted.taps.filter(
2443+
(hook) => hook.name === "DevMiddleware"
2444+
).length
2445+
).toBe(1);
2446+
2447+
done();
2448+
}
2449+
);
2450+
});
2451+
});
2452+
});
2453+
23762454
describe('should work with "false" value', () => {
23772455
let compiler;
23782456

0 commit comments

Comments
 (0)