Skip to content

Commit 353c467

Browse files
authoredJan 27, 2025··
fix(ssr): pretty print plugin error in ssrLoadModule (#19290)
1 parent 97569ef commit 353c467

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
 

‎packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url'
22
import path from 'node:path'
33
import fs from 'node:fs'
44
import { stripVTControlCharacters } from 'node:util'
5-
import { expect, test } from 'vitest'
5+
import { expect, onTestFinished, test, vi } from 'vitest'
66
import { createServer } from '../../server'
77
import { normalizePath } from '../../utils'
88

@@ -251,3 +251,44 @@ test('file url', async () => {
251251
)
252252
expect(modWithSpace.msg).toBe('works')
253253
})
254+
255+
test('plugin error', async () => {
256+
const server = await createServer({
257+
configFile: false,
258+
root,
259+
logLevel: 'error',
260+
plugins: [
261+
{
262+
name: 'test-plugin',
263+
resolveId(source) {
264+
if (source === 'virtual:test') {
265+
return '\0' + source
266+
}
267+
},
268+
load(id) {
269+
if (id === '\0virtual:test') {
270+
return this.error('test-error')
271+
}
272+
},
273+
},
274+
],
275+
})
276+
onTestFinished(() => server.close())
277+
278+
const spy = vi
279+
.spyOn(server.config.logger, 'error')
280+
.mockImplementation(() => {})
281+
try {
282+
await server.ssrLoadModule('virtual:test')
283+
expect.unreachable()
284+
} catch {}
285+
expect(
286+
stripVTControlCharacters(spy.mock.lastCall![0])
287+
.split('\n')
288+
.slice(0, 2)
289+
.join('\n'),
290+
).toMatchInlineSnapshot(`
291+
"Error when evaluating SSR module virtual:test: test-error
292+
Plugin: test-plugin"
293+
`)
294+
})

‎packages/vite/src/node/ssr/ssrModuleLoader.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ViteDevServer } from '../server'
55
import { unwrapId } from '../../shared/utils'
66
import type { DevEnvironment } from '../server/environment'
77
import type { NormalizedServerHotChannel } from '../server/hmr'
8+
import { buildErrorMessage } from '../server/middlewares/error'
89
import { ssrFixStacktrace } from './ssrStacktrace'
910
import { createServerModuleRunnerTransport } from './runtime/serverModuleRunner'
1011

@@ -47,7 +48,9 @@ async function instantiateModule(
4748
}
4849

4950
environment.logger.error(
50-
colors.red(`Error when evaluating SSR module ${url}:\n|- ${e.stack}\n`),
51+
buildErrorMessage(e, [
52+
colors.red(`Error when evaluating SSR module ${url}: ${e.message}`),
53+
]),
5154
{
5255
timestamp: true,
5356
clear: environment.config.clearScreen,

0 commit comments

Comments
 (0)
Please sign in to comment.