@tanstack/router-plugin
Version:
Modern and scalable routing for React applications
1 lines • 2.19 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 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,gBAAgB,CAAC,SAAS,QAAQ,IAAI,eAAe;;;;;;;;AAShE,SAAgB,cAAc,MAAsB;AAClD,QAAO,KAAK,QAAQ,OAAO,IAAI;;AAGjC,SAAgB,yBACd,MACoB;AACpB,KAAI,KAAK,SACP;AAGF,KAAI,aAAE,aAAa,KAAK,IAAI,CAC1B,QAAO,KAAK,IAAI;AAGlB,KAAI,aAAE,gBAAgB,KAAK,IAAI,CAC7B,QAAO,KAAK,IAAI;;AAMpB,SAAgB,2BACd,aACA,UACc;CACd,IAAI,OAAO;CACX,IAAI,SAAS;CAEb,MAAM,eAAe,YAAY,MAAM,kBAAkB;AAEzD,QACE,aAAa,WAAW,KAAK,IAC7B,aAAa,UAAU,KAAK,IAC5B,aAAa,aAAa,KAAK,EAC/B;AACA,SAAO,GAAG,WAAW;AACrB;;AAKF,cAAa,WAAW,QAAQ;AAEhC,QAAO,aAAE,WAAW,KAAK"}