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