Skip to content

Commit 68bb979

Browse files
jods4posva
andauthored
feat(hmr): improve Vite HMR implementation (#502)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 3c9e0f3 commit 68bb979

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

playground/src/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ app.use(DataLoaderPlugin, { router })
3131
app.use(router)
3232

3333
app.mount('#app')
34+
35+
// small logger for navigations, useful to check HMR
36+
router.isReady().then(() => {
37+
router.beforeEach((to, from) => {
38+
console.log('🧭', from.fullPath, '->', to.fullPath)
39+
})
40+
})

src/core/vite/index.ts

+6-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ViteDevServer } from 'vite'
2-
import { ServerContext } from '../../options'
2+
import { type ServerContext } from '../../options'
33
import { MODULE_ROUTES_PATH, asVirtualId } from '../moduleConstants'
44

55
export function createViteContext(server: ViteDevServer): ServerContext {
@@ -39,28 +39,15 @@ export function createViteContext(server: ViteDevServer): ServerContext {
3939
})
4040
}
4141

42-
// NOTE: still not working
43-
// based on https://github.com/vuejs/vitepress/blob/1188951785fd2a72f9242d46dc55abb1effd212a/src/node/plugins/localSearchPlugin.ts#L90
44-
// https://github.com/unocss/unocss/blob/f375524d9bca3f2f8b445b322ec0fc3eb124ec3c/packages/vite/src/modes/global/dev.ts#L47-L66
45-
42+
/**
43+
* Triggers HMR for the vue-router/auto-routes module.
44+
*/
4645
async function updateRoutes() {
4746
const modId = asVirtualId(MODULE_ROUTES_PATH)
4847
const mod = server.moduleGraph.getModuleById(modId)
49-
if (!mod) {
50-
return
48+
if (mod) {
49+
return server.reloadModule(mod)
5150
}
52-
server.moduleGraph.invalidateModule(mod)
53-
server.ws.send({
54-
type: 'update',
55-
updates: [
56-
{
57-
acceptedPath: mod.url,
58-
path: mod.url,
59-
timestamp: Date.now(),
60-
type: 'js-update',
61-
},
62-
],
63-
})
6451
}
6552

6653
return {

src/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export const DEFAULT_OPTIONS = {
246246

247247
export interface ServerContext {
248248
invalidate: (module: string) => void
249-
updateRoutes: () => void
249+
updateRoutes: () => Promise<void>
250250
reload: () => void
251251
}
252252

0 commit comments

Comments
 (0)