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(types): share hot context type #7475

Merged
merged 1 commit into from Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/vite/src/client/client.ts
Expand Up @@ -6,6 +6,7 @@ import type {
Update,
UpdatePayload
} from 'types/hmrPayload'
import type { ViteHotContext } from 'types/hot'
import type { CustomEventName } from 'types/customEvent'
import { ErrorOverlay, overlayId } from './overlay'
// eslint-disable-next-line node/no-missing-import
Expand Down Expand Up @@ -391,9 +392,7 @@ const ctxToListenersMap = new Map<
Map<string, ((data: any) => void)[]>
>()

// Just infer the return type for now
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const createHotContext = (ownerPath: string) => {
export function createHotContext(ownerPath: string): ViteHotContext {
if (!dataMap.has(ownerPath)) {
dataMap.set(ownerPath, {})
}
Expand Down Expand Up @@ -434,12 +433,12 @@ export const createHotContext = (ownerPath: string) => {
hotModulesMap.set(ownerPath, mod)
}

const hot = {
const hot: ViteHotContext = {
get data() {
return dataMap.get(ownerPath)
},

accept(deps: any, callback?: any) {
accept(deps?: any, callback?: any) {
if (typeof deps === 'function' || !deps) {
// self-accept: hot.accept(() => {})
acceptDeps([ownerPath], ([mod]) => deps && deps(mod))
Expand All @@ -460,10 +459,11 @@ export const createHotContext = (ownerPath: string) => {
)
},

dispose(cb: (data: any) => void) {
dispose(cb) {
disposeMap.set(ownerPath, cb)
},

// @ts-expect-error untyped
prune(cb: (data: any) => void) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not typed in import.meta.hot - IIRC Svelte is using it - Should we add the type for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it in another PR

pruneMap.set(ownerPath, cb)
},
Expand All @@ -479,7 +479,7 @@ export const createHotContext = (ownerPath: string) => {
},

// custom events
on: (event: string, cb: (data: any) => void) => {
on(event, cb) {
const addToMap = (map: Map<string, any[]>) => {
const existing = map.get(event) || []
existing.push(cb)
Expand All @@ -489,7 +489,7 @@ export const createHotContext = (ownerPath: string) => {
addToMap(newListeners)
},

send: (event: string, data?: any) => {
send(event, data) {
messageBuffer.push(JSON.stringify({ type: 'custom', event, data }))
sendMessageBuffer()
}
Expand Down
43 changes: 43 additions & 0 deletions packages/vite/types/hot.d.ts
@@ -0,0 +1,43 @@
import type {
ErrorPayload,
FullReloadPayload,
PrunePayload,
UpdatePayload
} from './hmrPayload'

export interface ViteHotContext {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

/**
* @deprecated
*/
acceptDeps(): never

dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on: {
(event: 'vite:beforeUpdate', cb: (payload: UpdatePayload) => void): void
(event: 'vite:beforePrune', cb: (payload: PrunePayload) => void): void
(
event: 'vite:beforeFullReload',
cb: (payload: FullReloadPayload) => void
): void
(event: 'vite:error', cb: (payload: ErrorPayload) => void): void
(event: string, cb: (data: any) => void): void
}

send(event: string, data?: any): void
}

// See https://stackoverflow.com/a/63549561.
export type CustomEventName<T extends string> = (T extends `vite:${T}`
? never
: T) &
(`vite:${T}` extends T ? never : T)
43 changes: 1 addition & 42 deletions packages/vite/types/importMeta.d.ts
Expand Up @@ -20,48 +20,7 @@ interface GlobOptions {
interface ImportMeta {
url: string

readonly hot?: {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

/**
* @deprecated
*/
acceptDeps(): never

dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on: {
(
event: 'vite:beforeUpdate',
cb: (payload: import('./hmrPayload').UpdatePayload) => void
): void
(
event: 'vite:beforePrune',
cb: (payload: import('./hmrPayload').PrunePayload) => void
): void
(
event: 'vite:beforeFullReload',
cb: (payload: import('./hmrPayload').FullReloadPayload) => void
): void
(
event: 'vite:error',
cb: (payload: import('./hmrPayload').ErrorPayload) => void
): void
<T extends string>(
event: import('./customEvent').CustomEventName<T>,
cb: (data: any) => void
): void
}

send(event: string, data?: any): void
}
readonly hot?: import('./hot').ViteHotContext

readonly env: ImportMetaEnv

Expand Down