mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(runtime): fix sourcemap with prepareStackTrace
(#16220)
This commit is contained in:
parent
d7c5256996
commit
dad7f4f5a5
@ -202,21 +202,6 @@ function createRuntimeConfig(isProduction: boolean) {
|
||||
isProduction ? false : './dist/node',
|
||||
),
|
||||
esbuildMinifyPlugin({ minify: false, minifySyntax: true }),
|
||||
{
|
||||
name: 'replace bias',
|
||||
transform(code, id) {
|
||||
if (id.includes('@jridgewell+trace-mapping')) {
|
||||
return {
|
||||
code: code.replaceAll(
|
||||
'bias === LEAST_UPPER_BOUND',
|
||||
'true' +
|
||||
`/*${'bias === LEAST_UPPER_BOUND'.length - '/**/'.length - 'true'.length}*/`,
|
||||
),
|
||||
map: null,
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
bundleSizeLimit(45),
|
||||
],
|
||||
})
|
||||
|
@ -0,0 +1,7 @@
|
||||
function crash(message: string) {
|
||||
throw new Error(message)
|
||||
}
|
||||
|
||||
export function main(): void {
|
||||
crash('crash')
|
||||
}
|
@ -21,6 +21,11 @@ describe('vite-runtime initialization', async () => {
|
||||
const serializeStack = (runtime: ViteRuntime, err: Error) => {
|
||||
return err.stack!.split('\n')[1].replace(runtime.options.root, '<root>')
|
||||
}
|
||||
const serializeStackDeep = (runtime: ViteRuntime, err: Error) => {
|
||||
return err
|
||||
.stack!.split('\n')
|
||||
.map((s) => s.replace(runtime.options.root, '<root>'))
|
||||
}
|
||||
|
||||
it('source maps are correctly applied to stack traces', async ({
|
||||
runtime,
|
||||
@ -59,4 +64,16 @@ describe('vite-runtime initialization', async () => {
|
||||
' at Module.throwError (<root>/fixtures/throws-error-method.ts:11:9)',
|
||||
)
|
||||
})
|
||||
|
||||
it('deep stacktrace', async ({ runtime }) => {
|
||||
const methodError = await getError(async () => {
|
||||
const mod = await runtime.executeUrl('/fixtures/has-error-deep.ts')
|
||||
mod.main()
|
||||
})
|
||||
expect(serializeStackDeep(runtime, methodError).slice(0, 3)).toEqual([
|
||||
'Error: crash',
|
||||
' at crash (<root>/fixtures/has-error-deep.ts:2:9)',
|
||||
' at Module.main (<root>/fixtures/has-error-deep.ts:6:3)',
|
||||
])
|
||||
})
|
||||
})
|
||||
|
@ -98,6 +98,12 @@ describe.runIf(isServe)('stacktrace', () => {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
test('with Vite runtime', async () => {
|
||||
await execFileAsync('node', ['test-stacktrace-runtime.js'], {
|
||||
cwd: fileURLToPath(new URL('..', import.meta.url)),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe.runIf(isServe)('network-imports', () => {
|
||||
|
7
playground/ssr-html/src/has-error-deep.ts
Normal file
7
playground/ssr-html/src/has-error-deep.ts
Normal file
@ -0,0 +1,7 @@
|
||||
function crash(message: string) {
|
||||
throw new Error(message)
|
||||
}
|
||||
|
||||
export function main(): void {
|
||||
crash('crash')
|
||||
}
|
29
playground/ssr-html/test-stacktrace-runtime.js
Normal file
29
playground/ssr-html/test-stacktrace-runtime.js
Normal file
@ -0,0 +1,29 @@
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import assert from 'node:assert'
|
||||
import { createServer, createViteRuntime } from 'vite'
|
||||
|
||||
// same test case as packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts
|
||||
// implemented for e2e to catch build specific behavior
|
||||
|
||||
const server = await createServer({
|
||||
configFile: false,
|
||||
root: fileURLToPath(new URL('.', import.meta.url)),
|
||||
server: {
|
||||
middlewareMode: true,
|
||||
},
|
||||
})
|
||||
|
||||
const runtime = await createViteRuntime(server, {
|
||||
sourcemapInterceptor: 'prepareStackTrace',
|
||||
})
|
||||
|
||||
const mod = await runtime.executeEntrypoint('/src/has-error-deep.ts')
|
||||
let error
|
||||
try {
|
||||
mod.main()
|
||||
} catch (e) {
|
||||
error = e
|
||||
} finally {
|
||||
await server.close()
|
||||
}
|
||||
assert.match(error?.stack, /has-error-deep.ts:6:3/)
|
Loading…
Reference in New Issue
Block a user