WatchIgnorePlugin.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const validateOptions = require("schema-utils");
  7. const schema = require("../schemas/plugins/WatchIgnorePlugin.json");
  8. /** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
  9. class IgnoringWatchFileSystem {
  10. constructor(wfs, paths) {
  11. this.wfs = wfs;
  12. this.paths = paths;
  13. }
  14. watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
  15. const ignored = path =>
  16. this.paths.some(p =>
  17. p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
  18. );
  19. const notIgnored = path => !ignored(path);
  20. const ignoredFiles = files.filter(ignored);
  21. const ignoredDirs = dirs.filter(ignored);
  22. const watcher = this.wfs.watch(
  23. files.filter(notIgnored),
  24. dirs.filter(notIgnored),
  25. missing,
  26. startTime,
  27. options,
  28. (
  29. err,
  30. filesModified,
  31. dirsModified,
  32. missingModified,
  33. fileTimestamps,
  34. dirTimestamps,
  35. removedFiles
  36. ) => {
  37. if (err) return callback(err);
  38. for (const path of ignoredFiles) {
  39. fileTimestamps.set(path, 1);
  40. }
  41. for (const path of ignoredDirs) {
  42. dirTimestamps.set(path, 1);
  43. }
  44. callback(
  45. err,
  46. filesModified,
  47. dirsModified,
  48. missingModified,
  49. fileTimestamps,
  50. dirTimestamps,
  51. removedFiles
  52. );
  53. },
  54. callbackUndelayed
  55. );
  56. return {
  57. close: () => watcher.close(),
  58. pause: () => watcher.pause(),
  59. getContextTimestamps: () => {
  60. const dirTimestamps = watcher.getContextTimestamps();
  61. for (const path of ignoredDirs) {
  62. dirTimestamps.set(path, 1);
  63. }
  64. return dirTimestamps;
  65. },
  66. getFileTimestamps: () => {
  67. const fileTimestamps = watcher.getFileTimestamps();
  68. for (const path of ignoredFiles) {
  69. fileTimestamps.set(path, 1);
  70. }
  71. return fileTimestamps;
  72. }
  73. };
  74. }
  75. }
  76. class WatchIgnorePlugin {
  77. /**
  78. * @param {WatchIgnorePluginOptions} paths list of paths
  79. */
  80. constructor(paths) {
  81. validateOptions(schema, paths, "Watch Ignore Plugin");
  82. this.paths = paths;
  83. }
  84. apply(compiler) {
  85. compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => {
  86. compiler.watchFileSystem = new IgnoringWatchFileSystem(
  87. compiler.watchFileSystem,
  88. this.paths
  89. );
  90. });
  91. }
  92. }
  93. module.exports = WatchIgnorePlugin;