@rangojs/router 0.0.0-experimental.d7eeaa75 → 0.0.0-experimental.dc2bd2b4

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 (253) hide show
  1. package/README.md +120 -25
  2. package/dist/bin/rango.js +147 -57
  3. package/dist/testing/vitest.js +48 -0
  4. package/dist/vite/index.js +2151 -846
  5. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  6. package/package.json +57 -11
  7. package/skills/breadcrumbs/SKILL.md +3 -1
  8. package/skills/bundle-analysis/SKILL.md +159 -0
  9. package/skills/cache-guide/SKILL.md +220 -30
  10. package/skills/caching/SKILL.md +116 -8
  11. package/skills/composability/SKILL.md +27 -2
  12. package/skills/document-cache/SKILL.md +78 -55
  13. package/skills/handler-use/SKILL.md +364 -0
  14. package/skills/hooks/SKILL.md +229 -20
  15. package/skills/host-router/SKILL.md +45 -20
  16. package/skills/i18n/SKILL.md +276 -0
  17. package/skills/intercept/SKILL.md +46 -4
  18. package/skills/layout/SKILL.md +28 -7
  19. package/skills/links/SKILL.md +247 -17
  20. package/skills/loader/SKILL.md +219 -9
  21. package/skills/middleware/SKILL.md +47 -12
  22. package/skills/migrate-nextjs/SKILL.md +562 -0
  23. package/skills/migrate-react-router/SKILL.md +769 -0
  24. package/skills/mime-routes/SKILL.md +27 -0
  25. package/skills/observability/SKILL.md +137 -0
  26. package/skills/parallel/SKILL.md +71 -6
  27. package/skills/prerender/SKILL.md +14 -33
  28. package/skills/rango/SKILL.md +242 -22
  29. package/skills/react-compiler/SKILL.md +168 -0
  30. package/skills/response-routes/SKILL.md +66 -9
  31. package/skills/route/SKILL.md +57 -4
  32. package/skills/router-setup/SKILL.md +3 -3
  33. package/skills/server-actions/SKILL.md +751 -0
  34. package/skills/streams-and-websockets/SKILL.md +283 -0
  35. package/skills/testing/SKILL.md +647 -0
  36. package/skills/typesafety/SKILL.md +319 -27
  37. package/skills/use-cache/SKILL.md +34 -5
  38. package/skills/view-transitions/SKILL.md +294 -0
  39. package/src/__augment-tests__/augment.ts +81 -0
  40. package/src/__augment-tests__/augmented.check.ts +117 -0
  41. package/src/browser/action-coordinator.ts +53 -36
  42. package/src/browser/app-shell.ts +52 -0
  43. package/src/browser/event-controller.ts +86 -70
  44. package/src/browser/history-state.ts +21 -0
  45. package/src/browser/index.ts +3 -3
  46. package/src/browser/navigation-bridge.ts +84 -11
  47. package/src/browser/navigation-client.ts +76 -28
  48. package/src/browser/navigation-store.ts +32 -9
  49. package/src/browser/navigation-transaction.ts +10 -28
  50. package/src/browser/partial-update.ts +64 -26
  51. package/src/browser/prefetch/cache.ts +129 -21
  52. package/src/browser/prefetch/fetch.ts +148 -16
  53. package/src/browser/prefetch/queue.ts +36 -5
  54. package/src/browser/rango-state.ts +53 -13
  55. package/src/browser/react/Link.tsx +30 -2
  56. package/src/browser/react/NavigationProvider.tsx +72 -31
  57. package/src/browser/react/filter-segment-order.ts +51 -7
  58. package/src/browser/react/index.ts +3 -0
  59. package/src/browser/react/location-state-shared.ts +175 -4
  60. package/src/browser/react/location-state.ts +39 -13
  61. package/src/browser/react/use-handle.ts +17 -9
  62. package/src/browser/react/use-navigation.ts +22 -2
  63. package/src/browser/react/use-params.ts +20 -8
  64. package/src/browser/react/use-reverse.ts +106 -0
  65. package/src/browser/react/use-router.ts +22 -2
  66. package/src/browser/react/use-segments.ts +11 -8
  67. package/src/browser/response-adapter.ts +25 -0
  68. package/src/browser/rsc-router.tsx +64 -22
  69. package/src/browser/scroll-restoration.ts +22 -14
  70. package/src/browser/segment-reconciler.ts +36 -14
  71. package/src/browser/segment-structure-assert.ts +2 -2
  72. package/src/browser/server-action-bridge.ts +23 -30
  73. package/src/browser/types.ts +21 -0
  74. package/src/build/collect-fallback-refs.ts +107 -0
  75. package/src/build/generate-manifest.ts +60 -35
  76. package/src/build/generate-route-types.ts +2 -0
  77. package/src/build/index.ts +2 -0
  78. package/src/build/route-trie.ts +52 -25
  79. package/src/build/route-types/codegen.ts +4 -4
  80. package/src/build/route-types/include-resolution.ts +1 -1
  81. package/src/build/route-types/per-module-writer.ts +7 -4
  82. package/src/build/route-types/router-processing.ts +55 -14
  83. package/src/build/route-types/scan-filter.ts +1 -1
  84. package/src/build/route-types/source-scan.ts +118 -0
  85. package/src/build/runtime-discovery.ts +9 -20
  86. package/src/cache/cache-scope.ts +28 -42
  87. package/src/cache/cf/cf-cache-store.ts +54 -13
  88. package/src/client.rsc.tsx +3 -0
  89. package/src/client.tsx +92 -182
  90. package/src/context-var.ts +5 -5
  91. package/src/decode-loader-results.ts +36 -0
  92. package/src/errors.ts +30 -1
  93. package/src/handle.ts +26 -13
  94. package/src/host/index.ts +2 -2
  95. package/src/host/router.ts +129 -57
  96. package/src/host/types.ts +31 -2
  97. package/src/host/utils.ts +1 -1
  98. package/src/href-client.ts +140 -20
  99. package/src/index.rsc.ts +9 -4
  100. package/src/index.ts +53 -15
  101. package/src/loader-store.ts +500 -0
  102. package/src/loader.rsc.ts +2 -5
  103. package/src/loader.ts +3 -10
  104. package/src/missing-id-error.ts +68 -0
  105. package/src/outlet-context.ts +1 -1
  106. package/src/prerender.ts +4 -4
  107. package/src/response-utils.ts +37 -0
  108. package/src/reverse.ts +65 -36
  109. package/src/route-content-wrapper.tsx +6 -28
  110. package/src/route-definition/dsl-helpers.ts +384 -257
  111. package/src/route-definition/helper-factories.ts +29 -139
  112. package/src/route-definition/helpers-types.ts +100 -28
  113. package/src/route-definition/resolve-handler-use.ts +6 -0
  114. package/src/route-definition/use-item-types.ts +32 -0
  115. package/src/route-types.ts +26 -41
  116. package/src/router/basename.ts +14 -0
  117. package/src/router/content-negotiation.ts +15 -2
  118. package/src/router/error-handling.ts +1 -1
  119. package/src/router/handler-context.ts +21 -38
  120. package/src/router/intercept-resolution.ts +4 -18
  121. package/src/router/lazy-includes.ts +8 -8
  122. package/src/router/loader-resolution.ts +19 -2
  123. package/src/router/manifest.ts +22 -13
  124. package/src/router/match-api.ts +4 -3
  125. package/src/router/match-handlers.ts +63 -20
  126. package/src/router/match-middleware/cache-lookup.ts +44 -91
  127. package/src/router/match-middleware/cache-store.ts +3 -2
  128. package/src/router/match-result.ts +53 -32
  129. package/src/router/metrics.ts +1 -1
  130. package/src/router/middleware-types.ts +15 -26
  131. package/src/router/middleware.ts +99 -84
  132. package/src/router/pattern-matching.ts +101 -17
  133. package/src/router/prerender-match.ts +1 -1
  134. package/src/router/preview-match.ts +3 -1
  135. package/src/router/request-classification.ts +4 -28
  136. package/src/router/revalidation.ts +58 -2
  137. package/src/router/router-interfaces.ts +45 -28
  138. package/src/router/router-options.ts +40 -1
  139. package/src/router/router-registry.ts +2 -5
  140. package/src/router/segment-resolution/fresh.ts +27 -6
  141. package/src/router/segment-resolution/revalidation.ts +147 -106
  142. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  143. package/src/router/substitute-pattern-params.ts +56 -0
  144. package/src/router/telemetry.ts +99 -0
  145. package/src/router/trie-matching.ts +18 -13
  146. package/src/router/types.ts +8 -0
  147. package/src/router/url-params.ts +49 -0
  148. package/src/router.ts +38 -23
  149. package/src/rsc/handler-context.ts +2 -2
  150. package/src/rsc/handler.ts +28 -69
  151. package/src/rsc/helpers.ts +91 -43
  152. package/src/rsc/index.ts +1 -1
  153. package/src/rsc/origin-guard.ts +28 -10
  154. package/src/rsc/progressive-enhancement.ts +4 -0
  155. package/src/rsc/response-route-handler.ts +46 -53
  156. package/src/rsc/rsc-rendering.ts +35 -51
  157. package/src/rsc/runtime-warnings.ts +9 -10
  158. package/src/rsc/server-action.ts +17 -37
  159. package/src/rsc/ssr-setup.ts +16 -0
  160. package/src/rsc/types.ts +8 -2
  161. package/src/search-params.ts +4 -4
  162. package/src/segment-content-promise.ts +67 -0
  163. package/src/segment-loader-promise.ts +122 -0
  164. package/src/segment-system.tsx +132 -116
  165. package/src/serialize.ts +243 -0
  166. package/src/server/context.ts +143 -53
  167. package/src/server/cookie-store.ts +28 -4
  168. package/src/server/request-context.ts +20 -42
  169. package/src/ssr/index.tsx +5 -1
  170. package/src/static-handler.ts +1 -1
  171. package/src/testing/cache-status.ts +166 -0
  172. package/src/testing/collect-handle.ts +63 -0
  173. package/src/testing/dispatch.ts +440 -0
  174. package/src/testing/dom.entry.ts +22 -0
  175. package/src/testing/e2e/fixture.ts +154 -0
  176. package/src/testing/e2e/index.ts +149 -0
  177. package/src/testing/e2e/matchers.ts +51 -0
  178. package/src/testing/e2e/page-helpers.ts +272 -0
  179. package/src/testing/e2e/parity.ts +306 -0
  180. package/src/testing/e2e/server.ts +183 -0
  181. package/src/testing/flight-matchers.ts +104 -0
  182. package/src/testing/flight-runtime.d.ts +21 -0
  183. package/src/testing/flight.entry.ts +22 -0
  184. package/src/testing/flight.ts +182 -0
  185. package/src/testing/generated-routes.ts +223 -0
  186. package/src/testing/index.ts +105 -0
  187. package/src/testing/internal/context.ts +193 -0
  188. package/src/testing/render-route.tsx +536 -0
  189. package/src/testing/run-loader.ts +296 -0
  190. package/src/testing/run-middleware.ts +170 -0
  191. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  192. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  193. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  194. package/src/testing/vitest-stubs/version.ts +5 -0
  195. package/src/testing/vitest.ts +183 -0
  196. package/src/types/global-namespace.ts +39 -26
  197. package/src/types/handler-context.ts +68 -50
  198. package/src/types/index.ts +1 -0
  199. package/src/types/loader-types.ts +5 -6
  200. package/src/types/request-scope.ts +126 -0
  201. package/src/types/route-entry.ts +11 -0
  202. package/src/types/segments.ts +35 -2
  203. package/src/urls/include-helper.ts +34 -67
  204. package/src/urls/index.ts +0 -3
  205. package/src/urls/path-helper-types.ts +41 -7
  206. package/src/urls/path-helper.ts +17 -52
  207. package/src/urls/pattern-types.ts +36 -19
  208. package/src/urls/response-types.ts +22 -29
  209. package/src/urls/type-extraction.ts +26 -116
  210. package/src/urls/urls-function.ts +1 -5
  211. package/src/use-loader.tsx +413 -42
  212. package/src/vite/debug.ts +185 -0
  213. package/src/vite/discovery/bundle-postprocess.ts +6 -6
  214. package/src/vite/discovery/discover-routers.ts +101 -51
  215. package/src/vite/discovery/discovery-errors.ts +194 -0
  216. package/src/vite/discovery/gate-state.ts +171 -0
  217. package/src/vite/discovery/prerender-collection.ts +67 -26
  218. package/src/vite/discovery/route-types-writer.ts +40 -84
  219. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  220. package/src/vite/discovery/state.ts +33 -0
  221. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  222. package/src/vite/index.ts +2 -0
  223. package/src/vite/plugin-types.ts +67 -0
  224. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  225. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  226. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  227. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  228. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  229. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  230. package/src/vite/plugins/expose-action-id.ts +54 -30
  231. package/src/vite/plugins/expose-id-utils.ts +12 -8
  232. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  233. package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
  234. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  235. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  236. package/src/vite/plugins/expose-internal-ids.ts +496 -486
  237. package/src/vite/plugins/performance-tracks.ts +29 -25
  238. package/src/vite/plugins/use-cache-transform.ts +65 -50
  239. package/src/vite/plugins/version-injector.ts +39 -23
  240. package/src/vite/plugins/version-plugin.ts +59 -2
  241. package/src/vite/plugins/virtual-entries.ts +2 -2
  242. package/src/vite/rango.ts +116 -29
  243. package/src/vite/router-discovery.ts +750 -100
  244. package/src/vite/utils/ast-handler-extract.ts +15 -15
  245. package/src/vite/utils/banner.ts +1 -1
  246. package/src/vite/utils/bundle-analysis.ts +4 -2
  247. package/src/vite/utils/client-chunks.ts +190 -0
  248. package/src/vite/utils/forward-user-plugins.ts +193 -0
  249. package/src/vite/utils/manifest-utils.ts +21 -5
  250. package/src/vite/utils/package-resolution.ts +41 -1
  251. package/src/vite/utils/prerender-utils.ts +21 -6
  252. package/src/vite/utils/shared-utils.ts +107 -26
  253. package/src/browser/action-response-classifier.ts +0 -99
