Skip to content

Commit

Permalink
fix: properly format css pre-processor errors from @imported files
Browse files Browse the repository at this point in the history
fix #1600, close #1601
  • Loading branch information
yyx990803 committed Jan 20, 2021
1 parent 1c62706 commit ec18bde
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -517,6 +517,9 @@ const scss: StylePreprocessor = async (source, map, options) => {

return { code: result.css.toString(), errors: [], deps }
} catch (e) {
// normalize SASS error
e.id = e.file
e.frame = e.formatted
return { code: '', errors: [e], deps: [] }
}
}
Expand All @@ -529,6 +532,7 @@ const sass: StylePreprocessor = (source, map, options) =>

// .less
interface LessError {
filename: string
message: string
line: number
column: number
Expand All @@ -552,7 +556,7 @@ const less: StylePreprocessor = (source, map, options) => {
// normalize error info
const normalizedError: RollupError = new Error(error!.message)
normalizedError.loc = {
file: options.filename,
file: error!.filename || options.filename,
line: error!.line,
column: error!.column
}
Expand Down
12 changes: 10 additions & 2 deletions packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -300,15 +300,23 @@ export async function createPluginContainer(
...numberToPos(ctx._activeCode, pos)
}
err.frame = err.frame || generateCodeFrame(ctx._activeCode, pos)
} else if (err.loc) {
// css preprocessors may report errors in an included file
if (!err.frame) {
let code = ctx._activeCode
if (err.loc.file) {
err.id = normalizePath(err.loc.file)
code = fs.readFileSync(err.loc.file, 'utf-8')
}
err.frame = generateCodeFrame(code, err.loc)
}
} else if ((err as any).line && (err as any).column) {
err.loc = {
file: err.id,
line: (err as any).line,
column: (err as any).column
}
err.frame = err.frame || generateCodeFrame(ctx._activeCode, err.loc)
} else if (err.loc) {
err.frame = err.frame || generateCodeFrame(ctx._activeCode, err.loc)
}
}
return err
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -201,7 +201,7 @@ export function posToNumber(
const { line, column } = pos
let start = 0
for (let i = 0; i < line - 1; i++) {
start += lines[i].length
start += lines[i].length + 1
}
return start + column
}
Expand Down

0 comments on commit ec18bde

Please sign in to comment.