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

fix: determine anonymous function wrapper offset at runtime #2266

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/vite/src/node/ssr/ssrStacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { SourceMapConsumer, RawSourceMap } from 'source-map'
import { 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) {
return stack
.split('\n')
Expand All @@ -22,9 +33,7 @@ export function ssrRewriteStacktrace(stack: string, moduleGraph: ModuleGraph) {
)

const pos = consumer.originalPositionFor({
// source map lines generated via new Function() in Node.js is always
// incremented by 2 due to the function wrapper
line: Number(line) - 2,
line: Number(line) - offset,
column: Number(column),
bias: SourceMapConsumer.LEAST_UPPER_BOUND
})
Expand Down