Skip to content

Commit

Permalink
feat: support import.meta.hot.invalidate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aleclarson committed Sep 25, 2022
1 parent 6d02e62 commit a45385f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-react/src/fast-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ import(/* @vite-ignore */ import.meta.url).then(mod => {
if (isReactRefreshBoundary(mod)) {
import.meta.hot.accept();
${timeout}
} else {
import.meta.hot.invalidate();
}
})
`
Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<ModuleNode>()
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
Expand Down

0 comments on commit a45385f

Please sign in to comment.