Skip to content

Commit

Permalink
fix(ssr): show ssr module loader error stack (#12651)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Mar 31, 2023
1 parent 15177a1 commit 050c0f9
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SSRModule = Record<string, any>

const pendingModules = new Map<string, Promise<SSRModule>>()
const pendingImports = new Map<string, string[]>()
const importErrors = new WeakMap<Error, { importee: string }>()

export async function ssrLoadModule(
url: string,
Expand Down Expand Up @@ -131,32 +132,39 @@ async function instantiateModule(
const pendingDeps: string[] = []

const ssrImport = async (dep: string) => {
if (dep[0] !== '.' && dep[0] !== '/') {
return nodeImport(dep, mod.file!, resolveOptions)
}
// convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
dep = unwrapId(dep)
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
pendingDeps.push(dep)
if (pendingDeps.length === 1) {
pendingImports.set(url, pendingDeps)
try {
if (dep[0] !== '.' && dep[0] !== '/') {
return await nodeImport(dep, mod.file!, resolveOptions)
}
const mod = await ssrLoadModule(
dep,
server,
context,
urlStack,
fixStacktrace,
)
if (pendingDeps.length === 1) {
pendingImports.delete(url)
} else {
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
// convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
dep = unwrapId(dep)
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
pendingDeps.push(dep)
if (pendingDeps.length === 1) {
pendingImports.set(url, pendingDeps)
}
const mod = await ssrLoadModule(
dep,
server,
context,
urlStack,
fixStacktrace,
)
if (pendingDeps.length === 1) {
pendingImports.delete(url)
} else {
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
}
// return local module to avoid race condition #5470
return mod
}
// return local module to avoid race condition #5470
return mod
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
} catch (err) {
// tell external error handler which mod was imported with error
importErrors.set(err, { importee: dep })

throw err
}
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
}

const ssrDynamicImport = (dep: string) => {
Expand Down Expand Up @@ -204,6 +212,7 @@ async function instantiateModule(
)
} catch (e) {
mod.ssrError = e
const errorData = importErrors.get(e)

if (e.stack && fixStacktrace) {
ssrFixStacktrace(e, moduleGraph)
Expand All @@ -212,7 +221,10 @@ async function instantiateModule(
server.config.logger.error(
colors.red(
`Error when evaluating SSR module ${url}:` +
(e.importee ? ` failed to import "${e.importee}"\n` : '\n'),
(errorData?.importee
? ` failed to import "${errorData.importee}"`
: '') +
`\n|- ${e.stack}\n`,
),
{
timestamp: true,
Expand All @@ -221,7 +233,6 @@ async function instantiateModule(
},
)

delete e.importee
throw e
}

Expand Down Expand Up @@ -262,15 +273,8 @@ async function nodeImport(
}
}

try {
const mod = await dynamicImport(url)
return proxyESM(mod)
} catch (err) {
// tell external error handler which mod was imported with error
err.importee = id

throw err
}
const mod = await dynamicImport(url)
return proxyESM(mod)
}

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

0 comments on commit 050c0f9

Please sign in to comment.