Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare const __HMR_DIRECT_TARGET__: string
declare const __HMR_BASE__: string
declare const __HMR_TIMEOUT__: number
declare const __HMR_ENABLE_OVERLAY__: boolean
declare const __HMR_RUNTIME_ERRORS__: boolean | ((err: ErrorEvent) => unknown)
declare const __WS_TOKEN__: string

console.debug('[vite] connecting...')
Expand All @@ -37,6 +38,7 @@ const directSocketHost = __HMR_DIRECT_TARGET__
const base = __BASE__ || '/'
const hmrTimeout = __HMR_TIMEOUT__
const wsToken = __WS_TOKEN__
const runtimeErrors = __HMR_RUNTIME_ERRORS__

const transport = normalizeModuleRunnerTransport(
(() => {
Expand Down Expand Up @@ -109,6 +111,19 @@ if (typeof window !== 'undefined') {
window.addEventListener?.('beforeunload', () => {
willUnload = true
})

if (runtimeErrors) {
if (typeof runtimeErrors === 'function') {
window.addEventListener('error', runtimeErrors)
} else {
window.addEventListener('error', (err: ErrorEvent) => {
const { error, message } = err
const errorObject =
error instanceof Error ? error : new Error(error || message)
createErrorOverlay(errorObject)
})
}
}
}

function cleanUrl(pathname: string): string {
Expand Down Expand Up @@ -306,7 +321,7 @@ async function handleMessage(payload: HotPayload) {
const enableOverlay = __HMR_ENABLE_OVERLAY__
const hasDocument = 'document' in globalThis

function createErrorOverlay(err: ErrorPayload['err']) {
function createErrorOverlay(err: ErrorPayload['err'] | Error) {
clearErrorOverlay()
const { customElements } = globalThis
if (customElements) {
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
const overlay = hmrConfig?.overlay !== false
const isHmrServerSpecified = !!hmrConfig?.server
const hmrConfigName = path.basename(config.configFile || 'vite.config.js')
const runtimeErrors = hmrConfig?.runtimeErrors ?? true

// hmr.clientPort -> hmr.port
// -> (24678 if middleware mode and HMR server is not specified) -> new URL(import.meta.url).port
Expand Down Expand Up @@ -77,6 +78,10 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
const hmrEnableOverlayReplacement = escapeReplacement(overlay)
const hmrConfigNameReplacement = escapeReplacement(hmrConfigName)
const wsTokenReplacement = escapeReplacement(config.webSocketToken)
const hmrRuntimeErrorsReplacement =
typeof runtimeErrors === 'function'
? () => runtimeErrors.toString()
: escapeReplacement(runtimeErrors)

injectConfigValues = (code: string) => {
return code
Expand All @@ -92,6 +97,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
.replace(`__HMR_ENABLE_OVERLAY__`, hmrEnableOverlayReplacement)
.replace(`__HMR_CONFIG_NAME__`, hmrConfigNameReplacement)
.replace(`__WS_TOKEN__`, wsTokenReplacement)
.replace(`__HMR_RUNTIME_ERRORS__`, hmrRuntimeErrorsReplacement)
}
},
async transform(code, id) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface HmrOptions {
timeout?: number
overlay?: boolean
server?: HttpServer
runtimeErrors?: boolean | ((err: ErrorEvent) => unknown)
}

export interface HotUpdateOptions {
Expand Down
Loading