Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ssr): calculate stacktrace offset lazily #13256

Merged
merged 1 commit into from May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/vite/src/node/ssr/ssrStacktrace.ts
Expand Up @@ -2,20 +2,28 @@ 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

function calculateOffsetOnce() {
if (offset !== undefined) {
return
}

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 {
calculateOffsetOnce()
return stack
.split('\n')
.map((line) => {
Expand Down