UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

1 lines 2.63 kB
{"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 { getUniqueProgramIdentifier } from '../../utils'\nimport type { ReferenceRouteCompilerPlugin } from '../plugins'\n\nconst REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([\n 'component',\n 'pendingComponent',\n 'errorComponent',\n 'notFoundComponent',\n])\n\nexport function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {\n return {\n name: 'react-refresh-route-components',\n onUnsplittableRoute(ctx) {\n if (!ctx.opts.addHmr) {\n return\n }\n\n const hoistedDeclarations: Array<t.VariableDeclaration> = []\n\n ctx.routeOptions.properties.forEach((prop) => {\n if (!t.isObjectProperty(prop) || !t.isIdentifier(prop.key)) {\n return\n }\n\n if (!REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(prop.key.name)) {\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${prop.key.name[0]!.toUpperCase()}${prop.key.name.slice(1)}`,\n )\n\n hoistedDeclarations.push(\n t.variableDeclaration('const', [\n t.variableDeclarator(\n hoistedIdentifier,\n t.cloneNode(prop.value, true),\n ),\n ]),\n )\n\n prop.value = t.cloneNode(hoistedIdentifier)\n })\n\n if (hoistedDeclarations.length === 0) {\n return\n }\n\n ctx.insertionPath.insertBefore(hoistedDeclarations)\n return { modified: true }\n },\n }\n}\n"],"mappings":";;;;;AAIA,IAAM,uCAAuC,IAAI,IAAI;CACnD;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAgB,0CAAwE;AACtF,QAAO;EACL,MAAM;EACN,oBAAoB,KAAK;AACvB,OAAI,CAAC,IAAI,KAAK,OACZ;GAGF,MAAM,sBAAoD,EAAE;AAE5D,OAAI,aAAa,WAAW,SAAS,SAAS;AAC5C,QAAI,CAAC,aAAE,iBAAiB,KAAK,IAAI,CAAC,aAAE,aAAa,KAAK,IAAI,CACxD;AAGF,QAAI,CAAC,qCAAqC,IAAI,KAAK,IAAI,KAAK,CAC1D;AAGF,QACE,CAAC,aAAE,0BAA0B,KAAK,MAAM,IACxC,CAAC,aAAE,qBAAqB,KAAK,MAAM,CAEnC;IAGF,MAAM,oBAAoB,cAAA,2BACxB,IAAI,aACJ,MAAM,KAAK,IAAI,KAAK,GAAI,aAAa,GAAG,KAAK,IAAI,KAAK,MAAM,EAAE,GAC/D;AAED,wBAAoB,KAClB,aAAE,oBAAoB,SAAS,CAC7B,aAAE,mBACA,mBACA,aAAE,UAAU,KAAK,OAAO,KAAK,CAC9B,CACF,CAAC,CACH;AAED,SAAK,QAAQ,aAAE,UAAU,kBAAkB;KAC3C;AAEF,OAAI,oBAAoB,WAAW,EACjC;AAGF,OAAI,cAAc,aAAa,oBAAoB;AACnD,UAAO,EAAE,UAAU,MAAM;;EAE5B"}