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