NodeEnvironmentPlugin.js 2.2 KB

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