|
1 | 1 | import * as template from '@babel/template' |
| 2 | +import type { AnyRoute } from '@tanstack/router-core' |
| 3 | + |
| 4 | +type AnyRouteWithPrivateProps = AnyRoute & { |
| 5 | + _path: string |
| 6 | + _id: string |
| 7 | + _fullPath: string |
| 8 | + _to: string |
| 9 | +} |
| 10 | + |
| 11 | +function handleRouteUpdate( |
| 12 | + oldRoute: AnyRouteWithPrivateProps, |
| 13 | + newRoute: AnyRouteWithPrivateProps, |
| 14 | +) { |
| 15 | + newRoute._path = oldRoute._path |
| 16 | + newRoute._id = oldRoute._id |
| 17 | + newRoute._fullPath = oldRoute._fullPath |
| 18 | + newRoute._to = oldRoute._to |
| 19 | + newRoute.children = oldRoute.children |
| 20 | + newRoute.parentRoute = oldRoute.parentRoute |
| 21 | + |
| 22 | + const router = window.__TSR_ROUTER__! |
| 23 | + router.routesById[newRoute.id] = newRoute |
| 24 | + router.routesByPath[newRoute.fullPath] = newRoute |
| 25 | + const oldRouteIndex = router.flatRoutes.indexOf(oldRoute) |
| 26 | + if (oldRouteIndex > -1) { |
| 27 | + router.flatRoutes[oldRouteIndex] = newRoute |
| 28 | + } |
| 29 | + router.invalidate({ filter: (m) => m.routeId === oldRoute.id }) |
| 30 | +} |
2 | 31 |
|
3 | 32 | export const routeHmrStatement = template.statement( |
4 | 33 | ` |
5 | 34 | if (import.meta.hot) { |
6 | 35 | import.meta.hot.accept((newModule) => { |
7 | | - if (newModule && newModule.Route && typeof newModule.Route.clone === 'function') { |
8 | | - newModule.Route.clone(Route) |
| 36 | + if (Route && newModule && newModule.Route) { |
| 37 | + (${handleRouteUpdate.toString()})(Route, newModule.Route) |
9 | 38 | } |
10 | 39 | }) |
11 | 40 | } |
12 | 41 | `, |
| 42 | + // Disable placeholder parsing so identifiers like __TSR_ROUTER__ are treated as normal identifiers instead of template placeholders |
| 43 | + { placeholderPattern: false }, |
13 | 44 | )() |
0 commit comments