@rangojs/router 0.0.0-experimental.debug-cache-fix → 0.0.0-experimental.dfdb0387
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -18
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +702 -231
- package/package.json +2 -2
- package/skills/cache-guide/SKILL.md +32 -0
- package/skills/caching/SKILL.md +8 -0
- package/skills/links/SKILL.md +3 -1
- package/skills/loader/SKILL.md +53 -43
- package/skills/middleware/SKILL.md +2 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/route/SKILL.md +31 -0
- package/skills/router-setup/SKILL.md +87 -2
- package/skills/typesafety/SKILL.md +10 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/navigation-bridge.ts +16 -3
- package/src/browser/navigation-client.ts +98 -46
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/partial-update.ts +32 -5
- package/src/browser/prefetch/cache.ts +16 -6
- package/src/browser/prefetch/fetch.ts +52 -6
- package/src/browser/prefetch/queue.ts +61 -29
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/react/Link.tsx +67 -8
- package/src/browser/react/NavigationProvider.tsx +13 -4
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +26 -3
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/segment-reconciler.ts +26 -0
- package/src/browser/server-action-bridge.ts +8 -6
- package/src/browser/types.ts +27 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/cache/cache-scope.ts +12 -14
- package/src/cache/taint.ts +55 -0
- package/src/client.tsx +2 -56
- package/src/context-var.ts +72 -2
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +12 -0
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/reverse.ts +22 -1
- package/src/route-definition/dsl-helpers.ts +42 -19
- package/src/route-definition/helpers-types.ts +10 -6
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +11 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/handler-context.ts +79 -23
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/loader-resolution.ts +156 -21
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/cache-lookup.ts +26 -7
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +82 -4
- package/src/router/middleware-types.ts +6 -8
- package/src/router/middleware.ts +2 -5
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +80 -9
- package/src/router/segment-resolution/helpers.ts +29 -24
- package/src/router/segment-resolution/revalidation.ts +91 -8
- package/src/router/types.ts +1 -0
- package/src/router.ts +54 -5
- package/src/rsc/handler.ts +472 -372
- package/src/rsc/loader-fetch.ts +23 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +14 -2
- package/src/rsc/rsc-rendering.ts +10 -1
- package/src/rsc/server-action.ts +8 -0
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +9 -1
- package/src/server/context.ts +50 -1
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +175 -15
- package/src/ssr/index.tsx +3 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +37 -19
- package/src/types/loader-types.ts +36 -9
- package/src/types/route-entry.ts +1 -1
- package/src/types/segments.ts +1 -0
- package/src/urls/path-helper-types.ts +9 -2
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +77 -5
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +88 -0
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +19 -2
- package/src/vite/router-discovery.ts +178 -37
- package/src/vite/utils/prerender-utils.ts +18 -0
- package/src/vite/utils/shared-utils.ts +3 -2
|
@@ -31,25 +31,25 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
31
31
|
state.rscEntryFileName ?? "index.js",
|
|
32
32
|
);
|
|
33
33
|
|
|
34
|
-
// 1. Evict handler code from
|
|
35
|
-
//
|
|
34
|
+
// 1. Evict handler code from whichever chunks contain handler exports.
|
|
35
|
+
// handlerChunkInfoMap/staticHandlerChunkInfoMap are populated by generateBundle
|
|
36
36
|
// after the production RSC build. In Vite 6 multi-environment builds, the
|
|
37
|
-
// RSC build runs twice (analysis + production).
|
|
38
|
-
//
|
|
37
|
+
// RSC build runs twice (analysis + production). The maps are cleared at the
|
|
38
|
+
// start of each generateBundle pass so only production data is used here.
|
|
39
39
|
const evictionTargets: Array<{
|
|
40
|
-
|
|
40
|
+
infos: Iterable<import("./state.js").ChunkInfo>;
|
|
41
41
|
fnName: string;
|
|
42
42
|
brand: string;
|
|
43
43
|
label: string;
|
|
44
44
|
}> = [
|
|
45
45
|
{
|
|
46
|
-
|
|
46
|
+
infos: state.handlerChunkInfoMap.values(),
|
|
47
47
|
fnName: "Prerender",
|
|
48
48
|
brand: "prerenderHandler",
|
|
49
49
|
label: "handler code from RSC bundle",
|
|
50
50
|
},
|
|
51
51
|
{
|
|
52
|
-
|
|
52
|
+
infos: state.staticHandlerChunkInfoMap.values(),
|
|
53
53
|
fnName: "Static",
|
|
54
54
|
brand: "staticHandler",
|
|
55
55
|
label: "static handler code",
|
|
@@ -57,35 +57,32 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
57
57
|
];
|
|
58
58
|
|
|
59
59
|
for (const target of evictionTargets) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
60
|
+
for (const info of target.infos) {
|
|
61
|
+
const chunkPath = resolve(state.projectRoot, "dist/rsc", info.fileName);
|
|
62
|
+
try {
|
|
63
|
+
const code = readFileSync(chunkPath, "utf-8");
|
|
64
|
+
const result = evictHandlerCode(
|
|
65
|
+
code,
|
|
66
|
+
info.exports,
|
|
67
|
+
target.fnName,
|
|
68
|
+
target.brand,
|
|
69
|
+
);
|
|
70
|
+
if (result) {
|
|
71
|
+
writeFileSync(chunkPath, result.code);
|
|
72
|
+
const savedKB = (result.savedBytes / 1024).toFixed(1);
|
|
73
|
+
console.log(
|
|
74
|
+
`[rsc-router] Evicted ${target.label} (${savedKB} KB saved): ${info.fileName}`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} catch (replaceErr: any) {
|
|
78
|
+
console.warn(
|
|
79
|
+
`[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
|
|
79
80
|
);
|
|
80
81
|
}
|
|
81
|
-
} catch (replaceErr: any) {
|
|
82
|
-
console.warn(
|
|
83
|
-
`[rsc-router] Failed to evict ${target.label}: ${replaceErr.message}`,
|
|
84
|
-
);
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
|
-
state.
|
|
88
|
-
state.
|
|
84
|
+
state.handlerChunkInfoMap.clear();
|
|
85
|
+
state.staticHandlerChunkInfoMap.clear();
|
|
89
86
|
|
|
90
87
|
// 2. Write prerender data as separate importable asset modules
|
|
91
88
|
// and inject a lazy manifest loader into the RSC entry.
|
|
@@ -138,7 +135,7 @@ export function postprocessBundle(state: DiscoveryState): void {
|
|
|
138
135
|
// and inject a __STATIC_MANIFEST import into the RSC entry.
|
|
139
136
|
if (hasStaticData && existsSync(rscEntryPath)) {
|
|
140
137
|
const rscCode = readFileSync(rscEntryPath, "utf-8");
|
|
141
|
-
if (!rscCode.includes("
|
|
138
|
+
if (!rscCode.includes("__static-manifest.js")) {
|
|
142
139
|
try {
|
|
143
140
|
const manifestEntries: string[] = [];
|
|
144
141
|
let totalBytes = copyStagedBuildAssets(
|
|
@@ -135,7 +135,11 @@ export async function discoverRouters(
|
|
|
135
135
|
continue;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
const manifest = generateManifestFull(
|
|
138
|
+
const manifest = generateManifestFull(
|
|
139
|
+
router.urlpatterns,
|
|
140
|
+
routerMountIndex,
|
|
141
|
+
router.__basename ? { urlPrefix: router.__basename } : undefined,
|
|
142
|
+
);
|
|
139
143
|
routerMountIndex++;
|
|
140
144
|
allManifests.push({ id, manifest });
|
|
141
145
|
const routeCount = Object.keys(manifest.routeManifest).length;
|
|
@@ -51,93 +51,144 @@ export async function expandPrerenderRoutes(
|
|
|
51
51
|
return substituteRouteParams(pattern, params);
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
let resolvedRoutes = 0;
|
|
55
|
+
let totalDynamic = 0;
|
|
56
|
+
|
|
57
|
+
// Count dynamic routes upfront for progress reporting
|
|
54
58
|
for (const { manifest } of allManifests) {
|
|
55
59
|
if (!manifest.prerenderRoutes) continue;
|
|
56
|
-
const defs = manifest._prerenderDefs || {};
|
|
57
60
|
for (const routeName of manifest.prerenderRoutes) {
|
|
58
61
|
const pattern = manifest.routeManifest[routeName];
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
62
|
+
if (pattern && (pattern.includes(":") || pattern.includes("*"))) {
|
|
63
|
+
totalDynamic++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Periodic progress log so long getParams() calls don't look stalled
|
|
69
|
+
const paramsStart = performance.now();
|
|
70
|
+
const progressInterval =
|
|
71
|
+
totalDynamic > 0
|
|
72
|
+
? setInterval(() => {
|
|
73
|
+
const elapsed = ((performance.now() - paramsStart) / 1000).toFixed(1);
|
|
74
|
+
console.log(
|
|
75
|
+
`[rsc-router] Resolving prerender params... ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
|
|
76
|
+
);
|
|
77
|
+
}, 5000)
|
|
78
|
+
: undefined;
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
for (const { manifest } of allManifests) {
|
|
82
|
+
if (!manifest.prerenderRoutes) continue;
|
|
83
|
+
const defs = manifest._prerenderDefs || {};
|
|
84
|
+
const passthroughSet = new Set(manifest.passthroughRoutes || []);
|
|
85
|
+
for (const routeName of manifest.prerenderRoutes) {
|
|
86
|
+
const pattern = manifest.routeManifest[routeName];
|
|
87
|
+
if (!pattern) continue;
|
|
88
|
+
const def = defs[routeName];
|
|
89
|
+
const isPassthroughRoute = passthroughSet.has(routeName);
|
|
90
|
+
const hasDynamic = pattern.includes(":") || pattern.includes("*");
|
|
91
|
+
if (!hasDynamic) {
|
|
92
|
+
// Static route: use pattern directly (strip trailing slash for URL)
|
|
93
|
+
entries.push({
|
|
94
|
+
urlPath: pattern.replace(/\/$/, "") || "/",
|
|
95
|
+
routeName,
|
|
96
|
+
concurrency: 1,
|
|
97
|
+
isPassthroughRoute,
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
// Dynamic route: call getParams() to enumerate param combinations
|
|
101
|
+
if (def?.getParams) {
|
|
102
|
+
try {
|
|
103
|
+
const buildVars: Record<string, any> = {};
|
|
104
|
+
const buildEnv = state.resolvedBuildEnv;
|
|
105
|
+
const getParamsCtx = {
|
|
106
|
+
build: true as const,
|
|
107
|
+
dev: !state.isBuildMode,
|
|
108
|
+
set: ((keyOrVar: any, value: any) => {
|
|
109
|
+
contextSet(buildVars, keyOrVar, value);
|
|
110
|
+
}) as any,
|
|
111
|
+
reverse: getParamsReverse,
|
|
112
|
+
get env() {
|
|
113
|
+
if (buildEnv !== undefined) return buildEnv;
|
|
114
|
+
throw new Error(
|
|
115
|
+
"[rsc-router] ctx.env is not available during build-time getParams(). " +
|
|
116
|
+
"Configure buildEnv in your rango() plugin options to enable build-time env access.",
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
const paramsList = await def.getParams(getParamsCtx);
|
|
121
|
+
const concurrency = def.options?.concurrency ?? 1;
|
|
122
|
+
const hasBuildVars =
|
|
123
|
+
Object.keys(buildVars).length > 0 ||
|
|
124
|
+
Object.getOwnPropertySymbols(buildVars).length > 0;
|
|
125
|
+
for (const params of paramsList) {
|
|
126
|
+
let url = substituteRouteParams(
|
|
127
|
+
pattern,
|
|
128
|
+
params as Record<string, string>,
|
|
129
|
+
encodePathParam,
|
|
130
|
+
);
|
|
131
|
+
// Anonymous wildcard fallback: use conventional keys if provided
|
|
132
|
+
if (url.includes("*")) {
|
|
133
|
+
const wildcardValue =
|
|
134
|
+
(params as Record<string, string>)["*"] ??
|
|
135
|
+
(params as Record<string, string>).splat;
|
|
136
|
+
if (wildcardValue !== undefined) {
|
|
137
|
+
url = url.replace(
|
|
138
|
+
/\*[^/]*$/,
|
|
139
|
+
encodePathParam(wildcardValue),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
101
142
|
}
|
|
143
|
+
entries.push({
|
|
144
|
+
urlPath: url.replace(/\/$/, "") || "/",
|
|
145
|
+
routeName,
|
|
146
|
+
concurrency,
|
|
147
|
+
...(hasBuildVars ? { buildVars } : {}),
|
|
148
|
+
isPassthroughRoute,
|
|
149
|
+
});
|
|
102
150
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
151
|
+
resolvedRoutes++;
|
|
152
|
+
} catch (err: any) {
|
|
153
|
+
resolvedRoutes++;
|
|
154
|
+
// Skip in getParams() skips the entire route
|
|
155
|
+
if (err.name === "Skip") {
|
|
156
|
+
console.log(
|
|
157
|
+
`[rsc-router] SKIP route "${routeName}" - ${err.message}`,
|
|
158
|
+
);
|
|
159
|
+
notifyOnError(
|
|
160
|
+
registry,
|
|
161
|
+
err,
|
|
162
|
+
"prerender",
|
|
163
|
+
routeName,
|
|
164
|
+
undefined,
|
|
165
|
+
true,
|
|
166
|
+
);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
// Regular error: fail the build
|
|
170
|
+
console.error(
|
|
171
|
+
`[rsc-router] Failed to get params for prerender route "${routeName}": ${err.message}`,
|
|
124
172
|
);
|
|
125
|
-
|
|
173
|
+
notifyOnError(registry, err, "prerender", routeName);
|
|
174
|
+
throw err;
|
|
126
175
|
}
|
|
127
|
-
|
|
128
|
-
console.
|
|
129
|
-
`[rsc-router]
|
|
176
|
+
} else {
|
|
177
|
+
console.warn(
|
|
178
|
+
`[rsc-router] Dynamic prerender route "${routeName}" has no getParams(), skipping`,
|
|
130
179
|
);
|
|
131
|
-
notifyOnError(registry, err, "prerender", routeName);
|
|
132
|
-
throw err;
|
|
133
180
|
}
|
|
134
|
-
} else {
|
|
135
|
-
console.warn(
|
|
136
|
-
`[rsc-router] Dynamic prerender route "${routeName}" has no getParams(), skipping`,
|
|
137
|
-
);
|
|
138
181
|
}
|
|
139
182
|
}
|
|
140
183
|
}
|
|
184
|
+
} finally {
|
|
185
|
+
if (progressInterval) {
|
|
186
|
+
clearInterval(progressInterval);
|
|
187
|
+
const elapsed = ((performance.now() - paramsStart) / 1000).toFixed(1);
|
|
188
|
+
console.log(
|
|
189
|
+
`[rsc-router] Resolved prerender params: ${resolvedRoutes}/${totalDynamic} routes (${elapsed}s)`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
141
192
|
}
|
|
142
193
|
|
|
143
194
|
if (entries.length === 0) return;
|
|
@@ -175,6 +226,7 @@ export async function expandPrerenderRoutes(
|
|
|
175
226
|
{},
|
|
176
227
|
entry.buildVars,
|
|
177
228
|
entry.isPassthroughRoute,
|
|
229
|
+
state.resolvedBuildEnv,
|
|
178
230
|
);
|
|
179
231
|
if (!result) continue;
|
|
180
232
|
|
|
@@ -326,6 +378,8 @@ export async function renderStaticHandlers(
|
|
|
326
378
|
def.handler,
|
|
327
379
|
def.$$id,
|
|
328
380
|
(def as any).$$routePrefix,
|
|
381
|
+
state.resolvedBuildEnv,
|
|
382
|
+
!state.isBuildMode,
|
|
329
383
|
);
|
|
330
384
|
if (result) {
|
|
331
385
|
const hasHandles = Object.keys(result.handles).length > 0;
|
|
@@ -16,6 +16,10 @@ export interface PluginOptions {
|
|
|
16
16
|
// Mutable ref for deferred auto-discovery (node preset).
|
|
17
17
|
// The auto-discover config() hook populates this before configResolved.
|
|
18
18
|
routerPathRef?: { path?: string };
|
|
19
|
+
/** Build-time env option from rango() config. */
|
|
20
|
+
buildEnv?: import("../plugin-types.js").BuildEnvOption;
|
|
21
|
+
/** Deployment preset (needed for buildEnv "auto" resolution). */
|
|
22
|
+
preset?: "node" | "cloudflare";
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export interface PrecomputedEntry {
|
|
@@ -56,8 +60,8 @@ export interface DiscoveryState {
|
|
|
56
60
|
|
|
57
61
|
prerenderManifestEntries: Record<string, string> | null;
|
|
58
62
|
staticManifestEntries: Record<string, string> | null;
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
handlerChunkInfoMap: Map<string, ChunkInfo>;
|
|
64
|
+
staticHandlerChunkInfoMap: Map<string, ChunkInfo>;
|
|
61
65
|
rscEntryFileName: string | null;
|
|
62
66
|
resolvedPrerenderModules: Map<string, string[]> | undefined;
|
|
63
67
|
resolvedStaticModules: Map<string, string[]> | undefined;
|
|
@@ -67,6 +71,11 @@ export interface DiscoveryState {
|
|
|
67
71
|
devServer: any;
|
|
68
72
|
selfWrittenGenFiles: Map<string, { at: number; hash: string }>;
|
|
69
73
|
SELF_WRITE_WINDOW_MS: number;
|
|
74
|
+
|
|
75
|
+
/** Resolved build-time env bindings (set during buildStart/configureServer). */
|
|
76
|
+
resolvedBuildEnv?: Record<string, unknown>;
|
|
77
|
+
/** Cleanup function for build-time env resources (e.g., miniflare). */
|
|
78
|
+
buildEnvDispose?: (() => Promise<void> | void) | null;
|
|
70
79
|
}
|
|
71
80
|
|
|
72
81
|
export function createDiscoveryState(
|
|
@@ -93,8 +102,8 @@ export function createDiscoveryState(
|
|
|
93
102
|
|
|
94
103
|
prerenderManifestEntries: null,
|
|
95
104
|
staticManifestEntries: null,
|
|
96
|
-
|
|
97
|
-
|
|
105
|
+
handlerChunkInfoMap: new Map(),
|
|
106
|
+
staticHandlerChunkInfoMap: new Map(),
|
|
98
107
|
rscEntryFileName: null,
|
|
99
108
|
resolvedPrerenderModules: undefined,
|
|
100
109
|
resolvedStaticModules: undefined,
|
package/src/vite/index.ts
CHANGED
package/src/vite/plugin-types.ts
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
+
// -- Build-time environment types -------------------------------------------
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Context passed to a buildEnv factory function.
|
|
5
|
+
* Provides Vite config details for conditional env setup.
|
|
6
|
+
*/
|
|
7
|
+
export interface BuildEnvFactoryContext {
|
|
8
|
+
/** Vite project root directory. */
|
|
9
|
+
root: string;
|
|
10
|
+
/** Vite mode (e.g. "development", "production"). */
|
|
11
|
+
mode: string;
|
|
12
|
+
/** Vite command ("serve" for dev, "build" for production). */
|
|
13
|
+
command: "serve" | "build";
|
|
14
|
+
/** Router deployment preset. */
|
|
15
|
+
preset: "node" | "cloudflare";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Factory function that creates build-time environment bindings.
|
|
20
|
+
* Called once at plugin startup. Return `dispose` to clean up resources.
|
|
21
|
+
*/
|
|
22
|
+
export type BuildEnvFactory = (
|
|
23
|
+
ctx: BuildEnvFactoryContext,
|
|
24
|
+
) => Promise<BuildEnvResult> | BuildEnvResult;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Result of resolving build-time environment bindings.
|
|
28
|
+
*/
|
|
29
|
+
export interface BuildEnvResult {
|
|
30
|
+
/** Environment bindings available to Prerender/Static handlers via ctx.env. */
|
|
31
|
+
env: Record<string, unknown>;
|
|
32
|
+
/** Called after build completes to clean up resources (e.g., miniflare). */
|
|
33
|
+
dispose?: () => Promise<void> | void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build-time environment configuration for Prerender and Static handlers.
|
|
38
|
+
*
|
|
39
|
+
* - `false` (default): no build-time env, `ctx.env` throws.
|
|
40
|
+
* - `"auto"`: calls `wrangler.getPlatformProxy()` (cloudflare preset only).
|
|
41
|
+
* - Object: used directly as `ctx.env` during build.
|
|
42
|
+
* - Factory: called once at startup, must return `{ env, dispose? }`.
|
|
43
|
+
*/
|
|
44
|
+
export type BuildEnvOption =
|
|
45
|
+
| false
|
|
46
|
+
| "auto"
|
|
47
|
+
| Record<string, unknown>
|
|
48
|
+
| BuildEnvFactory;
|
|
49
|
+
|
|
50
|
+
// -- Plugin options ---------------------------------------------------------
|
|
51
|
+
|
|
1
52
|
/**
|
|
2
53
|
* Base options shared by all presets
|
|
3
54
|
*/
|
|
@@ -9,12 +60,16 @@ interface RangoBaseOptions {
|
|
|
9
60
|
banner?: boolean;
|
|
10
61
|
|
|
11
62
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
63
|
+
* Environment bindings available to Prerender and Static handlers at build
|
|
64
|
+
* time via `ctx.env`. Applies to both production build and dev on-demand
|
|
65
|
+
* prerender (`/__rsc_prerender`).
|
|
66
|
+
*
|
|
67
|
+
* This is the build-time env supplied by the Vite plugin, not the live
|
|
68
|
+
* request env. It is shared across all prerender invocations for the build.
|
|
69
|
+
*
|
|
70
|
+
* @default false
|
|
16
71
|
*/
|
|
17
|
-
|
|
72
|
+
buildEnv?: BuildEnvOption;
|
|
18
73
|
}
|
|
19
74
|
|
|
20
75
|
/**
|
|
@@ -19,6 +19,18 @@ export function hashId(filePath: string, exportName: string): string {
|
|
|
19
19
|
return `${hash.slice(0, 8)}#${exportName}`;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Build a stable ID for an export binding. Uses hashed IDs in production
|
|
24
|
+
* builds (short + opaque) and readable path#name IDs in dev.
|
|
25
|
+
*/
|
|
26
|
+
export function makeStubId(
|
|
27
|
+
filePath: string,
|
|
28
|
+
exportName: string,
|
|
29
|
+
isBuild: boolean,
|
|
30
|
+
): string {
|
|
31
|
+
return isBuild ? hashId(filePath, exportName) : `${filePath}#${exportName}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
22
34
|
/**
|
|
23
35
|
* Generate an 8-char hex hash for an inline static handler call site.
|
|
24
36
|
* Uses file path and line number (plus optional index for same-line collisions).
|
|
@@ -138,6 +138,36 @@ export function generateExprStubs(
|
|
|
138
138
|
};
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Replace handler call expressions with lightweight stub objects on an
|
|
143
|
+
* existing MagicString. Unlike generateExprStubs (which creates its own
|
|
144
|
+
* MagicString and returns the full result), this integrates into the
|
|
145
|
+
* unified transform pipeline so all transforms share one sourcemap.
|
|
146
|
+
*/
|
|
147
|
+
export function stubHandlerExprs(
|
|
148
|
+
cfg: HandlerTransformConfig,
|
|
149
|
+
bindings: CreateExportBinding[],
|
|
150
|
+
s: MagicString,
|
|
151
|
+
filePath: string,
|
|
152
|
+
isBuild: boolean,
|
|
153
|
+
): boolean {
|
|
154
|
+
let hasChanges = false;
|
|
155
|
+
for (const binding of bindings) {
|
|
156
|
+
const exportName = binding.exportNames[0];
|
|
157
|
+
const handlerId = isBuild
|
|
158
|
+
? hashId(filePath, exportName)
|
|
159
|
+
: `${filePath}#${exportName}`;
|
|
160
|
+
|
|
161
|
+
s.overwrite(
|
|
162
|
+
binding.callExprStart,
|
|
163
|
+
binding.callCloseParenPos + 1,
|
|
164
|
+
`{ __brand: "${cfg.brand}", $$id: "${handlerId}" }`,
|
|
165
|
+
);
|
|
166
|
+
hasChanges = true;
|
|
167
|
+
}
|
|
168
|
+
return hasChanges;
|
|
169
|
+
}
|
|
170
|
+
|
|
141
171
|
/**
|
|
142
172
|
* Inject $$id into export const handler calls in RSC environments.
|
|
143
173
|
*/
|