@sigil-dev/grimoire 0.9.3 → 0.9.5
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 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -19
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +174 -68
- 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 +159 -138
- package/src/rendering/index.ts +140 -18
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +13 -1
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +196 -141
- 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 -243
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -364
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +711 -655
- 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 -356
- package/src/types.ts +286 -282
- package/test/client-router.test.ts +295 -44
- 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 -615
- package/test/routing.test.ts +197 -81
- package/test/scanning.test.ts +314 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
package/src/rendering/head.ts
CHANGED
|
@@ -1,47 +1,52 @@
|
|
|
1
|
-
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
-
|
|
3
|
-
const HeadKey = createContext<string[]>();
|
|
4
|
-
const NonceKey = createContext<string>();
|
|
5
|
-
|
|
6
|
-
export function initHead(nonce?: string): void {
|
|
7
|
-
setContext(HeadKey, []);
|
|
8
|
-
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function setHeadNonce(nonce: string): void {
|
|
12
|
-
setContext(NonceKey, nonce);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getHeadNonce(): string {
|
|
16
|
-
return getContext(NonceKey) ?? "";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function collectHead(): string {
|
|
20
|
-
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function Head({
|
|
24
|
-
children,
|
|
25
|
-
}: {
|
|
26
|
-
children: string | Node;
|
|
27
|
-
}): string | Comment {
|
|
28
|
-
if (typeof document === "undefined") {
|
|
29
|
-
const buf = getContext(HeadKey);
|
|
30
|
-
if (buf) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
1
|
+
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
2
|
+
|
|
3
|
+
const HeadKey = createContext<string[]>();
|
|
4
|
+
const NonceKey = createContext<string>();
|
|
5
|
+
|
|
6
|
+
export function initHead(nonce?: string): void {
|
|
7
|
+
setContext(HeadKey, []);
|
|
8
|
+
if (nonce) setContext(NonceKey, nonce);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setHeadNonce(nonce: string): void {
|
|
12
|
+
setContext(NonceKey, nonce);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getHeadNonce(): string {
|
|
16
|
+
return getContext(NonceKey) ?? "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function collectHead(): string {
|
|
20
|
+
return (getContext(HeadKey) ?? []).join("\n ");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function Head({
|
|
24
|
+
children,
|
|
25
|
+
}: {
|
|
26
|
+
children: string | Node;
|
|
27
|
+
}): string | Comment {
|
|
28
|
+
if (typeof document === "undefined") {
|
|
29
|
+
const buf = getContext(HeadKey);
|
|
30
|
+
if (buf) {
|
|
31
|
+
// Dynamic text inside <Head> children arrives wrapped in hydration
|
|
32
|
+
// claim markers; those belong to the body runtime and must never
|
|
33
|
+
// reach document.title or meta content.
|
|
34
|
+
const nonce = getContext(NonceKey);
|
|
35
|
+
const clean = String(children).replace(/<!--\/?g[^>]*-->/g, "");
|
|
36
|
+
if (nonce) {
|
|
37
|
+
buf.push(
|
|
38
|
+
clean
|
|
39
|
+
.replace(/<script /g, `<script nonce="${nonce}" `)
|
|
40
|
+
.replace(/<style /g, `<style nonce="${nonce}" `),
|
|
41
|
+
);
|
|
42
|
+
} else {
|
|
43
|
+
buf.push(clean);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
if (children instanceof Node) {
|
|
49
|
+
document.head.appendChild(children);
|
|
50
|
+
}
|
|
51
|
+
return document.createComment("head");
|
|
52
|
+
}
|
package/src/rendering/hydrate.ts
CHANGED
|
@@ -1,138 +1,159 @@
|
|
|
1
|
-
import {
|
|
2
|
-
claim,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
(globalThis as any).
|
|
23
|
-
(globalThis as any).
|
|
24
|
-
(globalThis as any).
|
|
25
|
-
(globalThis as any).
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
state.pattern
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
claim,
|
|
3
|
+
flushDeferredEffects,
|
|
4
|
+
getHydrationNodes,
|
|
5
|
+
insert,
|
|
6
|
+
popHydrationNodes,
|
|
7
|
+
pushHydrationNodes,
|
|
8
|
+
} from "@sigil-dev/runtime";
|
|
9
|
+
//@ts-expect-error compiler generated
|
|
10
|
+
import { layouts, matchers, routes } from "#grimoire-routes";
|
|
11
|
+
import {
|
|
12
|
+
afterNavigate,
|
|
13
|
+
beforeNavigate,
|
|
14
|
+
navigate,
|
|
15
|
+
onNavigate,
|
|
16
|
+
} from "../client/router";
|
|
17
|
+
import { initRouter } from "../client/router.ts";
|
|
18
|
+
import { withEffectScope } from "../client/scope.ts";
|
|
19
|
+
import { Head } from "./head";
|
|
20
|
+
|
|
21
|
+
// expose for HMR module shim
|
|
22
|
+
(globalThis as any).__grimoire_Head__ = Head;
|
|
23
|
+
(globalThis as any).__grimoire_navigate__ = navigate;
|
|
24
|
+
(globalThis as any).__grimoire_beforeNavigate__ = beforeNavigate;
|
|
25
|
+
(globalThis as any).__grimoire_onNavigate__ = onNavigate;
|
|
26
|
+
(globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
|
|
27
|
+
|
|
28
|
+
async function resolveInitialPage(pattern: string): Promise<any> {
|
|
29
|
+
const entry = routes[pattern];
|
|
30
|
+
if (!entry) return null;
|
|
31
|
+
if (typeof entry === "function") return entry;
|
|
32
|
+
return (await entry.__lazy()).default;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function hydrate(): Promise<(() => void) | undefined> {
|
|
36
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
37
|
+
if (!stateEl) return undefined;
|
|
38
|
+
|
|
39
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
40
|
+
const Page = await resolveInitialPage(state.pattern);
|
|
41
|
+
if (!Page) return undefined;
|
|
42
|
+
const slot = document.getElementById("grimoire-root");
|
|
43
|
+
if (!slot) return undefined;
|
|
44
|
+
|
|
45
|
+
const matchedLayouts = layouts
|
|
46
|
+
.filter(
|
|
47
|
+
(l: any) =>
|
|
48
|
+
l.path === "/" ||
|
|
49
|
+
state.pattern === l.path ||
|
|
50
|
+
state.pattern.startsWith(`${l.path}/`),
|
|
51
|
+
)
|
|
52
|
+
.sort((a: any, b: any) => a.path.length - b.path.length);
|
|
53
|
+
|
|
54
|
+
// The page's SSR container is findable by id, so the page can hydrate
|
|
55
|
+
// in place inside it while a single root layout claims from the
|
|
56
|
+
// top-level pool. With nested layouts (>1) the inner layout's DOM
|
|
57
|
+
// position can't be located in the flat pool — fall back to clearing
|
|
58
|
+
// the SSR content and re-rendering in DOM mode (empty pool).
|
|
59
|
+
const pageHost = document.getElementById("grimoire-page");
|
|
60
|
+
const hasLayouts = matchedLayouts.length > 0;
|
|
61
|
+
const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
|
|
62
|
+
if (!inPlace) {
|
|
63
|
+
slot.replaceChildren();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ssrClones = inPlace
|
|
67
|
+
? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
|
|
68
|
+
: [];
|
|
69
|
+
pushHydrationNodes(
|
|
70
|
+
inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : [],
|
|
71
|
+
);
|
|
72
|
+
let initialDispose: (() => void) | undefined;
|
|
73
|
+
try {
|
|
74
|
+
initialDispose = withEffectScope(() => {
|
|
75
|
+
try {
|
|
76
|
+
let renderFn = () => {
|
|
77
|
+
const pageDiv =
|
|
78
|
+
hasLayouts && inPlace && pageHost
|
|
79
|
+
? pageHost
|
|
80
|
+
: claim(getHydrationNodes(), "div");
|
|
81
|
+
pageDiv.id = "grimoire-page";
|
|
82
|
+
|
|
83
|
+
if (pageDiv.childNodes.length > 0) {
|
|
84
|
+
pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
|
|
85
|
+
let pageNode: unknown;
|
|
86
|
+
try {
|
|
87
|
+
pageNode = Page({
|
|
88
|
+
data: state.data,
|
|
89
|
+
params: state.params,
|
|
90
|
+
});
|
|
91
|
+
} finally {
|
|
92
|
+
// always rebalance the global hydration stack — a throwing
|
|
93
|
+
// page component must not corrupt claims for the whole session
|
|
94
|
+
popHydrationNodes();
|
|
95
|
+
}
|
|
96
|
+
insert(pageDiv, pageNode);
|
|
97
|
+
} else {
|
|
98
|
+
const pageNode = Page({
|
|
99
|
+
data: state.data,
|
|
100
|
+
params: state.params,
|
|
101
|
+
});
|
|
102
|
+
insert(pageDiv, pageNode);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return pageDiv;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
for (let i = matchedLayouts.length - 1; i >= 0; i--) {
|
|
109
|
+
const LayoutComponent = matchedLayouts[i].component;
|
|
110
|
+
const innerRender = renderFn;
|
|
111
|
+
const layoutData = state.layoutData?.[i];
|
|
112
|
+
renderFn = () => {
|
|
113
|
+
// Pre-render inner content so children is a Node (insert() doesn't call functions)
|
|
114
|
+
const childNode = innerRender();
|
|
115
|
+
return LayoutComponent({
|
|
116
|
+
data: layoutData,
|
|
117
|
+
params: state.params,
|
|
118
|
+
path: state.pattern,
|
|
119
|
+
children: childNode,
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Defer effects during hydration render — they'll run after DOM insertion,
|
|
125
|
+
// so document.querySelector() etc. can find elements that are in the document.
|
|
126
|
+
(globalThis as any).__sigilDeferEffects = true;
|
|
127
|
+
try {
|
|
128
|
+
const rootNode = renderFn();
|
|
129
|
+
insert(slot, rootNode);
|
|
130
|
+
} finally {
|
|
131
|
+
// finally: a throwing render must not leave the flag set forever
|
|
132
|
+
(globalThis as any).__sigilDeferEffects = false;
|
|
133
|
+
flushDeferredEffects();
|
|
134
|
+
}
|
|
135
|
+
} finally {
|
|
136
|
+
popHydrationNodes();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.warn("[grimoire] hydration error:", e);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!slot.hasChildNodes() && ssrClones.length > 0) {
|
|
144
|
+
slot.replaceChildren(...ssrClones);
|
|
145
|
+
}
|
|
146
|
+
return initialDispose;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
hydrate().then((initialDispose) => {
|
|
150
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
151
|
+
const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
|
|
152
|
+
initRouter(routes, layouts, initialDispose, {
|
|
153
|
+
matchers,
|
|
154
|
+
deps:
|
|
155
|
+
state && Array.isArray(state.deps)
|
|
156
|
+
? { [state.pattern]: state.deps }
|
|
157
|
+
: undefined,
|
|
158
|
+
});
|
|
159
|
+
});
|