@rangojs/router 0.0.0-experimental.66 → 0.0.0-experimental.66cdebe3

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 +1462 -422
  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 +151 -9
  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 +95 -44
  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 +69 -28
  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 +44 -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 +545 -304
  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,
@@ -23,6 +28,7 @@ import {
23
28
  getImportedFnNames,
24
29
  collectCreateExportBindings,
25
30
  buildUnsupportedShapeWarning,
31
+ isExportOnlyFile,
26
32
  } from "./expose-ids/export-analysis.js";
27
33
  import {
28
34
  hasCreateLoaderImport,
@@ -37,6 +43,9 @@ import {
37
43
  stubHandlerExprs,
38
44
  transformHandlerIds,
39
45
  } from "./expose-ids/handler-transform.js";
46
+ import { createRangoDebugger, createCounter, NS } from "../debug.js";
47
+
48
+ const debug = createRangoDebugger(NS.transform);
40
49
 
41
50
  // Re-exports consumed by other packages/plugins
42
51
  export { exposeRouterId } from "./expose-ids/router-transform.js";
@@ -81,10 +90,16 @@ export function exposeInternalIds(options?: { forceBuild?: boolean }): Plugin {
81
90
  // De-duplicate unsupported shape warnings across repeated transforms.
82
91
  const unsupportedShapeWarnings = new Set<string>();
83
92
 
93
+ const counter = createCounter(debug, "expose-internal-ids");
94
+
84
95
  return {
85
96
  name: "@rangojs/router:expose-internal-ids",
86
97
  enforce: "post",
87
98
 
99
+ buildEnd() {
100
+ counter?.flush();
101
+ },
102
+
88
103
  api: {
89
104
  prerenderHandlerModules,
90
105
  staticHandlerModules,
@@ -227,349 +242,575 @@ ${lazyImports.join(",\n")}
227
242
  transform(code, id) {
228
243
  if (id.includes("/node_modules/")) return;
229
244
 
230
- const filePath = normalizePath(path.relative(projectRoot, id));
231
- const isRscEnv = this.environment?.name === "rsc";
232
-
233
- // Warn if named-routes.gen is imported in a client component.
234
- // NamedRoutes is server-only data and would bloat the client bundle.
235
- if (
236
- id.includes(".named-routes.gen.") &&
237
- !isRscEnv &&
238
- this.environment?.name === "client"
239
- ) {
240
- this.warn(
241
- `\n` +
242
- `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n` +
243
- `!! !!\n` +
244
- `!! WARNING: NamedRoutes imported in a CLIENT component! !!\n` +
245
- `!! !!\n` +
246
- `!! File: ${filePath.padEnd(53)}!!\n` +
247
- `!! !!\n` +
248
- `!! NamedRoutes contains your entire route structure — every !!\n` +
249
- `!! route name and URL pattern in your application. Shipping !!\n` +
250
- `!! this to the browser exposes your full routing topology to !!\n` +
251
- `!! the client, which is a security concern (internal/admin !!\n` +
252
- `!! routes, API endpoints, hidden paths become visible). !!\n` +
253
- `!! !!\n` +
254
- `!! It also bloats the client bundle — this map contains all !!\n` +
255
- `!! named routes in your application. !!\n` +
256
- `!! !!\n` +
257
- `!! Fix: remove the import or move it to a server component. !!\n` +
258
- `!! !!\n` +
259
- `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n`,
260
- );
261
- }
262
-
263
- // Fast exit: if the file doesn't import from @rangojs/router at all,
264
- // skip all create* analysis and transforms.
265
- if (!code.includes("@rangojs/router")) return;
266
-
267
- // Detect all relevant imports in one pass
268
- const has = detectImports(code);
269
-
270
- // Quick bail-out: also check for raw create* identifiers.
271
- // This is safe even with aliases (e.g., `import { createLoader as cl }`)
272
- // because the import statement itself always contains the canonical name
273
- // "createLoader", so code.includes("createLoader") will still match.
274
- const hasLoaderCode = has.loader && code.includes("createLoader");
275
- const hasHandleCode = has.handle && code.includes("createHandle");
276
- const hasLocationStateCode =
277
- has.locationState && code.includes("createLocationState");
278
- const hasPrerenderHandlerCode =
279
- has.prerenderHandler && code.includes("Prerender");
280
- const hasStaticHandlerCode = has.staticHandler && code.includes("Static");
281
- if (
282
- !hasLoaderCode &&
283
- !hasHandleCode &&
284
- !hasLocationStateCode &&
285
- !hasPrerenderHandlerCode &&
286
- !hasStaticHandlerCode
287
- ) {
288
- return;
289
- }
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
+ );
278
+ }
290
279
 
291
- // Per-invocation caches to avoid redundant AST parsing.
292
- // getImportedFnNames is cached by canonical name (imports never change).
293
- // collectCreateExportBindings is cached by fnNames key; the cache is
294
- // cleared when `code` changes (e.g., after inline handler extraction).
295
- const _fnNamesCache = new Map<string, string[]>();
296
- const _bindingsCache = new Map<string, CreateExportBinding[]>();
297
- let _cachedAst: any;
298
- let _astParseFailed = false;
299
- let _astCodeRef = code;
300
-
301
- const getFnNames = (canonicalName: string): string[] => {
302
- let result = _fnNamesCache.get(canonicalName);
303
- if (!result) {
304
- result = getImportedFnNames(code, canonicalName);
305
- _fnNamesCache.set(canonicalName, result);
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;
306
307
  }
307
- return result;
308
- };
309
-
310
- // Lazy AST parse: parsed once and shared across all
311
- // collectCreateExportBindings calls for the same code string.
312
- const lazyAst = (): any | undefined => {
313
- if (code !== _astCodeRef) {
314
- _cachedAst = undefined;
315
- _astParseFailed = false;
316
- _astCodeRef = code;
308
+
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;
372
+
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));
317
382
  }
318
- if (_cachedAst !== undefined || _astParseFailed) return _cachedAst;
319
- try {
320
- _cachedAst = parseAst(code, { jsx: true });
321
- } catch {
322
- _astParseFailed = true;
383
+
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
+ }
323
396
  }
324
- return _cachedAst;
325
- };
326
-
327
- const getBindings = (
328
- currentCode: string,
329
- fnNames: string[],
330
- ): CreateExportBinding[] => {
331
- const key = fnNames.join("\0");
332
- let result = _bindingsCache.get(key);
333
- if (!result) {
334
- result = collectCreateExportBindings(currentCode, fnNames, lazyAst());
335
- _bindingsCache.set(key, result);
397
+
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,
404
+ code,
405
+ filePath,
406
+ isBuild,
407
+ );
408
+ if (stubResult) return stubResult;
336
409
  }
