@sigil-dev/grimoire 0.9.28 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/src/dev/hmr-client.ts
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
export const HMR_CLIENT_SOURCE = `
|
|
2
|
-
(function() {
|
|
3
|
-
let version = 0;
|
|
4
|
-
|
|
5
|
-
// The router turns off the browser's scroll restoration, so a full reload
|
|
6
|
-
// would land at the top. Stash the offset and put it back after the reload.
|
|
7
|
-
const SCROLL_KEY = '__sigil_hmr_scroll__';
|
|
8
|
-
|
|
9
|
-
function reload() {
|
|
10
|
-
try {
|
|
11
|
-
sessionStorage.setItem(SCROLL_KEY, JSON.stringify({
|
|
12
|
-
path: location.pathname + location.search,
|
|
13
|
-
y: window.scrollY,
|
|
14
|
-
}));
|
|
15
|
-
} catch {}
|
|
16
|
-
location.reload();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
(function restoreScroll() {
|
|
20
|
-
let saved = null;
|
|
21
|
-
try {
|
|
22
|
-
saved = JSON.parse(sessionStorage.getItem(SCROLL_KEY) || 'null');
|
|
23
|
-
sessionStorage.removeItem(SCROLL_KEY);
|
|
24
|
-
} catch {}
|
|
25
|
-
if (!saved || saved.path !== location.pathname + location.search) return;
|
|
26
|
-
const apply = () => window.scrollTo(0, saved.y);
|
|
27
|
-
apply();
|
|
28
|
-
requestAnimationFrame(apply);
|
|
29
|
-
window.addEventListener('load', apply, { once: true });
|
|
30
|
-
})();
|
|
31
|
-
|
|
32
|
-
const moduleUrl = (entry) =>
|
|
33
|
-
'/__grimoire__/m/' + entry.modUrl + '?v=' + entry.version;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Apply one server batch. Route and layout modules are re-imported at
|
|
37
|
-
* their new versions; the module the browser fetches carries version
|
|
38
|
-
* stamps on its own imports, so an edited component two levels down comes
|
|
39
|
-
* back fresh instead of out of the module cache.
|
|
40
|
-
*/
|
|
41
|
-
async function applyBatch(payloads) {
|
|
42
|
-
const css = [];
|
|
43
|
-
const routes = [];
|
|
44
|
-
const layouts = [];
|
|
45
|
-
let invalidate = false;
|
|
46
|
-
|
|
47
|
-
for (const p of payloads) {
|
|
48
|
-
if (p.kind === 'reload') { reload(); return; }
|
|
49
|
-
if (p.kind === 'css') css.push(p);
|
|
50
|
-
if (p.kind === 'routes') routes.push(...p.entries);
|
|
51
|
-
if (p.kind === 'layouts') layouts.push(...p.entries);
|
|
52
|
-
if (p.kind === 'invalidate') invalidate = true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
for (const p of css) applyCss(p.hash, p.text);
|
|
56
|
-
|
|
57
|
-
const hmr = window.__grimoire_hmr__;
|
|
58
|
-
if (!hmr) { reload(); return; }
|
|
59
|
-
|
|
60
|
-
const current = window.__grimoire_current_pattern__;
|
|
61
|
-
captureSignals();
|
|
62
|
-
try {
|
|
63
|
-
// layouts wrap every page: always needed, so import eagerly
|
|
64
|
-
for (const entry of layouts) {
|
|
65
|
-
const mod = await import(moduleUrl(entry));
|
|
66
|
-
if (!mod.default) throw new Error('layout ' + entry.path + ' has no default export');
|
|
67
|
-
hmr.setLayout(entry.path, mod.default);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
let touchesCurrent = layouts.length > 0;
|
|
71
|
-
for (const entry of routes) {
|
|
72
|
-
if (entry.pattern === current) {
|
|
73
|
-
const mod = await import(moduleUrl(entry));
|
|
74
|
-
if (!mod.default) throw new Error('route ' + entry.pattern + ' has no default export');
|
|
75
|
-
hmr.setRoute(entry.pattern, mod.default);
|
|
76
|
-
touchesCurrent = true;
|
|
77
|
-
} else {
|
|
78
|
-
// not on screen — re-register lazily so the next
|
|
79
|
-
// navigation picks up the new version
|
|
80
|
-
const url = moduleUrl(entry);
|
|
81
|
-
hmr.setRoute(entry.pattern, { __lazy: () => import(url) });
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// a changed load()/action means the rendered data is stale too:
|
|
86
|
-
// refetch instead of re-rendering against the old payload
|
|
87
|
-
if (invalidate) await refetchCurrent();
|
|
88
|
-
else if (touchesCurrent) await hmr.rerender();
|
|
89
|
-
} catch (e) {
|
|
90
|
-
console.error('[sigil hmr] swap failed, reloading:', e);
|
|
91
|
-
reload();
|
|
92
|
-
} finally {
|
|
93
|
-
clearCapture();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function refetchCurrent() {
|
|
98
|
-
if (window.__grimoire_navigate__) {
|
|
99
|
-
await window.__grimoire_navigate__(location.pathname, true);
|
|
100
|
-
} else {
|
|
101
|
-
reload();
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function applyCss(hash, text) {
|
|
106
|
-
let el = document.getElementById('sigil-' + hash);
|
|
107
|
-
if (!el) {
|
|
108
|
-
el = document.createElement('style');
|
|
109
|
-
el.id = 'sigil-' + hash;
|
|
110
|
-
document.head.appendChild(el);
|
|
111
|
-
}
|
|
112
|
-
el.textContent = text;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const __hmrRegistry__ = new Map(); // key -> signal getter fn
|
|
116
|
-
|
|
117
|
-
window.__hmrAdopt__ = function(key, initial) {
|
|
118
|
-
if (!window.__hmrCapture__) return initial;
|
|
119
|
-
if (!window.__hmrCapture__.has(key)) return initial;
|
|
120
|
-
const stored = window.__hmrCapture__.get(key);
|
|
121
|
-
// shape guard — only adopt if same type and array-ness
|
|
122
|
-
if (typeof stored !== typeof initial) return initial;
|
|
123
|
-
if (Array.isArray(stored) !== Array.isArray(initial)) return initial;
|
|
124
|
-
return stored;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
window.__hmrRegister__ = function(key, signal) {
|
|
128
|
-
__hmrRegistry__.set(key, signal);
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
function captureSignals() {
|
|
132
|
-
window.__hmrCapture__ = new Map();
|
|
133
|
-
for (const [key, signal] of __hmrRegistry__) {
|
|
134
|
-
try { window.__hmrCapture__.set(key, signal()); } catch {}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function clearCapture() {
|
|
139
|
-
window.__hmrCapture__ = null;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function connect() {
|
|
143
|
-
const ws = new WebSocket(
|
|
144
|
-
(location.protocol === 'https:' ? 'wss' : 'ws') +
|
|
145
|
-
'://' + location.host + '/__grimoire__/hmr'
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
ws.onopen = () => {
|
|
149
|
-
console.log('[sigil hmr] connected');
|
|
150
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
ws.onmessage = async (ev) => {
|
|
154
|
-
const msg = JSON.parse(ev.data);
|
|
155
|
-
if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
|
|
156
|
-
if (msg.v !== undefined && msg.v <= version) return;
|
|
157
|
-
if (msg.v !== undefined) version = msg.v;
|
|
158
|
-
|
|
159
|
-
if (msg.type === 'batch') {
|
|
160
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
161
|
-
await applyBatch(msg.payloads);
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
ws.onclose = () => {
|
|
166
|
-
console.log('[sigil hmr] disconnected, reconnecting...');
|
|
167
|
-
const retry = setInterval(async () => {
|
|
168
|
-
try {
|
|
169
|
-
await fetch('/', { method: 'HEAD' });
|
|
170
|
-
clearInterval(retry);
|
|
171
|
-
reload();
|
|
172
|
-
} catch {}
|
|
173
|
-
}, 300);
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function stripAnsi(str) {
|
|
178
|
-
return str.replace(/\u001b[[0-9;]*m/g, '');
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function unescapeHtml(str) {
|
|
182
|
-
let result = str;
|
|
183
|
-
// run twice to handle double-escaped entities
|
|
184
|
-
for (let i = 0; i < 2; i++) {
|
|
185
|
-
result = result
|
|
186
|
-
.replace(/&/g, '&')
|
|
187
|
-
.replace(/</g, '<')
|
|
188
|
-
.replace(/>/g, '>')
|
|
189
|
-
.replace(/"/g, '"')
|
|
190
|
-
.replace(/'/g, "'");
|
|
191
|
-
}
|
|
192
|
-
return result;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function showOverlay(message, loc) {
|
|
196
|
-
document.getElementById('__grimoire_overlay__')?.remove();
|
|
197
|
-
const clean = unescapeHtml(stripAnsi(message));
|
|
198
|
-
const d = document.createElement('div');
|
|
199
|
-
d.id = '__grimoire_overlay__';
|
|
200
|
-
d.style.cssText = [
|
|
201
|
-
'position:fixed;inset:0;z-index:99999',
|
|
202
|
-
'background:rgba(15,15,20,.94)',
|
|
203
|
-
'color:#ff8a8a',
|
|
204
|
-
'font:13px/1.6 monospace',
|
|
205
|
-
'padding:40px',
|
|
206
|
-
'overflow:auto',
|
|
207
|
-
'white-space:pre-wrap',
|
|
208
|
-
].join(';');
|
|
209
|
-
|
|
210
|
-
d.textContent = '[sigil] compile error\\n\\n' + clean;
|
|
211
|
-
|
|
212
|
-
if (loc) {
|
|
213
|
-
const link = document.createElement('a');
|
|
214
|
-
link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
|
|
215
|
-
'&line=' + loc.line + '&col=' + loc.col;
|
|
216
|
-
link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
|
|
217
|
-
link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
|
|
218
|
-
link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
|
|
219
|
-
d.appendChild(link);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
d.title = 'Click anywhere to dismiss';
|
|
223
|
-
d.onclick = (ev) => { if (ev.target === d) d.remove(); };
|
|
224
|
-
document.body.appendChild(d);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
connect();
|
|
228
|
-
})();
|
|
229
|
-
`;
|
|
1
|
+
export const HMR_CLIENT_SOURCE = `
|
|
2
|
+
(function() {
|
|
3
|
+
let version = 0;
|
|
4
|
+
|
|
5
|
+
// The router turns off the browser's scroll restoration, so a full reload
|
|
6
|
+
// would land at the top. Stash the offset and put it back after the reload.
|
|
7
|
+
const SCROLL_KEY = '__sigil_hmr_scroll__';
|
|
8
|
+
|
|
9
|
+
function reload() {
|
|
10
|
+
try {
|
|
11
|
+
sessionStorage.setItem(SCROLL_KEY, JSON.stringify({
|
|
12
|
+
path: location.pathname + location.search,
|
|
13
|
+
y: window.scrollY,
|
|
14
|
+
}));
|
|
15
|
+
} catch {}
|
|
16
|
+
location.reload();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
(function restoreScroll() {
|
|
20
|
+
let saved = null;
|
|
21
|
+
try {
|
|
22
|
+
saved = JSON.parse(sessionStorage.getItem(SCROLL_KEY) || 'null');
|
|
23
|
+
sessionStorage.removeItem(SCROLL_KEY);
|
|
24
|
+
} catch {}
|
|
25
|
+
if (!saved || saved.path !== location.pathname + location.search) return;
|
|
26
|
+
const apply = () => window.scrollTo(0, saved.y);
|
|
27
|
+
apply();
|
|
28
|
+
requestAnimationFrame(apply);
|
|
29
|
+
window.addEventListener('load', apply, { once: true });
|
|
30
|
+
})();
|
|
31
|
+
|
|
32
|
+
const moduleUrl = (entry) =>
|
|
33
|
+
'/__grimoire__/m/' + entry.modUrl + '?v=' + entry.version;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Apply one server batch. Route and layout modules are re-imported at
|
|
37
|
+
* their new versions; the module the browser fetches carries version
|
|
38
|
+
* stamps on its own imports, so an edited component two levels down comes
|
|
39
|
+
* back fresh instead of out of the module cache.
|
|
40
|
+
*/
|
|
41
|
+
async function applyBatch(payloads) {
|
|
42
|
+
const css = [];
|
|
43
|
+
const routes = [];
|
|
44
|
+
const layouts = [];
|
|
45
|
+
let invalidate = false;
|
|
46
|
+
|
|
47
|
+
for (const p of payloads) {
|
|
48
|
+
if (p.kind === 'reload') { reload(); return; }
|
|
49
|
+
if (p.kind === 'css') css.push(p);
|
|
50
|
+
if (p.kind === 'routes') routes.push(...p.entries);
|
|
51
|
+
if (p.kind === 'layouts') layouts.push(...p.entries);
|
|
52
|
+
if (p.kind === 'invalidate') invalidate = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (const p of css) applyCss(p.hash, p.text);
|
|
56
|
+
|
|
57
|
+
const hmr = window.__grimoire_hmr__;
|
|
58
|
+
if (!hmr) { reload(); return; }
|
|
59
|
+
|
|
60
|
+
const current = window.__grimoire_current_pattern__;
|
|
61
|
+
captureSignals();
|
|
62
|
+
try {
|
|
63
|
+
// layouts wrap every page: always needed, so import eagerly
|
|
64
|
+
for (const entry of layouts) {
|
|
65
|
+
const mod = await import(moduleUrl(entry));
|
|
66
|
+
if (!mod.default) throw new Error('layout ' + entry.path + ' has no default export');
|
|
67
|
+
hmr.setLayout(entry.path, mod.default);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let touchesCurrent = layouts.length > 0;
|
|
71
|
+
for (const entry of routes) {
|
|
72
|
+
if (entry.pattern === current) {
|
|
73
|
+
const mod = await import(moduleUrl(entry));
|
|
74
|
+
if (!mod.default) throw new Error('route ' + entry.pattern + ' has no default export');
|
|
75
|
+
hmr.setRoute(entry.pattern, mod.default);
|
|
76
|
+
touchesCurrent = true;
|
|
77
|
+
} else {
|
|
78
|
+
// not on screen — re-register lazily so the next
|
|
79
|
+
// navigation picks up the new version
|
|
80
|
+
const url = moduleUrl(entry);
|
|
81
|
+
hmr.setRoute(entry.pattern, { __lazy: () => import(url) });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// a changed load()/action means the rendered data is stale too:
|
|
86
|
+
// refetch instead of re-rendering against the old payload
|
|
87
|
+
if (invalidate) await refetchCurrent();
|
|
88
|
+
else if (touchesCurrent) await hmr.rerender();
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error('[sigil hmr] swap failed, reloading:', e);
|
|
91
|
+
reload();
|
|
92
|
+
} finally {
|
|
93
|
+
clearCapture();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function refetchCurrent() {
|
|
98
|
+
if (window.__grimoire_navigate__) {
|
|
99
|
+
await window.__grimoire_navigate__(location.pathname, true);
|
|
100
|
+
} else {
|
|
101
|
+
reload();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function applyCss(hash, text) {
|
|
106
|
+
let el = document.getElementById('sigil-' + hash);
|
|
107
|
+
if (!el) {
|
|
108
|
+
el = document.createElement('style');
|
|
109
|
+
el.id = 'sigil-' + hash;
|
|
110
|
+
document.head.appendChild(el);
|
|
111
|
+
}
|
|
112
|
+
el.textContent = text;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const __hmrRegistry__ = new Map(); // key -> signal getter fn
|
|
116
|
+
|
|
117
|
+
window.__hmrAdopt__ = function(key, initial) {
|
|
118
|
+
if (!window.__hmrCapture__) return initial;
|
|
119
|
+
if (!window.__hmrCapture__.has(key)) return initial;
|
|
120
|
+
const stored = window.__hmrCapture__.get(key);
|
|
121
|
+
// shape guard — only adopt if same type and array-ness
|
|
122
|
+
if (typeof stored !== typeof initial) return initial;
|
|
123
|
+
if (Array.isArray(stored) !== Array.isArray(initial)) return initial;
|
|
124
|
+
return stored;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
window.__hmrRegister__ = function(key, signal) {
|
|
128
|
+
__hmrRegistry__.set(key, signal);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function captureSignals() {
|
|
132
|
+
window.__hmrCapture__ = new Map();
|
|
133
|
+
for (const [key, signal] of __hmrRegistry__) {
|
|
134
|
+
try { window.__hmrCapture__.set(key, signal()); } catch {}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function clearCapture() {
|
|
139
|
+
window.__hmrCapture__ = null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function connect() {
|
|
143
|
+
const ws = new WebSocket(
|
|
144
|
+
(location.protocol === 'https:' ? 'wss' : 'ws') +
|
|
145
|
+
'://' + location.host + '/__grimoire__/hmr'
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
ws.onopen = () => {
|
|
149
|
+
console.log('[sigil hmr] connected');
|
|
150
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
ws.onmessage = async (ev) => {
|
|
154
|
+
const msg = JSON.parse(ev.data);
|
|
155
|
+
if (msg.type === 'error') { showOverlay(msg.message, msg.loc); return; }
|
|
156
|
+
if (msg.v !== undefined && msg.v <= version) return;
|
|
157
|
+
if (msg.v !== undefined) version = msg.v;
|
|
158
|
+
|
|
159
|
+
if (msg.type === 'batch') {
|
|
160
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
161
|
+
await applyBatch(msg.payloads);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
ws.onclose = () => {
|
|
166
|
+
console.log('[sigil hmr] disconnected, reconnecting...');
|
|
167
|
+
const retry = setInterval(async () => {
|
|
168
|
+
try {
|
|
169
|
+
await fetch('/', { method: 'HEAD' });
|
|
170
|
+
clearInterval(retry);
|
|
171
|
+
reload();
|
|
172
|
+
} catch {}
|
|
173
|
+
}, 300);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function stripAnsi(str) {
|
|
178
|
+
return str.replace(/\u001b[[0-9;]*m/g, '');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function unescapeHtml(str) {
|
|
182
|
+
let result = str;
|
|
183
|
+
// run twice to handle double-escaped entities
|
|
184
|
+
for (let i = 0; i < 2; i++) {
|
|
185
|
+
result = result
|
|
186
|
+
.replace(/&/g, '&')
|
|
187
|
+
.replace(/</g, '<')
|
|
188
|
+
.replace(/>/g, '>')
|
|
189
|
+
.replace(/"/g, '"')
|
|
190
|
+
.replace(/'/g, "'");
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function showOverlay(message, loc) {
|
|
196
|
+
document.getElementById('__grimoire_overlay__')?.remove();
|
|
197
|
+
const clean = unescapeHtml(stripAnsi(message));
|
|
198
|
+
const d = document.createElement('div');
|
|
199
|
+
d.id = '__grimoire_overlay__';
|
|
200
|
+
d.style.cssText = [
|
|
201
|
+
'position:fixed;inset:0;z-index:99999',
|
|
202
|
+
'background:rgba(15,15,20,.94)',
|
|
203
|
+
'color:#ff8a8a',
|
|
204
|
+
'font:13px/1.6 monospace',
|
|
205
|
+
'padding:40px',
|
|
206
|
+
'overflow:auto',
|
|
207
|
+
'white-space:pre-wrap',
|
|
208
|
+
].join(';');
|
|
209
|
+
|
|
210
|
+
d.textContent = '[sigil] compile error\\n\\n' + clean;
|
|
211
|
+
|
|
212
|
+
if (loc) {
|
|
213
|
+
const link = document.createElement('a');
|
|
214
|
+
link.href = '/__grimoire__/open?file=' + encodeURIComponent(loc.file) +
|
|
215
|
+
'&line=' + loc.line + '&col=' + loc.col;
|
|
216
|
+
link.style.cssText = 'color:#88aaff;text-decoration:underline;cursor:pointer;display:block;margin:8px 0 16px';
|
|
217
|
+
link.textContent = loc.file + ':' + loc.line + ':' + loc.col;
|
|
218
|
+
link.onclick = (e) => { e.preventDefault(); fetch(link.href); };
|
|
219
|
+
d.appendChild(link);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
d.title = 'Click anywhere to dismiss';
|
|
223
|
+
d.onclick = (ev) => { if (ev.target === d) d.remove(); };
|
|
224
|
+
document.body.appendChild(d);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
connect();
|
|
228
|
+
})();
|
|
229
|
+
`;
|