@sigil-dev/grimoire 0.9.2 → 0.9.4

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 (88) 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 +31 -8
  6. package/src/client/enhance.ts +102 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +529 -371
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +163 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +114 -114
  14. package/src/dev/hmr-client.ts +185 -185
  15. package/src/dev/loader.ts +47 -47
  16. package/src/dev/paths.ts +14 -14
  17. package/src/dev/runtime-bundle.ts +52 -52
  18. package/src/dev/watcher.ts +47 -46
  19. package/src/env/dynamic-private.ts +19 -19
  20. package/src/env/dynamic-public.ts +22 -22
  21. package/src/env/index.ts +25 -25
  22. package/src/env/plugin.ts +19 -19
  23. package/src/env/private.ts +5 -5
  24. package/src/env/public.ts +7 -7
  25. package/src/env/typegen.ts +61 -61
  26. package/src/headers.ts +114 -114
  27. package/src/integrations/vite.ts +73 -73
  28. package/src/logger/index.ts +4 -4
  29. package/src/logger/instance.ts +6 -6
  30. package/src/logger/logger.ts +113 -113
  31. package/src/logger/request-middleware.ts +24 -24
  32. package/src/logger/transports.ts +117 -117
  33. package/src/logger/types.ts +39 -39
  34. package/src/rendering/head.ts +47 -47
  35. package/src/rendering/hydrate.ts +146 -119
  36. package/src/rendering/index.ts +573 -372
  37. package/src/rendering/ssrPlugin.ts +69 -69
  38. package/src/routing/manifest-gen.ts +55 -42
  39. package/src/routing/router.ts +135 -109
  40. package/src/routing/scanner.ts +187 -141
  41. package/src/routing/transform-routes.ts +102 -102
  42. package/src/sentinels/error.ts +52 -52
  43. package/src/sentinels/fail.ts +41 -41
  44. package/src/sentinels/redirect.ts +35 -35
  45. package/src/server/build.ts +255 -219
  46. package/src/server/context.ts +10 -10
  47. package/src/server/cookie-utils.ts +66 -66
  48. package/src/server/coordinator.ts +364 -356
  49. package/src/server/hooks.ts +114 -114
  50. package/src/server/index.ts +677 -658
  51. package/src/server/interactive.ts +56 -56
  52. package/src/server/plugins.ts +121 -120
  53. package/src/server/worker.ts +58 -58
  54. package/src/typegen/index.ts +389 -356
  55. package/src/types.ts +8 -4
  56. package/test/client-router.test.ts +295 -35
  57. package/test/context.test.ts +52 -52
  58. package/test/env.test.ts +70 -70
  59. package/test/exports.test.ts +37 -37
  60. package/test/fail.test.ts +46 -46
  61. package/test/headers.test.ts +100 -100
  62. package/test/hydration.test.ts +119 -119
  63. package/test/middleware.test.ts +223 -223
  64. package/test/preload.ts +5 -5
  65. package/test/redirect-error.test.ts +112 -112
  66. package/test/rendering.test.ts +728 -579
  67. package/test/routing.test.ts +194 -81
  68. package/test/scanning.test.ts +254 -200
  69. package/test/scope.test.ts +195 -195
  70. package/test/server.test.ts +354 -249
  71. package/test/streaming.test.ts +242 -125
  72. package/test/transform-routes.test.ts +87 -84
  73. package/test/typegen.test.ts +697 -682
  74. package/tsconfig.json +10 -10
  75. package/.grimoire/_routes.dom.js +0 -8
  76. package/.grimoire/_routes.hydrate.js +0 -8
  77. package/.grimoire/tsconfig.generated.json +0 -11
  78. package/.grimoire/types/ambient.d.ts +0 -59
  79. package/.grimoire/types/api/hello/$types.d.ts +0 -50
  80. package/.grimoire/types/api/items/$types.d.ts +0 -50
  81. package/.grimoire/types/echo/$types.d.ts +0 -50
  82. package/.grimoire/types/env-private.d.ts +0 -5
  83. package/.grimoire/types/env-public.d.ts +0 -5
  84. package/.grimoire/types/mixed/$types.d.ts +0 -50
  85. package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
  86. package/.grimoire/types/reject/$types.d.ts +0 -50
  87. package/public/__grimoire__/hydrate.js +0 -585
  88. package/public/__grimoire__/index.js +0 -490
