@teyik0/furin 0.7.0-alpha.1 → 0.7.0-alpha.3
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/dist/build/elysia-aot-worker.d.ts +22 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/index.js +236 -152
- package/dist/build/ssg-cache.d.ts +3 -2
- package/dist/cli/index.js +237 -153
- package/dist/client/link.js +10 -4
- package/dist/client/router/types.d.ts +2 -0
- package/dist/furin.js +57 -19
- package/dist/server/router/patterns.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/sync/index.js +1 -1
- package/package.json +1 -1
- package/src/adapter/runtime-build.ts +1 -1
- package/src/adapter/vercel.ts +8 -12
- package/src/build/elysia-aot-worker.ts +51 -0
- package/src/build/elysia-aot.ts +83 -19
- package/src/build/hydrate.ts +4 -0
- package/src/build/ssg-cache.ts +5 -3
- package/src/client/router/boundary-tree.tsx +12 -10
- package/src/client/router/provider.tsx +2 -2
- package/src/client/router/types.ts +2 -0
- package/src/plugin/routes.ts +25 -13
- package/src/server/render/element.tsx +14 -9
- package/src/server/router/discovery.ts +25 -9
- package/src/server/router/patterns.ts +16 -0
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/mutation-handler.ts +6 -1
package/dist/client/link.js
CHANGED
|
@@ -1083,18 +1083,24 @@ function buildPageElement(match, root, data, options, error) {
|
|
|
1083
1083
|
}
|
|
1084
1084
|
const byDepth = new Map;
|
|
1085
1085
|
for (const segment of match.segmentBoundaries ?? []) {
|
|
1086
|
-
byDepth.
|
|
1086
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
1087
|
+
boundaries.unshift(segment);
|
|
1088
|
+
byDepth.set(segment.depth, boundaries);
|
|
1087
1089
|
}
|
|
1088
1090
|
const rootOffset = root ? 1 : 0;
|
|
1089
1091
|
for (let i = chain.length - 1;i >= rootOffset; i -= 1) {
|
|
1090
|
-
|
|
1092
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
1093
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
1094
|
+
}
|
|
1091
1095
|
const Layout = chain[i]?.layout;
|
|
1092
1096
|
if (Layout) {
|
|
1093
1097
|
element = createElement3(Layout, data, element);
|
|
1094
1098
|
}
|
|
1095
1099
|
}
|
|
1096
1100
|
if (root) {
|
|
1097
|
-
|
|
1101
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
1102
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
1103
|
+
}
|
|
1098
1104
|
if (root.layout) {
|
|
1099
1105
|
element = createElement3(root.layout, data, element);
|
|
1100
1106
|
}
|
|
@@ -5297,7 +5303,7 @@ function RouterProvider({
|
|
|
5297
5303
|
const resolvedSearch = state.data.query ?? EMPTY_SEARCH;
|
|
5298
5304
|
const rootBoundaries = useMemo2(() => {
|
|
5299
5305
|
for (const route of routes) {
|
|
5300
|
-
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0);
|
|
5306
|
+
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0 && b.isRoot !== false);
|
|
5301
5307
|
if (depth0) {
|
|
5302
5308
|
return [depth0];
|
|
5303
5309
|
}
|
|
@@ -174,6 +174,8 @@ export interface RouterContextValue {
|
|
|
174
174
|
export interface ClientSegmentBoundary {
|
|
175
175
|
depth: number;
|
|
176
176
|
error?: ErrorComponent;
|
|
177
|
+
/** False when a pathless directory shares index 0 but does not own the root fallback. */
|
|
178
|
+
isRoot?: boolean;
|
|
177
179
|
notFound?: NotFoundComponent;
|
|
178
180
|
}
|
|
179
181
|
export interface ClientRoute {
|
package/dist/furin.js
CHANGED
|
@@ -258,6 +258,12 @@ function routeSegmentToPattern(segment) {
|
|
|
258
258
|
}
|
|
259
259
|
return dynamic.catchAll ? "*" : `:${dynamic.name}`;
|
|
260
260
|
}
|
|
261
|
+
function isRouteGroup(segment) {
|
|
262
|
+
return ROUTE_GROUP_RE.test(segment);
|
|
263
|
+
}
|
|
264
|
+
function routePatternKey(pattern) {
|
|
265
|
+
return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
|
|
266
|
+
}
|
|
261
267
|
function filePathToPattern(path) {
|
|
262
268
|
const parts = path.replaceAll("\\", "/").split("/");
|
|
263
269
|
const segments = [];
|
|
@@ -269,6 +275,9 @@ function filePathToPattern(path) {
|
|
|
269
275
|
}
|
|
270
276
|
const isFile = idx === lastIndex;
|
|
271
277
|
const name = isFile ? parse(part).name : part;
|
|
278
|
+
if (!isFile && isRouteGroup(name)) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
272
281
|
if (isFile && name === "index") {
|
|
273
282
|
continue;
|
|
274
283
|
}
|
|
@@ -363,9 +372,11 @@ function buildRouteMatcher(routes) {
|
|
|
363
372
|
return null;
|
|
364
373
|
};
|
|
365
374
|
}
|
|
366
|
-
var DYNAMIC_PARAMETER_NAME_RE, REGEX_META_CHARS_RE;
|
|
375
|
+
var DYNAMIC_PARAMETER_NAME_RE, ROUTE_GROUP_RE, DYNAMIC_ROUTE_SEGMENT_RE, REGEX_META_CHARS_RE;
|
|
367
376
|
var init_patterns = __esm(() => {
|
|
368
377
|
DYNAMIC_PARAMETER_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
378
|
+
ROUTE_GROUP_RE = /^\([^()]+\)$/;
|
|
379
|
+
DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
|
|
369
380
|
REGEX_META_CHARS_RE = /[.*+?^${}()|[\]\\]/;
|
|
370
381
|
});
|
|
371
382
|
|
|
@@ -7162,9 +7173,6 @@ async function scanRootLayout(pagesDir) {
|
|
|
7162
7173
|
route: rootRoute
|
|
7163
7174
|
};
|
|
7164
7175
|
}
|
|
7165
|
-
function routePatternKey(pattern) {
|
|
7166
|
-
return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
|
|
7167
|
-
}
|
|
7168
7176
|
function isConventionFileName(name) {
|
|
7169
7177
|
return CONVENTION_FILE_NAMES.includes(name);
|
|
7170
7178
|
}
|
|
@@ -7213,7 +7221,7 @@ async function scanPageFiles(pagesDir, root) {
|
|
|
7213
7221
|
const devRoute = await buildDevRoute(absolutePath, relativePath, pattern, root, pagesDir);
|
|
7214
7222
|
devRoute.notFound = notFound;
|
|
7215
7223
|
devRoute.error = errorComponent;
|
|
7216
|
-
devRoute.segmentBoundaries = segmentBoundaries;
|
|
7224
|
+
devRoute.segmentBoundaries = alignSegmentBoundaryDepths(segmentBoundaries, devRoute.routeChain);
|
|
7217
7225
|
routes.push(devRoute);
|
|
7218
7226
|
continue;
|
|
7219
7227
|
}
|
|
@@ -7233,7 +7241,7 @@ async function scanPageFiles(pagesDir, root) {
|
|
|
7233
7241
|
path: absolutePath,
|
|
7234
7242
|
pattern,
|
|
7235
7243
|
routeChain,
|
|
7236
|
-
segmentBoundaries,
|
|
7244
|
+
segmentBoundaries: alignSegmentBoundaryDepths(segmentBoundaries, routeChain),
|
|
7237
7245
|
tags: collectRouteTags(routeChain, page)
|
|
7238
7246
|
});
|
|
7239
7247
|
}
|
|
@@ -7271,6 +7279,21 @@ async function resolveDefinedLayoutParent(pagePath, pagesDir, rootRoute) {
|
|
|
7271
7279
|
}
|
|
7272
7280
|
return parent;
|
|
7273
7281
|
}
|
|
7282
|
+
function alignSegmentBoundaryDepths(boundaries, routeChain) {
|
|
7283
|
+
return boundaries.map((boundary) => {
|
|
7284
|
+
let depth = 0;
|
|
7285
|
+
for (const [index, entry] of routeChain.entries()) {
|
|
7286
|
+
if (!entry.sourcePath) {
|
|
7287
|
+
continue;
|
|
7288
|
+
}
|
|
7289
|
+
const directory = entry.sourcePath.slice(0, entry.sourcePath.lastIndexOf("/"));
|
|
7290
|
+
if (boundary.path === directory || boundary.path.startsWith(`${directory}/`)) {
|
|
7291
|
+
depth = index;
|
|
7292
|
+
}
|
|
7293
|
+
}
|
|
7294
|
+
return { ...boundary, depth };
|
|
7295
|
+
});
|
|
7296
|
+
}
|
|
7274
7297
|
async function collectSegmentBoundaries(pageAbsolutePath, pagesDir, notFoundCache, errorCache) {
|
|
7275
7298
|
const pageDir = pageAbsolutePath.slice(0, pageAbsolutePath.lastIndexOf("/"));
|
|
7276
7299
|
const dirs = [pagesDir];
|
|
@@ -7371,14 +7394,13 @@ async function collectPageFilePaths(dir) {
|
|
|
7371
7394
|
}
|
|
7372
7395
|
return files;
|
|
7373
7396
|
}
|
|
7374
|
-
var CONVENTION_FILE_NAMES, SOURCE_MODULE_EXTENSIONS,
|
|
7397
|
+
var CONVENTION_FILE_NAMES, SOURCE_MODULE_EXTENSIONS, DIR_SCAN_CONCURRENCY = 8;
|
|
7375
7398
|
var init_discovery = __esm(() => {
|
|
7376
7399
|
init_internal();
|
|
7377
7400
|
init_patterns();
|
|
7378
7401
|
init_source_version();
|
|
7379
7402
|
CONVENTION_FILE_NAMES = ["not-found", "error"];
|
|
7380
7403
|
SOURCE_MODULE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
|
|
7381
|
-
DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
|
|
7382
7404
|
});
|
|
7383
7405
|
|
|
7384
7406
|
// src/server/dev/transform-failure.ts
|
|
@@ -11111,7 +11133,8 @@ function routeModuleSpecifier(instance) {
|
|
|
11111
11133
|
return `@teyik0/furin/routes?instance=${Bun.hash(instanceKey(instance)).toString(16)}`;
|
|
11112
11134
|
}
|
|
11113
11135
|
function scanRouteFiles(directory, directorySegments, files) {
|
|
11114
|
-
|
|
11136
|
+
const entries = readdirSync(directory, { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name));
|
|
11137
|
+
for (const entry of entries) {
|
|
11115
11138
|
if (entry.isDirectory()) {
|
|
11116
11139
|
if (entry.name.startsWith("_")) {
|
|
11117
11140
|
continue;
|
|
@@ -11144,9 +11167,6 @@ function scanRouteFiles(directory, directorySegments, files) {
|
|
|
11144
11167
|
function segmentPath(segment) {
|
|
11145
11168
|
return routeSegmentToPattern(segment);
|
|
11146
11169
|
}
|
|
11147
|
-
function routePath(segments) {
|
|
11148
|
-
return segments.length === 0 ? "/" : `/${segments.map(segmentPath).join("/")}`;
|
|
11149
|
-
}
|
|
11150
11170
|
function routeId(instanceId, path) {
|
|
11151
11171
|
const encodedPath = Array.from(new TextEncoder().encode(path), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
11152
11172
|
return `_route_${instanceId}_${encodedPath}`;
|
|
@@ -11175,14 +11195,22 @@ function buildRouteTree(pagesDir, instanceId) {
|
|
|
11175
11195
|
return node;
|
|
11176
11196
|
};
|
|
11177
11197
|
const root = ensureNode([]);
|
|
11198
|
+
const seenPatterns = new Map;
|
|
11178
11199
|
for (const file of scannedFiles) {
|
|
11179
11200
|
const node = ensureNode(file.directorySegments);
|
|
11180
11201
|
if (file.base === "_route") {
|
|
11181
|
-
const path =
|
|
11202
|
+
const path = `/${file.directorySegments.map(segmentPath).join("/")}#layout`;
|
|
11182
11203
|
node.layout = { id: routeId(instanceId, path), path: "", sourcePath: file.sourcePath };
|
|
11183
11204
|
continue;
|
|
11184
11205
|
}
|
|
11185
|
-
const path =
|
|
11206
|
+
const path = filePathToPattern(relative7(pagesDir, file.sourcePath));
|
|
11207
|
+
const patternKey = routePatternKey(path);
|
|
11208
|
+
const relativePath = relative7(pagesDir, file.sourcePath).replaceAll("\\", "/");
|
|
11209
|
+
const previousPath = seenPatterns.get(patternKey);
|
|
11210
|
+
if (previousPath !== undefined) {
|
|
11211
|
+
throw new Error(`[furin] Duplicate route pattern "${patternKey}" from "${previousPath}" and "${relativePath}".`);
|
|
11212
|
+
}
|
|
11213
|
+
seenPatterns.set(patternKey, relativePath);
|
|
11186
11214
|
const route = { id: routeId(instanceId, path), path, sourcePath: file.sourcePath };
|
|
11187
11215
|
if (file.base === "index") {
|
|
11188
11216
|
node.indexRoute = route;
|
|
@@ -11581,7 +11609,7 @@ async function composableRouteApps(route) {
|
|
|
11581
11609
|
};
|
|
11582
11610
|
}
|
|
11583
11611
|
async function composeRuntimeNode(node) {
|
|
11584
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
11612
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
11585
11613
|
const { fileRoutes } = node;
|
|
11586
11614
|
const [layoutApps, indexRouteApps, fileRouteApps, childApps] = await Promise.all([
|
|
11587
11615
|
node.layout ? composableRouteApps(node.layout) : undefined,
|
|
@@ -12592,6 +12620,7 @@ var init_route_types = () => {};
|
|
|
12592
12620
|
import { existsSync as existsSync7, mkdirSync, readFileSync as readFileSync7, writeFileSync as writeFileSync2 } from "fs";
|
|
12593
12621
|
import { join as join6 } from "path";
|
|
12594
12622
|
function generateHydrateEntry(routes, rootLayout, basePath, clientLogging) {
|
|
12623
|
+
const rootDirectory = rootLayout.replaceAll("\\", "/").split("/").slice(0, -1).join("/");
|
|
12595
12624
|
const getBoundaryIdent = (idents, filePath) => {
|
|
12596
12625
|
if (!filePath) {
|
|
12597
12626
|
return;
|
|
@@ -12632,6 +12661,9 @@ function generateHydrateEntry(routes, rootLayout, basePath, clientLogging) {
|
|
|
12632
12661
|
if (notFoundIdent) {
|
|
12633
12662
|
parts.push(`notFound: ${notFoundIdent}.default`);
|
|
12634
12663
|
}
|
|
12664
|
+
if (seg.depth === 0 && seg.path.replaceAll("\\", "/") !== rootDirectory) {
|
|
12665
|
+
parts.push("isRoot: false");
|
|
12666
|
+
}
|
|
12635
12667
|
boundaryLiterals.push(`{ ${parts.join(", ")} }`);
|
|
12636
12668
|
}
|
|
12637
12669
|
const lazyImports = [
|
|
@@ -18584,17 +18616,23 @@ function buildElement(route, data, rootLayout) {
|
|
|
18584
18616
|
const byDepth = new Map;
|
|
18585
18617
|
const legacyRoute = route;
|
|
18586
18618
|
for (const segment of legacyRoute.segmentBoundaries ?? []) {
|
|
18587
|
-
byDepth.
|
|
18619
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
18620
|
+
boundaries.unshift(segment);
|
|
18621
|
+
byDepth.set(segment.depth, boundaries);
|
|
18588
18622
|
}
|
|
18589
18623
|
for (let i = route.routeChain.length - 1;i >= 1; i -= 1) {
|
|
18590
|
-
|
|
18624
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
18625
|
+
element = wrapSegmentBoundaries(element, segment, undefined);
|
|
18626
|
+
}
|
|
18591
18627
|
const routeEntry = route.routeChain[i];
|
|
18592
18628
|
if (routeEntry?.layout) {
|
|
18593
18629
|
const Layout = routeEntry.layout;
|
|
18594
18630
|
element = createElement6(Layout, data, element);
|
|
18595
18631
|
}
|
|
18596
18632
|
}
|
|
18597
|
-
|
|
18633
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
18634
|
+
element = wrapSegmentBoundaries(element, segment, undefined);
|
|
18635
|
+
}
|
|
18598
18636
|
if (rootLayout.layout) {
|
|
18599
18637
|
const RootLayoutComponent = rootLayout.layout;
|
|
18600
18638
|
element = createElement6(RootLayoutComponent, data, element);
|
|
@@ -21453,7 +21491,7 @@ function installMutationHandlers(app) {
|
|
|
21453
21491
|
table.inheritedChain[index],
|
|
21454
21492
|
table.macroScope?.get(index)
|
|
21455
21493
|
] : app["~routes"][index]);
|
|
21456
|
-
if (!nativeRoute || ["GET", "HEAD", "OPTIONS", "WS"].includes(nativeRoute[0])) {
|
|
21494
|
+
if (!nativeRoute || ["GET", "HEAD", "OPTIONS", "WS"].includes(nativeRoute[0]) || typeof nativeRoute[2] === "function" && "~mount" in nativeRoute[2]) {
|
|
21457
21495
|
return compileHandler(index, immediate, route, aliases, table);
|
|
21458
21496
|
}
|
|
21459
21497
|
const [method, path, handle, owner, hooks, chain, inherited, macroScope] = nativeRoute;
|
|
@@ -11,6 +11,8 @@ export interface DynamicRouteSegment {
|
|
|
11
11
|
}
|
|
12
12
|
export declare function parseDynamicRouteSegment(segment: string, sourcePath: string | undefined): DynamicRouteSegment | undefined;
|
|
13
13
|
export declare function routeSegmentToPattern(segment: string): string;
|
|
14
|
+
export declare function isRouteGroup(segment: string): boolean;
|
|
15
|
+
export declare function routePatternKey(pattern: string): string;
|
|
14
16
|
export declare function filePathToPattern(path: string): string;
|
|
15
17
|
export declare function escapeRegExpChar(ch: string): string;
|
|
16
18
|
/**
|
|
@@ -11,7 +11,7 @@ import type { NotFoundComponent } from "../../shared/not-found.ts";
|
|
|
11
11
|
* exact nesting level where the user authored them (Next.js app-router model).
|
|
12
12
|
*/
|
|
13
13
|
export interface SegmentBoundary {
|
|
14
|
-
/**
|
|
14
|
+
/** Index of the enclosing layout in routeChain; 0 = the root layout. */
|
|
15
15
|
depth: number;
|
|
16
16
|
error?: ErrorComponent;
|
|
17
17
|
/**
|
|
@@ -3801,7 +3801,7 @@ function installMutationHandlers(app) {
|
|
|
3801
3801
|
table.inheritedChain[index],
|
|
3802
3802
|
table.macroScope?.get(index)
|
|
3803
3803
|
] : app["~routes"][index]);
|
|
3804
|
-
if (!nativeRoute || ["GET", "HEAD", "OPTIONS", "WS"].includes(nativeRoute[0])) {
|
|
3804
|
+
if (!nativeRoute || ["GET", "HEAD", "OPTIONS", "WS"].includes(nativeRoute[0]) || typeof nativeRoute[2] === "function" && "~mount" in nativeRoute[2]) {
|
|
3805
3805
|
return compileHandler(index, immediate, route, aliases, table);
|
|
3806
3806
|
}
|
|
3807
3807
|
const [method, path, handle, owner, hooks, chain, inherited, macroScope] = nativeRoute;
|
package/package.json
CHANGED
|
@@ -391,7 +391,7 @@ export async function buildRuntimeApp(
|
|
|
391
391
|
let prerenders: RoutePrerender[] = [];
|
|
392
392
|
if (serverEntry) {
|
|
393
393
|
if (targetName === "vercel") {
|
|
394
|
-
prerenders = await buildRoutePrerenders(routes, root, "http://localhost", prefix);
|
|
394
|
+
prerenders = await buildRoutePrerenders(routes, root, "http://localhost", prefix, true);
|
|
395
395
|
} else {
|
|
396
396
|
ssgCache = await buildSSGCacheSnapshot(routes, root, "http://localhost", prefix);
|
|
397
397
|
}
|
package/src/adapter/vercel.ts
CHANGED
|
@@ -538,7 +538,7 @@ export default { fetch: handle };
|
|
|
538
538
|
`;
|
|
539
539
|
}
|
|
540
540
|
|
|
541
|
-
async function
|
|
541
|
+
async function buildProductionFallbacks(
|
|
542
542
|
apps: RuntimeTargetApp[],
|
|
543
543
|
builds: RuntimeAppBuild[],
|
|
544
544
|
specs: PrerenderSpec[],
|
|
@@ -551,13 +551,9 @@ async function buildPprFallbacks(
|
|
|
551
551
|
.filter((prerender) => prerender.result === undefined)
|
|
552
552
|
.map((prerender) => {
|
|
553
553
|
const path = physicalPath((apps[index] as RuntimeTargetApp).prefix, prerender.path);
|
|
554
|
-
const spec = specs.find(
|
|
555
|
-
(candidate) =>
|
|
556
|
-
candidate.config.chain !== undefined &&
|
|
557
|
-
new RegExp(`^(?:${candidate.source})$`).test(path)
|
|
558
|
-
);
|
|
554
|
+
const spec = specs.find((candidate) => new RegExp(`^(?:${candidate.source})$`).test(path));
|
|
559
555
|
if (!spec) {
|
|
560
|
-
throw new Error(`[furin] Missing
|
|
556
|
+
throw new Error(`[furin] Missing prerender build target for ${path}`);
|
|
561
557
|
}
|
|
562
558
|
return { path, prerender, spec };
|
|
563
559
|
})
|
|
@@ -565,7 +561,7 @@ async function buildPprFallbacks(
|
|
|
565
561
|
if (jobs.length === 0) {
|
|
566
562
|
return;
|
|
567
563
|
}
|
|
568
|
-
const resultPath = join(targetDir, "
|
|
564
|
+
const resultPath = join(targetDir, "prerender-results.json");
|
|
569
565
|
const requests = jobs.map(
|
|
570
566
|
({ path, spec }) =>
|
|
571
567
|
`http://localhost/${spec.functionName}?${ISR_PATH_PARAM}=${encodeURIComponent(path)}`
|
|
@@ -573,9 +569,9 @@ async function buildPprFallbacks(
|
|
|
573
569
|
const script = `
|
|
574
570
|
const handler = (await import(${JSON.stringify(pathToFileURL(join(functionsDir, "__server.func/index.js")).href)})).default;
|
|
575
571
|
const results = [];
|
|
576
|
-
for (const url of ${JSON.stringify(requests)}) {
|
|
572
|
+
for (const [index, url] of ${JSON.stringify(requests)}.entries()) {
|
|
577
573
|
const response = await handler.fetch(new Request(url));
|
|
578
|
-
if (response.status >= 400) throw new Error("
|
|
574
|
+
if (response.status >= 500 || (response.status >= 400 && ${JSON.stringify(jobs.map(({ spec }) => spec.config.chain !== undefined))}[index])) throw new Error("Production prerender failed for " + url + ": HTTP " + response.status);
|
|
579
575
|
results.push({ body: await response.text(), headers: Object.fromEntries(response.headers), status: response.status });
|
|
580
576
|
}
|
|
581
577
|
await Bun.write(${JSON.stringify(resultPath)}, JSON.stringify(results));
|
|
@@ -594,7 +590,7 @@ process.exit(0);
|
|
|
594
590
|
new Response(child.stderr).text(),
|
|
595
591
|
]);
|
|
596
592
|
if (exitCode !== 0) {
|
|
597
|
-
throw new Error(`[furin]
|
|
593
|
+
throw new Error(`[furin] Production prerender failed:\n${stderr}\n${stdout}`);
|
|
598
594
|
}
|
|
599
595
|
const results = JSON.parse(readFileSync(resultPath, "utf8")) as {
|
|
600
596
|
body: string;
|
|
@@ -812,7 +808,7 @@ export async function buildVercelTarget(
|
|
|
812
808
|
.filter((output) => output.path.endsWith(".js"))
|
|
813
809
|
.reduce((total, output) => total + output.size, 0);
|
|
814
810
|
writeFileSync(join(serverFunctionDir, "index.js"), vercelBootstrapSource(serverBundleBytes));
|
|
815
|
-
await
|
|
811
|
+
await buildProductionFallbacks(apps, builds, prerenderSpecs, functionsDir, rootDir, targetDir);
|
|
816
812
|
|
|
817
813
|
writeFileSync(
|
|
818
814
|
join(serverFunctionDir, ".vc-config.json"),
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
+
import { aotFactory } from "elysia/plugin/aot/unplugin";
|
|
3
|
+
import { isomorphicTransformPlugin } from "../plugin/transform-isomorphic.ts";
|
|
4
|
+
|
|
5
|
+
export type AotWorkerOperation =
|
|
6
|
+
| { method: "start" | "end" }
|
|
7
|
+
| { code: string; method: "transform"; path: string };
|
|
8
|
+
|
|
9
|
+
export type AotWorkerRequest = AotWorkerOperation & { id: number };
|
|
10
|
+
|
|
11
|
+
export interface AotWorkerResult {
|
|
12
|
+
code?: string;
|
|
13
|
+
virtualType?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type AotWorkerResponse =
|
|
17
|
+
| (AotWorkerResult & { id: number; ok: true })
|
|
18
|
+
| { error: Error; id: number; ok: false };
|
|
19
|
+
|
|
20
|
+
const port = parentPort;
|
|
21
|
+
if (!port) {
|
|
22
|
+
throw new Error("[furin] Elysia AOT worker requires a parent port.");
|
|
23
|
+
}
|
|
24
|
+
const { entry } = workerData as { entry: string };
|
|
25
|
+
const hooks = aotFactory({ entry, strip: false, target: "bun" });
|
|
26
|
+
Bun.plugin(isomorphicTransformPlugin("server"));
|
|
27
|
+
|
|
28
|
+
port.on("message", async (request: AotWorkerRequest) => {
|
|
29
|
+
try {
|
|
30
|
+
let code: string | undefined;
|
|
31
|
+
let virtualType: string | undefined;
|
|
32
|
+
if (request.method === "start") {
|
|
33
|
+
await hooks.buildStart?.();
|
|
34
|
+
code = hooks.load?.("\0elysia/compiled");
|
|
35
|
+
virtualType = hooks.load?.("\0elysia/type");
|
|
36
|
+
} else if (request.method === "transform") {
|
|
37
|
+
if (hooks.transformInclude?.(request.path) !== false) {
|
|
38
|
+
code = hooks.transform?.(request.code, request.path);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
hooks.buildEnd?.();
|
|
42
|
+
}
|
|
43
|
+
port.postMessage({ code, id: request.id, ok: true, virtualType } satisfies AotWorkerResponse);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
port.postMessage({
|
|
46
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
47
|
+
id: request.id,
|
|
48
|
+
ok: false,
|
|
49
|
+
} satisfies AotWorkerResponse);
|
|
50
|
+
}
|
|
51
|
+
});
|
package/src/build/elysia-aot.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
import { dirname, resolve } from "node:path";
|
|
2
|
-
import {
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { Worker } from "node:worker_threads";
|
|
4
|
+
import type {
|
|
5
|
+
AotWorkerOperation,
|
|
6
|
+
AotWorkerRequest,
|
|
7
|
+
AotWorkerResponse,
|
|
8
|
+
AotWorkerResult,
|
|
9
|
+
} from "./elysia-aot-worker.ts";
|
|
3
10
|
|
|
4
11
|
const AOT_NAMESPACE = "furin-elysia-aot";
|
|
5
12
|
const ELYSIA_RESOLVE_DIR = dirname(Bun.resolveSync("elysia", import.meta.dir));
|
|
13
|
+
// The published package ships src/ too; resolving from the package entry also
|
|
14
|
+
// works when this adapter is bundled into dist/build/index.js or dist/cli/index.js.
|
|
15
|
+
const AOT_WORKER_PATH = join(
|
|
16
|
+
dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin")))),
|
|
17
|
+
"src/build/elysia-aot-worker.ts"
|
|
18
|
+
);
|
|
6
19
|
const SOURCE_FILTER = /\.[cm]?[jt]sx?$/;
|
|
20
|
+
const ELYSIA_IMPORT_FILTER = /^elysia(?:\/(?!compiled$|type$).*)?$/;
|
|
21
|
+
const ELYSIA_VIRTUAL_IMPORT_FILTER = /^elysia\/(?:compiled|type)$/;
|
|
22
|
+
const ANY_MODULE_FILTER = /.*/;
|
|
7
23
|
const WEBSOCKET_STUB_MARKER =
|
|
8
24
|
"[elysia-aot] WebSocket route builder was stripped (strip mode) but a WS route was used.";
|
|
9
25
|
|
|
@@ -39,16 +55,57 @@ export function handleWSResponse(){return e()}
|
|
|
39
55
|
`;
|
|
40
56
|
}
|
|
41
57
|
|
|
42
|
-
/** Elysia AOT
|
|
58
|
+
/** Elysia AOT evaluation has its own module cache, independent of prerender loaders. */
|
|
43
59
|
export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
44
60
|
const entryPath = resolve(entry);
|
|
45
|
-
const hooks = aotFactory({ entry, strip: false, target: "bun" });
|
|
46
61
|
return {
|
|
47
62
|
name: "elysia-aot",
|
|
48
63
|
async setup(build) {
|
|
64
|
+
const worker = new Worker(AOT_WORKER_PATH, { workerData: { entry: entryPath } });
|
|
65
|
+
const pending = new Map<
|
|
66
|
+
number,
|
|
67
|
+
{
|
|
68
|
+
reject: (error: Error) => void;
|
|
69
|
+
resolve: (response: AotWorkerResult) => void;
|
|
70
|
+
}
|
|
71
|
+
>();
|
|
72
|
+
let nextId = 0;
|
|
73
|
+
let workerFailure: Error | undefined;
|
|
74
|
+
const fail = (error: Error): void => {
|
|
75
|
+
workerFailure = error;
|
|
76
|
+
for (const request of pending.values()) {
|
|
77
|
+
request.reject(error);
|
|
78
|
+
}
|
|
79
|
+
pending.clear();
|
|
80
|
+
};
|
|
81
|
+
worker.on("message", (response: AotWorkerResponse) => {
|
|
82
|
+
const request = pending.get(response.id);
|
|
83
|
+
pending.delete(response.id);
|
|
84
|
+
if (response.ok) {
|
|
85
|
+
request?.resolve(response);
|
|
86
|
+
} else {
|
|
87
|
+
request?.reject(response.error);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
worker.on("error", fail);
|
|
91
|
+
worker.on("exit", (code) => fail(new Error(`[furin] Elysia AOT worker exited (${code}).`)));
|
|
92
|
+
const request = (payload: AotWorkerOperation) =>
|
|
93
|
+
new Promise<AotWorkerResult>((resolveResponse, reject) => {
|
|
94
|
+
if (workerFailure) {
|
|
95
|
+
reject(workerFailure);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const id = nextId;
|
|
99
|
+
nextId += 1;
|
|
100
|
+
pending.set(id, { reject, resolve: resolveResponse });
|
|
101
|
+
worker.postMessage({ ...payload, id } satisfies AotWorkerRequest);
|
|
102
|
+
});
|
|
103
|
+
let compiled: string | undefined;
|
|
104
|
+
let virtualType: string | undefined;
|
|
49
105
|
try {
|
|
50
|
-
await
|
|
106
|
+
({ code: compiled, virtualType } = await request({ method: "start" }));
|
|
51
107
|
} catch (error) {
|
|
108
|
+
await worker.terminate();
|
|
52
109
|
if (
|
|
53
110
|
error instanceof Error &&
|
|
54
111
|
error.message.startsWith("[elysia-aot]") &&
|
|
@@ -59,20 +116,33 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
59
116
|
}
|
|
60
117
|
throw error;
|
|
61
118
|
}
|
|
119
|
+
build.onEnd(async () => {
|
|
120
|
+
try {
|
|
121
|
+
await request({ method: "end" });
|
|
122
|
+
} finally {
|
|
123
|
+
await worker.terminate();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
62
126
|
const entrySource = await Bun.file(entryPath).text();
|
|
63
|
-
const transformedEntry = await
|
|
127
|
+
const { code: transformedEntry } = await request({
|
|
128
|
+
code: entrySource,
|
|
129
|
+
method: "transform",
|
|
130
|
+
path: entryPath,
|
|
131
|
+
});
|
|
64
132
|
if (transformedEntry !== undefined) {
|
|
65
133
|
await Bun.write(entryPath, transformedEntry);
|
|
66
134
|
}
|
|
67
|
-
build.onResolve({ filter:
|
|
135
|
+
build.onResolve({ filter: ELYSIA_IMPORT_FILTER }, ({ path }) => ({
|
|
68
136
|
path: Bun.resolveSync(path, ELYSIA_RESOLVE_DIR),
|
|
69
137
|
}));
|
|
70
|
-
build.onResolve({ filter:
|
|
71
|
-
|
|
72
|
-
|
|
138
|
+
build.onResolve({ filter: ELYSIA_VIRTUAL_IMPORT_FILTER }, ({ path }) => {
|
|
139
|
+
if (path === "elysia/type" && virtualType === undefined) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
return { namespace: AOT_NAMESPACE, path };
|
|
73
143
|
});
|
|
74
|
-
build.onLoad({ filter:
|
|
75
|
-
const contents =
|
|
144
|
+
build.onLoad({ filter: ANY_MODULE_FILTER, namespace: AOT_NAMESPACE }, ({ path }) => {
|
|
145
|
+
const contents = path === "elysia/compiled" ? compiled : virtualType;
|
|
76
146
|
return contents === undefined
|
|
77
147
|
? undefined
|
|
78
148
|
: { contents, loader: "js", resolveDir: ELYSIA_RESOLVE_DIR };
|
|
@@ -81,11 +151,8 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
81
151
|
if (resolve(path) === entryPath) {
|
|
82
152
|
return;
|
|
83
153
|
}
|
|
84
|
-
if (hooks.transformInclude?.(path) === false) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
154
|
const original = await Bun.file(path).text();
|
|
88
|
-
const transformed = await
|
|
155
|
+
const { code: transformed } = await request({ code: original, method: "transform", path });
|
|
89
156
|
if (transformed === undefined) {
|
|
90
157
|
return;
|
|
91
158
|
}
|
|
@@ -94,9 +161,6 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
94
161
|
loader: loaderFor(path),
|
|
95
162
|
};
|
|
96
163
|
});
|
|
97
|
-
build.onEnd(() => {
|
|
98
|
-
hooks.buildEnd?.();
|
|
99
|
-
});
|
|
100
164
|
},
|
|
101
165
|
};
|
|
102
166
|
}
|
package/src/build/hydrate.ts
CHANGED
|
@@ -31,6 +31,7 @@ export function generateHydrateEntry(
|
|
|
31
31
|
basePath: string,
|
|
32
32
|
clientLogging: boolean
|
|
33
33
|
): string {
|
|
34
|
+
const rootDirectory = rootLayout.replaceAll("\\", "/").split("/").slice(0, -1).join("/");
|
|
34
35
|
const getBoundaryIdent = (idents: Map<string, string>, filePath: string | undefined) => {
|
|
35
36
|
if (!filePath) {
|
|
36
37
|
return;
|
|
@@ -85,6 +86,9 @@ export function generateHydrateEntry(
|
|
|
85
86
|
if (notFoundIdent) {
|
|
86
87
|
parts.push(`notFound: ${notFoundIdent}.default`);
|
|
87
88
|
}
|
|
89
|
+
if (seg.depth === 0 && seg.path.replaceAll("\\", "/") !== rootDirectory) {
|
|
90
|
+
parts.push("isRoot: false");
|
|
91
|
+
}
|
|
88
92
|
boundaryLiterals.push(`{ ${parts.join(", ")} }`);
|
|
89
93
|
}
|
|
90
94
|
|
package/src/build/ssg-cache.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type SSGCacheSnapshot = Record<string, SsgCacheEntry>;
|
|
|
12
12
|
|
|
13
13
|
export interface RoutePrerender {
|
|
14
14
|
path: string;
|
|
15
|
-
/**
|
|
15
|
+
/** Deferred targets are materialized later by the exact production bundle. */
|
|
16
16
|
result?: SsgCacheEntry | Response;
|
|
17
17
|
route: ResolvedRoute;
|
|
18
18
|
}
|
|
@@ -31,12 +31,14 @@ function hasRequestDependentInput(route: ResolvedRoute, root: RootLayout): boole
|
|
|
31
31
|
* known; dynamic routes contribute the values returned by staticParams(). ISR
|
|
32
32
|
* routes with query schemas stay on-demand. PPR paths are collected here, then
|
|
33
33
|
* rendered by the production bundle so React's postponed tree matches at runtime.
|
|
34
|
+
* Vercel defers all rendering to preserve the final application's runtime configuration.
|
|
34
35
|
*/
|
|
35
36
|
export async function buildRoutePrerenders(
|
|
36
37
|
routes: ResolvedRoute[],
|
|
37
38
|
root: RootLayout,
|
|
38
39
|
origin: string,
|
|
39
|
-
basePath: string
|
|
40
|
+
basePath: string,
|
|
41
|
+
deferRendering?: boolean
|
|
40
42
|
): Promise<RoutePrerender[]> {
|
|
41
43
|
const prerenders: RoutePrerender[] = [];
|
|
42
44
|
const searchRoutes = createSearchRouteMetadata(routes);
|
|
@@ -65,7 +67,7 @@ export async function buildRoutePrerenders(
|
|
|
65
67
|
if (matchRoute(path)?.route !== route) {
|
|
66
68
|
continue;
|
|
67
69
|
}
|
|
68
|
-
if (root.route.requestLoader !== undefined || hasRequestLoader(route)) {
|
|
70
|
+
if (deferRendering || root.route.requestLoader !== undefined || hasRequestLoader(route)) {
|
|
69
71
|
prerenders.push({ path, route });
|
|
70
72
|
continue;
|
|
71
73
|
}
|