Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions packages/vite/src/module-runner/esmEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import type { ModuleEvaluator, ModuleRunnerContext } from './types'

export class ESModulesEvaluator implements ModuleEvaluator {
public readonly startOffset: number =
getAsyncFunctionDeclarationPaddingLineCount()
// +1 for the `"use strict";` directive placed on its own line below
getAsyncFunctionDeclarationPaddingLineCount() + 1

async runInlinedModule(
context: ModuleRunnerContext,
Expand All @@ -28,8 +29,9 @@ export class ESModulesEvaluator implements ModuleEvaluator {
ssrDynamicImportKey,
ssrExportAllKey,
ssrExportNameKey,
// source map should already be inlined by Vite
'"use strict";' + code,
// source map should already be inlined by Vite.
// Place "use strict" on its own line to avoid shifting the column numbers
'"use strict";\n' + code,
)

await initModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ describe('module runner initialization', async () => {
})

it('stacktrace column on first line', async ({ runner, server }) => {
// column is off by "use strict"
const topLevelError = await getError(() =>
runner.import('/fixtures/has-error-first.js'),
)
expect(serializeStack(server, topLevelError)).toBe(
' at <root>/fixtures/has-error-first.js:1:18',
' at <root>/fixtures/has-error-first.js:1:7',
)

const topLevelErrorTs = await getError(() =>
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/ssr/ssrStacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function calculateOffsetOnce() {
}

try {
new Function('throw new Error(1)')()
// `"use strict";` mirrors the directive prepended on its own line by
// `ESModulesEvaluator`
new Function('"use strict";\nthrow 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,
Expand Down
Loading