WatchIgnorePlugin.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class IgnoringWatchFileSystem {
  9. constructor(wfs, paths) {
  10. this.wfs = wfs;
  11. this.paths = paths;
  12. }
  13. watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
  14. const ignored = path =>
  15. this.paths.some(
  16. p => (p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0)
  17. );
  18. const notIgnored = path => !ignored(path);
  19. const ignoredFiles = files.filter(ignored);
  20. const ignoredDirs = dirs.filter(ignored);
  21. const watcher = this.wfs.watch(
  22. files.filter(notIgnored),
  23. dirs.filter(notIgnored),
  24. missing,
  25. startTime,
  26. options,
  27. (
  28. err,
  29. filesModified,
  30. dirsModified,
  31. missingModified,
  32. fileTimestamps,
  33. dirTimestamps
  34. ) => {
  35. if (err) return callback(err);
  36. for (const path of ignoredFiles) {
  37. fileTimestamps.set(path, 1);
  38. }
  39. for (const path of ignoredDirs) {
  40. dirTimestamps.set(path, 1);
  41. }
  42. callback(
  43. err,
  44. filesModified,
  45. dirsModified,
  46. missingModified,
  47. fileTimestamps,
  48. dirTimestamps
  49. );
  50. },
  51. callbackUndelayed
  52. );
  53. return {
  54. close: () => watcher.close(),
  55. pause: () => watcher.pause(),
  56. getContextTimestamps: () => {
  57. const dirTimestamps = watcher.getContextTimestamps();
  58. for (const path of ignoredDirs) {
  59. dirTimestamps.set(path, 1);
  60. }
  61. return dirTimestamps;
  62. },
  63. getFileTimestamps: () => {
  64. const fileTimestamps = watcher.getFileTimestamps();
  65. for (const path of ignoredFiles) {
  66. fileTimestamps.set(path, 1);
  67. }
  68. return fileTimestamps;
  69. }
  70. };
  71. }
  72. }
  73. class WatchIgnorePlugin {
  74. constructor(paths) {
  75. validateOptions(schema, paths, "Watch Ignore Plugin");
  76. this.paths = paths;
  77. }
  78. apply(compiler) {
  79. compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => {
  80. compiler.watchFileSystem = new IgnoringWatchFileSystem(
  81. compiler.watchFileSystem,
  82. this.paths
  83. );
  84. });
  85. }
  86. }
  87. module.exports = WatchIgnorePlugin;