diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 53b7eba51076ec..8e8482776d9c03 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -10,10 +10,12 @@ import { bareImportRE, cleanUrl, combineSourcemaps, + generateCodeFrame, isDataUrl, isExternalUrl, isInNodeModules, moduleListContains, + numberToPos, } from '../utils' import type { Plugin } from '../plugin' import { getDepOptimizationConfig } from '../config' @@ -221,7 +223,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { try { imports = parseImports(source)[0] } catch (e: any) { - this.error(e) + this.error(e, e.idx) } if (!imports.length) { @@ -462,7 +464,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { try { imports = parseImports(code)[0].filter((i) => i.d > -1) } catch (e: any) { - this.error(e) + const loc = numberToPos(code, e.idx) + this.error({ + name: e.name, + message: e.message, + stack: e.stack, + cause: e.cause, + pos: e.idx, + loc: { ...loc, file: chunk.fileName }, + frame: generateCodeFrame(code, loc), + }) } const s = new MagicString(code) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 88283145fbd7fa..5b4e432c9c3a08 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -279,7 +279,7 @@ export async function createPluginContainer( // active plugin in that pipeline can be tracked in a concurrency-safe manner. // using a class to make creating new contexts more efficient class Context implements PluginContext { - meta = minimalContext.meta! + meta = minimalContext.meta ssr = false _scan = false _activePlugin: Plugin | null diff --git a/packages/vite/src/node/ssr/ssrManifestPlugin.ts b/packages/vite/src/node/ssr/ssrManifestPlugin.ts index bd2fff7368c51b..f6beb218759247 100644 --- a/packages/vite/src/node/ssr/ssrManifestPlugin.ts +++ b/packages/vite/src/node/ssr/ssrManifestPlugin.ts @@ -6,7 +6,12 @@ import jsonStableStringify from 'json-stable-stringify' import type { ResolvedConfig } from '..' import type { Plugin } from '../plugin' import { preloadMethod } from '../plugins/importAnalysisBuild' -import { joinUrlSegments, normalizePath } from '../utils' +import { + generateCodeFrame, + joinUrlSegments, + normalizePath, + numberToPos, +} from '../utils' export function ssrManifestPlugin(config: ResolvedConfig): Plugin { // module id => preload assets mapping @@ -42,7 +47,16 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { try { imports = parseImports(code)[0].filter((i) => i.n && i.d > -1) } catch (e: any) { - this.error(e) + const loc = numberToPos(code, e.idx) + this.error({ + name: e.name, + message: e.message, + stack: e.stack, + cause: e.cause, + pos: e.idx, + loc: { ...loc, file: chunk.fileName }, + frame: generateCodeFrame(code, loc), + }) } if (imports.length) { for (let index = 0; index < imports.length; index++) {