NodeEnvironmentPlugin.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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");
  7. const fs = require("graceful-fs");
  8. const createConsoleLogger = require("../logging/createConsoleLogger");
  9. const NodeWatchFileSystem = require("./NodeWatchFileSystem");
  10. const nodeConsole = require("./nodeConsole");
  11. /**
  12. * @import {
  13. * InfrastructureLogging
  14. * } from "../../declarations/WebpackOptions"
  15. */
  16. /** @import Compiler from "../Compiler" */
  17. /** @import { InputFileSystem } from "../util/fs" */
  18. /**
  19. * Defines the node environment plugin options type used by this module.
  20. * @typedef {object} NodeEnvironmentPluginOptions
  21. * @property {InfrastructureLogging} infrastructureLogging infrastructure logging options
  22. */
  23. const PLUGIN_NAME = "NodeEnvironmentPlugin";
  24. class NodeEnvironmentPlugin {
  25. /**
  26. * Creates an instance of NodeEnvironmentPlugin.
  27. * @param {NodeEnvironmentPluginOptions} options options
  28. */
  29. constructor(options) {
  30. /** @type {NodeEnvironmentPluginOptions} */
  31. this.options = options;
  32. }
  33. /**
  34. * Applies the plugin by registering its hooks on the compiler.
  35. * @param {Compiler} compiler the compiler instance
  36. * @returns {void}
  37. */
  38. apply(compiler) {
  39. const { infrastructureLogging } = this.options;
  40. compiler.infrastructureLogger = createConsoleLogger({
  41. level: infrastructureLogging.level || "info",
  42. debug: infrastructureLogging.debug || false,
  43. console:
  44. infrastructureLogging.console ||
  45. nodeConsole({
  46. colors: infrastructureLogging.colors,
  47. appendOnly: infrastructureLogging.appendOnly,
  48. stream:
  49. /** @type {NodeJS.WritableStream} */
  50. (infrastructureLogging.stream),
  51. compiler
  52. })
  53. });
  54. // @ts-expect-error need to fix on enhanced-resolve side
  55. compiler.inputFileSystem = new CachedInputFileSystem(fs, 60000);
  56. const inputFileSystem =
  57. /** @type {InputFileSystem} */
  58. (compiler.inputFileSystem);
  59. compiler.outputFileSystem = fs;
  60. compiler.intermediateFileSystem = fs;
  61. compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem);
  62. // Symmetric to the beforeRun purge below: the stat/readdir/readFile caches
  63. // would otherwise outlive `close()` for as long as the compiler is held.
  64. compiler.hooks.shutdown.tap(PLUGIN_NAME, () => {
  65. if (
  66. compiler.inputFileSystem === inputFileSystem &&
  67. inputFileSystem.purge
  68. ) {
  69. inputFileSystem.purge();
  70. }
  71. });
  72. compiler.hooks.beforeRun.tap(PLUGIN_NAME, (compiler) => {
  73. if (
  74. compiler.inputFileSystem === inputFileSystem &&
  75. inputFileSystem.purge
  76. ) {
  77. compiler.fsStartTime = Date.now();
  78. inputFileSystem.purge();
  79. }
  80. });
  81. }
  82. }
  83. module.exports = NodeEnvironmentPlugin;