Skip to content

Commit

Permalink
feat(html): clickable error position for html parse error (#11334)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 12, 2022
1 parent 729fb1a commit 2e15f3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
34 changes: 13 additions & 21 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,17 @@ export function overwriteAttrValue(
/**
* Format parse5 @type {ParserError} to @type {RollupError}
*/
function formatParseError(
parserError: ParserError,
id: string,
html: string,
): RollupError {
const formattedError: RollupError = {
function formatParseError(parserError: ParserError, id: string, html: string) {
const formattedError = {
code: parserError.code,
message: `parse5 error code ${parserError.code}`,
}
formattedError.frame = generateCodeFrame(html, parserError.startOffset)
formattedError.loc = {
file: id,
line: parserError.startLine,
column: parserError.startCol,
}
frame: generateCodeFrame(html, parserError.startOffset),
loc: {
file: id,
line: parserError.startLine,
column: parserError.startCol,
},
} satisfies RollupError
return formattedError
}

Expand All @@ -266,15 +262,11 @@ function handleParseError(
// Allow self closing on non-void elements #10439
return
}
const parseError = {
loc: filePath,
frame: '',
...formatParseError(parserError, filePath, html),
}
const parseError = formatParseError(parserError, filePath, html)
throw new Error(
`Unable to parse HTML; ${parseError.message}\n at ${JSON.stringify(
parseError.loc,
)}\n${parseError.frame}`,
`Unable to parse HTML; ${parseError.message}\n` +
` at ${parseError.loc.file}:${parseError.loc.line}:${parseError.loc.column}\n` +
`${parseError.frame}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
)
}

await traverseHtml(html, htmlPath, (node) => {
await traverseHtml(html, filename, (node) => {
if (!nodeIsElement(node)) {
return
}
Expand Down

0 comments on commit 2e15f3d

Please sign in to comment.