-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathv8.js
343 lines (287 loc) · 9.69 KB
/
v8.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const EC = require('eight-colors');
const Util = require('../utils/util.js');
const { getV8Summary } = require('./v8-summary.js');
const { dedupeFlatRanges } = require('../utils/dedupe.js');
const {
resolveAnonymousUrl, normalizeSourcePath, getSourcePathReplacer
} = require('../utils/source-path.js');
const { collectSourceMaps } = require('../converter/collect-source-maps.js');
const { v8Report } = require('../reports/v8.js');
const { v8JsonReport } = require('../reports/v8-json.js');
const { customReport } = require('../reports/custom.js');
const getWrapperSource = (offset, source) => {
// NO \n because it break line number in mappings
let startStr = '';
if (offset >= 4) {
// fill comments
const spaces = ''.padEnd(offset - 4, '*');
startStr = `/*${spaces}*/`;
} else {
// fill spaces (should no chance)
startStr += ''.padEnd(offset, ' ');
}
return startStr + source;
};
const initV8ListAndSourcemap = async (mcr, v8list) => {
const { options, fileCache } = mcr;
// entry filter
const entryFilter = Util.getEntryFilter(options);
const lengthBefore = v8list.length;
v8list = v8list.filter(entryFilter);
const lengthAfter = v8list.length;
Util.logFilter('entry filter (add):', lengthBefore, lengthAfter);
// filter no source or text
// init type and css source from text
// keep functions
v8list = v8list.filter((entry) => {
// console.log(entry.url);
if (typeof entry.source === 'string' && entry.functions) {
entry.type = 'js';
return true;
}
if (typeof entry.text === 'string' && entry.ranges) {
entry.type = 'css';
return true;
}
// allow empty coverage
if (entry.empty) {
if (!['js', 'css'].includes(entry.type)) {
entry.type = 'js';
}
return true;
}
// 404 css, text will be empty
Util.logDebug(EC.red(`Invalid source or coverage data: ${entry.url}`));
// console.log(entry);
});
// onEntry hook
// after filter but before init, because id depends source and sourcePath
const onEntry = options.onEntry;
if (typeof onEntry === 'function') {
for (const entry of v8list) {
await onEntry(entry);
}
}
const sourcePathReplacer = getSourcePathReplacer(options);
const baseDir = options.baseDir;
// init id, sourcePath
v8list = v8list.map((entry, i) => {
// it could be a empty file
let source = `${entry.source || entry.text || ''}`;
// fix source with script offset
let scriptOffset;
const offset = Util.toNum(entry.scriptOffset, true);
if (offset > 0) {
scriptOffset = offset;
source = getWrapperSource(offset, source);
}
const data = {
url: entry.url,
type: entry.type,
source,
// script offset >= 0, for vm script
scriptOffset,
// Manually Resolve the Sourcemap
sourceMap: entry.sourceMap,
// fake source with `lineLengths`
fake: entry.fake,
// empty coverage
empty: entry.empty,
// match source if using empty coverage
distFile: entry.distFile
};
// coverage info
if (data.type === 'css') {
data.ranges = entry.ranges;
} else {
data.functions = entry.functions;
}
// resolve source path
const sourceUrl = resolveAnonymousUrl(data.url, i + 1, data.type);
let sourcePath = normalizeSourcePath(sourceUrl, baseDir);
if (sourcePathReplacer) {
const newSourcePath = sourcePathReplacer(sourcePath, data);
if (typeof newSourcePath === 'string' && newSourcePath) {
sourcePath = newSourcePath;
}
}
data.sourcePath = sourcePath;
// console.log(data.url, sourcePath);
// calculate source id
data.id = Util.calculateSha1(sourcePath + data.source);
return data;
});
// collect sourcemap first
const time_start = Date.now();
const { sourceList, sourcemapList } = await collectSourceMaps(v8list, options);
if (sourcemapList.length) {
// debug level time
Util.logTime(`loaded sourcemaps: ${sourcemapList.length}`, time_start);
// console.log(smList);
}
// save source list
for (const sourceData of sourceList) {
await Util.saveSourceCacheFile(sourceData, options, fileCache);
}
return v8list;
};
// ========================================================================================================
// force to async
const mergeCssRanges = (itemList) => {
return new Promise((resolve) => {
let concatRanges = [];
itemList.forEach((item) => {
concatRanges = concatRanges.concat(item.ranges);
});
// ranges: [ {start, end} ]
const ranges = dedupeFlatRanges(concatRanges);
resolve(ranges);
});
};
const mergeJsFunctions = async (itemList) => {
const res = await Util.mergeV8Coverage(itemList);
return res.functions;
};
const mergeV8DataList = async (dataList, options) => {
const allList = dataList.map((d) => d.data).flat();
// separate coverage and empty items
const coverageList = [];
const allEmptyList = [];
allList.forEach((it) => {
if (it.empty) {
allEmptyList.push(it);
} else {
coverageList.push(it);
}
});
// connect all functions and ranges
const itemMap = {};
const sourcePathMap = {};
const mergeMap = {};
coverageList.forEach((item) => {
const { id, sourcePath } = item;
const prev = itemMap[id];
if (prev) {
if (mergeMap[id]) {
mergeMap[id].push(item);
} else {
mergeMap[id] = [prev, item];
}
} else {
itemMap[id] = item;
sourcePathMap[sourcePath] = item;
}
});
// merge functions and ranges
const mergeIds = Object.keys(mergeMap);
for (const id of mergeIds) {
const itemList = mergeMap[id];
const item = itemMap[id];
if (item.type === 'css') {
item.ranges = await mergeCssRanges(itemList);
} else {
item.functions = await mergeJsFunctions(itemList);
}
}
// first time filter for empty, (not for sources)
// empty and not in item map
const emptyList = allEmptyList.filter((it) => {
if (itemMap[it.id]) {
return false;
}
if (sourcePathMap[it.sourcePath]) {
return false;
}
return true;
});
// merged list + empty list
const mergedList = Object.values(itemMap).concat(emptyList);
return mergedList;
};
const mergeV8Coverage = async (dataList, sourceCache, options) => {
const mergedList = await mergeV8DataList(dataList, options);
// try to load coverage and source by id
for (const entry of mergedList) {
const json = sourceCache.get(entry.id);
if (json) {
entry.source = json.source;
entry.sourceMap = json.sourceMap;
} else {
Util.logError(`Not found source data: ${Util.relativePath(entry.sourcePath)}`);
entry.source = '';
}
// add empty coverage after source init
if (entry.empty) {
Util.setEmptyV8Coverage(entry);
}
}
// GC
sourceCache.clear();
return mergedList;
};
// ============================================================
const saveV8Report = async (v8list, options, istanbulReportPath) => {
const defaultWatermarks = {
bytes: [50, 80],
statements: [50, 80],
branches: [50, 80],
functions: [50, 80],
lines: [50, 80]
};
const watermarks = Util.resolveWatermarks(defaultWatermarks, options.watermarks);
const summary = getV8Summary(v8list, watermarks);
const reportData = {
type: 'v8',
// override with saved file path
reportPath: '',
version: Util.version,
name: options.name,
watermarks,
summary,
files: v8list
};
// v8 only reports
const buildInV8Reports = {
'v8': v8Report,
'v8-json': v8JsonReport
};
const outputs = {};
const v8Group = options.reportGroup.get('v8');
// could be no v8 only reports, but have `both` data reports
if (v8Group) {
for (const [reportName, reportOptions] of v8Group) {
const buildInHandler = buildInV8Reports[reportName];
const t1 = Date.now();
if (buildInHandler) {
outputs[reportName] = await buildInHandler(reportData, reportOptions, options);
} else {
outputs[reportName] = await customReport(reportName, reportData, reportOptions, options);
}
Util.logTime(`${EC.magenta('├')} [generate] saved report: ${reportName}`, t1);
}
}
if (istanbulReportPath) {
outputs.istanbul = istanbulReportPath;
}
// outputPath, should be html or json
const reportPath = Util.resolveReportPath(options, () => {
return outputs.v8 || outputs.istanbul || Object.values(outputs).filter((it) => it && typeof it === 'string').shift();
});
const coverageResults = {
type: 'v8',
// html or json
reportPath,
version: Util.version,
name: options.name,
watermarks,
summary,
files: v8list
};
return coverageResults;
};
module.exports = {
initV8ListAndSourcemap,
mergeV8DataList,
mergeV8Coverage,
saveV8Report
};