forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssrStacktrace.ts
86 lines (73 loc) · 2.37 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
76
77
78
79
80
81
82
83
84
85
86
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import type { 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.*?)\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 traced = new TraceMap(rawSourceMap as any)
const pos = originalPositionFor(traced, {
line: Number(line) - offset,
column: Number(column),
})
if (!pos.source || pos.line == null || pos.column == null) {
return input
}
const trimedVarName = varName.trim()
const source = `${pos.source}:${pos.line}:${pos.column}`
if (!trimedVarName || trimedVarName === 'eval') {
return ` at ${source}`
} else {
return ` at ${trimedVarName} (${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
}
}
const rewroteStacktraces = new WeakSet()
export function ssrFixStacktrace(e: Error, moduleGraph: ModuleGraph): void {
if (!e.stack) return
// stacktrace shouldn't be rewritten more than once
if (rewroteStacktraces.has(e)) return
const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph)
rebindErrorStacktrace(e, stacktrace)
rewroteStacktraces.add(e)
}