Skip to content

Commit

Permalink
fix: remove log clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Sep 4, 2024
1 parent d4b70a5 commit bb6a6c2
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 66 deletions.
8 changes: 2 additions & 6 deletions vike/node/plugin/plugins/importUserCode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { isVirtualFileIdPageConfigValuesAll } from '../../../shared/virtual-file
import { isVirtualFileIdImportUserCode } from '../../../shared/virtual-files/virtualFileImportUserCode.js'
import { vikeConfigDependencies, reloadVikeConfig, isVikeConfigFile } from './v1-design/getVikeConfig.js'
import pc from '@brillout/picocolors'
import { logConfigInfo, clearLogs } from '../../shared/loggerNotProd.js'
import { logConfigInfo } from '../../shared/loggerNotProd.js'
import { getModuleFilePathAbsolute } from '../../shared/getFilePath.js'

function importUserCode(): Plugin {
Expand Down Expand Up @@ -109,18 +109,14 @@ function handleHotUpdate(ctx: HmrContext, config: ResolvedConfig) {
`${msg} — ${pc.cyan('no HMR')}, see https://vike.dev/on-demand-compiler`,
'info',
null,
true,
clear,
config
true
)
return
}
//*/

// HMR can resolve errors => we clear previously shown errors.
// It can hide an error it shouldn't hide (because the error isn't shown again), but it's ok since users can reload the page and the error will be shown again (Vite transpilation errors are shown again upon a page reload).
if (!isVikeConfig && isViteModule) {
clearLogs({ clearErrors: true })
return
}

Expand Down
37 changes: 2 additions & 35 deletions vike/node/plugin/shared/loggerNotProd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export { logConfigInfo }
export { logConfigError }
export { logConfigErrorRecover }
export { logErrorDebugNote }
export { clearLogs }
export type { LogInfo }
export type { LogInfoArgs }
export type { LogError }
Expand All @@ -38,18 +37,9 @@ import {
getConfigExecutionErrorIntroMsg,
getConfigBuildErrorFormatted
} from '../plugins/importUserCode/v1-design/getVikeConfig/transpileAndExecuteFile.js'
import {
logWithVikeTag,
logWithViteTag,
logDirectly,
isFirstLog,
screenHasErrors,
clearScreen,
applyViteSourceMapToStackTrace
} from './loggerNotProd/log.js'
import { logWithVikeTag, logWithViteTag, logDirectly, applyViteSourceMapToStackTrace } from './loggerNotProd/log.js'
import pc from '@brillout/picocolors'
import { setAlreadyLogged } from '../../runtime/renderPage/isNewError.js'
import { isConfigInvalid } from '../../runtime/renderPage/isConfigInvalid.js'
import { onRuntimeError } from '../../runtime/renderPage/loggerProd.js'
import { isUserHookError } from '../../../shared/hooks/executeHook.js'

Expand All @@ -64,8 +54,7 @@ type LogInfoArgs = Parameters<typeof logRuntimeInfo>
type LogError = (...args: LogErrorArgs) => void
type LogErrorArgs = Parameters<typeof logRuntimeError>

