@rangojs/router 0.0.0-experimental.8 → 0.0.0-experimental.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (312) hide show
  1. package/AGENTS.md +9 -0
  2. package/README.md +942 -4
  3. package/dist/bin/rango.js +1689 -0
  4. package/dist/vite/index.js +4960 -935
  5. package/package.json +70 -60
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +294 -0
  8. package/skills/caching/SKILL.md +93 -23
  9. package/skills/composability/SKILL.md +172 -0
  10. package/skills/debug-manifest/SKILL.md +12 -8
  11. package/skills/document-cache/SKILL.md +18 -16
  12. package/skills/fonts/SKILL.md +167 -0
  13. package/skills/handler-use/SKILL.md +362 -0
  14. package/skills/hooks/SKILL.md +334 -72
  15. package/skills/host-router/SKILL.md +218 -0
  16. package/skills/intercept/SKILL.md +151 -8
  17. package/skills/layout/SKILL.md +122 -3
  18. package/skills/links/SKILL.md +92 -31
  19. package/skills/loader/SKILL.md +404 -44
  20. package/skills/middleware/SKILL.md +205 -37
  21. package/skills/migrate-nextjs/SKILL.md +560 -0
  22. package/skills/migrate-react-router/SKILL.md +764 -0
  23. package/skills/mime-routes/SKILL.md +128 -0
  24. package/skills/parallel/SKILL.md +263 -1
  25. package/skills/prerender/SKILL.md +685 -0
  26. package/skills/rango/SKILL.md +87 -16
  27. package/skills/response-routes/SKILL.md +411 -0
  28. package/skills/route/SKILL.md +281 -14
  29. package/skills/router-setup/SKILL.md +210 -32
  30. package/skills/tailwind/SKILL.md +129 -0
  31. package/skills/theme/SKILL.md +9 -8
  32. package/skills/typesafety/SKILL.md +328 -89
  33. package/skills/use-cache/SKILL.md +324 -0
  34. package/src/__internal.ts +102 -4
  35. package/src/bin/rango.ts +321 -0
  36. package/src/browser/action-coordinator.ts +97 -0
  37. package/src/browser/action-response-classifier.ts +99 -0
  38. package/src/browser/app-version.ts +14 -0
  39. package/src/browser/event-controller.ts +92 -64
  40. package/src/browser/history-state.ts +80 -0
  41. package/src/browser/intercept-utils.ts +52 -0
  42. package/src/browser/link-interceptor.ts +24 -4
  43. package/src/browser/logging.ts +55 -0
  44. package/src/browser/merge-segment-loaders.ts +20 -12
  45. package/src/browser/navigation-bridge.ts +317 -560
  46. package/src/browser/navigation-client.ts +206 -68
  47. package/src/browser/navigation-store.ts +73 -55
  48. package/src/browser/navigation-transaction.ts +297 -0
  49. package/src/browser/network-error-handler.ts +61 -0
  50. package/src/browser/partial-update.ts +343 -316
  51. package/src/browser/prefetch/cache.ts +216 -0
  52. package/src/browser/prefetch/fetch.ts +206 -0
  53. package/src/browser/prefetch/observer.ts +65 -0
  54. package/src/browser/prefetch/policy.ts +48 -0
  55. package/src/browser/prefetch/queue.ts +160 -0
  56. package/src/browser/prefetch/resource-ready.ts +77 -0
  57. package/src/browser/rango-state.ts +112 -0
  58. package/src/browser/react/Link.tsx +253 -74
  59. package/src/browser/react/NavigationProvider.tsx +87 -11
  60. package/src/browser/react/context.ts +11 -0
  61. package/src/browser/react/filter-segment-order.ts +11 -0
  62. package/src/browser/react/index.ts +12 -12
  63. package/src/browser/react/location-state-shared.ts +95 -53
  64. package/src/browser/react/location-state.ts +60 -15
  65. package/src/browser/react/mount-context.ts +6 -1
  66. package/src/browser/react/nonce-context.ts +23 -0
  67. package/src/browser/react/shallow-equal.ts +27 -0
  68. package/src/browser/react/use-action.ts +29 -51
  69. package/src/browser/react/use-client-cache.ts +5 -3
  70. package/src/browser/react/use-handle.ts +30 -126
  71. package/src/browser/react/use-href.tsx +2 -2
  72. package/src/browser/react/use-link-status.ts +6 -5
  73. package/src/browser/react/use-navigation.ts +44 -65
  74. package/src/browser/react/use-params.ts +65 -0
  75. package/src/browser/react/use-pathname.ts +47 -0
  76. package/src/browser/react/use-router.ts +76 -0
  77. package/src/browser/react/use-search-params.ts +56 -0
  78. package/src/browser/react/use-segments.ts +80 -97
  79. package/src/browser/response-adapter.ts +73 -0
  80. package/src/browser/rsc-router.tsx +214 -58
  81. package/src/browser/scroll-restoration.ts +127 -52
  82. package/src/browser/segment-reconciler.ts +243 -0
  83. package/src/browser/segment-structure-assert.ts +16 -0
  84. package/src/browser/server-action-bridge.ts +510 -603
  85. package/src/browser/shallow.ts +6 -1
  86. package/src/browser/types.ts +141 -48
  87. package/src/browser/validate-redirect-origin.ts +29 -0
  88. package/src/build/generate-manifest.ts +235 -24
  89. package/src/build/generate-route-types.ts +39 -0
  90. package/src/build/index.ts +13 -0
  91. package/src/build/route-trie.ts +291 -0
  92. package/src/build/route-types/ast-helpers.ts +25 -0
  93. package/src/build/route-types/ast-route-extraction.ts +98 -0
  94. package/src/build/route-types/codegen.ts +102 -0
  95. package/src/build/route-types/include-resolution.ts +418 -0
  96. package/src/build/route-types/param-extraction.ts +48 -0
  97. package/src/build/route-types/per-module-writer.ts +128 -0
  98. package/src/build/route-types/router-processing.ts +618 -0
  99. package/src/build/route-types/scan-filter.ts +85 -0
  100. package/src/build/runtime-discovery.ts +231 -0
  101. package/src/cache/background-task.ts +34 -0
  102. package/src/cache/cache-key-utils.ts +44 -0
  103. package/src/cache/cache-policy.ts +125 -0
  104. package/src/cache/cache-runtime.ts +342 -0
  105. package/src/cache/cache-scope.ts +167 -309
  106. package/src/cache/cf/cf-cache-store.ts +571 -17
  107. package/src/cache/cf/index.ts +13 -3
  108. package/src/cache/document-cache.ts +116 -77
  109. package/src/cache/handle-capture.ts +81 -0
  110. package/src/cache/handle-snapshot.ts +41 -0
  111. package/src/cache/index.ts +1 -15
  112. package/src/cache/memory-segment-store.ts +191 -13
  113. package/src/cache/profile-registry.ts +73 -0
  114. package/src/cache/read-through-swr.ts +134 -0
  115. package/src/cache/segment-codec.ts +256 -0
  116. package/src/cache/taint.ts +153 -0
  117. package/src/cache/types.ts +72 -122
  118. package/src/client.rsc.tsx +3 -1
  119. package/src/client.tsx +135 -301
  120. package/src/component-utils.ts +4 -4
  121. package/src/components/DefaultDocument.tsx +5 -1
  122. package/src/context-var.ts +156 -0
  123. package/src/debug.ts +19 -9
  124. package/src/errors.ts +108 -2
  125. package/src/handle.ts +55 -29
  126. package/src/handles/MetaTags.tsx +73 -20
  127. package/src/handles/breadcrumbs.ts +66 -0
  128. package/src/handles/index.ts +1 -0
  129. package/src/handles/meta.ts +30 -13
  130. package/src/host/cookie-handler.ts +21 -15
  131. package/src/host/errors.ts +8 -8
  132. package/src/host/index.ts +4 -7
  133. package/src/host/pattern-matcher.ts +27 -27
  134. package/src/host/router.ts +61 -39
  135. package/src/host/testing.ts +8 -8
  136. package/src/host/types.ts +15 -7
  137. package/src/host/utils.ts +1 -1
  138. package/src/href-client.ts +119 -29
  139. package/src/index.rsc.ts +155 -19
  140. package/src/index.ts +251 -30
  141. package/src/internal-debug.ts +11 -0
  142. package/src/loader.rsc.ts +26 -157
  143. package/src/loader.ts +27 -10
  144. package/src/network-error-thrower.tsx +3 -1
  145. package/src/outlet-provider.tsx +45 -0
  146. package/src/prerender/param-hash.ts +37 -0
  147. package/src/prerender/store.ts +186 -0
  148. package/src/prerender.ts +524 -0
  149. package/src/reverse.ts +354 -0
  150. package/src/root-error-boundary.tsx +41 -29
  151. package/src/route-content-wrapper.tsx +7 -4
  152. package/src/route-definition/dsl-helpers.ts +1121 -0
  153. package/src/route-definition/helper-factories.ts +200 -0
  154. package/src/route-definition/helpers-types.ts +478 -0
  155. package/src/route-definition/index.ts +55 -0
  156. package/src/route-definition/redirect.ts +101 -0
  157. package/src/route-definition/resolve-handler-use.ts +149 -0
  158. package/src/route-definition.ts +1 -1428
  159. package/src/route-map-builder.ts +217 -123
  160. package/src/route-name.ts +53 -0
  161. package/src/route-types.ts +77 -8
  162. package/src/router/content-negotiation.ts +215 -0
  163. package/src/router/debug-manifest.ts +72 -0
  164. package/src/router/error-handling.ts +9 -9
  165. package/src/router/find-match.ts +160 -0
  166. package/src/router/handler-context.ts +438 -86
  167. package/src/router/intercept-resolution.ts +402 -0
  168. package/src/router/lazy-includes.ts +237 -0
  169. package/src/router/loader-resolution.ts +356 -128
  170. package/src/router/logging.ts +251 -0
  171. package/src/router/manifest.ts +163 -35
  172. package/src/router/match-api.ts +555 -0
  173. package/src/router/match-context.ts +5 -3
  174. package/src/router/match-handlers.ts +440 -0
  175. package/src/router/match-middleware/background-revalidation.ts +108 -93
  176. package/src/router/match-middleware/cache-lookup.ts +460 -10
  177. package/src/router/match-middleware/cache-store.ts +98 -26
  178. package/src/router/match-middleware/intercept-resolution.ts +57 -17
  179. package/src/router/match-middleware/segment-resolution.ts +80 -6
  180. package/src/router/match-pipelines.ts +10 -45
  181. package/src/router/match-result.ts +135 -35
  182. package/src/router/metrics.ts +240 -15
  183. package/src/router/middleware-cookies.ts +55 -0
  184. package/src/router/middleware-types.ts +220 -0
  185. package/src/router/middleware.ts +324 -369
  186. package/src/router/navigation-snapshot.ts +182 -0
  187. package/src/router/pattern-matching.ts +211 -43
  188. package/src/router/prerender-match.ts +502 -0
  189. package/src/router/preview-match.ts +98 -0
  190. package/src/router/request-classification.ts +310 -0
  191. package/src/router/revalidation.ts +137 -38
  192. package/src/router/route-snapshot.ts +245 -0
  193. package/src/router/router-context.ts +41 -21
  194. package/src/router/router-interfaces.ts +484 -0
  195. package/src/router/router-options.ts +618 -0
  196. package/src/router/router-registry.ts +24 -0
  197. package/src/router/segment-resolution/fresh.ts +748 -0
  198. package/src/router/segment-resolution/helpers.ts +268 -0
  199. package/src/router/segment-resolution/loader-cache.ts +199 -0
  200. package/src/router/segment-resolution/revalidation.ts +1379 -0
  201. package/src/router/segment-resolution/static-store.ts +67 -0
  202. package/src/router/segment-resolution.ts +21 -0
  203. package/src/router/segment-wrappers.ts +291 -0
  204. package/src/router/telemetry-otel.ts +299 -0
  205. package/src/router/telemetry.ts +300 -0
  206. package/src/router/timeout.ts +148 -0
  207. package/src/router/trie-matching.ts +239 -0
  208. package/src/router/types.ts +78 -3
  209. package/src/router.ts +740 -4252
  210. package/src/rsc/handler-context.ts +45 -0
  211. package/src/rsc/handler.ts +907 -797
  212. package/src/rsc/helpers.ts +140 -6
  213. package/src/rsc/index.ts +0 -20
  214. package/src/rsc/loader-fetch.ts +229 -0
  215. package/src/rsc/manifest-init.ts +90 -0
  216. package/src/rsc/nonce.ts +14 -0
  217. package/src/rsc/origin-guard.ts +141 -0
  218. package/src/rsc/progressive-enhancement.ts +391 -0
  219. package/src/rsc/response-error.ts +37 -0
  220. package/src/rsc/response-route-handler.ts +347 -0
  221. package/src/rsc/rsc-rendering.ts +246 -0
  222. package/src/rsc/runtime-warnings.ts +42 -0
  223. package/src/rsc/server-action.ts +356 -0
  224. package/src/rsc/ssr-setup.ts +128 -0
  225. package/src/rsc/types.ts +46 -11
  226. package/src/search-params.ts +230 -0
  227. package/src/segment-content-promise.ts +67 -0
  228. package/src/segment-loader-promise.ts +122 -0
  229. package/src/segment-system.tsx +134 -36
  230. package/src/server/context.ts +341 -61
  231. package/src/server/cookie-store.ts +190 -0
  232. package/src/server/fetchable-loader-store.ts +37 -0
  233. package/src/server/handle-store.ts +113 -15
  234. package/src/server/loader-registry.ts +24 -64
  235. package/src/server/request-context.ts +607 -81
  236. package/src/server.ts +35 -130
  237. package/src/ssr/index.tsx +103 -30
  238. package/src/static-handler.ts +126 -0
  239. package/src/theme/ThemeProvider.tsx +21 -15
  240. package/src/theme/ThemeScript.tsx +5 -5
  241. package/src/theme/constants.ts +5 -2
  242. package/src/theme/index.ts +4 -14
  243. package/src/theme/theme-context.ts +4 -30
  244. package/src/theme/theme-script.ts +21 -18
  245. package/src/types/boundaries.ts +158 -0
  246. package/src/types/cache-types.ts +198 -0
  247. package/src/types/error-types.ts +192 -0
  248. package/src/types/global-namespace.ts +100 -0
  249. package/src/types/handler-context.ts +791 -0
  250. package/src/types/index.ts +88 -0
  251. package/src/types/loader-types.ts +210 -0
  252. package/src/types/route-config.ts +170 -0
  253. package/src/types/route-entry.ts +120 -0
  254. package/src/types/segments.ts +150 -0
  255. package/src/types.ts +1 -1623
  256. package/src/urls/include-helper.ts +207 -0
  257. package/src/urls/index.ts +53 -0
  258. package/src/urls/path-helper-types.ts +372 -0
  259. package/src/urls/path-helper.ts +364 -0
  260. package/src/urls/pattern-types.ts +107 -0
  261. package/src/urls/response-types.ts +116 -0
  262. package/src/urls/type-extraction.ts +372 -0
  263. package/src/urls/urls-function.ts +98 -0
  264. package/src/urls.ts +1 -802
  265. package/src/use-loader.tsx +161 -81
  266. package/src/vite/discovery/bundle-postprocess.ts +181 -0
  267. package/src/vite/discovery/discover-routers.ts +348 -0
  268. package/src/vite/discovery/prerender-collection.ts +439 -0
  269. package/src/vite/discovery/route-types-writer.ts +258 -0
  270. package/src/vite/discovery/self-gen-tracking.ts +47 -0
  271. package/src/vite/discovery/state.ts +117 -0
  272. package/src/vite/discovery/virtual-module-codegen.ts +203 -0
  273. package/src/vite/index.ts +15 -1133
  274. package/src/vite/plugin-types.ts +103 -0
  275. package/src/vite/plugins/cjs-to-esm.ts +93 -0
  276. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  277. package/src/vite/plugins/client-ref-hashing.ts +105 -0
  278. package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +72 -53
  279. package/src/vite/plugins/expose-id-utils.ts +299 -0
  280. package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
  281. package/src/vite/plugins/expose-ids/handler-transform.ts +209 -0
  282. package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
  283. package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
  284. package/src/vite/plugins/expose-ids/types.ts +45 -0
  285. package/src/vite/plugins/expose-internal-ids.ts +786 -0
  286. package/src/vite/plugins/performance-tracks.ts +88 -0
  287. package/src/vite/plugins/refresh-cmd.ts +127 -0
  288. package/src/vite/plugins/use-cache-transform.ts +323 -0
  289. package/src/vite/plugins/version-injector.ts +83 -0
  290. package/src/vite/plugins/version-plugin.ts +266 -0
  291. package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
  292. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  293. package/src/vite/rango.ts +462 -0
  294. package/src/vite/router-discovery.ts +918 -0
  295. package/src/vite/utils/ast-handler-extract.ts +517 -0
  296. package/src/vite/utils/banner.ts +36 -0
  297. package/src/vite/utils/bundle-analysis.ts +137 -0
  298. package/src/vite/utils/manifest-utils.ts +70 -0
  299. package/src/vite/{package-resolution.ts → utils/package-resolution.ts} +25 -29
  300. package/src/vite/utils/prerender-utils.ts +221 -0
  301. package/src/vite/utils/shared-utils.ts +170 -0
  302. package/CLAUDE.md +0 -43
  303. package/src/browser/lru-cache.ts +0 -69
  304. package/src/browser/request-controller.ts +0 -164
  305. package/src/cache/memory-store.ts +0 -253
  306. package/src/href-context.ts +0 -33
  307. package/src/href.ts +0 -255
  308. package/src/server/route-manifest-cache.ts +0 -173
  309. package/src/vite/expose-handle-id.ts +0 -209
  310. package/src/vite/expose-loader-id.ts +0 -426
  311. package/src/vite/expose-location-state-id.ts +0 -177
  312. /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
