UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

40 lines (39 loc) 1.19 kB
import { parseAst, generateFromAst, logDiff } from "@tanstack/router-utils"; import { routeHmrStatement } from "./route-hmr-statement.js"; import { debug } from "./utils.js"; const unpluginRouterHmrFactory = () => { return { name: "tanstack-router:hmr", enforce: "pre", transform: { filter: { // this is necessary for webpack / rspack to avoid matching .html files id: /\.(m|c)?(j|t)sx?$/, code: "createFileRoute(" }, handler(code, id) { var _a; if (!((_a = globalThis.TSR_ROUTES_BY_ID_MAP) == null ? void 0 : _a.has(id))) { return null; } if (debug) console.info("Adding HMR handling to route ", id); const ast = parseAst({ code }); ast.program.body.push(routeHmrStatement); const result = generateFromAst(ast, { sourceMaps: true, filename: id, sourceFileName: id }); if (debug) { logDiff(code, result.code); console.log("Output:\n", result.code + "\n\n"); } return result; } } }; }; export { unpluginRouterHmrFactory }; //# sourceMappingURL=router-hmr-plugin.js.map