Skip to content

Commit 906c4c1

Browse files
author
Tomer Ohana
authored
perf(ssr): calculate stacktrace offset lazily (#13256)
1 parent 3f3fff2 commit 906c4c1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

packages/vite/src/node/ssr/ssrStacktrace.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
33
import type { ModuleGraph } from '../server/moduleGraph'
44

55
let offset: number
6-
try {
7-
new Function('throw new Error(1)')()
8-
} catch (e) {
9-
// in Node 12, stack traces account for the function wrapper.
10-
// in Node 13 and later, the function wrapper adds two lines,
11-
// which must be subtracted to generate a valid mapping
12-
const match = /:(\d+):\d+\)$/.exec(e.stack.split('\n')[1])
13-
offset = match ? +match[1] - 1 : 0
6+
7+
function calculateOffsetOnce() {
8+
if (offset !== undefined) {
9+
return
10+
}
11+
12+
try {
13+
new Function('throw new Error(1)')()
14+
} catch (e) {
15+
// in Node 12, stack traces account for the function wrapper.
16+
// in Node 13 and later, the function wrapper adds two lines,
17+
// which must be subtracted to generate a valid mapping
18+
const match = /:(\d+):\d+\)$/.exec(e.stack.split('\n')[1])
19+
offset = match ? +match[1] - 1 : 0
20+
}
1421
}
1522

1623
export function ssrRewriteStacktrace(
1724
stack: string,
1825
moduleGraph: ModuleGraph,
1926
): string {
27+
calculateOffsetOnce()
2028
return stack
2129
.split('\n')
2230
.map((line) => {

0 commit comments

Comments
 (0)