From c2118245be8f211a2898f13ccc4283279e2dd177 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 25 Sep 2022 12:17:54 -0400 Subject: [PATCH 01/14] feat: support `import.meta.hot.invalidate` Self-accepting modules can call this to continue propagating the HMR update to importers. Conditional `hot.accept` calls do not work unless `hot.invalidate` is called when the condition fails. --- packages/vite/src/client/client.ts | 5 ++--- packages/vite/src/node/server/index.ts | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 83465d794358e2..64e3bef43173b2 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -546,10 +546,9 @@ export function createHotContext(ownerPath: string): ViteHotContext { // eslint-disable-next-line @typescript-eslint/no-empty-function decline() {}, + // tell the server to re-perform hmr propagation from this module as root invalidate() { - // TODO should tell the server to re-perform hmr propagation - // from this module as root - location.reload() + this.send('vite:invalidate', ownerPath) }, // custom events diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 57a6a01e1fea12..2980dd2c3de943 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -64,14 +64,15 @@ import { serveStaticMiddleware } from './middlewares/static' import { timeMiddleware } from './middlewares/time' -import { ModuleGraph } from './moduleGraph' +import { ModuleGraph, ModuleNode } from './moduleGraph' import { errorMiddleware, prepareError } from './middlewares/error' -import type { HmrOptions } from './hmr' +import { getShortName, HmrOptions, updateModules } from './hmr' import { handleFileAddUnlink, handleHMRUpdate } from './hmr' import { openBrowser } from './openBrowser' import type { TransformOptions, TransformResult } from './transformRequest' import { transformRequest } from './transformRequest' import { searchForWorkspaceRoot } from './searchRoot' +import { HMRPayload } from '../../types/hmrPayload' export { searchForWorkspaceRoot } from './searchRoot' @@ -489,6 +490,18 @@ export async function createServer( handleFileAddUnlink(normalizePath(file), server) }) + ws.on('vite:invalidate', async (url: string) => { + const mod = moduleGraph.urlToModuleMap.get(url) + if (mod) { + const importers = new Set() + for (const importer of mod.importers) { + importers.add(importer) + } + const file = getShortName(mod.file!, config.root) + updateModules(file, Array.from(importers), Date.now(), server) + } + }) + if (!middlewareMode && httpServer) { httpServer.once('listening', () => { // update actual port since this may be different from initial value From 002bd60f82e932a7c43a846ee761c7ffb3ed53af Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 25 Sep 2022 13:24:39 -0400 Subject: [PATCH 02/14] fix: enable `hot.invalidate` for self-accepting modules only --- packages/vite/src/node/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 2980dd2c3de943..3d8ccad5b10f36 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -492,7 +492,7 @@ export async function createServer( ws.on('vite:invalidate', async (url: string) => { const mod = moduleGraph.urlToModuleMap.get(url) - if (mod) { + if (mod && mod.isSelfAccepting) { const importers = new Set() for (const importer of mod.importers) { importers.add(importer) From 6c71e79b7c9b4d7acd36b4ad90ed6b85902b7897 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 25 Sep 2022 13:25:19 -0400 Subject: [PATCH 03/14] fix: ignore `hot.invalidate` call if hmr timestamp is zero --- packages/vite/src/node/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 3d8ccad5b10f36..29e87147251b1a 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -492,7 +492,7 @@ export async function createServer( ws.on('vite:invalidate', async (url: string) => { const mod = moduleGraph.urlToModuleMap.get(url) - if (mod && mod.isSelfAccepting) { + if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) { const importers = new Set() for (const importer of mod.importers) { importers.add(importer) From 8b9cb674949f3f26408cae8b5ff224c2cfc1df9d Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Sun, 25 Sep 2022 13:36:19 -0400 Subject: [PATCH 04/14] fix: use `lastHMRTimestamp` in `hot.invalidate` handler --- packages/vite/src/node/server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 29e87147251b1a..4f8eeda492b2e7 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -498,7 +498,7 @@ export async function createServer( importers.add(importer) } const file = getShortName(mod.file!, config.root) - updateModules(file, Array.from(importers), Date.now(), server) + updateModules(file, Array.from(importers), mod.lastHMRTimestamp, server) } }) From 295c6de524bdd073b0c1740c0f16b7a1de1ef542 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sun, 25 Sep 2022 14:40:51 -0400 Subject: [PATCH 05/14] chore: remove unused import --- packages/vite/src/node/server/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 4f8eeda492b2e7..c125a4d89292e4 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -64,15 +64,15 @@ import { serveStaticMiddleware } from './middlewares/static' import { timeMiddleware } from './middlewares/time' -import { ModuleGraph, ModuleNode } from './moduleGraph' +import type { ModuleNode } from './moduleGraph'; +import { ModuleGraph } from './moduleGraph' import { errorMiddleware, prepareError } from './middlewares/error' -import { getShortName, HmrOptions, updateModules } from './hmr' -import { handleFileAddUnlink, handleHMRUpdate } from './hmr' +import type { HmrOptions} from './hmr'; +import { getShortName, handleFileAddUnlink , handleHMRUpdate, updateModules } from './hmr' import { openBrowser } from './openBrowser' import type { TransformOptions, TransformResult } from './transformRequest' import { transformRequest } from './transformRequest' import { searchForWorkspaceRoot } from './searchRoot' -import { HMRPayload } from '../../types/hmrPayload' export { searchForWorkspaceRoot } from './searchRoot' From d0eb297831bb18ed4a327ad2fc9e0161b163fe32 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sun, 25 Sep 2022 14:42:10 -0400 Subject: [PATCH 06/14] chore: format --- packages/vite/src/node/server/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c125a4d89292e4..f17fcc9f134eaa 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -64,11 +64,16 @@ import { serveStaticMiddleware } from './middlewares/static' import { timeMiddleware } from './middlewares/time' -import type { ModuleNode } from './moduleGraph'; +import type { ModuleNode } from './moduleGraph' import { ModuleGraph } from './moduleGraph' import { errorMiddleware, prepareError } from './middlewares/error' -import type { HmrOptions} from './hmr'; -import { getShortName, handleFileAddUnlink , handleHMRUpdate, updateModules } from './hmr' +import type { HmrOptions } from './hmr' +import { + getShortName, + handleFileAddUnlink, + handleHMRUpdate, + updateModules +} from './hmr' import { openBrowser } from './openBrowser' import type { TransformOptions, TransformResult } from './transformRequest' import { transformRequest } from './transformRequest' From 68fe6ffafc24867913de5500c643dac83c448256 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sun, 25 Sep 2022 15:54:26 -0400 Subject: [PATCH 07/14] feat: notify listeners to invalidation changes --- packages/vite/src/client/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 64e3bef43173b2..a07c854267926d 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -548,6 +548,7 @@ export function createHotContext(ownerPath: string): ViteHotContext { // tell the server to re-perform hmr propagation from this module as root invalidate() { + notifyListeners('vite:invalidate', ownerPath) this.send('vite:invalidate', ownerPath) }, From 83cb7cc6694b68cd732533ee608546a8a69f62e9 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sun, 25 Sep 2022 16:01:56 -0400 Subject: [PATCH 08/14] test: add test for invalidation --- playground/hmr/__tests__/hmr.spec.ts | 24 ++++++++++++++++++++++-- playground/hmr/hmr.ts | 5 +++++ playground/hmr/index.html | 1 + playground/hmr/invalidation/child.js | 8 ++++++++ playground/hmr/invalidation/parent.js | 9 +++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 playground/hmr/invalidation/child.js create mode 100644 playground/hmr/invalidation/parent.js diff --git a/playground/hmr/__tests__/hmr.spec.ts b/playground/hmr/__tests__/hmr.spec.ts index ef8def29a389a5..70d5a1b9ace52e 100644 --- a/playground/hmr/__tests__/hmr.spec.ts +++ b/playground/hmr/__tests__/hmr.spec.ts @@ -18,14 +18,14 @@ test('should render', async () => { if (!isBuild) { test('should connect', async () => { - expect(browserLogs.length).toBe(2) + expect(browserLogs.length).toBe(3) expect(browserLogs.some((msg) => msg.match('connected'))).toBe(true) browserLogs.length = 0 }) test('self accept', async () => { const el = await page.$('.app') - + browserLogs.length = 0 editFile('hmr.ts', (code) => code.replace('const foo = 1', 'const foo = 2')) await untilUpdated(() => el.textContent(), '2') @@ -91,6 +91,7 @@ if (!isBuild) { test('nested dep propagation', async () => { const el = await page.$('.nested') + browserLogs.length = 0 editFile('hmrNestedDep.js', (code) => code.replace('const foo = 1', 'const foo = 2') @@ -127,6 +128,25 @@ if (!isBuild) { browserLogs.length = 0 }) + test('invalidate', async () => { + browserLogs.length = 0 + const el = await page.$('.invalidation') + + editFile('invalidation/child.js', (code) => + code.replace('child', 'child updated') + ) + await untilUpdated(() => el.textContent(), 'child updated') + expect(browserLogs).toMatchObject([ + '>>> vite:beforeUpdate -- update', + '>>> vite:invalidate -- /invalidation/child.js', + '[vite] hot updated: /invalidation/child.js', + '>>> vite:beforeUpdate -- update', + '(invalidation) parent is executing', + '[vite] hot updated: /invalidation/parent.js' + ]) + browserLogs.length = 0 + }) + test('plugin hmr handler + custom event', async () => { const el = await page.$('.custom') editFile('customFile.js', (code) => code.replace('custom', 'edited')) diff --git a/playground/hmr/hmr.ts b/playground/hmr/hmr.ts index dc3c22eac9d56e..269fea2ff0f2ab 100644 --- a/playground/hmr/hmr.ts +++ b/playground/hmr/hmr.ts @@ -2,6 +2,7 @@ import { virtual } from 'virtual:file' import { foo as depFoo, nestedFoo } from './hmrDep' import './importing-updated' +import './invalidation/parent' export const foo = 1 text('.app', foo) @@ -88,6 +89,10 @@ if (import.meta.hot) { console.log(`>>> vite:error -- ${event.type}`) }) + import.meta.hot.on('vite:invalidate', (event) => { + console.log(`>>> vite:invalidate -- ${event}`) + }) + import.meta.hot.on('custom:foo', ({ msg }) => { text('.custom', msg) }) diff --git a/playground/hmr/index.html b/playground/hmr/index.html index 28f08014036ade..b8d6065a9fd5e2 100644 --- a/playground/hmr/index.html +++ b/playground/hmr/index.html @@ -20,6 +20,7 @@
+
diff --git a/playground/hmr/invalidation/child.js b/playground/hmr/invalidation/child.js new file mode 100644 index 00000000000000..efa9e0c058e025 --- /dev/null +++ b/playground/hmr/invalidation/child.js @@ -0,0 +1,8 @@ +if (import.meta.hot) { + // Need to accept, to register a callback for HMR + import.meta.hot.accept() + // Trigger HMR in importers + import.meta.hot.invalidate() +} + +export const value = 'child' diff --git a/playground/hmr/invalidation/parent.js b/playground/hmr/invalidation/parent.js new file mode 100644 index 00000000000000..0b10298fff1aa4 --- /dev/null +++ b/playground/hmr/invalidation/parent.js @@ -0,0 +1,9 @@ +import { value } from './child' + +if (import.meta.hot) { + import.meta.hot.accept() +} + +console.log('(invalidation) parent is executing') + +document.querySelector('.invalidation').innerHTML = value From 970aa7738ae72fc9427e43eca027979ad8b15f61 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sun, 25 Sep 2022 16:07:16 -0400 Subject: [PATCH 09/14] docs: update documentation for hot.invalidate --- docs/guide/api-hmr.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/guide/api-hmr.md b/docs/guide/api-hmr.md index 3b416169f5231a..a115ca69b3224a 100644 --- a/docs/guide/api-hmr.md +++ b/docs/guide/api-hmr.md @@ -125,7 +125,7 @@ Calling `import.meta.hot.decline()` indicates this module is not hot-updatable, ## `hot.invalidate()` -For now, calling `import.meta.hot.invalidate()` simply reloads the page. +Calling `import.meta.hot.invalidate()` indicates that changes have occurred and the module's state should be treated as invalid. This will cause importers of the module to HMR, and thus provides a way to force changes to propagate upwards. ## `hot.on(event, cb)` @@ -136,6 +136,7 @@ The following HMR events are dispatched by Vite automatically: - `'vite:beforeUpdate'` when an update is about to be applied (e.g. a module will be replaced) - `'vite:beforeFullReload'` when a full reload is about to occur - `'vite:beforePrune'` when modules that are no longer needed are about to be pruned +- `'vite:invalidate'` when a module is invalidated with `import.meta.hot.invalidate()` - `'vite:error'` when an error occurs (e.g. syntax error) Custom HMR events can also be sent from plugins. See [handleHotUpdate](./api-plugin#handlehotupdate) for more details. From 93608ea96a1c6da7169c4d1a4e6b7717939135e3 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 26 Sep 2022 09:33:45 -0400 Subject: [PATCH 10/14] Update packages/vite/src/node/server/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- packages/vite/src/node/server/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index f17fcc9f134eaa..e5bee484459a41 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -498,12 +498,8 @@ export async function createServer( ws.on('vite:invalidate', async (url: string) => { const mod = moduleGraph.urlToModuleMap.get(url) if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) { - const importers = new Set() - for (const importer of mod.importers) { - importers.add(importer) - } const file = getShortName(mod.file!, config.root) - updateModules(file, Array.from(importers), mod.lastHMRTimestamp, server) + updateModules(file, [...mod.importers], mod.lastHMRTimestamp, server) } }) From 63309482647281af6b55d032a8d98efb27a90d6e Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Mon, 26 Sep 2022 09:45:37 -0400 Subject: [PATCH 11/14] Update playground/hmr/invalidation/child.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- playground/hmr/invalidation/child.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/playground/hmr/invalidation/child.js b/playground/hmr/invalidation/child.js index efa9e0c058e025..b424e2f83c3233 100644 --- a/playground/hmr/invalidation/child.js +++ b/playground/hmr/invalidation/child.js @@ -1,8 +1,9 @@ if (import.meta.hot) { // Need to accept, to register a callback for HMR - import.meta.hot.accept() - // Trigger HMR in importers - import.meta.hot.invalidate() + import.meta.hot.accept(() => { + // Trigger HMR in importers + import.meta.hot.invalidate() + }) } export const value = 'child' From 898e09eed4ba0de5cd94ed599a8a2b38242cca8a Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Mon, 26 Sep 2022 09:47:19 -0400 Subject: [PATCH 12/14] chore: remove unused import --- packages/vite/src/node/server/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index e5bee484459a41..40da1c410bbf1f 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -64,7 +64,6 @@ import { serveStaticMiddleware } from './middlewares/static' import { timeMiddleware } from './middlewares/time' -import type { ModuleNode } from './moduleGraph' import { ModuleGraph } from './moduleGraph' import { errorMiddleware, prepareError } from './middlewares/error' import type { HmrOptions } from './hmr' From a21886d53b4631dca95a62eb7a50ffe9fb9b9e25 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Mon, 26 Sep 2022 17:54:39 -0400 Subject: [PATCH 13/14] Update docs/guide/api-hmr.md Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> --- docs/guide/api-hmr.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/guide/api-hmr.md b/docs/guide/api-hmr.md index a115ca69b3224a..12a859370d2e11 100644 --- a/docs/guide/api-hmr.md +++ b/docs/guide/api-hmr.md @@ -125,7 +125,18 @@ Calling `import.meta.hot.decline()` indicates this module is not hot-updatable, ## `hot.invalidate()` -Calling `import.meta.hot.invalidate()` indicates that changes have occurred and the module's state should be treated as invalid. This will cause importers of the module to HMR, and thus provides a way to force changes to propagate upwards. +A self-accepting module may realize during runtime that it can't handle a HMR update, and so the update needs to be forcefully propagated to importers. By calling `import.meta.hot.invalidate()`, the HMR server will invalidate the importers of the caller, as if the caller wasn't self-accepting. + +Note that you should always call `import.meta.hot.accept` even if you plan to call `invalidate` immediately afterwards, or else the HMR client won't listen for future changes to the self-accepting module. To communicate your intent clearly, we recommend calling `invalidate` within the `accept` callback like so: + + ```ts + import.meta.hot.accept(module => { + // You may use the new module instance to decide whether to invalidate. + if (cannotHandleUpdate(module)) { + import.meta.hot.invalidate() + } + }) + ``` ## `hot.on(event, cb)` From d147d6b2529252a528ab8accded2418aaf9347e0 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Wed, 28 Sep 2022 07:23:23 -0400 Subject: [PATCH 14/14] types: add type for vite:invalidate payload --- packages/vite/src/client-types.d.ts | 3 ++- packages/vite/src/client/client.ts | 4 ++-- packages/vite/src/node/index.ts | 6 +++++- packages/vite/src/node/server/index.ts | 5 +++-- packages/vite/src/types/customEvent.d.ts | 5 +++++ packages/vite/types/customEvent.d.ts | 6 +++++- playground/hmr/hmr.ts | 4 ++-- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/client-types.d.ts b/packages/vite/src/client-types.d.ts index 270a1ac29f2d8b..cca320bc5d4e1b 100644 --- a/packages/vite/src/client-types.d.ts +++ b/packages/vite/src/client-types.d.ts @@ -1,6 +1,7 @@ export type { CustomEventMap, - InferCustomEventPayload + InferCustomEventPayload, + InvalidatePayload } from './types/customEvent' export type { HMRPayload, diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index a07c854267926d..3f974e77d9b52b 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -548,8 +548,8 @@ export function createHotContext(ownerPath: string): ViteHotContext { // tell the server to re-perform hmr propagation from this module as root invalidate() { - notifyListeners('vite:invalidate', ownerPath) - this.send('vite:invalidate', ownerPath) + notifyListeners('vite:invalidate', { path: ownerPath }) + this.send('vite:invalidate', { path: ownerPath }) }, // custom events diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 2bc81f001d6947..0e246615bfa5f3 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -102,7 +102,11 @@ export type { PrunePayload, ErrorPayload } from 'types/hmrPayload' -export type { CustomEventMap, InferCustomEventPayload } from 'types/customEvent' +export type { + CustomEventMap, + InferCustomEventPayload, + InvalidatePayload +} from 'types/customEvent' // [deprecated: use vite/client/types instead] export type { ImportGlobFunction, diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 40da1c410bbf1f..77b91a4dfd6dba 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -13,6 +13,7 @@ import launchEditorMiddleware from 'launch-editor-middleware' import type { SourceMap } from 'rollup' import picomatch from 'picomatch' import type { Matcher } from 'picomatch' +import type { InvalidatePayload } from 'types/customEvent' import type { CommonServerOptions } from '../http' import { httpServerStart, @@ -494,8 +495,8 @@ export async function createServer( handleFileAddUnlink(normalizePath(file), server) }) - ws.on('vite:invalidate', async (url: string) => { - const mod = moduleGraph.urlToModuleMap.get(url) + ws.on('vite:invalidate', async ({ path }: InvalidatePayload) => { + const mod = moduleGraph.urlToModuleMap.get(path) if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) { const file = getShortName(mod.file!, config.root) updateModules(file, [...mod.importers], mod.lastHMRTimestamp, server) diff --git a/packages/vite/src/types/customEvent.d.ts b/packages/vite/src/types/customEvent.d.ts index af4db5d14fbe97..839e17dd729eda 100644 --- a/packages/vite/src/types/customEvent.d.ts +++ b/packages/vite/src/types/customEvent.d.ts @@ -10,6 +10,11 @@ export interface CustomEventMap { 'vite:beforePrune': PrunePayload 'vite:beforeFullReload': FullReloadPayload 'vite:error': ErrorPayload + 'vite:invalidate': InvalidatePayload +} + +export interface InvalidatePayload { + path: string } export type InferCustomEventPayload = diff --git a/packages/vite/types/customEvent.d.ts b/packages/vite/types/customEvent.d.ts index 09fd7dc36ea481..d5bdbde98984fb 100644 --- a/packages/vite/types/customEvent.d.ts +++ b/packages/vite/types/customEvent.d.ts @@ -1 +1,5 @@ -export type { CustomEventMap, InferCustomEventPayload } from '../client/types' +export type { + CustomEventMap, + InferCustomEventPayload, + InvalidatePayload +} from '../client/types' diff --git a/playground/hmr/hmr.ts b/playground/hmr/hmr.ts index 269fea2ff0f2ab..473dff9fdbfb88 100644 --- a/playground/hmr/hmr.ts +++ b/playground/hmr/hmr.ts @@ -89,8 +89,8 @@ if (import.meta.hot) { console.log(`>>> vite:error -- ${event.type}`) }) - import.meta.hot.on('vite:invalidate', (event) => { - console.log(`>>> vite:invalidate -- ${event}`) + import.meta.hot.on('vite:invalidate', ({ path }) => { + console.log(`>>> vite:invalidate -- ${path}`) }) import.meta.hot.on('custom:foo', ({ msg }) => {