@rangojs/router 0.0.0-experimental.122 → 0.0.0-experimental.125

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 (260) hide show
  1. package/dist/bin/rango.js +10 -6
  2. package/dist/testing/vitest.js +82 -0
  3. package/dist/vite/index.js +55 -48
  4. package/package.json +61 -21
  5. package/skills/caching/SKILL.md +2 -1
  6. package/skills/hooks/SKILL.md +40 -29
  7. package/skills/host-router/SKILL.md +16 -2
  8. package/skills/intercept/SKILL.md +4 -2
  9. package/skills/layout/SKILL.md +11 -6
  10. package/skills/loader/SKILL.md +6 -2
  11. package/skills/middleware/SKILL.md +4 -2
  12. package/skills/migrate-nextjs/SKILL.md +3 -1
  13. package/skills/parallel/SKILL.md +9 -4
  14. package/skills/rango/SKILL.md +12 -0
  15. package/skills/route/SKILL.md +10 -2
  16. package/skills/testing/SKILL.md +129 -0
  17. package/skills/testing/bindings.md +89 -0
  18. package/skills/testing/cache-prerender.md +98 -0
  19. package/skills/testing/client-components.md +122 -0
  20. package/skills/testing/e2e-parity.md +125 -0
  21. package/skills/testing/flight.md +89 -0
  22. package/skills/testing/handles.md +129 -0
  23. package/skills/testing/loader.md +128 -0
  24. package/skills/testing/middleware.md +99 -0
  25. package/skills/testing/render-handler.md +118 -0
  26. package/skills/testing/response-routes.md +95 -0
  27. package/skills/testing/reverse-and-types.md +84 -0
  28. package/skills/testing/server-actions.md +107 -0
  29. package/skills/testing/server-tree.md +128 -0
  30. package/skills/testing/setup.md +120 -0
  31. package/src/__internal.ts +0 -65
  32. package/src/browser/action-coordinator.ts +1 -1
  33. package/src/browser/action-fence.ts +47 -0
  34. package/src/browser/cookie-name.ts +140 -0
  35. package/src/browser/event-controller.ts +1 -83
  36. package/src/browser/invalidate-client-cache.ts +52 -0
  37. package/src/browser/navigation-bridge.ts +14 -1
  38. package/src/browser/navigation-client.ts +14 -1
  39. package/src/browser/navigation-store-handle.ts +38 -0
  40. package/src/browser/navigation-store.ts +26 -51
  41. package/src/browser/navigation-transaction.ts +0 -32
  42. package/src/browser/partial-update.ts +1 -83
  43. package/src/browser/prefetch/cache.ts +6 -45
  44. package/src/browser/prefetch/fetch.ts +7 -0
  45. package/src/browser/prefetch/queue.ts +6 -3
  46. package/src/browser/rango-state.ts +157 -99
  47. package/src/browser/react/Link.tsx +0 -2
  48. package/src/browser/react/NavigationProvider.tsx +2 -1
  49. package/src/browser/react/ScrollRestoration.tsx +10 -6
  50. package/src/browser/react/filter-segment-order.ts +0 -2
  51. package/src/browser/react/index.ts +0 -51
  52. package/src/browser/react/location-state-shared.ts +0 -13
  53. package/src/browser/react/location-state.ts +0 -1
  54. package/src/browser/react/use-action.ts +6 -15
  55. package/src/browser/react/use-handle.ts +0 -5
  56. package/src/browser/react/use-link-status.ts +0 -4
  57. package/src/browser/react/use-navigation.ts +0 -3
  58. package/src/browser/react/use-params.ts +0 -2
  59. package/src/browser/react/use-search-params.ts +0 -5
  60. package/src/browser/react/use-segments.ts +0 -13
  61. package/src/browser/rsc-router.tsx +12 -4
  62. package/src/browser/server-action-bridge.ts +77 -15
  63. package/src/browser/types.ts +7 -2
  64. package/src/browser/validate-redirect-origin.ts +4 -5
  65. package/src/build/route-trie.ts +3 -0
  66. package/src/build/route-types/param-extraction.ts +6 -3
  67. package/src/build/route-types/router-processing.ts +0 -8
  68. package/src/cache/cache-policy.ts +0 -54
  69. package/src/cache/cache-runtime.ts +27 -24
  70. package/src/cache/cache-scope.ts +0 -27
  71. package/src/cache/cache-tag.ts +0 -37
  72. package/src/cache/cf/cf-cache-store.ts +94 -46
  73. package/src/cache/cf/index.ts +0 -24
  74. package/src/cache/document-cache.ts +11 -36
  75. package/src/cache/handle-snapshot.ts +0 -40
  76. package/src/cache/index.ts +0 -27
  77. package/src/cache/memory-segment-store.ts +2 -48
  78. package/src/cache/profile-registry.ts +7 -3
  79. package/src/cache/read-through-swr.ts +41 -11
  80. package/src/cache/segment-codec.ts +0 -16
  81. package/src/cache/types.ts +0 -98
  82. package/src/client.rsc.tsx +1 -22
  83. package/src/client.tsx +14 -38
  84. package/src/component-utils.ts +19 -0
  85. package/src/deps/ssr.ts +0 -1
  86. package/src/handle.ts +28 -18
  87. package/src/handles/MetaTags.tsx +0 -14
  88. package/src/handles/meta.ts +0 -39
  89. package/src/host/cookie-handler.ts +0 -36
  90. package/src/host/errors.ts +0 -24
  91. package/src/host/index.ts +6 -0
  92. package/src/host/pattern-matcher.ts +7 -50
  93. package/src/host/router.ts +1 -65
  94. package/src/host/testing.ts +40 -27
  95. package/src/host/types.ts +6 -2
  96. package/src/href-client.ts +0 -4
  97. package/src/index.rsc.ts +42 -3
  98. package/src/index.ts +31 -1
  99. package/src/internal-debug.ts +2 -4
  100. package/src/loader.rsc.ts +19 -9
  101. package/src/loader.ts +12 -4
  102. package/src/network-error-thrower.tsx +1 -6
  103. package/src/outlet-provider.tsx +1 -5
  104. package/src/prerender/param-hash.ts +10 -11
  105. package/src/prerender/store.ts +23 -30
  106. package/src/prerender.ts +58 -3
  107. package/src/root-error-boundary.tsx +1 -19
  108. package/src/route-content-wrapper.tsx +1 -44
  109. package/src/route-definition/dsl-helpers.ts +7 -19
  110. package/src/route-definition/helpers-types.ts +3 -3
  111. package/src/route-definition/redirect.ts +11 -1
  112. package/src/route-map-builder.ts +0 -16
  113. package/src/router/basename.ts +14 -0
  114. package/src/router/content-negotiation.ts +0 -13
  115. package/src/router/error-handling.ts +12 -16
  116. package/src/router/find-match.ts +4 -30
  117. package/src/router/intercept-resolution.ts +10 -1
  118. package/src/router/lazy-includes.ts +1 -57
  119. package/src/router/loader-resolution.ts +3 -2
  120. package/src/router/logging.ts +0 -6
  121. package/src/router/manifest.ts +1 -25
  122. package/src/router/match-api.ts +0 -20
  123. package/src/router/match-context.ts +0 -22
  124. package/src/router/match-handlers.ts +57 -58
  125. package/src/router/match-middleware/background-revalidation.ts +0 -7
  126. package/src/router/match-middleware/cache-lookup.ts +1 -54
  127. package/src/router/match-middleware/cache-store.ts +0 -31
  128. package/src/router/match-middleware/intercept-resolution.ts +0 -22
  129. package/src/router/match-middleware/segment-resolution.ts +0 -21
  130. package/src/router/match-pipelines.ts +1 -42
  131. package/src/router/match-result.ts +1 -52
  132. package/src/router/metrics.ts +0 -34
  133. package/src/router/middleware-cookies.ts +0 -13
  134. package/src/router/middleware-types.ts +0 -115
  135. package/src/router/middleware.ts +7 -30
  136. package/src/router/navigation-snapshot.ts +0 -51
  137. package/src/router/params-util.ts +23 -0
  138. package/src/router/pattern-matching.ts +1 -33
  139. package/src/router/prerender-match.ts +33 -45
  140. package/src/router/request-classification.ts +1 -38
  141. package/src/router/revalidation.ts +5 -58
  142. package/src/router/router-context.ts +0 -26
  143. package/src/router/router-interfaces.ts +7 -0
  144. package/src/router/router-options.ts +30 -0
  145. package/src/router/segment-resolution/fresh.ts +25 -57
  146. package/src/router/segment-resolution/helpers.ts +34 -0
  147. package/src/router/segment-resolution/loader-cache.ts +10 -13
  148. package/src/router/segment-resolution/revalidation.ts +5 -42
  149. package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
  150. package/src/router/segment-resolution.ts +4 -1
  151. package/src/router/state-cookie-name.ts +33 -0
  152. package/src/router/telemetry-otel.ts +0 -20
  153. package/src/router/telemetry.ts +96 -19
  154. package/src/router/timeout.ts +0 -20
  155. package/src/router/trie-matching.ts +63 -40
  156. package/src/router/types.ts +1 -63
  157. package/src/router/url-params.ts +0 -5
  158. package/src/router.ts +40 -9
  159. package/src/rsc/handler.ts +14 -2
  160. package/src/rsc/helpers.ts +34 -0
  161. package/src/rsc/origin-guard.ts +0 -12
  162. package/src/rsc/progressive-enhancement.ts +4 -1
  163. package/src/rsc/rsc-rendering.ts +4 -7
  164. package/src/rsc/runtime-warnings.ts +14 -0
  165. package/src/rsc/server-action.ts +30 -28
  166. package/src/rsc/types.ts +2 -1
  167. package/src/runtime-env.ts +18 -0
  168. package/src/search-params.ts +0 -16
  169. package/src/segment-loader-promise.ts +14 -2
  170. package/src/segment-system.tsx +79 -88
  171. package/src/server/cookie-store.ts +52 -1
  172. package/src/server/handle-store.ts +7 -24
  173. package/src/server/loader-registry.ts +5 -24
  174. package/src/server/request-context.ts +74 -77
  175. package/src/ssr/index.tsx +14 -14
  176. package/src/static-handler.ts +10 -13
  177. package/src/testing/cache-status.ts +119 -0
  178. package/src/testing/collect-handle.ts +40 -0
  179. package/src/testing/dispatch.ts +581 -0
  180. package/src/testing/dom.entry.ts +22 -0
  181. package/src/testing/e2e/fixture.ts +188 -0
  182. package/src/testing/e2e/index.ts +127 -0
  183. package/src/testing/e2e/matchers.ts +35 -0
  184. package/src/testing/e2e/page-helpers.ts +272 -0
  185. package/src/testing/e2e/parity.ts +387 -0
  186. package/src/testing/e2e/server.ts +195 -0
  187. package/src/testing/flight-matchers.ts +97 -0
  188. package/src/testing/flight-normalize.ts +11 -0
  189. package/src/testing/flight-runtime.d.ts +57 -0
  190. package/src/testing/flight-tree.ts +682 -0
  191. package/src/testing/flight.entry.ts +52 -0
  192. package/src/testing/flight.ts +186 -0
  193. package/src/testing/generated-routes.ts +183 -0
  194. package/src/testing/index.ts +98 -0
  195. package/src/testing/internal/context.ts +348 -0
  196. package/src/testing/internal/flight-client-globals.ts +30 -0
  197. package/src/testing/internal/seed-vars.ts +54 -0
  198. package/src/testing/render-handler.ts +311 -0
  199. package/src/testing/render-route.tsx +504 -0
  200. package/src/testing/run-loader.ts +378 -0
  201. package/src/testing/run-middleware.ts +205 -0
  202. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  203. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  204. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  205. package/src/testing/vitest-stubs/version.ts +5 -0
  206. package/src/testing/vitest.ts +305 -0
  207. package/src/theme/ThemeProvider.tsx +0 -52
  208. package/src/theme/ThemeScript.tsx +0 -6
  209. package/src/theme/constants.ts +0 -12
  210. package/src/theme/index.ts +0 -7
  211. package/src/theme/theme-context.ts +1 -5
  212. package/src/theme/theme-script.ts +0 -14
  213. package/src/theme/use-theme.ts +0 -3
  214. package/src/types/boundaries.ts +0 -35
  215. package/src/types/error-types.ts +25 -89
  216. package/src/types/global-namespace.ts +15 -15
  217. package/src/types/handler-context.ts +16 -13
  218. package/src/types/index.ts +0 -10
  219. package/src/types/request-scope.ts +0 -19
  220. package/src/types/route-config.ts +6 -50
  221. package/src/types/route-entry.ts +0 -6
  222. package/src/types/segments.ts +0 -13
  223. package/src/urls/include-helper.ts +0 -4
  224. package/src/urls/index.ts +0 -6
  225. package/src/urls/path-helper-types.ts +2 -2
  226. package/src/urls/path-helper.ts +0 -54
  227. package/src/urls/urls-function.ts +0 -13
  228. package/src/use-loader.tsx +0 -186
  229. package/src/vite/discovery/bundle-postprocess.ts +2 -1
  230. package/src/vite/discovery/discover-routers.ts +6 -7
  231. package/src/vite/discovery/virtual-module-codegen.ts +1 -11
  232. package/src/vite/plugin-types.ts +3 -1
  233. package/src/vite/plugins/cjs-to-esm.ts +0 -11
  234. package/src/vite/plugins/client-ref-dedup.ts +0 -11
  235. package/src/vite/plugins/client-ref-hashing.ts +0 -10
  236. package/src/vite/plugins/cloudflare-protocol-stub.ts +0 -20
  237. package/src/vite/plugins/expose-action-id.ts +2 -73
  238. package/src/vite/plugins/expose-id-utils.ts +0 -55
  239. package/src/vite/plugins/expose-ids/export-analysis.ts +0 -38
  240. package/src/vite/plugins/expose-ids/handler-transform.ts +0 -15
  241. package/src/vite/plugins/expose-ids/loader-transform.ts +0 -15
  242. package/src/vite/plugins/expose-ids/router-transform.ts +0 -13
  243. package/src/vite/plugins/expose-internal-ids.ts +10 -0
  244. package/src/vite/plugins/performance-tracks.ts +0 -3
  245. package/src/vite/plugins/use-cache-transform.ts +0 -36
  246. package/src/vite/plugins/version-injector.ts +0 -20
  247. package/src/vite/plugins/version-plugin.ts +1 -49
  248. package/src/vite/plugins/virtual-entries.ts +0 -15
  249. package/src/vite/rango.ts +1 -108
  250. package/src/vite/router-discovery.ts +2 -1
  251. package/src/vite/utils/ast-handler-extract.ts +0 -16
  252. package/src/vite/utils/bundle-analysis.ts +6 -13
  253. package/src/vite/utils/client-chunks.ts +0 -6
  254. package/src/vite/utils/forward-user-plugins.ts +0 -22
  255. package/src/vite/utils/manifest-utils.ts +0 -4
  256. package/src/vite/utils/package-resolution.ts +1 -73
  257. package/src/vite/utils/prerender-utils.ts +0 -35
  258. package/src/vite/utils/shared-utils.ts +3 -35
  259. package/src/browser/react/use-client-cache.ts +0 -58
  260. package/src/browser/shallow.ts +0 -40
