|
1 | 1 | import { promises as fsp } from 'fs'
|
2 |
| -import { relative, resolve, join, dirname } from 'pathe' |
| 2 | +import { relative, resolve, join, dirname, isAbsolute } from 'pathe' |
3 | 3 | import * as rollup from 'rollup'
|
4 | 4 | import fse from 'fs-extra'
|
5 | 5 | import { defu } from 'defu'
|
6 | 6 | import { watch } from 'chokidar'
|
7 | 7 | import { debounce } from 'perfect-debounce'
|
8 | 8 | import type { TSConfig } from 'pkg-types'
|
| 9 | +import type { RollupError } from 'rollup' |
| 10 | +import type { OnResolveResult, PartialMessage } from 'esbuild' |
9 | 11 | import { printFSTree } from './utils/tree'
|
10 | 12 | import { getRollupConfig, RollupConfig } from './rollup/config'
|
11 | 13 | import { prettyPath, writeFile, isDirectory } from './utils'
|
@@ -138,7 +140,7 @@ async function _build (nitro: Nitro, rollupConfig: RollupConfig) {
|
138 | 140 |
|
139 | 141 | nitro.logger.start('Building server...')
|
140 | 142 | const build = await rollup.rollup(rollupConfig).catch((error) => {
|
141 |
| - nitro.logger.error('Rollup error: ' + error.message) |
| 143 | + nitro.logger.error(formatRollupError(error)) |
142 | 144 | throw error
|
143 | 145 | })
|
144 | 146 |
|
@@ -204,7 +206,7 @@ function startRollupWatcher (nitro: Nitro, rollupConfig: RollupConfig) {
|
204 | 206 |
|
205 | 207 | // Encountered an error while bundling
|
206 | 208 | case 'ERROR':
|
207 |
| - nitro.logger.error('Rollup error: ', event.error) |
| 209 | + nitro.logger.error(formatRollupError(event.error)) |
208 | 210 | }
|
209 | 211 | })
|
210 | 212 | return watcher
|
@@ -240,3 +242,21 @@ async function _watch (nitro: Nitro, rollupConfig: RollupConfig) {
|
240 | 242 |
|
241 | 243 | await reload()
|
242 | 244 | }
|
| 245 | + |
| 246 | +function formatRollupError (_error: RollupError | OnResolveResult) { |
| 247 | + try { |
| 248 | + const logs: string[] = [] |
| 249 | + for (const error of ('errors' in _error ? _error.errors : [_error as RollupError])) { |
| 250 | + const id = error.id || (_error as RollupError).id |
| 251 | + let path = isAbsolute(id) ? relative(process.cwd(), id) : id |
| 252 | + const location = (error as RollupError).loc || (error as PartialMessage).location |
| 253 | + if (location) { |
| 254 | + path += `:${location.line}:${location.column}` |
| 255 | + } |
| 256 | + logs.push(`Rollup error while processing \`${path}\`` + '\n\n' + ((error as PartialMessage).text || (error as RollupError).frame)) |
| 257 | + } |
| 258 | + return logs.join('\n\n') |
| 259 | + } catch { |
| 260 | + return _error?.toString() |
| 261 | + } |
| 262 | +} |
0 commit comments