Skip to content

Commit 90271a6

Browse files
authored
chore: show error position (#13623)
1 parent f899f9a commit 90271a6

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

packages/vite/src/node/plugins/importAnalysisBuild.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
bareImportRE,
1111
cleanUrl,
1212
combineSourcemaps,
13+
generateCodeFrame,
1314
isDataUrl,
1415
isExternalUrl,
1516
isInNodeModules,
1617
moduleListContains,
18+
numberToPos,
1719
} from '../utils'
1820
import type { Plugin } from '../plugin'
1921
import { getDepOptimizationConfig } from '../config'
@@ -221,7 +223,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
221223
try {
222224
imports = parseImports(source)[0]
223225
} catch (e: any) {
224-
this.error(e)
226+
this.error(e, e.idx)
225227
}
226228

227229
if (!imports.length) {
@@ -462,7 +464,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
462464
try {
463465
imports = parseImports(code)[0].filter((i) => i.d > -1)
464466
} catch (e: any) {
465-
this.error(e)
467+
const loc = numberToPos(code, e.idx)
468+
this.error({
469+
name: e.name,
470+
message: e.message,
471+
stack: e.stack,
472+
cause: e.cause,
473+
pos: e.idx,
474+
loc: { ...loc, file: chunk.fileName },
475+
frame: generateCodeFrame(code, loc),
476+
})
466477
}
467478

468479
const s = new MagicString(code)

packages/vite/src/node/server/pluginContainer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export async function createPluginContainer(
279279
// active plugin in that pipeline can be tracked in a concurrency-safe manner.
280280
// using a class to make creating new contexts more efficient
281281
class Context implements PluginContext {
282-
meta = minimalContext.meta!
282+
meta = minimalContext.meta
283283
ssr = false
284284
_scan = false
285285
_activePlugin: Plugin | null

packages/vite/src/node/ssr/ssrManifestPlugin.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import jsonStableStringify from 'json-stable-stringify'
66
import type { ResolvedConfig } from '..'
77
import type { Plugin } from '../plugin'
88
import { preloadMethod } from '../plugins/importAnalysisBuild'
9-
import { joinUrlSegments, normalizePath } from '../utils'
9+
import {
10+
generateCodeFrame,
11+
joinUrlSegments,
12+
normalizePath,
13+
numberToPos,
14+
} from '../utils'
1015

1116
export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
1217
// module id => preload assets mapping
@@ -42,7 +47,16 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin {
4247
try {
4348
imports = parseImports(code)[0].filter((i) => i.n && i.d > -1)
4449
} catch (e: any) {
45-
this.error(e)
50+
const loc = numberToPos(code, e.idx)
51+
this.error({
52+
name: e.name,
53+
message: e.message,
54+
stack: e.stack,
55+
cause: e.cause,
56+
pos: e.idx,
57+
loc: { ...loc, file: chunk.fileName },
58+
frame: generateCodeFrame(code, loc),
59+
})
4660
}
4761
if (imports.length) {
4862
for (let index = 0; index < imports.length; index++) {

0 commit comments

Comments
 (0)