@rangojs/router 0.0.0-experimental.68 → 0.0.0-experimental.6c70a2ab

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