MCPcopy
hub / github.com/vitejs/vite / ssrRewriteStacktrace

Function ssrRewriteStacktrace

packages/vite/src/node/ssr/ssrStacktrace.ts:25–75  ·  view source on GitHub ↗
(
  stack: string,
  moduleGraph: EnvironmentModuleGraph,
)

Source from the content-addressed store, hash-verified

23}
24
25export function ssrRewriteStacktrace(
26 stack: string,
27 moduleGraph: EnvironmentModuleGraph,
28): { result: string; alreadyRewritten: boolean } {
29 calculateOffsetOnce()
30
31 let alreadyRewritten = false
32 const rewritten = stack
33 .split('\n')
34 .map((line) => {
35 return line.replace(
36 /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
37 (input, varName, id, originalLine, originalColumn) => {
38 if (!id) return input
39
40 const mod = moduleGraph.getModuleById(id)
41 const rawSourceMap = mod?.transformResult?.map
42
43 if (!rawSourceMap) {
44 return input
45 }
46
47 const traced = new TraceMap(rawSourceMap as any)
48 const line = Number(originalLine) - offset
49 // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
50 const column = Number(originalColumn) - 1
51 if (line <= 0 || column < 0) {
52 alreadyRewritten = true
53 return input
54 }
55
56 const pos = originalPositionFor(traced, { line, column })
57 if (!pos.source) {
58 return input
59 }
60
61 const trimmedVarName = varName?.trim()
62 const sourceFile = path.resolve(path.dirname(id), pos.source)
63 // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
64 const source = `${sourceFile}:${pos.line}:${pos.column + 1}`
65 if (!trimmedVarName || trimmedVarName === 'eval') {
66 return ` at ${source}`
67 } else {
68 return ` at ${trimmedVarName} (${source})`
69 }
70 },
71 )
72 })
73 .join('\n')
74 return { result: rewritten, alreadyRewritten }
75}
76
77export function rebindErrorStacktrace(e: Error, stacktrace: string): void {
78 const { configurable, writable } = Object.getOwnPropertyDescriptor(

Callers 2

ssrRewriteStacktraceFunction · 0.90
ssrFixStacktraceFunction · 0.70

Calls 3

calculateOffsetOnceFunction · 0.85
resolveMethod · 0.65
getModuleByIdMethod · 0.45

Tested by

no test coverage detected