function logRuntimeInfo(msg: string, httpRequestId: number, logType: LogType, clearErrors?: boolean) {
if (clearErrors) clearLogs({ clearErrors: true })
function logRuntimeInfo(msg: string, httpRequestId: number, logType: LogType) {
const category = getCategory(httpRequestId)
assert(category)
logWithVikeTag(msg, logType, category)
Expand All @@ -84,7 +73,6 @@ function logConfigInfo(msg: string, logType: LogType): void {
}
function logConfigErrorRecover(): void {
const msg = pc.bold(pc.green('Configuration successfully loaded'))
clearLogs({ clearAlsoIfConfigIsInvalid: true })
const category = getConfigCategory()
logWithVikeTag(msg, 'error-recover', category)
}
Expand Down Expand Up @@ -159,8 +147,6 @@ function logErr(err: unknown, httpRequestId: number | null = null, errorComesFro
}

function logConfigError(err: unknown): void {
clearLogs({ clearAlsoIfConfigIsInvalid: true })

warnIfErrorIsNotObject(err)

const category = getConfigCategory()
Expand Down Expand Up @@ -225,25 +211,6 @@ function assertLogger(thing: string | Error, logType: LogType): void {
logWithVikeTag(assertMsg, logType, category, showVikeVersion)
}

function clearLogs(
conditions: { clearErrors?: boolean; clearIfFirstLog?: boolean; clearAlsoIfConfigIsInvalid?: boolean } = {}
): void {
if (!conditions.clearAlsoIfConfigIsInvalid && isConfigInvalid) {
// Avoid hiding the config error: the config error is printed only once
return
}
if (conditions.clearErrors && !screenHasErrors) {
return
}
if (conditions.clearIfFirstLog && !isFirstLog) {
return
}
const viteConfig = getViteConfig()
if (viteConfig) {
clearScreen(viteConfig)
}
}

/** Note shown to user when vike does something risky:
* - When vike dedupes (i.e. swallows) an error with getHttpRequestAsyncStore().shouldErrorBeSwallowed(err)
* - When vike modifies the error with getPrettyErrorWithCodeSnippet(err)
Expand Down
16 changes: 0 additions & 16 deletions vike/node/plugin/shared/loggerNotProd/log.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
export { logWithViteTag }
export { logWithVikeTag }
export { logDirectly }
export { isFirstLog }
export { clearScreen }
export { screenHasErrors }
export { applyViteSourceMapToStackTrace }

import { assert, projectInfo, stripAnsi, hasProp, assertIsNotProductionRuntime } from '../../utils.js'
import pc from '@brillout/picocolors'
import { isErrorDebug } from '../../../shared/isErrorDebug.js'
import { getViteDevServer } from '../../../runtime/globalContext.js'
import type { LogCategory, LogType } from '../loggerNotProd.js'
import type { ResolvedConfig } from 'vite'

assertIsNotProductionRuntime()

type ProjectTag = `[vike]` | `[vike@${typeof projectInfo.projectVersion}]`

let isFirstLog = true
let screenHasErrors = false

function logWithVikeTag(msg: string, logType: LogType, category: LogCategory | null, showVikeVersion = false) {
const projectTag = getProjectTag(showVikeVersion)
msg = prependTags(msg, projectTag, category, logType)
Expand All @@ -45,8 +38,6 @@ function logWithViteTag(msg: string, logType: LogType, category: LogCategory | n
function logDirectly(thing: unknown, logType: LogType) {
applyViteSourceMapToStackTrace(thing)

isFirstLog = false

if (logType === 'info') {
console.log(thing)
return
Expand All @@ -56,7 +47,6 @@ function logDirectly(thing: unknown, logType: LogType) {
return
}
if (logType === 'error') {
screenHasErrors = true
console.error(thing)
return
}
Expand All @@ -69,12 +59,6 @@ function logDirectly(thing: unknown, logType: LogType) {
assert(false)
}

function clearScreen(viteConfig: ResolvedConfig) {
// We use Vite's logger in order to respect the user's `clearScreen: false` setting
viteConfig.logger.clearScreen('error')
screenHasErrors = false
}

function applyViteSourceMapToStackTrace(thing: unknown) {
if (isErrorDebug()) return
if (!hasProp(thing, 'stack')) return
Expand Down
4 changes: 1 addition & 3 deletions vike/node/plugin/shared/loggerVite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { improveViteLogs }

import { assert, removeEmptyLines, trimWithAnsi, trimWithAnsiTrailOnly } from '../utils.js'
import { logViteError, logViteAny, clearLogs } from './loggerNotProd.js'
import { logViteError, logViteAny } from './loggerNotProd.js'
import { getHttpRequestAsyncStore } from './getHttpRequestAsyncStore.js'
import { removeSuperfluousViteLog } from './loggerVite/removeSuperfluousViteLog.js'
import type { LogType, ResolvedConfig, LogErrorOptions } from 'vite'
Expand Down Expand Up @@ -43,8 +43,6 @@ function intercept(logType: LogType, config: ResolvedConfig) {
return
}

// Only allow Vite to clear for its first log. All other clearing is controlled by vike.
if (options.clear) clearLogs({ clearIfFirstLog: true })
// Vite's default logger preprends the "[vite]" tag if and only if options.timestamp is true
const prependViteTag = options.timestamp || !!store?.httpRequestId
logViteAny(msg, logType, store?.httpRequestId ?? null, prependViteTag)
Expand Down
8 changes: 2 additions & 6 deletions vike/node/runtime/renderPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ import { resolveRedirects } from './renderPage/resolveRedirects.js'
import { PageContextBuiltInServerInternal } from '../../shared/types.js'

const globalObject = getGlobalObject('runtime/renderPage.ts', {
httpRequestsCount: 0,
pendingRequestsCount: 0
httpRequestsCount: 0
})
let renderPage_wrapper = async <PageContext>(_httpRequestId: number, ret: () => Promise<PageContext>) => ({
pageContextReturn: await ret()
Expand Down Expand Up @@ -102,14 +101,12 @@ async function renderPage<
const httpRequestId = getRequestId()
const urlOriginalPretty = getUrlPretty(pageContextInit.urlOriginal)
logHttpRequest(urlOriginalPretty, httpRequestId)
globalObject.pendingRequestsCount++

const { pageContextReturn } = await renderPage_wrapper(httpRequestId, () =>
renderPageAndPrepare(pageContextInit, httpRequestId)
)

logHttpResponse(urlOriginalPretty, httpRequestId, pageContextReturn)
globalObject.pendingRequestsCount--

checkType<PageContextAfterRender>(pageContextReturn)
return pageContextReturn as any
Expand Down Expand Up @@ -315,8 +312,7 @@ async function renderPageAlreadyPrepared(
}

function logHttpRequest(urlOriginal: string, httpRequestId: number) {
const clearErrors = globalObject.pendingRequestsCount === 0
logRuntimeInfo?.(getRequestInfoMessage(urlOriginal), httpRequestId, 'info', clearErrors)
logRuntimeInfo?.(getRequestInfoMessage(urlOriginal), httpRequestId, 'info')
}
function getRequestInfoMessage(urlOriginal: string) {
return `HTTP request: ${prettyUrl(urlOriginal)}`
Expand Down

0 comments on commit bb6a6c2

Please sign in to comment.