@tanstack/router-plugin
Version:
Modern and scalable routing for React applications
1 lines • 3.36 kB
Source Map (JSON)
{"version":3,"file":"react-refresh-route-components.cjs","names":[],"sources":["../../../../../src/core/code-splitter/plugins/react-refresh-route-components.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n getObjectPropertyKeyName,\n getUniqueProgramIdentifier,\n} from '../../utils'\nimport type { ReferenceRouteCompilerPlugin } from '../plugins'\n\nconst REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([\n 'component',\n 'shellComponent',\n 'pendingComponent',\n 'errorComponent',\n 'notFoundComponent',\n])\n\nfunction hoistInlineRouteComponents(ctx: {\n programPath: Parameters<typeof getUniqueProgramIdentifier>[0]\n insertionPath: { insertBefore: (nodes: Array<t.VariableDeclaration>) => void }\n routeOptions: t.ObjectExpression\n}) {\n const hoistedDeclarations: Array<t.VariableDeclaration> = []\n\n ctx.routeOptions.properties.forEach((prop) => {\n if (!t.isObjectProperty(prop)) {\n return\n }\n\n const key = getObjectPropertyKeyName(prop)\n\n if (!key || !REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(key)) {\n return\n }\n\n if (\n !t.isArrowFunctionExpression(prop.value) &&\n !t.isFunctionExpression(prop.value)\n ) {\n return\n }\n\n const hoistedIdentifier = getUniqueProgramIdentifier(\n ctx.programPath,\n `TSR${key[0]!.toUpperCase()}${key.slice(1)}`,\n )\n\n hoistedDeclarations.push(\n t.variableDeclaration('const', [\n t.variableDeclarator(hoistedIdentifier, t.cloneNode(prop.value, true)),\n ]),\n )\n\n prop.value = t.cloneNode(hoistedIdentifier)\n })\n\n if (hoistedDeclarations.length === 0) {\n return false\n }\n\n ctx.insertionPath.insertBefore(hoistedDeclarations)\n return true\n}\n\nexport function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {\n return {\n name: 'react-refresh-route-components',\n getStableRouteOptionKeys() {\n return [...REACT_REFRESH_ROUTE_COMPONENT_IDENTS]\n },\n onUnsplittableRoute(ctx) {\n if (!ctx.opts.addHmr) {\n return\n }\n\n if (hoistInlineRouteComponents(ctx)) {\n return { modified: true }\n }\n\n return\n },\n onAddHmr(ctx) {\n if (!ctx.opts.addHmr) {\n return\n }\n\n if (hoistInlineRouteComponents(ctx)) {\n return { modified: true }\n }\n\n return\n },\n }\n}\n"],"mappings":";;;;;AAOA,IAAM,uCAAuC,IAAI,IAAI;CACnD;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,SAAS,2BAA2B,KAIjC;CACD,MAAM,sBAAoD,CAAC;CAE3D,IAAI,aAAa,WAAW,SAAS,SAAS;EAC5C,IAAI,CAAC,aAAE,iBAAiB,IAAI,GAC1B;EAGF,MAAM,MAAM,cAAA,yBAAyB,IAAI;EAEzC,IAAI,CAAC,OAAO,CAAC,qCAAqC,IAAI,GAAG,GACvD;EAGF,IACE,CAAC,aAAE,0BAA0B,KAAK,KAAK,KACvC,CAAC,aAAE,qBAAqB,KAAK,KAAK,GAElC;EAGF,MAAM,oBAAoB,cAAA,2BACxB,IAAI,aACJ,MAAM,IAAI,GAAI,YAAY,IAAI,IAAI,MAAM,CAAC,GAC3C;EAEA,oBAAoB,KAClB,aAAE,oBAAoB,SAAS,CAC7B,aAAE,mBAAmB,mBAAmB,aAAE,UAAU,KAAK,OAAO,IAAI,CAAC,CACvE,CAAC,CACH;EAEA,KAAK,QAAQ,aAAE,UAAU,iBAAiB;CAC5C,CAAC;CAED,IAAI,oBAAoB,WAAW,GACjC,OAAO;CAGT,IAAI,cAAc,aAAa,mBAAmB;CAClD,OAAO;AACT;AAEA,SAAgB,0CAAwE;CACtF,OAAO;EACL,MAAM;EACN,2BAA2B;GACzB,OAAO,CAAC,GAAG,oCAAoC;EACjD;EACA,oBAAoB,KAAK;GACvB,IAAI,CAAC,IAAI,KAAK,QACZ;GAGF,IAAI,2BAA2B,GAAG,GAChC,OAAO,EAAE,UAAU,KAAK;EAI5B;EACA,SAAS,KAAK;GACZ,IAAI,CAAC,IAAI,KAAK,QACZ;GAGF,IAAI,2BAA2B,GAAG,GAChC,OAAO,EAAE,UAAU,KAAK;EAI5B;CACF;AACF"}