@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.
Files changed (78) hide show
  1. package/index.ts +37 -37
  2. package/package.json +3 -3
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -36
  6. package/src/client/enhance.ts +102 -102
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +128 -38
  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 +16 -3
  39. package/src/rendering/index.ts +43 -17
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +55 -55
  42. package/src/routing/router.ts +135 -135
  43. package/src/routing/scanner.ts +196 -187
  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 -255
  49. package/src/server/context.ts +10 -10
  50. package/src/server/cookie-utils.ts +66 -66
  51. package/src/server/coordinator.ts +1 -1
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +110 -69
  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 -389
  58. package/src/types.ts +286 -286
  59. package/test/client-router.test.ts +76 -8
  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 -728
  71. package/test/routing.test.ts +197 -194
  72. package/test/scanning.test.ts +314 -254
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -354
  75. package/test/streaming.test.ts +40 -1
  76. package/test/transform-routes.test.ts +87 -87
  77. package/test/typegen.test.ts +697 -697
  78. package/tsconfig.json +10 -10
@@ -1,195 +1,195 @@
1
- /**
2
- * Tests for the effect-scope mechanism used by the router to prevent effect
3
- * accumulation across client-side navigations.
4
- *
5
- * Two invariants must hold forever:
6
- * 1. Effects created during a page mount stop running when the page is disposed.
7
- * 2. __nodes is reset to [] before each SPA navigation so the new page gets
8
- * a fresh empty pool instead of recycling stale DOM nodes.
9
- *
10
- * await tick() must be called after all signal edits, given Bun's runner is
11
- * synchronous and doesnt wait for our microtasks. In the browser the microtasks
12
- * flush between event loop turns
13
- */
14
- import { describe, expect, test } from "bun:test";
15
- import {
16
- createEffect,
17
- createSignal,
18
- withEffectScope as runtimeScope,
19
- tick,
20
- } from "@sigil-dev/runtime";
21
- import { withEffectScope } from "../src/client/scope.ts";
22
-
23
- describe("grimoire withEffectScope", () => {
24
- test("effects inside scope stop after dispose", async () => {
25
- const count = createSignal(0);
26
- let runs = 0;
27
- const dispose = withEffectScope(() => {
28
- createEffect(() => {
29
- count();
30
- runs++;
31
- });
32
- });
33
-
34
- count.set(1);
35
- await tick(); // let microtask queue flush
36
- expect(runs).toBe(2);
37
-
38
- dispose();
39
- count.set(2);
40
- await tick();
41
- expect(runs).toBe(2);
42
- });
43
-
44
- test("simulates two page navigations — only latest page's effects run", async () => {
45
- const count = createSignal(0);
46
- let pageARuns = 0;
47
- let pageBRuns = 0;
48
-
49
- // Mount page A
50
- let disposeCurrentPage = withEffectScope(() => {
51
- createEffect(() => {
52
- count();
53
- pageARuns++;
54
- });
55
- });
56
- count.set(1);
57
- await tick();
58
- expect(pageARuns).toBe(2);
59
- expect(pageBRuns).toBe(0);
60
-
61
- // Navigate to page B — dispose A first
62
- disposeCurrentPage();
63
- disposeCurrentPage = withEffectScope(() => {
64
- createEffect(() => {
65
- count();
66
- pageBRuns++;
67
- });
68
- });
69
-
70
- count.set(2);
71
- await tick();
72
- expect(pageARuns).toBe(2); // A's effect is gone
73
- expect(pageBRuns).toBe(2); // B initial + update
74
-
75
- // Navigate away from B — dispose B
76
- disposeCurrentPage();
77
- count.set(3);
78
- await tick();
79
- expect(pageARuns).toBe(2);
80
- expect(pageBRuns).toBe(2); // B's effect is gone too
81
- });
82
-
83
- test("three navigations: each page's effects are independent", async () => {
84
- const signal = createSignal("a");
85
- const log: string[] = [];
86
-
87
- const mountPage = (label: string) =>
88
- withEffectScope(() => {
89
- createEffect(() => {
90
- log.push(`${label}:${signal()}`);
91
- });
92
- });
93
-
94
- const disposeA = mountPage("A");
95
- const disposeB = mountPage("B");
96
-
97
- signal.set("b");
98
- await tick();
99
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b"]);
100
-
101
- disposeA();
102
- signal.set("c");
103
- await tick();
104
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // only B ran
105
-
106
- disposeB();
107
- signal.set("d");
108
- await tick();
109
- expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // neither ran
110
- });
111
-
112
- test("grimoire scope uses same globalThis channel as runtime scope", async () => {
113
- // Both must route through the same global so that createEffect
114
- // in page components (compiled by sigil, imports @sigil-dev/runtime)
115
- // is captured by grimoire's router scope.
116
- const count = createSignal(0);
117
- let runs = 0;
118
-
119
- // grimoire's local withEffectScope wraps a createEffect from @sigil-dev/runtime
120
- const dispose = withEffectScope(() => {
121
- createEffect(() => {
122
- count();
123
- runs++;
124
- });
125
- });
126
-
127
- count.set(1);
128
- await tick();
129
- expect(runs).toBe(2);
130
-
131
- dispose();
132
- count.set(2);
133
- await tick();
134
- expect(runs).toBe(2); // runtime's createEffect was captured by grimoire's scope ✓
135
- });
136
-
137
- test("runtime's withEffectScope and grimoire's are interchangeable", async () => {
138
- // The runtime exports its own withEffectScope for user code.
139
- // Both implementations must work the same way via the globalThis channel.
140
- const count = createSignal(0);
141
- let runs = 0;
142
-
143
- const dispose = runtimeScope(() => {
144
- createEffect(() => {
145
- count();
146
- runs++;
147
- });
148
- });
149
-
150
- count.set(1);
151
- await tick();
152
- expect(runs).toBe(2);
153
- dispose();
154
- count.set(2);
155
- await tick();
156
- expect(runs).toBe(2);
157
- });
158
- });
159
-
160
- describe("__nodes pool reset on SPA navigation", () => {
161
- test("navigate sets __nodes to [] before calling Page()", () => {
162
- // Simulate exactly what router.ts's navigate() does:
163
- // dispose old effects → set __nodes = [] → call Page() → replaceChildren(node)
164
- // We only care that Page() sees an empty pool.
165
- let nodesAtCallTime: unknown = "not set";
166
-
167
- const mockPage = () => {
168
- nodesAtCallTime = (globalThis as any).__nodes;
169
- return document.createElement("div"); // satisfy replaceChildren
170
- };
171
- // Mimic the three lines in navigate() that matter
172
- (globalThis as any).__nodes = ["stale", "dom", "nodes"]; // leftover from previous page
173
- (globalThis as any).__nodes = []; // the reset
174
- mockPage();
175
-
176
- expect(nodesAtCallTime).toEqual([]);
177
- });
178
-
179
- test("Page() called during SSR hydration sees real DOM nodes, not empty pool", () => {
180
- // Contrast: hydrate.ts sets __nodes to Array.from(slot.childNodes) before calling Page()
181
- const fakeSlotChild = document.createElement("div");
182
- let nodesAtCallTime: unknown = "not set";
183
-
184
- const mockPage = () => {
185
- nodesAtCallTime = (globalThis as any).__nodes;
186
- };
187
-
188
- (globalThis as any).__nodes = [fakeSlotChild]; // hydrate.ts sets real nodes
189
- mockPage();
190
-
191
- expect(Array.isArray(nodesAtCallTime)).toBe(true);
192
- expect((nodesAtCallTime as unknown[]).length).toBe(1);
193
- expect((nodesAtCallTime as unknown[])[0]).toBe(fakeSlotChild);
194
- });
195
- });
1
+ /**
2
+ * Tests for the effect-scope mechanism used by the router to prevent effect
3
+ * accumulation across client-side navigations.
4
+ *
5
+ * Two invariants must hold forever:
6
+ * 1. Effects created during a page mount stop running when the page is disposed.
7
+ * 2. __nodes is reset to [] before each SPA navigation so the new page gets
8
+ * a fresh empty pool instead of recycling stale DOM nodes.
9
+ *
10
+ * await tick() must be called after all signal edits, given Bun's runner is
11
+ * synchronous and doesnt wait for our microtasks. In the browser the microtasks
12
+ * flush between event loop turns
13
+ */
14
+ import { describe, expect, test } from "bun:test";
15
+ import {
16
+ createEffect,
17
+ createSignal,
18
+ withEffectScope as runtimeScope,
19
+ tick,
20
+ } from "@sigil-dev/runtime";
21
+ import { withEffectScope } from "../src/client/scope.ts";
22
+
23
+ describe("grimoire withEffectScope", () => {
24
+ test("effects inside scope stop after dispose", async () => {
25
+ const count = createSignal(0);
26
+ let runs = 0;
27
+ const dispose = withEffectScope(() => {
28
+ createEffect(() => {
29
+ count();
30
+ runs++;
31
+ });
32
+ });
33
+
34
+ count.set(1);
35
+ await tick(); // let microtask queue flush
36
+ expect(runs).toBe(2);
37
+
38
+ dispose();
39
+ count.set(2);
40
+ await tick();
41
+ expect(runs).toBe(2);
42
+ });
43
+
44
+ test("simulates two page navigations — only latest page's effects run", async () => {
45
+ const count = createSignal(0);
46
+ let pageARuns = 0;
47
+ let pageBRuns = 0;
48
+
49
+ // Mount page A
50
+ let disposeCurrentPage = withEffectScope(() => {
51
+ createEffect(() => {
52
+ count();
53
+ pageARuns++;
54
+ });
55
+ });
56
+ count.set(1);
57
+ await tick();
58
+ expect(pageARuns).toBe(2);
59
+ expect(pageBRuns).toBe(0);
60
+
61
+ // Navigate to page B — dispose A first
62
+ disposeCurrentPage();
63
+ disposeCurrentPage = withEffectScope(() => {
64
+ createEffect(() => {
65
+ count();
66
+ pageBRuns++;
67
+ });
68
+ });
69
+
70
+ count.set(2);
71
+ await tick();
72
+ expect(pageARuns).toBe(2); // A's effect is gone
73
+ expect(pageBRuns).toBe(2); // B initial + update
74
+
75
+ // Navigate away from B — dispose B
76
+ disposeCurrentPage();
77
+ count.set(3);
78
+ await tick();
79
+ expect(pageARuns).toBe(2);
80
+ expect(pageBRuns).toBe(2); // B's effect is gone too
81
+ });
82
+
83
+ test("three navigations: each page's effects are independent", async () => {
84
+ const signal = createSignal("a");
85
+ const log: string[] = [];
86
+
87
+ const mountPage = (label: string) =>
88
+ withEffectScope(() => {
89
+ createEffect(() => {
90
+ log.push(`${label}:${signal()}`);
91
+ });
92
+ });
93
+
94
+ const disposeA = mountPage("A");
95
+ const disposeB = mountPage("B");
96
+
97
+ signal.set("b");
98
+ await tick();
99
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b"]);
100
+
101
+ disposeA();
102
+ signal.set("c");
103
+ await tick();
104
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // only B ran
105
+
106
+ disposeB();
107
+ signal.set("d");
108
+ await tick();
109
+ expect(log).toEqual(["A:a", "B:a", "A:b", "B:b", "B:c"]); // neither ran
110
+ });
111
+
112
+ test("grimoire scope uses same globalThis channel as runtime scope", async () => {
113
+ // Both must route through the same global so that createEffect
114
+ // in page components (compiled by sigil, imports @sigil-dev/runtime)
115
+ // is captured by grimoire's router scope.
116
+ const count = createSignal(0);
117
+ let runs = 0;
118
+
119
+ // grimoire's local withEffectScope wraps a createEffect from @sigil-dev/runtime
120
+ const dispose = withEffectScope(() => {
121
+ createEffect(() => {
122
+ count();
123
+ runs++;
124
+ });
125
+ });
126
+
127
+ count.set(1);
128
+ await tick();
129
+ expect(runs).toBe(2);
130
+
131
+ dispose();
132
+ count.set(2);
133
+ await tick();
134
+ expect(runs).toBe(2); // runtime's createEffect was captured by grimoire's scope ✓
135
+ });
136
+
137
+ test("runtime's withEffectScope and grimoire's are interchangeable", async () => {
138
+ // The runtime exports its own withEffectScope for user code.
139
+ // Both implementations must work the same way via the globalThis channel.
140
+ const count = createSignal(0);
141
+ let runs = 0;
142
+
143
+ const dispose = runtimeScope(() => {
144
+ createEffect(() => {
145
+ count();
146
+ runs++;
147
+ });
148
+ });
149
+
150
+ count.set(1);
151
+ await tick();
152
+ expect(runs).toBe(2);
153
+ dispose();
154
+ count.set(2);
155
+ await tick();
156
+ expect(runs).toBe(2);
157
+ });
158
+ });
159
+
160
+ describe("__nodes pool reset on SPA navigation", () => {
161
+ test("navigate sets __nodes to [] before calling Page()", () => {
162
+ // Simulate exactly what router.ts's navigate() does:
163
+ // dispose old effects → set __nodes = [] → call Page() → replaceChildren(node)
164
+ // We only care that Page() sees an empty pool.
165
+ let nodesAtCallTime: unknown = "not set";
166
+
167
+ const mockPage = () => {
168
+ nodesAtCallTime = (globalThis as any).__nodes;
169
+ return document.createElement("div"); // satisfy replaceChildren
170
+ };
171
+ // Mimic the three lines in navigate() that matter
172
+ (globalThis as any).__nodes = ["stale", "dom", "nodes"]; // leftover from previous page
173
+ (globalThis as any).__nodes = []; // the reset
174
+ mockPage();
175
+
176
+ expect(nodesAtCallTime).toEqual([]);
177
+ });
178
+
179
+ test("Page() called during SSR hydration sees real DOM nodes, not empty pool", () => {
180
+ // Contrast: hydrate.ts sets __nodes to Array.from(slot.childNodes) before calling Page()
181
+ const fakeSlotChild = document.createElement("div");
182
+ let nodesAtCallTime: unknown = "not set";
183
+
184
+ const mockPage = () => {
185
+ nodesAtCallTime = (globalThis as any).__nodes;
186
+ };
187
+
188
+ (globalThis as any).__nodes = [fakeSlotChild]; // hydrate.ts sets real nodes
189
+ mockPage();
190
+
191
+ expect(Array.isArray(nodesAtCallTime)).toBe(true);
192
+ expect((nodesAtCallTime as unknown[]).length).toBe(1);
193
+ expect((nodesAtCallTime as unknown[])[0]).toBe(fakeSlotChild);
194
+ });
195
+ });