Skip to content

Commit

Permalink
chore(client): expose hot.prune API (#11016)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Nov 22, 2022
1 parent d413848 commit f40c18d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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

0 comments on commit f40c18d

Please sign in to comment.