store.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {computed, observable} from 'mobx';
  2. import {isChunkParsed, walkModules} from './utils';
  3. export class Store {
  4. cid = 0;
  5. sizes = new Set(['statSize', 'parsedSize', 'gzipSize']);
  6. @observable.ref allChunks;
  7. @observable.shallow selectedChunks;
  8. @observable defaultSize;
  9. @observable showConcatenatedModulesContent =
  10. localStorage.getItem('showConcatenatedModulesContent') === true;
  11. @observable selectedSize;
  12. setModules(modules) {
  13. walkModules(modules, module => {
  14. module.cid = this.cid++;
  15. });
  16. this.allChunks = modules;
  17. this.selectedChunks = this.allChunks;
  18. }
  19. setEntrypoints(entrypoints) {
  20. this.entrypoints = entrypoints;
  21. }
  22. @computed get hasParsedSizes() {
  23. return this.allChunks.some(isChunkParsed);
  24. }
  25. @computed get activeSize() {
  26. const activeSize = this.selectedSize || this.defaultSize;
  27. if (!this.hasParsedSizes || !this.sizes.has(activeSize)) {
  28. return 'statSize';
  29. }
  30. return activeSize;
  31. }
  32. @computed get visibleChunks() {
  33. const visibleChunks = this.allChunks.filter(chunk =>
  34. this.selectedChunks.includes(chunk)
  35. );
  36. return this.filterModulesForSize(visibleChunks, this.activeSize);
  37. }
  38. @computed get allChunksSelected() {
  39. return this.visibleChunks.length === this.allChunks.length;
  40. }
  41. @computed get totalChunksSize() {
  42. return this.allChunks.reduce(
  43. (totalSize, chunk) => totalSize + (chunk[this.activeSize] || 0),
  44. 0
  45. );
  46. }
  47. @computed get isSearching() {
  48. return !!this.searchQueryRegexp;
  49. }
  50. @computed get foundModulesByChunk() {
  51. if (!this.isSearching) {
  52. return [];
  53. }
  54. const query = this.searchQueryRegexp;
  55. return this.visibleChunks
  56. .map(chunk => {
  57. let foundGroups = [];
  58. walkModules(chunk.groups, module => {
  59. let weight = 0;
  60. /**
  61. * Splitting found modules/directories into groups:
  62. *
  63. * 1) Module with matched label (weight = 4)
  64. * 2) Directory with matched label (weight = 3)
  65. * 3) Module with matched path (weight = 2)
  66. * 4) Directory with matched path (weight = 1)
  67. */
  68. if (query.test(module.label)) {
  69. weight += 3;
  70. } else if (module.path && query.test(module.path)) {
  71. weight++;
  72. }
  73. if (!weight) {
  74. return;
  75. }
  76. if (!module.groups) {
  77. weight += 1;
  78. }
  79. const foundModules = (foundGroups[weight - 1] = foundGroups[weight - 1] || []);
  80. foundModules.push(module);
  81. });
  82. const {activeSize} = this;
  83. // Filtering out missing groups
  84. foundGroups = foundGroups.filter(Boolean).reverse();
  85. // Sorting each group by active size
  86. foundGroups.forEach(modules =>
  87. modules.sort((m1, m2) => m2[activeSize] - m1[activeSize])
  88. );
  89. return {
  90. chunk,
  91. modules: [].concat(...foundGroups),
  92. };
  93. })
  94. .filter(result => result.modules.length > 0)
  95. .sort((c1, c2) => c1.modules.length - c2.modules.length);
  96. }
  97. @computed get foundModules() {
  98. return this.foundModulesByChunk.reduce((arr, chunk) => arr.concat(chunk.modules), []);
  99. }
  100. @computed get hasFoundModules() {
  101. return this.foundModules.length > 0;
  102. }
  103. @computed get hasConcatenatedModules() {
  104. let result = false;
  105. walkModules(this.visibleChunks, module => {
  106. if (module.concatenated) {
  107. result = true;
  108. return false;
  109. }
  110. return undefined;
  111. });
  112. return result;
  113. }
  114. @computed get foundModulesSize() {
  115. return this.foundModules.reduce((summ, module) => summ + module[this.activeSize], 0);
  116. }
  117. filterModulesForSize(modules, sizeProp) {
  118. return modules.reduce((filteredModules, module) => {
  119. if (module[sizeProp]) {
  120. if (module.groups) {
  121. const showContent = !module.concatenated || this.showConcatenatedModulesContent;
  122. module = {
  123. ...module,
  124. groups: showContent
  125. ? this.filterModulesForSize(module.groups, sizeProp)
  126. : null,
  127. };
  128. }
  129. module.weight = module[sizeProp];
  130. filteredModules.push(module);
  131. }
  132. return filteredModules;
  133. }, []);
  134. }
  135. }
  136. export const store = new Store();