@teyik0/furin 0.3.0-alpha.1 → 0.4.0-alpha.1
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/adapter/bun.d.ts +1 -1
- package/dist/build/client.d.ts +2 -2
- package/dist/build/entry-template.d.ts +4 -0
- package/dist/build/entry-template.ts +17 -3
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +8288 -6629
- package/dist/build/route-types.d.ts +2 -22
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +1 -1
- package/dist/cli/index.js +7862 -6168
- package/dist/client/boundaries.d.ts +2 -0
- package/dist/client/define-route.d.ts +35 -0
- package/dist/client/document.d.ts +26 -0
- package/dist/client/hmr.d.ts +10 -0
- package/dist/client/internal/runtime-types.d.ts +3 -1
- package/dist/client/link.js +1177 -894
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +9 -0
- package/dist/client/router/search/index.d.ts +3 -7
- package/dist/client/router/types.d.ts +32 -24
- package/dist/client.d.ts +4 -159
- package/dist/client.js +183 -21
- package/dist/config.d.ts +8 -8
- package/dist/config.js +8 -8
- package/dist/define-route.d.ts +1355 -0
- package/dist/furin.d.ts +17 -33
- package/dist/furin.js +13444 -10301
- package/dist/plugin/index.js +953 -244
- package/dist/plugin/route-config-autofix.d.ts +31 -0
- package/dist/plugin/routes.d.ts +27 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +1 -0
- package/dist/rsc/build/index.d.ts +1 -1
- package/dist/rsc.js +3 -3
- package/dist/server/dev-page-plugin.d.ts +7 -5
- package/dist/server/devtools/instrumentation.d.ts +2 -2
- package/dist/server/devtools/instrumentation.production.d.ts +2 -2
- package/dist/server/devtools/plugin.d.ts +2 -2
- package/dist/server/internal.d.ts +3 -0
- package/dist/server/logger.d.ts +4 -0
- package/dist/server/render/assemble.d.ts +0 -9
- package/dist/server/render/document.d.ts +4 -0
- package/dist/server/render/element.d.ts +2 -1
- package/dist/server/render/index.d.ts +1 -1
- package/dist/server/render/index.ts +0 -1
- package/dist/server/render/isr.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +1 -1
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-route.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +1 -1
- package/dist/server/render/ssr.d.ts +7 -6
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +24 -0
- package/dist/server/router/hmr.d.ts +16 -8
- package/dist/server/router/patterns.d.ts +6 -0
- package/dist/server/router/plugin.d.ts +4 -3
- package/dist/server/router/schema-merge.d.ts +2 -2
- package/dist/server/router/schemas.d.ts +5 -5
- package/dist/server/router/source-version.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -0
- package/dist/server/sync/index.js +961 -806
- package/dist/server/sync/postgres/migrate.js +3 -2
- package/dist/shared/await.d.ts +6 -7
- package/dist/shared/elysia-contract.d.ts +41 -0
- package/dist/shared/route-map.d.ts +5 -0
- package/dist/shared/utils/index.d.ts +1 -3
- package/package.json +44 -41
- package/src/adapter/bun.ts +42 -22
- package/src/adapter/package.ts +20 -14
- package/src/adapter/static.ts +4 -3
- package/src/build/client.ts +20 -3
- package/src/build/entry-template.ts +17 -3
- package/src/build/hydrate.ts +98 -28
- package/src/build/index.ts +2 -2
- package/src/build/route-types.ts +19 -114
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +2 -5
- package/src/build/types.ts +1 -1
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +7 -7
- package/src/client/boundaries.tsx +9 -1
- package/src/client/define-route.ts +48 -0
- package/src/client/document.tsx +165 -0
- package/src/client/hmr.ts +40 -0
- package/src/client/internal/runtime-types.ts +3 -1
- package/src/client/link.tsx +21 -6
- package/src/client/router/index.ts +1 -0
- package/src/client/router/link-utils.ts +26 -0
- package/src/client/router/provider.tsx +25 -2
- package/src/client/router/search/index.ts +4 -12
- package/src/client/router/types.ts +50 -24
- package/src/client.ts +14 -316
- package/src/config.ts +10 -10
- package/src/define-route.ts +959 -0
- package/src/devtools/devtools-element.js +11 -10
- package/src/furin.ts +303 -53
- package/src/plugin/route-config-autofix.ts +1140 -0
- package/src/plugin/routes.ts +891 -0
- package/src/plugin/transform-client.ts +132 -266
- package/src/plugin/transform-isomorphic.ts +1 -1
- package/src/routes.ts +6 -0
- package/src/rsc/build/index.ts +1 -1
- package/src/server/auto-invalidate/registry.ts +2 -2
- package/src/server/dev-page-plugin.ts +31 -12
- package/src/server/devtools/instrumentation.production.ts +2 -2
- package/src/server/devtools/instrumentation.ts +2 -2
- package/src/server/devtools/plugin.ts +5 -3
- package/src/server/instance.ts +3 -3
- package/src/server/internal.ts +3 -0
- package/src/server/logger.ts +21 -0
- package/src/server/render/assemble.ts +0 -41
- package/src/server/render/document.tsx +25 -0
- package/src/server/render/element.tsx +13 -1
- package/src/server/render/index.ts +0 -1
- package/src/server/render/isr.ts +31 -10
- package/src/server/render/loaders.ts +31 -6
- package/src/server/render/not-found.ts +51 -27
- package/src/server/render/ppr-route.ts +1 -1
- package/src/server/render/ssg.ts +1 -1
- package/src/server/render/ssr.ts +271 -113
- package/src/server/render/template.ts +91 -1
- package/src/server/router/defined-route.ts +110 -0
- package/src/server/router/discovery.ts +129 -29
- package/src/server/router/hmr.ts +111 -40
- package/src/server/router/patterns.ts +40 -12
- package/src/server/router/plugin.ts +63 -46
- package/src/server/router/schema-merge.ts +4 -14
- package/src/server/router/schemas.ts +11 -8
- package/src/server/router/source-version.ts +16 -0
- package/src/server/router/types.ts +2 -0
- package/src/server/sync/postgres/migrate.ts +3 -2
- package/src/server/sync/response.ts +14 -10
- package/src/shared/await.tsx +6 -7
- package/src/shared/elysia-contract.ts +63 -0
- package/src/shared/route-map.ts +37 -0
- package/src/shared/utils/index.ts +2 -20
- package/dist/build/server-routes-entry.d.ts +0 -13
- package/dist/build/server-routes-entry.ts +0 -30
- package/dist/server/router/index.d.ts +0 -7
- package/dist/server/router/index.js +0 -27
- package/dist/server/router/index.ts +0 -28
- package/src/build/server-routes-entry.ts +0 -30
- package/src/server/router/index.ts +0 -28
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// ── HTML template state (per furin instance) ────────────────────────────────
|
|
2
2
|
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
|
+
import type { DocumentAssets } from "../../client/document.tsx";
|
|
4
5
|
import { injectInstrumentationClient } from "../devtools/instrumentation.ts";
|
|
5
6
|
import {
|
|
6
7
|
allStateBuckets,
|
|
@@ -11,16 +12,71 @@ import {
|
|
|
11
12
|
} from "../instance.ts";
|
|
12
13
|
|
|
13
14
|
interface TemplateState {
|
|
15
|
+
devAssets: { assets: DocumentAssets; ts: number } | null;
|
|
14
16
|
devCache: { html: string; ts: number } | null;
|
|
17
|
+
prodAssets: DocumentAssets | null;
|
|
15
18
|
prodContent: string | null;
|
|
16
19
|
prodPath: string | null;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
const instanceTemplateState = instanceSlot(
|
|
20
|
-
(): TemplateState => ({
|
|
23
|
+
(): TemplateState => ({
|
|
24
|
+
devAssets: null,
|
|
25
|
+
devCache: null,
|
|
26
|
+
prodAssets: null,
|
|
27
|
+
prodContent: null,
|
|
28
|
+
prodPath: null,
|
|
29
|
+
})
|
|
21
30
|
);
|
|
22
31
|
|
|
23
32
|
const DEV_TEMPLATE_TTL_MS = 1000;
|
|
33
|
+
const TAG_ATTRIBUTE_PATTERN = /([A-Za-z][\w:-]*)="([^"]*)"/g;
|
|
34
|
+
const BUILD_ID_META_PATTERN = /<meta\s+name="furin-build-id"\s+content="([^"]+)"\s*>/;
|
|
35
|
+
|
|
36
|
+
function attributesOf(tag: string): Map<string, string> {
|
|
37
|
+
const attributes = new Map<string, string>();
|
|
38
|
+
for (const match of tag.matchAll(TAG_ATTRIBUTE_PATTERN)) {
|
|
39
|
+
const [, name, value] = match;
|
|
40
|
+
if (name !== undefined && value !== undefined) {
|
|
41
|
+
attributes.set(name.toLowerCase(), value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return attributes;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function documentAssetsFromTemplate(template: string): DocumentAssets {
|
|
48
|
+
const stylesheets: string[] = [];
|
|
49
|
+
let faviconHref: string | undefined;
|
|
50
|
+
for (const tag of template.match(/<link\b[^>]*>/g) ?? []) {
|
|
51
|
+
const attributes = attributesOf(tag);
|
|
52
|
+
const rel = attributes.get("rel");
|
|
53
|
+
const href = attributes.get("href");
|
|
54
|
+
if (rel === "stylesheet" && href !== undefined) {
|
|
55
|
+
stylesheets.push(href);
|
|
56
|
+
} else if (rel === "icon") {
|
|
57
|
+
faviconHref = href;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let entryModule: string | undefined;
|
|
62
|
+
for (const tag of template.match(/<script\b[^>]*>/g) ?? []) {
|
|
63
|
+
const attributes = attributesOf(tag);
|
|
64
|
+
if (attributes.get("type") === "module" && attributes.has("src")) {
|
|
65
|
+
entryModule = attributes.get("src");
|
|
66
|
+
if (tag.includes("data-bun-dev-server-script")) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
buildId: template.match(BUILD_ID_META_PATTERN)?.[1],
|
|
74
|
+
entryModule,
|
|
75
|
+
faviconHref,
|
|
76
|
+
staticMode: template.includes('<meta name="furin-mode" content="static">'),
|
|
77
|
+
stylesheets,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
24
80
|
|
|
25
81
|
export async function getDevTemplate(origin: string): Promise<string> {
|
|
26
82
|
const instance = currentInstance();
|
|
@@ -39,9 +95,20 @@ export async function getDevTemplate(origin: string): Promise<string> {
|
|
|
39
95
|
return html;
|
|
40
96
|
}
|
|
41
97
|
|
|
98
|
+
export async function getDevDocumentAssets(origin: string): Promise<DocumentAssets> {
|
|
99
|
+
const state = instanceTemplateState();
|
|
100
|
+
if (state.devAssets && Date.now() - state.devAssets.ts < DEV_TEMPLATE_TTL_MS) {
|
|
101
|
+
return state.devAssets.assets;
|
|
102
|
+
}
|
|
103
|
+
const assets = documentAssetsFromTemplate(await getDevTemplate(origin));
|
|
104
|
+
state.devAssets = { assets, ts: Date.now() };
|
|
105
|
+
return assets;
|
|
106
|
+
}
|
|
107
|
+
|
|
42
108
|
export function setProductionTemplatePath(path: string | null, instance?: FurinInstance): void {
|
|
43
109
|
const state = instanceTemplateState(instance);
|
|
44
110
|
state.prodPath = path;
|
|
111
|
+
state.prodAssets = null;
|
|
45
112
|
state.prodContent = null;
|
|
46
113
|
}
|
|
47
114
|
|
|
@@ -49,6 +116,15 @@ export function setProductionTemplateContent(content: string, instance?: FurinIn
|
|
|
49
116
|
const state = instanceTemplateState(instance);
|
|
50
117
|
state.prodPath = null;
|
|
51
118
|
state.prodContent = content;
|
|
119
|
+
state.prodAssets = documentAssetsFromTemplate(content);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function getProductionDocumentAssets(): DocumentAssets | null {
|
|
123
|
+
const own = readDocumentAssets(instanceTemplateState());
|
|
124
|
+
if (own !== null) {
|
|
125
|
+
return own;
|
|
126
|
+
}
|
|
127
|
+
return readDocumentAssets(instanceTemplateState(defaultInstanceBucket()));
|
|
52
128
|
}
|
|
53
129
|
|
|
54
130
|
export function getProductionTemplate(): string | null {
|
|
@@ -78,11 +154,25 @@ function readTemplate(state: TemplateState): string | null {
|
|
|
78
154
|
}
|
|
79
155
|
}
|
|
80
156
|
|
|
157
|
+
function readDocumentAssets(state: TemplateState): DocumentAssets | null {
|
|
158
|
+
if (state.prodAssets !== null) {
|
|
159
|
+
return state.prodAssets;
|
|
160
|
+
}
|
|
161
|
+
const template = readTemplate(state);
|
|
162
|
+
if (template === null) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
state.prodAssets = documentAssetsFromTemplate(template);
|
|
166
|
+
return state.prodAssets;
|
|
167
|
+
}
|
|
168
|
+
|
|
81
169
|
/** @internal test-only — resets all template state */
|
|
82
170
|
export function __resetTemplateState(): void {
|
|
83
171
|
for (const instance of allStateBuckets()) {
|
|
84
172
|
const state = instanceTemplateState(instance);
|
|
173
|
+
state.devAssets = null;
|
|
85
174
|
state.devCache = null;
|
|
175
|
+
state.prodAssets = null;
|
|
86
176
|
state.prodContent = null;
|
|
87
177
|
state.prodPath = null;
|
|
88
178
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RuntimeData,
|
|
3
|
+
RuntimePage,
|
|
4
|
+
RuntimeRoute,
|
|
5
|
+
} from "../../client/internal/runtime-types.ts";
|
|
6
|
+
import type { HeadOptions, RenderingMode } from "../../client.ts";
|
|
7
|
+
|
|
8
|
+
interface DefinedRouteTerminal {
|
|
9
|
+
__type: "FURIN_ROUTE";
|
|
10
|
+
component: (...args: never[]) => React.ReactNode;
|
|
11
|
+
elysia: object;
|
|
12
|
+
head?: (...args: never[]) => HeadOptions;
|
|
13
|
+
layout?: (...args: never[]) => React.ReactNode;
|
|
14
|
+
loader?: (...args: never[]) => unknown;
|
|
15
|
+
mode?: RenderingMode;
|
|
16
|
+
page?: (...args: never[]) => React.ReactNode;
|
|
17
|
+
requestLoader?: (...args: never[]) => unknown;
|
|
18
|
+
revalidate?: number;
|
|
19
|
+
schemas?: { params?: unknown; query?: unknown };
|
|
20
|
+
staticParams?: () => Promise<readonly unknown[]> | readonly unknown[];
|
|
21
|
+
tags?: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface DefinedRenderContext {
|
|
25
|
+
children?: React.ReactNode;
|
|
26
|
+
data: RuntimeData;
|
|
27
|
+
params: unknown;
|
|
28
|
+
path: string;
|
|
29
|
+
query: unknown;
|
|
30
|
+
requestData?: Promise<object>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function toDefinedRenderContext(props: RuntimeData): DefinedRenderContext {
|
|
34
|
+
const {
|
|
35
|
+
children,
|
|
36
|
+
params = {},
|
|
37
|
+
path: _path,
|
|
38
|
+
query = {},
|
|
39
|
+
requestData,
|
|
40
|
+
...data
|
|
41
|
+
} = props as RuntimeData & { children?: React.ReactNode };
|
|
42
|
+
return {
|
|
43
|
+
children,
|
|
44
|
+
data,
|
|
45
|
+
params,
|
|
46
|
+
path: (_path as string | undefined) ?? "",
|
|
47
|
+
query,
|
|
48
|
+
requestData: requestData as Promise<object> | undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function copyTags(tags: readonly string[] | undefined): string[] | undefined {
|
|
53
|
+
return tags ? [...tags] : undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isDefinedRouteTerminal(value: unknown): value is DefinedRouteTerminal {
|
|
57
|
+
if (typeof value !== "object" || value === null) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const candidate = value as Partial<DefinedRouteTerminal>;
|
|
61
|
+
return (
|
|
62
|
+
candidate.__type === "FURIN_ROUTE" &&
|
|
63
|
+
typeof candidate.component === "function" &&
|
|
64
|
+
typeof candidate.elysia === "object" &&
|
|
65
|
+
candidate.elysia !== null
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function adaptDefinedLayout(
|
|
70
|
+
route: DefinedRouteTerminal,
|
|
71
|
+
parent: RuntimeRoute | undefined,
|
|
72
|
+
sourcePath?: string
|
|
73
|
+
): RuntimeRoute {
|
|
74
|
+
const component = route.component as (props: DefinedRenderContext) => React.ReactNode;
|
|
75
|
+
return {
|
|
76
|
+
__type: "FURIN_ROUTE",
|
|
77
|
+
layout: (props) => component(toDefinedRenderContext(props)),
|
|
78
|
+
loader: route.loader as RuntimeRoute["loader"],
|
|
79
|
+
mode: route.mode,
|
|
80
|
+
params: route.schemas?.params,
|
|
81
|
+
...(parent ? { parent } : {}),
|
|
82
|
+
query: route.schemas?.query,
|
|
83
|
+
requestLoader: route.requestLoader as RuntimeRoute["requestLoader"],
|
|
84
|
+
revalidate: route.revalidate,
|
|
85
|
+
sourcePath,
|
|
86
|
+
tags: copyTags(route.tags),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function adaptDefinedPage(route: DefinedRouteTerminal, parent: RuntimeRoute): RuntimePage {
|
|
91
|
+
const component = route.component as (props: DefinedRenderContext) => React.ReactNode;
|
|
92
|
+
const head = route.head as ((context: DefinedRenderContext) => HeadOptions) | undefined;
|
|
93
|
+
return {
|
|
94
|
+
__type: "FURIN_PAGE",
|
|
95
|
+
_route: {
|
|
96
|
+
__type: "FURIN_ROUTE",
|
|
97
|
+
params: route.schemas?.params,
|
|
98
|
+
parent,
|
|
99
|
+
query: route.schemas?.query,
|
|
100
|
+
requestLoader: route.requestLoader as RuntimeRoute["requestLoader"],
|
|
101
|
+
},
|
|
102
|
+
component: (props) => component(toDefinedRenderContext(props)),
|
|
103
|
+
head: head ? (props) => head(toDefinedRenderContext(props)) : undefined,
|
|
104
|
+
loader: route.loader as RuntimePage["loader"],
|
|
105
|
+
mode: route.mode,
|
|
106
|
+
revalidate: route.revalidate,
|
|
107
|
+
staticParams: route.staticParams as RuntimePage["staticParams"],
|
|
108
|
+
tags: copyTags(route.tags),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -7,14 +7,14 @@ import type { ErrorComponent } from "../../shared/error.ts";
|
|
|
7
7
|
import type { NotFoundComponent } from "../../shared/not-found.ts";
|
|
8
8
|
import {
|
|
9
9
|
collectRouteChainFromRoute,
|
|
10
|
-
isFurinPage,
|
|
11
|
-
isFurinRoute,
|
|
12
10
|
mapWithConcurrency,
|
|
13
11
|
validateRouteChain,
|
|
14
12
|
} from "../../shared/utils/index.ts";
|
|
15
13
|
import { type CompileContext, getCompileContext } from "../internal.ts";
|
|
16
14
|
import { IS_DEV } from "../runtime-env.ts";
|
|
15
|
+
import { adaptDefinedLayout, adaptDefinedPage, isDefinedRouteTerminal } from "./defined-route.ts";
|
|
17
16
|
import { filePathToPattern, resolveMode } from "./patterns.ts";
|
|
17
|
+
import { routeModuleSourceVersion } from "./source-version.ts";
|
|
18
18
|
import type { ResolvedRoute, RootLayout, SegmentBoundary } from "./types.ts";
|
|
19
19
|
|
|
20
20
|
export function isModuleNotFoundError(err: unknown): boolean {
|
|
@@ -58,10 +58,11 @@ export function loadProdRoutes(ctx: CompileContext): {
|
|
|
58
58
|
routes: ResolvedRoute[];
|
|
59
59
|
} {
|
|
60
60
|
const rootMod = ctx.modules[ctx.rootPath] as Record<string, unknown>;
|
|
61
|
-
const rootExport = rootMod.route
|
|
62
|
-
if (!(rootExport
|
|
63
|
-
throw new Error("[furin] root.tsx:
|
|
61
|
+
const rootExport = rootMod.route;
|
|
62
|
+
if (!isDefinedRouteTerminal(rootExport) || typeof rootExport.layout !== "function") {
|
|
63
|
+
throw new Error("[furin] root.tsx: defineRoute().layout() not found in CompileContext.");
|
|
64
64
|
}
|
|
65
|
+
const rootRoute = adaptDefinedLayout(rootExport, undefined, ctx.rootPath);
|
|
65
66
|
|
|
66
67
|
function resolveModuleComponent<T>(modPath: string | undefined): T | undefined {
|
|
67
68
|
if (!modPath) {
|
|
@@ -75,14 +76,14 @@ export function loadProdRoutes(ctx: CompileContext): {
|
|
|
75
76
|
error: resolveModuleComponent(ctx.rootConventions?.errorPath),
|
|
76
77
|
notFound: resolveModuleComponent(ctx.rootConventions?.notFoundPath),
|
|
77
78
|
path: ctx.rootPath,
|
|
78
|
-
route:
|
|
79
|
+
route: rootRoute,
|
|
79
80
|
};
|
|
80
81
|
|
|
81
82
|
const routes: ResolvedRoute[] = [];
|
|
82
83
|
for (const { pattern, path, mode } of ctx.routes) {
|
|
83
|
-
const pageMod = ctx.modules[path] as {
|
|
84
|
-
const page
|
|
85
|
-
if (!
|
|
84
|
+
const pageMod = ctx.modules[path] as { route?: unknown };
|
|
85
|
+
const page = resolveCompiledRuntimePage(pageMod, path, root, ctx);
|
|
86
|
+
if (!page) {
|
|
86
87
|
throw new Error(`[furin] ${path}: invalid page module in CompileContext.`);
|
|
87
88
|
}
|
|
88
89
|
const routeChain = collectRouteChainFromRoute(page._route as RuntimeRoute);
|
|
@@ -123,6 +124,52 @@ export function loadProdRoutes(ctx: CompileContext): {
|
|
|
123
124
|
return { root, routes };
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
function resolveCompiledRuntimePage(
|
|
128
|
+
module: { route?: unknown },
|
|
129
|
+
path: string,
|
|
130
|
+
root: RootLayout,
|
|
131
|
+
context: CompileContext
|
|
132
|
+
): RuntimePage | undefined {
|
|
133
|
+
if (!isDefinedRouteTerminal(module.route) || typeof module.route.page !== "function") {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const pagesDir = root.path.slice(0, root.path.lastIndexOf("/"));
|
|
137
|
+
const parent = resolveCompiledLayoutParent(path, pagesDir, root.route, context);
|
|
138
|
+
return adaptDefinedPage(module.route, parent);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function resolveCompiledLayoutParent(
|
|
142
|
+
pagePath: string,
|
|
143
|
+
pagesDir: string,
|
|
144
|
+
rootRoute: RuntimeRoute,
|
|
145
|
+
context: CompileContext
|
|
146
|
+
): RuntimeRoute {
|
|
147
|
+
const pageDirectory = pagePath.slice(0, pagePath.lastIndexOf("/"));
|
|
148
|
+
if (pageDirectory.length <= pagesDir.length) {
|
|
149
|
+
return rootRoute;
|
|
150
|
+
}
|
|
151
|
+
const segments = pageDirectory.slice(pagesDir.length + 1).split("/");
|
|
152
|
+
let directory = pagesDir;
|
|
153
|
+
let parent = rootRoute;
|
|
154
|
+
|
|
155
|
+
for (const segment of segments) {
|
|
156
|
+
directory = `${directory}/${segment}`;
|
|
157
|
+
for (const candidate of getSourceModuleCandidates(directory, "_route")) {
|
|
158
|
+
const layoutModule = context.modules[candidate] as { route?: unknown } | undefined;
|
|
159
|
+
if (!layoutModule) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const layout = layoutModule.route;
|
|
163
|
+
if (isDefinedRouteTerminal(layout) && typeof layout.layout === "function") {
|
|
164
|
+
parent = adaptDefinedLayout(layout, parent, candidate);
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return parent;
|
|
171
|
+
}
|
|
172
|
+
|
|
126
173
|
/**
|
|
127
174
|
* Normalises OS-native path separators to POSIX "/" so the slash-based path
|
|
128
175
|
* arithmetic in the scan (relative slicing, `lastIndexOf("/")`,
|
|
@@ -161,14 +208,15 @@ export async function scanRootLayout(pagesDir: string): Promise<RootLayout> {
|
|
|
161
208
|
}
|
|
162
209
|
|
|
163
210
|
const mod = (ctx?.modules[rootPath] ?? (await import(rootPath))) as Record<string, unknown>;
|
|
164
|
-
const rootExport = mod.route
|
|
165
|
-
if (!(rootExport
|
|
166
|
-
throw new Error("[furin] root.tsx:
|
|
211
|
+
const rootExport = mod.route;
|
|
212
|
+
if (!isDefinedRouteTerminal(rootExport)) {
|
|
213
|
+
throw new Error("[furin] root.tsx: defineRoute() export not found.");
|
|
167
214
|
}
|
|
168
215
|
|
|
169
216
|
if (!rootExport.layout) {
|
|
170
|
-
throw new Error("[furin] root.tsx:
|
|
217
|
+
throw new Error("[furin] root.tsx: defineRoute() has no layout.");
|
|
171
218
|
}
|
|
219
|
+
const rootRoute = adaptDefinedLayout(rootExport, undefined, rootPath);
|
|
172
220
|
|
|
173
221
|
const [notFoundEntry, errorEntry] = await Promise.all([
|
|
174
222
|
loadConventionComponent<NotFoundComponent>(pagesDir, "not-found"),
|
|
@@ -180,7 +228,7 @@ export async function scanRootLayout(pagesDir: string): Promise<RootLayout> {
|
|
|
180
228
|
notFound: notFoundEntry?.component,
|
|
181
229
|
notFoundPath: notFoundEntry?.path,
|
|
182
230
|
path: rootPath,
|
|
183
|
-
route:
|
|
231
|
+
route: rootRoute,
|
|
184
232
|
};
|
|
185
233
|
}
|
|
186
234
|
|
|
@@ -217,7 +265,11 @@ async function loadConventionComponent<T>(
|
|
|
217
265
|
const ctx = getCompileContext();
|
|
218
266
|
for (const filePath of getSourceModuleCandidates(dir, name)) {
|
|
219
267
|
if (existsSync(filePath) || ctx?.modules[filePath]) {
|
|
220
|
-
const
|
|
268
|
+
const moduleSpecifier =
|
|
269
|
+
IS_DEV && existsSync(filePath)
|
|
270
|
+
? `${filePath}?furin-server&t=${routeModuleSourceVersion(filePath)}`
|
|
271
|
+
: filePath;
|
|
272
|
+
const mod = (ctx?.modules[filePath] ?? (await import(moduleSpecifier))) as {
|
|
221
273
|
default?: T;
|
|
222
274
|
};
|
|
223
275
|
if (mod.default) {
|
|
@@ -277,7 +329,7 @@ async function scanPageFiles(pagesDir: string, root: RootLayout): Promise<Resolv
|
|
|
277
329
|
]);
|
|
278
330
|
|
|
279
331
|
if (IS_DEV) {
|
|
280
|
-
const devRoute = await buildDevRoute(absolutePath, relativePath, pattern, root);
|
|
332
|
+
const devRoute = await buildDevRoute(absolutePath, relativePath, pattern, root, pagesDir);
|
|
281
333
|
devRoute.notFound = notFound;
|
|
282
334
|
devRoute.error = errorComponent;
|
|
283
335
|
devRoute.segmentBoundaries = segmentBoundaries;
|
|
@@ -287,11 +339,11 @@ async function scanPageFiles(pagesDir: string, root: RootLayout): Promise<Resolv
|
|
|
287
339
|
|
|
288
340
|
const ctx = getCompileContext();
|
|
289
341
|
const pageMod = (ctx?.modules[absolutePath] ?? (await import(absolutePath))) as {
|
|
290
|
-
|
|
342
|
+
route?: unknown;
|
|
291
343
|
};
|
|
292
|
-
const page
|
|
293
|
-
if (!
|
|
294
|
-
throw new Error(`[furin] ${relativePath}: no valid
|
|
344
|
+
const page = await resolveRuntimePage(pageMod, absolutePath, pagesDir, root.route);
|
|
345
|
+
if (!page) {
|
|
346
|
+
throw new Error(`[furin] ${relativePath}: no valid defineRoute().page() export found`);
|
|
295
347
|
}
|
|
296
348
|
|
|
297
349
|
const routeChain = collectRouteChainFromRoute(page._route as RuntimeRoute);
|
|
@@ -314,6 +366,51 @@ async function scanPageFiles(pagesDir: string, root: RootLayout): Promise<Resolv
|
|
|
314
366
|
return routes;
|
|
315
367
|
}
|
|
316
368
|
|
|
369
|
+
async function resolveRuntimePage(
|
|
370
|
+
module: { route?: unknown },
|
|
371
|
+
absolutePath: string,
|
|
372
|
+
pagesDir: string,
|
|
373
|
+
rootRoute: RuntimeRoute
|
|
374
|
+
): Promise<RuntimePage | undefined> {
|
|
375
|
+
if (!isDefinedRouteTerminal(module.route) || typeof module.route.page !== "function") {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const parent = await resolveDefinedLayoutParent(absolutePath, pagesDir, rootRoute);
|
|
379
|
+
return adaptDefinedPage(module.route, parent);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function resolveDefinedLayoutParent(
|
|
383
|
+
pagePath: string,
|
|
384
|
+
pagesDir: string,
|
|
385
|
+
rootRoute: RuntimeRoute
|
|
386
|
+
): Promise<RuntimeRoute> {
|
|
387
|
+
const pageDirectory = pagePath.slice(0, pagePath.lastIndexOf("/"));
|
|
388
|
+
if (pageDirectory.length <= pagesDir.length) {
|
|
389
|
+
return rootRoute;
|
|
390
|
+
}
|
|
391
|
+
const relativeDirectory = pageDirectory.slice(pagesDir.length + 1);
|
|
392
|
+
const segments = relativeDirectory.split("/");
|
|
393
|
+
let directory = pagesDir;
|
|
394
|
+
let parent = rootRoute;
|
|
395
|
+
|
|
396
|
+
for (const segment of segments) {
|
|
397
|
+
directory = `${directory}/${segment}`;
|
|
398
|
+
for (const candidate of getSourceModuleCandidates(directory, "_route")) {
|
|
399
|
+
if (!existsSync(candidate)) {
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const layoutModule = (await import(candidate)) as { route?: unknown };
|
|
403
|
+
const layout = layoutModule.route;
|
|
404
|
+
if (isDefinedRouteTerminal(layout) && typeof layout.layout === "function") {
|
|
405
|
+
parent = adaptDefinedLayout(layout, parent, candidate);
|
|
406
|
+
}
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return parent;
|
|
412
|
+
}
|
|
413
|
+
|
|
317
414
|
/**
|
|
318
415
|
* Walks every directory from `pagesDir` down to the directory containing
|
|
319
416
|
* `pageAbsolutePath` and records the OWN (not inherited) error.tsx /
|
|
@@ -407,7 +504,8 @@ async function buildDevRoute(
|
|
|
407
504
|
absolutePath: string,
|
|
408
505
|
relativePath: string,
|
|
409
506
|
pattern: string,
|
|
410
|
-
root: RootLayout
|
|
507
|
+
root: RootLayout,
|
|
508
|
+
pagesDir: string
|
|
411
509
|
): Promise<ResolvedRoute> {
|
|
412
510
|
// Import via the virtual namespace (registerDevPagePlugin must be called first)
|
|
413
511
|
// so page files stay out of --hot's module graph. We still extract the route
|
|
@@ -417,20 +515,22 @@ async function buildDevRoute(
|
|
|
417
515
|
let routeChain: RuntimeRoute[] = [];
|
|
418
516
|
|
|
419
517
|
try {
|
|
420
|
-
const pageMod = (await import(
|
|
421
|
-
|
|
518
|
+
const pageMod = (await import(
|
|
519
|
+
`${absolutePath}?furin-server&t=${routeModuleSourceVersion(absolutePath)}`
|
|
520
|
+
)) as {
|
|
521
|
+
route?: unknown;
|
|
422
522
|
};
|
|
423
|
-
|
|
424
|
-
|
|
523
|
+
const resolvedPage = await resolveRuntimePage(pageMod, absolutePath, pagesDir, root.route);
|
|
524
|
+
if (resolvedPage) {
|
|
525
|
+
page = resolvedPage;
|
|
425
526
|
routeChain = collectRouteChainFromRoute(page._route as RuntimeRoute);
|
|
426
527
|
validateRouteChain(routeChain, root.route, relativePath);
|
|
427
528
|
}
|
|
428
529
|
} catch {
|
|
429
|
-
//
|
|
530
|
+
// The dev renderer retries the named terminal on the first request.
|
|
430
531
|
}
|
|
431
532
|
|
|
432
|
-
// Dev stub:
|
|
433
|
-
// actually rendered — createRoutePlugin always re-imports from disk in dev mode.
|
|
533
|
+
// Dev stub: never rendered; the dev renderer re-imports from disk.
|
|
434
534
|
const devStubPage: RuntimePage = {
|
|
435
535
|
__type: "FURIN_PAGE",
|
|
436
536
|
_route: { __type: "FURIN_ROUTE" },
|
|
@@ -439,7 +539,7 @@ async function buildDevRoute(
|
|
|
439
539
|
|
|
440
540
|
return {
|
|
441
541
|
mode: page ? resolveMode(page, routeChain) : "ssr",
|
|
442
|
-
// Still lazily re-imported on each request
|
|
542
|
+
// Still lazily re-imported on each request for fresh code.
|
|
443
543
|
page: page ?? devStubPage,
|
|
444
544
|
path: absolutePath,
|
|
445
545
|
pattern,
|