Skip to content

Commit 2e15f3d

Browse files
authoredDec 12, 2022
feat(html): clickable error position for html parse error (#11334)
1 parent 729fb1a commit 2e15f3d

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed
 

‎packages/vite/src/node/plugins/html.ts

+13-21
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,17 @@ export function overwriteAttrValue(
228228
/**
229229
* Format parse5 @type {ParserError} to @type {RollupError}
230230
*/
231-
function formatParseError(
232-
parserError: ParserError,
233-
id: string,
234-
html: string,
235-
): RollupError {
236-
const formattedError: RollupError = {
231+
function formatParseError(parserError: ParserError, id: string, html: string) {
232+
const formattedError = {
237233
code: parserError.code,
238234
message: `parse5 error code ${parserError.code}`,
239-
}
240-
formattedError.frame = generateCodeFrame(html, parserError.startOffset)
241-
formattedError.loc = {
242-
file: id,
243-
line: parserError.startLine,
244-
column: parserError.startCol,
245-
}
235+
frame: generateCodeFrame(html, parserError.startOffset),
236+
loc: {
237+
file: id,
238+
line: parserError.startLine,
239+
column: parserError.startCol,
240+
},
241+
} satisfies RollupError
246242
return formattedError
247243
}
248244

@@ -266,15 +262,11 @@ function handleParseError(
266262
// Allow self closing on non-void elements #10439
267263
return
268264
}
269-
const parseError = {
270-
loc: filePath,
271-
frame: '',
272-
...formatParseError(parserError, filePath, html),
273-
}
265+
const parseError = formatParseError(parserError, filePath, html)
274266
throw new Error(
275-
`Unable to parse HTML; ${parseError.message}\n at ${JSON.stringify(
276-
parseError.loc,
277-
)}\n${parseError.frame}`,
267+
`Unable to parse HTML; ${parseError.message}\n` +
268+
` at ${parseError.loc.file}:${parseError.loc.line}:${parseError.loc.column}\n` +
269+
`${parseError.frame}`,
278270
)
279271
}
280272

‎packages/vite/src/node/server/middlewares/indexHtml.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
193193
)
194194
}
195195

196-
await traverseHtml(html, htmlPath, (node) => {
196+
await traverseHtml(html, filename, (node) => {
197197
if (!nodeIsElement(node)) {
198198
return
199199
}

0 commit comments

Comments
 (0)