-
-
Notifications
You must be signed in to change notification settings - Fork 488
/
store.js
184 lines (145 loc) · 4.45 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {observable, computed} from 'mobx';
import {isChunkParsed, walkModules} from './utils';
import localStorage from './localStorage';
export class Store {
cid = 0;
sizes = new Set(['statSize', 'parsedSize', 'gzipSize']);
@observable.ref allChunks;
@observable.shallow selectedChunks;
@observable searchQuery = '';
@observable defaultSize;
@observable selectedSize;
@observable showConcatenatedModulesContent = (localStorage.getItem('showConcatenatedModulesContent') === true);
setModules(modules) {
walkModules(modules, module => {
module.cid = this.cid++;
});
this.allChunks = modules;
this.selectedChunks = this.allChunks;
}
setEntrypoints(entrypoints) {
this.entrypoints = entrypoints;
}
@computed get hasParsedSizes() {
return this.allChunks.some(isChunkParsed);
}
@computed get activeSize() {
const activeSize = this.selectedSize || this.defaultSize;
if (!this.hasParsedSizes || !this.sizes.has(activeSize)) {
return 'statSize';
}
return activeSize;
}
@computed get visibleChunks() {
const visibleChunks = this.allChunks.filter(chunk =>
this.selectedChunks.includes(chunk)
);
return this.filterModulesForSize(visibleChunks, this.activeSize);
}
@computed get allChunksSelected() {
return this.visibleChunks.length === this.allChunks.length;
}
@computed get totalChunksSize() {
return this.allChunks.reduce((totalSize, chunk) =>
totalSize + (chunk[this.activeSize] || 0),
0);
}
@computed get searchQueryRegexp() {
const query = this.searchQuery.trim();
if (!query) {
return null;
}
try {
return new RegExp(query, 'iu');
} catch (err) {
return null;
}
}
@computed get isSearching() {
return !!this.searchQueryRegexp;
}
@computed get foundModulesByChunk() {
if (!this.isSearching) {
return [];
}
const query = this.searchQueryRegexp;
return this.visibleChunks
.map(chunk => {
let foundGroups = [];
walkModules(chunk.groups, module => {
let weight = 0;
/**
* Splitting found modules/directories into groups:
*
* 1) Module with matched label (weight = 4)
* 2) Directory with matched label (weight = 3)
* 3) Module with matched path (weight = 2)
* 4) Directory with matched path (weight = 1)
*/
if (query.test(module.label)) {
weight += 3;
} else if (module.path && query.test(module.path)) {
weight++;
}
if (!weight) return;
if (!module.groups) {
weight += 1;
}
const foundModules = foundGroups[weight - 1] = foundGroups[weight - 1] || [];
foundModules.push(module);
});
const {activeSize} = this;
// Filtering out missing groups
foundGroups = foundGroups.filter(Boolean).reverse();
// Sorting each group by active size
foundGroups.forEach(modules =>
modules.sort((m1, m2) => m2[activeSize] - m1[activeSize])
);
return {
chunk,
modules: [].concat(...foundGroups)
};
})
.filter(result => result.modules.length > 0)
.sort((c1, c2) => c1.modules.length - c2.modules.length);
}
@computed get foundModules() {
return this.foundModulesByChunk.reduce((arr, chunk) => arr.concat(chunk.modules), []);
}
@computed get hasFoundModules() {
return this.foundModules.length > 0;
}
@computed get hasConcatenatedModules() {
let result = false;
walkModules(this.visibleChunks, module => {
if (module.concatenated) {
result = true;
return false;
}
});
return result;
}
@computed get foundModulesSize() {
return this.foundModules.reduce(
(summ, module) => summ + module[this.activeSize],
0
);
}
filterModulesForSize(modules, sizeProp) {
return modules.reduce((filteredModules, module) => {
if (module[sizeProp]) {
if (module.groups) {
const showContent = (!module.concatenated || this.showConcatenatedModulesContent);
module = {
...module,
groups: showContent ? this.filterModulesForSize(module.groups, sizeProp) : null
};
}
module.weight = module[sizeProp];
filteredModules.push(module);
}
return filteredModules;
}, []);
}
}
export const store = new Store();