forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
istanbul-local.js
57 lines (48 loc) · 1.92 KB
/
istanbul-local.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const { SourceMapConsumer } = require('source-map');
const Istanbul = require('istanbul');
const inlineSourceMapRe = /\/\/# sourceMappingURL=data:application\/json;base64,(\S+)$/;
// Use the internal DevKit Hook of the require extension installed by our bootstrapping code to add
// Istanbul (not Constantinople) collection to the code.
const codeMap = new Map();
exports.codeMap = codeMap;
exports.istanbulRequireHook = function(code, filename) {
// Skip spec files.
if (filename.match(/_spec(_large)?\.ts$/)) {
return code;
}
const codeFile = codeMap.get(filename);
if (codeFile) {
return codeFile.code;
}
const instrumenter = new Istanbul.Instrumenter({
esModules: true,
codeGenerationOptions: {
sourceMap: filename,
sourceMapWithCode: true,
},
});
let instrumentedCode = instrumenter.instrumentSync(code, filename);
const match = code.match(inlineSourceMapRe);
if (match) {
const sourceMapGenerator = instrumenter.sourceMap;
// Fix source maps for exception reporting (since the exceptions happen in the instrumented
// code.
const sourceMapJson = JSON.parse(Buffer.from(match[1], 'base64').toString());
const consumer = new SourceMapConsumer(sourceMapJson);
sourceMapGenerator.applySourceMap(consumer, filename);
instrumentedCode = instrumentedCode.replace(inlineSourceMapRe, '')
+ '//# sourceMappingURL=data:application/json;base64,'
+ Buffer.from(sourceMapGenerator.toString()).toString('base64');
// Keep the consumer from the original source map, because the reports from Istanbul (not
// Constantinople) are already mapped against the code.
codeMap.set(filename, { code: instrumentedCode, map: consumer });
}
return instrumentedCode;
};