NodeEnvironmentPlugin.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CachedInputFileSystem = require("enhanced-resolve").CachedInputFileSystem;
  7. const fs = require("graceful-fs");
  8. const createConsoleLogger = require("../logging/createConsoleLogger");
  9. const NodeWatchFileSystem = require("./NodeWatchFileSystem");
  10. const nodeConsole = require("./nodeConsole");
  11. /** @typedef {import("../../declarations/WebpackOptions").InfrastructureLogging} InfrastructureLogging */
  12. /** @typedef {import("../Compiler")} Compiler */
  13. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  14. class NodeEnvironmentPlugin {
  15. /**
  16. * @param {object} options options
  17. * @param {InfrastructureLogging} options.infrastructureLogging infrastructure logging options
  18. */
  19. constructor(options) {
  20. this.options = options;
  21. }
  22. /**
  23. * Apply the plugin
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. const { infrastructureLogging } = this.options;
  29. compiler.infrastructureLogger = createConsoleLogger({
  30. level: infrastructureLogging.level || "info",
  31. debug: infrastructureLogging.debug || false,
  32. console:
  33. infrastructureLogging.console ||
  34. nodeConsole({
  35. colors: infrastructureLogging.colors,
  36. appendOnly: infrastructureLogging.appendOnly,
  37. stream:
  38. /** @type {NodeJS.WritableStream} */
  39. (infrastructureLogging.stream)
  40. })
  41. });
  42. compiler.inputFileSystem = new CachedInputFileSystem(fs, 60000);
  43. const inputFileSystem =
  44. /** @type {InputFileSystem} */
  45. (compiler.inputFileSystem);
  46. compiler.outputFileSystem = fs;
  47. compiler.intermediateFileSystem = fs;
  48. compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
  49. compiler.hooks.beforeRun.tap("NodeEnvironmentPlugin", compiler => {
  50. if (
  51. compiler.inputFileSystem === inputFileSystem &&
  52. inputFileSystem.purge
  53. ) {
  54. compiler.fsStartTime = Date.now();
  55. inputFileSystem.purge();
  56. }
  57. });
  58. }
  59. }
  60. module.exports = NodeEnvironmentPlugin;