files-change.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.aggregateFilesChanges = exports.clearFilesChange = exports.updateFilesChange = exports.consumeFilesChange = exports.getFilesChange = void 0;
  4. // we ignore package.json file because of https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/issues/674
  5. const IGNORED_FILES = ['package.json'];
  6. const isIgnoredFile = (file) => IGNORED_FILES.some((ignoredFile) => file.endsWith(`/${ignoredFile}`) || file.endsWith(`\\${ignoredFile}`));
  7. const compilerFilesChangeMap = new WeakMap();
  8. function getFilesChange(compiler) {
  9. const { changedFiles = [], deletedFiles = [] } = compilerFilesChangeMap.get(compiler) || {
  10. changedFiles: [],
  11. deletedFiles: [],
  12. };
  13. return {
  14. changedFiles: changedFiles.filter((changedFile) => !isIgnoredFile(changedFile)),
  15. deletedFiles: deletedFiles.filter((deletedFile) => !isIgnoredFile(deletedFile)),
  16. };
  17. }
  18. exports.getFilesChange = getFilesChange;
  19. function consumeFilesChange(compiler) {
  20. const change = getFilesChange(compiler);
  21. clearFilesChange(compiler);
  22. return change;
  23. }
  24. exports.consumeFilesChange = consumeFilesChange;
  25. function updateFilesChange(compiler, change) {
  26. compilerFilesChangeMap.set(compiler, aggregateFilesChanges([getFilesChange(compiler), change]));
  27. }
  28. exports.updateFilesChange = updateFilesChange;
  29. function clearFilesChange(compiler) {
  30. compilerFilesChangeMap.delete(compiler);
  31. }
  32. exports.clearFilesChange = clearFilesChange;
  33. /**
  34. * Computes aggregated files change based on the subsequent files changes.
  35. *
  36. * @param changes List of subsequent files changes
  37. * @returns Files change that represents all subsequent changes as a one event
  38. */
  39. function aggregateFilesChanges(changes) {
  40. const changedFilesSet = new Set();
  41. const deletedFilesSet = new Set();
  42. for (const { changedFiles = [], deletedFiles = [] } of changes) {
  43. for (const changedFile of changedFiles) {
  44. changedFilesSet.add(changedFile);
  45. deletedFilesSet.delete(changedFile);
  46. }
  47. for (const deletedFile of deletedFiles) {
  48. changedFilesSet.delete(deletedFile);
  49. deletedFilesSet.add(deletedFile);
  50. }
  51. }
  52. return {
  53. changedFiles: Array.from(changedFilesSet),
  54. deletedFiles: Array.from(deletedFilesSet),
  55. };
  56. }
  57. exports.aggregateFilesChanges = aggregateFilesChanges;