fs.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const MemoryFileSystem = require('memory-fs');
  5. const mkdirp = require('mkdirp');
  6. const { colors } = require('webpack-log');
  7. const DevMiddlewareError = require('./DevMiddlewareError');
  8. module.exports = {
  9. toDisk(context) {
  10. const compilers = context.compiler.compilers || [context.compiler];
  11. for (const compiler of compilers) {
  12. compiler.hooks.emit.tap('WebpackDevMiddleware', (compilation) => {
  13. if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
  14. return;
  15. }
  16. compiler.hooks.assetEmitted.tapAsync(
  17. 'WebpackDevMiddleware',
  18. (file, info, callback) => {
  19. let targetPath = null;
  20. let content = null;
  21. // webpack@5
  22. if (info.compilation) {
  23. ({ targetPath, content } = info);
  24. } else {
  25. let targetFile = file;
  26. const queryStringIdx = targetFile.indexOf('?');
  27. if (queryStringIdx >= 0) {
  28. targetFile = targetFile.substr(0, queryStringIdx);
  29. }
  30. let { outputPath } = compiler;
  31. // TODO Why? Need remove in future major release
  32. if (outputPath === '/') {
  33. outputPath = compiler.context;
  34. }
  35. outputPath = compilation.getPath(outputPath, {});
  36. content = info;
  37. targetPath = path.join(outputPath, targetFile);
  38. }
  39. const { writeToDisk: filter } = context.options;
  40. const allowWrite =
  41. filter && typeof filter === 'function'
  42. ? filter(targetPath)
  43. : true;
  44. if (!allowWrite) {
  45. return callback();
  46. }
  47. const { log } = context;
  48. const dir = path.dirname(targetPath);
  49. return mkdirp(dir, (mkdirpError) => {
  50. if (mkdirpError) {
  51. return callback(mkdirpError);
  52. }
  53. return fs.writeFile(targetPath, content, (writeFileError) => {
  54. if (writeFileError) {
  55. return callback(writeFileError);
  56. }
  57. log.debug(
  58. colors.cyan(
  59. `Asset written to disk: ${path.relative(
  60. process.cwd(),
  61. targetPath
  62. )}`
  63. )
  64. );
  65. return callback();
  66. });
  67. });
  68. }
  69. );
  70. compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true;
  71. });
  72. }
  73. },
  74. setFs(context, compiler) {
  75. if (
  76. typeof compiler.outputPath === 'string' &&
  77. !path.posix.isAbsolute(compiler.outputPath) &&
  78. !path.win32.isAbsolute(compiler.outputPath)
  79. ) {
  80. throw new DevMiddlewareError(
  81. '`output.path` needs to be an absolute path or `/`.'
  82. );
  83. }
  84. let fileSystem;
  85. // store our files in memory
  86. const isConfiguredFs = context.options.fs;
  87. const isMemoryFs =
  88. !isConfiguredFs &&
  89. !compiler.compilers &&
  90. compiler.outputFileSystem instanceof MemoryFileSystem;
  91. if (isConfiguredFs) {
  92. // eslint-disable-next-line no-shadow
  93. const { fs } = context.options;
  94. if (typeof fs.join !== 'function') {
  95. // very shallow check
  96. throw new Error(
  97. 'Invalid options: options.fs.join() method is expected'
  98. );
  99. }
  100. // eslint-disable-next-line no-param-reassign
  101. compiler.outputFileSystem = fs;
  102. fileSystem = fs;
  103. } else if (isMemoryFs) {
  104. fileSystem = compiler.outputFileSystem;
  105. } else {
  106. fileSystem = new MemoryFileSystem();
  107. // eslint-disable-next-line no-param-reassign
  108. compiler.outputFileSystem = fileSystem;
  109. }
  110. // eslint-disable-next-line no-param-reassign
  111. context.fs = fileSystem;
  112. },
  113. };