@rangojs/router 0.0.0-experimental.0f44aca1

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