-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-source-map.js
70 lines (62 loc) · 2.08 KB
/
generate-source-map.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
const path = require('path');
const sourceMap = require('source-map');
const splitRE = /\r?\n/g;
module.exports = function generateSourceMap(
scriptResult,
src,
filename,
renderFnStartLine,
renderFnEndLine,
templateLine,
) {
const file = path.basename(filename);
const map = new sourceMap.SourceMapGenerator();
const scriptFromExternalSrc = scriptResult && scriptResult.externalSrc;
// If script uses external file for content (using the src attribute) then
// map result to this file, instead of original.
const srcContent = scriptFromExternalSrc ? scriptResult.externalSrc : src;
map.setSourceContent(file, srcContent);
if (scriptResult) {
const inputMapConsumer =
scriptResult.map && new sourceMap.SourceMapConsumer(scriptResult.map);
scriptResult.code.split(splitRE).forEach((line, index) => {
const ln = index + 1;
const originalLine = inputMapConsumer ?
inputMapConsumer.originalPositionFor({ line: ln, column: 0 }).line :
ln;
if (originalLine) {
map.addMapping({
source: file,
generated: {
line: ln,
column: 0,
},
original: {
line: originalLine,
column: 0,
},
});
}
});
}
// If script is from external src then the source map will only show the
// script section. We won't map the generated render function to this file,
// because the coverage report would be confusing
if (scriptFromExternalSrc) {
return map;
}
for (; renderFnStartLine < renderFnEndLine; renderFnStartLine++) {
map.addMapping({
source: file,
generated: {
line: renderFnStartLine,
column: 0,
},
original: {
line: templateLine,
column: 0,
},
});
}
return map;
};