Skip to content

Commit

Permalink
fix: better error message for parse failures (#5192)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Oct 5, 2021
1 parent 916f9d3 commit 8fe8df3
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,22 @@ export async function ssrTransform(
): Promise<TransformResult | null> {
const s = new MagicString(code)

const ast = parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true
}) as any
let ast: any
try {
ast = parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true
})
} catch (err) {
if (!err.loc || !err.loc.line) throw err
const line = err.loc.line
throw new Error(
`Parse failure: ${err.message}\nContents of line ${line}: ${
code.split('\n')[line - 1]
}`
)
}

let uid = 0
const deps = new Set<string>()
Expand Down

0 comments on commit 8fe8df3

Please sign in to comment.