337
- return result;
338
- };
339
-
340
- // Warn on create* declaration shapes that are currently unsupported by
341
- // non-AST transforms (loader/handle/locationState only).
342
- for (const cfg of STRICT_CREATE_CONFIGS) {
343
- const hasCode =
344
- cfg.fnName === "createLoader"
345
- ? hasLoaderCode
346
- : cfg.fnName === "createHandle"
347
- ? hasHandleCode
348
- : hasLocationStateCode;
349
- if (!hasCode) continue;
350
-
351
- const fnNames = getFnNames(cfg.fnName);
352
- const totalCalls = countCreateCallsForNames(code, fnNames);
353
- const supportedBindings = getBindings(code, fnNames).length;
354
- if (totalCalls <= supportedBindings) continue;
355
-
356
- const warnKey = `${id}::${cfg.fnName}`;
357
- if (unsupportedShapeWarnings.has(warnKey)) continue;
358
- unsupportedShapeWarnings.add(warnKey);
359
- this.warn(buildUnsupportedShapeWarning(filePath, cfg.fnName));
360
- }
361
410
 
362
- // --- Loader: track for manifest (RSC env only) ---
363
- if (hasLoaderCode && isRscEnv) {
364
- const fnNames = getFnNames("createLoader");
365
- const bindings = getBindings(code, fnNames);
366
- for (const binding of bindings) {
367
- const exportName = binding.exportNames[0];
368
- const hashedId = hashId(filePath, exportName);
369
- loaderRegistry.set(hashedId, {
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,
370
421
  filePath,
371
- exportName,
372
- });
422
+ isBuild,
423
+ );
424
+ if (wholeFile) return wholeFile;
373
425
  }
