@rangojs/router 0.0.0-experimental.13 → 0.0.0-experimental.13221847

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