Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw ssr import error directly (fix #12322) #12324

Merged
merged 5 commits into from
Mar 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { transformRequest } from '../server/transformRequest'
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import type { ModuleNode } from '../server/moduleGraph'
import {
ssrDynamicImportKey,
ssrExportAllKey,
Expand Down Expand Up @@ -131,7 +132,7 @@ async function instantiateModule(

const ssrImport = async (dep: string) => {
if (dep[0] !== '.' && dep[0] !== '/') {
return nodeImport(dep, mod.file!, resolveOptions)
return nodeImport(dep, mod, resolveOptions)
}
// convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
dep = unwrapId(dep)
Expand Down Expand Up @@ -203,17 +204,21 @@ 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(
`Error when evaluating SSR module ${url}:` +
(e.importee ? ` failed to import '${e.importee}'\n` : '\n'),
sun0day marked this conversation as resolved.
Show resolved Hide resolved
{
timestamp: true,
clear: server.config.clearScreen,
},
)
sun0day marked this conversation as resolved.
Show resolved Hide resolved

delete e.importee
throw e
}

Expand All @@ -223,10 +228,12 @@ async function instantiateModule(
// In node@12+ we can use dynamic import to load CJS and ESM
async function nodeImport(
id: string,
importer: string,
mod: ModuleNode,
resolveOptions: InternalResolveOptionsWithOverrideConditions,
) {
let url: string
const importer = mod.file

if (id.startsWith('node:') || isBuiltin(id)) {
url = id
} else {
Expand Down Expand Up @@ -257,7 +264,11 @@ async function nodeImport(
try {
const mod = await dynamicImport(url)
return proxyESM(mod)
} catch {}
} catch (err) {
err.importee = id

throw err
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

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