UNPKG

@tanstack/router-plugin

Version:

Modern and scalable routing for React applications

1 lines 3.33 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 {\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;CACD,CAAC;AAEF,SAAS,2BAA2B,KAIjC;CACD,MAAM,sBAAoD,EAAE;AAE5D,KAAI,aAAa,WAAW,SAAS,SAAS;AAC5C,MAAI,CAAC,aAAE,iBAAiB,KAAK,CAC3B;EAGF,MAAM,MAAM,cAAA,yBAAyB,KAAK;AAE1C,MAAI,CAAC,OAAO,CAAC,qCAAqC,IAAI,IAAI,CACxD;AAGF,MACE,CAAC,aAAE,0BAA0B,KAAK,MAAM,IACxC,CAAC,aAAE,qBAAqB,KAAK,MAAM,CAEnC;EAGF,MAAM,oBAAoB,cAAA,2BACxB,IAAI,aACJ,MAAM,IAAI,GAAI,aAAa,GAAG,IAAI,MAAM,EAAE,GAC3C;AAED,sBAAoB,KAClB,aAAE,oBAAoB,SAAS,CAC7B,aAAE,mBAAmB,mBAAmB,aAAE,UAAU,KAAK,OAAO,KAAK,CAAC,CACvE,CAAC,CACH;AAED,OAAK,QAAQ,aAAE,UAAU,kBAAkB;GAC3C;AAEF,KAAI,oBAAoB,WAAW,EACjC,QAAO;AAGT,KAAI,cAAc,aAAa,oBAAoB;AACnD,QAAO;;AAGT,SAAgB,0CAAwE;AACtF,QAAO;EACL,MAAM;EACN,2BAA2B;AACzB,UAAO,CAAC,GAAG,qCAAqC;;EAElD,oBAAoB,KAAK;AACvB,OAAI,CAAC,IAAI,KAAK,OACZ;AAGF,OAAI,2BAA2B,IAAI,CACjC,QAAO,EAAE,UAAU,MAAM;;EAK7B,SAAS,KAAK;AACZ,OAAI,CAAC,IAAI,KAAK,OACZ;AAGF,OAAI,2BAA2B,IAAI,CACjC,QAAO,EAAE,UAAU,MAAM;;EAK9B"}