Skip to content

Commit

Permalink
feat(server): trace error.loc back to original source (#5467)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson authored Jan 3, 2022
1 parent f144aa9 commit 65cd44d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import type { ResolvedConfig } from '../config'
import { buildErrorMessage } from './middlewares/error'
import type { ModuleGraph } from './moduleGraph'
import { performance } from 'perf_hooks'
import { SourceMapConsumer } from 'source-map'
import type * as postcss from 'postcss'

export interface PluginContainerOptions {
Expand Down Expand Up @@ -313,20 +314,25 @@ export async function createPluginContainer(
const err = (
typeof e === 'string' ? new Error(e) : e
) as postcss.CssSyntaxError & RollupError
if (err.pluginCode) {
return err // The plugin likely called `this.error`
}
if (err.file && err.name === 'CssSyntaxError') {
err.id = normalizePath(err.file)
}
if (ctx._activePlugin) err.plugin = ctx._activePlugin.name
if (ctx._activeId && !err.id) err.id = ctx._activeId
if (ctx._activeCode) {
err.pluginCode = ctx._activeCode

const pos =
position != null
? position
: err.pos != null
? err.pos
: // some rollup plugins, e.g. json, sets position instead of pos
(err as any).position

if (pos != null) {
let errLocation
try {
Expand Down Expand Up @@ -366,6 +372,21 @@ export async function createPluginContainer(
}
err.frame = err.frame || generateCodeFrame(err.id!, err.loc)
}

if (err.loc && ctx instanceof TransformContext) {
const rawSourceMap = ctx._getCombinedSourcemap()
if (rawSourceMap) {
const consumer = new SourceMapConsumer(rawSourceMap as any)
const { source, line, column } = consumer.originalPositionFor({
line: Number(err.loc.line),
column: Number(err.loc.column),
bias: SourceMapConsumer.GREATEST_LOWER_BOUND
})
if (source) {
err.loc = { file: source, line, column }
}
}
}
}
return err
}
Expand Down

0 comments on commit 65cd44d

Please sign in to comment.