@rangojs/router 0.0.0-experimental.7 → 0.0.0-experimental.71

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 (307) hide show
  1. package/AGENTS.md +9 -0
  2. package/README.md +942 -4
  3. package/dist/bin/rango.js +1689 -0
  4. package/dist/vite/index.js +4951 -930
  5. package/package.json +70 -60
  6. package/skills/breadcrumbs/SKILL.md +250 -0
  7. package/skills/cache-guide/SKILL.md +294 -0
  8. package/skills/caching/SKILL.md +93 -23
  9. package/skills/composability/SKILL.md +172 -0
  10. package/skills/debug-manifest/SKILL.md +12 -8
  11. package/skills/document-cache/SKILL.md +18 -16
  12. package/skills/fonts/SKILL.md +167 -0
  13. package/skills/hooks/SKILL.md +334 -72
  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 +92 -31
  18. package/skills/loader/SKILL.md +404 -44
  19. package/skills/middleware/SKILL.md +173 -34
  20. package/skills/mime-routes/SKILL.md +128 -0
  21. package/skills/parallel/SKILL.md +204 -1
  22. package/skills/prerender/SKILL.md +685 -0
  23. package/skills/rango/SKILL.md +85 -16
  24. package/skills/response-routes/SKILL.md +411 -0
  25. package/skills/route/SKILL.md +257 -14
  26. package/skills/router-setup/SKILL.md +210 -32
  27. package/skills/tailwind/SKILL.md +129 -0
  28. package/skills/theme/SKILL.md +9 -8
  29. package/skills/typesafety/SKILL.md +328 -89
  30. package/skills/use-cache/SKILL.md +324 -0
  31. package/src/__internal.ts +102 -4
  32. package/src/bin/rango.ts +321 -0
  33. package/src/browser/action-coordinator.ts +97 -0
  34. package/src/browser/action-response-classifier.ts +99 -0
  35. package/src/browser/app-version.ts +14 -0
  36. package/src/browser/event-controller.ts +92 -64
  37. package/src/browser/history-state.ts +80 -0
  38. package/src/browser/intercept-utils.ts +52 -0
  39. package/src/browser/link-interceptor.ts +24 -4
  40. package/src/browser/logging.ts +55 -0
  41. package/src/browser/merge-segment-loaders.ts +20 -12
  42. package/src/browser/navigation-bridge.ts +296 -558
  43. package/src/browser/navigation-client.ts +179 -69
  44. package/src/browser/navigation-store.ts +73 -55
  45. package/src/browser/navigation-transaction.ts +297 -0
  46. package/src/browser/network-error-handler.ts +61 -0
  47. package/src/browser/partial-update.ts +328 -313
  48. package/src/browser/prefetch/cache.ts +206 -0
  49. package/src/browser/prefetch/fetch.ts +150 -0
  50. package/src/browser/prefetch/observer.ts +65 -0
  51. package/src/browser/prefetch/policy.ts +48 -0
  52. package/src/browser/prefetch/queue.ts +160 -0
  53. package/src/browser/prefetch/resource-ready.ts +77 -0
  54. package/src/browser/rango-state.ts +112 -0
  55. package/src/browser/react/Link.tsx +230 -74
  56. package/src/browser/react/NavigationProvider.tsx +87 -11
  57. package/src/browser/react/context.ts +11 -0
  58. package/src/browser/react/filter-segment-order.ts +11 -0
  59. package/src/browser/react/index.ts +12 -12
  60. package/src/browser/react/location-state-shared.ts +95 -53
  61. package/src/browser/react/location-state.ts +60 -15
  62. package/src/browser/react/mount-context.ts +6 -1
  63. package/src/browser/react/nonce-context.ts +23 -0
  64. package/src/browser/react/shallow-equal.ts +27 -0
  65. package/src/browser/react/use-action.ts +29 -51
  66. package/src/browser/react/use-client-cache.ts +5 -3
  67. package/src/browser/react/use-handle.ts +30 -126
  68. package/src/browser/react/use-href.tsx +2 -2
  69. package/src/browser/react/use-link-status.ts +6 -5
  70. package/src/browser/react/use-navigation.ts +22 -63
  71. package/src/browser/react/use-params.ts +65 -0
  72. package/src/browser/react/use-pathname.ts +47 -0
  73. package/src/browser/react/use-router.ts +76 -0
  74. package/src/browser/react/use-search-params.ts +56 -0
  75. package/src/browser/react/use-segments.ts +80 -97
  76. package/src/browser/response-adapter.ts +73 -0
  77. package/src/browser/rsc-router.tsx +214 -58
  78. package/src/browser/scroll-restoration.ts +127 -52
  79. package/src/browser/segment-reconciler.ts +221 -0
  80. package/src/browser/segment-structure-assert.ts +16 -0
  81. package/src/browser/server-action-bridge.ts +510 -603
  82. package/src/browser/shallow.ts +6 -1
  83. package/src/browser/types.ts +141 -48
  84. package/src/browser/validate-redirect-origin.ts +29 -0
  85. package/src/build/generate-manifest.ts +235 -24
  86. package/src/build/generate-route-types.ts +39 -0
  87. package/src/build/index.ts +13 -0
  88. package/src/build/route-trie.ts +265 -0
  89. package/src/build/route-types/ast-helpers.ts +25 -0
  90. package/src/build/route-types/ast-route-extraction.ts +98 -0
  91. package/src/build/route-types/codegen.ts +102 -0
  92. package/src/build/route-types/include-resolution.ts +418 -0
  93. package/src/build/route-types/param-extraction.ts +48 -0
  94. package/src/build/route-types/per-module-writer.ts +128 -0
  95. package/src/build/route-types/router-processing.ts +618 -0
  96. package/src/build/route-types/scan-filter.ts +85 -0
  97. package/src/build/runtime-discovery.ts +231 -0
  98. package/src/cache/background-task.ts +34 -0
  99. package/src/cache/cache-key-utils.ts +44 -0
  100. package/src/cache/cache-policy.ts +125 -0
  101. package/src/cache/cache-runtime.ts +342 -0
  102. package/src/cache/cache-scope.ts +167 -309
  103. package/src/cache/cf/cf-cache-store.ts +571 -17
  104. package/src/cache/cf/index.ts +13 -3
  105. package/src/cache/document-cache.ts +116 -77
  106. package/src/cache/handle-capture.ts +81 -0
  107. package/src/cache/handle-snapshot.ts +41 -0
  108. package/src/cache/index.ts +1 -15
  109. package/src/cache/memory-segment-store.ts +191 -13
  110. package/src/cache/profile-registry.ts +73 -0
  111. package/src/cache/read-through-swr.ts +134 -0
  112. package/src/cache/segment-codec.ts +256 -0
  113. package/src/cache/taint.ts +153 -0
  114. package/src/cache/types.ts +72 -122
  115. package/src/client.rsc.tsx +3 -1
  116. package/src/client.tsx +105 -179
  117. package/src/component-utils.ts +4 -4
  118. package/src/components/DefaultDocument.tsx +5 -1
  119. package/src/context-var.ts +156 -0
  120. package/src/debug.ts +19 -9
  121. package/src/errors.ts +108 -2
  122. package/src/handle.ts +55 -29
  123. package/src/handles/MetaTags.tsx +73 -20
  124. package/src/handles/breadcrumbs.ts +66 -0
  125. package/src/handles/index.ts +1 -0
  126. package/src/handles/meta.ts +30 -13
  127. package/src/host/cookie-handler.ts +21 -15
  128. package/src/host/errors.ts +8 -8
  129. package/src/host/index.ts +4 -7
  130. package/src/host/pattern-matcher.ts +27 -27
  131. package/src/host/router.ts +61 -39
  132. package/src/host/testing.ts +8 -8
  133. package/src/host/types.ts +15 -7
  134. package/src/host/utils.ts +1 -1
  135. package/src/href-client.ts +119 -29
  136. package/src/index.rsc.ts +155 -19
  137. package/src/index.ts +223 -30
  138. package/src/internal-debug.ts +11 -0
  139. package/src/loader.rsc.ts +26 -157
  140. package/src/loader.ts +27 -10
  141. package/src/network-error-thrower.tsx +3 -1
  142. package/src/outlet-provider.tsx +45 -0
  143. package/src/prerender/param-hash.ts +37 -0
  144. package/src/prerender/store.ts +186 -0
  145. package/src/prerender.ts +524 -0
  146. package/src/reverse.ts +351 -0
  147. package/src/root-error-boundary.tsx +41 -29
  148. package/src/route-content-wrapper.tsx +7 -4
  149. package/src/route-definition/dsl-helpers.ts +982 -0
  150. package/src/route-definition/helper-factories.ts +200 -0
  151. package/src/route-definition/helpers-types.ts +434 -0
  152. package/src/route-definition/index.ts +55 -0
  153. package/src/route-definition/redirect.ts +101 -0
  154. package/src/route-definition/resolve-handler-use.ts +149 -0
  155. package/src/route-definition.ts +1 -1428
  156. package/src/route-map-builder.ts +217 -123
  157. package/src/route-name.ts +53 -0
  158. package/src/route-types.ts +70 -8
  159. package/src/router/content-negotiation.ts +215 -0
  160. package/src/router/debug-manifest.ts +72 -0
  161. package/src/router/error-handling.ts +9 -9
  162. package/src/router/find-match.ts +160 -0
  163. package/src/router/handler-context.ts +435 -86
  164. package/src/router/intercept-resolution.ts +402 -0
  165. package/src/router/lazy-includes.ts +237 -0
  166. package/src/router/loader-resolution.ts +356 -128
  167. package/src/router/logging.ts +251 -0
  168. package/src/router/manifest.ts +154 -35
  169. package/src/router/match-api.ts +555 -0
  170. package/src/router/match-context.ts +5 -3
  171. package/src/router/match-handlers.ts +440 -0
  172. package/src/router/match-middleware/background-revalidation.ts +108 -93
  173. package/src/router/match-middleware/cache-lookup.ts +459 -10
  174. package/src/router/match-middleware/cache-store.ts +98 -26
  175. package/src/router/match-middleware/intercept-resolution.ts +57 -17
  176. package/src/router/match-middleware/segment-resolution.ts +80 -6
  177. package/src/router/match-pipelines.ts +10 -45
  178. package/src/router/match-result.ts +135 -35
  179. package/src/router/metrics.ts +240 -15
  180. package/src/router/middleware-cookies.ts +55 -0
  181. package/src/router/middleware-types.ts +220 -0
  182. package/src/router/middleware.ts +324 -369
  183. package/src/router/navigation-snapshot.ts +182 -0
  184. package/src/router/pattern-matching.ts +211 -43
  185. package/src/router/prerender-match.ts +502 -0
  186. package/src/router/preview-match.ts +98 -0
  187. package/src/router/request-classification.ts +310 -0
  188. package/src/router/revalidation.ts +137 -38
  189. package/src/router/route-snapshot.ts +245 -0
  190. package/src/router/router-context.ts +41 -21
  191. package/src/router/router-interfaces.ts +484 -0
  192. package/src/router/router-options.ts +618 -0
  193. package/src/router/router-registry.ts +24 -0
  194. package/src/router/segment-resolution/fresh.ts +748 -0
  195. package/src/router/segment-resolution/helpers.ts +268 -0
  196. package/src/router/segment-resolution/loader-cache.ts +199 -0
  197. package/src/router/segment-resolution/revalidation.ts +1379 -0
  198. package/src/router/segment-resolution/static-store.ts +67 -0
  199. package/src/router/segment-resolution.ts +21 -0
  200. package/src/router/segment-wrappers.ts +291 -0
  201. package/src/router/telemetry-otel.ts +299 -0
  202. package/src/router/telemetry.ts +300 -0
  203. package/src/router/timeout.ts +148 -0
  204. package/src/router/trie-matching.ts +239 -0
  205. package/src/router/types.ts +78 -3
  206. package/src/router.ts +740 -4252
  207. package/src/rsc/handler-context.ts +45 -0
  208. package/src/rsc/handler.ts +907 -797
  209. package/src/rsc/helpers.ts +140 -6
  210. package/src/rsc/index.ts +0 -20
  211. package/src/rsc/loader-fetch.ts +229 -0
  212. package/src/rsc/manifest-init.ts +90 -0
  213. package/src/rsc/nonce.ts +14 -0
  214. package/src/rsc/origin-guard.ts +141 -0
  215. package/src/rsc/progressive-enhancement.ts +391 -0
  216. package/src/rsc/response-error.ts +37 -0
  217. package/src/rsc/response-route-handler.ts +347 -0
  218. package/src/rsc/rsc-rendering.ts +246 -0
  219. package/src/rsc/runtime-warnings.ts +42 -0
  220. package/src/rsc/server-action.ts +356 -0
  221. package/src/rsc/ssr-setup.ts +128 -0
  222. package/src/rsc/types.ts +46 -11
  223. package/src/search-params.ts +230 -0
  224. package/src/segment-system.tsx +165 -17
  225. package/src/server/context.ts +315 -58
  226. package/src/server/cookie-store.ts +190 -0
  227. package/src/server/fetchable-loader-store.ts +37 -0
  228. package/src/server/handle-store.ts +113 -15
  229. package/src/server/loader-registry.ts +24 -64
  230. package/src/server/request-context.ts +607 -81
  231. package/src/server.ts +35 -130
  232. package/src/ssr/index.tsx +103 -30
  233. package/src/static-handler.ts +126 -0
  234. package/src/theme/ThemeProvider.tsx +21 -15
  235. package/src/theme/ThemeScript.tsx +5 -5
  236. package/src/theme/constants.ts +5 -2
  237. package/src/theme/index.ts +4 -14
  238. package/src/theme/theme-context.ts +4 -30
  239. package/src/theme/theme-script.ts +21 -18
  240. package/src/types/boundaries.ts +158 -0
  241. package/src/types/cache-types.ts +198 -0
  242. package/src/types/error-types.ts +192 -0
  243. package/src/types/global-namespace.ts +100 -0
  244. package/src/types/handler-context.ts +791 -0
  245. package/src/types/index.ts +88 -0
  246. package/src/types/loader-types.ts +210 -0
  247. package/src/types/route-config.ts +170 -0
  248. package/src/types/route-entry.ts +109 -0
  249. package/src/types/segments.ts +151 -0
  250. package/src/types.ts +1 -1623
  251. package/src/urls/include-helper.ts +197 -0
  252. package/src/urls/index.ts +53 -0
  253. package/src/urls/path-helper-types.ts +346 -0
  254. package/src/urls/path-helper.ts +364 -0
  255. package/src/urls/pattern-types.ts +107 -0
  256. package/src/urls/response-types.ts +116 -0
  257. package/src/urls/type-extraction.ts +372 -0
  258. package/src/urls/urls-function.ts +98 -0
  259. package/src/urls.ts +1 -802
  260. package/src/use-loader.tsx +161 -81
  261. package/src/vite/discovery/bundle-postprocess.ts +181 -0
  262. package/src/vite/discovery/discover-routers.ts +348 -0
  263. package/src/vite/discovery/prerender-collection.ts +439 -0
  264. package/src/vite/discovery/route-types-writer.ts +258 -0
  265. package/src/vite/discovery/self-gen-tracking.ts +47 -0
  266. package/src/vite/discovery/state.ts +117 -0
  267. package/src/vite/discovery/virtual-module-codegen.ts +203 -0
  268. package/src/vite/index.ts +15 -1129
  269. package/src/vite/plugin-types.ts +103 -0
  270. package/src/vite/plugins/cjs-to-esm.ts +93 -0
  271. package/src/vite/plugins/client-ref-dedup.ts +115 -0
  272. package/src/vite/plugins/client-ref-hashing.ts +105 -0
  273. package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +72 -53
  274. package/src/vite/plugins/expose-id-utils.ts +299 -0
  275. package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
  276. package/src/vite/plugins/expose-ids/handler-transform.ts +209 -0
  277. package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
  278. package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
  279. package/src/vite/plugins/expose-ids/types.ts +45 -0
  280. package/src/vite/plugins/expose-internal-ids.ts +786 -0
  281. package/src/vite/plugins/performance-tracks.ts +88 -0
  282. package/src/vite/plugins/refresh-cmd.ts +127 -0
  283. package/src/vite/plugins/use-cache-transform.ts +323 -0
  284. package/src/vite/plugins/version-injector.ts +83 -0
  285. package/src/vite/plugins/version-plugin.ts +266 -0
  286. package/src/vite/{virtual-entries.ts → plugins/virtual-entries.ts} +23 -14
  287. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  288. package/src/vite/rango.ts +462 -0
  289. package/src/vite/router-discovery.ts +918 -0
  290. package/src/vite/utils/ast-handler-extract.ts +517 -0
  291. package/src/vite/utils/banner.ts +36 -0
  292. package/src/vite/utils/bundle-analysis.ts +137 -0
  293. package/src/vite/utils/manifest-utils.ts +70 -0
  294. package/src/vite/{package-resolution.ts → utils/package-resolution.ts} +25 -29
  295. package/src/vite/utils/prerender-utils.ts +207 -0
  296. package/src/vite/utils/shared-utils.ts +170 -0
  297. package/CLAUDE.md +0 -43
  298. package/src/browser/lru-cache.ts +0 -69
  299. package/src/browser/request-controller.ts +0 -164
  300. package/src/cache/memory-store.ts +0 -253
  301. package/src/href-context.ts +0 -33
  302. package/src/href.ts +0 -255
  303. package/src/server/route-manifest-cache.ts +0 -173
  304. package/src/vite/expose-handle-id.ts +0 -209
  305. package/src/vite/expose-loader-id.ts +0 -426
  306. package/src/vite/expose-location-state-id.ts +0 -177
  307. /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
