Skip to content

Commit 8b2993c

Browse files
committed
refactor: wip hmr for auto-routes
1 parent c98ddbf commit 8b2993c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/core/context.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export function createRoutesContext(options: ResolvedOptions) {
170170
// unlinkDir event
171171
}
172172

173+
let lastAutoRoutes: string | undefined
173174
function generateRoutes() {
174175
const importsMap = new ImportsMap()
175176

@@ -181,14 +182,22 @@ export function createRoutesContext(options: ResolvedOptions) {
181182
// 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)
182183

183184
// generate the list of imports
184-
let imports = `${importsMap}`
185+
let imports = importsMap.toString()
185186
// add an empty line for readability
186187
if (imports) {
187188
imports += '\n'
188189
}
189190

191+
const newAutoRoutes = `${imports}${routesExport}\n`
192+
193+
if (lastAutoRoutes !== newAutoRoutes) {
194+
// cache hit, triigger HMR (not working yet)
195+
server?.updateRoutes()
196+
lastAutoRoutes = newAutoRoutes
197+
}
198+
190199
// prepend it to the code
191-
return `${imports}${routesExport}\n`
200+
return newAutoRoutes
192201
}
193202

194203
function generateDTS(): string {
@@ -224,12 +233,6 @@ export function createRoutesContext(options: ResolvedOptions) {
224233
await fs.writeFile(dts, content, 'utf-8')
225234
logger.timeLog('writeConfigFiles', 'wrote dts file')
226235
lastDTS = content
227-
228-
// update the files
229-
server && logger.log(`⚙️ Invalidating server "${MODULE_ROUTES_PATH}"`)
230-
// server?.invalidate(MODULE_ROUTES_PATH)
231-
server?.updateRoutes()
232-
// server?.reload()
233236
}
234237
}
235238
logger.timeEnd('writeConfigFiles')

0 commit comments

Comments
 (0)