setupOutputFileSystem.js
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
const path = require("path");
const memfs = require("memfs");
/** @typedef {import("webpack").MultiCompiler} MultiCompiler */
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").Context<Request, Response>} context
*/
function setupOutputFileSystem(context) {
let outputFileSystem;
if (context.options.outputFileSystem) {
const {
outputFileSystem: outputFileSystemFromOptions
} = context.options; // Todo remove when we drop webpack@4 support
if (typeof outputFileSystemFromOptions.join !== "function") {
throw new Error("Invalid options: options.outputFileSystem.join() method is expected");
} // Todo remove when we drop webpack@4 support
// @ts-ignore
if (typeof outputFileSystemFromOptions.mkdirp !== "function") {
throw new Error("Invalid options: options.outputFileSystem.mkdirp() method is expected");
}
outputFileSystem = outputFileSystemFromOptions;
} else {
outputFileSystem = memfs.createFsFromVolume(new memfs.Volume()); // TODO: remove when we drop webpack@4 support
// @ts-ignore
outputFileSystem.join = path.join.bind(path);
}
const compilers =
/** @type {MultiCompiler} */
context.compiler.compilers || [context.compiler];
for (const compiler of compilers) {
compiler.outputFileSystem = outputFileSystem;
} // @ts-ignore
// eslint-disable-next-line no-param-reassign
context.outputFileSystem = outputFileSystem;
}
module.exports = setupOutputFileSystem;