setupOutputFileSystem.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. const memfs = require("memfs");
  3. /** @typedef {import("webpack").MultiCompiler} MultiCompiler */
  4. /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
  5. /** @typedef {import("../index.js").ServerResponse} ServerResponse */
  6. /**
  7. * @template {IncomingMessage} Request
  8. * @template {ServerResponse} Response
  9. * @param {import("../index.js").Context<Request, Response>} context
  10. */
  11. function setupOutputFileSystem(context) {
  12. let outputFileSystem;
  13. if (context.options.outputFileSystem) {
  14. const {
  15. outputFileSystem: outputFileSystemFromOptions
  16. } = context.options;
  17. outputFileSystem = outputFileSystemFromOptions;
  18. }
  19. // Don't use `memfs` when developer wants to write everything to a disk, because it doesn't make sense.
  20. else if (context.options.writeToDisk !== true) {
  21. outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
  22. } else {
  23. const isMultiCompiler = /** @type {MultiCompiler} */
  24. context.compiler.compilers;
  25. if (isMultiCompiler) {
  26. // Prefer compiler with `devServer` option or fallback on the first
  27. // TODO we need to support webpack-dev-server as a plugin or revisit it
  28. const compiler = /** @type {MultiCompiler} */
  29. context.compiler.compilers.filter(item => Object.prototype.hasOwnProperty.call(item.options, "devServer"));
  30. ({
  31. outputFileSystem
  32. } = compiler[0] || /** @type {MultiCompiler} */
  33. context.compiler.compilers[0]);
  34. } else {
  35. ({
  36. outputFileSystem
  37. } = context.compiler);
  38. }
  39. }
  40. const compilers = /** @type {MultiCompiler} */
  41. context.compiler.compilers || [context.compiler];
  42. for (const compiler of compilers) {
  43. compiler.outputFileSystem = outputFileSystem;
  44. }
  45. // @ts-ignore
  46. // eslint-disable-next-line no-param-reassign
  47. context.outputFileSystem = outputFileSystem;
  48. }
  49. module.exports = setupOutputFileSystem;