@@ -0,0 +1,1689 @@
1
+ #!/usr/bin/env node
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
+ };
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+
12
+ // src/build/route-types/param-extraction.ts
13
+ function extractParamsFromPattern(pattern) {
14
+ const params = {};
15
+ const regex = /:([a-zA-Z_$][\w$]*)(?:\([^)]+\))?(\?)?/g;
16
+ let match;
17
+ while ((match = regex.exec(pattern)) !== null) {
18
+ params[match[1]] = match[2] ? "string?" : "string";
19
+ }
20
+ return Object.keys(params).length > 0 ? params : void 0;
21
+ }
22
+ function formatRouteEntry(key, pattern, _params, search) {
23
+ const hasSearch = search && Object.keys(search).length > 0;
24
+ if (!hasSearch) {
25
+ return ` ${key}: "${pattern}",`;
26
+ }
27
+ const searchBody = Object.entries(search).map(([k, v]) => `${k}: "${v}"`).join(", ");
28
+ return ` ${key}: { path: "${pattern}", search: { ${searchBody} } },`;
29
+ }
30
+ var init_param_extraction = __esm({
31
+ "src/build/route-types/param-extraction.ts"() {
32
+ "use strict";
33
+ }
34
+ });
35
+
36
+ // src/build/route-types/ast-helpers.ts
37
+ import ts from "typescript";
38
+ function getStringValue(node) {
39
+ if (ts.isStringLiteral(node)) return node.text;
40
+ if (ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
41
+ return null;
42
+ }
43
+ function extractObjectStringProperties(node) {
44
+ const result = {};
45
+ for (const prop of node.properties) {
46
+ if (!ts.isPropertyAssignment(prop)) continue;
47
+ const key = ts.isIdentifier(prop.name) ? prop.name.text : ts.isStringLiteral(prop.name) ? prop.name.text : null;
48
+ if (!key) continue;
49
+ const val = getStringValue(prop.initializer);
50
+ if (val !== null) result[key] = val;
51
+ }
52
+ return result;
53
+ }
54
+ var init_ast_helpers = __esm({
55
+ "src/build/route-types/ast-helpers.ts"() {
56
+ "use strict";
57
+ }
58
+ });
59
+
60
+ // src/build/route-types/ast-route-extraction.ts
61
+ import ts2 from "typescript";
62
+ function extractRoutesFromSource(code) {
63
+ const sourceFile = ts2.createSourceFile(
64
+ "input.tsx",
65
+ code,
66
+ ts2.ScriptTarget.Latest,
67
+ true,
68
+ ts2.ScriptKind.TSX
69
+ );
70
+ const routes = [];
71
+ function visit(node) {
72
+ if (ts2.isCallExpression(node)) {
73
+ const callee = node.expression;
74
+ const isPath = ts2.isIdentifier(callee) && callee.text === "path" || ts2.isPropertyAccessExpression(callee) && ts2.isIdentifier(callee.expression) && callee.expression.text === "path";
75
+ if (isPath && node.arguments.length >= 1) {
76
+ const route = extractRouteFromCallExpression(node);
77
+ if (route) routes.push(route);
78
+ }
79
+ }
80
+ ts2.forEachChild(node, visit);
81
+ }
82
+ visit(sourceFile);
83
+ return routes;
84
+ }
85
+ function extractRouteFromCallExpression(node) {
86
+ const patternNode = node.arguments[0];
87
+ const pattern = getStringValue(patternNode);
88
+ if (pattern === null) return null;
89
+ let name = null;
90
+ let search;
91
+ for (let i = 1; i < node.arguments.length; i++) {
92
+ const arg = node.arguments[i];
93
+ if (ts2.isObjectLiteralExpression(arg)) {
94
+ for (const prop of arg.properties) {
95
+ if (!ts2.isPropertyAssignment(prop)) continue;
96
+ const propName = ts2.isIdentifier(prop.name) ? prop.name.text : null;
97
+ if (propName === "name") {
98
+ name = getStringValue(prop.initializer);
99
+ } else if (propName === "search" && ts2.isObjectLiteralExpression(prop.initializer)) {
100
+ search = extractObjectStringProperties(prop.initializer);
101
+ }
102
+ }
103
+ }
104
+ }
105
+ if (!name) return null;
106
+ const params = extractParamsFromPattern(pattern);
107
+ return {
108
+ name,
109
+ pattern,
110
+ ...params ? { params } : {},
111
+ ...search && Object.keys(search).length > 0 ? { search } : {}
112
+ };
113
+ }
114
+ var init_ast_route_extraction = __esm({
115
+ "src/build/route-types/ast-route-extraction.ts"() {
116
+ "use strict";
117
+ init_ast_helpers();
118
+ init_param_extraction();
119
+ }
120
+ });
121
+
122
+ // src/route-name.ts
123
+ function isAutoGeneratedRouteName(name) {
124
+ return name.split(".").some((segment) => {
125
+ return segment.startsWith(AUTO_GENERATED_ROUTE_PREFIX) || segment.startsWith(INTERNAL_INCLUDE_SCOPE_PREFIX);
126
+ });
127
+ }
128
+ var AUTO_GENERATED_ROUTE_PREFIX, INTERNAL_INCLUDE_SCOPE_PREFIX;
129
+ var init_route_name = __esm({
130
+ "src/route-name.ts"() {
131
+ "use strict";
132
+ AUTO_GENERATED_ROUTE_PREFIX = "$path_";
133
+ INTERNAL_INCLUDE_SCOPE_PREFIX = "$prefix_";
134
+ }
135
+ });
136
+
137
+ // src/build/route-types/codegen.ts
138
+ function generatePerModuleTypesSource(routes) {
139
+ const valid = routes.filter(({ name }) => {
140
+ if (!name || /["'\\`\n\r]/.test(name)) {
141
+ console.warn(
142
+ `[rsc-router] Skipping route with invalid name: ${JSON.stringify(name)}`
143
+ );
144
+ return false;
145
+ }
146
+ return true;
147
+ });
148
+ const deduped = /* @__PURE__ */ new Map();
149
+ for (const { name, pattern, params, search } of valid) {
150
+ if (deduped.has(name)) {
151
+ console.warn(
152
+ `[rsc-router] Duplicate route name "${name}" \u2014 keeping first definition`
153
+ );
154
+ continue;
155
+ }
156
+ deduped.set(name, { pattern, params, search });
157
+ }
158
+ const sorted = [...deduped.entries()].sort(([a], [b]) => a.localeCompare(b));
159
+ const body = sorted.map(([name, { pattern, params, search }]) => {
160
+ const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name) ? name : `"${name}"`;
161
+ return formatRouteEntry(key, pattern, params, search);
162
+ }).join("\n");
163
+ return `// Auto-generated by @rangojs/router - do not edit
164
+ export const routes = {
165
+ ${body}
166
+ } as const;
167
+ export type routes = typeof routes;
168
+ `;
169
+ }
170
+ function generateRouteTypesSource(routeManifest, searchSchemas) {
171
+ const entries = Object.entries(routeManifest).filter(([name]) => !isAutoGeneratedRouteName(name)).sort(([a], [b]) => a.localeCompare(b));
172
+ const filteredSearchSchemas = searchSchemas ? Object.fromEntries(
173
+ Object.entries(searchSchemas).filter(
174
+ ([name]) => !isAutoGeneratedRouteName(name)
175
+ )
176
+ ) : void 0;
177
+ const objectBody = entries.map(([name, pattern]) => {
178
+ const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name) ? name : `"${name}"`;
179
+ const params = extractParamsFromPattern(pattern);
180
+ const search = filteredSearchSchemas?.[name];
181
+ return formatRouteEntry(key, pattern, params, search);
182
+ }).join("\n");
183
+ return `// Auto-generated by @rangojs/router - do not edit
184
+ export const NamedRoutes = {
185
+ ${objectBody}
186
+ } as const;
187
+
188
+ declare global {
189
+ namespace RSCRouter {
190
+ interface GeneratedRouteMap extends Readonly<typeof NamedRoutes> {}
191
+ }
192
+ }
193
+ `;
194
+ }
195
+ var init_codegen = __esm({
196
+ "src/build/route-types/codegen.ts"() {
197
+ "use strict";
198
+ init_param_extraction();
199
+ init_route_name();
200
+ }
201
+ });
202
+
203
+ // src/build/route-types/scan-filter.ts
204
+ import { join, relative } from "node:path";
205
+ import { readdirSync } from "node:fs";
206
+ import picomatch from "picomatch";
207
+ function findTsFiles(dir, filter) {
208
+ const results = [];
209
+ let entries;
210
+ try {
211
+ entries = readdirSync(dir, { withFileTypes: true });
212
+ } catch (err) {
213
+ console.warn(
214
+ `[rsc-router] Failed to scan directory ${dir}: ${err.message}`
215
+ );
216
+ return results;
217
+ }
218
+ for (const entry of entries) {
219
+ const fullPath = join(dir, entry.name);
220
+ if (entry.isDirectory()) {
221
+ if (entry.name === "node_modules" || entry.name.startsWith(".") || entry.name === "dist" || entry.name === "build" || entry.name === "coverage")
222
+ continue;
223
+ results.push(...findTsFiles(fullPath, filter));
224
+ } else if ((entry.name.endsWith(".ts") || entry.name.endsWith(".tsx") || entry.name.endsWith(".js") || entry.name.endsWith(".jsx")) && !entry.name.includes(".gen.")) {
225
+ if (filter && !filter(fullPath)) continue;
226
+ results.push(fullPath);
227
+ }
228
+ }
229
+ return results;
230
+ }
231
+ var init_scan_filter = __esm({
232
+ "src/build/route-types/scan-filter.ts"() {
233
+ "use strict";
234
+ }
235
+ });
236
+
237
+ // src/build/route-types/include-resolution.ts
238
+ import { readFileSync, existsSync } from "node:fs";
239
+ import { dirname, resolve } from "node:path";
240
+ import ts3 from "typescript";
241
+ function extractNamePrefixFromInclude(node) {
242
+ if (node.arguments.length >= 3) {
243
+ const thirdArg = node.arguments[2];
244
+ if (ts3.isObjectLiteralExpression(thirdArg)) {
245
+ for (const prop of thirdArg.properties) {
246
+ if (!ts3.isPropertyAssignment(prop)) continue;
247
+ const propName = ts3.isIdentifier(prop.name) ? prop.name.text : null;
248
+ if (propName === "name") {
249
+ return getStringValue(prop.initializer);
250
+ }
251
+ }
252
+ }
253
+ }
254
+ return null;
255
+ }
256
+ function extractIncludesWithDiagnostics(code) {
257
+ const sourceFile = ts3.createSourceFile(
258
+ "input.tsx",
259
+ code,
260
+ ts3.ScriptTarget.Latest,
261
+ true,
262
+ ts3.ScriptKind.TSX
263
+ );
264
+ const resolved = [];
265
+ const unresolvable = [];
266
+ function visit(node) {
267
+ if (ts3.isCallExpression(node)) {
268
+ const callee = node.expression;
269
+ if (ts3.isIdentifier(callee) && callee.text === "include") {
270
+ if (node.arguments.length < 2) {
271
+ ts3.forEachChild(node, visit);
272
+ return;
273
+ }
274
+ const pathPrefix = getStringValue(node.arguments[0]);
275
+ if (pathPrefix === null) {
276
+ ts3.forEachChild(node, visit);
277
+ return;
278
+ }
279
+ const secondArg = node.arguments[1];
280
+ const namePrefix = extractNamePrefixFromInclude(node);
281
+ if (ts3.isIdentifier(secondArg)) {
282
+ resolved.push({
283
+ pathPrefix,
284
+ variableName: secondArg.text,
285
+ namePrefix
286
+ });
287
+ } else if (ts3.isCallExpression(secondArg)) {
288
+ const callText = secondArg.expression.getText(sourceFile);
289
+ unresolvable.push({
290
+ pathPrefix,
291
+ namePrefix,
292
+ reason: "factory-call",
293
+ detail: `${callText}()`
294
+ });
295
+ } else {
296
+ unresolvable.push({
297
+ pathPrefix,
298
+ namePrefix,
299
+ reason: "dynamic-expression",
300
+ detail: secondArg.getText(sourceFile)
301
+ });
302
+ }
303
+ }
304
+ }
305
+ ts3.forEachChild(node, visit);
306
+ }
307
+ visit(sourceFile);
308
+ return { resolved, unresolvable };
309
+ }
310
+ function resolveImportedVariable(code, localName) {
311
+ const importRegex = /import\s*\{([^}]+)\}\s*from\s*["']([^"']+)["']/g;
312
+ let match;
313
+ while ((match = importRegex.exec(code)) !== null) {
314
+ const imports = match[1];
315
+ const specifier = match[2];
316
+ const parts = imports.split(",").map((s) => s.trim()).filter(Boolean);
317
+ for (const part of parts) {
318
+ const asMatch = part.match(/^(\w+)\s+as\s+(\w+)$/);
319
+ if (asMatch && asMatch[2] === localName)
320
+ return { specifier, exportedName: asMatch[1] };
321
+ if (part === localName) return { specifier, exportedName: localName };
322
+ }
323
+ }
324
+ return null;
325
+ }
326
+ function resolveImportPath(importSpec, fromFile) {
327
+ if (!importSpec.startsWith(".")) return null;
328
+ const dir = dirname(fromFile);
329
+ let base = importSpec;
330
+ if (base.endsWith(".js")) base = base.slice(0, -3);
331
+ else if (base.endsWith(".mjs")) base = base.slice(0, -4);
332
+ else if (base.endsWith(".jsx")) base = base.slice(0, -4);
333
+ const candidates = [
334
+ resolve(dir, base + ".ts"),
335
+ resolve(dir, base + ".tsx"),
336
+ resolve(dir, base + ".js"),
337
+ resolve(dir, base + ".jsx"),
338
+ resolve(dir, base + "/index.ts"),
339
+ resolve(dir, base + "/index.tsx"),
340
+ resolve(dir, base + "/index.js"),
341
+ resolve(dir, base + "/index.jsx")
342
+ ];
343
+ for (const candidate of candidates) {
344
+ if (existsSync(candidate)) return candidate;
345
+ }
346
+ return null;
347
+ }
348
+ function extractUrlsBlockForVariable(code, varName) {
349
+ const sourceFile = ts3.createSourceFile(
350
+ "input.tsx",
351
+ code,
352
+ ts3.ScriptTarget.Latest,
353
+ true,
354
+ ts3.ScriptKind.TSX
355
+ );
356
+ let result = null;
357
+ function visit(node) {
358
+ if (result) return;
359
+ if (ts3.isVariableDeclaration(node) && ts3.isIdentifier(node.name) && node.name.text === varName && node.initializer && ts3.isCallExpression(node.initializer)) {
360
+ const callee = node.initializer.expression;
361
+ if (ts3.isIdentifier(callee) && callee.text === "urls") {
362
+ result = node.initializer.getText(sourceFile);
363
+ return;
364
+ }
365
+ }
366
+ ts3.forEachChild(node, visit);
367
+ }
368
+ visit(sourceFile);
369
+ return result;
370
+ }
371
+ function buildRouteMapFromBlock(block, fullSource, filePath, visited, searchSchemasOut, diagnosticsOut) {
372
+ const routeMap = {};
373
+ const localRoutes = extractRoutesFromSource(block);
374
+ for (const { name, pattern, search } of localRoutes) {
375
+ routeMap[name] = pattern;
376
+ if (search && searchSchemasOut) {
377
+ searchSchemasOut[name] = search;
378
+ }
379
+ }
380
+ const { resolved: includes, unresolvable } = extractIncludesWithDiagnostics(block);
381
+ if (diagnosticsOut) {
382
+ for (const entry of unresolvable) {
383
+ diagnosticsOut.push({ ...entry, sourceFile: filePath });
384
+ }
385
+ }
386
+ for (const { pathPrefix, variableName, namePrefix } of includes) {
387
+ let childResult;
388
+ const imported = resolveImportedVariable(fullSource, variableName);
389
+ if (imported) {
390
+ const targetFile = resolveImportPath(imported.specifier, filePath);
391
+ if (!targetFile) {
392
+ if (diagnosticsOut) {
393
+ diagnosticsOut.push({
394
+ pathPrefix,
395
+ namePrefix,
396
+ reason: "file-not-found",
397
+ sourceFile: filePath,
398
+ detail: `import "${imported.specifier}" resolved to no file`
399
+ });
400
+ }
401
+ continue;
402
+ }
403
+ childResult = buildCombinedRouteMapWithSearch(
404
+ targetFile,
405
+ imported.exportedName,
406
+ visited,
407
+ diagnosticsOut
408
+ );
409
+ } else {
410
+ const sameFileBlock = extractUrlsBlockForVariable(
411
+ fullSource,
412
+ variableName
413
+ );
414
+ if (!sameFileBlock) {
415
+ if (diagnosticsOut) {
416
+ diagnosticsOut.push({
417
+ pathPrefix,
418
+ namePrefix,
419
+ reason: "unresolvable-import",
420
+ sourceFile: filePath,
421
+ detail: `variable "${variableName}" not found in imports or same-file scope`
422
+ });
423
+ }
424
+ continue;
425
+ }
426
+ childResult = buildCombinedRouteMapWithSearch(
427
+ filePath,
428
+ variableName,
429
+ visited,
430
+ diagnosticsOut
431
+ );
432
+ }
433
+ if (namePrefix === null) {
434
+ continue;
435
+ }
436
+ for (const [name, pattern] of Object.entries(childResult.routes)) {
437
+ const prefixedName = namePrefix ? `${namePrefix}.${name}` : name;
438
+ let prefixedPattern;
439
+ if (pattern === "/") {
440
+ prefixedPattern = pathPrefix || "/";
441
+ } else if (pathPrefix.endsWith("/") && pattern.startsWith("/")) {
442
+ prefixedPattern = pathPrefix + pattern.slice(1);
443
+ } else {
444
+ prefixedPattern = pathPrefix + pattern;
445
+ }
446
+ routeMap[prefixedName] = prefixedPattern;
447
+ if (childResult.searchSchemas[name] && searchSchemasOut) {
448
+ searchSchemasOut[prefixedName] = childResult.searchSchemas[name];
449
+ }
450
+ }
451
+ }
452
+ return routeMap;
453
+ }
454
+ function buildCombinedRouteMapWithSearch(filePath, variableName, visited, diagnosticsOut, inlineBlock) {
455
+ visited = visited ?? /* @__PURE__ */ new Set();
456
+ const realPath = resolve(filePath);
457
+ const key = variableName ? `${realPath}:${variableName}` : realPath;
458
+ if (visited.has(key)) {
459
+ console.warn(`[rsc-router] Circular include detected, skipping: ${key}`);
460
+ return { routes: {}, searchSchemas: {} };
461
+ }
462
+ visited.add(key);
463
+ let source;
464
+ try {
465
+ source = readFileSync(realPath, "utf-8");
466
+ } catch {
467
+ return { routes: {}, searchSchemas: {} };
468
+ }
469
+ let block;
470
+ if (inlineBlock) {
471
+ block = inlineBlock;
472
+ } else if (variableName) {
473
+ const extracted = extractUrlsBlockForVariable(source, variableName);
474
+ if (!extracted) return { routes: {}, searchSchemas: {} };
475
+ block = extracted;
476
+ } else {
477
+ block = source;
478
+ }
479
+ const searchSchemas = {};
480
+ const routes = buildRouteMapFromBlock(
481
+ block,
482
+ source,
483
+ realPath,
484
+ visited,
485
+ searchSchemas,
486
+ diagnosticsOut
487
+ );
488
+ visited.delete(key);
489
+ return { routes, searchSchemas };
490
+ }
491
+ var init_include_resolution = __esm({
492
+ "src/build/route-types/include-resolution.ts"() {
493
+ "use strict";
494
+ init_ast_helpers();
495
+ init_ast_route_extraction();
496
+ }
497
+ });
498
+
499
+ // src/build/route-types/per-module-writer.ts
500
+ import { readFileSync as readFileSync2, writeFileSync, existsSync as existsSync2 } from "node:fs";
501
+ import ts4 from "typescript";
502
+ function findUrlsVariableNames(code) {
503
+ const sourceFile = ts4.createSourceFile(
504
+ "input.tsx",
505
+ code,
506
+ ts4.ScriptTarget.Latest,
507
+ true,
508
+ ts4.ScriptKind.TSX
509
+ );
510
+ const names = [];
511
+ function visit(node) {
512
+ if (ts4.isVariableDeclaration(node) && ts4.isIdentifier(node.name) && node.initializer && ts4.isCallExpression(node.initializer)) {
513
+ const callee = node.initializer.expression;
514
+ if (ts4.isIdentifier(callee) && callee.text === "urls") {
515
+ names.push(node.name.text);
516
+ }
517
+ }
518
+ ts4.forEachChild(node, visit);
519
+ }
520
+ visit(sourceFile);
521
+ return names;
522
+ }
523
+ function writePerModuleRouteTypesForFile(filePath) {
524
+ try {
525
+ const source = readFileSync2(filePath, "utf-8");
526
+ if (!source.includes("urls(")) return;
527
+ const varNames = findUrlsVariableNames(source);
528
+ let routes;
529
+ if (varNames.length > 0) {
530
+ routes = [];
531
+ for (const varName of varNames) {
532
+ const { routes: routeMap, searchSchemas } = buildCombinedRouteMapWithSearch(filePath, varName);
533
+ for (const [name, pattern] of Object.entries(routeMap)) {
534
+ const params = extractParamsFromPattern(pattern);
535
+ routes.push({
536
+ name,
537
+ pattern,
538
+ ...params ? { params } : {},
539
+ ...searchSchemas[name] ? { search: searchSchemas[name] } : {}
540
+ });
541
+ }
542
+ }
543
+ } else {
544
+ routes = extractRoutesFromSource(source);
545
+ }
546
+ const genPath = filePath.replace(/\.(tsx?)$/, ".gen.ts");
547
+ if (routes.length === 0) {
548
+ if (varNames.length > 0 && !existsSync2(genPath)) {
549
+ writeFileSync(genPath, generatePerModuleTypesSource([]));
550
+ console.log(
551
+ `[rsc-router] Generated route types (placeholder) -> ${genPath}`
552
+ );
553
+ }
554
+ return;
555
+ }
556
+ const genSource = generatePerModuleTypesSource(routes);
557
+ const existing = existsSync2(genPath) ? readFileSync2(genPath, "utf-8") : null;
558
+ if (existing !== genSource) {
559
+ writeFileSync(genPath, genSource);
560
+ console.log(`[rsc-router] Generated route types -> ${genPath}`);
561
+ }
562
+ } catch (err) {
563
+ console.warn(
564
+ `[rsc-router] Failed to generate route types for ${filePath}: ${err.message}`
565
+ );
566
+ }
567
+ }
568
+ var init_per_module_writer = __esm({
569
+ "src/build/route-types/per-module-writer.ts"() {
570
+ "use strict";
571
+ init_param_extraction();
572
+ init_ast_route_extraction();
573
+ init_codegen();
574
+ init_include_resolution();
575
+ init_scan_filter();
576
+ }
577
+ });
578
+
579
+ // src/build/route-types/router-processing.ts
580
+ import {
581
+ readFileSync as readFileSync3,
582
+ writeFileSync as writeFileSync2,
583
+ existsSync as existsSync3,
584
+ unlinkSync,
585
+ readdirSync as readdirSync2
586
+ } from "node:fs";
587
+ import {
588
+ join as join2,
589
+ dirname as dirname2,
590
+ resolve as resolve2,
591
+ sep,
592
+ basename as pathBasename
593
+ } from "node:path";
594
+ import ts5 from "typescript";
595
+ function countPublicRouteEntries(source) {
596
+ const matches = source.matchAll(/^\s+(?:"([^"]+)"|([a-zA-Z_$][^:]*)):\s*["{]/gm) ?? [];
597
+ let count = 0;
598
+ for (const match of matches) {
599
+ const routeName = match[1] || match[2];
600
+ if (routeName && !isAutoGeneratedRouteName(routeName.trim())) {
601
+ count++;
602
+ }
603
+ }
604
+ return count;
605
+ }
606
+ function isRoutableSourceFile(name) {
607
+ return (name.endsWith(".ts") || name.endsWith(".tsx") || name.endsWith(".js") || name.endsWith(".jsx")) && !name.includes(".gen.") && !name.includes(".test.") && !name.includes(".spec.");
608
+ }
609
+ function findRouterFilesRecursive(dir, filter, results) {
610
+ let entries;
611
+ try {
612
+ entries = readdirSync2(dir, { withFileTypes: true });
613
+ } catch (err) {
614
+ console.warn(
615
+ `[rsc-router] Failed to scan directory ${dir}: ${err.message}`
616
+ );
617
+ return;
618
+ }
619
+ const childDirs = [];
620
+ const routerFilesInDir = [];
621
+ for (const entry of entries) {
622
+ const fullPath = join2(dir, entry.name);
623
+ if (entry.isDirectory()) {
624
+ if (entry.name === "node_modules" || entry.name === "dist" || entry.name === "coverage" || entry.name === "__tests__" || entry.name === "__mocks__" || entry.name.startsWith("."))
625
+ continue;
626
+ childDirs.push(fullPath);
627
+ continue;
628
+ }
629
+ if (!isRoutableSourceFile(entry.name)) continue;
630
+ if (filter && !filter(fullPath)) continue;
631
+ try {
632
+ const source = readFileSync3(fullPath, "utf-8");
633
+ if (ROUTER_CALL_PATTERN.test(source)) {
634
+ routerFilesInDir.push(fullPath);
635
+ }
636
+ } catch {
637
+ continue;
638
+ }
639
+ }
640
+ if (routerFilesInDir.length > 0) {
641
+ results.push(...routerFilesInDir);
642
+ return;
643
+ }
644
+ for (const childDir of childDirs) {
645
+ findRouterFilesRecursive(childDir, filter, results);
646
+ }
647
+ }
648
+ function findNestedRouterConflict(routerFiles) {
649
+ const routerDirs = [
650
+ ...new Set(routerFiles.map((filePath) => dirname2(resolve2(filePath))))
651
+ ].sort((a, b) => a.length - b.length);
652
+ for (let i = 0; i < routerDirs.length; i++) {
653
+ const ancestorDir = routerDirs[i];
654
+ const prefix = ancestorDir.endsWith(sep) ? ancestorDir : `${ancestorDir}${sep}`;
655
+ for (let j = i + 1; j < routerDirs.length; j++) {
656
+ const nestedDir = routerDirs[j];
657
+ if (!nestedDir.startsWith(prefix)) continue;
658
+ const ancestorFile = routerFiles.find(
659
+ (filePath) => dirname2(resolve2(filePath)) === ancestorDir
660
+ );
661
+ const nestedFile = routerFiles.find(
662
+ (filePath) => dirname2(resolve2(filePath)) === nestedDir
663
+ );
664
+ if (ancestorFile && nestedFile) {
665
+ return { ancestor: ancestorFile, nested: nestedFile };
666
+ }
667
+ }
668
+ }
669
+ return null;
670
+ }
671
+ function formatNestedRouterConflictError(conflict, prefix = "[rsc-router]") {
672
+ return `${prefix} Nested router roots are not supported.
673
+ Router root: ${conflict.ancestor}
674
+ Nested router: ${conflict.nested}
675
+ Move the nested router into a sibling directory or configure it as a separate app root.`;
676
+ }
677
+ function extractUrlsFromRouter(code) {
678
+ const sourceFile = ts5.createSourceFile(
679
+ "router.tsx",
680
+ code,
681
+ ts5.ScriptTarget.Latest,
682
+ true,
683
+ ts5.ScriptKind.TSX
684
+ );
685
+ let result = null;
686
+ function isCreateRouterCall(node) {
687
+ if (!ts5.isCallExpression(node)) return false;
688
+ const callee = node.expression;
689
+ return ts5.isIdentifier(callee) && callee.text === "createRouter";
690
+ }
691
+ function isInlineBuilder(node) {
692
+ return ts5.isArrowFunction(node) || ts5.isFunctionExpression(node);
693
+ }
694
+ function isRoutesOnCreateRouter(node) {
695
+ if (!ts5.isPropertyAccessExpression(node.expression) || node.expression.name.text !== "routes")
696
+ return false;
697
+ let inner = node.expression.expression;
698
+ while (ts5.isCallExpression(inner) && ts5.isPropertyAccessExpression(inner.expression)) {
699
+ inner = inner.expression.expression;
700
+ }
701
+ return isCreateRouterCall(inner);
702
+ }
703
+ function visit(node) {
704
+ if (result) return;
705
+ if (ts5.isCallExpression(node) && node.arguments.length >= 1 && isRoutesOnCreateRouter(node)) {
706
+ const arg = node.arguments[0];
707
+ if (ts5.isIdentifier(arg)) {
708
+ result = { kind: "variable", name: arg.text };
709
+ } else if (isInlineBuilder(arg)) {
710
+ result = { kind: "inline", block: arg.getText(sourceFile) };
711
+ }
712
+ return;
713
+ }
714
+ if (isCreateRouterCall(node)) {
715
+ const callExpr = node;
716
+ for (const callArg of callExpr.arguments) {
717
+ if (ts5.isObjectLiteralExpression(callArg)) {
718
+ for (const prop of callArg.properties) {
719
+ if (ts5.isPropertyAssignment(prop) && ts5.isIdentifier(prop.name) && prop.name.text === "urls") {
720
+ if (ts5.isIdentifier(prop.initializer)) {
721
+ result = { kind: "variable", name: prop.initializer.text };
722
+ } else if (isInlineBuilder(prop.initializer)) {
723
+ result = {
724
+ kind: "inline",
725
+ block: prop.initializer.getText(sourceFile)
726
+ };
727
+ }
728
+ return;
729
+ }
730
+ }
731
+ }
732
+ }
733
+ }
734
+ ts5.forEachChild(node, visit);
735
+ }
736
+ visit(sourceFile);
737
+ return result;
738
+ }
739
+ function extractBasenameFromRouter(code) {
740
+ const sourceFile = ts5.createSourceFile(
741
+ "router.tsx",
742
+ code,
743
+ ts5.ScriptTarget.Latest,
744
+ true,
745
+ ts5.ScriptKind.TSX
746
+ );
747
+ let result;
748
+ function visit(node) {
749
+ if (result !== void 0) return;
750
+ if (ts5.isCallExpression(node) && ts5.isIdentifier(node.expression) && node.expression.text === "createRouter") {
751
+ for (const arg of node.arguments) {
752
+ if (ts5.isObjectLiteralExpression(arg)) {
753
+ for (const prop of arg.properties) {
754
+ if (ts5.isPropertyAssignment(prop) && ts5.isIdentifier(prop.name) && prop.name.text === "basename" && ts5.isStringLiteral(prop.initializer)) {
755
+ result = prop.initializer.text;
756
+ return;
757
+ }
758
+ }
759
+ }
760
+ }
761
+ }
762
+ ts5.forEachChild(node, visit);
763
+ }
764
+ visit(sourceFile);
765
+ return result;
766
+ }
767
+ function applyBasenameToRoutes(result, basename2) {
768
+ const prefixed = {};
769
+ for (const [name, pattern] of Object.entries(result.routes)) {
770
+ if (pattern === "/") {
771
+ prefixed[name] = basename2;
772
+ } else if (basename2.endsWith("/") && pattern.startsWith("/")) {
773
+ prefixed[name] = basename2 + pattern.slice(1);
774
+ } else {
775
+ prefixed[name] = basename2 + pattern;
776
+ }
777
+ }
778
+ return { routes: prefixed, searchSchemas: result.searchSchemas };
779
+ }
780
+ function buildCombinedRouteMapForRouterFile(routerFilePath) {
781
+ let routerSource;
782
+ try {
783
+ routerSource = readFileSync3(routerFilePath, "utf-8");
784
+ } catch {
785
+ return { routes: {}, searchSchemas: {} };
786
+ }
787
+ const extraction = extractUrlsFromRouter(routerSource);
788
+ if (!extraction) {
789
+ return { routes: {}, searchSchemas: {} };
790
+ }
791
+ const rawBasename = extractBasenameFromRouter(routerSource);
792
+ const basename2 = rawBasename ? ("/" + rawBasename.replace(/^\/+|\/+$/g, "")).replace(/^\/$/, "") : void 0;
793
+ let result;
794
+ if (extraction.kind === "inline") {
795
+ result = buildCombinedRouteMapWithSearch(
796
+ routerFilePath,
797
+ void 0,
798
+ void 0,
799
+ void 0,
800
+ extraction.block
801
+ );
802
+ } else {
803
+ const imported = resolveImportedVariable(routerSource, extraction.name);
804
+ if (imported) {
805
+ const targetFile = resolveImportPath(imported.specifier, routerFilePath);
806
+ if (!targetFile) {
807
+ return { routes: {}, searchSchemas: {} };
808
+ }
809
+ result = buildCombinedRouteMapWithSearch(
810
+ targetFile,
811
+ imported.exportedName
812
+ );
813
+ } else {
814
+ result = buildCombinedRouteMapWithSearch(routerFilePath, extraction.name);
815
+ }
816
+ }
817
+ if (basename2) {
818
+ result = applyBasenameToRoutes(result, basename2);
819
+ }
820
+ return result;
821
+ }
822
+ function detectUnresolvableIncludes(routerFilePath) {
823
+ const realPath = resolve2(routerFilePath);
824
+ let source;
825
+ try {
826
+ source = readFileSync3(realPath, "utf-8");
827
+ } catch {
828
+ return [];
829
+ }
830
+ const extraction = extractUrlsFromRouter(source);
831
+ if (!extraction) return [];
832
+ const diagnostics = [];
833
+ if (extraction.kind === "inline") {
834
+ buildCombinedRouteMapWithSearch(
835
+ realPath,
836
+ void 0,
837
+ /* @__PURE__ */ new Set(),
838
+ diagnostics,
839
+ extraction.block
840
+ );
841
+ return diagnostics;
842
+ }
843
+ const imported = resolveImportedVariable(source, extraction.name);
844
+ let targetFile;
845
+ let exportedName;
846
+ if (imported) {
847
+ const resolved = resolveImportPath(imported.specifier, realPath);
848
+ if (!resolved) {
849
+ return [
850
+ {
851
+ pathPrefix: "/",
852
+ namePrefix: null,
853
+ reason: "file-not-found",
854
+ sourceFile: realPath,
855
+ detail: `import "${imported.specifier}" resolved to no file`
856
+ }
857
+ ];
858
+ }
859
+ targetFile = resolved;
860
+ exportedName = imported.exportedName;
861
+ } else {
862
+ targetFile = realPath;
863
+ exportedName = extraction.name;
864
+ }
865
+ buildCombinedRouteMapWithSearch(
866
+ targetFile,
867
+ exportedName,
868
+ /* @__PURE__ */ new Set(),
869
+ diagnostics
870
+ );
871
+ return diagnostics;
872
+ }
873
+ function detectUnresolvableIncludesForUrlsFile(filePath) {
874
+ const realPath = resolve2(filePath);
875
+ let source;
876
+ try {
877
+ source = readFileSync3(realPath, "utf-8");
878
+ } catch {
879
+ return [];
880
+ }
881
+ const varNames = findUrlsVariableNames(source);
882
+ if (varNames.length === 0) return [];
883
+ const diagnostics = [];
884
+ for (const varName of varNames) {
885
+ buildCombinedRouteMapWithSearch(realPath, varName, /* @__PURE__ */ new Set(), diagnostics);
886
+ }
887
+ return diagnostics;
888
+ }
889
+ function findRouterFiles(root, filter) {
890
+ const result = [];
891
+ findRouterFilesRecursive(root, filter, result);
892
+ return result;
893
+ }
894
+ function writeCombinedRouteTypes(root, knownRouterFiles, opts) {
895
+ try {
896
+ const oldCombinedPath = join2(root, "src", "named-routes.gen.ts");
897
+ if (existsSync3(oldCombinedPath)) {
898
+ unlinkSync(oldCombinedPath);
899
+ console.log(
900
+ `[rsc-router] Removed stale combined route types: ${oldCombinedPath}`
901
+ );
902
+ }
903
+ } catch {
904
+ }
905
+ const routerFilePaths = knownRouterFiles ?? findRouterFiles(root);
906
+ if (routerFilePaths.length === 0) return;
907
+ const nestedRouterConflict = findNestedRouterConflict(routerFilePaths);
908
+ if (nestedRouterConflict) {
909
+ throw new Error(formatNestedRouterConflictError(nestedRouterConflict));
910
+ }
911
+ for (const routerFilePath of routerFilePaths) {
912
+ const result = buildCombinedRouteMapForRouterFile(routerFilePath);
913
+ if (Object.keys(result.routes).length === 0 && Object.keys(result.searchSchemas).length === 0) {
914
+ let routerSource;
915
+ try {
916
+ routerSource = readFileSync3(routerFilePath, "utf-8");
917
+ } catch {
918
+ continue;
919
+ }
920
+ if (!extractUrlsFromRouter(routerSource)) continue;
921
+ }
922
+ const routerBasename = pathBasename(routerFilePath).replace(
923
+ /\.(tsx?|jsx?)$/,
924
+ ""
925
+ );
926
+ const outPath = join2(
927
+ dirname2(routerFilePath),
928
+ `${routerBasename}.named-routes.gen.ts`
929
+ );
930
+ const existing = existsSync3(outPath) ? readFileSync3(outPath, "utf-8") : null;
931
+ if (Object.keys(result.routes).length === 0) {
932
+ if (!existing) {
933
+ const emptySource = generateRouteTypesSource({});
934
+ writeFileSync2(outPath, emptySource);
935
+ }
936
+ continue;
937
+ }
938
+ const hasSearchSchemas = Object.keys(result.searchSchemas).length > 0;
939
+ const source = generateRouteTypesSource(
940
+ result.routes,
941
+ hasSearchSchemas ? result.searchSchemas : void 0
942
+ );
943
+ if (existing !== source) {
944
+ if (opts?.preserveIfLarger && existing) {
945
+ const existingCount = countPublicRouteEntries(existing);
946
+ const newCount = Object.keys(result.routes).filter(
947
+ (name) => !isAutoGeneratedRouteName(name)
948
+ ).length;
949
+ if (existingCount > newCount) {
950
+ continue;
951
+ }
952
+ }
953
+ writeFileSync2(outPath, source);
954
+ console.log(
955
+ `[rsc-router] Generated route types (${Object.keys(result.routes).length} routes) -> ${outPath}`
956
+ );
957
+ }
958
+ }
959
+ }
960
+ var ROUTER_CALL_PATTERN;
961
+ var init_router_processing = __esm({
962
+ "src/build/route-types/router-processing.ts"() {
963
+ "use strict";
964
+ init_codegen();
965
+ init_include_resolution();
966
+ init_per_module_writer();
967
+ init_route_name();
968
+ ROUTER_CALL_PATTERN = /\bcreateRouter\s*[<(]/;
969
+ }
970
+ });
971
+
972
+ // src/build/generate-route-types.ts
973
+ var init_generate_route_types = __esm({
974
+ "src/build/generate-route-types.ts"() {
975
+ "use strict";
976
+ init_param_extraction();
977
+ init_ast_route_extraction();
978
+ init_codegen();
979
+ init_scan_filter();
980
+ init_per_module_writer();
981
+ init_include_resolution();
982
+ init_router_processing();
983
+ init_per_module_writer();
984
+ }
985
+ });
986
+
987
+ // src/vite/plugins/virtual-entries.ts
988
+ function getVirtualVersionContent(version) {
989
+ return `export const VERSION = ${JSON.stringify(version)};`;
990
+ }
991
+ var VIRTUAL_ENTRY_BROWSER, VIRTUAL_ENTRY_SSR, VIRTUAL_IDS;
992
+ var init_virtual_entries = __esm({
993
+ "src/vite/plugins/virtual-entries.ts"() {
994
+ "use strict";
995
+ VIRTUAL_ENTRY_BROWSER = `
996
+ import {
997
+ createFromReadableStream,
998
+ createFromFetch,
999
+ setServerCallback,
1000
+ encodeReply,
1001
+ createTemporaryReferenceSet,
1002
+ } from "@rangojs/router/internal/deps/browser";
1003
+ import { createElement, StrictMode } from "react";
1004
+ import { hydrateRoot } from "react-dom/client";
1005
+ import { rscStream } from "@rangojs/router/internal/deps/html-stream-client";
1006
+ import { initBrowserApp, RSCRouter } from "@rangojs/router/browser";
1007
+
1008
+ async function initializeApp() {
1009
+ const deps = {
1010
+ createFromFetch,
1011
+ createFromReadableStream,
1012
+ encodeReply,
1013
+ setServerCallback,
1014
+ createTemporaryReferenceSet,
1015
+ };
1016
+
1017
+ await initBrowserApp({ rscStream, deps });
1018
+
1019
+ hydrateRoot(
1020
+ document,
1021
+ createElement(StrictMode, null, createElement(RSCRouter))
1022
+ );
1023
+ }
1024
+
1025
+ initializeApp().catch(console.error);
1026
+ `.trim();
1027
+ VIRTUAL_ENTRY_SSR = `
1028
+ import { createFromReadableStream } from "@rangojs/router/internal/deps/ssr";
1029
+ import { renderToReadableStream } from "react-dom/server.edge";
1030
+ import { injectRSCPayload } from "@rangojs/router/internal/deps/html-stream-server";
1031
+ import { createSSRHandler } from "@rangojs/router/ssr";
1032
+
1033
+ export const renderHTML = createSSRHandler({
1034
+ createFromReadableStream,
1035
+ renderToReadableStream,
1036
+ injectRSCPayload,
1037
+ loadBootstrapScriptContent: () =>
1038
+ import.meta.viteRsc.loadBootstrapScriptContent("index"),
1039
+ });
1040
+ `.trim();
1041
+ VIRTUAL_IDS = {
1042
+ browser: "virtual:rsc-router/entry.browser.js",
1043
+ ssr: "virtual:rsc-router/entry.ssr.js",
1044
+ rsc: "virtual:rsc-router/entry.rsc.js",
1045
+ version: "@rangojs/router:version"
1046
+ };
1047
+ }
1048
+ });
1049
+
1050
+ // src/vite/plugins/version-plugin.ts
1051
+ var version_plugin_exports = {};
1052
+ __export(version_plugin_exports, {
1053
+ createVersionPlugin: () => createVersionPlugin
1054
+ });
1055
+ import { parseAst } from "vite";
1056
+ function isCodeModule(id) {
1057
+ return /\.(tsx?|jsx?)($|\?)/.test(id);
1058
+ }
1059
+ function normalizeModuleId(id) {
1060
+ return id.split("?", 1)[0];
1061
+ }
1062
+ function getClientModuleSignature(source) {
1063
+ let program;
1064
+ try {
1065
+ program = parseAst(source, { jsx: true });
1066
+ } catch {
1067
+ return void 0;
1068
+ }
1069
+ let isUseClient = false;
1070
+ for (const node of program.body ?? []) {
1071
+ if (node?.type === "ExpressionStatement" && node.expression?.type === "Literal" && typeof node.expression.value === "string") {
1072
+ if (node.expression.value === "use client") {
1073
+ isUseClient = true;
1074
+ }
1075
+ continue;
1076
+ }
1077
+ break;
1078
+ }
1079
+ if (!isUseClient) return void 0;
1080
+ const exports = /* @__PURE__ */ new Set();
1081
+ let hasDefault = false;
1082
+ let hasExportAll = false;
1083
+ const collectBindingNames = (pattern) => {
1084
+ if (!pattern) return;
1085
+ if (pattern.type === "Identifier") {
1086
+ exports.add(pattern.name);
1087
+ } else if (pattern.type === "ObjectPattern") {
1088
+ for (const prop of pattern.properties ?? []) {
1089
+ if (prop?.type === "RestElement") {
1090
+ collectBindingNames(prop.argument);
1091
+ } else {
1092
+ collectBindingNames(prop?.value);
1093
+ }
1094
+ }
1095
+ } else if (pattern.type === "ArrayPattern") {
1096
+ for (const el of pattern.elements ?? []) {
1097
+ if (el?.type === "RestElement") {
1098
+ collectBindingNames(el.argument);
1099
+ } else {
1100
+ collectBindingNames(el);
1101
+ }
1102
+ }
1103
+ }
1104
+ };
1105
+ const collectDeclarationNames = (declaration) => {
1106
+ if (!declaration) return;
1107
+ if (declaration.type === "VariableDeclaration") {
1108
+ for (const decl of declaration.declarations ?? []) {
1109
+ collectBindingNames(decl?.id);
1110
+ }
1111
+ return;
1112
+ }
1113
+ collectBindingNames(declaration.id);
1114
+ };
1115
+ for (const node of program.body ?? []) {
1116
+ if (node?.type === "ExportDefaultDeclaration") {
1117
+ hasDefault = true;
1118
+ continue;
1119
+ }
1120
+ if (node?.type === "ExportAllDeclaration") {
1121
+ hasExportAll = true;
1122
+ continue;
1123
+ }
1124
+ if (node?.type !== "ExportNamedDeclaration") continue;
1125
+ collectDeclarationNames(node.declaration);
1126
+ for (const specifier of node.specifiers ?? []) {
1127
+ const exportedName = specifier?.exported?.name ?? specifier?.exported?.value;
1128
+ if (exportedName === "default") {
1129
+ hasDefault = true;
1130
+ } else if (typeof exportedName === "string") {
1131
+ exports.add(exportedName);
1132
+ }
1133
+ }
1134
+ }
1135
+ return {
1136
+ key: JSON.stringify({
1137
+ default: hasDefault,
1138
+ exportAll: hasExportAll,
1139
+ exports: [...exports].sort()
1140
+ })
1141
+ };
1142
+ }
1143
+ function createVersionPlugin() {
1144
+ const buildVersion = Date.now().toString(16);
1145
+ let currentVersion = buildVersion;
1146
+ let isDev = false;
1147
+ let server = null;
1148
+ const clientModuleSignatures = /* @__PURE__ */ new Map();
1149
+ let versionCounter = 0;
1150
+ const bumpVersion = (reason) => {
1151
+ currentVersion = Date.now().toString(16) + String(++versionCounter);
1152
+ console.log(`[rsc-router] ${reason}, version updated: ${currentVersion}`);
1153
+ const rscEnv = server?.environments?.rsc;
1154
+ const versionMod = rscEnv?.moduleGraph?.getModuleById(
1155
+ "\0" + VIRTUAL_IDS.version
1156
+ );
1157
+ if (versionMod) {
1158
+ rscEnv.moduleGraph.invalidateModule(versionMod);
1159
+ }
1160
+ };
1161
+ return {
1162
+ name: "@rangojs/router:version",
1163
+ enforce: "pre",
1164
+ configResolved(config) {
1165
+ isDev = config.command === "serve";
1166
+ },
1167
+ configureServer(devServer) {
1168
+ server = devServer;
1169
+ devServer.watcher.on("unlink", (filePath) => {
1170
+ if (!isDev) return;
1171
+ if (!clientModuleSignatures.has(filePath)) return;
1172
+ clientModuleSignatures.delete(filePath);
1173
+ bumpVersion("Client module removed");
1174
+ });
1175
+ },
1176
+ resolveId(id) {
1177
+ if (id === VIRTUAL_IDS.version) {
1178
+ return "\0" + id;
1179
+ }
1180
+ return null;
1181
+ },
1182
+ load(id) {
1183
+ if (id === "\0" + VIRTUAL_IDS.version) {
1184
+ return getVirtualVersionContent(currentVersion);
1185
+ }
1186
+ return null;
1187
+ },
1188
+ transform(code, id) {
1189
+ if (!isDev || !isCodeModule(id)) return null;
1190
+ const normalizedId = normalizeModuleId(id);
1191
+ if (!code.includes("use client") && !clientModuleSignatures.has(normalizedId)) {
1192
+ return null;
1193
+ }
1194
+ const signature = getClientModuleSignature(code);
1195
+ if (signature) {
1196
+ clientModuleSignatures.set(normalizedId, signature);
1197
+ } else {
1198
+ clientModuleSignatures.delete(normalizedId);
1199
+ }
1200
+ return null;
1201
+ },
1202
+ // Track RSC module changes and update version
1203
+ async hotUpdate(ctx) {
1204
+ if (!isDev) return;
1205
+ const isRscModule = this.environment?.name === "rsc";
1206
+ if (!isRscModule) return;
1207
+ if (ctx.modules.length === 1 && ctx.modules[0].id === "\0" + VIRTUAL_IDS.version) {
1208
+ return;
1209
+ }
1210
+ if (isCodeModule(ctx.file)) {
1211
+ const filePath = normalizeModuleId(ctx.file);
1212
+ const previousSignature = clientModuleSignatures.get(filePath);
1213
+ try {
1214
+ const source = await ctx.read();
1215
+ const nextSignature = getClientModuleSignature(source);
1216
+ if (nextSignature) {
1217
+ clientModuleSignatures.set(filePath, nextSignature);
1218
+ if (previousSignature && previousSignature.key === nextSignature.key) {
1219
+ return;
1220
+ }
1221
+ } else {
1222
+ clientModuleSignatures.delete(filePath);
1223
+ if (!previousSignature) {
1224
+ if (ctx.modules.length === 0) return;
1225
+ }
1226
+ }
1227
+ } catch {
1228
+ }
1229
+ } else {
1230
+ if (ctx.modules.length === 0) return;
1231
+ }
1232
+ bumpVersion("RSC module changed");
1233
+ }
1234
+ };
1235
+ }
1236
+ var init_version_plugin = __esm({
1237
+ "src/vite/plugins/version-plugin.ts"() {
1238
+ "use strict";
1239
+ init_virtual_entries();
1240
+ }
1241
+ });
1242
+
1243
+ // src/vite/plugins/virtual-stub-plugin.ts
1244
+ var virtual_stub_plugin_exports = {};
1245
+ __export(virtual_stub_plugin_exports, {
1246
+ createVirtualStubPlugin: () => createVirtualStubPlugin
1247
+ });
1248
+ function createVirtualStubPlugin() {
1249
+ const STUB_PREFIXES = [
1250
+ "virtual:rsc-router/",
1251
+ "virtual:entry-",
1252
+ "virtual:vite-rsc/"
1253
+ ];
1254
+ return {
1255
+ name: "@rangojs/router:virtual-stubs",
1256
+ resolveId(id) {
1257
+ if (STUB_PREFIXES.some((p) => id.startsWith(p))) {
1258
+ return "\0stub:" + id;
1259
+ }
1260
+ return null;
1261
+ },
1262
+ load(id) {
1263
+ if (id.startsWith("\0stub:")) {
1264
+ return "export default {}";
1265
+ }
1266
+ return null;
1267
+ }
1268
+ };
1269
+ }
1270
+ var init_virtual_stub_plugin = __esm({
1271
+ "src/vite/plugins/virtual-stub-plugin.ts"() {
1272
+ "use strict";
1273
+ }
1274
+ });
1275
+
1276
+ // src/build/runtime-discovery.ts
1277
+ var runtime_discovery_exports = {};
1278
+ __export(runtime_discovery_exports, {
1279
+ discoverAndWriteRouteTypes: () => discoverAndWriteRouteTypes
1280
+ });
1281
+ import { dirname as dirname3, join as join3, basename, resolve as resolve3 } from "node:path";
1282
+ import { existsSync as existsSync4, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "node:fs";
1283
+ async function discoverAndWriteRouteTypes(opts) {
1284
+ let createViteServer;
1285
+ let loadConfigFromFile;
1286
+ let rsc;
1287
+ try {
1288
+ const vite = await import("vite");
1289
+ createViteServer = vite.createServer;
1290
+ loadConfigFromFile = vite.loadConfigFromFile;
1291
+ } catch {
1292
+ throw new Error(
1293
+ "Runtime discovery requires 'vite'. Install it with: pnpm add -D vite"
1294
+ );
1295
+ }
1296
+ try {
1297
+ const rscMod = await import("@vitejs/plugin-rsc");
1298
+ rsc = rscMod.default;
1299
+ } catch {
1300
+ throw new Error(
1301
+ "Runtime discovery requires '@vitejs/plugin-rsc'. Install it with: pnpm add -D @vitejs/plugin-rsc"
1302
+ );
1303
+ }
1304
+ const { createVersionPlugin: createVersionPlugin2 } = await Promise.resolve().then(() => (init_version_plugin(), version_plugin_exports));
1305
+ const { createVirtualStubPlugin: createVirtualStubPlugin2 } = await Promise.resolve().then(() => (init_virtual_stub_plugin(), virtual_stub_plugin_exports));
1306
+ let userResolveAlias = opts.resolveAlias;
1307
+ if (!userResolveAlias) {
1308
+ const configPath = opts.configFile;
1309
+ try {
1310
+ const loaded = await loadConfigFromFile(
1311
+ { command: "serve", mode: "development" },
1312
+ configPath,
1313
+ opts.root
1314
+ );
1315
+ if (loaded?.config?.resolve?.alias) {
1316
+ userResolveAlias = loaded.config.resolve.alias;
1317
+ }
1318
+ } catch {
1319
+ }
1320
+ }
1321
+ const entryPath = resolve3(opts.entry);
1322
+ let tempServer = null;
1323
+ try {
1324
+ tempServer = await createViteServer({
1325
+ root: opts.root,
1326
+ configFile: false,
1327
+ server: { middlewareMode: true },
1328
+ appType: "custom",
1329
+ logLevel: "silent",
1330
+ cacheDir: "node_modules/.vite_rango_generate",
1331
+ resolve: userResolveAlias ? { alias: userResolveAlias } : void 0,
1332
+ esbuild: { jsx: "automatic", jsxImportSource: "react" },
1333
+ plugins: [
1334
+ rsc({
1335
+ entries: {
1336
+ client: "virtual:entry-client",
1337
+ ssr: "virtual:entry-ssr",
1338
+ rsc: entryPath
1339
+ }
1340
+ }),
1341
+ createVersionPlugin2(),
1342
+ createVirtualStubPlugin2()
1343
+ ]
1344
+ });
1345
+ const rscEnv = tempServer.environments?.rsc;
1346
+ if (!rscEnv?.runner) {
1347
+ throw new Error("RSC environment runner not available");
1348
+ }
1349
+ await rscEnv.runner.import(entryPath);
1350
+ const serverMod = await rscEnv.runner.import("@rangojs/router/server");
1351
+ const registry = serverMod.RouterRegistry;
1352
+ if (!registry || registry.size === 0) {
1353
+ throw new Error(
1354
+ `No routers found in registry after importing ${opts.entry}`
1355
+ );
1356
+ }
1357
+ const buildMod = await rscEnv.runner.import("@rangojs/router/build");
1358
+ const generateManifest = buildMod.generateManifest;
1359
+ if (!generateManifest) {
1360
+ throw new Error("generateManifest not found in @rangojs/router/build");
1361
+ }
1362
+ const outputFiles = [];
1363
+ let totalRouteCount = 0;
1364
+ let routerMountIndex = 0;
1365
+ for (const [id, router] of registry) {
1366
+ if (!router.urlpatterns) continue;
1367
+ const manifest = generateManifest(router.urlpatterns, routerMountIndex);
1368
+ routerMountIndex++;
1369
+ const rawManifest = manifest.routeManifest;
1370
+ const routeManifest = {};
1371
+ for (const [name, pattern] of Object.entries(rawManifest)) {
1372
+ if (!isAutoGeneratedRouteName(name)) {
1373
+ routeManifest[name] = pattern;
1374
+ }
1375
+ }
1376
+ let routeSearchSchemas = manifest.routeSearchSchemas;
1377
+ const sourceFile = router.__sourceFile;
1378
+ if (!sourceFile) {
1379
+ console.warn(
1380
+ `[rango] Router "${id}" has no __sourceFile, skipping gen file`
1381
+ );
1382
+ continue;
1383
+ }
1384
+ if (sourceFile.includes("node_modules")) {
1385
+ throw new Error(
1386
+ `[rango] Router "${id}" has sourceFile inside node_modules: ${sourceFile}
1387
+ This means createRouter() stack trace parsing matched an internal frame.
1388
+ Set an explicit \`id\` on createRouter() or check the call site.`
1389
+ );
1390
+ }
1391
+ if (!routeSearchSchemas || Object.keys(routeSearchSchemas).length === 0) {
1392
+ const staticParsed = buildCombinedRouteMapForRouterFile(sourceFile);
1393
+ if (Object.keys(staticParsed.searchSchemas).length > 0) {
1394
+ const filtered = {};
1395
+ for (const name of Object.keys(routeManifest)) {
1396
+ const schema = staticParsed.searchSchemas[name];
1397
+ if (schema) filtered[name] = schema;
1398
+ }
1399
+ if (Object.keys(filtered).length > 0) {
1400
+ routeSearchSchemas = filtered;
1401
+ }
1402
+ }
1403
+ }
1404
+ const routerDir = dirname3(sourceFile);
1405
+ const routerBasename = basename(sourceFile).replace(/\.(tsx?|jsx?)$/, "");
1406
+ const outPath = join3(routerDir, `${routerBasename}.named-routes.gen.ts`);
1407
+ const source = generateRouteTypesSource(
1408
+ routeManifest,
1409
+ routeSearchSchemas && Object.keys(routeSearchSchemas).length > 0 ? routeSearchSchemas : void 0
1410
+ );
1411
+ const existing = existsSync4(outPath) ? readFileSync4(outPath, "utf-8") : null;
1412
+ if (existing !== source) {
1413
+ writeFileSync3(outPath, source);
1414
+ }
1415
+ const routeCount = Object.keys(routeManifest).length;
1416
+ totalRouteCount += routeCount;
1417
+ outputFiles.push(outPath);
1418
+ console.log(
1419
+ `[rango] Generated route types (${routeCount} routes) -> ${outPath}`
1420
+ );
1421
+ }
1422
+ return {
1423
+ routerCount: routerMountIndex,
1424
+ routeCount: totalRouteCount,
1425
+ outputFiles
1426
+ };
1427
+ } finally {
1428
+ if (tempServer) {
1429
+ await tempServer.close();
1430
+ }
1431
+ }
1432
+ }
1433
+ var init_runtime_discovery = __esm({
1434
+ "src/build/runtime-discovery.ts"() {
1435
+ "use strict";
1436
+ init_generate_route_types();
1437
+ init_route_name();
1438
+ }
1439
+ });
1440
+
1441
+ // src/bin/rango.ts
1442
+ init_generate_route_types();
1443
+ import { resolve as resolve4, dirname as dirname4 } from "node:path";
1444
+ import { readFileSync as readFileSync5, statSync, existsSync as existsSync5 } from "node:fs";
1445
+ var [command, ...rawArgs] = process.argv.slice(2);
1446
+ if (command === "generate") {
1447
+ let mode = "default";
1448
+ let configFile;
1449
+ const positionalArgs = [];
1450
+ for (let i = 0; i < rawArgs.length; i++) {
1451
+ const arg = rawArgs[i];
1452
+ if (arg === "--runtime") {
1453
+ mode = "runtime";
1454
+ } else if (arg === "--static") {
1455
+ mode = "static";
1456
+ } else if (arg === "--config") {
1457
+ configFile = rawArgs[++i];
1458
+ if (!configFile) {
1459
+ console.error("[rango] --config requires a path argument");
1460
+ process.exit(1);
1461
+ }
1462
+ } else if (arg.startsWith("--")) {
1463
+ console.error(`[rango] Unknown flag: ${arg}`);
1464
+ process.exit(1);
1465
+ } else {
1466
+ positionalArgs.push(arg);
1467
+ }
1468
+ }
1469
+ if (positionalArgs.length === 0) {
1470
+ console.error(
1471
+ "[rango] Usage: rango generate <file|dir> [file2 ...] [--runtime|--static] [--config <path>]"
1472
+ );
1473
+ process.exit(1);
1474
+ }
1475
+ if (configFile && mode !== "runtime") {
1476
+ console.warn("[rango] --config is only used with --runtime, ignoring");
1477
+ }
1478
+ if (mode === "runtime") {
1479
+ runRuntimeDiscovery(positionalArgs, configFile).catch((err) => {
1480
+ console.error(`[rango] Runtime discovery failed: ${err.message}`);
1481
+ process.exit(1);
1482
+ });
1483
+ } else {
1484
+ runStaticGeneration(positionalArgs, mode);
1485
+ }
1486
+ } else {
1487
+ if (command && command !== "help" && command !== "--help" && command !== "-h") {
1488
+ console.error(`[rango] Unknown command: ${command}
1489
+ `);
1490
+ }
1491
+ console.log(`Usage: rango generate <file|dir> [file2 ...] [--runtime|--static] [--config <path>]
1492
+
1493
+ Auto-detects file type (createRouter, urls) and generates
1494
+ the appropriate .gen.ts route type files.
1495
+
1496
+ Modes:
1497
+ (default) Static parser with error on unresolvable includes
1498
+ --runtime Vite-based runtime discovery (100% coverage)
1499
+ Requires vite and @vitejs/plugin-rsc
1500
+ --static Static parser, accept partial output with warnings
1501
+
1502
+ Options:
1503
+ --config <path> Path to vite.config.ts (--runtime only, auto-detected if omitted)
1504
+
1505
+ Examples:
1506
+ rango generate src/router.tsx
1507
+ rango generate src/router.tsx --runtime
1508
+ rango generate src/ --static`);
1509
+ process.exit(
1510
+ command && command !== "help" && command !== "--help" && command !== "-h" ? 1 : 0
1511
+ );
1512
+ }
1513
+ function findProjectRoot(fromPath) {
1514
+ let dir = dirname4(resolve4(fromPath));
1515
+ while (dir !== dirname4(dir)) {
1516
+ if (existsSync5(resolve4(dir, "package.json")) || existsSync5(resolve4(dir, "vite.config.ts")) || existsSync5(resolve4(dir, "vite.config.js"))) {
1517
+ return dir;
1518
+ }
1519
+ dir = dirname4(dir);
1520
+ }
1521
+ return process.cwd();
1522
+ }
1523
+ function runStaticGeneration(args, mode) {
1524
+ const files = [];
1525
+ for (const arg of args) {
1526
+ const resolved = resolve4(arg);
1527
+ try {
1528
+ if (statSync(resolved).isDirectory()) {
1529
+ files.push(...findTsFiles(resolved));
1530
+ } else {
1531
+ files.push(resolved);
1532
+ }
1533
+ } catch {
1534
+ console.warn(`[rango] Skipping ${arg}: not found`);
1535
+ }
1536
+ }
1537
+ if (files.length === 0) {
1538
+ console.log("[rango] No files to process");
1539
+ process.exit(0);
1540
+ }
1541
+ const routerFiles = [];
1542
+ const urlsFiles = [];
1543
+ for (const filePath of files) {
1544
+ try {
1545
+ const source = readFileSync5(filePath, "utf-8");
1546
+ if (/\bcreateRouter\s*[<(]/.test(source)) {
1547
+ routerFiles.push(filePath);
1548
+ }
1549
+ if (source.includes("urls(")) {
1550
+ urlsFiles.push(filePath);
1551
+ }
1552
+ } catch (err) {
1553
+ console.warn(
1554
+ `[rango] Failed to process ${filePath}: ${err.message}`
1555
+ );
1556
+ }
1557
+ }
1558
+ const allDiagnostics = [];
1559
+ for (const routerFile of routerFiles) {
1560
+ const diagnostics = detectUnresolvableIncludes(routerFile);
1561
+ for (const d of diagnostics) {
1562
+ allDiagnostics.push({ ...d, routerFile });
1563
+ }
1564
+ }
1565
+ const routerFileSet = new Set(routerFiles);
1566
+ for (const urlsFile of urlsFiles) {
1567
+ if (routerFileSet.has(urlsFile)) continue;
1568
+ const diagnostics = detectUnresolvableIncludesForUrlsFile(urlsFile);
1569
+ for (const d of diagnostics) {
1570
+ allDiagnostics.push({ ...d, routerFile: urlsFile });
1571
+ }
1572
+ }
1573
+ const seen = /* @__PURE__ */ new Set();
1574
+ const uniqueDiagnostics = allDiagnostics.filter((d) => {
1575
+ const key = `${d.sourceFile}:${d.pathPrefix}:${d.reason}`;
1576
+ if (seen.has(key)) return false;
1577
+ seen.add(key);
1578
+ return true;
1579
+ });
1580
+ if (uniqueDiagnostics.length > 0 && mode === "default") {
1581
+ console.error("\n[rango] Unresolvable includes detected:\n");
1582
+ formatDiagnostics(uniqueDiagnostics);
1583
+ console.error(
1584
+ "\nThe static parser cannot resolve these includes because they use factory functions or dynamic expressions.\n\nOptions:\n rango generate <path> --runtime Use Vite-based discovery (requires vite)\n rango generate <path> --static Accept partial output (missing routes above)\n"
1585
+ );
1586
+ process.exit(1);
1587
+ }
1588
+ if (uniqueDiagnostics.length > 0 && mode === "static") {
1589
+ console.warn(
1590
+ "\n[rango] Warning: partial output (unresolvable includes):\n"
1591
+ );
1592
+ formatDiagnostics(uniqueDiagnostics);
1593
+ console.warn("");
1594
+ }
1595
+ const nestedRouterConflict = findNestedRouterConflict(routerFiles);
1596
+ if (nestedRouterConflict) {
1597
+ console.error(
1598
+ `
1599
+ ${formatNestedRouterConflictError(nestedRouterConflict, "[rango]")}
1600
+ `
1601
+ );
1602
+ process.exit(1);
1603
+ }
1604
+ for (const urlsFile of urlsFiles) {
1605
+ writePerModuleRouteTypesForFile(urlsFile);
1606
+ }
1607
+ for (const routerFile of routerFiles) {
1608
+ const projectRoot = findProjectRoot(routerFile);
1609
+ writeCombinedRouteTypes(projectRoot, [routerFile]);
1610
+ }
1611
+ console.log(
1612
+ `[rango] Processed ${files.length} file(s)${routerFiles.length ? ` (${routerFiles.length} router)` : ""}`
1613
+ );
1614
+ process.exit(0);
1615
+ }
1616
+ async function runRuntimeDiscovery(args, configFile) {
1617
+ const files = [];
1618
+ for (const arg of args) {
1619
+ const resolved = resolve4(arg);
1620
+ try {
1621
+ if (statSync(resolved).isDirectory()) {
1622
+ files.push(...findTsFiles(resolved));
1623
+ } else {
1624
+ files.push(resolved);
1625
+ }
1626
+ } catch {
1627
+ console.warn(`[rango] Skipping ${arg}: not found`);
1628
+ }
1629
+ }
1630
+ const routerEntries = [];
1631
+ for (const filePath of files) {
1632
+ try {
1633
+ const source = readFileSync5(filePath, "utf-8");
1634
+ if (/\bcreateRouter\s*[<(]/.test(source)) {
1635
+ routerEntries.push(filePath);
1636
+ }
1637
+ if (source.includes("urls(")) {
1638
+ writePerModuleRouteTypesForFile(filePath);
1639
+ }
1640
+ } catch {
1641
+ }
1642
+ }
1643
+ if (routerEntries.length === 0) {
1644
+ console.error("[rango] No router files found in the provided paths");
1645
+ process.exit(1);
1646
+ }
1647
+ const nestedRouterConflict = findNestedRouterConflict(routerEntries);
1648
+ if (nestedRouterConflict) {
1649
+ console.error(
1650
+ `
1651
+ ${formatNestedRouterConflictError(nestedRouterConflict, "[rango]")}
1652
+ `
1653
+ );
1654
+ process.exit(1);
1655
+ }
1656
+ let discoverAndWriteRouteTypes2;
1657
+ try {
1658
+ const mod = await Promise.resolve().then(() => (init_runtime_discovery(), runtime_discovery_exports));
1659
+ discoverAndWriteRouteTypes2 = mod.discoverAndWriteRouteTypes;
1660
+ } catch (err) {
1661
+ if (err.code === "ERR_MODULE_NOT_FOUND" || err.code === "MODULE_NOT_FOUND") {
1662
+ console.error(
1663
+ "[rango] Runtime discovery requires 'vite' and '@vitejs/plugin-rsc'.\nInstall them with: pnpm add -D vite @vitejs/plugin-rsc"
1664
+ );
1665
+ } else {
1666
+ console.error(`[rango] Failed to load runtime discovery: ${err.message}`);
1667
+ }
1668
+ process.exit(1);
1669
+ }
1670
+ for (const entry of routerEntries) {
1671
+ const projectRoot = findProjectRoot(entry);
1672
+ const result = await discoverAndWriteRouteTypes2({
1673
+ root: projectRoot,
1674
+ configFile,
1675
+ entry
1676
+ });
1677
+ console.log(
1678
+ `[rango] Runtime discovery: ${result.routerCount} router(s), ${result.routeCount} route(s)`
1679
+ );
1680
+ }
1681
+ }
1682
+ function formatDiagnostics(diagnostics) {
1683
+ for (const d of diagnostics) {
1684
+ const prefix = d.namePrefix ? `${d.namePrefix}.*` : `${d.pathPrefix}*`;
1685
+ console.error(` ${prefix}`);
1686
+ console.error(` Reason: ${d.reason} -- ${d.detail}`);
1687
+ console.error(` Source: ${d.sourceFile}`);
1688
+ }
1689
+ }