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

fix: code frame was not generated for postcss errors #14986

Merged
merged 9 commits into from
Nov 15, 2023
7 changes: 4 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,12 @@ async function compileCSS(
code,
{
line: warning.line,
column: warning.column,
column: warning.column - 1, // 1-based
},
warning.endLine !== undefined && warning.endColumn !== undefined
? {
line: warning.endLine,
column: warning.endColumn,
column: warning.endColumn - 1, // 1-based
}
: undefined,
)}`
Expand All @@ -1207,8 +1207,9 @@ async function compileCSS(
e.message = `[postcss] ${e.message}`
e.code = code
e.loc = {
column: e.column,
file: e.file,
line: e.line,
column: e.column - 1, // 1-based
}
throw e
}
Expand Down
10 changes: 2 additions & 8 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import MagicString from 'magic-string'
import type { FSWatcher } from 'chokidar'
import colors from 'picocolors'
import type * as postcss from 'postcss'
import type { Plugin } from '../plugin'
import {
cleanUrl,
Expand Down Expand Up @@ -429,15 +428,10 @@ export async function createPluginContainer(
position: number | { column: number; line: number } | undefined,
ctx: Context,
) {
const err = (
typeof e === 'string' ? new Error(e) : e
) as postcss.CssSyntaxError & RollupError
const err = (typeof e === 'string' ? new Error(e) : e) as 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) {
Expand Down Expand Up @@ -483,7 +477,7 @@ export async function createPluginContainer(
line: (err as any).line,
column: (err as any).column,
}
err.frame = err.frame || generateCodeFrame(err.id!, err.loc)
err.frame = err.frame || generateCodeFrame(ctx._activeCode, err.loc)
}

if (
Expand Down