|
2 | 2 |
|
3 | 3 | import fs from 'node:fs' |
4 | 4 | import path from 'node:path' |
5 | | -import type { DevEnvironment, Rollup } from 'vite' |
| 5 | +import type { DevEnvironment, ErrorPayload, Rollup } from 'vite' |
| 6 | +import { stripVTControlCharacters as strip } from 'node:util' |
6 | 7 |
|
7 | 8 | export const VALID_ID_PREFIX = `/@id/` |
8 | 9 |
|
@@ -125,3 +126,29 @@ export function normalizeViteImportAnalysisUrl( |
125 | 126 |
|
126 | 127 | return url |
127 | 128 | } |
| 129 | + |
| 130 | +// error formatting |
| 131 | +// https://github.com/vitejs/vite/blob/8033e5bf8d3ff43995d0620490ed8739c59171dd/packages/vite/src/node/server/middlewares/error.ts#L11 |
| 132 | + |
| 133 | +type RollupError = Rollup.RollupError |
| 134 | + |
| 135 | +export function prepareError(err: Error | RollupError): ErrorPayload['err'] { |
| 136 | + // only copy the information we need and avoid serializing unnecessary |
| 137 | + // properties, since some errors may attach full objects (e.g. PostCSS) |
| 138 | + return { |
| 139 | + message: strip(err.message), |
| 140 | + stack: strip(cleanStack(err.stack || '')), |
| 141 | + id: (err as RollupError).id, |
| 142 | + frame: strip((err as RollupError).frame || ''), |
| 143 | + plugin: (err as RollupError).plugin, |
| 144 | + pluginCode: (err as RollupError).pluginCode?.toString(), |
| 145 | + loc: (err as RollupError).loc, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function cleanStack(stack: string) { |
| 150 | + return stack |
| 151 | + .split(/\n/) |
| 152 | + .filter((l) => /^\s*at/.test(l)) |
| 153 | + .join('\n') |
| 154 | +} |
0 commit comments