Skip to content

Commit

Permalink
fix: code frame was not generated for postcss errors (#14986)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 15, 2023
1 parent 2b4e793 commit bedfcfa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
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

0 comments on commit bedfcfa

Please sign in to comment.