Skip to content

Commit f40c18d

Browse files
authoredNov 22, 2022
chore(client): expose hot.prune API (#11016)
1 parent d413848 commit f40c18d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
 

‎docs/guide/api-hmr.md

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface ViteHotContext {
2929
): void
3030

3131
dispose(cb: (data: any) => void): void
32+
prune(cb: (data: any) => void): void
3233
decline(): void
3334
invalidate(message?: string): void
3435

@@ -115,6 +116,18 @@ if (import.meta.hot) {
115116
}
116117
```
117118
119+
## `hot.prune(cb)`
120+
121+
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.
122+
123+
```js
124+
if (import.meta.hot) {
125+
import.meta.hot.prune((data) => {
126+
// cleanup side effect
127+
})
128+
}
129+
```
130+
118131
## `hot.data`
119132
120133
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.

‎packages/vite/src/client/client.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,7 @@ export function createHotContext(ownerPath: string): ViteHotContext {
548548
disposeMap.set(ownerPath, cb)
549549
},
550550

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

‎packages/vite/types/hot.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ViteHotContext {
2222
): void
2323

2424
dispose(cb: (data: any) => void): void
25+
prune(cb: (data: any) => void): void
2526
decline(): void
2627
invalidate(message?: string): void
2728

0 commit comments

Comments
 (0)
Please sign in to comment.