Skip to content

Commit

Permalink
feat: catch postcss error messages (vitejs#6293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Niputi authored Dec 29, 2021
1 parent f68ed8b commit 4d75b2e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 8 additions & 2 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 type * as postcss from 'postcss'

export interface PluginContainerOptions {
cwd?: string
Expand Down Expand Up @@ -309,7 +310,12 @@ export async function createPluginContainer(
position: number | { column: number; line: number } | undefined,
ctx: Context
) {
const err = (typeof e === 'string' ? new Error(e) : e) as RollupError
const err = (
typeof e === 'string' ? new Error(e) : e
) as postcss.CssSyntaxError & RollupError
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) {
Expand Down Expand Up @@ -358,7 +364,7 @@ export async function createPluginContainer(
line: (err as any).line,
column: (err as any).column
}
err.frame = err.frame || generateCodeFrame(ctx._activeCode, err.loc)
err.frame = err.frame || generateCodeFrame(err.id!, err.loc)
}
}
return err
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ export function posToNumber(
const { line, column } = pos
let start = 0
for (let i = 0; i < line - 1; i++) {
start += lines[i].length + 1
if (lines[i]) {
start += lines[i].length + 1
}
}
return start + column
}
Expand Down

0 comments on commit 4d75b2e

Please sign in to comment.