@rangojs/router 0.0.0-experimental.10 → 0.0.0-experimental.100

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