Skip to content

Commit

Permalink
types: add type for vite:invalidate payload
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Sep 28, 2022
1 parent a21886d commit f813817
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/client-types.d.ts
@@ -1,6 +1,7 @@
export type {
CustomEventMap,
InferCustomEventPayload
InferCustomEventPayload,
InvalidatePayload
} from './types/customEvent'
export type {
HMRPayload,
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/client/client.ts
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/types/customEvent.d.ts
Expand Up @@ -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<T extends string> =
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/types/customEvent.d.ts
@@ -1 +1,5 @@
export type { CustomEventMap, InferCustomEventPayload } from '../client/types'
export type {
CustomEventMap,
InferCustomEventPayload,
InvalidatePayload
} from '../client/types'
4 changes: 2 additions & 2 deletions playground/hmr/hmr.ts
Expand Up @@ -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 }) => {
Expand Down

0 comments on commit f813817

Please sign in to comment.