UNPKG

ts-project-builder

Version:

Rollup-based TypeScript builder with multi-format output and built-in common plugins.

42 lines (39 loc) 1.67 kB
import { bold, red, cyan, dim } from './colors.mjs'; import { relativeId } from './relativeId.mjs'; // log to stderr to keep `rollup main.js > bundle.js` from breaking const stderr = (...parameters) => process.stderr.write(`${parameters.join('')}\n`); function handleError(error) { const name = error.name || error.cause?.name; const nameSection = name ? `${name}: ` : ''; const pluginSection = error.plugin ? `(plugin ${error.plugin}) ` : ''; const message = `${pluginSection}${nameSection}${error.message}`; const outputLines = [bold(red(`[!] ${bold(message.toString())}`))]; if (error.url) outputLines.push(cyan(error.url)); if (error.loc) { outputLines.push(`${relativeId((error.loc.file || error.id))}:${error.loc.line}:${error.loc.column}`); } else if (error.id) outputLines.push(relativeId(error.id)); if (error.frame) outputLines.push(dim(error.frame)); if (error.stack) outputLines.push(dim(error.stack?.replace(`${nameSection}${error.message}\n`, ''))); // ES2022: Error.prototype.cause is optional if (error.cause) { let cause = error.cause; const causeErrorLines = []; let indent = ''; while (cause) { indent += ' '; const message = cause.stack || cause; causeErrorLines.push(...`[cause] ${message}`.split('\n').map((line) => `${indent}${line}`)); cause = cause.cause; } outputLines.push(dim(causeErrorLines.join('\n'))); } outputLines.push('', ''); stderr(outputLines.join('\n')); } export { handleError, stderr }; //# sourceMappingURL=logging.mjs.map