@tanstack/router-plugin
Version:
Modern and scalable routing for React applications
1 lines • 2.41 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":[],"sources":["../../../src/core/utils.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type babel from '@babel/core'\n\nexport const debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'router-plugin'].includes(process.env.TSR_VITE_DEBUG)\n\n/**\n * Normalizes a file path by converting Windows backslashes to forward slashes.\n * This ensures consistent path handling across different bundlers and operating systems.\n *\n * The route generator stores paths with forward slashes, but rspack/webpack on Windows\n * pass native paths with backslashes to transform handlers.\n */\nexport function normalizePath(path: string): string {\n return path.replace(/\\\\/g, '/')\n}\n\nexport const routeFactoryCallCodeFilter = [\n /\\bcreateFileRoute\\s*\\(/,\n /\\bcreateRootRoute\\s*\\(/,\n /\\bcreateRootRouteWithContext\\s*(?:<|\\()/,\n]\n\nexport function getObjectPropertyKeyName(\n prop: t.ObjectProperty,\n): string | undefined {\n if (prop.computed) {\n return undefined\n }\n\n if (t.isIdentifier(prop.key)) {\n return prop.key.name\n }\n\n if (t.isStringLiteral(prop.key)) {\n return prop.key.value\n }\n\n return undefined\n}\n\nexport function getUniqueProgramIdentifier(\n programPath: babel.NodePath<t.Program>,\n baseName: string,\n): t.Identifier {\n let name = baseName\n let suffix = 2\n\n const programScope = programPath.scope.getProgramParent()\n\n while (\n programScope.hasBinding(name) ||\n programScope.hasGlobal(name) ||\n programScope.hasReference(name)\n ) {\n name = `${baseName}${suffix}`\n suffix++\n }\n\n // Register the name so subsequent calls within the same traversal\n // see it and avoid collisions\n programScope.references[name] = true\n\n return t.identifier(name)\n}\n"],"mappings":";;;;AAGA,IAAa,QACX,QAAQ,IAAI,kBACZ,CAAC,QAAQ,eAAe,EAAE,SAAS,QAAQ,IAAI,cAAc;;;;;;;;AAS/D,SAAgB,cAAc,MAAsB;CAClD,OAAO,KAAK,QAAQ,OAAO,GAAG;AAChC;AAEA,IAAa,6BAA6B;CACxC;CACA;CACA;AACF;AAEA,SAAgB,yBACd,MACoB;CACpB,IAAI,KAAK,UACP;CAGF,IAAI,aAAE,aAAa,KAAK,GAAG,GACzB,OAAO,KAAK,IAAI;CAGlB,IAAI,aAAE,gBAAgB,KAAK,GAAG,GAC5B,OAAO,KAAK,IAAI;AAIpB;AAEA,SAAgB,2BACd,aACA,UACc;CACd,IAAI,OAAO;CACX,IAAI,SAAS;CAEb,MAAM,eAAe,YAAY,MAAM,iBAAiB;CAExD,OACE,aAAa,WAAW,IAAI,KAC5B,aAAa,UAAU,IAAI,KAC3B,aAAa,aAAa,IAAI,GAC9B;EACA,OAAO,GAAG,WAAW;EACrB;CACF;CAIA,aAAa,WAAW,QAAQ;CAEhC,OAAO,aAAE,WAAW,IAAI;AAC1B"}