@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,185 +1,202 @@
1
- export const HMR_CLIENT_SOURCE = `
2
- (function() {
3
- let version = 0;
4
-
5
- const staleRoutes = new Set();
6
-
7
- async function applySwap(p) {
8
- try {
9
- captureSignals(); // ← capture before
10
- const url = '/__grimoire__/m/' + p.modUrl + '?v=' + p.version;
11
- const mod = await import(url);
12
- clearCapture(); // ← clear after
13
-
14
- if (window.__grimoire_routes__) {
15
- window.__grimoire_routes__[p.pattern] = mod.default;
16
- }
17
- const currentPattern = window.__grimoire_current_pattern__;
18
- if (currentPattern === p.pattern && window.__grimoire_rerender__) {
19
- await window.__grimoire_rerender__();
20
- }
21
- } catch (e) {
22
- clearCapture();
23
- console.error('[sigil hmr] swap failed, reloading:', e);
24
- location.reload();
25
- }
26
- }
27
-
28
- async function refetchCurrent() {
29
- if (window.__grimoire_navigate__) {
30
- await window.__grimoire_navigate__(location.pathname, true);
31
- } else {
32
- location.reload();
33
- }
34
- }
35
-
36
- function applyCss(hash, text) {
37
- let el = document.getElementById('sigil-' + hash);
38
- if (!el) {
39
- el = document.createElement('style');
40
- el.id = 'sigil-' + hash;
41
- document.head.appendChild(el);
42
- }
43
- el.textContent = text;
44
- }
45
-
46
- const __hmrRegistry__ = new Map(); // key -> signal getter fn
47
-
48
- window.__hmrAdopt__ = function(key, initial) {
49
- if (!window.__hmrCapture__) return initial;
50
- if (!window.__hmrCapture__.has(key)) return initial;
51
- const stored = window.__hmrCapture__.get(key);
52
- // shape guard — only adopt if same type and array-ness
53
- if (typeof stored !== typeof initial) return initial;
54
- if (Array.isArray(stored) !== Array.isArray(initial)) return initial;
55
- return stored;
56
- };
57
-
58
- window.__hmrRegister__ = function(key, signal) {
59
- __hmrRegistry__.set(key, signal);
60
- };
61
-
62
- function captureSignals() {
63
- window.__hmrCapture__ = new Map();
64
- for (const [key, signal] of __hmrRegistry__) {
65
- try { window.__hmrCapture__.set(key, signal()); } catch {}
66
- }
67
- }
68
-
69
- function clearCapture() {
70
- window.__hmrCapture__ = null;
71
- }
72
-
73
- function connect() {
74
- const ws = new WebSocket(
75
- (location.protocol === 'https:' ? 'wss' : 'ws') +
76
- '://' + location.host + '/__grimoire__/hmr'
77
- );
78
-
79
- ws.onopen = () => {
80
- console.log('[sigil hmr] connected');
81
- document.getElementById('__grimoire_overlay__')?.remove();
82
- };
83
-
84
- ws.onmessage = async (ev) => {
85
- const msg = JSON.parse(ev.data);
86
- if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
87
- if (msg.v !== undefined && msg.v <= version) return;
88
- if (msg.v !== undefined) version = msg.v;
89
-
90
- if (msg.type === 'batch') {
91
- document.getElementById('__grimoire_overlay__')?.remove();
92
-
93
- let hasReload = false;
94
- let swapPayload = null;
95
- const cssPayloads = [];
96
- const stalePatterns = [];
97
-
98
- for (const p of msg.payloads) {
99
- if (p.kind === 'reload') { hasReload = true; break; }
100
- if (p.kind === 'css') cssPayloads.push(p);
101
- if (p.kind === 'swap') swapPayload = p;
102
- if (p.kind === 'stale') stalePatterns.push(...p.patterns);
103
- if (p.kind === 'invalidate') refetchCurrent();
104
- }
105
-
106
- if (hasReload) { location.reload(); return; }
107
-
108
- // apply CSS instantly
109
- for (const p of cssPayloads) applyCss(p.hash, p.text);
110
-
111
- // mark stale routes
112
- for (const pattern of stalePatterns) staleRoutes.add(pattern);
113
-
114
- // apply swap
115
- if (swapPayload) {
116
- await applySwap(swapPayload);
117
- }
118
- }
119
- };
120
-
121
- ws.onclose = () => {
122
- console.log('[sigil hmr] disconnected, reconnecting...');
123
- const retry = setInterval(async () => {
124
- try {
125
- await fetch('/', { method: 'HEAD' });
126
- clearInterval(retry);
127
- location.reload();
128
- } catch {}
129
- }, 300);
130
- };
131
- }
132
-
133
- function stripAnsi(str) {
134
- return str.replace(/\u001b[[0-9;]*m/g, '');
135
- }
136
-
137
- function unescapeHtml(str) {
138
- let result = str;
139
- // run twice to handle double-escaped entities
140
- for (let i = 0; i < 2; i++) {
141
- result = result
142
- .replace(/&amp;/g, '&')
143
- .replace(/&lt;/g, '<')
144
- .replace(/&gt;/g, '>')
145
- .replace(/&quot;/g, '"')
146
- .replace(/&#39;/g, "'");
147
- }
148
- return result;
149
- }
150
-
151
- function showOverlay(message, loc) {
152
- document.getElementById('__grimoire_overlay__')?.remove();
153
- const clean = unescapeHtml(stripAnsi(message));
154
- const d = document.createElement('div');
155
- d.id = '__grimoire_overlay__';
156
- d.style.cssText = [
157
- 'position:fixed;inset:0;z-index:99999',
158
- 'background:rgba(15,15,20,.94)',
159
- 'color:#ff8a8a',
160
- 'font:13px/1.6 monospace',
161
- 'padding:40px',
162
- 'overflow:auto',
163
- 'white-space:pre-wrap',
164
- ].join(';');
165
-
166
- d.textContent = '[sigil] compile error\\n\\n' + clean;
167
-
168
- if (loc) {
169
- const link = document.createElement('a');
170
- link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
171
- '&line=' + loc.line + '&col=' + loc.col;
172
- link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
173
- link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
174
- link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
175
- d.appendChild(link);
176
- }
177
-
178
- d.title = 'Click anywhere to dismiss';
179
- d.onclick = (ev) => { if (ev.target === d) d.remove(); };
180
- document.body.appendChild(d);
181
- }
182
-
183
- connect();
184
- })();
185
- `;
1
+ export const HMR_CLIENT_SOURCE = `
2
+ (function() {
3
+ let version = 0;
4
+
5
+ const moduleUrl = (entry) =>
6
+ '/__grimoire__/m/' + entry.modUrl + '?v=' + entry.version;
7
+
8
+ /**
9
+ * Apply one server batch. Route and layout modules are re-imported at
10
+ * their new versions; the module the browser fetches carries version
11
+ * stamps on its own imports, so an edited component two levels down comes
12
+ * back fresh instead of out of the module cache.
13
+ */
14
+ async function applyBatch(payloads) {
15
+ const css = [];
16
+ const routes = [];
17
+ const layouts = [];
18
+ let invalidate = false;
19
+
20
+ for (const p of payloads) {
21
+ if (p.kind === 'reload') { location.reload(); return; }
22
+ if (p.kind === 'css') css.push(p);
23
+ if (p.kind === 'routes') routes.push(...p.entries);
24
+ if (p.kind === 'layouts') layouts.push(...p.entries);
25
+ if (p.kind === 'invalidate') invalidate = true;
26
+ }
27
+
28
+ for (const p of css) applyCss(p.hash, p.text);
29
+
30
+ const hmr = window.__grimoire_hmr__;
31
+ if (!hmr) { location.reload(); return; }
32
+
33
+ const current = window.__grimoire_current_pattern__;
34
+ captureSignals();
35
+ try {
36
+ // layouts wrap every page: always needed, so import eagerly
37
+ for (const entry of layouts) {
38
+ const mod = await import(moduleUrl(entry));
39
+ if (!mod.default) throw new Error('layout ' + entry.path + ' has no default export');
40
+ hmr.setLayout(entry.path, mod.default);
41
+ }
42
+
43
+ let touchesCurrent = layouts.length > 0;
44
+ for (const entry of routes) {
45
+ if (entry.pattern === current) {
46
+ const mod = await import(moduleUrl(entry));
47
+ if (!mod.default) throw new Error('route ' + entry.pattern + ' has no default export');
48
+ hmr.setRoute(entry.pattern, mod.default);
49
+ touchesCurrent = true;
50
+ } else {
51
+ // not on screen — re-register lazily so the next
52
+ // navigation picks up the new version
53
+ const url = moduleUrl(entry);
54
+ hmr.setRoute(entry.pattern, { __lazy: () => import(url) });
55
+ }
56
+ }
57
+
58
+ // a changed load()/action means the rendered data is stale too:
59
+ // refetch instead of re-rendering against the old payload
60
+ if (invalidate) await refetchCurrent();
61
+ else if (touchesCurrent) await hmr.rerender();
62
+ } catch (e) {
63
+ console.error('[sigil hmr] swap failed, reloading:', e);
64
+ location.reload();
65
+ } finally {
66
+ clearCapture();
67
+ }
68
+ }
69
+
70
+ async function refetchCurrent() {
71
+ if (window.__grimoire_navigate__) {
72
+ await window.__grimoire_navigate__(location.pathname, true);
73
+ } else {
74
+ location.reload();
75
+ }
76
+ }
77
+
78
+ function applyCss(hash, text) {
79
+ let el = document.getElementById('sigil-' + hash);
80
+ if (!el) {
81
+ el = document.createElement('style');
82
+ el.id = 'sigil-' + hash;
83
+ document.head.appendChild(el);
84
+ }
85
+ el.textContent = text;
86
+ }
87
+
88
+ const __hmrRegistry__ = new Map(); // key -> signal getter fn
89
+
90
+ window.__hmrAdopt__ = function(key, initial) {
91
+ if (!window.__hmrCapture__) return initial;
92
+ if (!window.__hmrCapture__.has(key)) return initial;
93
+ const stored = window.__hmrCapture__.get(key);
94
+ // shape guard — only adopt if same type and array-ness
95
+ if (typeof stored !== typeof initial) return initial;
96
+ if (Array.isArray(stored) !== Array.isArray(initial)) return initial;
97
+ return stored;
98
+ };
99
+
100
+ window.__hmrRegister__ = function(key, signal) {
101
+ __hmrRegistry__.set(key, signal);
102
+ };
103
+
104
+ function captureSignals() {
105
+ window.__hmrCapture__ = new Map();
106
+ for (const [key, signal] of __hmrRegistry__) {
107
+ try { window.__hmrCapture__.set(key, signal()); } catch {}
108
+ }
109
+ }
110
+
111
+ function clearCapture() {
112
+ window.__hmrCapture__ = null;
113
+ }
114
+
115
+ function connect() {
116
+ const ws = new WebSocket(
117
+ (location.protocol === 'https:' ? 'wss' : 'ws') +
118
+ '://' + location.host + '/__grimoire__/hmr'
119
+ );
120
+
121
+ ws.onopen = () => {
122
+ console.log('[sigil hmr] connected');
123
+ document.getElementById('__grimoire_overlay__')?.remove();
124
+ };
125
+
126
+ ws.onmessage = async (ev) => {
127
+ const msg = JSON.parse(ev.data);
128
+ if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
129
+ if (msg.v !== undefined && msg.v <= version) return;
130
+ if (msg.v !== undefined) version = msg.v;
131
+
132
+ if (msg.type === 'batch') {
133
+ document.getElementById('__grimoire_overlay__')?.remove();
134
+ await applyBatch(msg.payloads);
135
+ }
136
+ };
137
+
138
+ ws.onclose = () => {
139
+ console.log('[sigil hmr] disconnected, reconnecting...');
140
+ const retry = setInterval(async () => {
141
+ try {
142
+ await fetch('/', { method: 'HEAD' });
143
+ clearInterval(retry);
144
+ location.reload();
145
+ } catch {}
146
+ }, 300);
147
+ };
148
+ }
149
+
150
+ function stripAnsi(str) {
151
+ return str.replace(/\u001b[[0-9;]*m/g, '');
152
+ }
153
+
154
+ function unescapeHtml(str) {
155
+ let result = str;
156
+ // run twice to handle double-escaped entities
157
+ for (let i = 0; i < 2; i++) {
158
+ result = result
159
+ .replace(/&amp;/g, '&')
160
+ .replace(/&lt;/g, '<')
161
+ .replace(/&gt;/g, '>')
162
+ .replace(/&quot;/g, '"')
163
+ .replace(/&#39;/g, "'");
164
+ }
165
+ return result;
166
+ }
167
+
168
+ function showOverlay(message, loc) {
169
+ document.getElementById('__grimoire_overlay__')?.remove();
170
+ const clean = unescapeHtml(stripAnsi(message));
171
+ const d = document.createElement('div');
172
+ d.id = '__grimoire_overlay__';
173
+ d.style.cssText = [
174
+ 'position:fixed;inset:0;z-index:99999',
175
+ 'background:rgba(15,15,20,.94)',
176
+ 'color:#ff8a8a',
177
+ 'font:13px/1.6 monospace',
178
+ 'padding:40px',
179
+ 'overflow:auto',
180
+ 'white-space:pre-wrap',
181
+ ].join(';');
182
+
183
+ d.textContent = '[sigil] compile error\\n\\n' + clean;
184
+
185
+ if (loc) {
186
+ const link = document.createElement('a');
187
+ link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
188
+ '&line=' + loc.line + '&col=' + loc.col;
189
+ link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
190
+ link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
191
+ link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
192
+ d.appendChild(link);
193
+ }
194
+
195
+ d.title = 'Click anywhere to dismiss';
196
+ d.onclick = (ev) => { if (ev.target === d) d.remove(); };
197
+ document.body.appendChild(d);
198
+ }
199
+
200
+ connect();
201
+ })();
202
+ `;