diff --git a/packages/vite/src/client-types.d.ts b/packages/vite/src/client-types.d.ts index 270a1ac29f2d8b..cca320bc5d4e1b 100644 --- a/packages/vite/src/client-types.d.ts +++ b/packages/vite/src/client-types.d.ts @@ -1,6 +1,7 @@ export type { CustomEventMap, - InferCustomEventPayload + InferCustomEventPayload, + InvalidatePayload } from './types/customEvent' export type { HMRPayload, diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index a07c854267926d..3f974e77d9b52b 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -548,8 +548,8 @@ export function createHotContext(ownerPath: string): ViteHotContext { // tell the server to re-perform hmr propagation from this module as root invalidate() { - notifyListeners('vite:invalidate', ownerPath) - this.send('vite:invalidate', ownerPath) + notifyListeners('vite:invalidate', { path: ownerPath }) + this.send('vite:invalidate', { path: ownerPath }) }, // custom events diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 40da1c410bbf1f..77b91a4dfd6dba 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -13,6 +13,7 @@ import launchEditorMiddleware from 'launch-editor-middleware' import type { SourceMap } from 'rollup' import picomatch from 'picomatch' import type { Matcher } from 'picomatch' +import type { InvalidatePayload } from 'types/customEvent' import type { CommonServerOptions } from '../http' import { httpServerStart, @@ -494,8 +495,8 @@ export async function createServer( handleFileAddUnlink(normalizePath(file), server) }) - ws.on('vite:invalidate', async (url: string) => { - const mod = moduleGraph.urlToModuleMap.get(url) + ws.on('vite:invalidate', async ({ path }: InvalidatePayload) => { + const mod = moduleGraph.urlToModuleMap.get(path) if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) { const file = getShortName(mod.file!, config.root) updateModules(file, [...mod.importers], mod.lastHMRTimestamp, server) diff --git a/packages/vite/src/types/customEvent.d.ts b/packages/vite/src/types/customEvent.d.ts index af4db5d14fbe97..839e17dd729eda 100644 --- a/packages/vite/src/types/customEvent.d.ts +++ b/packages/vite/src/types/customEvent.d.ts @@ -10,6 +10,11 @@ export interface CustomEventMap { 'vite:beforePrune': PrunePayload 'vite:beforeFullReload': FullReloadPayload 'vite:error': ErrorPayload + 'vite:invalidate': InvalidatePayload +} + +export interface InvalidatePayload { + path: string } export type InferCustomEventPayload = diff --git a/packages/vite/types/customEvent.d.ts b/packages/vite/types/customEvent.d.ts index 09fd7dc36ea481..d5bdbde98984fb 100644 --- a/packages/vite/types/customEvent.d.ts +++ b/packages/vite/types/customEvent.d.ts @@ -1 +1,5 @@ -export type { CustomEventMap, InferCustomEventPayload } from '../client/types' +export type { + CustomEventMap, + InferCustomEventPayload, + InvalidatePayload +} from '../client/types' diff --git a/playground/hmr/hmr.ts b/playground/hmr/hmr.ts index 269fea2ff0f2ab..473dff9fdbfb88 100644 --- a/playground/hmr/hmr.ts +++ b/playground/hmr/hmr.ts @@ -89,8 +89,8 @@ if (import.meta.hot) { console.log(`>>> vite:error -- ${event.type}`) }) - import.meta.hot.on('vite:invalidate', (event) => { - console.log(`>>> vite:invalidate -- ${event}`) + import.meta.hot.on('vite:invalidate', ({ path }) => { + console.log(`>>> vite:invalidate -- ${path}`) }) import.meta.hot.on('custom:foo', ({ msg }) => {