NodeEnvironmentPlugin.js 2.1 KB

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