Skip to content

Commit f5fc2ec

Browse files
committed
chore: trying to make hmr work
1 parent 07f2777 commit f5fc2ec

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

playground/src/pages/[name].vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ definePage({
106106
meta: {
107107
[dummy_id]: 'id',
108108
fixed: dummy_number,
109-
// hello: 'there',
110109
mySymbol: Symbol(),
111110
['hello' + 'expr']: true,
112111
test: (to: RouteLocationNormalized) => {
@@ -144,13 +143,16 @@ definePage({
144143
<pre>{{ one }}</pre>
145144
<p>two</p>
146145
<pre>{{ two }}</pre>
146+
<p>meta:</p>
147+
<pre>{{ route.meta }}</pre>
147148
</main>
148149
</template>
149150

150151
<route lang="json">
151152
{
152153
"meta": {
153-
"hello": "there"
154+
"hello": "there",
155+
"n": 1
154156
}
155157
}
156158
</route>

playground/src/router.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,47 @@ export const router = createRouter({
77
routes,
88
})
99

10+
// let removeRoutes: Array<() => void> = []
11+
// for (const route of routes) {
12+
// removeRoutes.push(router.addRoute(route))
13+
// }
14+
1015
if (import.meta.hot) {
1116
// How to trigger this? tried virtual: /@id/
12-
import.meta.hot.accept('vue-router/auto-routes', (mod) => {
13-
console.log('✨ got new routes', mod)
14-
})
15-
import.meta.hot.accept((mod) => {
16-
console.log('🔁 reloading routes from router...', mod)
17-
console.log(mod!.router.getRoutes())
18-
})
17+
const id = 'vue-router/auto-routes'
18+
// import.meta.hot.accept('vue-router/auto-routes', (mod) => {
19+
// console.log('✨ got new routes', mod)
20+
// })
21+
// import.meta.hot.accept(id, (mod) => {
22+
// console.log('✨ got new routes', mod)
23+
// })
24+
// import.meta.hot.accept((mod) => {
25+
// console.log('🔁 reloading routes from router...', mod)
26+
// console.log(mod!.router.getRoutes())
27+
// })
28+
29+
// NOTE: this approach doesn't make sense and doesn't work anyway: it seems to fetch an outdated version of the file
30+
// import.meta.hot.on('vite:beforeUpdate', async (payload) => {
31+
// console.log('🔁 vite:beforeUpdate', payload)
32+
// const routesUpdate = payload.updates.find(
33+
// (update) => update.path === 'virtual:vue-router/auto-routes'
34+
// )
35+
// if (routesUpdate) {
36+
// console.log('🔁 reloading routes from router...')
37+
// const { routes } = await import(
38+
// '/@id/' + routesUpdate.path + `?t=${routesUpdate.timestamp}`
39+
// )
40+
// for (const removeRoute of removeRoutes) {
41+
// console.log('Removing route...')
42+
// removeRoute()
43+
// }
44+
// removeRoutes = []
45+
// for (const route of routes) {
46+
// removeRoutes.push(router.addRoute(route))
47+
// }
48+
// router.replace('')
49+
// }
50+
// })
1951
}
2052

2153
// manual extension of route types

src/core/context.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { TreeNode, PrefixTree } from './tree'
33
import { promises as fs } from 'fs'
44
import { asRoutePath, ImportsMap, logTree, throttle } from './utils'
55
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
6-
import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER_AUTO } from './moduleConstants'
6+
import {
7+
MODULE_ROUTES_PATH,
8+
MODULE_VUE_ROUTER_AUTO,
9+
asVirtualId,
10+
} from './moduleConstants'
711
import { generateRouteRecord } from '../codegen/generateRouteRecords'
812
import fg from 'fast-glob'
913
import { relative, resolve } from 'pathe'
@@ -116,6 +120,13 @@ export function createRoutesContext(options: ResolvedOptions) {
116120
...routeBlock,
117121
...definedPageNameAndPath,
118122
})
123+
124+
// TODO: if definePage changed
125+
server?.invalidate(filePath + '?definePage&vue&lang.tsx')
126+
server?.invalidate(asVirtualId(MODULE_ROUTES_PATH))
127+
128+
// TODO: only if needed
129+
// server?.updateRoutes()
119130
}
120131

121132
async function addPage(
@@ -162,8 +173,8 @@ export function createRoutesContext(options: ResolvedOptions) {
162173
await addPage(ctx, true)
163174
writeConfigFiles()
164175
})
165-
.on('unlink', async (ctx) => {
166-
await removePage(ctx)
176+
.on('unlink', (ctx) => {
177+
removePage(ctx)
167178
writeConfigFiles()
168179
})
169180

src/core/vite/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,29 @@ import { MODULE_ROUTES_PATH, asVirtualId } from '../moduleConstants'
55
export function createViteContext(server: ViteDevServer): ServerContext {
66
function invalidate(path: string) {
77
const { moduleGraph } = server
8-
const foundModule = moduleGraph.getModuleById(asVirtualId(path))
8+
const foundModule = moduleGraph.getModuleById(path)
99
if (foundModule) {
1010
moduleGraph.invalidateModule(foundModule)
11+
// for (const mod of foundModule.importers) {
12+
// console.log(`Invalidating ${mod.url}`)
13+
// moduleGraph.invalidateModule(mod)
14+
// }
15+
setTimeout(() => {
16+
console.log(`Sending update for ${foundModule.url}`)
17+
server.ws.send({
18+
type: 'update',
19+
updates: [
20+
{
21+
acceptedPath: path,
22+
path: path,
23+
// NOTE: this was in the
24+
// timestamp: ROUTES_LAST_LOAD_TIME.value,
25+
timestamp: Date.now(),
26+
type: 'js-update',
27+
},
28+
],
29+
})
30+
}, 100)
1131
}
1232
return !!foundModule
1333
}

0 commit comments

Comments
 (0)