Skip to content

Commit 21ffc6a

Browse files
authored
fix: throw ssr import error directly (fix #12322) (#12324)
1 parent 716286e commit 21ffc6a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { pathToFileURL } from 'node:url'
3+
import colors from 'picocolors'
34
import type { ViteDevServer } from '../server'
45
import {
56
dynamicImport,
@@ -203,17 +204,24 @@ async function instantiateModule(
203204
)
204205
} catch (e) {
205206
mod.ssrError = e
207+
206208
if (e.stack && fixStacktrace) {
207209
ssrFixStacktrace(e, moduleGraph)
208-
server.config.logger.error(
209-
`Error when evaluating SSR module ${url}:\n${e.stack}`,
210-
{
211-
timestamp: true,
212-
clear: server.config.clearScreen,
213-
error: e,
214-
},
215-
)
216210
}
211+
212+
server.config.logger.error(
213+
colors.red(
214+
`Error when evaluating SSR module ${url}:` +
215+
(e.importee ? ` failed to import "${e.importee}"\n` : '\n'),
216+
),
217+
{
218+
timestamp: true,
219+
clear: server.config.clearScreen,
220+
error: e,
221+
},
222+
)
223+
224+
delete e.importee
217225
throw e
218226
}
219227

@@ -257,7 +265,12 @@ async function nodeImport(
257265
try {
258266
const mod = await dynamicImport(url)
259267
return proxyESM(mod)
260-
} catch {}
268+
} catch (err) {
269+
// tell external error handler which mod was imported with error
270+
err.importee = id
271+
272+
throw err
273+
}
261274
}
262275

263276
// rollup-style default import interop for cjs

0 commit comments

Comments
 (0)