Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: catch postcss error messages #6293

Merged
merged 3 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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