@sigil-dev/grimoire 0.7.6 → 0.7.7
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/index.ts +35 -34
- package/package.json +8 -6
- package/preload.js +3 -2
- package/server.ts +13 -13
- package/src/client/head.ts +29 -29
- package/src/client/router.ts +290 -224
- package/src/dev/compile-module.ts +173 -0
- package/src/dev/effect-registry.ts +23 -0
- package/src/dev/graph.ts +114 -0
- package/src/dev/hmr-client.ts +158 -0
- package/src/dev/hmr-server.ts +187 -0
- package/src/dev/loader.ts +47 -0
- package/src/dev/paths.ts +14 -0
- package/src/dev/runtime-bundle.ts +49 -0
- package/src/dev/watcher.ts +44 -0
- package/src/integrations/vite.ts +73 -72
- package/src/rendering/hydrate.ts +120 -81
- package/src/rendering/index.ts +296 -199
- package/src/rendering/ssrPlugin.ts +67 -53
- package/src/routing/manifest-gen.ts +42 -39
- package/src/routing/router.ts +109 -106
- package/src/routing/scanner.ts +141 -135
- package/src/routing/transform-routes.ts +101 -101
- package/src/server/build.ts +239 -147
- package/src/server/coordinator.ts +306 -306
- package/src/server/index.ts +260 -50
- package/src/server/worker.ts +59 -59
- package/src/typegen/index.ts +356 -353
- package/src/types.ts +270 -269
- package/test/context.test.ts +52 -52
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/rendering.test.ts +579 -425
- package/test/routing.test.ts +81 -83
- package/test/scanning.test.ts +200 -181
- package/test/scope.test.ts +24 -8
- package/test/server.test.ts +249 -229
- package/test/streaming.test.ts +125 -106
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +35 -25
- package/tsconfig.json +1 -0
package/src/client/router.ts
CHANGED
|
@@ -1,224 +1,290 @@
|
|
|
1
|
-
import { popHydrationNodes, pushHydrationNodes } from "@sigil-dev/runtime";
|
|
2
|
-
import { withEffectScope } from "./scope";
|
|
3
|
-
|
|
4
|
-
let routeMap: Record<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
1
|
+
import { popHydrationNodes, pushHydrationNodes } from "@sigil-dev/runtime";
|
|
2
|
+
import { withEffectScope } from "./scope";
|
|
3
|
+
|
|
4
|
+
let routeMap: Record<
|
|
5
|
+
string,
|
|
6
|
+
((props: any) => any) & { load?: (ctx: any) => Promise<any> }
|
|
7
|
+
> = {};
|
|
8
|
+
let layoutList: { path: string; component: (props: any) => any }[] = [];
|
|
9
|
+
let disposeCurrentPage: (() => void) | null = null;
|
|
10
|
+
let currentPath = location.pathname;
|
|
11
|
+
|
|
12
|
+
// C1: Navigation hooks
|
|
13
|
+
let beforeNavigateCb: ((url: URL) => boolean | void) | null = null;
|
|
14
|
+
let onNavigateCb: ((url: URL) => void) | null = null;
|
|
15
|
+
let afterNavigateCb: ((url: URL) => void) | null = null;
|
|
16
|
+
|
|
17
|
+
// // HMR
|
|
18
|
+
// (globalThis as any).__grimoire_routes__ = routeMap;
|
|
19
|
+
// (globalThis as any).__grimoire_current_pattern__ = currentPath;
|
|
20
|
+
// (globalThis as any).__grimoire_navigate__ = navigate;
|
|
21
|
+
|
|
22
|
+
export async function hmrRerender(): Promise<void> {
|
|
23
|
+
if (!lastState) {
|
|
24
|
+
location.reload();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
disposeCurrentPage?.();
|
|
28
|
+
pushHydrationNodes([]);
|
|
29
|
+
let rootNode: any;
|
|
30
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
31
|
+
rootNode = buildRouteTree(lastState!);
|
|
32
|
+
});
|
|
33
|
+
popHydrationNodes();
|
|
34
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// (globalThis as any).__grimoire_rerender__ = hmrRerender;
|
|
38
|
+
|
|
39
|
+
export function beforeNavigate(cb: (url: URL) => boolean | void): void {
|
|
40
|
+
beforeNavigateCb = cb;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function onNavigate(cb: (url: URL) => void): void {
|
|
44
|
+
onNavigateCb = cb;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function afterNavigate(cb: (url: URL) => void): void {
|
|
48
|
+
afterNavigateCb = cb;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// C2: Scroll restoration
|
|
52
|
+
const scrollPositions = new Map<string, number>();
|
|
53
|
+
|
|
54
|
+
function saveScrollPosition(): void {
|
|
55
|
+
scrollPositions.set(currentPath, window.scrollY);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function restoreScrollPosition(path: string): void {
|
|
59
|
+
const saved = scrollPositions.get(path);
|
|
60
|
+
if (saved !== undefined) {
|
|
61
|
+
requestAnimationFrame(() => window.scrollTo(0, saved));
|
|
62
|
+
} else {
|
|
63
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// C3: Preloading
|
|
68
|
+
let preloadTimer: Timer | null = null;
|
|
69
|
+
|
|
70
|
+
function setupPreload(): void {
|
|
71
|
+
document.addEventListener("mouseover", (e) => {
|
|
72
|
+
const a = (e.target as Element).closest("a");
|
|
73
|
+
if (!a) return;
|
|
74
|
+
const href = a.getAttribute("href");
|
|
75
|
+
if (!href) return;
|
|
76
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
77
|
+
|
|
78
|
+
const preloadData = a.hasAttribute("data-sigil-preload-data");
|
|
79
|
+
const preloadCode = a.hasAttribute("data-sigil-preload-code");
|
|
80
|
+
if (!preloadData && !preloadCode) return;
|
|
81
|
+
|
|
82
|
+
if (preloadTimer) clearTimeout(preloadTimer);
|
|
83
|
+
preloadTimer = setTimeout(() => {
|
|
84
|
+
if (preloadCode) {
|
|
85
|
+
const link = document.createElement("link");
|
|
86
|
+
link.rel = "prefetch";
|
|
87
|
+
link.href = href;
|
|
88
|
+
document.head.appendChild(link);
|
|
89
|
+
}
|
|
90
|
+
if (preloadData) {
|
|
91
|
+
fetch(href, {
|
|
92
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
93
|
+
priority: "low",
|
|
94
|
+
}).catch(() => {});
|
|
95
|
+
}
|
|
96
|
+
}, 80);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
document.addEventListener("mouseout", (e) => {
|
|
100
|
+
const a = (e.target as Element).closest("a");
|
|
101
|
+
if (!a) return;
|
|
102
|
+
if (preloadTimer) clearTimeout(preloadTimer);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let lastState: {
|
|
107
|
+
data: any;
|
|
108
|
+
layoutData: any[];
|
|
109
|
+
params: any;
|
|
110
|
+
pattern: string;
|
|
111
|
+
} | null = null;
|
|
112
|
+
|
|
113
|
+
function buildRouteTree(state: typeof lastState): any {
|
|
114
|
+
const { data, layoutData, params, pattern } = state!;
|
|
115
|
+
const Page = routeMap[pattern];
|
|
116
|
+
if (!Page) throw new Error(`no component for pattern ${pattern}`);
|
|
117
|
+
|
|
118
|
+
const matchedLayouts = layoutList
|
|
119
|
+
.filter(
|
|
120
|
+
(l) =>
|
|
121
|
+
l.path === "/" ||
|
|
122
|
+
pattern === l.path ||
|
|
123
|
+
pattern.startsWith(l.path + "/"),
|
|
124
|
+
)
|
|
125
|
+
.sort((a, b) => a.path.length - b.path.length);
|
|
126
|
+
|
|
127
|
+
let renderFn = () => {
|
|
128
|
+
const pageNode = Page({ data, params });
|
|
129
|
+
const pageDiv = document.createElement("div");
|
|
130
|
+
pageDiv.id = "grimoire-page";
|
|
131
|
+
pageDiv.appendChild(pageNode);
|
|
132
|
+
return pageDiv;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
136
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
137
|
+
const innerRender = renderFn;
|
|
138
|
+
const currentLayoutData = layoutData?.[i];
|
|
139
|
+
renderFn = () => {
|
|
140
|
+
const childNode = innerRender();
|
|
141
|
+
return LayoutComponent({
|
|
142
|
+
data: currentLayoutData,
|
|
143
|
+
params,
|
|
144
|
+
children: childNode,
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return renderFn();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function navigate(path: string) {
|
|
153
|
+
const url = new URL(path, location.origin);
|
|
154
|
+
|
|
155
|
+
if (beforeNavigateCb) {
|
|
156
|
+
const shouldProceed = beforeNavigateCb(url);
|
|
157
|
+
if (shouldProceed === false) return;
|
|
158
|
+
}
|
|
159
|
+
onNavigateCb?.(url);
|
|
160
|
+
saveScrollPosition();
|
|
161
|
+
|
|
162
|
+
const res = await fetch(path, { headers: { "x-grimoire-navigate": "1" } });
|
|
163
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
164
|
+
if (!contentType.includes("application/json")) {
|
|
165
|
+
window.location.href = path;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const json = await res.json();
|
|
169
|
+
const { data, layoutData, params, pattern, head } = json;
|
|
170
|
+
const Page = routeMap[pattern];
|
|
171
|
+
if (!Page) {
|
|
172
|
+
window.location.href = path;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let mergedData = data;
|
|
177
|
+
if (Page.load) {
|
|
178
|
+
try {
|
|
179
|
+
const clientData = await Page.load({
|
|
180
|
+
params,
|
|
181
|
+
url,
|
|
182
|
+
request: new Request(url),
|
|
183
|
+
});
|
|
184
|
+
mergedData = { ...clientData, ...data };
|
|
185
|
+
} catch (e: any) {
|
|
186
|
+
if (e?.location) {
|
|
187
|
+
window.location.href = e.location;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
console.error("[grimoire] universal load error:", e);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// track state for HMR rerender
|
|
195
|
+
lastState = { data: mergedData, layoutData, params, pattern };
|
|
196
|
+
(globalThis as any).__grimoire_current_pattern__ = pattern;
|
|
197
|
+
|
|
198
|
+
disposeCurrentPage?.();
|
|
199
|
+
disposeCurrentPage = null;
|
|
200
|
+
pushHydrationNodes([]);
|
|
201
|
+
let rootNode: any;
|
|
202
|
+
disposeCurrentPage = withEffectScope(() => {
|
|
203
|
+
try {
|
|
204
|
+
rootNode = buildRouteTree(lastState!);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
console.error(e);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
popHydrationNodes();
|
|
210
|
+
|
|
211
|
+
updateHead(head);
|
|
212
|
+
document.title = head?.title ?? document.title;
|
|
213
|
+
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
214
|
+
currentPath = path;
|
|
215
|
+
restoreScrollPosition(path);
|
|
216
|
+
afterNavigateCb?.(url);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function initRouter(
|
|
220
|
+
routes: Record<
|
|
221
|
+
string,
|
|
222
|
+
((props: any) => any) & { load?: (ctx: any) => Promise<any> }
|
|
223
|
+
>,
|
|
224
|
+
layouts: any[],
|
|
225
|
+
initialDispose?: () => void,
|
|
226
|
+
) {
|
|
227
|
+
routeMap = routes;
|
|
228
|
+
layoutList = layouts;
|
|
229
|
+
disposeCurrentPage = initialDispose ?? null;
|
|
230
|
+
currentPath = location.pathname;
|
|
231
|
+
|
|
232
|
+
// set initial HMR state from SSR
|
|
233
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
234
|
+
if (stateEl) {
|
|
235
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
236
|
+
lastState = {
|
|
237
|
+
data: state.data,
|
|
238
|
+
layoutData: state.layoutData,
|
|
239
|
+
params: state.params,
|
|
240
|
+
pattern: state.pattern,
|
|
241
|
+
};
|
|
242
|
+
(globalThis as any).__grimoire_current_pattern__ = state.pattern;
|
|
243
|
+
(globalThis as any).__grimoire_routes__ = routeMap;
|
|
244
|
+
(globalThis as any).__grimoire_rerender__ = hmrRerender;
|
|
245
|
+
(globalThis as any).__grimoire_navigate__ = (path: string) =>
|
|
246
|
+
navigate(path);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
saveScrollPosition();
|
|
250
|
+
document.addEventListener("click", handleClick);
|
|
251
|
+
window.addEventListener("popstate", () => {
|
|
252
|
+
if (location.pathname !== currentPath) {
|
|
253
|
+
saveScrollPosition();
|
|
254
|
+
currentPath = location.pathname;
|
|
255
|
+
navigate(location.pathname);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
setupPreload();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function updateHead(headHtml: string) {
|
|
262
|
+
document
|
|
263
|
+
.querySelectorAll("[data-grimoire-head]")
|
|
264
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
265
|
+
.forEach((el) => el.remove());
|
|
266
|
+
const parsed = headHtml
|
|
267
|
+
? new DOMParser().parseFromString(`<head>${headHtml}</head>`, "text/html")
|
|
268
|
+
: null;
|
|
269
|
+
const incomingTitle = parsed?.querySelector("title");
|
|
270
|
+
//biome-ignore lint/suspicious/useIterableCallbackReturn: shut up
|
|
271
|
+
document.querySelectorAll("title").forEach((el) => el.remove());
|
|
272
|
+
document.title = incomingTitle?.textContent ?? document.title;
|
|
273
|
+
if (parsed) {
|
|
274
|
+
for (const el of Array.from(parsed.head.children)) {
|
|
275
|
+
if (el.tagName === "TITLE") continue;
|
|
276
|
+
(el as HTMLElement).dataset.grimoireHead = "1";
|
|
277
|
+
document.head.appendChild(el);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function handleClick(e: MouseEvent) {
|
|
283
|
+
const a = (e.target as Element).closest("a");
|
|
284
|
+
if (!a) return;
|
|
285
|
+
const href = a.getAttribute("href");
|
|
286
|
+
if (!href) return;
|
|
287
|
+
if (/^(https?:\/\/|\/\/|#|mailto:|tel:)/.test(href)) return;
|
|
288
|
+
e.preventDefault();
|
|
289
|
+
navigate(href);
|
|
290
|
+
}
|