NodeEnvironmentPlugin.js 2.1 KB

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