|
| 1 | +import type { LogErrorOptions, LogLevel, LogType, Logger, LoggerOptions } from 'vite' |
| 2 | +import type { RollupError } from 'rollup' |
| 3 | +import colors from 'tinyrainbow' |
| 4 | +import type { Logger as VitestLogger } from './logger' |
| 5 | + |
| 6 | +const LogLevels: Record<LogLevel, number> = { |
| 7 | + silent: 0, |
| 8 | + error: 1, |
| 9 | + warn: 2, |
| 10 | + info: 3, |
| 11 | +} |
| 12 | + |
| 13 | +function clearScreen(logger: VitestLogger) { |
| 14 | + const repeatCount = process.stdout.rows - 2 |
| 15 | + const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : '' |
| 16 | + logger.clearScreen(blank) |
| 17 | +} |
| 18 | + |
| 19 | +let lastType: LogType | undefined |
| 20 | +let lastMsg: string | undefined |
| 21 | +let sameCount = 0 |
| 22 | + |
| 23 | +// Only initialize the timeFormatter when the timestamp option is used, and |
| 24 | +// reuse it across all loggers |
| 25 | +let timeFormatter: Intl.DateTimeFormat |
| 26 | +function getTimeFormatter() { |
| 27 | + timeFormatter ??= new Intl.DateTimeFormat(undefined, { |
| 28 | + hour: 'numeric', |
| 29 | + minute: 'numeric', |
| 30 | + second: 'numeric', |
| 31 | + }) |
| 32 | + return timeFormatter |
| 33 | +} |
| 34 | + |
| 35 | +// This is copy-pasted and needs to be synced from time to time. Ideally, Vite's `createLogger` should accept a custom `console` |
| 36 | +// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/logger.ts?rgh-link-date=2024-10-16T23%3A29%3A19Z |
| 37 | +// When Vitest supports only Vite 6 and above, we can use Vite's `createLogger({ console })` |
| 38 | +// https://github.com/vitejs/vite/pull/18379 |
| 39 | +export function createViteLogger( |
| 40 | + console: VitestLogger, |
| 41 | + level: LogLevel = 'info', |
| 42 | + options: LoggerOptions = {}, |
| 43 | +): Logger { |
| 44 | + const loggedErrors = new WeakSet<Error | RollupError>() |
| 45 | + const { prefix = '[vite]', allowClearScreen = true } = options |
| 46 | + const thresh = LogLevels[level] |
| 47 | + const canClearScreen |
| 48 | + = allowClearScreen && process.stdout.isTTY && !process.env.CI |
| 49 | + const clear = canClearScreen ? clearScreen : () => {} |
| 50 | + |
| 51 | + function format(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 52 | + if (options.timestamp) { |
| 53 | + let tag = '' |
| 54 | + if (type === 'info') { |
| 55 | + tag = colors.cyan(colors.bold(prefix)) |
| 56 | + } |
| 57 | + else if (type === 'warn') { |
| 58 | + tag = colors.yellow(colors.bold(prefix)) |
| 59 | + } |
| 60 | + else { |
| 61 | + tag = colors.red(colors.bold(prefix)) |
| 62 | + } |
| 63 | + const environment = (options as any).environment ? `${(options as any).environment} ` : '' |
| 64 | + return `${colors.dim(getTimeFormatter().format(new Date()))} ${tag} ${environment}${msg}` |
| 65 | + } |
| 66 | + else { |
| 67 | + return msg |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function output(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 72 | + if (thresh >= LogLevels[type]) { |
| 73 | + const method = type === 'info' ? 'log' : type |
| 74 | + |
| 75 | + if (options.error) { |
| 76 | + loggedErrors.add(options.error) |
| 77 | + } |
| 78 | + if (canClearScreen) { |
| 79 | + if (type === lastType && msg === lastMsg) { |
| 80 | + sameCount++ |
| 81 | + clear(console) |
| 82 | + console[method]( |
| 83 | + format(type, msg, options), |
| 84 | + colors.yellow(`(x${sameCount + 1})`), |
| 85 | + ) |
| 86 | + } |
| 87 | + else { |
| 88 | + sameCount = 0 |
| 89 | + lastMsg = msg |
| 90 | + lastType = type |
| 91 | + if (options.clear) { |
| 92 | + clear(console) |
| 93 | + } |
| 94 | + console[method](format(type, msg, options)) |
| 95 | + } |
| 96 | + } |
| 97 | + else { |
| 98 | + console[method](format(type, msg, options)) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const warnedMessages = new Set<string>() |
| 104 | + |
| 105 | + const logger: Logger = { |
| 106 | + hasWarned: false, |
| 107 | + info(msg, opts) { |
| 108 | + output('info', msg, opts) |
| 109 | + }, |
| 110 | + warn(msg, opts) { |
| 111 | + logger.hasWarned = true |
| 112 | + output('warn', msg, opts) |
| 113 | + }, |
| 114 | + warnOnce(msg, opts) { |
| 115 | + if (warnedMessages.has(msg)) { |
| 116 | + return |
| 117 | + } |
| 118 | + logger.hasWarned = true |
| 119 | + output('warn', msg, opts) |
| 120 | + warnedMessages.add(msg) |
| 121 | + }, |
| 122 | + error(msg, opts) { |
| 123 | + logger.hasWarned = true |
| 124 | + output('error', msg, opts) |
| 125 | + }, |
| 126 | + clearScreen(type) { |
| 127 | + if (thresh >= LogLevels[type]) { |
| 128 | + clear(console) |
| 129 | + } |
| 130 | + }, |
| 131 | + hasErrorLogged(error) { |
| 132 | + return loggedErrors.has(error) |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + return logger |
| 137 | +} |
0 commit comments