|
| 1 | +import { RollupError } from 'rollup'; |
| 2 | +import { Warning } from './options'; |
| 3 | +import { buildExtendedLogMessage } from './log'; |
| 4 | +import { PartialMessage } from 'esbuild'; |
| 5 | + |
| 6 | +/** |
| 7 | + * convert an error thrown by svelte.compile to a RollupError so that vite displays it in a user friendly way |
| 8 | + * @param error a svelte compiler error, which is a mix of Warning and an error |
| 9 | + * @returns {RollupError} the converted error |
| 10 | + */ |
| 11 | +export function toRollupError(error: Warning & Error): RollupError { |
| 12 | + const { filename, frame, start, code, name } = error; |
| 13 | + const rollupError: RollupError = { |
| 14 | + name, // needed otherwise sveltekit coalesce_to_error turns it into a string |
| 15 | + id: filename, |
| 16 | + message: buildExtendedLogMessage(error), // include filename:line:column so that it's clickable |
| 17 | + frame: formatFrameForVite(frame), |
| 18 | + code, |
| 19 | + stack: '' |
| 20 | + }; |
| 21 | + if (start) { |
| 22 | + rollupError.loc = { |
| 23 | + line: start.line, |
| 24 | + column: start.column, |
| 25 | + file: filename |
| 26 | + }; |
| 27 | + } |
| 28 | + return rollupError; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * convert an error thrown by svelte.compile to an esbuild PartialMessage |
| 33 | + * @param error a svelte compiler error, which is a mix of Warning and an error |
| 34 | + * @returns {PartialMessage} the converted error |
| 35 | + */ |
| 36 | +export function toESBuildError(error: Warning & Error): PartialMessage { |
| 37 | + const { filename, frame, start } = error; |
| 38 | + const partialMessage: PartialMessage = { |
| 39 | + text: buildExtendedLogMessage(error) |
| 40 | + }; |
| 41 | + if (start) { |
| 42 | + partialMessage.location = { |
| 43 | + line: start.line, |
| 44 | + column: start.column, |
| 45 | + file: filename, |
| 46 | + lineText: lineFromFrame(start.line, frame) // needed to get a meaningful error message on cli |
| 47 | + }; |
| 48 | + } |
| 49 | + return partialMessage; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * extract line with number from codeframe |
| 54 | + */ |
| 55 | +function lineFromFrame(lineNo: number, frame?: string): string { |
| 56 | + if (!frame) { |
| 57 | + return ''; |
| 58 | + } |
| 59 | + const lines = frame.split('\n'); |
| 60 | + const errorLine = lines.find((line) => line.trimStart().startsWith(`${lineNo}: `)); |
| 61 | + return errorLine ? errorLine.substring(errorLine.indexOf(': ') + 3) : ''; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * vite error overlay expects a specific format to show frames |
| 66 | + * this reformats svelte frame (colon separated, less whitespace) |
| 67 | + * to one that vite displays on overlay ( pipe separated, more whitespace) |
| 68 | + * e.g. |
| 69 | + * ``` |
| 70 | + * 1: foo |
| 71 | + * 2: bar; |
| 72 | + * ^ |
| 73 | + * 3: baz |
| 74 | + * ``` |
| 75 | + * to |
| 76 | + * ``` |
| 77 | + * 1 | foo |
| 78 | + * 2 | bar; |
| 79 | + * ^ |
| 80 | + * 3 | baz |
| 81 | + * ``` |
| 82 | + * @see https://github.com/vitejs/vite/blob/96591bf9989529de839ba89958755eafe4c445ae/packages/vite/src/client/overlay.ts#L116 |
| 83 | + */ |
| 84 | +function formatFrameForVite(frame?: string): string { |
| 85 | + if (!frame) { |
| 86 | + return ''; |
| 87 | + } |
| 88 | + return frame |
| 89 | + .split('\n') |
| 90 | + .map((line) => (line.match(/^\s+\^/) ? ' ' + line : ' ' + line.replace(':', ' | '))) |
| 91 | + .join('\n'); |
| 92 | +} |
0 commit comments