/
ssrStacktrace.ts
75 lines (66 loc) · 2 KB
/
ssrStacktrace.ts
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
import { SourceMapConsumer, RawSourceMap } from 'source-map'
import { ModuleGraph } from '../server/moduleGraph'
let offset: number
try {
new Function('throw new Error(1)')()
} catch (e) {
// in Node 12, stack traces account for the function wrapper.
// in Node 13 and later, the function wrapper adds two lines,
// which must be subtracted to generate a valid mapping
const match = /:(\d+):\d+\)$/.exec(e.stack.split('\n')[1])
offset = match ? +match[1] - 1 : 0
}
export function ssrRewriteStacktrace(
stack: string,
moduleGraph: ModuleGraph
): string {
return stack
.split('\n')
.map((line) => {
return line.replace(
/^ {4}at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?)\)?/,
(input, varName, url, line, column) => {
if (!url) return input
const mod = moduleGraph.urlToModuleMap.get(url)
const rawSourceMap = mod?.ssrTransformResult?.map
if (!rawSourceMap) {
return input
}
const consumer = new SourceMapConsumer(
rawSourceMap as any as RawSourceMap
)
const pos = consumer.originalPositionFor({
line: Number(line) - offset,
column: Number(column),
bias: SourceMapConsumer.LEAST_UPPER_BOUND
})
if (!pos.source) {
return input
}
const source = `${pos.source}:${pos.line || 0}:${pos.column || 0}`
if (!varName || varName === 'eval') {
return ` at ${source}`
} else {
return ` at ${varName} (${source})`
}
}
)
})
.join('\n')
}
export function rebindErrorStacktrace(e: Error, stacktrace: string): void {
const { configurable, writable } = Object.getOwnPropertyDescriptor(
e,
'stack'
)!
if (configurable) {
Object.defineProperty(e, 'stack', {
value: stacktrace,
enumerable: true,
configurable: true,
writable: true
})
} else if (writable) {
e.stack = stacktrace
}
}