@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.
Files changed (78) hide show
  1. package/index.ts +37 -35
  2. package/package.json +9 -12
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -19
  6. package/src/client/enhance.ts +102 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +2 -2
  9. package/src/client/router.ts +174 -68
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +215 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +109 -114
  14. package/src/dev/hmr-client.ts +202 -185
  15. package/src/dev/hmr-server.ts +261 -187
  16. package/src/dev/loader.ts +120 -47
  17. package/src/dev/paths.ts +14 -14
  18. package/src/dev/resolve.ts +70 -0
  19. package/src/dev/runtime-bundle.ts +52 -52
  20. package/src/dev/scan.ts +78 -0
  21. package/src/dev/watcher.ts +69 -47
  22. package/src/env/dynamic-private.ts +19 -19
  23. package/src/env/dynamic-public.ts +22 -22
  24. package/src/env/index.ts +25 -25
  25. package/src/env/plugin.ts +19 -19
  26. package/src/env/private.ts +5 -5
  27. package/src/env/public.ts +7 -7
  28. package/src/env/typegen.ts +61 -61
  29. package/src/headers.ts +114 -114
  30. package/src/integrations/vite.ts +73 -73
  31. package/src/logger/index.ts +4 -4
  32. package/src/logger/instance.ts +6 -6
  33. package/src/logger/logger.ts +113 -113
  34. package/src/logger/request-middleware.ts +24 -24
  35. package/src/logger/transports.ts +117 -117
  36. package/src/logger/types.ts +39 -39
  37. package/src/rendering/head.ts +52 -47
  38. package/src/rendering/hydrate.ts +159 -138
  39. package/src/rendering/index.ts +140 -18
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +13 -1
  42. package/src/routing/router.ts +135 -109
  43. package/src/routing/scanner.ts +196 -141
  44. package/src/routing/transform-routes.ts +102 -102
  45. package/src/sentinels/error.ts +52 -52
  46. package/src/sentinels/fail.ts +41 -41
  47. package/src/sentinels/redirect.ts +35 -35
  48. package/src/server/build.ts +250 -243
  49. package/src/server/context.ts +10 -10
  50. package/src/server/cookie-utils.ts +66 -66
  51. package/src/server/coordinator.ts +364 -364
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +711 -655
  54. package/src/server/interactive.ts +56 -56
  55. package/src/server/plugins.ts +121 -121
  56. package/src/server/worker.ts +58 -58
  57. package/src/typegen/index.ts +389 -356
  58. package/src/types.ts +286 -282
  59. package/test/client-router.test.ts +295 -44
  60. package/test/context.test.ts +52 -52
  61. package/test/env.test.ts +90 -90
  62. package/test/exports.test.ts +37 -37
  63. package/test/fail.test.ts +46 -46
  64. package/test/headers.test.ts +100 -100
  65. package/test/hmr.test.ts +426 -0
  66. package/test/hydration.test.ts +119 -119
  67. package/test/middleware.test.ts +223 -223
  68. package/test/preload.ts +5 -5
  69. package/test/redirect-error.test.ts +112 -112
  70. package/test/rendering.test.ts +719 -615
  71. package/test/routing.test.ts +197 -81
  72. package/test/scanning.test.ts +314 -200
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -249
  75. package/test/streaming.test.ts +242 -125
  76. package/test/transform-routes.test.ts +87 -84
  77. package/test/typegen.test.ts +697 -682
  78. package/tsconfig.json +10 -10
