From 12af61343e6d7561e2e6a3663a299fff4d5aa6c3 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 8 Mar 2023 00:33:41 +0800 Subject: [PATCH 1/5] fix: throw ssr import error directly (fix #12322) --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 4cb226de83944e..af378825392ae5 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -257,7 +257,11 @@ async function nodeImport( try { const mod = await dynamicImport(url) return proxyESM(mod) - } catch {} + } catch (err) { + err.message = `\nFailed to import ${url} from ${importer}\n${err.message}` + + throw err + } } // rollup-style default import interop for cjs From db8b7be0de0a9aed8a7d524b3944303694ceacba Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 12 Mar 2023 13:30:45 +0800 Subject: [PATCH 2/5] fix: format ssr load module error --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index af378825392ae5..b5ad97d52b45e8 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -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, @@ -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) @@ -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'), + { + timestamp: true, + clear: server.config.clearScreen, + }, + ) + + delete e.importee throw e } @@ -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 { @@ -258,7 +265,7 @@ async function nodeImport( const mod = await dynamicImport(url) return proxyESM(mod) } catch (err) { - err.message = `\nFailed to import ${url} from ${importer}\n${err.message}` + err.importee = id throw err } From 08d19f72b0ac5f8478106b70921c3bdc74935a27 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 12 Mar 2023 18:02:01 +0800 Subject: [PATCH 3/5] refactor: undo nodeImport mod param --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index b5ad97d52b45e8..5eeeda85047aba 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -10,7 +10,6 @@ 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, @@ -132,7 +131,7 @@ async function instantiateModule( const ssrImport = async (dep: string) => { if (dep[0] !== '.' && dep[0] !== '/') { - return nodeImport(dep, mod, resolveOptions) + return nodeImport(dep, mod.file!, resolveOptions) } // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that dep = unwrapId(dep) @@ -228,12 +227,10 @@ async function instantiateModule( // In node@12+ we can use dynamic import to load CJS and ESM async function nodeImport( id: string, - mod: ModuleNode, + importer: string, resolveOptions: InternalResolveOptionsWithOverrideConditions, ) { let url: string - const importer = mod.file - if (id.startsWith('node:') || isBuiltin(id)) { url = id } else { From 0ef6d2011b94b62c13371f9bea38cfa053658a17 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 12 Mar 2023 18:11:51 +0800 Subject: [PATCH 4/5] chore: add err.importee comment --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 5eeeda85047aba..a8cb12c5738592 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -262,6 +262,7 @@ async function nodeImport( 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 From f7bfca4e32a1e4cbedb034361453bc7098db6e89 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 12 Mar 2023 18:17:30 +0800 Subject: [PATCH 5/5] feat: mark ssr load error red --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index a8cb12c5738592..2e7b265c085115 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/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, @@ -209,11 +210,14 @@ async function instantiateModule( } server.config.logger.error( - `Error when evaluating SSR module ${url}:` + - (e.importee ? ` failed to import '${e.importee}'\n` : '\n'), + 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, }, )