@@ -1,119 +1,146 @@
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
- const stateEl = document.getElementById("__grimoire_state__");
28
- let initialDispose: (() => void) | undefined;
29
-
30
- if (stateEl) {
31
- const state = JSON.parse(stateEl.textContent!);
32
- const Page = routes[state.pattern];
33
- if (Page) {
34
- const slot = document.getElementById("grimoire-root");
35
- if (slot) {
36
- const matchedLayouts = layouts
37
- .filter(
38
- (l: any) =>
39
- l.path === "/" ||
40
- state.pattern === l.path ||
41
- state.pattern.startsWith(`${l.path}/`),
42
- )
43
- .sort((a: any, b: any) => a.path.length - b.path.length);
44
-
45
- // When layouts are involved, nested <!--g--> delimiters in the flat SSR pool
46
- // cause anchor-mount to claim the wrong anchor and remove layout DOM nodes.
47
- // Clear SSR content and re-render in DOM mode (empty pool) instead.
48
- const hasLayouts = matchedLayouts.length > 0;
49
- if (hasLayouts) {
50
- slot.replaceChildren();
51
- }
52
-
53
- const ssrClones = hasLayouts
54
- ? []
55
- : Array.from(slot.childNodes).map((n) => n.cloneNode(true));
56
- pushHydrationNodes(
57
- hasLayouts ? [] : (Array.from(slot.childNodes) as ChildNode[]),
58
- );
59
- try {
60
- initialDispose = withEffectScope(() => {
61
- try {
62
- let renderFn = () => {
63
- const pageDiv = claim(getHydrationNodes(), "div");
64
- pageDiv.id = "grimoire-page";
65
-
66
- if (pageDiv.childNodes.length > 0) {
67
- pushHydrationNodes(
68
- Array.from(pageDiv.childNodes) as ChildNode[],
69
- );
70
- const pageNode = Page({
71
- data: state.data,
72
- params: state.params,
73
- });
74
- popHydrationNodes();
75
- insert(pageDiv, pageNode);
76
- } else {
77
- const pageNode = Page({
78
- data: state.data,
79
- params: state.params,
80
- });
81
- insert(pageDiv, pageNode);
82
- }
83
-
84
- return pageDiv;
85
- };
86
-
87
- for (let i = matchedLayouts.length - 1; i >= 0; i--) {
88
- const LayoutComponent = matchedLayouts[i].component;
89
- const innerRender = renderFn;
90
- const layoutData = state.layoutData?.[i];
91
- renderFn = () => {
92
- // Pre-render inner content so children is a Node (insert() doesn't call functions)
93
- const childNode = innerRender();
94
- return LayoutComponent({
95
- data: layoutData,
96
- params: state.params,
97
- children: childNode,
98
- });
99
- };
100
- }
101
-
102
- const rootNode = renderFn();
103
- insert(slot, rootNode);
104
- } finally {
105
- popHydrationNodes();
106
- }
107
- });
108
- } catch (e) {
109
- console.warn("[grimoire] hydration error:", e);
110
- }
111
-
112
- if (!slot.hasChildNodes() && ssrClones.length > 0) {
113
- slot.replaceChildren(...ssrClones);
114
- }
115
- }
116
- }
117
- }
118
-
119
- initRouter(routes, layouts, initialDispose);
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, matchers, 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
+ const stateEl = document.getElementById("__grimoire_state__");
138
+ const state = stateEl ? JSON.parse(stateEl.textContent!) : null;
139
+ initRouter(routes, layouts, initialDispose, {
140
+ matchers,
141
+ deps:
142
+ state && Array.isArray(state.deps)
143
+ ? { [state.pattern]: state.deps }
144
+ : undefined,
145
+ });
146
+ });