Skip to content

Commit

Permalink
feat: add logger.hasErrorLogged(error) method (vitejs#3957)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
aleclarson and antfu committed Sep 30, 2021
1 parent ff68671 commit 3992ca0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type { Plugin } from './plugin'
export type {
Logger,
LogOptions,
LogErrorOptions,
LogLevel,
LogType,
LoggerOptions
Expand Down
23 changes: 10 additions & 13 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ export interface Logger {
info(msg: string, options?: LogOptions): void
warn(msg: string, options?: LogOptions): void
warnOnce(msg: string, options?: LogOptions): void
error(
msg: string,
options: LogOptions & { error: Error | RollupError | null }
): void
error(msg: string, options?: LogErrorOptions): void
clearScreen(type: LogType): void
hasLogged(error: Error | RollupError): boolean
hasErrorLogged(error: Error | RollupError): boolean
hasWarned: boolean
}

Expand All @@ -26,6 +23,10 @@ export interface LogOptions {
timestamp?: boolean
}

export interface LogErrorOptions extends LogOptions {
error?: Error | RollupError | null
}

export const LogLevels: Record<LogLevel, number> = {
silent: 0,
error: 1,
Expand Down Expand Up @@ -59,19 +60,15 @@ export function createLogger(
return options.customLogger
}

const loggedErrors = new WeakMap<Error | RollupError, boolean>()
const loggedErrors = new WeakSet<Error | RollupError>()
const { prefix = '[vite]', allowClearScreen = true } = options
const thresh = LogLevels[level]
const clear =
allowClearScreen && process.stdout.isTTY && !process.env.CI
? clearScreen
: () => {}

function output(
type: LogType,
msg: string,
options: LogOptions & { error?: Error | RollupError | null } = {}
) {
function output(type: LogType, msg: string, options: LogErrorOptions = {}) {
if (thresh >= LogLevels[type]) {
const method = type === 'info' ? 'log' : type
const format = () => {
Expand All @@ -88,7 +85,7 @@ export function createLogger(
}
}
if (options.error) {
loggedErrors.set(options.error, true)
loggedErrors.add(options.error)
}
if (type === lastType && msg === lastMsg) {
sameCount++
Expand Down Expand Up @@ -132,7 +129,7 @@ export function createLogger(
clear()
}
},
hasLogged(error) {
hasErrorLogged(error) {
return loggedErrors.has(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function ssrLoadModule(
pendingImports.delete(url)

const { logger } = server.config
if (!logger.hasLogged(e)) {
if (!logger.hasErrorLogged(e)) {
try {
e.stack = ssrRewriteStacktrace(e, server.moduleGraph)
} catch {}
Expand Down

0 comments on commit 3992ca0

Please sign in to comment.