Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: proxy environment logger, remove hardcoded [vite] from hmr logger #16407

Merged
merged 6 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ const debounceReload = (time: number) => {
const pageReload = debounceReload(50)

const hmrClient = new HMRClient(
console,
{
error: (err) => console.error('[vite]', err),
debug: (...msg) => console.debug('[vite]', ...msg),
},
{
isReady: () => socket && socket.readyState === 1,
send: (message) => socket.send(message),
Expand Down Expand Up @@ -172,7 +175,7 @@ const hmrClient = new HMRClient(
async function handleMessage(payload: HMRPayload) {
switch (payload.type) {
case 'connected':
console.debug(`[vite] connected.`)
console.debug(`connected.`)
hmrClient.messenger.flush()
// proxy(nginx, docker) hmr ws maybe caused timeout,
// so send ping package let ws keep alive.
Expand Down
10 changes: 4 additions & 6 deletions packages/vite/src/module-runner/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function handleHMRPayload(
if (!hmrClient || runner.isDestroyed()) return
switch (payload.type) {
case 'connected':
hmrClient.logger.debug(`[vite] connected.`)
hmrClient.logger.debug(`connected.`)
hmrClient.messenger.flush()
break
case 'update':
Expand All @@ -32,9 +32,7 @@ export async function handleHMRPayload(
return hmrClient.queueUpdate(update)
}

hmrClient.logger.error(
'[vite] css hmr is not supported in runner mode.',
)
hmrClient.logger.error('css hmr is not supported in runner mode.')
}),
)
await hmrClient.notifyListeners('vite:afterUpdate', payload)
Expand All @@ -54,7 +52,7 @@ export async function handleHMRPayload(

if (!clearEntrypoints.size) break

hmrClient.logger.debug(`[vite] program reload`)
hmrClient.logger.debug(`program reload`)
await hmrClient.notifyListeners('vite:beforeFullReload', payload)
runner.moduleCache.clear()

Expand All @@ -71,7 +69,7 @@ export async function handleHMRPayload(
await hmrClient.notifyListeners('vite:error', payload)
const err = payload.err
hmrClient.logger.error(
`[vite] Internal Server Error\n${err.message}\n${err.stack}`,
`Internal Server Error\n${err.message}\n${err.stack}`,
)
break
}
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/module-runner/hmrLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ export const silentConsole: HMRLogger = {
debug: noop,
error: noop,
}

export const hmrLogger: HMRLogger = {
debug: (...msg) => console.log('[vite]', ...msg),
error: (error) => console.log('[vite]', error),
}
6 changes: 3 additions & 3 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
ssrImportMetaKey,
ssrModuleExportsKey,
} from './constants'
import { silentConsole } from './hmrLogger'
import { hmrLogger, silentConsole } from './hmrLogger'
import { createHMRHandler } from './hmrHandler'
import { enableSourceMapSupport } from './sourcemap/index'
import type { RunnerTransport } from './runnerTransport'
Expand Down Expand Up @@ -78,7 +78,7 @@ export class ModuleRunner {
this.hmrClient = new HMRClient(
options.hmr.logger === false
? silentConsole
: options.hmr.logger || console,
: options.hmr.logger || hmrLogger,
options.hmr.connection,
({ acceptedPath, invalidates }) => {
this.moduleCache.invalidate(acceptedPath)
Expand Down Expand Up @@ -245,7 +245,7 @@ export class ModuleRunner {
importer?: string,
): Promise<ResolvedResult> {
if (this._destroyed) {
throw new Error(`[vite] Vite runtime has been destroyed.`)
throw new Error(`Vite module runner has been destroyed.`)
}
const normalized = this.idToUrlMap.get(id)
if (normalized) {
Expand Down
33 changes: 32 additions & 1 deletion packages/vite/src/node/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,39 @@ export class Environment {
name: string
config: ResolvedConfig
options: ResolvedEnvironmentOptions
#logger: Logger | undefined
get logger(): Logger {
return this.config.logger
if (this.#logger) {
return this.#logger
}
const logger = this.config.logger
const format = (msg: string) => {
return `(${this.name}) ${msg}`
}
this.#logger = {
get hasWarned() {
return logger.hasWarned
},
info(msg, opts) {
return logger.info(format(msg), opts)
},
warn(msg, opts) {
return logger.warn(format(msg), opts)
},
warnOnce(msg, opts) {
return logger.warnOnce(format(msg), opts)
},
error(msg, opts) {
return logger.error(format(msg), opts)
},
clearScreen(type) {
return logger.clearScreen(type)
},
hasErrorLogged(error) {
return logger.hasErrorLogged(error)
},
}
return this.#logger
}
constructor(
name: string,
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/shared/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class HMRContext implements ViteHotContext {
message,
})
this.hmrClient.logger.debug(
`[vite] invalidate ${this.ownerPath}${message ? `: ${message}` : ''}`,
`invalidate ${this.ownerPath}${message ? `: ${message}` : ''}`,
)
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export class HMRClient {
this.logger.error(err)
}
this.logger.error(
`[hmr] Failed to reload ${path}. ` +
`Failed to reload ${path}. ` +
`This could be due to syntax errors or importing non-existent ` +
`modules. (see errors above)`,
)
Expand Down Expand Up @@ -316,7 +316,7 @@ export class HMRClient {
)
}
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`
this.logger.debug(`[vite] hot updated: ${loggedPath}`)
this.logger.debug(`hot updated: ${loggedPath}`)
}
}
}
7 changes: 4 additions & 3 deletions playground/hmr-ssr/__tests__/hmr-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,12 +1158,13 @@ async function setupModuleRunner(

class HMRMockLogger {
debug(...msg: unknown[]) {
const log = msg.join(' ')
const log = ['[vite]', ...msg].join(' ')
clientLogs.push(log)
logsEmitter.emit('log', log)
}
error(msg: string) {
clientLogs.push(msg)
logsEmitter.emit('log', msg)
const log = ['[vite]', msg].join(' ')
clientLogs.push(log)
logsEmitter.emit('log', log)
}
}
Loading