@rangojs/router 0.0.0-experimental.b02a2fec → 0.0.0-experimental.b30bbf02

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 (112) hide show
  1. package/README.md +112 -17
  2. package/dist/vite/index.js +1338 -462
  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 +362 -0
  7. package/skills/hooks/SKILL.md +33 -20
  8. package/skills/intercept/SKILL.md +20 -0
  9. package/skills/layout/SKILL.md +22 -0
  10. package/skills/links/SKILL.md +90 -16
  11. package/skills/loader/SKILL.md +70 -3
  12. package/skills/middleware/SKILL.md +34 -3
  13. package/skills/migrate-nextjs/SKILL.md +562 -0
  14. package/skills/migrate-react-router/SKILL.md +769 -0
  15. package/skills/parallel/SKILL.md +66 -0
  16. package/skills/rango/SKILL.md +25 -22
  17. package/skills/response-routes/SKILL.md +8 -0
  18. package/skills/route/SKILL.md +24 -0
  19. package/skills/server-actions/SKILL.md +739 -0
  20. package/skills/streams-and-websockets/SKILL.md +283 -0
  21. package/skills/typesafety/SKILL.md +3 -1
  22. package/src/browser/app-shell.ts +52 -0
  23. package/src/browser/event-controller.ts +44 -4
  24. package/src/browser/navigation-bridge.ts +71 -5
  25. package/src/browser/navigation-client.ts +64 -13
  26. package/src/browser/navigation-store.ts +25 -1
  27. package/src/browser/partial-update.ts +34 -3
  28. package/src/browser/prefetch/cache.ts +129 -21
  29. package/src/browser/prefetch/fetch.ts +148 -16
  30. package/src/browser/prefetch/queue.ts +36 -5
  31. package/src/browser/rango-state.ts +53 -13
  32. package/src/browser/react/Link.tsx +30 -2
  33. package/src/browser/react/NavigationProvider.tsx +70 -18
  34. package/src/browser/react/filter-segment-order.ts +51 -7
  35. package/src/browser/react/use-navigation.ts +22 -2
  36. package/src/browser/react/use-params.ts +11 -1
  37. package/src/browser/react/use-router.ts +8 -1
  38. package/src/browser/react/use-segments.ts +11 -8
  39. package/src/browser/rsc-router.tsx +34 -6
  40. package/src/browser/segment-reconciler.ts +36 -14
  41. package/src/browser/types.ts +19 -0
  42. package/src/build/route-trie.ts +50 -24
  43. package/src/cache/cf/cf-cache-store.ts +5 -7
  44. package/src/client.tsx +82 -174
  45. package/src/index.rsc.ts +3 -0
  46. package/src/index.ts +40 -9
  47. package/src/outlet-context.ts +1 -1
  48. package/src/response-utils.ts +28 -0
  49. package/src/reverse.ts +7 -3
  50. package/src/route-definition/dsl-helpers.ts +175 -23
  51. package/src/route-definition/helpers-types.ts +63 -14
  52. package/src/route-definition/resolve-handler-use.ts +6 -0
  53. package/src/route-types.ts +7 -0
  54. package/src/router/handler-context.ts +24 -4
  55. package/src/router/lazy-includes.ts +6 -6
  56. package/src/router/loader-resolution.ts +3 -0
  57. package/src/router/manifest.ts +22 -13
  58. package/src/router/match-api.ts +4 -3
  59. package/src/router/match-handlers.ts +1 -0
  60. package/src/router/match-result.ts +21 -2
  61. package/src/router/middleware-types.ts +2 -22
  62. package/src/router/middleware.ts +54 -7
  63. package/src/router/pattern-matching.ts +87 -17
  64. package/src/router/revalidation.ts +15 -1
  65. package/src/router/segment-resolution/fresh.ts +8 -0
  66. package/src/router/segment-resolution/revalidation.ts +128 -100
  67. package/src/router/trie-matching.ts +18 -13
  68. package/src/router/url-params.ts +49 -0
  69. package/src/router.ts +1 -2
  70. package/src/rsc/handler.ts +8 -4
  71. package/src/rsc/helpers.ts +69 -41
  72. package/src/rsc/progressive-enhancement.ts +4 -0
  73. package/src/rsc/response-route-handler.ts +14 -1
  74. package/src/rsc/rsc-rendering.ts +10 -0
  75. package/src/rsc/server-action.ts +4 -0
  76. package/src/rsc/types.ts +6 -0
  77. package/src/segment-content-promise.ts +67 -0
  78. package/src/segment-loader-promise.ts +122 -0
  79. package/src/segment-system.tsx +11 -61
  80. package/src/server/context.ts +26 -3
  81. package/src/server/request-context.ts +10 -42
  82. package/src/ssr/index.tsx +5 -1
  83. package/src/types/handler-context.ts +12 -39
  84. package/src/types/loader-types.ts +5 -6
  85. package/src/types/request-scope.ts +126 -0
  86. package/src/types/route-entry.ts +11 -0
  87. package/src/types/segments.ts +17 -1
  88. package/src/urls/include-helper.ts +24 -14
  89. package/src/urls/path-helper-types.ts +30 -4
  90. package/src/urls/response-types.ts +2 -10
  91. package/src/vite/debug.ts +184 -0
  92. package/src/vite/discovery/discover-routers.ts +31 -3
  93. package/src/vite/discovery/gate-state.ts +171 -0
  94. package/src/vite/discovery/prerender-collection.ts +48 -1
  95. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  96. package/src/vite/plugins/cjs-to-esm.ts +5 -0
  97. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  98. package/src/vite/plugins/client-ref-hashing.ts +16 -4
  99. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  100. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  101. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  102. package/src/vite/plugins/expose-action-id.ts +52 -28
  103. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  104. package/src/vite/plugins/expose-internal-ids.ts +516 -486
  105. package/src/vite/plugins/performance-tracks.ts +17 -9
  106. package/src/vite/plugins/use-cache-transform.ts +56 -43
  107. package/src/vite/plugins/version-injector.ts +37 -11
  108. package/src/vite/rango.ts +49 -14
  109. package/src/vite/router-discovery.ts +558 -53
  110. package/src/vite/utils/banner.ts +1 -1
  111. package/src/vite/utils/package-resolution.ts +41 -1
  112. package/src/vite/utils/prerender-utils.ts +20 -6
