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

Function ssrRewriteStacktrace

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

Source from the content-addressed store, hash-verified

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