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

chore(client): expose hot.prune API #11016

Merged
merged 1 commit into from Nov 22, 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
13 changes: 13 additions & 0 deletions docs/guide/api-hmr.md
Expand Up @@ -29,6 +29,7 @@ interface ViteHotContext {
): void

dispose(cb: (data: any) => void): void
prune(cb: (data: any) => void): void
decline(): void
invalidate(message?: string): void

Expand Down Expand Up @@ -115,6 +116,18 @@ if (import.meta.hot) {
}
```

## `hot.prune(cb)`

Register a callback that will call when the module is no longer imported on the page. This can be used to clean up side effects like style injections. Vite already does this for `.css` imports.

```js
if (import.meta.hot) {
import.meta.hot.prune((data) => {
// cleanup side effect
})
}
```

## `hot.data`

The `import.meta.hot.data` object is persisted across different instances of the same updated module. It can be used to pass on information from a previous version of the module to the next one.
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/client/client.ts
Expand Up @@ -548,8 +548,7 @@ export function createHotContext(ownerPath: string): ViteHotContext {
disposeMap.set(ownerPath, cb)
},

// @ts-expect-error untyped
prune(cb: (data: any) => void) {
prune(cb) {
pruneMap.set(ownerPath, cb)
},

Expand Down
1 change: 1 addition & 0 deletions packages/vite/types/hot.d.ts
Expand Up @@ -22,6 +22,7 @@ export interface ViteHotContext {
): void

dispose(cb: (data: any) => void): void
prune(cb: (data: any) => void): void
decline(): void
invalidate(message?: string): void

Expand Down