@@ -49,7 +49,6 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
49
49
  );
50
50
  genFileVars.push(varName);
51
51
  } else {
52
- // Routers without sourceFile: inline their manifest data directly
53
52
  routersWithoutGenFile.push({
54
53
  id: entry.id,
55
54
  manifest: entry.routeManifest,
@@ -68,14 +67,12 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
68
67
  `clearAllRouterData();`,
69
68
  ];
70
69
 
71
- // Flatten NamedRoutes entries: search schema objects -> plain string paths
72
70
  if (genFileVars.length > 0) {
73
71
  lines.push(
74
72
  `function __flat(r) { const o = {}; for (const [k, v] of Object.entries(r)) o[k] = typeof v === "string" ? v : v.path; return o; }`,
75
73
  );
76
74
  }
77
75
 
78
- // Build the merged manifest from gen file imports + inlined data
79
76
  if (genFileVars.length === 1 && routersWithoutGenFile.length === 0) {
80
77
  lines.push(`setCachedManifest(__flat(${genFileVars[0]}));`);
81
78
  } else {
@@ -86,7 +83,6 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
86
83
  lines.push(`setCachedManifest({ ${parts.join(", ")} });`);
87
84
  }
88
85
 
89
- // Set per-router manifests
90
86
  let genVarIdx = 0;
91
87
  for (const entry of state.perRouterManifests) {
92
88
  if (entry.sourceFile) {
@@ -114,8 +110,6 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
114
110
  // against live router.urlpatterns, which is always correct after a
115
111
  // program reload.
116
112
 
117
- // Register lazy loaders for per-router manifest modules.
118
- // Each import() uses a static string literal so Rollup creates separate chunks.
119
113
  for (const routerId of state.perRouterManifestDataMap.keys()) {
120
114
  lines.push(
121
115
  `registerRouterManifestLoader(${JSON.stringify(routerId)}, () => import(${JSON.stringify(VIRTUAL_ROUTES_MANIFEST_ID + "/" + routerId)}));`,
@@ -129,9 +123,6 @@ export function generateRoutesManifestModule(state: DiscoveryState): string {
129
123
  return lines.join("\n");
130
124
  }
131
125
 
132
- // No manifest: either discovery hasn't completed or no runner (Cloudflare dev).
133
- // Still inject __PRERENDER_DEV_URL so the prerender store can fetch on-demand.
134
- // Re-resolve origin now since the server is listening by module load time.
135
126
  if (!state.isBuildMode) {
136
127
  const origin =
137
128
  state.devServerOrigin ||
@@ -160,7 +151,6 @@ export function generatePerRouterModule(
160
151
  const lines: string[] = [];
161
152
 
162
153
  if (routerEntry?.sourceFile) {
163
- // Import manifest from the gen file so HMR auto-propagates
164
154
  const routerDir = dirname(routerEntry.sourceFile);
165
155
  const routerBasename = basename(routerEntry.sourceFile).replace(
166
156
  /\.(tsx?|jsx?)$/,
@@ -189,5 +179,5 @@ export function generatePerRouterModule(
189
179
  `export const precomputedEntries = ${jsonParseExpression(entries)};`,
190
180
  );
191
181
  }
192
- return lines.join("\n") || "// empty router manifest";
182
+ return lines.join("\n") || "";
193
183
  }
@@ -156,7 +156,9 @@ export interface RangoCloudflareOptions extends RangoBaseOptions {
156
156
  /**
157
157
  * Deployment preset for Cloudflare Workers.
158
158
  * When using cloudflare preset:
159
- * - @vitejs/plugin-rsc is NOT added (cloudflare plugin adds it)
159
+ * - @vitejs/plugin-rsc IS still added by rango(), but with `serverHandler: false`
160
+ * (the cloudflare plugin owns the RSC worker/server entry); only `client` and
161
+ * `ssr` virtual entries are configured, no rsc entry
160
162
  * - Your worker entry (e.g., worker.rsc.tsx) imports the router directly
161
163
  * - Browser and SSR use virtual entries
162
164
  * - Build-time manifest generation is auto-detected from the resolved RSC environment config
@@ -14,7 +14,6 @@ export function createCjsToEsmPlugin(): Plugin {
14
14
  transform(code, id) {
15
15
  const cleanId = id.split("?")[0].replaceAll("\\", "/");
16
16
 
17
- // Transform the client.browser.js entry point to re-export from CJS
18
17
  if (cleanId.includes("vendor/react-server-dom/client.browser.js")) {
19
18
  const isProd = process.env.NODE_ENV === "production";
20
19
  const cjsFile = isProd
@@ -28,57 +27,47 @@ export function createCjsToEsmPlugin(): Plugin {
28
27
  };
29
28
  }
30
29
 
31
- // Transform the actual CJS files to ESM
32
30
  if (
33
31
  cleanId.includes("vendor/react-server-dom/cjs/") &&
34
32
  cleanId.includes("client.browser")
35
33
  ) {
36
34
  let transformed = code;
37
35
 
38
- // Extract the license comment to preserve it
39
36
  const licenseMatch = transformed.match(/^\/\*\*[\s\S]*?\*\//);
40
37
  const license = licenseMatch ? licenseMatch[0] : "";
41
38
  if (license) {
42
39
  transformed = transformed.slice(license.length);
43
40
  }
44
41
 
45
- // Remove "use strict" (both dev and prod have this)
46
42
  transformed = transformed.replace(/^\s*["']use strict["'];\s*/, "");
47
43
 
48
- // Remove the conditional IIFE wrapper (development only)
49
44
  transformed = transformed.replace(
50
45
  /^\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
51
46
  "",
52
47
  );
53
48
 
54
- // Remove the closing of the conditional IIFE at the end (development only)
55
49
  transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
56
50
 
57
- // Replace require('react') and require('react-dom') with imports (development)
58
51
  transformed = transformed.replace(
59
52
  /var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
60
53
  'import React from "react";\nimport ReactDOM from "react-dom";\nvar ',
61
54
  );
62
55
 
63
- // Replace require('react-dom') only (production - doesn't import React)
64
56
  transformed = transformed.replace(
65
57
  /var\s+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
66
58
  'import ReactDOM from "react-dom";\nvar ',
67
59
  );
68
60
 
69
- // Transform exports.xyz = function() to export function xyz()
70
61
  transformed = transformed.replace(
71
62
  /exports\.(\w+)\s*=\s*function\s*\(/g,
72
63
  "export function $1(",
73
64
  );
74
65
 
75
- // Transform exports.xyz = value to export const xyz = value
76
66
  transformed = transformed.replace(
77
67
  /exports\.(\w+)\s*=/g,
78
68
  "export const $1 =",
79
69
  );
80
70
 
81
- // Reconstruct with license at the top
82
71
  transformed = license + "\n" + transformed;
83
72
 
84
73
  debug?.("cjs-to-esm body rewrite %s", id);
@@ -73,8 +73,6 @@ export function clientRefDedup(): Plugin {
73
73
  apply: "serve",
74
74
 
75
75
  configResolved(config: ResolvedConfig) {
76
- // Respect user's optimizeDeps.exclude — if a package is explicitly
77
- // excluded from pre-bundling, we shouldn't redirect it there.
78
76
  const clientEnv = config.environments?.["client"];
79
77
  clientExclude =
80
78
  clientEnv?.optimizeDeps?.exclude ?? config.optimizeDeps?.exclude ?? [];
@@ -91,27 +89,19 @@ export function clientRefDedup(): Plugin {
91
89
  },
92
90
 
93
91
  resolveId(source, importer, options) {
94
- // Only intercept in the client environment
95
92
  if (this.environment?.name !== "client") return;
96
93
 
97
- // Only handle imports from client-in-server-package-proxy virtual modules
98
94
  if (!importer?.includes(CLIENT_IN_SERVER_PROXY_PREFIX)) return;
99
95
 
100
- // Only handle absolute node_modules paths
101
96
  if (!source.includes("/node_modules/")) return;
102
97
 
103
- // Must have an importer
104
- if (!importer) return;
105
-
106
98
  const packageName = extractPackageName(source);
107
99
  if (!packageName) return;
108
100
 
109
- // Don't redirect packages that are excluded from optimization
110
101
  if (clientExclude.includes(packageName)) return;
111
102
 
112
103
  if (debug) dedupedPackages.add(packageName);
113
104
 
114
- // Return a virtual module that re-exports via bare specifier
115
105
  return `\0rango:dedup/${packageName}`;
116
106
  },
117
107
 
@@ -120,7 +110,6 @@ export function clientRefDedup(): Plugin {
120
110
 
121
111
  const packageName = id.slice("\0rango:dedup/".length);
122
112
 
123
- // Re-export via bare specifier so Vite routes through pre-bundling
124
113
  return [
125
114
  `export * from ${JSON.stringify(packageName)};`,
126
115
  `import * as __all__ from ${JSON.stringify(packageName)};`,
@@ -5,7 +5,6 @@ import { createRangoDebugger, createCounter, NS } from "../debug.js";
5
5
 
6
6
  const debug = createRangoDebugger(NS.transform);
7
7
 
8
- // Dev-mode client-reference key prefixes emitted by @vitejs/plugin-rsc
9
8
  const CLIENT_PKG_PROXY_PREFIX =
10
9
  "/@id/__x00__virtual:vite-rsc/client-package-proxy/";
11
10
  const CLIENT_IN_SERVER_PKG_PROXY_PREFIX =
@@ -40,32 +39,24 @@ export function computeProductionHash(
40
39
  let toHash: string;
41
40
 
42
41
  if (refKey.startsWith(CLIENT_PKG_PROXY_PREFIX)) {
43
- // /@id/__x00__virtual:vite-rsc/client-package-proxy/<pkg> -> hash("<pkg>")
44
42
  toHash = refKey.slice(CLIENT_PKG_PROXY_PREFIX.length);
45
43
  } else if (refKey.startsWith(CLIENT_IN_SERVER_PKG_PROXY_PREFIX)) {
46
- // /@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/<encodedAbsPath>
47
44
  const absPath = decodeURIComponent(
48
45
  refKey.slice(CLIENT_IN_SERVER_PKG_PROXY_PREFIX.length),
49
46
  );
50
47
  toHash = relative(projectRoot, absPath).replaceAll("\\", "/");
51
48
  } else if (refKey.startsWith(FS_PREFIX)) {
52
- // /@fs/abs/path.tsx -> hash(relative(root, "/abs/path.tsx"))
53
49
  const absPath = refKey.slice(FS_PREFIX.length - 1); // keep leading /
54
50
  toHash = relative(projectRoot, absPath).replaceAll("\\", "/");
55
51
  } else if (refKey.startsWith("/")) {
56
- // /src/Button.tsx -> hash("src/Button.tsx")
57
52
  toHash = refKey.slice(1);
58
53
  } else {
59
- // Already hashed or unknown format — return unchanged
60
54
  return refKey;
61
55
  }
62
56
 
63
57
  return hashRefKey(toHash);
64
58
  }
65
59
 
66
- // Regex to match registerClientReference() calls as emitted by @vitejs/plugin-rsc.
67
- // Captures the reference key (second argument) from the call.
68
- // Handles two proxy forms: parenthesized expression `(expr)` and arrow-throw `() => { ... }`.
69
60
  const REGISTER_CLIENT_REF_RE =
70
61
  /registerClientReference\(\s*(?:(?:\([^)]*\))|(?:\(\)[\s\S]*?\}))\s*,\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)/g;
71
62
 
@@ -106,7 +97,6 @@ export function hashClientRefs(projectRoot: string): Plugin {
106
97
  const counter = createCounter(debug, "hash-client-refs");
107
98
  return {
108
99
  name: "@rangojs/router:hash-client-refs",
109
- // Run after the RSC plugin's transform (default enforce is normal)
110
100
  enforce: "post",
111
101
  applyToEnvironment(env) {
112
102
  return env.name === "rsc";
@@ -32,14 +32,6 @@ const IMPORT_NODE_TYPES = new Set([
32
32
  "ExportAllDeclaration",
33
33
  ]);
34
34
 
35
- // Keep in sync with `STUBS` in cloudflare-protocol-loader-hook.mjs —
36
- // both paths (Vite transform and Node loader) need to hand out the same
37
- // classes. Unknown `cloudflare:*` modules fall back to an empty default
38
- // export so third-party packages (e.g. the Cloudflare Agents SDK) can
39
- // pull them into the graph without crashing discovery. Discovery only
40
- // evaluates module top-level code — no handlers run — so missing named
41
- // exports only fail if something does `class X extends Missing {}` at
42
- // module scope, which is rare outside the already-stubbed classes.
43
35
  const STUBS: Record<string, string> = {
44
36
  "cloudflare:workers": `
45
37
  export class DurableObject { constructor(_ctx, _env) {} }
@@ -65,15 +57,6 @@ export default {};
65
57
  `,
66
58
  };
67
59
 
68
- // Policy: unknown `cloudflare:*` specifiers resolve permissively (empty
69
- // default export) rather than throwing. We prioritize dependency-graph
70
- // resilience over strict validation of user imports because third-party
71
- // packages can pull `cloudflare:*` modules we haven't curated, and
72
- // discovery should not fail just because those modules appear in the graph.
73
- // Tradeoff: unsupported user-authored `cloudflare:*` imports may fail later
74
- // with a generic JS/module error instead of a tailored rango-branded hint.
75
- // The test below pins this behavior so dependency compatibility is not
76
- // regressed accidentally.
77
60
  const FALLBACK_STUB = `export default {};\n`;
78
61
 
79
62
  interface AstNode {
@@ -163,9 +146,6 @@ export function createCloudflareProtocolStubPlugin(): Plugin {
163
146
 
164
147
  if (hits.length === 0) return null;
165
148
 
166
- // Rewrite from last to first so earlier offsets stay valid. `start`/
167
- // `end` span the full literal including quotes, so we re-emit the
168
- // same quote character around the new specifier.
169
149
  hits.sort((a, b) => b.start - a.start);
170
150
  let out = code;
171
151
  for (const hit of hits) {
@@ -7,9 +7,6 @@ import { createRangoDebugger, createCounter, NS } from "../debug.js";
7
7
 
8
8
  const debug = createRangoDebugger(NS.transform);
9
9
 
10
- /**
11
- * Type for the RSC plugin's manager API
12
- */
13
10
  interface RscPluginManager {
14
11
  serverReferenceMetaMap: Record<
15
12
  string,
@@ -26,14 +23,9 @@ interface RscPluginApi {
26
23
  manager: RscPluginManager;
27
24
  }
28
25
 
29
- /**
30
- * Get the RSC plugin's API from Vite config
31
- */
32
26
  function getRscPluginApi(config: ResolvedConfig): RscPluginApi | undefined {
33
- // Try by name first
34
27
  let plugin = config.plugins.find((p) => p.name === "rsc:minimal");
35
28
 
36
- // Fallback: find by API structure if name lookup fails
37
29
  if (!plugin) {
38
30
  plugin = config.plugins.find(
39
31
  (p) =>
@@ -62,13 +54,11 @@ function getRscPluginApi(config: ResolvedConfig): RscPluginApi | undefined {
62
54
  function isUseServerModule(filePath: string): boolean {
63
55
  try {
64
56
  const content = fs.readFileSync(filePath, "utf-8");
65
- // Remove leading comments and whitespace to find the first meaningful content
66
57
  const trimmed = content
67
- .replace(/^\s*\/\/[^\n]*\n/gm, "") // Remove single-line comments
68
- .replace(/^\s*\/\*[\s\S]*?\*\/\s*/gm, "") // Remove multi-line comments
58
+ .replace(/^\s*\/\/[^\n]*\n/gm, "")
59
+ .replace(/^\s*\/\*[\s\S]*?\*\/\s*/gm, "")
69
60
  .trimStart();
70
61
 
71
- // Check if the file starts with "use server" directive
72
62
  return (
73
63
  trimmed.startsWith('"use server"') || trimmed.startsWith("'use server'")
74
64
  );
@@ -77,18 +67,6 @@ function isUseServerModule(filePath: string): boolean {
77
67
  }
78
68
  }
79
69
 
80
- /**
81
- * Transform code to expose action IDs on createServerReference calls.
82
- * Wraps each call with an IIFE that attaches $id to the returned function.
83
- *
84
- * @param code - The source code to transform
85
- * @param sourceId - The source file identifier (for sourcemap)
86
- * @param hashToFileMap - Optional mapping from hash to file path (for server bundles)
87
- */
88
- /**
89
- * Apply createServerReference wrapping to a MagicString instance.
90
- * Returns true if any changes were made.
91
- */
92
70
  function applyServerReferenceWrapping(
93
71
  code: string,
94
72
  s: MagicString,
@@ -98,11 +76,6 @@ function applyServerReferenceWrapping(
98
76
  return false;
99
77
  }
100
78
 
101
- // Match: createServerReference("hash#actionName", ...) or $$ReactClient.createServerReference(...)
102
- // The RSC plugin uses $$ReactClient namespace in transformed code.
103
- // Note: [^)]* cannot handle nested parens in trailing args. This is safe in practice
104
- // because the RSC plugin always generates simple variable references (e.g., callServer)
105
- // as the second argument, never nested function calls.
106
79
  const pattern =
107
80
  /((?:\$\$\w+\.)?createServerReference)\(("[^"]+#[^"]+")([^)]*)\)/g;
108
81
 
@@ -115,23 +88,19 @@ function applyServerReferenceWrapping(
115
88
  const start = match.index;
116
89
  const end = start + fullMatch.length;
117
90
 
118
- // Parse the ID to potentially replace hash with file path
119
91
  let finalIdArg = idArg;
120
92
  if (hashToFileMap) {
121
- // idArg is like '"hash#actionName"', extract the parts
122
93
  const idValue = idArg.slice(1, -1); // Remove quotes
123
94
  const hashMatch = idValue.match(/^([^#]+)#(.+)$/);
124
95
  if (hashMatch) {
125
96
  const [, hash, actionName] = hashMatch;
126
97
  const filePath = hashToFileMap.get(hash);
127
98
  if (filePath) {
128
- // Replace hash with file path for server-side
129
99
  finalIdArg = `"${filePath}#${actionName}"`;
130
100
  }
131
101
  }
132
102
  }
133
103
 
134
- // Wrap the createServerReference call to attach $$id to the returned function
135
104
  const replacement = `(function(fn) { fn.$$id = ${finalIdArg}; return fn; })(${fnCall}(${idArg}${rest}))`;
136
105
  s.overwrite(start, end, replacement);
137
106
  }
@@ -155,29 +124,6 @@ function transformServerReferences(
155
124
  };
156
125
  }
157
126
 
158
- /**
159
- * Transform registerServerReference calls in server bundles to use file paths instead of hashes.
160
- * Pattern: registerServerReference(fn, "hash", "exportName")
161
- * React's registerServerReference sets $$id = hash + "#" + exportName
162
- * By replacing the hash with file path, $$id will contain the file path for revalidation matching.
163
- *
164
- * Only actions from module-level "use server" files are transformed.
165
- * Inline actions (defined in RSC components with "use server" inside a function) are NOT in
166
- * hashToFileMap and keep their hashed IDs. This is intentional for client security:
167
- * - Module-level "use server" files: shared action modules, file path helps revalidation
168
- * - Inline actions: one-off actions in RSC, hash ID prevents file path exposure to client
169
- *
170
- * @param code - The source code to transform
171
- * @param sourceId - The source file identifier (for sourcemap)
172
- * @param hashToFileMap - Mapping from hash to file path (only module-level "use server" files)
173
- */
174
- /**
175
- * Apply registerServerReference wrapping to a MagicString instance.
176
- * Returns true if any changes were made.
177
- *
178
- * Only actions from module-level "use server" files are transformed.
179
- * Inline actions keep their hashed IDs for client security.
180
- */
181
127
  function applyRegisterReferenceWrapping(
182
128
  code: string,
183
129
  s: MagicString,
@@ -187,8 +133,6 @@ function applyRegisterReferenceWrapping(
187
133
  return false;
188
134
  }
189
135
 
190
- // Match: registerServerReference(fn, "hash", "exportName")
191
- // The hash is the second argument, exportName is the third
192
136
  const pattern =
193
137
  /registerServerReference\(([^,]+),\s*"([^"]+)",\s*"([^"]+)"\)/g;
194
138
 
@@ -200,15 +144,9 @@ function applyRegisterReferenceWrapping(
200
144
  const start = match.index;
201
145
  const end = start + fullMatch.length;
202
146
 
203
- // Look up the file path for this hash
204
147
  const filePath = hashToFileMap.get(hash);
205
148
  if (filePath) {
206
149
  hasChanges = true;
207
- // WRAP the call to add $id property with file path
208
- // Keep the original hash for React's action registry (so loadServerAction works)
209
- // Add $id (single dollar) with file path for revalidation matching
210
- // Note: We use $id instead of $$id because React's registerServerReference
211
- // sets $$id as a non-writable property
212
150
  const filePathId = `${filePath}#${exportName}`;
213
151
  const replacement = `(function(fn) { fn.$id = "${filePathId}"; return fn; })(registerServerReference(${fnArg}, "${hash}", "${exportName}"))`;
214
152
  s.overwrite(start, end, replacement);
@@ -336,27 +274,19 @@ export function exposeActionId(): Plugin {
336
274
 
337
275
  const start = counterTransform ? performance.now() : 0;
338
276
  try {
339
- // Dev mode: no hash-to-file mapping needed (IDs are already file paths)
340
277
  return transformServerReferences(code, id);
341
278
  } finally {
342
279
  counterTransform?.record(id, performance.now() - start);
343
280
  }
344
281
  },
345
282
 
346
- // Build mode: renderChunk runs after all transforms and bundling complete
347
283
  renderChunk(code, chunk) {
348
284
  const start = counterRender ? performance.now() : 0;
349
285
  try {
350
- // Only RSC bundle should get file paths for revalidation matching
351
- // SSR bundle must NOT use file paths because client components run there
352
- // and need to match the client bundle during hydration (otherwise: error #418)
353
286
  const isRscEnv = this.environment?.name === "rsc";
354
287
 
355
- // Only use file path mapping for RSC environment
356
288
  const effectiveMap = isRscEnv ? hashToFileMap : undefined;
357
289
 
358
- // For RSC bundles, both createServerReference and registerServerReference
359
- // may need transforming. Use a single MagicString for correct sourcemaps.
360
290
  if (isRscEnv && hashToFileMap) {
361
291
  const s = new MagicString(code);
362
292
  const changed1 = applyServerReferenceWrapping(code, s, effectiveMap);
@@ -377,7 +307,6 @@ export function exposeActionId(): Plugin {
377
307
  return null;
378
308
  }
379
309
 
380
- // Non-RSC environments: only transform createServerReference calls
381
310
  return transformServerReferences(code, chunk.fileName, effectiveMap);
382
311
  } finally {
383
312
  counterRender?.record(chunk.fileName, performance.now() - start);
@@ -8,21 +8,12 @@ export function normalizePath(p: string): string {
8
8
  return p.split(path.sep).join("/");
9
9
  }
10
10
 
11
- /**
12
- * Generate a short hash for an ID.
13
- * Uses first 8 chars of SHA-256 hash for uniqueness while keeping IDs short.
14
- * Appends export name for easier debugging in production: "abc123#ExportName"
15
- */
16
11
  export function hashId(filePath: string, exportName: string): string {
17
12
  const input = `${filePath}#${exportName}`;
18
13
  const hash = crypto.createHash("sha256").update(input).digest("hex");
19
14
  return `${hash.slice(0, 8)}#${exportName}`;
20
15
  }
21
16
 
22
- /**
23
- * Build a stable ID for an export binding. Uses hashed IDs in production
24
- * builds (short + opaque) and readable path#name IDs in dev.
25
- */
26
17
  export function makeStubId(
27
18
  filePath: string,
28
19
  exportName: string,
@@ -31,17 +22,6 @@ export function makeStubId(
31
22
  return isBuild ? hashId(filePath, exportName) : `${filePath}#${exportName}`;
32
23
  }
33
24
 
34
- /**
35
- * Generate an 8-char hex hash for an inline handler call site.
36
- *
37
- * Keyed on the source-order INDEX of the call (the Nth inline `fnName(...)` in
38
- * the file), NOT its line number. Line numbers shift between the prerender
39
- * build context and the production build context (preceding transforms differ,
40
- * e.g. plugin-react boilerplate), which would desync the prerender manifest key
41
- * from the runtime handler id and break prerender/static freezing. The
42
- * source-order index is invariant to line shifts; `fnName` keeps Static and
43
- * Prerender inline ids from colliding at the same index.
44
- */
45
25
  export function hashInlineId(
46
26
  filePath: string,
47
27
  fnName: string,
@@ -61,11 +41,6 @@ export interface DetectedImports {
61
41
  any: boolean;
62
42
  }
63
43
 
64
- /**
65
- * Build a map from local binding name to exported names by walking
66
- * ExportNamedDeclaration nodes. Handles `export const X`, `export { X }`,
67
- * and `export { X as Y }`. Skips re-exports (`export { X } from "..."`).
68
- */
69
44
  export function buildExportMap(program: any): Map<string, string[]> {
70
45
  const exportMap = new Map<string, string[]>();
71
46
 
@@ -105,10 +80,6 @@ export function buildExportMap(program: any): Map<string, string[]> {
105
80
  return exportMap;
106
81
  }
107
82
 
108
- /**
109
- * Single-pass detection of all create* imports from @rangojs/router.
110
- * Returns which create functions are imported so we can skip unnecessary transforms.
111
- */
112
83
  export function detectImports(code: string): DetectedImports {
113
84
  // Extract all import declarations from @rangojs/router in one scan
114
85
  const importPattern =
@@ -135,10 +106,6 @@ export function detectImports(code: string): DetectedImports {
135
106
  if (/\bcreateRouter\b/.test(imports)) result.router = true;
136
107
  }
137
108
 
138
- // createRouter has a stricter check: only from "@rangojs/router" (not sub-paths).
139
- // NOTE: This is intentional — detectImports is used as a fast pre-filter in
140
- // exposeInternalIds (which does NOT handle router transforms). The separate
141
- // exposeRouterId plugin handles createRouter and DOES accept the /server subpath.
142
109
  if (result.router) {
143
110
  result.router =
144
111
  /import\s*\{[^}]*\bcreateRouter\b[^}]*\}\s*from\s*["']@rangojs\/router["']/.test(
@@ -157,11 +124,6 @@ export function detectImports(code: string): DetectedImports {
157
124
  return result;
158
125
  }
159
126
 
160
- /**
161
- * Skip past a string literal, template literal, or comment starting at pos.
162
- * Returns the index after the closing delimiter, or pos if not at a
163
- * string/comment start. Handles escape sequences and nested ${} in templates.
164
- */
165
127
  export function skipStringOrComment(code: string, pos: number): number {
166
128
  const ch = code[pos];
167
129
 
@@ -219,11 +181,6 @@ export function skipStringOrComment(code: string, pos: number): number {
219
181
  return pos;
220
182
  }
221
183
 
222
- /**
223
- * Find the matching closing paren starting after an already-opened paren.
224
- * Skips strings, template literals, and comments so parens inside them
225
- * don't affect depth tracking. Returns the index after the closing paren.
226
- */
227
184
  export function findMatchingParen(code: string, startPos: number): number {
228
185
  let depth = 1;
229
186
  let i = startPos;
@@ -240,10 +197,6 @@ export function findMatchingParen(code: string, startPos: number): number {
240
197
  return i;
241
198
  }
242
199
 
243
- /**
244
- * Count the number of top-level arguments in a function call.
245
- * Skips nested parens, brackets, braces, strings, and comments.
246
- */
247
200
  export function countArgs(
248
201
  code: string,
249
202
  startPos: number,
@@ -279,10 +232,6 @@ export function countArgs(
279
232
  return hasContent ? argCount + 1 : 0;
280
233
  }
281
234
 
282
- /**
283
- * Find the end of a statement: skip whitespace and optional semicolon after
284
- * a closing paren position.
285
- */
286
235
  export function findStatementEnd(code: string, pos: number): number {
287
236
  let i = pos;
288
237
  while (i < code.length && /\s/.test(code[i])) {
@@ -294,10 +243,6 @@ export function findStatementEnd(code: string, pos: number): number {
294
243
  return i;
295
244
  }
296
245
 
297
- /**
298
- * Escape special regex characters in a string so it can be safely
299
- * interpolated into a RegExp pattern.
300
- */
301
246
  export function escapeRegExp(input: string): string {
302
247
  return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
303
248
  }