Skip to content

fix: add code location and codeframe for rollup errors #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 31, 2022
18 changes: 14 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { promises as fsp } from 'fs'
import { relative, resolve, join, dirname } from 'pathe'
import { relative, resolve, join, dirname, isAbsolute } from 'pathe'
import * as rollup from 'rollup'
import fse from 'fs-extra'
import { defu } from 'defu'
Expand Down Expand Up @@ -137,9 +137,19 @@ async function _build (nitro: Nitro, rollupConfig: RollupConfig) {
await _snapshot(nitro)

nitro.logger.start('Building server...')
const build = await rollup.rollup(rollupConfig).catch((error) => {
nitro.logger.error('Rollup error: ' + error.message)
throw error
const build = await rollup.rollup(rollupConfig).catch((_error) => {
try {
for (const error of ('errors' in _error ? _error.errors : [_error])) {
const id = error.id || _error.id
let path = isAbsolute(id) ? relative(process.cwd(), id) : id
const location = error.loc || error.location
if (location) {
path += `:${location.line}:${location.column}`
}
nitro.logger.error(`Rollup error while processing \`${path}\`` + '\n' + '\n' + (error.text || error.frame))
}
} catch {}
throw _error
})

nitro.logger.start('Writing server bundle...')
Expand Down