374
- }
375
426
 
376
- // --- Loader: client stubs for non-RSC environments ---
377
- if (hasLoaderCode && !isRscEnv) {
378
- const fnNames = getFnNames("createLoader");
379
- const bindings = getBindings(code, fnNames);
380
- const stubResult = generateClientLoaderStubs(
381
- bindings,
382
- code,
383
- filePath,
384
- isBuild,
385
- );
386
- if (stubResult) return stubResult;
387
- }
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],
432
+ );
433
+ if (exportNames.length > 0) {
434
+ prerenderHandlerModules.set(id, exportNames);
435
+ }
436
+ }
388
437
 
389
- // --- PrerenderHandler: non-RSC whole-file stub replacement ---
390
- // When ALL exports are Prerender() calls, replace the entire file.
391
- // Mixed-export files are handled in the unified pipeline below.
392
- if (hasPrerenderHandlerCode && !isRscEnv) {
393
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
394
- const bindings = getBindings(code, fnNames);
395
- const wholeFile = generateWholeFileStubs(
396
- PRERENDER_CONFIG,
397
- bindings,
398
- code,
399
- filePath,
400
- isBuild,
401
- );
402
- if (wholeFile) return wholeFile;
403
- }
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
+ });
404
460
 
405
- // --- PrerenderHandler: RSC build module tracking ---
406
- if (hasPrerenderHandlerCode && isRscEnv && isBuild) {
407
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
408
- const exportNames = getBindings(code, fnNames).map(
409
- (b) => b.exportNames[0],
410
- );
411
- if (exportNames.length > 0) {
412
- prerenderHandlerModules.set(id, exportNames);
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,
476
+ );
477
+ if (result) {
478
+ changed = true;
479
+ code = iterS.toString();
480
+ _bindingsCache.clear();
481
+ }
482
+ }
413
483
  }
414
- }
415
484
 
416
- // --- Inline handler extraction to virtual modules ---
417
- // Runs before stubs/tracking so inline calls become imports, then
418
- // the existing regex fast path handles both the original file's
419
- // export const patterns and the virtual modules independently.
420
- //
421
- // Cheap pre-check: count total fnName( occurrences vs export const
422
- // patterns. If they match, every call is a named export and the
423
- // regex fast path handles them -- skip the AST parse entirely.
424
- //
425
- // Each iteration creates a fresh MagicString so that AST positions
426
- // from findHandlerCalls always match the string they were parsed from.
427
- let changed = false;
428
-
429
- const handlerConfigs = [
430
- hasStaticHandlerCode && STATIC_CONFIG,
431
- hasPrerenderHandlerCode && PRERENDER_CONFIG,
432
- ]
433
- .filter((c): c is HandlerTransformConfig => !!c)
434
- .map((cfg) => {
435
- const fnNames = getFnNames(cfg.fnName);
436
- return { cfg, fnNames };
437
- });
438
-
439
- for (const { cfg, fnNames } of handlerConfigs) {
440
- const totalCalls = countCreateCallsForNames(code, fnNames);
441
- const supportedBindings = getBindings(code, fnNames).length;
442
-
443
- if (totalCalls > supportedBindings) {
444
- const iterS = new MagicString(code);
445
- const result = transformInlineHandlers(
446
- cfg.fnName,
447
- VIRTUAL_HANDLER_PREFIX,
448
- iterS,
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,
449
493
  code,
450
494
  filePath,
451
- virtualHandlers,
452
- id,
453
- parseAst,
495
+ isBuild,
454
496
  );
455
- if (result) {
456
- changed = true;
457
- code = iterS.toString();
458
- _bindingsCache.clear();
459
- }
497
+ if (wholeFile) return wholeFile;
460
498
  }
461
- }
462
499
 
