inclusive-node-watch-file-system.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.InclusiveNodeWatchFileSystem = void 0;
  7. const path_1 = require("path");
  8. const chokidar_1 = __importDefault(require("chokidar"));
  9. const minimatch_1 = __importDefault(require("minimatch"));
  10. const files_change_1 = require("../files-change");
  11. const infrastructure_logger_1 = require("../infrastructure-logger");
  12. const is_inside_another_path_1 = require("../utils/path/is-inside-another-path");
  13. const BUILTIN_IGNORED_DIRS = ['.git'];
  14. function createIsIgnored(ignored, excluded) {
  15. const ignoredPatterns = ignored ? (Array.isArray(ignored) ? ignored : [ignored]) : [];
  16. const ignoredFunctions = ignoredPatterns.map((pattern) => {
  17. // ensure patterns are valid - see https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/594
  18. if (typeof pattern === 'string') {
  19. return (path) => (0, minimatch_1.default)(path, pattern);
  20. }
  21. else if (pattern instanceof RegExp) {
  22. return (path) => pattern.test(path);
  23. }
  24. else {
  25. // fallback to no-ignore function
  26. return () => false;
  27. }
  28. });
  29. ignoredFunctions.push((path) => excluded.some((excludedPath) => (0, is_inside_another_path_1.isInsideAnotherPath)(excludedPath, path)));
  30. ignoredFunctions.push((path) => BUILTIN_IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`) || path.includes(`\\${ignoredDir}\\`)));
  31. return function isIgnored(path) {
  32. return ignoredFunctions.some((ignoredFunction) => ignoredFunction(path));
  33. };
  34. }
  35. class InclusiveNodeWatchFileSystem {
  36. constructor(watchFileSystem, compiler, pluginState) {
  37. this.watchFileSystem = watchFileSystem;
  38. this.compiler = compiler;
  39. this.pluginState = pluginState;
  40. this.paused = true;
  41. this.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
  42. var _a, _b, _c, _d;
  43. const { debug } = (0, infrastructure_logger_1.getInfrastructureLogger)(this.compiler);
  44. (0, files_change_1.clearFilesChange)(this.compiler);
  45. const isIgnored = createIsIgnored(options === null || options === void 0 ? void 0 : options.ignored, ((_a = this.pluginState.lastDependencies) === null || _a === void 0 ? void 0 : _a.excluded) || []);
  46. // use standard watch file system for files and missing
  47. const standardWatcher = this.watchFileSystem.watch(files, dirs, missing, startTime, options, callback, callbackUndelayed);
  48. (_b = this.watcher) === null || _b === void 0 ? void 0 : _b.on('change', (file) => {
  49. if (typeof file !== 'string') {
  50. return;
  51. }
  52. if (!isIgnored(file)) {
  53. debug('Detected file change', file);
  54. this.deletedFiles.delete(file);
  55. (0, files_change_1.updateFilesChange)(this.compiler, { changedFiles: [file] });
  56. }
  57. else {
  58. debug("Detected file change but it's ignored", file);
  59. }
  60. });
  61. (_c = this.watcher) === null || _c === void 0 ? void 0 : _c.on('remove', (file) => {
  62. if (typeof file !== 'string') {
  63. return;
  64. }
  65. if (this.deletedFiles.has(file)) {
  66. debug('Skipping duplicated remove event.');
  67. return;
  68. }
  69. if (!isIgnored(file)) {
  70. debug('Detected file remove', file);
  71. this.deletedFiles.add(file);
  72. (0, files_change_1.updateFilesChange)(this.compiler, { deletedFiles: [file] });
  73. }
  74. else {
  75. debug("Detected file remove but it's ignored", file);
  76. }
  77. });
  78. // calculate what to change
  79. const prevDirs = Array.from(this.dirsWatchers.keys());
  80. const nextDirs = Array.from(((_d = this.pluginState.lastDependencies) === null || _d === void 0 ? void 0 : _d.dirs) || []);
  81. const dirsToUnwatch = prevDirs.filter((prevDir) => !nextDirs.includes(prevDir));
  82. const dirsToWatch = nextDirs.filter((nextDir) => !prevDirs.includes(nextDir) && !isIgnored(nextDir));
  83. // update dirs watcher
  84. dirsToUnwatch.forEach((dirToUnwatch) => {
  85. var _a;
  86. (_a = this.dirsWatchers.get(dirToUnwatch)) === null || _a === void 0 ? void 0 : _a.close();
  87. this.dirsWatchers.delete(dirToUnwatch);
  88. });
  89. dirsToWatch.forEach((dirToWatch) => {
  90. const interval = typeof (options === null || options === void 0 ? void 0 : options.poll) === 'number' ? options.poll : undefined;
  91. const dirWatcher = chokidar_1.default.watch(dirToWatch, {
  92. ignoreInitial: true,
  93. ignorePermissionErrors: true,
  94. ignored: (path) => isIgnored(path),
  95. usePolling: (options === null || options === void 0 ? void 0 : options.poll) ? true : undefined,
  96. interval: interval,
  97. binaryInterval: interval,
  98. alwaysStat: true,
  99. atomic: true,
  100. awaitWriteFinish: true,
  101. });
  102. dirWatcher.on('add', (file, stats) => {
  103. var _a, _b;
  104. if (this.paused) {
  105. return;
  106. }
  107. const extension = (0, path_1.extname)(file);
  108. const supportedExtensions = ((_a = this.pluginState.lastDependencies) === null || _a === void 0 ? void 0 : _a.extensions) || [];
  109. if (!supportedExtensions.includes(extension)) {
  110. debug('Detected new file add but extension is not supported', file);
  111. return;
  112. }
  113. debug('Detected new file add', file);
  114. this.deletedFiles.delete(file);
  115. (0, files_change_1.updateFilesChange)(this.compiler, { changedFiles: [file] });
  116. (_b = this.watcher) === null || _b === void 0 ? void 0 : _b._onChange(dirToWatch, (stats === null || stats === void 0 ? void 0 : stats.mtimeMs) || (stats === null || stats === void 0 ? void 0 : stats.ctimeMs) || 1, file, 'rename');
  117. });
  118. dirWatcher.on('unlink', (file) => {
  119. var _a, _b;
  120. if (this.paused) {
  121. return;
  122. }
  123. const extension = (0, path_1.extname)(file);
  124. const supportedExtensions = ((_a = this.pluginState.lastDependencies) === null || _a === void 0 ? void 0 : _a.extensions) || [];
  125. if (!supportedExtensions.includes(extension)) {
  126. debug('Detected new file remove but extension is not supported', file);
  127. return;
  128. }
  129. if (this.deletedFiles.has(file)) {
  130. debug('Skipping duplicated unlink event.');
  131. return;
  132. }
  133. debug('Detected new file remove', file);
  134. this.deletedFiles.add(file);
  135. (0, files_change_1.updateFilesChange)(this.compiler, { deletedFiles: [file] });
  136. (_b = this.watcher) === null || _b === void 0 ? void 0 : _b._onRemove(dirToWatch, file, 'rename');
  137. });
  138. this.dirsWatchers.set(dirToWatch, dirWatcher);
  139. });
  140. this.paused = false;
  141. return Object.assign(Object.assign({}, standardWatcher), { close: () => {
  142. (0, files_change_1.clearFilesChange)(this.compiler);
  143. if (standardWatcher) {
  144. standardWatcher.close();
  145. }
  146. this.dirsWatchers.forEach((dirWatcher) => {
  147. dirWatcher === null || dirWatcher === void 0 ? void 0 : dirWatcher.close();
  148. });
  149. this.dirsWatchers.clear();
  150. this.paused = true;
  151. }, pause: () => {
  152. if (standardWatcher) {
  153. standardWatcher.pause();
  154. }
  155. this.paused = true;
  156. } });
  157. };
  158. this.dirsWatchers = new Map();
  159. this.deletedFiles = new Set();
  160. }
  161. get watcher() {
  162. var _a;
  163. return this.watchFileSystem.watcher || ((_a = this.watchFileSystem.wfs) === null || _a === void 0 ? void 0 : _a.watcher);
  164. }
  165. }
  166. exports.InclusiveNodeWatchFileSystem = InclusiveNodeWatchFileSystem;