(
console: VitestLogger,
level: LogLevel = 'info',
options: LoggerOptions = {},
)
| 36 | // When Vitest supports only Vite 6 and above, we can use Vite's `createLogger({ console })` |
| 37 | // https://github.com/vitejs/vite/pull/18379 |
| 38 | export function createViteLogger( |
| 39 | console: VitestLogger, |
| 40 | level: LogLevel = 'info', |
| 41 | options: LoggerOptions = {}, |
| 42 | ): Logger { |
| 43 | const loggedErrors = new WeakSet<Error | Rollup.RollupError>() |
| 44 | const { prefix = '[vite]', allowClearScreen = true } = options |
| 45 | const thresh = LogLevels[level] |
| 46 | const canClearScreen |
| 47 | = allowClearScreen && process.stdout.isTTY && !process.env.CI |
| 48 | const clear = canClearScreen ? clearScreen : () => {} |
| 49 | |
| 50 | function format(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 51 | if (options.timestamp) { |
| 52 | let tag = '' |
| 53 | if (type === 'info') { |
| 54 | tag = colors.cyan(colors.bold(prefix)) |
| 55 | } |
| 56 | else if (type === 'warn') { |
| 57 | tag = colors.yellow(colors.bold(prefix)) |
| 58 | } |
| 59 | else { |
| 60 | tag = colors.red(colors.bold(prefix)) |
| 61 | } |
| 62 | const environment = (options as any).environment ? `${(options as any).environment} ` : '' |
| 63 | return `${colors.dim(getTimeFormatter().format(new Date()))} ${tag} ${environment}${msg}` |
| 64 | } |
| 65 | else { |
| 66 | return msg |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function output(type: LogType, msg: string, options: LogErrorOptions = {}) { |
| 71 | if (thresh >= LogLevels[type]) { |
| 72 | const method = type === 'info' ? 'log' : type |
| 73 | |
| 74 | if (options.error) { |
| 75 | loggedErrors.add(options.error) |
| 76 | } |
| 77 | if (canClearScreen) { |
| 78 | if (type === lastType && msg === lastMsg) { |
| 79 | sameCount++ |
| 80 | clear(console) |
| 81 | console[method]( |
| 82 | format(type, msg, options), |
| 83 | colors.yellow(`(x${sameCount + 1})`), |
| 84 | ) |
| 85 | } |
| 86 | else { |
| 87 | sameCount = 0 |
| 88 | lastMsg = msg |
| 89 | lastType = type |
| 90 | if (options.clear) { |
| 91 | clear(console) |
| 92 | } |
| 93 | console[method](format(type, msg, options)) |
| 94 | } |
| 95 | } |
no outgoing calls
no test coverage detected