463
- // --- StaticHandler: non-RSC whole-file stub replacement ---
464
- // When ALL exports are Static() calls, replace the entire file.
465
- // Mixed-export files are handled in the unified pipeline below.
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
- }
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
+ }
478
543
 
479
- // --- StaticHandler: RSC build module tracking ---
480
- if (hasStaticHandlerCode && isRscEnv && isBuild) {
481
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
482
- const exportNames = getBindings(code, fnNames).map(
483
- (b) => b.exportNames[0],
484
- );
485
- if (exportNames.length > 0) {
486
- staticHandlerModules.set(id, exportNames);
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
+ }
487
711
  }
488
- }
489
712
 
490
- // --- Unified MagicString transforms ---
491
- // Single pipeline for all downstream transforms (loaders, handles,
492
- // locationState, handler IDs). Uses the post-extraction code so
493
- // positions are always consistent.
494
- const s = new MagicString(code);
495
-
496
- if (hasLoaderCode) {
497
- const fnNames = getFnNames("createLoader");
498
- changed =
499
- transformLoaders(getBindings(code, fnNames), s, filePath, isBuild) ||
500
- changed;
501
- }
502
- if (hasHandleCode) {
503
- const fnNames = getFnNames("createHandle");
504
- changed =
505
- transformHandles(
506
- getBindings(code, fnNames),
507
- s,
508
- code,
509
- filePath,
510
- isBuild,
511
- ) || changed;
512
- }
513
- if (hasLocationStateCode) {
514
- const fnNames = getFnNames("createLocationState");
515
- changed =
516
- transformLocationState(
517
- getBindings(code, fnNames),
518
- s,
519
- filePath,
520
- isBuild,
521
- ) || changed;
522
- }
523
- if (hasPrerenderHandlerCode) {
524
- const fnNames = getFnNames(PRERENDER_CONFIG.fnName);
525
- const bindings = getBindings(code, fnNames);
526
- if (isRscEnv) {
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");
527
732
  changed =
528
- transformHandlerIds(
529
- PRERENDER_CONFIG,
530
- bindings,
733
+ transformLoaders(
734
+ getBindings(code, fnNames),
531
735
  s,
532
736
  filePath,
533
737
  isBuild,
534
738
  ) || changed;
535
- } else {
536
- // Non-RSC mixed-export file: replace Prerender() calls with stubs
537
- // on the shared MagicString so sourcemaps stay accurate.
739
+ }
740
+ if (hasHandleCode) {
741
+ const fnNames = getFnNames("createHandle");
538
742
  changed =
539
- stubHandlerExprs(
540
- PRERENDER_CONFIG,
541
- bindings,
743
+ transformHandles(
744
+ getBindings(code, fnNames),
542
745
  s,
746
+ code,
543
747
  filePath,
544
748
  isBuild,
545
749
  ) || changed;
546
750
  }
547
- }
548
- if (hasStaticHandlerCode) {
549
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
550
- const bindings = getBindings(code, fnNames);
551
- if (isRscEnv) {
751
+ if (hasLocationStateCode) {
752
+ const fnNames = getFnNames("createLocationState");
552
753
  changed =
553
- transformHandlerIds(
554
- STATIC_CONFIG,
555
- bindings,
754
+ transformLocationState(
755
+ getBindings(code, fnNames),
556
756
  s,
557
757
  filePath,
558
758
  isBuild,
559
759
  ) || changed;
560
- } else {
561
- changed =
562
- stubHandlerExprs(STATIC_CONFIG, bindings, s, filePath, isBuild) ||
563
- changed;
564
760
  }
565
- }
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
+ }
566
804
 
567
- if (!changed) return;
805
+ if (!changed) return;
568
806
 
569
- return {
570
- code: s.toString(),
571
- map: s.generateMap({ source: id, includeContent: true }),
572
- };
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
+ }
573
814
  },
574
815
  };
575
816
  }