You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.8 KiB

4 years ago
  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. };