Skip to content

Commit

Permalink
chore: show error position (#13623)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jun 25, 2023
1 parent f899f9a commit 90271a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
15 changes: 13 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions packages/vite/src/node/ssr/ssrManifestPlugin.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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++) {
Expand Down

0 comments on commit 90271a6

Please sign in to comment.