NodeEnvironmentPlugin.js 2.3 KB

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