@sigil-dev/grimoire 0.9.4 → 0.9.6
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 +37 -37
- package/package.json +3 -3
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -36
- package/src/client/enhance.ts +102 -102
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +128 -38
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +52 -47
- package/src/rendering/hydrate.ts +16 -3
- package/src/rendering/index.ts +43 -17
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +55 -55
- package/src/routing/router.ts +135 -135
- package/src/routing/scanner.ts +196 -187
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +250 -255
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +1 -1
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +110 -69
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -389
- package/src/types.ts +286 -286
- package/test/client-router.test.ts +76 -8
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +719 -728
- package/test/routing.test.ts +197 -194
- package/test/scanning.test.ts +314 -254
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -354
- package/test/streaming.test.ts +40 -1
- package/test/transform-routes.test.ts +87 -87
- package/test/typegen.test.ts +697 -697
- package/tsconfig.json +10 -10
package/src/client/router.ts
CHANGED
|
@@ -54,18 +54,17 @@ async function resolveRoute(pattern: string): Promise<PageComponent | null> {
|
|
|
54
54
|
}
|
|
55
55
|
let layoutList: { path: string; component: (props: any) => any }[] = [];
|
|
56
56
|
let disposeCurrentPage: (() => void) | null = null;
|
|
57
|
+
// Scroll keys and same-page refetch detection are pathname-only on purpose.
|
|
58
|
+
// currentSearch is tracked alongside so popstate can tell "?page=2" apart
|
|
59
|
+
// from "?page=1" without polluting either of those.
|
|
57
60
|
let currentPath = location.pathname;
|
|
61
|
+
let currentSearch = location.search;
|
|
58
62
|
|
|
59
63
|
// C1: Navigation hooks
|
|
60
64
|
let beforeNavigateCb: ((url: URL) => boolean | undefined) | null = null;
|
|
61
65
|
let onNavigateCb: ((url: URL) => void) | null = null;
|
|
62
66
|
let afterNavigateCb: ((url: URL) => void) | null = null;
|
|
63
67
|
|
|
64
|
-
// // HMR
|
|
65
|
-
// (globalThis as any).__grimoire_routes__ = routeMap;
|
|
66
|
-
// (globalThis as any).__grimoire_current_pattern__ = currentPath;
|
|
67
|
-
// (globalThis as any).__grimoire_navigate__ = navigate;
|
|
68
|
-
|
|
69
68
|
export async function hmrRerender(): Promise<void> {
|
|
70
69
|
if (!lastState) {
|
|
71
70
|
location.reload();
|
|
@@ -83,10 +82,34 @@ export async function hmrRerender(): Promise<void> {
|
|
|
83
82
|
rootNode = buildRouteTree(lastState!, Page);
|
|
84
83
|
});
|
|
85
84
|
popHydrationNodes();
|
|
85
|
+
const preserved = capturePreservedScroll();
|
|
86
86
|
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
87
|
+
restorePreservedScroll(preserved);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Dev-only surface the HMR client drives. Route entries are replaced rather
|
|
92
|
+
* than mutated in place so a swapped module also wins for later navigations,
|
|
93
|
+
* and the resolved-module cache is dropped with them.
|
|
94
|
+
*/
|
|
95
|
+
export interface HmrControl {
|
|
96
|
+
setRoute(pattern: string, entry: RouteEntry): void;
|
|
97
|
+
setLayout(path: string, component: (props: any) => any): void;
|
|
98
|
+
rerender(): Promise<void>;
|
|
87
99
|
}
|
|
88
100
|
|
|
89
|
-
|
|
101
|
+
const hmrControl: HmrControl = {
|
|
102
|
+
setRoute(pattern, entry) {
|
|
103
|
+
routeMap[pattern] = entry;
|
|
104
|
+
delete resolvedRoutes[pattern];
|
|
105
|
+
},
|
|
106
|
+
setLayout(path: string, component: (props: any) => any) {
|
|
107
|
+
const existing = layoutList.find((l) => l.path === path);
|
|
108
|
+
if (existing) existing.component = component;
|
|
109
|
+
else layoutList.push({ path, component });
|
|
110
|
+
},
|
|
111
|
+
rerender: hmrRerender,
|
|
112
|
+
};
|
|
90
113
|
|
|
91
114
|
export function beforeNavigate(cb: (url: URL) => boolean | undefined): void {
|
|
92
115
|
beforeNavigateCb = cb;
|
|
@@ -119,6 +142,37 @@ function scrollToTop(): void {
|
|
|
119
142
|
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
120
143
|
}
|
|
121
144
|
|
|
145
|
+
// Elements marked `data-sigil-scroll-preserve` keep their internal scrollTop
|
|
146
|
+
// across the root swap: the router rebuilds the whole tree on navigation, so a
|
|
147
|
+
// long independently-scrollable sidebar would otherwise snap back to the top.
|
|
148
|
+
// The attribute value is the matching key; elements without one fall back to
|
|
149
|
+
// their id, then to their ordinal position.
|
|
150
|
+
const PRESERVE_ATTR = "data-sigil-scroll-preserve";
|
|
151
|
+
|
|
152
|
+
function preserveKey(el: Element, ordinal: number): string {
|
|
153
|
+
return el.getAttribute(PRESERVE_ATTR) || el.id || `#${ordinal}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function capturePreservedScroll(): Map<string, number> {
|
|
157
|
+
const positions = new Map<string, number>();
|
|
158
|
+
let ordinal = 0;
|
|
159
|
+
for (const el of document.querySelectorAll(`[${PRESERVE_ATTR}]`)) {
|
|
160
|
+
positions.set(preserveKey(el, ordinal), el.scrollTop);
|
|
161
|
+
ordinal++;
|
|
162
|
+
}
|
|
163
|
+
return positions;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function restorePreservedScroll(positions: Map<string, number>): void {
|
|
167
|
+
if (positions.size === 0) return;
|
|
168
|
+
let ordinal = 0;
|
|
169
|
+
for (const el of document.querySelectorAll(`[${PRESERVE_ATTR}]`)) {
|
|
170
|
+
const top = positions.get(preserveKey(el, ordinal));
|
|
171
|
+
if (top !== undefined) el.scrollTop = top;
|
|
172
|
+
ordinal++;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
122
176
|
// C3: Preloading
|
|
123
177
|
let preloadTimer: Timer | null = null;
|
|
124
178
|
|
|
@@ -203,6 +257,7 @@ function buildRouteTree(state: typeof lastState, Page: PageComponent): any {
|
|
|
203
257
|
return LayoutComponent({
|
|
204
258
|
data: currentLayoutData,
|
|
205
259
|
params,
|
|
260
|
+
path: pattern,
|
|
206
261
|
children: childNode,
|
|
207
262
|
});
|
|
208
263
|
};
|
|
@@ -340,9 +395,7 @@ export async function navigate(
|
|
|
340
395
|
} catch (e: unknown) {
|
|
341
396
|
stopCollecting();
|
|
342
397
|
if (e && typeof e === "object" && "location" in e) {
|
|
343
|
-
window.location.href = String(
|
|
344
|
-
(e as { location: unknown }).location,
|
|
345
|
-
);
|
|
398
|
+
window.location.href = String((e as { location: unknown }).location);
|
|
346
399
|
return;
|
|
347
400
|
}
|
|
348
401
|
console.error("[grimoire] universal load error:", e);
|
|
@@ -372,10 +425,44 @@ export async function navigate(
|
|
|
372
425
|
popHydrationNodes();
|
|
373
426
|
|
|
374
427
|
updateHead(head);
|
|
428
|
+
// isRefetch is read before currentPath moves, and captured, because
|
|
429
|
+
// applyScroll may run after this function has returned.
|
|
430
|
+
const isRefetch = url.pathname === currentPath;
|
|
431
|
+
|
|
432
|
+
// Scroll has to be decided in the same frame the content lands. Running it
|
|
433
|
+
// here, outside doSwap, meant the hash lookup hit the *outgoing* DOM: a
|
|
434
|
+
// same-page anchor was already present and worked, while an anchor on the
|
|
435
|
+
// incoming page did not exist yet and fell through to scrollToTop().
|
|
436
|
+
const applyScroll = () => {
|
|
437
|
+
if (url.hash) {
|
|
438
|
+
const target = document.getElementById(url.hash.slice(1));
|
|
439
|
+
if (!target) {
|
|
440
|
+
scrollToTop();
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
target.scrollIntoView();
|
|
444
|
+
// re-assert after layout settles (late effects/images can shift content)
|
|
445
|
+
requestAnimationFrame(() => target.scrollIntoView());
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (historyMode === "none") {
|
|
449
|
+
// popstate: the browser already moved through history — restore what
|
|
450
|
+
// we saved for this path (back/forward semantics)
|
|
451
|
+
restoreScrollPosition(url.pathname);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
// fresh navigation starts at the top; a same-path replace
|
|
455
|
+
// (invalidate/refetch) keeps the current scroll
|
|
456
|
+
if (!isRefetch) scrollToTop();
|
|
457
|
+
};
|
|
458
|
+
|
|
375
459
|
// View transitions: animate the swap when the browser supports it and the
|
|
376
460
|
// user hasn't asked for reduced motion. The callback must stay synchronous.
|
|
461
|
+
const preserved = capturePreservedScroll();
|
|
377
462
|
const doSwap = () => {
|
|
378
463
|
document.getElementById("grimoire-root")?.replaceChildren(rootNode);
|
|
464
|
+
restorePreservedScroll(preserved);
|
|
465
|
+
applyScroll();
|
|
379
466
|
};
|
|
380
467
|
const doc = document as Document & {
|
|
381
468
|
startViewTransition?: (cb: () => void) => unknown;
|
|
@@ -388,8 +475,8 @@ export async function navigate(
|
|
|
388
475
|
} else {
|
|
389
476
|
doSwap();
|
|
390
477
|
}
|
|
391
|
-
const isRefetch = url.pathname === currentPath;
|
|
392
478
|
currentPath = url.pathname;
|
|
479
|
+
currentSearch = url.search;
|
|
393
480
|
|
|
394
481
|
if (historyMode !== "none") {
|
|
395
482
|
history[historyMode === "replace" ? "replaceState" : "pushState"](
|
|
@@ -398,19 +485,6 @@ export async function navigate(
|
|
|
398
485
|
path,
|
|
399
486
|
);
|
|
400
487
|
}
|
|
401
|
-
if (url.hash) {
|
|
402
|
-
const target = document.getElementById(url.hash.slice(1));
|
|
403
|
-
if (target) requestAnimationFrame(() => target.scrollIntoView());
|
|
404
|
-
else scrollToTop();
|
|
405
|
-
} else if (historyMode === "none") {
|
|
406
|
-
// popstate: the browser already moved through history — restore what
|
|
407
|
-
// we saved for this path (back/forward semantics)
|
|
408
|
-
restoreScrollPosition(url.pathname);
|
|
409
|
-
} else if (!isRefetch) {
|
|
410
|
-
// fresh navigation: the new page starts at the top
|
|
411
|
-
scrollToTop();
|
|
412
|
-
}
|
|
413
|
-
// same-path replace (invalidate/refetch): keep the current scroll
|
|
414
488
|
afterNavigateCb?.(url);
|
|
415
489
|
}
|
|
416
490
|
|
|
@@ -434,11 +508,27 @@ export function initRouter(
|
|
|
434
508
|
layoutList = layouts;
|
|
435
509
|
disposeCurrentPage = initialDispose ?? null;
|
|
436
510
|
currentPath = location.pathname;
|
|
511
|
+
currentSearch = location.search;
|
|
437
512
|
|
|
438
513
|
// the app owns scroll: the browser's automatic restoration fires after
|
|
439
514
|
// the router's own scroll and clobbers scroll-to-top / saved positions
|
|
440
515
|
if ("scrollRestoration" in history) history.scrollRestoration = "manual";
|
|
441
|
-
//
|
|
516
|
+
// dev HMR surface: available even without SSR state so the client can tell
|
|
517
|
+
// a missing router apart from a missing initial payload
|
|
518
|
+
const globals = globalThis as unknown as {
|
|
519
|
+
__grimoire_current_pattern__?: string;
|
|
520
|
+
__grimoire_routes__?: Record<string, RouteEntry>;
|
|
521
|
+
__grimoire_rerender__?: () => Promise<void>;
|
|
522
|
+
__grimoire_navigate__?: (path: string, replace?: boolean) => void;
|
|
523
|
+
__grimoire_hmr__?: HmrControl;
|
|
524
|
+
};
|
|
525
|
+
globals.__grimoire_routes__ = routeMap;
|
|
526
|
+
globals.__grimoire_rerender__ = hmrRerender;
|
|
527
|
+
globals.__grimoire_hmr__ = hmrControl;
|
|
528
|
+
globals.__grimoire_navigate__ = (path, replace) =>
|
|
529
|
+
navigate(path, replace ?? false);
|
|
530
|
+
|
|
531
|
+
// initial HMR state from SSR
|
|
442
532
|
const stateEl = document.getElementById("__grimoire_state__");
|
|
443
533
|
if (stateEl) {
|
|
444
534
|
const state = JSON.parse(stateEl.textContent!);
|
|
@@ -451,26 +541,26 @@ export function initRouter(
|
|
|
451
541
|
if (Array.isArray(state.deps)) {
|
|
452
542
|
setDeps(state.pattern, state.deps);
|
|
453
543
|
}
|
|
454
|
-
|
|
455
|
-
state.pattern;
|
|
456
|
-
(globalThis as unknown as { __grimoire_routes__: Record<string, RouteEntry> }).__grimoire_routes__ =
|
|
457
|
-
routeMap;
|
|
458
|
-
(globalThis as unknown as { __grimoire_rerender__: () => Promise<void> }).__grimoire_rerender__ =
|
|
459
|
-
hmrRerender;
|
|
460
|
-
(globalThis as unknown as {
|
|
461
|
-
__grimoire_navigate__: (path: string, replace?: boolean) => void;
|
|
462
|
-
}).__grimoire_navigate__ = (path: string, replace?: boolean) =>
|
|
463
|
-
navigate(path, replace ?? false);
|
|
544
|
+
globals.__grimoire_current_pattern__ = state.pattern;
|
|
464
545
|
}
|
|
465
546
|
|
|
466
547
|
saveScrollPosition();
|
|
467
548
|
document.addEventListener("click", handleClick);
|
|
468
549
|
window.addEventListener("popstate", () => {
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
550
|
+
if (
|
|
551
|
+
location.pathname === currentPath &&
|
|
552
|
+
location.search === currentSearch
|
|
553
|
+
) {
|
|
554
|
+
return;
|
|
473
555
|
}
|
|
556
|
+
// the browser already moved through history — render only, never
|
|
557
|
+
// push/replace here or the forward stack gets clobbered.
|
|
558
|
+
// The search has to come along: refetching the bare pathname served
|
|
559
|
+
// unfiltered data while the address bar still showed the query.
|
|
560
|
+
// The hash deliberately does not, so back/forward restores the saved
|
|
561
|
+
// scroll offset instead of re-jumping to the anchor, matching the
|
|
562
|
+
// browser's own behaviour for document history navigation.
|
|
563
|
+
navigate(location.pathname + location.search, { history: "none" });
|
|
474
564
|
});
|
|
475
565
|
setupPreload();
|
|
476
566
|
}
|
package/src/client/scope.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
-
* every createEffect registered during that call.
|
|
4
|
-
*
|
|
5
|
-
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
-
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
-
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
-
*/
|
|
9
|
-
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
-
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
-
const disposers: (() => void)[] = [];
|
|
12
|
-
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
-
try {
|
|
14
|
-
fn();
|
|
15
|
-
} finally {
|
|
16
|
-
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
-
}
|
|
18
|
-
return () => {
|
|
19
|
-
for (const d of disposers) d();
|
|
20
|
-
disposers.length = 0;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Open an effect scope: run fn() and return a dispose function that tears down
|
|
3
|
+
* every createEffect registered during that call.
|
|
4
|
+
*
|
|
5
|
+
* Communicates with @sigil-dev/runtime's createEffect via globalThis.__sigilEffectScope
|
|
6
|
+
* so this file does not need to import from @sigil-dev/runtime (which would pull the
|
|
7
|
+
* runtime source into Bun.build and break the test sandbox).
|
|
8
|
+
*/
|
|
9
|
+
export function withEffectScope(fn: () => void): () => void {
|
|
10
|
+
const prev = (globalThis as any).__sigilEffectScope;
|
|
11
|
+
const disposers: (() => void)[] = [];
|
|
12
|
+
(globalThis as any).__sigilEffectScope = disposers;
|
|
13
|
+
try {
|
|
14
|
+
fn();
|
|
15
|
+
} finally {
|
|
16
|
+
(globalThis as any).__sigilEffectScope = prev;
|
|
17
|
+
}
|
|
18
|
+
return () => {
|
|
19
|
+
for (const d of disposers) d();
|
|
20
|
+
disposers.length = 0;
|
|
21
|
+
};
|
|
22
|
+
}
|