Skip to content

Commit

Permalink
fix: throw ssr import error directly (fix #12322) (#12324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Mar 12, 2023
1 parent 716286e commit 21ffc6a
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
@@ -1,5 +1,6 @@
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import colors from 'picocolors'
import type { ViteDevServer } from '../server'
import {
dynamicImport,
Expand Down Expand Up @@ -203,17 +204,24 @@ async function instantiateModule(
)
} catch (e) {
mod.ssrError = e

if (e.stack && fixStacktrace) {
ssrFixStacktrace(e, moduleGraph)
server.config.logger.error(
`Error when evaluating SSR module ${url}:\n${e.stack}`,
{
timestamp: true,
clear: server.config.clearScreen,
error: e,
},
)
}

server.config.logger.error(
colors.red(
`Error when evaluating SSR module ${url}:` +
(e.importee ? ` failed to import "${e.importee}"\n` : '\n'),
),
{
timestamp: true,
clear: server.config.clearScreen,
error: e,
},
)

delete e.importee
throw e
}

Expand Down Expand Up @@ -257,7 +265,12 @@ async function nodeImport(
try {
const mod = await dynamicImport(url)
return proxyESM(mod)
} catch {}
} catch (err) {
// tell external error handler which mod was imported with error
err.importee = id

throw err
}
}

// rollup-style default import interop for cjs
Expand Down

0 comments on commit 21ffc6a

Please sign in to comment.