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