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