@@ -43,6 +43,9 @@ import {
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,575 @@ ${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, { 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;
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
+ 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
+ }
410
383
 
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);
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
+ }
419
396
  }
420
- }
421
397
 
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,
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,
455
404
  code,
456
405
  filePath,
457
- virtualHandlers,
458
- id,
459
- parseAst,
406
+ isBuild,
460
407
  );
461
- if (result) {
462
- changed = true;
463
- code = iterS.toString();
464
- _bindingsCache.clear();
465
- }
408
+ if (stubResult) return stubResult;
466
409
  }
467
- }
468
410
 
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
- }
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,
423
+ );
424
+ if (wholeFile) return wholeFile;
425
+ }
483
426
 
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));
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);
525
435
  }
526
436
  }
527
437
 
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);
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
+ });
534
460
 
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);
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();
557
481
  }
558
482
  }
483
+ }
484
+
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,
493
+ code,
494
+ filePath,
495
+ isBuild,
496
+ );
497
+ if (wholeFile) return wholeFile;
498
+ }
559
499
 
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]);
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));
569
541
  }
570
542
  }
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
543
 
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))
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`,
588
609
  );
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
- });
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
+ }
597
618
  }
598
- }
599
619
 
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
- }
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
+ }
610
630
 
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(
631
+ for (const binding of allBindings) {
632
+ const fnCall = code.slice(
638
633
  binding.callExprStart,
639
- binding.callOpenParenPos,
634
+ binding.callOpenParenPos + 1,
640
635
  );
641
- for (const alias of activeFnNames) {
642
- if (alias !== canonicalName && rawCallee.startsWith(alias)) {
643
- rawCallee = canonicalName + rawCallee.slice(alias.length);
644
- break;
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
+ }
645
669
  }
646
- }
647
670
 
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}";`);
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
+ }
659
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
+ }
660
700
  lines.push(
661
- `export const ${primaryName} = ${rawCallee}(${rawArgs});`,
662
- );
663
- lines.push(
664
- `${primaryName}.__rsc_ls_key = "__rsc_ls_${stubId}";`,
701
+ `export const ${primaryName} = { __brand: "${brand}", $$id: "${stubId}" };`,
665
702
  );
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};`);
703
+ for (const name of binding.exportNames.slice(1)) {
704
+ lines.push(`export const ${name} = ${primaryName};`);
705
+ }
682
706
  }
683
707
  }
684
- }
685
708
 
686
- return { code: lines.join("\n") + "\n", map: null };
709
+ return { code: lines.join("\n") + "\n", map: null };
710
+ }
687
711
  }
688
- }
689
712
 
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);
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
+ }
698
722
  }
699
- }
700
723
 
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) {
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");
738
732
  changed =
739
- transformHandlerIds(
740
- PRERENDER_CONFIG,
741
- bindings,
733
+ transformLoaders(
734
+ getBindings(code, fnNames),
742
735
  s,
743
736
  filePath,
744
737
  isBuild,
745
738
  ) || changed;
746
- } else {
747
- // Non-RSC mixed-export file: replace Prerender() calls with stubs
748
- // on the shared MagicString so sourcemaps stay accurate.
739
+ }
740
+ if (hasHandleCode) {
741
+ const fnNames = getFnNames("createHandle");
749
742
  changed =
750
- stubHandlerExprs(
751
- PRERENDER_CONFIG,
752
- bindings,
743
+ transformHandles(
744
+ getBindings(code, fnNames),
753
745
  s,
746
+ code,
754
747
  filePath,
755
748
  isBuild,
756
749
  ) || changed;
757
750
  }
758
- }
759
- if (hasStaticHandlerCode) {
760
- const fnNames = getFnNames(STATIC_CONFIG.fnName);
761
- const bindings = getBindings(code, fnNames);
762
- if (isRscEnv) {
751
+ if (hasLocationStateCode) {
752
+ const fnNames = getFnNames("createLocationState");
763
753
  changed =
764
- transformHandlerIds(
765
- STATIC_CONFIG,
766
- bindings,
754
+ transformLocationState(
755
+ getBindings(code, fnNames),
767
756
  s,
768
757
  filePath,
769
758
  isBuild,
770
759
  ) || changed;
771
- } else {
772
- changed =
773
- stubHandlerExprs(STATIC_CONFIG, bindings, s, filePath, isBuild) ||
774
- changed;
775
760
  }
776
- }
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
+ }
777
804
 
778
- if (!changed) return;
805
+ if (!changed) return;
779
806
 
780
- return {
781
- code: s.toString(),
782
- map: s.generateMap({ source: id, includeContent: true }),
783
- };
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
+ }
784
814
  },
785
815
  };
786
816
  }