@tanstack/router-plugin
Version:
Modern and scalable routing for React applications
1 lines • 3.44 kB
Source Map (JSON)
{"version":3,"file":"route-hmr-statement.cjs","names":[],"sources":["../../../src/core/route-hmr-statement.ts"],"sourcesContent":["import * as template from '@babel/template'\nimport type { AnyRoute, AnyRouteMatch, AnyRouter } from '@tanstack/router-core'\n\ntype AnyRouteWithPrivateProps = AnyRoute & {\n _path: string\n _id: string\n _fullPath: string\n _to: string\n}\n\nfunction handleRouteUpdate(\n oldRoute: AnyRouteWithPrivateProps,\n newRoute: AnyRouteWithPrivateProps,\n) {\n newRoute._path = oldRoute._path\n newRoute._id = oldRoute._id\n newRoute._fullPath = oldRoute._fullPath\n newRoute._to = oldRoute._to\n newRoute.children = oldRoute.children\n newRoute.parentRoute = oldRoute.parentRoute\n\n const router = window.__TSR_ROUTER__!\n router.routesById[newRoute.id] = newRoute\n router.routesByPath[newRoute.fullPath] = newRoute\n router.processedTree.matchCache.clear()\n router.processedTree.flatCache?.clear()\n router.processedTree.singleCache.clear()\n router.resolvePathCache.clear()\n // TODO: how to rebuild the tree if we add a new route?\n walkReplaceSegmentTree(newRoute, router.processedTree.segmentTree)\n const filter = (m: AnyRouteMatch) => m.routeId === oldRoute.id\n if (\n router.state.matches.find(filter) ||\n router.state.pendingMatches?.find(filter)\n ) {\n router.invalidate({ filter })\n }\n function walkReplaceSegmentTree(\n route: AnyRouteWithPrivateProps,\n node: AnyRouter['processedTree']['segmentTree'],\n ) {\n if (node.route?.id === route.id) node.route = route\n if (node.index) walkReplaceSegmentTree(route, node.index)\n node.static?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.staticInsensitive?.forEach((child) =>\n walkReplaceSegmentTree(route, child),\n )\n node.dynamic?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.optional?.forEach((child) => walkReplaceSegmentTree(route, child))\n node.wildcard?.forEach((child) => walkReplaceSegmentTree(route, child))\n }\n}\n\nexport const routeHmrStatement = template.statement(\n `\nif (import.meta.hot) {\n import.meta.hot.accept((newModule) => {\n if (Route && newModule && newModule.Route) {\n (${handleRouteUpdate.toString()})(Route, newModule.Route)\n }\n })\n}\n`,\n // Disable placeholder parsing so identifiers like __TSR_ROUTER__ are treated as normal identifiers instead of template placeholders\n { placeholderPattern: false },\n)()\n"],"mappings":";;;;AAUA,SAAS,kBACP,UACA,UACA;AACA,UAAS,QAAQ,SAAS;AAC1B,UAAS,MAAM,SAAS;AACxB,UAAS,YAAY,SAAS;AAC9B,UAAS,MAAM,SAAS;AACxB,UAAS,WAAW,SAAS;AAC7B,UAAS,cAAc,SAAS;CAEhC,MAAM,SAAS,OAAO;AACtB,QAAO,WAAW,SAAS,MAAM;AACjC,QAAO,aAAa,SAAS,YAAY;AACzC,QAAO,cAAc,WAAW,OAAO;AACvC,QAAO,cAAc,WAAW,OAAO;AACvC,QAAO,cAAc,YAAY,OAAO;AACxC,QAAO,iBAAiB,OAAO;AAE/B,wBAAuB,UAAU,OAAO,cAAc,YAAY;CAClE,MAAM,UAAU,MAAqB,EAAE,YAAY,SAAS;AAC5D,KACE,OAAO,MAAM,QAAQ,KAAK,OAAO,IACjC,OAAO,MAAM,gBAAgB,KAAK,OAAO,CAEzC,QAAO,WAAW,EAAE,QAAQ,CAAC;CAE/B,SAAS,uBACP,OACA,MACA;AACA,MAAI,KAAK,OAAO,OAAO,MAAM,GAAI,MAAK,QAAQ;AAC9C,MAAI,KAAK,MAAO,wBAAuB,OAAO,KAAK,MAAM;AACzD,OAAK,QAAQ,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACrE,OAAK,mBAAmB,SAAS,UAC/B,uBAAuB,OAAO,MAAM,CACrC;AACD,OAAK,SAAS,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACtE,OAAK,UAAU,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;AACvE,OAAK,UAAU,SAAS,UAAU,uBAAuB,OAAO,MAAM,CAAC;;;AAI3E,IAAa,oBAAoB,gBAAS,UACxC;;;;SAIO,kBAAkB,UAAU,CAAC;;;;GAMpC,EAAE,oBAAoB,OAAO,CAC9B,EAAE"}