NodeEnvironmentPlugin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/lib/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. class NodeEnvironmentPlugin {
  14. /**
  15. * @param {Object} options options
  16. * @param {InfrastructureLogging} options.infrastructureLogging infrastructure logging options
  17. */
  18. constructor(options) {
  19. this.options = options;
  20. }
  21. /**
  22. * Apply the plugin
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. const { infrastructureLogging } = this.options;
  28. compiler.infrastructureLogger = createConsoleLogger({
  29. level: infrastructureLogging.level || "info",
  30. debug: infrastructureLogging.debug || false,
  31. console:
  32. infrastructureLogging.console ||
  33. nodeConsole({
  34. colors: infrastructureLogging.colors,
  35. appendOnly: infrastructureLogging.appendOnly,
  36. stream: infrastructureLogging.stream
  37. })
  38. });
  39. compiler.inputFileSystem = new CachedInputFileSystem(fs, 60000);
  40. const inputFileSystem = compiler.inputFileSystem;
  41. compiler.outputFileSystem = fs;
  42. compiler.intermediateFileSystem = fs;
  43. compiler.watchFileSystem = new NodeWatchFileSystem(
  44. compiler.inputFileSystem
  45. );
  46. compiler.hooks.beforeRun.tap("NodeEnvironmentPlugin", compiler => {
  47. if (compiler.inputFileSystem === inputFileSystem) {
  48. compiler.fsStartTime = Date.now();
  49. inputFileSystem.purge();
  50. }
  51. });
  52. }
  53. }
  54. module.exports = NodeEnvironmentPlugin;