Skip to content

Commit

Permalink
refactor: improve vue compiler error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 29, 2020
1 parent ca02b9b commit 206b7b7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
30 changes: 10 additions & 20 deletions packages/plugin-vue/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,16 @@ export function createRollupError(
id: string,
error: CompilerError | SyntaxError
): RollupError {
if ('code' in error) {
return {
id,
plugin: 'vue',
message: error.message,
parserError: error,
loc: error.loc
? {
file: id,
line: error.loc.start.line,
column: error.loc.start.column
}
: undefined
}
} else {
return {
id,
plugin: 'vue',
message: error.message,
parserError: error
;(error as RollupError).id = id
;(error as RollupError).plugin = 'vue'

if ('code' in error && error.loc) {
;(error as any).loc = {
file: id,
line: error.loc.start.line,
column: error.loc.start.column
}
}

return error as RollupError
}
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function errorMiddleware(
// note the 4 args must be kept for connect to treat this as error middleware
return (err: RollupError, _req, res, _next) => {
const args = [chalk.red(`Internal server error:`)]
if (err.plugin) args.push(` Plugin: ${chalk.green(err.plugin)}`)
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
if (err.stack) args.push(pad(err.stack))
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ export async function createPluginContainer(
line: (err as any).line,
column: (err as any).column
}
err.frame = err.frame || generateCodeFrame(this._activeCode, err.loc)
} else if (err.loc) {
err.frame = err.frame || generateCodeFrame(this._activeCode, err.loc)
}
}
// error thrown here is caught by the transform middleware and passed on
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ export function posToNumber(
const lines = source.split(splitRE)
const { line, column } = pos
let start = 0
for (let i = 0; i < line; i++) {
for (let i = 0; i < line - 1; i++) {
start += lines[i].length
}
return start + column - 1
return start + column
}

export function numberToPos(
Expand Down

0 comments on commit 206b7b7

Please sign in to comment.