1 |
|
2 |
|
3 | const SourceMapConsumer = require('source-map').SourceMapConsumer
|
4 |
|
5 | const filenameRE = /\(([^)]+\.js):(\d+):(\d+)\)$/
|
6 |
|
7 | export function createSourceMapConsumers (rawMaps: Object) {
|
8 | const maps = {}
|
9 | Object.keys(rawMaps).forEach(file => {
|
10 | maps[file] = new SourceMapConsumer(rawMaps[file])
|
11 | })
|
12 | return maps
|
13 | }
|
14 |
|
15 | export function rewriteErrorTrace (e: any, mapConsumers: {
|
16 | [key: string]: SourceMapConsumer
|
17 | }) {
|
18 | if (e && typeof e.stack === 'string') {
|
19 | e.stack = e.stack.split('\n').map(line => {
|
20 | return rewriteTraceLine(line, mapConsumers)
|
21 | }).join('\n')
|
22 | }
|
23 | }
|
24 |
|
25 | function rewriteTraceLine (trace: string, mapConsumers: {
|
26 | [key: string]: SourceMapConsumer
|
27 | }) {
|
28 | const m = trace.match(filenameRE)
|
29 | const map = m && mapConsumers[m[1]]
|
30 | if (m != null && map) {
|
31 | const originalPosition = map.originalPositionFor({
|
32 | line: Number(m[2]),
|
33 | column: Number(m[3])
|
34 | })
|
35 | if (originalPosition.source != null) {
|
36 | const { source, line, column } = originalPosition
|
37 | const mappedPosition = `(${source.replace(/^webpack:\/\/\//, '')}:${String(line)}:${String(column)})`
|
38 | return trace.replace(filenameRE, mappedPosition)
|
39 | } else {
|
40 | return trace
|
41 | }
|
42 | } else {
|
43 | return trace
|
44 | }
|
45 | }
|