@@ -28,6 +28,7 @@ import {
28
28
  getImportedFnNames,
29
29
  collectCreateExportBindings,
30
30
  buildUnsupportedShapeWarning,
31
+ findUnsupportedCreateCallSites,
31
32
  isExportOnlyFile,
32
33
  } from "./expose-ids/export-analysis.js";
33
34
  import {
@@ -39,10 +40,12 @@ import {
39
40
  transformHandles,
40
41
  transformLocationState,
41
42
  generateWholeFileStubs,
42
- generateExprStubs,
43
43
  stubHandlerExprs,
44
44
  transformHandlerIds,
45
45
  } from "./expose-ids/handler-transform.js";
46
+ import { createRangoDebugger, createCounter, NS } from "../debug.js";
47
+
48
+ const debug = createRangoDebugger(NS.transform);
46
49
 
47
50
  // Re-exports consumed by other packages/plugins
48
51
  export { exposeRouterId } from "./expose-ids/router-transform.js";
@@ -87,10 +90,16 @@ export function exposeInternalIds(options?: { forceBuild?: boolean }): Plugin {
87
90
  // De-duplicate unsupported shape warnings across repeated transforms.
88
91
  const unsupportedShapeWarnings = new Set<string>();
89
92
 
93
+ const counter = createCounter(debug, "expose-internal-ids");
94
+
90
95
  return {
91
96
  name: "@rangojs/router:expose-internal-ids",
92
97
  enforce: "post",
93
98
 
99
+ buildEnd() {
100
+ counter?.flush();
101
+ },
102
+
94
103
  api: {
95
104
  prerenderHandlerModules,
96
105
  staticHandlerModules,
@@ -233,554 +242,555 @@ ${lazyImports.join(",\n")}
233
242
  transform(code, id) {
234
243
  if (id.includes("/node_modules/")) return;
235
244
 
236
- const filePath = normalizePath(path.relative(projectRoot, id));
237
- const isRscEnv = this.environment?.name === "rsc";
238
-
239
- // Warn if named-routes.gen is imported in a client component.
240
- // NamedRoutes is server-only data and would bloat the client bundle.
241
- if (
242
- id.includes(".named-routes.gen.") &&
243
- !isRscEnv &&
244
- this.environment?.name === "client"
245
- ) {
246
- this.warn(
247
- `\n` +
248
- `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n` +
249
- `!! !!\n` +
250
- `!! WARNING: NamedRoutes imported in a CLIENT component! !!\n` +
251
- `!! !!\n` +
252
- `!! File: ${filePath.padEnd(53)}!!\n` +
253
- `!! !!\n` +
254
- `!! NamedRoutes contains your entire route structure — every !!\n` +
255
- `!! route name and URL pattern in your application. Shipping !!\n` +
256
- `!! this to the browser exposes your full routing topology to !!\n` +
257
- `!! the client, which is a security concern (internal/admin !!\n` +
258
- `!! routes, API endpoints, hidden paths become visible). !!\n` +
259
- `!! !!\n` +
260
- `!! It also bloats the client bundle — this map contains all !!\n` +
261
- `!! named routes in your application. !!\n` +
262
- `!! !!\n` +
263
- `!! Fix: remove the import or move it to a server component. !!\n` +
264
- `!! !!\n` +
265
- `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n`,
266
- );
267
- }
268
-
269
- // Fast exit: if the file doesn't import from @rangojs/router at all,
270
- // skip all create* analysis and transforms.
271
- if (!code.includes("@rangojs/router")) return;
272
-
273
- // Detect all relevant imports in one pass
274
- const has = detectImports(code);
275
-
276
- // Quick bail-out: also check for raw create* identifiers.
277
- // This is safe even with aliases (e.g., `import { createLoader as cl }`)
278
- // because the import statement itself always contains the canonical name
279
- // "createLoader", so code.includes("createLoader") will still match.
280
- const hasLoaderCode = has.loader && code.includes("createLoader");
281
- const hasHandleCode = has.handle && code.includes("createHandle");
282
- const hasLocationStateCode =
283
- has.locationState && code.includes("createLocationState");
284
- const hasPrerenderHandlerCode =
285
- has.prerenderHandler && code.includes("Prerender");
286
- const hasStaticHandlerCode = has.staticHandler && code.includes("Static");
287
- if (
288
- !hasLoaderCode &&
289
- !hasHandleCode &&
290
- !hasLocationStateCode &&
291
- !hasPrerenderHandlerCode &&
292
- !hasStaticHandlerCode
293
- ) {
294
- return;
295
- }
245
+ const __t0 = counter ? performance.now() : 0;
246
+ try {
247
+ const filePath = normalizePath(path.relative(projectRoot, id));
248
+ const isRscEnv = this.environment?.name === "rsc";
296
249
 
297
- // Per-invocation caches to avoid redundant AST parsing.
298
- // getImportedFnNames is cached by canonical name (imports never change).
299
- // collectCreateExportBindings is cached by fnNames key; the cache is
300
- // cleared when `code` changes (e.g., after inline handler extraction).
301
- const _fnNamesCache = new Map<string, string[]>();
302
- const _bindingsCache = new Map<string, CreateExportBinding[]>();
303
- let _cachedAst: any;
304
- let _astParseFailed = false;
305
- let _astCodeRef = code;
306
-
307
- const getFnNames = (canonicalName: string): string[] => {
308
- let result = _fnNamesCache.get(canonicalName);
309
- if (!result) {
310
- result = getImportedFnNames(code, canonicalName);
311
- _fnNamesCache.set(canonicalName, result);
312
- }
313
- return result;
314
- };
315
-
316
- // Lazy AST parse: parsed once and shared across all
317
- // collectCreateExportBindings calls for the same code string.
318
- const lazyAst = (): any | undefined => {
319
- if (code !== _astCodeRef) {
320
- _cachedAst = undefined;
321
- _astParseFailed = false;
322
- _astCodeRef = code;
323
- }
324
- if (_cachedAst !== undefined || _astParseFailed) return _cachedAst;
325
- try {
326
- _cachedAst = parseAst(code, { jsx: true });
327
- } catch {
328
- _astParseFailed = true;
329
- }
330
- return _cachedAst;
331
- };
332
-
333
- const getBindings = (
334
- currentCode: string,
335
- fnNames: string[],
336
- ): CreateExportBinding[] => {
337
- const key = fnNames.join("\0");
338
- let result = _bindingsCache.get(key);
339
- if (!result) {
340
- result = collectCreateExportBindings(currentCode, fnNames, lazyAst());
341
- _bindingsCache.set(key, result);
250
+ // Warn if named-routes.gen is imported in a client component.
251
+ // NamedRoutes is server-only data and would bloat the client bundle.
252
+ if (
253
+ id.includes(".named-routes.gen.") &&
254
+ !isRscEnv &&
255
+ this.environment?.name === "client"
256
+ ) {
257
+ this.warn(
258
+ `\n` +
259
+ `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n` +
260
+ `!! !!\n` +
261
+ `!! WARNING: NamedRoutes imported in a CLIENT component! !!\n` +
262
+ `!! !!\n` +
263
+ `!! File: ${filePath.padEnd(53)}!!\n` +
264
+ `!! !!\n` +
265
+ `!! NamedRoutes contains your entire route structure — every !!\n` +
266
+ `!! route name and URL pattern in your application. Shipping !!\n` +
267
+ `!! this to the browser exposes your full routing topology to !!\n` +
268
+ `!! the client, which is a security concern (internal/admin !!\n` +
269
+ `!! routes, API endpoints, hidden paths become visible). !!\n` +
270
+ `!! !!\n` +
271
+ `!! It also bloats the client bundle this map contains all !!\n` +
272
+ `!! named routes in your application. !!\n` +
273
+ `!! !!\n` +
274
+ `!! Fix: remove the import or move it to a server component. !!\n` +
275
+ `!! !!\n` +
276
+ `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n`,
277
+ );
342
278
  }
343
- return result;
344
- };
345
-
346
- // Warn on create* declaration shapes that are currently unsupported by
347
- // non-AST transforms (loader/handle/locationState only).
348
- for (const cfg of STRICT_CREATE_CONFIGS) {
349
- const hasCode =
350
- cfg.fnName === "createLoader"
351
- ? hasLoaderCode
352
- : cfg.fnName === "createHandle"
353
- ? hasHandleCode
354
- : hasLocationStateCode;
355
- if (!hasCode) continue;
356
-
357
- const fnNames = getFnNames(cfg.fnName);
358
- const totalCalls = countCreateCallsForNames(code, fnNames);
359
- const supportedBindings = getBindings(code, fnNames).length;
360
- if (totalCalls <= supportedBindings) continue;
361
-
362
- const warnKey = `${id}::${cfg.fnName}`;
363
- if (unsupportedShapeWarnings.has(warnKey)) continue;
364
- unsupportedShapeWarnings.add(warnKey);
365
- this.warn(buildUnsupportedShapeWarning(filePath, cfg.fnName));
366
- }
367
279
 
368
- // --- Loader: track for manifest (RSC env only) ---
369
- if (hasLoaderCode && isRscEnv) {
370
- const fnNames = getFnNames("createLoader");
371
- const bindings = getBindings(code, fnNames);
372
- for (const binding of bindings) {
373
- const exportName = binding.exportNames[0];
374
- const hashedId = hashId(filePath, exportName);
375
- loaderRegistry.set(hashedId, {
376
- filePath,
377
- exportName,
378
- });
280
+ // Fast exit: if the file doesn't import from @rangojs/router at all,
281
+ // skip all create* analysis and transforms.
282
+ if (!code.includes("@rangojs/router")) return;
283
+
284
+ // Detect all relevant imports in one pass
285
+ const has = detectImports(code);
286
+
287
+ // Quick bail-out: also check for raw create* identifiers.
288
+ // This is safe even with aliases (e.g., `import { createLoader as cl }`)
289
+ // because the import statement itself always contains the canonical name
290
+ // "createLoader", so code.includes("createLoader") will still match.
291
+ const hasLoaderCode = has.loader && code.includes("createLoader");
292
+ const hasHandleCode = has.handle && code.includes("createHandle");
293
+ const hasLocationStateCode =
294
+ has.locationState && code.includes("createLocationState");
295
+ const hasPrerenderHandlerCode =
296
+ has.prerenderHandler && code.includes("Prerender");
297
+ const hasStaticHandlerCode =
298
+ has.staticHandler && code.includes("Static");
299
+ if (
300
+ !hasLoaderCode &&
301
+ !hasHandleCode &&
302
+ !hasLocationStateCode &&
303
+ !hasPrerenderHandlerCode &&
304
+ !hasStaticHandlerCode
305
+ ) {
306
+ return;
379
307
  }
380
- }
381
308
 
382
- // --- Loader: client stubs for non-RSC environments ---
383
- if (hasLoaderCode && !isRscEnv) {
384
- const fnNames = getFnNames("createLoader");
385
- const bindings = getBindings(code, fnNames);
386
- const stubResult = generateClientLoaderStubs(
387
- bindings,
388
- code,
389
- filePath,
390
- isBuild,
391
- );
392
- if (stubResult) return stubResult;
393
- }
309
+ // Per-invocation caches to avoid redundant AST parsing.
310
+ // getImportedFnNames is cached by canonical name (imports never change).
311
+ // collectCreateExportBindings is cached by fnNames key; the cache is
312
+ // cleared when `code` changes (e.g., after inline handler extraction).
313
+ const _fnNamesCache = new Map<string, string[]>();
314
+ const _bindingsCache = new Map<string, CreateExportBinding[]>();
315
+ let _cachedAst: any;
316
+ let _astParseFailed = false;
317
+ let _astCodeRef = code;
318
+
319
+ const getFnNames = (canonicalName: string): string[] => {
320
+ let result = _fnNamesCache.get(canonicalName);
321
+ if (!result) {
322
+ result = getImportedFnNames(code, canonicalName);
323
+ _fnNamesCache.set(canonicalName, result);
324
+ }
325
+ return result;
326
+ };
327
+
328
+ // Lazy AST parse: parsed once and shared across all
329
+ // collectCreateExportBindings calls for the same code string.
330
+ const lazyAst = (): any | undefined => {
331
+ if (code !== _astCodeRef) {
332
+ _cachedAst = undefined;
333
+ _astParseFailed = false;
334
+ _astCodeRef = code;
335
+ }
336
+ if (_cachedAst !== undefined || _astParseFailed) return _cachedAst;
337
+ try {
338
+ _cachedAst = parseAst(code, { lang: "tsx" });
339
+ } catch {
340
+ _astParseFailed = true;
341
+ }
342
+ return _cachedAst;
343
+ };
344
+
345
+ const getBindings = (
346
+ currentCode: string,
347
+ fnNames: string[],
348
+ ): CreateExportBinding[] => {
349
+ const key = fnNames.join("\0");
350
+ let result = _bindingsCache.get(key);
351
+ if (!result) {
352
+ result = collectCreateExportBindings(
353
+ currentCode,
354
+ fnNames,
355
+ lazyAst(),
356
+ );
357
+ _bindingsCache.set(key, result);
358
+ }
359
+ return result;
360
+ };
361
+
362
+ // Warn on create* declaration shapes that are currently unsupported by
363
+ // non-AST transforms (loader/handle/locationState only).
364
+ for (const cfg of STRICT_CREATE_CONFIGS) {
365
+ const hasCode =
366
+ cfg.fnName === "createLoader"
367
+ ? hasLoaderCode
368
+ : cfg.fnName === "createHandle"
369
+ ? hasHandleCode
370
+ : hasLocationStateCode;
371
+ if (!hasCode) continue;
394
372
 
395
- // --- PrerenderHandler: non-RSC whole-file stub replacement ---
396
- // When ALL exports are Prerender() calls, replace the entire file.
397
- // Mixed-export files are handled in the unified pipeline below.
398
- if (hasPrerenderHandlerCode && !isRscEnv) {
399
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
400
- const bindings = getBindings(code, fnNames);
401
- const wholeFile = generateWholeFileStubs(
402
- PRERENDER_CONFIG,
403
- bindings,
404
- code,
405
- filePath,
406
- isBuild,
407
- );
408
- if (wholeFile) return wholeFile;
409
- }
373
+ const fnNames = getFnNames(cfg.fnName);
374
+ // Locate the real (comment/string-free) create* calls not covered by
375
+ // a supported, id-injectable export shape. Empty means every call is
376
+ // fine in particular, a create*() token in a comment or string no
377
+ // longer trips a spurious warning.
378
+ const sites = findUnsupportedCreateCallSites(
379
+ code,
380
+ fnNames,
381
+ getBindings(code, fnNames),
382
+ );
383
+ if (sites.length === 0) continue;
410
384
 
411
- // --- PrerenderHandler: RSC build module tracking ---
412
- if (hasPrerenderHandlerCode && isRscEnv && isBuild) {
413
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
414
- const exportNames = getBindings(code, fnNames).map(
415
- (b) => b.exportNames[0],
416
- );
417
- if (exportNames.length > 0) {
418
- prerenderHandlerModules.set(id, exportNames);
385
+ const warnKey = `${id}::${cfg.fnName}`;
386
+ if (unsupportedShapeWarnings.has(warnKey)) continue;
387
+ unsupportedShapeWarnings.add(warnKey);
388
+ this.warn(buildUnsupportedShapeWarning(filePath, cfg.fnName, sites));
419
389
  }
420
- }
421
390
 
422
- // --- Inline handler extraction to virtual modules ---
423
- // Runs before stubs/tracking so inline calls become imports, then
424
- // the existing regex fast path handles both the original file's
425
- // export const patterns and the virtual modules independently.
426
- //
427
- // Cheap pre-check: count total fnName( occurrences vs export const
428
- // patterns. If they match, every call is a named export and the
429
- // regex fast path handles them -- skip the AST parse entirely.
430
- //
431
- // Each iteration creates a fresh MagicString so that AST positions
432
- // from findHandlerCalls always match the string they were parsed from.
433
- let changed = false;
434
-
435
- const handlerConfigs = [
436
- hasStaticHandlerCode && STATIC_CONFIG,
437
- hasPrerenderHandlerCode && PRERENDER_CONFIG,
438
- ]
439
- .filter((c): c is HandlerTransformConfig => !!c)
440
- .map((cfg) => {
441
- const fnNames = getFnNames(cfg.fnName);
442
- return { cfg, fnNames };
443
- });
444
-
445
- for (const { cfg, fnNames } of handlerConfigs) {
446
- const totalCalls = countCreateCallsForNames(code, fnNames);
447
- const supportedBindings = getBindings(code, fnNames).length;
448
-
449
- if (totalCalls > supportedBindings) {
450
- const iterS = new MagicString(code);
451
- const result = transformInlineHandlers(
452
- cfg.fnName,
453
- VIRTUAL_HANDLER_PREFIX,
454
- iterS,
391
+ // --- Loader: track for manifest (RSC env only) ---
392
+ if (hasLoaderCode && isRscEnv) {
393
+ const fnNames = getFnNames("createLoader");
394
+ const bindings = getBindings(code, fnNames);
395
+ for (const binding of bindings) {
396
+ const exportName = binding.exportNames[0];
397
+ const hashedId = hashId(filePath, exportName);
398
+ loaderRegistry.set(hashedId, {
399
+ filePath,
400
+ exportName,
401
+ });
402
+ }
403
+ }
404
+
405
+ // --- Loader: client stubs for non-RSC environments ---
406
+ if (hasLoaderCode && !isRscEnv) {
407
+ const fnNames = getFnNames("createLoader");
408
+ const bindings = getBindings(code, fnNames);
409
+ const stubResult = generateClientLoaderStubs(
410
+ bindings,
455
411
  code,
456
412
  filePath,
457
- virtualHandlers,
458
- id,
459
- parseAst,
413
+ isBuild,
460
414
  );
461
- if (result) {
462
- changed = true;
463
- code = iterS.toString();
464
- _bindingsCache.clear();
465
- }
415
+ if (stubResult) return stubResult;
466
416
  }
467
- }
468
417
 
469
- // --- StaticHandler: non-RSC whole-file stub replacement ---
470
- // When ALL exports are Static() calls, replace the entire file.
471
- if (hasStaticHandlerCode && !isRscEnv) {
472
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
473
- const bindings = getBindings(code, fnNames);
474
- const wholeFile = generateWholeFileStubs(
475
- STATIC_CONFIG,
476
- bindings,
477
- code,
478
- filePath,
479
- isBuild,
480
- );
481
- if (wholeFile) return wholeFile;
482
- }
418
+ // --- PrerenderHandler: non-RSC whole-file stub replacement ---
419
+ // When ALL exports are Prerender() calls, replace the entire file.
420
+ // Mixed-export files are handled in the unified pipeline below.
421
+ if (hasPrerenderHandlerCode && !isRscEnv) {
422
+ const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
423
+ const bindings = getBindings(code, fnNames);
424
+ const wholeFile = generateWholeFileStubs(
425
+ PRERENDER_CONFIG,
426
+ bindings,
427
+ code,
428
+ filePath,
429
+ isBuild,
430
+ );
431
+ if (wholeFile) return wholeFile;
432
+ }
433
+
434
+ // --- Inline handler extraction to virtual modules ---
435
+ // Runs before stubs/tracking so inline calls become imports, then
436
+ // the existing regex fast path handles both the original file's
437
+ // export const patterns and the virtual modules independently.
438
+ //
439
+ // Cheap pre-check: count total fnName( occurrences vs export const
440
+ // patterns. If they match, every call is a named export and the
441
+ // regex fast path handles them -- skip the AST parse entirely.
442
+ //
443
+ // Each iteration creates a fresh MagicString so that AST positions
444
+ // from findHandlerCalls always match the string they were parsed from.
445
+ let changed = false;
446
+
447
+ const handlerConfigs = [
448
+ hasStaticHandlerCode && STATIC_CONFIG,
449
+ hasPrerenderHandlerCode && PRERENDER_CONFIG,
450
+ ]
451
+ .filter((c): c is HandlerTransformConfig => !!c)
452
+ .map((cfg) => {
453
+ const fnNames = getFnNames(cfg.fnName);
454
+ return { cfg, fnNames };
455
+ });
483
456
 
484
- // --- Mixed-type whole-file stub replacement (non-RSC) ---
485
- // When the individual whole-file checks above fail (each only checks
486
- // one type), the file has mixed exports (e.g. createLoader + Prerender).
487
- // Gather ALL stub-safe bindings and check if they cover every export.
488
- // If yes, replace the entire file with stubs — this strips server-only
489
- // imports (node:fs, DB clients, etc.) that would crash in the browser.
490
- //
491
- // Only applies when the file contains Prerender/Static (the handler
492
- // types that bring server-only code). Files with only loaders, handles,
493
- // or locationState are handled correctly by the unified pipeline below.
494
- //
495
- // Loader, Prerender, and Static exports become plain { __brand, $$id }
496
- // stubs. createHandle and createLocationState need their create*()
497
- // functions to execute (collect registration / __rsc_ls_key), so their
498
- // call expressions are preserved with only a @rangojs/router import.
499
- // This strips all server-only imports while keeping the correct
500
- // client contract for every export type.
501
- if (!isRscEnv && (hasPrerenderHandlerCode || hasStaticHandlerCode)) {
502
- const prerenderFnNames = hasPrerenderHandlerCode
503
- ? getFnNames(PRERENDER_CONFIG.fnName)
504
- : [];
505
- const staticFnNames = hasStaticHandlerCode
506
- ? getFnNames(STATIC_CONFIG.fnName)
507
- : [];
508
- const loaderFnNames = hasLoaderCode ? getFnNames("createLoader") : [];
509
- const handleFnNames = hasHandleCode ? getFnNames("createHandle") : [];
510
- const lsFnNames = hasLocationStateCode
511
- ? getFnNames("createLocationState")
512
- : [];
513
-
514
- // Collect ALL recognized bindings to check export coverage
515
- const allBindings: CreateExportBinding[] = [];
516
- for (const fnNames of [
517
- prerenderFnNames,
518
- staticFnNames,
519
- loaderFnNames,
520
- handleFnNames,
521
- lsFnNames,
522
- ]) {
523
- if (fnNames.length > 0) {
524
- allBindings.push(...getBindings(code, fnNames));
457
+ for (const { cfg, fnNames } of handlerConfigs) {
458
+ const totalCalls = countCreateCallsForNames(code, fnNames);
459
+ const supportedBindings = getBindings(code, fnNames).length;
460
+
461
+ if (totalCalls > supportedBindings) {
462
+ const iterS = new MagicString(code);
463
+ const result = transformInlineHandlers(
464
+ cfg.fnName,
465
+ VIRTUAL_HANDLER_PREFIX,
466
+ iterS,
467
+ code,
468
+ filePath,
469
+ virtualHandlers,
470
+ id,
471
+ parseAst,
472
+ );
473
+ if (result) {
474
+ changed = true;
475
+ code = iterS.toString();
476
+ _bindingsCache.clear();
477
+ }
525
478
  }
526
479
  }
527
480
 
528
- // Check if preserved createHandle/createLocationState calls
529
- // reference non-exported locals (e.g. helper functions, constants).
530
- // If so, the whole-file stub would strip those locals, breaking
531
- // the call. Fall through to the unified pipeline instead.
532
- let canStubWholeFile =
533
- allBindings.length > 0 && isExportOnlyFile(code, allBindings);
481
+ // --- StaticHandler: non-RSC whole-file stub replacement ---
482
+ // When ALL exports are Static() calls, replace the entire file.
483
+ if (hasStaticHandlerCode && !isRscEnv) {
484
+ const fnNames = getFnNames(STATIC_CONFIG.fnName);
485
+ const bindings = getBindings(code, fnNames);
486
+ const wholeFile = generateWholeFileStubs(
487
+ STATIC_CONFIG,
488
+ bindings,
489
+ code,
490
+ filePath,
491
+ isBuild,
492
+ );
493
+ if (wholeFile) return wholeFile;
494
+ }
534
495
 
535
- if (
536
- canStubWholeFile &&
537
- (handleFnNames.length > 0 || lsFnNames.length > 0)
538
- ) {
539
- const exportedLocals = new Set(allBindings.map((b) => b.localName));
540
- // Collect bindings that would be stripped by whole-file replacement:
541
- // local declarations and imported bindings from non-@rangojs/router
542
- // modules. This is a regex-based heuristic it intentionally skips
543
- // edge cases (class decls, destructured bindings, combined
544
- // default+named imports) since those rarely appear in route files.
545
- const strippedBindings: string[] = [];
546
-
547
- // Skip React Fast Refresh temporaries (_c, _c2, ...) which are
548
- // injected by @vitejs/plugin-react in the client environment and
549
- // would falsely trigger the bailout.
550
- const localDeclPattern =
551
- /(?:^|;|\n)\s*(?:const|let|var|function)\s+(\w+)/g;
552
- let declMatch: RegExpExecArray | null;
553
- while ((declMatch = localDeclPattern.exec(code)) !== null) {
554
- const name = declMatch[1];
555
- if (!exportedLocals.has(name) && !/^_c\d*$/.test(name)) {
556
- strippedBindings.push(name);
496
+ // --- Mixed-type whole-file stub replacement (non-RSC) ---
497
+ // When the individual whole-file checks above fail (each only checks
498
+ // one type), the file has mixed exports (e.g. createLoader + Prerender).
499
+ // Gather ALL stub-safe bindings and check if they cover every export.
500
+ // If yes, replace the entire file with stubs — this strips server-only
501
+ // imports (node:fs, DB clients, etc.) that would crash in the browser.
502
+ //
503
+ // Only applies when the file contains Prerender/Static (the handler
504
+ // types that bring server-only code). Files with only loaders, handles,
505
+ // or locationState are handled correctly by the unified pipeline below.
506
+ //
507
+ // Loader, Prerender, and Static exports become plain { __brand, $$id }
508
+ // stubs. createHandle and createLocationState need their create*()
509
+ // functions to execute (collect registration / __rsc_ls_key), so their
510
+ // call expressions are preserved with only a @rangojs/router import.
511
+ // This strips all server-only imports while keeping the correct
512
+ // client contract for every export type.
513
+ if (!isRscEnv && (hasPrerenderHandlerCode || hasStaticHandlerCode)) {
514
+ const prerenderFnNames = hasPrerenderHandlerCode
515
+ ? getFnNames(PRERENDER_CONFIG.fnName)
516
+ : [];
517
+ const staticFnNames = hasStaticHandlerCode
518
+ ? getFnNames(STATIC_CONFIG.fnName)
519
+ : [];
520
+ const loaderFnNames = hasLoaderCode ? getFnNames("createLoader") : [];
521
+ const handleFnNames = hasHandleCode ? getFnNames("createHandle") : [];
522
+ const lsFnNames = hasLocationStateCode
523
+ ? getFnNames("createLocationState")
524
+ : [];
525
+
526
+ // Collect ALL recognized bindings to check export coverage
527
+ const allBindings: CreateExportBinding[] = [];
528
+ for (const fnNames of [
529
+ prerenderFnNames,
530
+ staticFnNames,
531
+ loaderFnNames,
532
+ handleFnNames,
533
+ lsFnNames,
534
+ ]) {
535
+ if (fnNames.length > 0) {
536
+ allBindings.push(...getBindings(code, fnNames));
557
537
  }
558
538
  }
559
539
 
560
- const importPattern =
561
- /import\s*\{([^}]*)\}\s*from\s*["'](?!@rangojs\/router)[^"']*["']/g;
562
- let importMatch: RegExpExecArray | null;
563
- while ((importMatch = importPattern.exec(code)) !== null) {
564
- for (const spec of importMatch[1].split(",")) {
565
- const m = spec
566
- .trim()
567
- .match(/^[A-Za-z_$][\w$]*(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
568
- if (m) strippedBindings.push(m[1] || m[0].trim().split(/\s/)[0]);
540
+ // Check if preserved createHandle/createLocationState calls
541
+ // reference non-exported locals (e.g. helper functions, constants).
542
+ // If so, the whole-file stub would strip those locals, breaking
543
+ // the call. Fall through to the unified pipeline instead.
544
+ let canStubWholeFile =
545
+ allBindings.length > 0 && isExportOnlyFile(code, allBindings);
546
+
547
+ if (
548
+ canStubWholeFile &&
549
+ (handleFnNames.length > 0 || lsFnNames.length > 0)
550
+ ) {
551
+ const exportedLocals = new Set(allBindings.map((b) => b.localName));
552
+ // Collect bindings that would be stripped by whole-file replacement:
553
+ // local declarations and imported bindings from non-@rangojs/router
554
+ // modules. This is a regex-based heuristic — it intentionally skips
555
+ // edge cases (class decls, destructured bindings, combined
556
+ // default+named imports) since those rarely appear in route files.
557
+ const strippedBindings: string[] = [];
558
+
559
+ // Skip React Fast Refresh temporaries (_c, _c2, ...) which are
560
+ // injected by @vitejs/plugin-react in the client environment and
561
+ // would falsely trigger the bailout.
562
+ const localDeclPattern =
563
+ /(?:^|;|\n)\s*(?:const|let|var|function)\s+(\w+)/g;
564
+ let declMatch: RegExpExecArray | null;
565
+ while ((declMatch = localDeclPattern.exec(code)) !== null) {
566
+ const name = declMatch[1];
567
+ if (!exportedLocals.has(name) && !/^_c\d*$/.test(name)) {
568
+ strippedBindings.push(name);
569
+ }
569
570
  }
570
- }
571
- const defaultImportPattern =
572
- /import\s+([A-Za-z_$][\w$]*)\s+from\s*["'](?!@rangojs\/router)[^"']*["']/g;
573
- while ((importMatch = defaultImportPattern.exec(code)) !== null) {
574
- strippedBindings.push(importMatch[1]);
575
- }
576
- const nsImportPattern =
577
- /import\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*["'](?!@rangojs\/router)[^"']*["']/g;
578
- while ((importMatch = nsImportPattern.exec(code)) !== null) {
579
- strippedBindings.push(importMatch[1]);
580
- }
581
571
 
582
- if (strippedBindings.length > 0) {
583
- const preservedBindings = allBindings.filter((b) => {
584
- const fc = code.slice(b.callExprStart, b.callOpenParenPos + 1);
585
- return (
586
- handleFnNames.some((n) => fc.includes(n)) ||
587
- lsFnNames.some((n) => fc.includes(n))
572
+ const importPattern =
573
+ /import\s*\{([^}]*)\}\s*from\s*["'](?!@rangojs\/router)[^"']*["']/g;
574
+ let importMatch: RegExpExecArray | null;
575
+ while ((importMatch = importPattern.exec(code)) !== null) {
576
+ for (const spec of importMatch[1].split(",")) {
577
+ const m = spec
578
+ .trim()
579
+ .match(/^[A-Za-z_$][\w$]*(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
580
+ if (m)
581
+ strippedBindings.push(m[1] || m[0].trim().split(/\s/)[0]);
582
+ }
583
+ }
584
+ const defaultImportPattern =
585
+ /import\s+([A-Za-z_$][\w$]*)\s+from\s*["'](?!@rangojs\/router)[^"']*["']/g;
586
+ while ((importMatch = defaultImportPattern.exec(code)) !== null) {
587
+ strippedBindings.push(importMatch[1]);
588
+ }
589
+ const nsImportPattern =
590
+ /import\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*["'](?!@rangojs\/router)[^"']*["']/g;
591
+ while ((importMatch = nsImportPattern.exec(code)) !== null) {
592
+ strippedBindings.push(importMatch[1]);
593
+ }
594
+
595
+ if (strippedBindings.length > 0) {
596
+ const preservedBindings = allBindings.filter((b) => {
597
+ const fc = code.slice(b.callExprStart, b.callOpenParenPos + 1);
598
+ return (
599
+ handleFnNames.some((n) => fc.includes(n)) ||
600
+ lsFnNames.some((n) => fc.includes(n))
601
+ );
602
+ });
603
+ const strippedRe = new RegExp(
604
+ `\\b(?:${strippedBindings.join("|")})\\b`,
588
605
  );
589
- });
590
- const strippedRe = new RegExp(
591
- `\\b(?:${strippedBindings.join("|")})\\b`,
592
- );
593
- canStubWholeFile = !preservedBindings.some((b) => {
594
- const expr = code.slice(b.callExprStart, b.callCloseParenPos + 1);
595
- return strippedRe.test(expr);
596
- });
606
+ canStubWholeFile = !preservedBindings.some((b) => {
607
+ const expr = code.slice(
608
+ b.callExprStart,
609
+ b.callCloseParenPos + 1,
610
+ );
611
+ return strippedRe.test(expr);
612
+ });
613
+ }
597
614
  }
598
- }
599
615
 
600
- if (canStubWholeFile) {
601
- const lines: string[] = [];
602
- const neededImports: string[] = [];
603
- if (handleFnNames.length > 0) neededImports.push("createHandle");
604
- if (lsFnNames.length > 0) neededImports.push("createLocationState");
605
- if (neededImports.length > 0) {
606
- lines.push(
607
- `import { ${neededImports.join(", ")} } from "@rangojs/router";`,
608
- );
609
- }
616
+ if (canStubWholeFile) {
617
+ const lines: string[] = [];
618
+ const neededImports: string[] = [];
619
+ if (handleFnNames.length > 0) neededImports.push("createHandle");
620
+ if (lsFnNames.length > 0) neededImports.push("createLocationState");
621
+ if (neededImports.length > 0) {
622
+ lines.push(
623
+ `import { ${neededImports.join(", ")} } from "@rangojs/router";`,
624
+ );
625
+ }
610
626
 
611
- for (const binding of allBindings) {
612
- const fnCall = code.slice(
613
- binding.callExprStart,
614
- binding.callOpenParenPos + 1,
615
- );
616
- const isHandle = handleFnNames.some((n) => fnCall.includes(n));
617
- const isLocationState = lsFnNames.some((n) => fnCall.includes(n));
618
-
619
- // Aliases share the primary name's ID (matches server transforms).
620
- const primaryName = binding.exportNames[0];
621
- const stubId = makeStubId(filePath, primaryName, isBuild);
622
-
623
- if (isHandle || isLocationState) {
624
- // Rewrite alias to canonical name since the stub file only
625
- // imports canonical names from @rangojs/router.
626
- // Strip React Fast Refresh `_c = ` wrappers from args
627
- // (e.g. `_c = (segments) => ...` → `(segments) => ...`)
628
- const rawArgs = code
629
- .slice(binding.callOpenParenPos + 1, binding.callCloseParenPos)
630
- .replace(/\b_c\d*\s*=\s*/g, "");
631
- const canonicalName = isHandle
632
- ? "createHandle"
633
- : "createLocationState";
634
- const activeFnNames = isHandle ? handleFnNames : lsFnNames;
635
-
636
- // Reconstruct the function name (handling aliases + generics)
637
- let rawCallee = code.slice(
627
+ for (const binding of allBindings) {
628
+ const fnCall = code.slice(
638
629
  binding.callExprStart,
639
- binding.callOpenParenPos,
630
+ binding.callOpenParenPos + 1,
640
631
  );
641
- for (const alias of activeFnNames) {
642
- if (alias !== canonicalName && rawCallee.startsWith(alias)) {
643
- rawCallee = canonicalName + rawCallee.slice(alias.length);
644
- break;
632
+ const isHandle = handleFnNames.some((n) => fnCall.includes(n));
633
+ const isLocationState = lsFnNames.some((n) => fnCall.includes(n));
634
+
635
+ // Aliases share the primary name's ID (matches server transforms).
636
+ const primaryName = binding.exportNames[0];
637
+ const stubId = makeStubId(filePath, primaryName, isBuild);
638
+
639
+ if (isHandle || isLocationState) {
640
+ // Rewrite alias to canonical name since the stub file only
641
+ // imports canonical names from @rangojs/router.
642
+ // Strip React Fast Refresh `_c = ` wrappers from args
643
+ // (e.g. `_c = (segments) => ...` → `(segments) => ...`)
644
+ const rawArgs = code
645
+ .slice(
646
+ binding.callOpenParenPos + 1,
647
+ binding.callCloseParenPos,
648
+ )
649
+ .replace(/\b_c\d*\s*=\s*/g, "");
650
+ const canonicalName = isHandle
651
+ ? "createHandle"
652
+ : "createLocationState";
653
+ const activeFnNames = isHandle ? handleFnNames : lsFnNames;
654
+
655
+ // Reconstruct the function name (handling aliases + generics)
656
+ let rawCallee = code.slice(
657
+ binding.callExprStart,
658
+ binding.callOpenParenPos,
659
+ );
660
+ for (const alias of activeFnNames) {
661
+ if (alias !== canonicalName && rawCallee.startsWith(alias)) {
662
+ rawCallee = canonicalName + rawCallee.slice(alias.length);
663
+ break;
664
+ }
645
665
  }
646
- }
647
666
 
648
- if (isHandle) {
649
- // createHandle checks __injectedId DURING the call, so $$id
650
- // must be a parameter, not a post-call property assignment.
651
- const idParam =
652
- binding.argCount === 0
653
- ? `undefined, "${stubId}"`
654
- : `, "${stubId}"`;
655
- lines.push(
656
- `export const ${primaryName} = ${rawCallee}(${rawArgs}${idParam});`,
657
- );
658
- lines.push(`${primaryName}.$$id = "${stubId}";`);
667
+ if (isHandle) {
668
+ // createHandle checks __injectedId DURING the call, so $$id
669
+ // must be a parameter, not a post-call property assignment.
670
+ const idParam =
671
+ binding.argCount === 0
672
+ ? `undefined, "${stubId}"`
673
+ : `, "${stubId}"`;
674
+ lines.push(
675
+ `export const ${primaryName} = ${rawCallee}(${rawArgs}${idParam});`,
676
+ );
677
+ lines.push(`${primaryName}.$$id = "${stubId}";`);
678
+ } else {
679
+ lines.push(
680
+ `export const ${primaryName} = ${rawCallee}(${rawArgs});`,
681
+ );
682
+ lines.push(
683
+ `${primaryName}.__rsc_ls_key = "__rsc_ls_${stubId}";`,
684
+ );
685
+ }
686
+ for (const name of binding.exportNames.slice(1)) {
687
+ lines.push(`export const ${name} = ${primaryName};`);
688
+ }
659
689
  } else {
690
+ let brand = "loader";
691
+ if (prerenderFnNames.some((n) => fnCall.includes(n))) {
692
+ brand = PRERENDER_CONFIG.brand;
693
+ } else if (staticFnNames.some((n) => fnCall.includes(n))) {
694
+ brand = STATIC_CONFIG.brand;
695
+ }
660
696
  lines.push(
661
- `export const ${primaryName} = ${rawCallee}(${rawArgs});`,
662
- );
663
- lines.push(
664
- `${primaryName}.__rsc_ls_key = "__rsc_ls_${stubId}";`,
697
+ `export const ${primaryName} = { __brand: "${brand}", $$id: "${stubId}" };`,
665
698
  );
666
- }
667
- for (const name of binding.exportNames.slice(1)) {
668
- lines.push(`export const ${name} = ${primaryName};`);
669
- }
670
- } else {
671
- let brand = "loader";
672
- if (prerenderFnNames.some((n) => fnCall.includes(n))) {
673
- brand = PRERENDER_CONFIG.brand;
674
- } else if (staticFnNames.some((n) => fnCall.includes(n))) {
675
- brand = STATIC_CONFIG.brand;
676
- }
677
- lines.push(
678
- `export const ${primaryName} = { __brand: "${brand}", $$id: "${stubId}" };`,
679
- );
680
- for (const name of binding.exportNames.slice(1)) {
681
- lines.push(`export const ${name} = ${primaryName};`);
699
+ for (const name of binding.exportNames.slice(1)) {
700
+ lines.push(`export const ${name} = ${primaryName};`);
701
+ }
682
702
  }
683
703
  }
684
- }
685
704
 
686
- return { code: lines.join("\n") + "\n", map: null };
705
+ return { code: lines.join("\n") + "\n", map: null };
706
+ }
687
707
  }
688
- }
689
708
 
690
- // --- StaticHandler: RSC build module tracking ---
691
- if (hasStaticHandlerCode && isRscEnv && isBuild) {
692
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
693
- const exportNames = getBindings(code, fnNames).map(
694
- (b) => b.exportNames[0],
695
- );
696
- if (exportNames.length > 0) {
697
- staticHandlerModules.set(id, exportNames);
709
+ // RSC build module tracking (prerender + static), consumed via the
710
+ // plugin API for prerender freezing. Export-binding sets are invariant
711
+ // across the inline-extraction loop, so tracking both here is equivalent
712
+ // to the pre-extraction prerender tracking this replaces.
713
+ if (isRscEnv && isBuild) {
714
+ const trackTypes: Array<
715
+ [boolean, HandlerTransformConfig, Map<string, string[]>]
716
+ > = [
717
+ [
718
+ hasPrerenderHandlerCode,
719
+ PRERENDER_CONFIG,
720
+ prerenderHandlerModules,
721
+ ],
722
+ [hasStaticHandlerCode, STATIC_CONFIG, staticHandlerModules],
723
+ ];
724
+ for (const [has, cfg, trackMap] of trackTypes) {
725
+ if (!has) continue;
726
+ const exportNames = getBindings(code, getFnNames(cfg.fnName)).map(
727
+ (b) => b.exportNames[0],
728
+ );
729
+ if (exportNames.length > 0) trackMap.set(id, exportNames);
730
+ }
698
731
  }
699
- }
700
732
 
701
- // --- Unified MagicString transforms ---
702
- // Single pipeline for all downstream transforms (loaders, handles,
703
- // locationState, handler IDs). Uses the post-extraction code so
704
- // positions are always consistent.
705
- const s = new MagicString(code);
706
-
707
- if (hasLoaderCode) {
708
- const fnNames = getFnNames("createLoader");
709
- changed =
710
- transformLoaders(getBindings(code, fnNames), s, filePath, isBuild) ||
711
- changed;
712
- }
713
- if (hasHandleCode) {
714
- const fnNames = getFnNames("createHandle");
715
- changed =
716
- transformHandles(
717
- getBindings(code, fnNames),
718
- s,
719
- code,
720
- filePath,
721
- isBuild,
722
- ) || changed;
723
- }
724
- if (hasLocationStateCode) {
725
- const fnNames = getFnNames("createLocationState");
726
- changed =
727
- transformLocationState(
728
- getBindings(code, fnNames),
729
- s,
730
- filePath,
731
- isBuild,
732
- ) || changed;
733
- }
734
- if (hasPrerenderHandlerCode) {
735
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
736
- const bindings = getBindings(code, fnNames);
737
- if (isRscEnv) {
733
+ // --- Unified MagicString transforms ---
734
+ // Single pipeline for all downstream transforms (loaders, handles,
735
+ // locationState, handler IDs). Uses the post-extraction code so
736
+ // positions are always consistent.
737
+ const s = new MagicString(code);
738
+
739
+ if (hasLoaderCode) {
740
+ const fnNames = getFnNames("createLoader");
738
741
  changed =
739
- transformHandlerIds(
740
- PRERENDER_CONFIG,
741
- bindings,
742
+ transformLoaders(
743
+ getBindings(code, fnNames),
742
744
  s,
743
745
  filePath,
744
746
  isBuild,
745
747
  ) || changed;
746
- } else {
747
- // Non-RSC mixed-export file: replace Prerender() calls with stubs
748
- // on the shared MagicString so sourcemaps stay accurate.
748
+ }
749
+ if (hasHandleCode) {
750
+ const fnNames = getFnNames("createHandle");
749
751
  changed =
750
- stubHandlerExprs(
751
- PRERENDER_CONFIG,
752
- bindings,
752
+ transformHandles(
753
+ getBindings(code, fnNames),
753
754
  s,
755
+ code,
754
756
  filePath,
755
757
  isBuild,
756
758
  ) || changed;
757
759
  }
758
- }
759
- if (hasStaticHandlerCode) {
760
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
761
- const bindings = getBindings(code, fnNames);
762
- if (isRscEnv) {
760
+ if (hasLocationStateCode) {
761
+ const fnNames = getFnNames("createLocationState");
763
762
  changed =
764
- transformHandlerIds(
765
- STATIC_CONFIG,
766
- bindings,
763
+ transformLocationState(
764
+ getBindings(code, fnNames),
767
765
  s,
768
766
  filePath,
769
767
  isBuild,
770
768
  ) || changed;
771
- } else {
769
+ }
770
+ // Prerender + Static share the RSC inject-id vs non-RSC stub dispatch.
771
+ // Call sites are disjoint (distinct fnNames), so loop order is irrelevant.
772
+ const finalHandlerConfigs = [
773
+ hasPrerenderHandlerCode && PRERENDER_CONFIG,
774
+ hasStaticHandlerCode && STATIC_CONFIG,
775
+ ].filter((c): c is HandlerTransformConfig => !!c);
776
+ for (const cfg of finalHandlerConfigs) {
777
+ const bindings = getBindings(code, getFnNames(cfg.fnName));
772
778
  changed =
773
- stubHandlerExprs(STATIC_CONFIG, bindings, s, filePath, isBuild) ||
779
+ (isRscEnv
780
+ ? transformHandlerIds(cfg, bindings, s, filePath, isBuild)
781
+ : stubHandlerExprs(cfg, bindings, s, filePath, isBuild)) ||
774
782
  changed;
775
783
  }
776
- }
777
784
 
778
- if (!changed) return;
785
+ if (!changed) return;
779
786
 
780
- return {
781
- code: s.toString(),
782
- map: s.generateMap({ source: id, includeContent: true }),
783
- };
787
+ return {
788
+ code: s.toString(),
789
+ map: s.generateMap({ source: id, includeContent: true }),
790
+ };
791
+ } finally {
792
+ counter?.record(id, performance.now() - __t0);
793
+ }
784
794
  },
785
795
  };
786
796
  }