UNPKG

@tanstack/router-generator

Version:

Modern and scalable routing for React applications

1 lines 6.2 kB
{"version":3,"file":"default-generator-plugin.cjs","sources":["../../../src/plugin/default-generator-plugin.ts"],"sourcesContent":["import { defaultTransformPlugin } from '../transform/default-transform-plugin'\nimport {\n checkRouteFullPathUniqueness,\n isRouteNodeValidForAugmentation,\n} from '../utils'\nimport type { ImportDeclaration } from '../types'\nimport type { GeneratorPluginWithTransform } from './types'\n\nconst EXPORT_NAME = 'Route'\nexport function defaultGeneratorPlugin(): GeneratorPluginWithTransform {\n return {\n name: 'default',\n transformPlugin: defaultTransformPlugin,\n imports: (opts) => {\n const imports: Array<ImportDeclaration> = []\n if (opts.acc.routeNodes.some((n) => n.isVirtual)) {\n imports.push({\n specifiers: [{ imported: 'createFileRoute' }],\n source: opts.generator.targetTemplate.fullPkg,\n })\n }\n if (opts.generator.config.verboseFileRoutes === false) {\n const typeImport: ImportDeclaration = {\n specifiers: [],\n source: opts.generator.targetTemplate.fullPkg,\n importKind: 'type',\n }\n if (\n opts.sortedRouteNodes.some(\n (d) =>\n isRouteNodeValidForAugmentation(d) && d._fsRouteType !== 'lazy',\n )\n ) {\n typeImport.specifiers.push({ imported: 'CreateFileRoute' })\n }\n if (\n opts.sortedRouteNodes.some(\n (node) =>\n opts.acc.routePiecesByPath[node.routePath!]?.lazy &&\n isRouteNodeValidForAugmentation(node),\n )\n ) {\n typeImport.specifiers.push({ imported: 'CreateLazyFileRoute' })\n }\n\n if (typeImport.specifiers.length > 0) {\n typeImport.specifiers.push({ imported: 'FileRoutesByPath' })\n imports.push(typeImport)\n }\n }\n const hasMatchingRouteFiles = opts.acc.routeNodes.length > 0\n if (hasMatchingRouteFiles) {\n // needs a virtual root route\n if (!opts.rootRouteNode.exports?.includes(EXPORT_NAME)) {\n imports.push({\n specifiers: [{ imported: 'createRootRoute' }],\n source: opts.generator.targetTemplate.fullPkg,\n })\n }\n }\n return imports\n },\n moduleAugmentation: ({ generator }) => ({\n module: generator.targetTemplate.fullPkg,\n interfaceName: 'FileRoutesByPath',\n }),\n onRouteTreesChanged: ({ routeTrees, generator }) => {\n const routeTree = routeTrees.find(\n (tree) => tree.exportName === EXPORT_NAME,\n )\n if (!routeTree) {\n throw new Error(\n 'No route tree found with export name \"Route\". Please ensure your routes are correctly defined.',\n )\n }\n checkRouteFullPathUniqueness(\n routeTree.sortedRouteNodes.filter(\n (d) =>\n d.children === undefined &&\n 'lazy' !== d._fsRouteType &&\n d.exports?.includes(EXPORT_NAME),\n ),\n generator.config,\n )\n },\n routeModuleAugmentation: ({ routeNode }) => {\n if (routeNode._fsRouteType === 'lazy') {\n return `const createLazyFileRoute: CreateLazyFileRoute<FileRoutesByPath['${routeNode.routePath}']['preLoaderRoute']>`\n } else {\n return `const createFileRoute: CreateFileRoute<'${routeNode.routePath}',\n FileRoutesByPath['${routeNode.routePath}']['parentRoute'],\n FileRoutesByPath['${routeNode.routePath}']['id'],\n FileRoutesByPath['${routeNode.routePath}']['path'],\n FileRoutesByPath['${routeNode.routePath}']['fullPath']\n >\n `\n }\n },\n createRootRouteCode: () => `createRootRoute()`,\n createVirtualRouteCode: ({ node }) =>\n `createFileRoute('${node.routePath}')()`,\n config: ({ sortedRouteNodes }) => {\n const hasMatchingRouteFiles = sortedRouteNodes.length > 0\n return {\n virtualRootRoute: hasMatchingRouteFiles,\n }\n },\n }\n}\n"],"names":["defaultTransformPlugin","isRouteNodeValidForAugmentation","_a","checkRouteFullPathUniqueness"],"mappings":";;;;AAQA,MAAM,cAAc;AACb,SAAS,yBAAuD;AAC9D,SAAA;AAAA,IACL,MAAM;AAAA,IACN,iBAAiBA,uBAAA;AAAA,IACjB,SAAS,CAAC,SAAS;;AACjB,YAAM,UAAoC,CAAC;AACvC,UAAA,KAAK,IAAI,WAAW,KAAK,CAAC,MAAM,EAAE,SAAS,GAAG;AAChD,gBAAQ,KAAK;AAAA,UACX,YAAY,CAAC,EAAE,UAAU,mBAAmB;AAAA,UAC5C,QAAQ,KAAK,UAAU,e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