@@ -107,42 +107,37 @@ export function createRoutesContext(options: ResolvedOptions) {
107
107
108
108
async function writeRouteInfoToNode ( node : TreeNode , filePath : string ) {
109
109
const content = await fs . readFile ( filePath , 'utf8' )
110
- // TODO: cache the result of parsing the SFC so the transform can reuse the parsing
110
+ // TODO: cache the result of parsing the SFC (in the extractDefinePageAndName) so the transform can reuse the parsing
111
111
node . hasDefinePage ||= content . includes ( 'definePage' )
112
+ // TODO: track if it changed and to not always trigger HMR
112
113
const definedPageNameAndPath = extractDefinePageNameAndPath (
113
114
content ,
114
115
filePath
115
116
)
117
+ // TODO: track if it changed and if generateRoutes should be called again
116
118
const routeBlock = getRouteBlock ( filePath , content , options )
117
119
// TODO: should warn if hasDefinePage and customRouteBlock
118
120
// if (routeBlock) logger.log(routeBlock)
119
121
node . setCustomRouteBlock ( filePath , {
120
122
...routeBlock ,
121
123
...definedPageNameAndPath ,
122
124
} )
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()
130
125
}
131
126
132
127
async function addPage (
133
128
{ filePath, routePath } : HandlerContext ,
134
129
triggerExtendRoute = false
135
130
) {
136
131
logger . log ( `added "${ routePath } " for "${ filePath } "` )
137
- // TODO: handle top level named view HMR
138
-
139
132
const node = routeTree . insert ( routePath , filePath )
140
-
141
133
await writeRouteInfoToNode ( node , filePath )
142
134
143
135
if ( triggerExtendRoute ) {
144
136
await options . extendRoute ?.( new EditableTreeNode ( node ) )
145
137
}
138
+
139
+ // TODO: trigger HMR vue-router/auto
140
+ server ?. updateRoutes ( )
146
141
}
147
142
148
143
async function updatePage ( { filePath, routePath } : HandlerContext ) {
@@ -154,11 +149,15 @@ export function createRoutesContext(options: ResolvedOptions) {
154
149
}
155
150
await writeRouteInfoToNode ( node , filePath )
156
151
await options . extendRoute ?.( new EditableTreeNode ( node ) )
152
+ // no need to manually trigger the update of vue-router/auto-routes because
153
+ // the change of the vue file will trigger HMR
157
154
}
158
155
159
156
function removePage ( { filePath, routePath } : HandlerContext ) {
160
157
logger . log ( `remove "${ routePath } " for "${ filePath } "` )
161
158
routeTree . removeChild ( filePath )
159
+ // TODO: HMR vue-router/auto
160
+ server ?. updateRoutes ( )
162
161
}
163
162
164
163
function setupWatcher ( watcher : RoutesFolderWatcher ) {
@@ -182,16 +181,37 @@ export function createRoutesContext(options: ResolvedOptions) {
182
181
// unlinkDir event
183
182
}
184
183
185
- let lastAutoRoutes : string | undefined
186
184
function generateRoutes ( ) {
187
185
const importsMap = new ImportsMap ( )
188
186
189
- const routesExport = `export const routes = ${ generateRouteRecord (
187
+ const routeList = `export const routes = ${ generateRouteRecord (
190
188
routeTree ,
191
189
options ,
192
190
importsMap
193
- ) } `
194
- // TODO: should we put some HMR code for routes here or should it be at the router creation level (that would be easier to replace the routes)
191
+ ) } \n`
192
+
193
+ let hmr = `
194
+ export function handleHotUpdate(_router) {
195
+ if (import.meta.hot) {
196
+ import.meta.hot.data.router = _router
197
+ }
198
+ }
199
+
200
+ if (import.meta.hot) {
201
+ import.meta.hot.accept((mod) => {
202
+ const router = import.meta.hot.data.router
203
+ if (!router) {
204
+ console.error('❌ router not found')
205
+ return
206
+ }
207
+ router.clearRoutes()
208
+ for (const route of mod.routes) {
209
+ router.addRoute(route)
210
+ }
211
+ router.replace('')
212
+ })
213
+ }
214
+ `
195
215
196
216
// generate the list of imports
197
217
let imports = importsMap . toString ( )
@@ -200,13 +220,7 @@ export function createRoutesContext(options: ResolvedOptions) {
200
220
imports += '\n'
201
221
}
202
222
203
- const newAutoRoutes = `${ imports } ${ routesExport } \n`
204
-
205
- if ( lastAutoRoutes !== newAutoRoutes ) {
206
- // cache hit, triigger HMR (not working yet)
207
- server ?. updateRoutes ( )
208
- lastAutoRoutes = newAutoRoutes
209
- }
223
+ const newAutoRoutes = `${ imports } ${ routeList } ${ hmr } \n`
210
224
211
225
// prepend it to the code
212
226
return newAutoRoutes
0 commit comments