@@ -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
- const nonce = getContext(NonceKey);
32
- if (nonce) {
33
- const withNonce = String(children)
34
- .replace(/<script /g, `<script nonce="${nonce}" `)
35
- .replace(/<style /g, `<style nonce="${nonce}" `);
36
- buf.push(withNonce);
37
- } else {
38
- buf.push(children as string);
39
- }
40
- }
41
- return "";
42
- }
43
- if (children instanceof Node) {
44
- document.head.appendChild(children);
45
- }
46
- return document.createComment("head");
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
+ }
@@ -1,138 +1,159 @@
1
- import {
2
- claim,
3
- getHydrationNodes,
4
- insert,
5
- popHydrationNodes,
6
- pushHydrationNodes,
7
- } from "@sigil-dev/runtime";
8
- //@ts-expect-error compiler generated
9
- import { layouts, routes } from "#grimoire-routes";
10
- import {
11
- afterNavigate,
12
- beforeNavigate,
13
- navigate,
14
- onNavigate,
15
- } from "../client/router";
16
- import { initRouter } from "../client/router.ts";
17
- import { withEffectScope } from "../client/scope.ts";
18
- import { Head } from "./head";
19
-
20
- // expose for HMR module shim
21
- (globalThis as any).__grimoire_Head__ = Head;
22
- (globalThis as any).__grimoire_navigate__ = navigate;
23
- (globalThis as any).__grimoire_beforeNavigate__ = beforeNavigate;
24
- (globalThis as any).__grimoire_onNavigate__ = onNavigate;
25
- (globalThis as any).__grimoire_afterNavigate__ = afterNavigate;
26
-
27
- async function resolveInitialPage(pattern: string): Promise<any> {
28
- const entry = routes[pattern];
29
- if (!entry) return null;
30
- if (typeof entry === "function") return entry;
31
- return (await entry.__lazy()).default;
32
- }
33
-
34
- async function hydrate(): Promise<(() => void) | undefined> {
35
- const stateEl = document.getElementById("__grimoire_state__");
36
- if (!stateEl) return undefined;
37
-
38
- const state = JSON.parse(stateEl.textContent!);
39
- const Page = await resolveInitialPage(state.pattern);
40
- if (!Page) return undefined;
41
- const slot = document.getElementById("grimoire-root");
42
- if (!slot) return undefined;
43
-
44
- const matchedLayouts = layouts
45
- .filter(
46
- (l: any) =>
47
- l.path === "/" ||
48
- state.pattern === l.path ||
49
- state.pattern.startsWith(`${l.path}/`),
50
- )
51
- .sort((a: any, b: any) => a.path.length - b.path.length);
52
-
53
- // The page's SSR container is findable by id, so the page can hydrate
54
- // in place inside it while a single root layout claims from the
55
- // top-level pool. With nested layouts (>1) the inner layout's DOM
56
- // position can't be located in the flat pool — fall back to clearing
57
- // the SSR content and re-rendering in DOM mode (empty pool).
58
- const pageHost = document.getElementById("grimoire-page");
59
- const hasLayouts = matchedLayouts.length > 0;
60
- const inPlace = !hasLayouts || (matchedLayouts.length === 1 && !!pageHost);
61
- if (!inPlace) {
62
- slot.replaceChildren();
63
- }
64
-
65
- const ssrClones = inPlace
66
- ? Array.from(slot.childNodes).map((n) => n.cloneNode(true))
67
- : [];
68
- pushHydrationNodes(inPlace ? (Array.from(slot.childNodes) as ChildNode[]) : []);
69
- let initialDispose: (() => void) | undefined;
70
- try {
71
- initialDispose = withEffectScope(() => {
72
- try {
73
- let renderFn = () => {
74
- const pageDiv =
75
- hasLayouts && inPlace && pageHost
76
- ? pageHost
77
- : claim(getHydrationNodes(), "div");
78
- pageDiv.id = "grimoire-page";
79
-
80
- if (pageDiv.childNodes.length > 0) {
81
- pushHydrationNodes(Array.from(pageDiv.childNodes) as ChildNode[]);
82
- let pageNode: unknown;
83
- try {
84
- pageNode = Page({
85
- data: state.data,
86
- params: state.params,
87
- });
88
- } finally {
89
- // always rebalance the global hydration stack — a throwing
90
- // page component must not corrupt claims for the whole session
91
- popHydrationNodes();
92
- }
93
- insert(pageDiv, pageNode);
94
- } else {
95
- const pageNode = Page({
96
- data: state.data,
97
- params: state.params,
98
- });
99
- insert(pageDiv, pageNode);
100
- }
101
-
102
- return pageDiv;
103
- };
104
-
105
- for (let i = matchedLayouts.length - 1; i >= 0; i--) {
106
- const LayoutComponent = matchedLayouts[i].component;
107
- const innerRender = renderFn;
108
- const layoutData = state.layoutData?.[i];
109
- renderFn = () => {
110
- // Pre-render inner content so children is a Node (insert() doesn't call functions)
111
- const childNode = innerRender();
112
- return LayoutComponent({
113
- data: layoutData,
114
- params: state.params,
115
- children: childNode,
116
- });
117
- };
118
- }
119
-
120
- const rootNode = renderFn();
121
- insert(slot, rootNode);
122
- } finally {
123
- popHydrationNodes();
124
- }
125
- });
126
- } catch (e) {
127
- console.warn("[grimoire] hydration error:", e);
128
- }
129
-
130
- if (!slot.hasChildNodes() && ssrClones.length > 0) {
131
- slot.replaceChildren(...ssrClones);
132
- }
133
- return initialDispose;
134
- }
135
-
136
- hydrate().then((initialDispose) => {
137
- initRouter(routes, layouts, initialDispose);
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
+ });