You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/guide/api-hmr.md
+2
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# HMR API
2
2
3
3
:::tip Note
4
+
This is the client HMR API. For handling HMR update in plugins, see [handleHotUpdate](./api-plugin#handlehotupdate).
5
+
4
6
The manual HMR API is primarly intended for framework and tooling authors. As an end user, HMR is likely already handled for you in the framework specific starter templates.
Perform custom HMR update handling. The hook receives the following arguments:
277
-
278
-
1. The changed file path
276
+
Perform custom HMR update handling. The hook receives a context object with the following signature:
279
277
280
-
2. An array of modules that are affected by the changed file. It's an array because a single file may map to multiple served modules (e.g. Vue SFCs).
278
+
```ts
279
+
interfaceHmrContext {
280
+
file:string
281
+
timestamp:number
282
+
modules:Array<ModuleNode>
283
+
read: () =>string|Promise<string>
284
+
server:ViteDevServer
285
+
}
286
+
```
281
287
282
-
3. An async read function that returns the content of the file. This is provided because on some systems, the file change callback may fire too fast before the editor finishes updating the file and direct `fs.readFile` will return empty content. The read function passed in normalizes this behavior.
288
+
-`modules` is an array of modules that are affected by the changed file. It's an array because a single file may map to multiple served modules (e.g. Vue SFCs).
283
289
284
-
4. The [`ViteDevServer`](./api-javascript#vitedevserver) instance.
290
+
-`read` is an async read function that returns the content of the file. This is provided because on some systems, the file change callback may fire too fast before the editor finishes updating the file and direct `fs.readFile` will return empty content. The read function passed in normalizes this behavior.
285
291
286
292
The hook can choose to:
287
293
@@ -290,7 +296,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
290
296
- Return an empty array and perform complete custom HMR handling by sending custom events to the client:
291
297
292
298
```js
293
-
handleHotUpdate(file, mods, read, server) {
299
+
handleHotUpdate({ server }) {
294
300
server.ws.send({
295
301
type:'custom',
296
302
event:'special-update',
@@ -304,7 +310,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
0 commit comments