source-map-support.js
1.21 KB
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
/* @flow */
const SourceMapConsumer = require('source-map').SourceMapConsumer
const filenameRE = /\(([^)]+\.js):(\d+):(\d+)\)$/
export function createSourceMapConsumers (rawMaps: Object) {
const maps = {}
Object.keys(rawMaps).forEach(file => {
maps[file] = new SourceMapConsumer(rawMaps[file])
})
return maps
}
export function rewriteErrorTrace (e: any, mapConsumers: {
[key: string]: SourceMapConsumer
}) {
if (e && typeof e.stack === 'string') {
e.stack = e.stack.split('\n').map(line => {
return rewriteTraceLine(line, mapConsumers)
}).join('\n')
}
}
function rewriteTraceLine (trace: string, mapConsumers: {
[key: string]: SourceMapConsumer
}) {
const m = trace.match(filenameRE)
const map = m && mapConsumers[m[1]]
if (m != null && map) {
const originalPosition = map.originalPositionFor({
line: Number(m[2]),
column: Number(m[3])
})
if (originalPosition.source != null) {
const { source, line, column } = originalPosition
const mappedPosition = `(${source.replace(/^webpack:\/\/\//, '')}:${String(line)}:${String(column)})`
return trace.replace(filenameRE, mappedPosition)
} else {
return trace
}
} else {
return trace
}
}