package/src/vite/index.ts CHANGED
@@ -1,1134 +1,20 @@
1
- import type { Plugin, PluginOption } from "vite";
2
- import { createServer as createViteServer } from "vite";
3
- import * as Vite from "vite";
4
- import { resolve, join } from "node:path";
5
- import { createHash } from "node:crypto";
6
- import { mkdirSync, writeFileSync, readFileSync, existsSync } from "node:fs";
7
- import { exposeActionId } from "./expose-action-id.ts";
8
- import { exposeLoaderId } from "./expose-loader-id.ts";
9
- import { exposeHandleId } from "./expose-handle-id.ts";
10
- import { exposeLocationStateId } from "./expose-location-state-id.ts";
11
- import {
12
- VIRTUAL_ENTRY_BROWSER,
13
- VIRTUAL_ENTRY_SSR,
14
- getVirtualEntryRSC,
15
- getVirtualVersionContent,
16
- VIRTUAL_IDS,
17
- } from "./virtual-entries.ts";
18
- import {
19
- getExcludeDeps,
20
- getPackageAliases,
21
- getPublishedPackageName,
22
- isWorkspaceDevelopment,
23
- } from "./package-resolution.ts";
24
-
25
- // Re-export plugins
26
- export { exposeActionId } from "./expose-action-id.ts";
27
- export { exposeLoaderId } from "./expose-loader-id.ts";
28
- export { exposeHandleId } from "./expose-handle-id.ts";
29
- export { exposeLocationStateId } from "./expose-location-state-id.ts";
30
-
31
- // Virtual module type declarations in ./version.d.ts
32
-
33
- /**
34
- * esbuild plugin to provide rsc-router:version virtual module during optimization.
35
- * This is needed because esbuild runs during Vite's dependency optimization phase,
36
- * before Vite's plugin system can handle virtual modules.
37
- */
38
- const versionEsbuildPlugin = {
39
- name: "@rangojs/router-version",
40
- setup(build: any) {
41
- build.onResolve({ filter: /^rsc-router:version$/ }, (args: any) => ({
42
- path: args.path,
43
- namespace: "@rangojs/router-virtual",
44
- }));
45
- build.onLoad({ filter: /.*/, namespace: "@rangojs/router-virtual" }, () => ({
46
- contents: `export const VERSION = "dev";`,
47
- loader: "js",
48
- }));
49
- },
50
- };
51
-
52
- /**
53
- * Shared esbuild options for dependency optimization.
54
- * Includes the version stub plugin for all environments.
55
- */
56
- const sharedEsbuildOptions = {
57
- plugins: [versionEsbuildPlugin],
58
- };
59
-
60
- /**
61
- * RSC plugin entry points configuration.
62
- * All entries use virtual modules by default. Specify a path to use a custom entry file.
63
- */
64
- export interface RscEntries {
65
- /**
66
- * Path to a custom browser/client entry file.
67
- * If not specified, a default virtual entry is used.
68
- */
69
- client?: string;
70
-
71
- /**
72
- * Path to a custom SSR entry file.
73
- * If not specified, a default virtual entry is used.
74
- */
75
- ssr?: string;
76
-
77
- /**
78
- * Path to a custom RSC entry file.
79
- * If not specified, a default virtual entry is used that imports the router from the `entry` option.
80
- */
81
- rsc?: string;
82
- }
83
-
84
- /**
85
- * Options for @vitejs/plugin-rsc integration
86
- */
87
- export interface RscPluginOptions {
88
- /**
89
- * Entry points for client, ssr, and rsc environments.
90
- * All entries use virtual modules by default.
91
- * Specify paths only when you need custom entry files.
92
- */
93
- entries?: RscEntries;
94
- }
95
-
96
- /**
97
- * Base options shared by all presets
98
- */
99
- interface RscRouterBaseOptions {
100
- /**
101
- * Expose $$id property on server action functions.
102
- * Required for action-based revalidation to work.
103
- * @default true
104
- */
105
- exposeActionId?: boolean;
106
- }
107
-
108
- /**
109
- * Options for Node.js deployment (default)
110
- */
111
- export interface RscRouterNodeOptions extends RscRouterBaseOptions {
112
- /**
113
- * Deployment preset. Defaults to 'node' when not specified.
114
- */
115
- preset?: "node";
116
-
117
- /**
118
- * Path to your router configuration file that exports the route tree.
119
- * This file must export a `router` object created with `createRouter()`.
120
- *
121
- * @example
122
- * ```ts
123
- * rscRouter({ router: './src/router.tsx' })
124
- * ```
125
- */
126
- router: string;
127
-
128
- /**
129
- * RSC plugin configuration. By default, rsc-router includes @vitejs/plugin-rsc
130
- * with sensible defaults.
131
- *
132
- * Entry files (browser, ssr, rsc) are optional - if they don't exist,
133
- * virtual defaults are used.
134
- *
135
- * - Omit or pass `true`/`{}` to use defaults (recommended)
136
- * - Pass `{ entries: {...} }` to customize entry paths
137
- * - Pass `false` to disable (for manual @vitejs/plugin-rsc configuration)
138
- *
139
- * @default true
140
- */
141
- rsc?: boolean | RscPluginOptions;
142
- }
143
-
144
- /**
145
- * Options for Cloudflare Workers deployment
146
- */
147
- export interface RscRouterCloudflareOptions extends RscRouterBaseOptions {
148
- /**
149
- * Deployment preset for Cloudflare Workers.
150
- * When using cloudflare preset:
151
- * - @vitejs/plugin-rsc is NOT added (cloudflare plugin adds it)
152
- * - Your worker entry (e.g., worker.rsc.tsx) imports the router directly
153
- * - Browser and SSR use virtual entries
154
- * - Build-time manifest generation is auto-detected from wrangler.json main entry
155
- */
156
- preset: "cloudflare";
157
- }
158
-
159
- /**
160
- * Options for rscRouter plugin
161
- */
162
- export type RscRouterOptions = RscRouterNodeOptions | RscRouterCloudflareOptions;
163
-
164
- /**
165
- * Create a virtual modules plugin for default entry files.
166
- * Provides virtual module content when entries use VIRTUAL_IDS (no custom entry configured).
167
- */
168
- function createVirtualEntriesPlugin(
169
- entries: { client: string; ssr: string; rsc?: string },
170
- routerPath?: string
171
- ): Plugin {
172
-
173
- // Build virtual modules map based on which entries use virtual IDs
174
- const virtualModules: Record<string, string> = {};
175
-
176
- if (entries.client === VIRTUAL_IDS.browser) {
177
- virtualModules[VIRTUAL_IDS.browser] = VIRTUAL_ENTRY_BROWSER;
178
- }
179
- if (entries.ssr === VIRTUAL_IDS.ssr) {
180
- virtualModules[VIRTUAL_IDS.ssr] = VIRTUAL_ENTRY_SSR;
181
- }
182
- if (entries.rsc === VIRTUAL_IDS.rsc && routerPath) {
183
- // Convert relative path to absolute for virtual module imports
184
- const absoluteRouterPath = routerPath.startsWith(".")
185
- ? "/" + routerPath.slice(2) // ./src/router.tsx -> /src/router.tsx
186
- : routerPath;
187
- virtualModules[VIRTUAL_IDS.rsc] = getVirtualEntryRSC(absoluteRouterPath);
188
- }
189
-
190
- return {
191
- name: "@rangojs/router:virtual-entries",
192
- enforce: "pre",
193
-
194
- resolveId(id) {
195
- if (id in virtualModules) {
196
- return "\0" + id;
197
- }
198
- // Handle if the id already has the null prefix (RSC plugin wrapper imports)
199
- if (id.startsWith("\0") && id.slice(1) in virtualModules) {
200
- return id;
201
- }
202
- return null;
203
- },
204
-
205
- load(id) {
206
- if (id.startsWith("\0virtual:rsc-router/")) {
207
- const virtualId = id.slice(1);
208
- if (virtualId in virtualModules) {
209
- return virtualModules[virtualId];
210
- }
211
- }
212
- return null;
213
- },
214
- };
215
- }
216
-
217
- /**
218
- * Manual chunks configuration for client build.
219
- * Splits React and router packages into separate chunks for better caching.
220
- */
221
- function getManualChunks(id: string): string | undefined {
222
- const normalized = Vite.normalizePath(id);
223
-
224
- if (
225
- normalized.includes("node_modules/react/") ||
226
- normalized.includes("node_modules/react-dom/") ||
227
- normalized.includes("node_modules/react-server-dom-webpack/") ||
228
- normalized.includes("node_modules/@vitejs/plugin-rsc/")
229
- ) {
230
- return "react";
231
- }
232
- // Use dynamic package name from package.json
233
- // Check both npm install path and workspace symlink resolved path
234
- const packageName = getPublishedPackageName();
235
- if (
236
- normalized.includes(`node_modules/${packageName}/`) ||
237
- normalized.includes("packages/rsc-router/") ||
238
- normalized.includes("packages/rangojs-router/")
239
- ) {
240
- return "router";
241
- }
242
- return undefined;
243
- }
244
-
245
- /**
246
- * Plugin providing rsc-router:version virtual module.
247
- * Exports VERSION that changes when RSC modules change (dev) or at build time (production).
248
- *
249
- * The version is used for:
250
- * 1. Cache invalidation - CFCacheStore uses VERSION to invalidate stale cache
251
- * 2. Version mismatch detection - client sends version, server reloads on mismatch
252
- *
253
- * In dev mode, the version updates when:
254
- * - Server starts (initial version)
255
- * - RSC modules change via HMR (triggers version module invalidation)
256
- *
257
- * Client-only HMR changes don't update the version since they don't affect
258
- * server-rendered content or cached RSC payloads.
259
- * @internal
260
- */
261
- function createVersionPlugin(): Plugin {
262
- // Generate version at plugin creation time (build/server start)
263
- const buildVersion = Date.now().toString(16);
264
- let currentVersion = buildVersion;
265
- let isDev = false;
266
- let server: any = null;
267
-
268
- return {
269
- name: "@rangojs/router:version",
270
- enforce: "pre",
271
-
272
- configResolved(config) {
273
- isDev = config.command === "serve";
274
- },
275
-
276
- configureServer(devServer) {
277
- server = devServer;
278
- },
279
-
280
- resolveId(id) {
281
- if (id === VIRTUAL_IDS.version) {
282
- return "\0" + id;
283
- }
284
- return null;
285
- },
286
-
287
- load(id) {
288
- if (id === "\0" + VIRTUAL_IDS.version) {
289
- return getVirtualVersionContent(currentVersion);
290
- }
291
- return null;
292
- },
293
-
294
- // Track RSC module changes and update version
295
- hotUpdate(ctx) {
296
- if (!isDev) return;
297
-
298
- // Check if this is an RSC environment update (not client/ssr)
299
- // RSC modules affect server-rendered content and cached payloads
300
- // In Vite 6, environment is accessed via `this.environment`
301
- const isRscModule = this.environment?.name === "rsc";
302
-
303
- if (isRscModule && ctx.modules.length > 0) {
304
- // Update version when RSC modules change
305
- currentVersion = Date.now().toString(16);
306
- console.log(
307
- `[rsc-router] RSC module changed, version updated: ${currentVersion}`
308
- );
309
-
310
- // Invalidate the version module so it gets reloaded with new version
311
- if (server) {
312
- const rscEnv = server.environments?.rsc;
313
- if (rscEnv?.moduleGraph) {
314
- const versionMod = rscEnv.moduleGraph.getModuleById(
315
- "\0" + VIRTUAL_IDS.version
316
- );
317
- if (versionMod) {
318
- rscEnv.moduleGraph.invalidateModule(versionMod);
319
- }
320
- }
321
- }
322
- }
323
- },
324
- };
325
- }
326
-
327
- /**
328
- * Plugin that discovers router instances at dev/build time via the RSC environment.
329
- *
330
- * Uses `server.environments.rsc.runner.import()` to load the user's router file
331
- * with full TS/TSX compilation. This triggers `createRouter()` which populates
332
- * the `RouterRegistry`. The plugin then generates manifests for each router.
333
- *
334
- * In dev mode, this runs in `configureServer` (post-middleware setup).
335
- * In build mode, this will run in `buildStart` (future).
336
- *
337
- * @internal
338
- */
339
- function createRouterDiscoveryPlugin(entryPath: string): Plugin {
340
- let projectRoot = "";
341
- let isBuildMode = false;
342
- let userResolveAlias: any = undefined;
343
-
344
- // Merged route manifest from all discovered routers.
345
- // Populated during discovery (dev: configureServer, build: buildStart).
346
- // Read by the virtual module's load hook to emit setCachedManifest() call.
347
- let mergedRouteManifest: Record<string, string> | null = null;
348
-
349
- // Shared discovery logic: import entry via RSC runner, generate manifests,
350
- // write static files, and populate mergedRouteManifest.
351
- async function discoverRouters(rscEnv: any) {
352
- // Import the entry file via RSC environment.
353
- // For node preset: this is the router file (createRouter() registers in RouterRegistry).
354
- // For cloudflare preset: this is the worker entry (which imports the router).
355
- await rscEnv.runner.import(entryPath);
356
-
357
- // Import the router package to access the registry
358
- const serverMod = await rscEnv.runner.import("@rangojs/router/server");
359
- let registry: Map<string, any> = serverMod.RouterRegistry;
360
-
361
- if (!registry || registry.size === 0) {
362
- // No RSC routers found directly. Check for host routers with lazy handlers
363
- // that need to be resolved to trigger sub-app createRouter() calls.
364
- try {
365
- const hostMod = await rscEnv.runner.import("@rangojs/router/host");
366
- const hostRegistry: Map<string, any> | undefined = hostMod.HostRouterRegistry;
367
-
368
- if (hostRegistry && hostRegistry.size > 0) {
369
- console.log(
370
- `[rsc-router] Found ${hostRegistry.size} host router(s), resolving lazy handlers...`
371
- );
372
-
373
- for (const [, entry] of hostRegistry) {
374
- for (const route of entry.routes) {
375
- if (typeof route.handler === 'function') {
376
- try {
377
- await route.handler();
378
- } catch {
379
- // Lazy handler may fail in temp server context, that's OK
380
- }
381
- }
382
- }
383
- if (entry.fallback && typeof entry.fallback.handler === 'function') {
384
- try {
385
- await entry.fallback.handler();
386
- } catch {
387
- // Fallback handler may fail in temp server context
388
- }
389
- }
390
- }
391
-
392
- // Re-read RouterRegistry - sub-app createRouter() calls should have populated it
393
- const freshServerMod = await rscEnv.runner.import("@rangojs/router/server");
394
- const freshRegistry: Map<string, any> = freshServerMod.RouterRegistry;
395
-
396
- if (freshRegistry && freshRegistry.size > 0) {
397
- // Update references so the manifest generation below uses the fresh data
398
- Object.assign(serverMod, freshServerMod);
399
- registry = freshRegistry;
400
- }
401
- }
402
- } catch {
403
- // @rangojs/router/host not available or import failed, skip
404
- }
405
-
406
- // If still no routers after host router resolution, fail
407
- if (!registry || registry.size === 0) {
408
- throw new Error(
409
- `[rsc-router] No routers found in registry after importing ${entryPath}`
410
- );
411
- }
412
- }
413
-
414
- // Import build utilities for manifest generation
415
- const buildMod = await rscEnv.runner.import("@rangojs/router/build");
416
- const generateManifest = buildMod.generateManifest;
417
-
418
- mergedRouteManifest = {};
419
-
420
- for (const [id, router] of registry) {
421
- if (!router.urlpatterns || !generateManifest) {
422
- continue;
423
- }
424
-
425
- const manifest = generateManifest(router.urlpatterns);
426
- const routeCount = Object.keys(manifest.routeManifest).length;
427
- const staticRoutes = Object.values(manifest.routeManifest).filter(
428
- (p: any) => !p.includes(":") && !p.includes("*")
429
- ).length;
430
- const dynamicRoutes = routeCount - staticRoutes;
431
-
432
- // Merge into the combined manifest
433
- Object.assign(mergedRouteManifest, manifest.routeManifest);
434
-
435
- // Write static files for this router
436
- const hash = hashRouterId(id);
437
- const outDir = join(projectRoot, "dist", "static", `__${hash}`);
438
- mkdirSync(outDir, { recursive: true });
439
-
440
- writeFileSync(
441
- join(outDir, "routes.json"),
442
- JSON.stringify(manifest.routeManifest, null, 2) + "\n"
443
- );
444
-
445
- writeFileSync(
446
- join(outDir, "prefixes.json"),
447
- JSON.stringify(manifest.prefixTree, null, 2) + "\n"
448
- );
449
-
450
- console.log(
451
- `[rsc-router] Router "${id}" -> ${routeCount} routes ` +
452
- `(${staticRoutes} static, ${dynamicRoutes} dynamic) ` +
453
- `-> dist/static/__${hash}/`
454
- );
455
- }
456
-
457
- return serverMod;
458
- }
459
-
460
- return {
461
- name: "@rangojs/router:discovery",
462
-
463
- configResolved(config) {
464
- projectRoot = config.root;
465
- isBuildMode = config.command === "build";
466
- // Capture user's resolve aliases for the temp server
467
- userResolveAlias = config.resolve.alias;
468
- },
469
-
470
- // Dev mode: discover routers and populate manifest in memory.
471
- // Skipped in build mode (buildStart handles it).
472
- configureServer(server) {
473
- if (isBuildMode) return;
474
- // Skip if this is a temp server created by buildStart
475
- if ((globalThis as any).__rscRouterDiscoveryActive) return;
476
- const discover = async () => {
477
- const rscEnv = (server.environments as any)?.rsc;
478
- if (!rscEnv?.runner) {
479
- return;
480
- }
481
-
482
- try {
483
- const serverMod = await discoverRouters(rscEnv);
484
-
485
- // In dev mode, also populate the route map in the RSC env
486
- // so href() works immediately without first-request penalty
487
- if (mergedRouteManifest && serverMod?.setCachedManifest) {
488
- serverMod.setCachedManifest(mergedRouteManifest);
489
- }
490
- } catch (err: any) {
491
- console.warn(
492
- `[rsc-router] Router discovery failed: ${err.message}`
493
- );
494
- }
495
- };
496
-
497
- // Schedule after all plugins have finished configureServer
498
- setTimeout(discover, 0);
499
- },
500
-
501
- // Build mode: create a temporary Vite dev server to access the RSC
502
- // environment's module runner, then discover routers and generate manifests.
503
- // The manifest data is stored for the virtual module's load hook.
504
- async buildStart() {
505
- if (!isBuildMode) return;
506
- // Only run once across environment builds
507
- if (mergedRouteManifest !== null) return;
508
-
509
- let tempServer: any = null;
510
- try {
511
- // Prevent the temp server's plugin instances from running discovery
512
- (globalThis as any).__rscRouterDiscoveryActive = true;
513
-
514
- // Create a minimal Vite server with just the RSC plugin.
515
- // We bypass the user's config file because:
516
- // - Custom environments (e.g., CloudflareDevEnvironment) may not expose
517
- // a module runner compatible with runner.import()
518
- // - The temp server only needs RSC conditions to import the router
519
- const { default: rsc } = await import("@vitejs/plugin-rsc");
520
- tempServer = await createViteServer({
521
- root: projectRoot,
522
- configFile: false,
523
- server: { middlewareMode: true },
524
- appType: "custom",
525
- logLevel: "silent",
526
- // Use the resolved aliases from the real config (includes user's path aliases
527
- // like @/ -> src/ AND package aliases from rsc-router)
528
- resolve: { alias: userResolveAlias },
529
- plugins: [
530
- rsc({ entries: { client: "virtual:entry-client", ssr: "virtual:entry-ssr", rsc: entryPath } }),
531
- createVersionPlugin(),
532
- // Stub virtual modules that the RSC entry may import
533
- // (e.g., virtual:rsc-router/routes-manifest, virtual:rsc-router/loader-manifest)
534
- createVirtualStubPlugin(),
535
- ],
536
- });
537
-
538
- const rscEnv = (tempServer.environments as any)?.rsc;
539
- if (!rscEnv?.runner) {
540
- console.warn(
541
- "[rsc-router] RSC environment runner not available during build, skipping manifest generation"
542
- );
543
- return;
544
- }
545
-
546
- await discoverRouters(rscEnv);
547
- } catch (err: any) {
548
- // Clean up before re-throwing so the temp server doesn't leak
549
- delete (globalThis as any).__rscRouterDiscoveryActive;
550
- if (tempServer) {
551
- await tempServer.close();
552
- }
553
- throw new Error(
554
- `[rsc-router] Build-time router discovery failed: ${err.message}`
555
- );
556
- } finally {
557
- delete (globalThis as any).__rscRouterDiscoveryActive;
558
- if (tempServer) {
559
- await tempServer.close();
560
- }
561
- }
562
- },
563
-
564
- // Virtual module: provides the pre-generated route manifest as a JS module
565
- // that calls setCachedManifest() at import time.
566
- resolveId(id) {
567
- if (id === VIRTUAL_ROUTES_MANIFEST_ID) {
568
- return "\0" + VIRTUAL_ROUTES_MANIFEST_ID;
569
- }
570
- return null;
571
- },
572
-
573
- load(id) {
574
- if (id === "\0" + VIRTUAL_ROUTES_MANIFEST_ID) {
575
- const hasManifest = mergedRouteManifest && Object.keys(mergedRouteManifest).length > 0;
576
- if (hasManifest) {
577
- return [
578
- `import { setCachedManifest } from "@rangojs/router/server";`,
579
- `setCachedManifest(${JSON.stringify(mergedRouteManifest)});`,
580
- ].join("\n");
581
- }
582
- // No manifest available yet (dev mode: discovery hasn't completed)
583
- return `// Route manifest will be populated at runtime`;
584
- }
585
- return null;
586
- },
587
- };
588
- }
589
-
590
- const VIRTUAL_ROUTES_MANIFEST_ID = "virtual:rsc-router/routes-manifest";
591
-
592
- /**
593
- * Resolve the entry path for build-time router discovery.
594
- * - Node preset: uses the required `router` option.
595
- * - Cloudflare preset: reads the `main` field from wrangler.json.
596
- */
597
- function resolveDiscoveryEntryPath(options: RscRouterOptions): string | undefined {
598
- if (options.preset === "cloudflare") {
599
- // Auto-detect from wrangler.json
600
- const wranglerPaths = ["wrangler.json", "wrangler.jsonc"];
601
- for (const filename of wranglerPaths) {
602
- if (existsSync(filename)) {
603
- try {
604
- const raw = readFileSync(filename, "utf-8");
605
- // Strip JSON comments for .jsonc
606
- const cleaned = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
607
- const config = JSON.parse(cleaned);
608
- if (config.main) {
609
- return config.main;
610
- }
611
- } catch {
612
- // Ignore parse errors
613
- }
614
- }
615
- }
616
- return undefined;
617
- }
618
- // Node preset: router is required
619
- return (options as RscRouterNodeOptions).router;
620
- }
621
-
622
1
  /**
623
- * Stub plugin for virtual modules in the temp discovery server.
624
- * The RSC entry may import virtual modules (routes-manifest, loader-manifest)
625
- * that aren't available in the temp server. The RSC plugin also requires
626
- * client/ssr entries which don't need real content for discovery.
627
- */
628
- function createVirtualStubPlugin(): Plugin {
629
- const STUB_PREFIXES = [
630
- "virtual:rsc-router/",
631
- "virtual:entry-",
632
- "virtual:vite-rsc/",
633
- ];
634
- return {
635
- name: "@rangojs/router:virtual-stubs",
636
- resolveId(id) {
637
- if (STUB_PREFIXES.some((p) => id.startsWith(p))) {
638
- return "\0stub:" + id;
639
- }
640
- return null;
641
- },
642
- load(id) {
643
- if (id.startsWith("\0stub:")) {
644
- return "export default {}";
645
- }
646
- return null;
647
- },
648
- };
649
- }
650
-
651
- /**
652
- * Generate a deterministic 12-char hex hash from a router id.
653
- * Used to create collision-free directory names for per-router static output.
654
- */
655
- function hashRouterId(id: string): string {
656
- return createHash("sha256").update(id).digest("hex").slice(0, 12);
657
- }
658
-
659
- /**
660
- * Plugin that auto-injects VERSION into custom entry.rsc files.
661
- * If a custom entry.rsc file uses createRSCHandler but doesn't pass version,
662
- * this transform adds the import and property automatically.
663
- * @internal
664
- */
665
- function createVersionInjectorPlugin(rscEntryPath: string): Plugin {
666
- let projectRoot = "";
667
- let resolvedEntryPath = "";
668
-
669
- return {
670
- name: "@rangojs/router:version-injector",
671
- enforce: "pre",
672
-
673
- configResolved(config) {
674
- projectRoot = config.root;
675
- resolvedEntryPath = resolve(projectRoot, rscEntryPath);
676
- },
677
-
678
- transform(code, id) {
679
- // Only transform the RSC entry file
680
- const normalizedId = Vite.normalizePath(id);
681
- const normalizedEntry = Vite.normalizePath(resolvedEntryPath);
682
-
683
- if (normalizedId !== normalizedEntry) {
684
- return null;
685
- }
686
-
687
- // Check if file uses createRSCHandler
688
- if (!code.includes("createRSCHandler")) {
689
- return null;
690
- }
691
-
692
- // Check if VERSION is already imported
693
- if (code.includes("@rangojs/router:version")) {
694
- return null;
695
- }
696
-
697
- // Check if version property is already being passed
698
- // Look for version: in the createRSCHandler call
699
- const handlerCallMatch = code.match(/createRSCHandler\s*\(\s*\{/);
700
- if (!handlerCallMatch) {
701
- return null;
702
- }
703
-
704
- // Add VERSION import after the last import statement
705
- const lastImportIndex = code.lastIndexOf("import ");
706
- if (lastImportIndex === -1) {
707
- return null;
708
- }
709
-
710
- // Find the end of the last import statement
711
- const afterLastImport = code.indexOf("\n", lastImportIndex);
712
- if (afterLastImport === -1) {
713
- return null;
714
- }
715
-
716
- // Find next line that's not an import continuation
717
- let insertIndex = afterLastImport + 1;
718
- while (
719
- insertIndex < code.length &&
720
- (code.slice(insertIndex).match(/^\s*(from|import)\s/) ||
721
- code[insertIndex] === "\n")
722
- ) {
723
- const nextNewline = code.indexOf("\n", insertIndex);
724
- if (nextNewline === -1) break;
725
- insertIndex = nextNewline + 1;
726
- }
727
-
728
- // Insert VERSION import
729
- const versionImport = `import { VERSION } from "@rangojs/router:version";\n`;
730
- let newCode = code.slice(0, insertIndex) + versionImport + code.slice(insertIndex);
731
-
732
- // Add version: VERSION to createRSCHandler call
733
- // Find createRSCHandler({ and add version: VERSION right after the opening brace
734
- newCode = newCode.replace(
735
- /createRSCHandler\s*\(\s*\{/,
736
- "createRSCHandler({\n version: VERSION,"
737
- );
738
-
739
- return {
740
- code: newCode,
741
- map: null,
742
- };
743
- },
744
- };
745
- }
746
-
747
- /**
748
- * Vite plugin for rsc-router.
749
- *
750
- * Includes @vitejs/plugin-rsc and all necessary transforms for the router
751
- * to function correctly with React Server Components.
2
+ * Public API for @rangojs/router/vite
752
3
  *
753
- * @example Node.js (default)
754
- * ```ts
755
- * export default defineConfig({
756
- * plugins: [react(), rscRouter({ router: './src/router.tsx' })],
757
- * });
758
- * ```
759
- *
760
- * @example Cloudflare Workers
761
- * ```ts
762
- * export default defineConfig({
763
- * plugins: [
764
- * react(),
765
- * rscRouter({ preset: 'cloudflare' }),
766
- * cloudflare({ viteEnvironment: { name: 'rsc' } }),
767
- * ],
768
- * });
769
- * ```
4
+ * Exports: rango() plugin factory, poke() dev utility plugin,
5
+ * and related option types. All other utilities are internal implementation
6
+ * details consumed via direct imports within the package.
770
7
  */
771
- export async function rscRouter(
772
- options: RscRouterOptions
773
- ): Promise<PluginOption[]> {
774
- const preset = options.preset ?? "node";
775
- const enableExposeActionId = options.exposeActionId ?? true;
776
-
777
- const plugins: PluginOption[] = [];
778
-
779
- // Get package resolution info (workspace vs npm install)
780
- const rscRouterAliases = getPackageAliases();
781
- const excludeDeps = getExcludeDeps();
782
-
783
- // Track RSC entry path for version injection
784
- let rscEntryPath: string | null = null;
785
-
786
- if (preset === "cloudflare") {
787
- // Cloudflare preset: configure entries for cloudflare worker setup
788
- // Router is not needed here - worker.rsc.tsx imports it directly
789
-
790
- // Dynamically import @vitejs/plugin-rsc
791
- const { default: rsc } = await import("@vitejs/plugin-rsc");
792
-
793
- // Only client and ssr entries - rsc entry is handled by cloudflare plugin
794
- // Always use virtual modules for cloudflare preset
795
- const finalEntries: { client: string; ssr: string } = {
796
- client: VIRTUAL_IDS.browser,
797
- ssr: VIRTUAL_IDS.ssr,
798
- };
799
-
800
- plugins.push({
801
- name: "@rangojs/router:cloudflare-integration",
802
- enforce: "pre",
803
- config() {
804
- // Configure environments for cloudflare deployment
805
- return {
806
- // Exclude rsc-router modules from optimization to prevent module duplication
807
- // This ensures the same Context instance is used by both browser entry and RSC proxy modules
808
- optimizeDeps: {
809
- exclude: excludeDeps,
810
- esbuildOptions: sharedEsbuildOptions,
811
- },
812
- resolve: {
813
- alias: rscRouterAliases,
814
- },
815
- environments: {
816
- client: {
817
- build: {
818
- rollupOptions: {
819
- output: {
820
- manualChunks: getManualChunks,
821
- },
822
- },
823
- },
824
- // Pre-bundle rsc-html-stream to prevent discovery during first request
825
- // Exclude rsc-router modules to ensure same Context instance
826
- optimizeDeps: {
827
- include: ["rsc-html-stream/client"],
828
- exclude: excludeDeps,
829
- esbuildOptions: sharedEsbuildOptions,
830
- },
831
- },
832
- ssr: {
833
- // Build SSR inside RSC directory so wrangler can deploy self-contained dist/rsc
834
- build: {
835
- outDir: "./dist/rsc/ssr",
836
- },
837
- resolve: {
838
- // Ensure single React instance in SSR child environment
839
- dedupe: ["react", "react-dom"],
840
- },
841
- // Pre-bundle SSR entry and React for proper module linking with childEnvironments
842
- // Exclude rsc-router modules to ensure same Context instance
843
- optimizeDeps: {
844
- entries: [finalEntries.ssr],
845
- include: [
846
- "react",
847
- "react-dom/server.edge",
848
- "react/jsx-runtime",
849
- "rsc-html-stream/server",
850
- ],
851
- exclude: excludeDeps,
852
- esbuildOptions: sharedEsbuildOptions,
853
- },
854
- },
855
- rsc: {
856
- // RSC environment needs exclude list and esbuild options
857
- // Exclude rsc-router modules to prevent createContext in RSC environment
858
- optimizeDeps: {
859
- exclude: excludeDeps,
860
- esbuildOptions: sharedEsbuildOptions,
861
- },
862
- },
863
- },
864
- };
865
- },
866
- });
867
-
868
- plugins.push(createVirtualEntriesPlugin(finalEntries));
869
-
870
- // Add RSC plugin with cloudflare-specific options
871
- // Note: loadModuleDevProxy should NOT be used with childEnvironments
872
- // since SSR runs in workerd alongside RSC
873
- plugins.push(
874
- rsc({
875
- get entries() {
876
- return finalEntries;
877
- },
878
- serverHandler: false,
879
- }) as PluginOption
880
- );
881
- } else {
882
- // Node preset: full RSC plugin integration
883
- const nodeOptions = options as RscRouterNodeOptions;
884
- const routerPath = nodeOptions.router;
885
- const rscOption = nodeOptions.rsc ?? true;
886
-
887
- // Add RSC plugin by default (can be disabled with rsc: false)
888
- if (rscOption !== false) {
889
- // Dynamically import @vitejs/plugin-rsc
890
- const { default: rsc } = await import("@vitejs/plugin-rsc");
891
-
892
- // Resolve entry paths: use explicit config or virtual modules
893
- const userEntries =
894
- typeof rscOption === "boolean" ? {} : rscOption.entries || {};
895
- const finalEntries = {
896
- client: userEntries.client ?? VIRTUAL_IDS.browser,
897
- ssr: userEntries.ssr ?? VIRTUAL_IDS.ssr,
898
- rsc: userEntries.rsc ?? VIRTUAL_IDS.rsc,
899
- };
900
-
901
- // Track RSC entry for version injection (only if custom entry provided)
902
- rscEntryPath = userEntries.rsc ?? null;
903
-
904
- // Create wrapper plugin that checks for duplicates
905
- let hasWarnedDuplicate = false;
906
-
907
- plugins.push({
908
- name: "@rangojs/router:rsc-integration",
909
- enforce: "pre",
910
-
911
- config() {
912
- // Configure environments for RSC
913
- // When using virtual entries, we need to explicitly configure optimizeDeps
914
- // so Vite pre-bundles React before processing the virtual modules.
915
- // Without this, the dep optimizer may run multiple times with different hashes,
916
- // causing React instance mismatches.
917
- const useVirtualClient = finalEntries.client === VIRTUAL_IDS.browser;
918
- const useVirtualSSR = finalEntries.ssr === VIRTUAL_IDS.ssr;
919
- const useVirtualRSC = finalEntries.rsc === VIRTUAL_IDS.rsc;
920
-
921
- return {
922
- // Exclude rsc-router modules from optimization to prevent module duplication
923
- // This ensures the same Context instance is used by both browser entry and RSC proxy modules
924
- optimizeDeps: {
925
- exclude: excludeDeps,
926
- esbuildOptions: sharedEsbuildOptions,
927
- },
928
- resolve: {
929
- alias: rscRouterAliases,
930
- },
931
- environments: {
932
- client: {
933
- build: {
934
- rollupOptions: {
935
- output: {
936
- manualChunks: getManualChunks,
937
- },
938
- },
939
- },
940
- // Always exclude rsc-router modules, conditionally add virtual entry
941
- optimizeDeps: {
942
- exclude: excludeDeps,
943
- esbuildOptions: sharedEsbuildOptions,
944
- ...(useVirtualClient && {
945
- // Tell Vite to scan the virtual entry for dependencies
946
- entries: [VIRTUAL_IDS.browser],
947
- }),
948
- },
949
- },
950
- ...(useVirtualSSR && {
951
- ssr: {
952
- optimizeDeps: {
953
- entries: [VIRTUAL_IDS.ssr],
954
- // Pre-bundle React for SSR to ensure single instance
955
- include: ["react", "react-dom/server.edge", "react/jsx-runtime"],
956
- exclude: excludeDeps,
957
- esbuildOptions: sharedEsbuildOptions,
958
- },
959
- },
960
- }),
961
- ...(useVirtualRSC && {
962
- rsc: {
963
- optimizeDeps: {
964
- entries: [VIRTUAL_IDS.rsc],
965
- // Pre-bundle React for RSC to ensure single instance
966
- include: ["react", "react/jsx-runtime"],
967
- esbuildOptions: sharedEsbuildOptions,
968
- },
969
- },
970
- }),
971
- },
972
- };
973
- },
974
-
975
- configResolved(config) {
976
- // Count how many RSC base plugins there are (rsc:minimal is the main one)
977
- const rscMinimalCount = config.plugins.filter(
978
- (p) => p.name === "rsc:minimal"
979
- ).length;
980
-
981
- if (rscMinimalCount > 1 && !hasWarnedDuplicate) {
982
- hasWarnedDuplicate = true;
983
- console.warn(
984
- "[rsc-router] Duplicate @vitejs/plugin-rsc detected. " +
985
- "Remove rsc() from your config or use rscRouter({ rsc: false }) for manual configuration."
986
- );
987
- }
988
- },
989
- });
990
-
991
- // Add virtual entries plugin
992
- plugins.push(createVirtualEntriesPlugin(finalEntries, routerPath));
993
-
994
- // Add the RSC plugin directly
995
- // Cast to PluginOption to handle type differences between bundled vite types
996
- plugins.push(
997
- rsc({
998
- entries: finalEntries,
999
- }) as PluginOption
1000
- );
1001
- }
1002
- }
1003
-
1004
- if (enableExposeActionId) {
1005
- plugins.push(exposeActionId());
1006
- }
1007
-
1008
- // Always add exposeLoaderId for GET-based loader fetching with useFetchLoader
1009
- plugins.push(exposeLoaderId());
1010
-
1011
- // Always add exposeHandleId for auto-generated handle IDs
1012
- plugins.push(exposeHandleId());
1013
-
1014
- // Always add exposeLocationStateId for auto-generated location state keys
1015
- plugins.push(exposeLocationStateId());
1016
-
1017
- // Add version virtual module plugin for cache invalidation
1018
- plugins.push(createVersionPlugin());
1019
-
1020
- // Add version injector for custom entry.rsc files
1021
- if (rscEntryPath) {
1022
- plugins.push(createVersionInjectorPlugin(rscEntryPath));
1023
- }
1024
-
1025
- // Transform CJS vendor files to ESM for browser compatibility
1026
- // optimizeDeps.include doesn't work because the file is loaded after initial optimization
1027
- plugins.push(createCjsToEsmPlugin());
1028
-
1029
- // Add router discovery plugin for build-time manifest generation.
1030
- // Node preset: uses the required router path.
1031
- // Cloudflare preset: auto-detects RSC entry from wrangler.json main field.
1032
- const discoveryEntryPath = resolveDiscoveryEntryPath(options);
1033
- if (discoveryEntryPath) {
1034
- plugins.push(createRouterDiscoveryPlugin(discoveryEntryPath));
1035
- }
1036
-
1037
- return plugins;
1038
- }
1039
-
1040
- /** Alias for backwards compatibility */
1041
- export const rango = rscRouter;
1042
-
1043
- /**
1044
- * Transform CJS vendor files from @vitejs/plugin-rsc to ESM for browser compatibility.
1045
- * The react-server-dom vendor files are shipped as CJS which doesn't work in browsers.
1046
- */
1047
- function createCjsToEsmPlugin(): Plugin {
1048
- return {
1049
- name: "@rangojs/router:cjs-to-esm",
1050
- enforce: "pre",
1051
- transform(code, id) {
1052
- const cleanId = id.split("?")[0];
1053
-
1054
- // Transform the client.browser.js entry point to re-export from CJS
1055
- if (
1056
- cleanId.includes("vendor/react-server-dom/client.browser.js") ||
1057
- cleanId.includes("vendor\\react-server-dom\\client.browser.js")
1058
- ) {
1059
- const isProd = process.env.NODE_ENV === "production";
1060
- const cjsFile = isProd
1061
- ? "./cjs/react-server-dom-webpack-client.browser.production.js"
1062
- : "./cjs/react-server-dom-webpack-client.browser.development.js";
1063
-
1064
- return {
1065
- code: `export * from "${cjsFile}";`,
1066
- map: null,
1067
- };
1068
- }
1069
-
1070
- // Transform the actual CJS files to ESM
1071
- if (
1072
- (cleanId.includes("vendor/react-server-dom/cjs/") ||
1073
- cleanId.includes("vendor\\react-server-dom\\cjs\\")) &&
1074
- cleanId.includes("client.browser")
1075
- ) {
1076
- let transformed = code;
1077
-
1078
- // Extract the license comment to preserve it
1079
- const licenseMatch = transformed.match(/^\/\*\*[\s\S]*?\*\//);
1080
- const license = licenseMatch ? licenseMatch[0] : "";
1081
- if (license) {
1082
- transformed = transformed.slice(license.length);
1083
- }
1084
-
1085
- // Remove "use strict" (both dev and prod have this)
1086
- transformed = transformed.replace(/^\s*["']use strict["'];\s*/, "");
1087
-
1088
- // Remove the conditional IIFE wrapper (development only)
1089
- transformed = transformed.replace(
1090
- /^\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
1091
- ""
1092
- );
1093
-
1094
- // Remove the closing of the conditional IIFE at the end (development only)
1095
- transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
1096
-
1097
- // Replace require('react') and require('react-dom') with imports (development)
1098
- transformed = transformed.replace(
1099
- /var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
1100
- 'import React from "react";\nimport ReactDOM from "react-dom";\nvar '
1101
- );
1102
-
1103
- // Replace require('react-dom') only (production - doesn't import React)
1104
- transformed = transformed.replace(
1105
- /var\s+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
1106
- 'import ReactDOM from "react-dom";\nvar '
1107
- );
1108
-
1109
- // Transform exports.xyz = function() to export function xyz()
1110
- transformed = transformed.replace(
1111
- /exports\.(\w+)\s*=\s*function\s*\(/g,
1112
- "export function $1("
1113
- );
1114
-
1115
- // Transform exports.xyz = value to export const xyz = value
1116
- transformed = transformed.replace(
1117
- /exports\.(\w+)\s*=/g,
1118
- "export const $1 ="
1119
- );
1120
-
1121
- // Reconstruct with license at the top
1122
- transformed = license + "\n" + transformed;
1123
-
1124
- return {
1125
- code: transformed,
1126
- map: null,
1127
- };
1128
- }
1129
8
 
1130
- return null;
1131
- },
1132
- };
1133
- }
9
+ export { rango } from "./rango.js";
10
+ export { poke } from "./plugins/refresh-cmd.js";
1134
11
 
12
+ export type {
13
+ RangoNodeOptions,
14
+ RangoCloudflareOptions,
15
+ RangoOptions,
16
+ BuildEnvOption,
17
+ BuildEnvFactory,
18
+ BuildEnvFactoryContext,
19
+ BuildEnvResult,
20
+ } from "./plugin-types.js";