@krate/runtime 0.4.0-rc.3 → 0.4.0-rc.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.
- package/dist/dev-overlay.d.ts +15 -0
- package/dist/dev-overlay.js +446 -0
- package/dist/dev-overlay.test.d.ts +1 -0
- package/dist/dev-overlay.test.js +99 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/krate-dev.js +373 -0
- package/dist/krate-hydrate.js +1 -1
- package/dist/krate-runtime.js +1 -1
- package/dist/router.d.ts +12 -0
- package/dist/router.js +235 -50
- package/dist/server-renderer.js +50 -9
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev-only error overlay + toolbar.
|
|
3
|
+
*
|
|
4
|
+
* Bundled separately as `krate-dev.js` and injected only by `krate dev`; it is
|
|
5
|
+
* never part of the production runtime. It renders structured compiler
|
|
6
|
+
* diagnostics (file:line:col + source + caret + hint) and client runtime errors,
|
|
7
|
+
* forwards runtime errors back to the dev server, and offers open-in-editor.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Route matcher: a segment starting with `[` matches anything; `[...x]` matches
|
|
11
|
+
* the rest. Exported (pure) for testing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function routeMatches(pattern: string, pathname: string, basePath?: string): boolean;
|
|
14
|
+
/** Initialise the dev overlay. Safe to call more than once. */
|
|
15
|
+
export declare function initDevOverlay(): void;
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev-only error overlay + toolbar.
|
|
3
|
+
*
|
|
4
|
+
* Bundled separately as `krate-dev.js` and injected only by `krate dev`; it is
|
|
5
|
+
* never part of the production runtime. It renders structured compiler
|
|
6
|
+
* diagnostics (file:line:col + source + caret + hint) and client runtime errors,
|
|
7
|
+
* forwards runtime errors back to the dev server, and offers open-in-editor.
|
|
8
|
+
*/
|
|
9
|
+
function devConfig() {
|
|
10
|
+
const raw = window.__KRATE_DEV__;
|
|
11
|
+
const runtime = window.__KRATE_CFG__;
|
|
12
|
+
return {
|
|
13
|
+
sse: raw?.sse ?? '/__krate/hotreload',
|
|
14
|
+
errors: raw?.errors ?? '/__krate/client-error',
|
|
15
|
+
open: raw?.open ?? '/__krate/open',
|
|
16
|
+
overlay: raw?.overlay ?? true,
|
|
17
|
+
toolbar: raw?.toolbar ?? true,
|
|
18
|
+
basePath: raw?.basePath ?? runtime?.basePath ?? '',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function esc(s) {
|
|
22
|
+
return String(s)
|
|
23
|
+
.replace(/&/g, '&')
|
|
24
|
+
.replace(/</g, '<')
|
|
25
|
+
.replace(/>/g, '>')
|
|
26
|
+
.replace(/"/g, '"')
|
|
27
|
+
.replace(/'/g, ''');
|
|
28
|
+
}
|
|
29
|
+
function norm(p) {
|
|
30
|
+
return p.replace(/\/+$/, '') || '/';
|
|
31
|
+
}
|
|
32
|
+
/** Strips the configured base path so match patterns (unprefixed) line up. */
|
|
33
|
+
function stripBase(pathname, basePath) {
|
|
34
|
+
if (basePath && (pathname === basePath || pathname.startsWith(basePath + '/'))) {
|
|
35
|
+
const rest = pathname.slice(basePath.length);
|
|
36
|
+
return rest === '' ? '/' : rest;
|
|
37
|
+
}
|
|
38
|
+
return pathname;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Route matcher: a segment starting with `[` matches anything; `[...x]` matches
|
|
42
|
+
* the rest. Exported (pure) for testing.
|
|
43
|
+
*/
|
|
44
|
+
export function routeMatches(pattern, pathname, basePath = '') {
|
|
45
|
+
const a = norm(stripBase(pathname, basePath)).split('/');
|
|
46
|
+
const b = norm(pattern).split('/');
|
|
47
|
+
for (let i = 0; i < b.length; i++) {
|
|
48
|
+
const s = b[i];
|
|
49
|
+
if (s.charAt(0) === '[') {
|
|
50
|
+
if (s.indexOf('[...') === 0)
|
|
51
|
+
return true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (s !== a[i])
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return a.length === b.length;
|
|
58
|
+
}
|
|
59
|
+
const ICON_RELOAD = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" ' +
|
|
60
|
+
'stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
|
|
61
|
+
'<path d="M21 12a9 9 0 1 1-2.64-6.36"/><path d="M21 3v6h-6"/></svg>';
|
|
62
|
+
const ICON_ALERT = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" ' +
|
|
63
|
+
'stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
|
|
64
|
+
'<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/>' +
|
|
65
|
+
'<path d="M12 9v4"/><path d="M12 17h.01"/></svg>';
|
|
66
|
+
const STYLE = `
|
|
67
|
+
:host { all: initial; }
|
|
68
|
+
* { box-sizing: border-box; }
|
|
69
|
+
|
|
70
|
+
/* ── palette ─────────────────────────────────────────────────────────── */
|
|
71
|
+
:host {
|
|
72
|
+
--red: #f87171;
|
|
73
|
+
--green: #34d399;
|
|
74
|
+
--bg: #111114;
|
|
75
|
+
--bg-2: #17171c;
|
|
76
|
+
--line: #2a2a31;
|
|
77
|
+
--fg: #e5e7eb;
|
|
78
|
+
--muted: #9ca3af;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ── toolbar ─────────────────────────────────────────────────────────── */
|
|
82
|
+
.toolbar {
|
|
83
|
+
position: fixed; left: 50%; bottom: 0; z-index: 2147483001;
|
|
84
|
+
transform: translate(-50%, 54%);
|
|
85
|
+
transition: transform .28s cubic-bezier(.22,.61,.36,1),
|
|
86
|
+
border-top-color .35s ease;
|
|
87
|
+
display: flex; align-items: stretch; gap: 2px;
|
|
88
|
+
background: var(--bg);
|
|
89
|
+
border: 1px solid var(--line);
|
|
90
|
+
border-top: 3px solid var(--green);
|
|
91
|
+
border-radius: 16px 16px 0 0;
|
|
92
|
+
padding: 7px 8px 9px;
|
|
93
|
+
box-shadow: 0 -10px 30px rgba(0,0,0,.45);
|
|
94
|
+
font: 12px/1.3 ui-sans-serif, system-ui, -apple-system, sans-serif;
|
|
95
|
+
color: var(--fg);
|
|
96
|
+
}
|
|
97
|
+
.toolbar:hover, .toolbar:focus-within { transform: translate(-50%, 0); }
|
|
98
|
+
.toolbar.bad { border-top-color: var(--red); }
|
|
99
|
+
|
|
100
|
+
.tb-btn {
|
|
101
|
+
display: flex; flex-direction: column; align-items: center; justify-content: center;
|
|
102
|
+
gap: 3px; min-width: 56px; padding: 5px 10px;
|
|
103
|
+
background: transparent; border: 1px solid transparent; border-radius: 11px;
|
|
104
|
+
color: var(--muted); cursor: pointer; font: inherit;
|
|
105
|
+
transition: color .15s ease, background .15s ease;
|
|
106
|
+
}
|
|
107
|
+
.tb-btn:hover { color: var(--fg); background: rgba(255,255,255,.06); }
|
|
108
|
+
.tb-btn .ic { display: block; width: 18px; height: 18px; }
|
|
109
|
+
.tb-btn .tb-label { font-size: 10px; letter-spacing: .02em; }
|
|
110
|
+
.toolbar.bad .tb-btn.errors { color: var(--red); }
|
|
111
|
+
|
|
112
|
+
.tb-center {
|
|
113
|
+
display: flex; flex-direction: column; align-items: center; justify-content: center;
|
|
114
|
+
gap: 1px; min-width: 130px; padding: 2px 16px;
|
|
115
|
+
}
|
|
116
|
+
.tb-center .brand { font-size: 12px; font-weight: 700; letter-spacing: .05em; color: #f3f4f6; }
|
|
117
|
+
.tb-center .route {
|
|
118
|
+
font-size: 10px; color: var(--muted);
|
|
119
|
+
max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* ── overlay ─────────────────────────────────────────────────────────── */
|
|
123
|
+
.overlay {
|
|
124
|
+
position: fixed; inset: 0; z-index: 2147483000;
|
|
125
|
+
background: rgba(9, 9, 11, 0.72); backdrop-filter: blur(6px);
|
|
126
|
+
display: flex; align-items: flex-start; justify-content: center;
|
|
127
|
+
padding: 4vh 16px; overflow: auto;
|
|
128
|
+
font: 13px/1.5 "SF Mono", Menlo, Monaco, Consolas, "Courier New", monospace;
|
|
129
|
+
color: var(--fg);
|
|
130
|
+
}
|
|
131
|
+
.overlay[hidden] { display: none; }
|
|
132
|
+
.card {
|
|
133
|
+
width: min(920px, 100%); background: var(--bg);
|
|
134
|
+
border: 1px solid var(--line);
|
|
135
|
+
border-radius: 14px; box-shadow: 0 24px 64px rgba(0,0,0,0.5); overflow: hidden;
|
|
136
|
+
}
|
|
137
|
+
.card-head {
|
|
138
|
+
display: flex; align-items: center; gap: 10px; padding: 14px 18px;
|
|
139
|
+
border-bottom: 1px solid var(--line); background: var(--bg-2);
|
|
140
|
+
}
|
|
141
|
+
.title { font-weight: 600; font-size: 14px; color: var(--red); }
|
|
142
|
+
.count { color: var(--muted); font-size: 12px; }
|
|
143
|
+
.spacer { flex: 1; }
|
|
144
|
+
.btn {
|
|
145
|
+
border: 1px solid var(--line); background: transparent; color: var(--muted);
|
|
146
|
+
border-radius: 9px; padding: 6px 12px; cursor: pointer; font: inherit; font-size: 12px;
|
|
147
|
+
transition: color .15s ease, background .15s ease, border-color .15s ease;
|
|
148
|
+
}
|
|
149
|
+
.btn:hover { color: var(--fg); background: rgba(255,255,255,.06); border-color: #44444f; }
|
|
150
|
+
.list { padding: 6px 0; }
|
|
151
|
+
.diag { padding: 14px 18px; border-bottom: 1px solid #1f1f26; }
|
|
152
|
+
.diag:last-child { border-bottom: none; }
|
|
153
|
+
.actions { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
|
|
154
|
+
.loc {
|
|
155
|
+
display: inline-flex; align-items: center; gap: 6px; color: #60a5fa;
|
|
156
|
+
background: transparent; border: none; padding: 0; cursor: pointer;
|
|
157
|
+
font: inherit; font-size: 12px;
|
|
158
|
+
}
|
|
159
|
+
.loc:hover { text-decoration: underline; }
|
|
160
|
+
.msg { color: #f3f4f6; white-space: pre-wrap; word-break: break-word; }
|
|
161
|
+
.frame {
|
|
162
|
+
margin-top: 10px; background: #0b0b0e; border: 1px solid #26262e;
|
|
163
|
+
border-radius: 7px; padding: 8px 10px; overflow-x: auto; white-space: pre;
|
|
164
|
+
}
|
|
165
|
+
.frame .src { color: #cbd5e1; }
|
|
166
|
+
.frame .caret { color: var(--red); }
|
|
167
|
+
.hint { margin-top: 8px; color: #a3e635; }
|
|
168
|
+
.stack {
|
|
169
|
+
margin-top: 10px; color: var(--muted); white-space: pre-wrap; word-break: break-word;
|
|
170
|
+
max-height: 320px; overflow: auto;
|
|
171
|
+
}
|
|
172
|
+
`;
|
|
173
|
+
class KrateDev {
|
|
174
|
+
constructor() {
|
|
175
|
+
this.cfg = devConfig();
|
|
176
|
+
this.buildDiags = [];
|
|
177
|
+
this.runtimeErrors = [];
|
|
178
|
+
this.seen = new Set();
|
|
179
|
+
}
|
|
180
|
+
init() {
|
|
181
|
+
const host = document.createElement('div');
|
|
182
|
+
host.id = 'krate-dev-root';
|
|
183
|
+
(document.body || document.documentElement).appendChild(host);
|
|
184
|
+
this.shadow = host.attachShadow({ mode: 'open' });
|
|
185
|
+
const style = document.createElement('style');
|
|
186
|
+
style.textContent = STYLE;
|
|
187
|
+
this.shadow.appendChild(style);
|
|
188
|
+
this.overlay = document.createElement('div');
|
|
189
|
+
this.overlay.className = 'overlay';
|
|
190
|
+
this.overlay.hidden = true;
|
|
191
|
+
this.overlay.innerHTML =
|
|
192
|
+
'<div class="card"><div class="card-head">' +
|
|
193
|
+
'<span class="title"></span><span class="count"></span>' +
|
|
194
|
+
'<span class="spacer"></span>' +
|
|
195
|
+
'<button class="btn close" type="button">Dismiss (Esc)</button></div>' +
|
|
196
|
+
'<div class="list"></div></div>';
|
|
197
|
+
this.shadow.appendChild(this.overlay);
|
|
198
|
+
this.title = this.overlay.querySelector('.title');
|
|
199
|
+
this.list = this.overlay.querySelector('.list');
|
|
200
|
+
if (this.cfg.toolbar) {
|
|
201
|
+
this.toolbar = document.createElement('div');
|
|
202
|
+
this.toolbar.className = 'toolbar';
|
|
203
|
+
this.toolbar.innerHTML =
|
|
204
|
+
'<button class="tb-btn errors" type="button" title="Show errors">' +
|
|
205
|
+
ICON_ALERT +
|
|
206
|
+
'<span class="tb-label">Errors</span></button>' +
|
|
207
|
+
'<div class="tb-center"><span class="brand">krate</span>' +
|
|
208
|
+
'<span class="route"></span></div>' +
|
|
209
|
+
'<button class="tb-btn reload" type="button" title="Reload">' +
|
|
210
|
+
ICON_RELOAD +
|
|
211
|
+
'<span class="tb-label">Reload</span></button>';
|
|
212
|
+
this.shadow.appendChild(this.toolbar);
|
|
213
|
+
this.routeEl = this.toolbar.querySelector('.route');
|
|
214
|
+
this.routeEl.textContent = location.pathname;
|
|
215
|
+
this.watchLocation();
|
|
216
|
+
}
|
|
217
|
+
this.shadow.addEventListener('click', (e) => this.onClick(e));
|
|
218
|
+
document.addEventListener('keydown', (e) => {
|
|
219
|
+
if (e.key === 'Escape')
|
|
220
|
+
this.hide();
|
|
221
|
+
});
|
|
222
|
+
this.connectSSE();
|
|
223
|
+
this.captureRuntimeErrors();
|
|
224
|
+
}
|
|
225
|
+
onClick(e) {
|
|
226
|
+
const t = e.target.closest('.btn, .loc, .tb-btn');
|
|
227
|
+
if (!t)
|
|
228
|
+
return;
|
|
229
|
+
// Toolbar buttons are inside a `:focus-within` container that keeps the
|
|
230
|
+
// half-hidden toolbar raised; blur after acting so a click (with no errors
|
|
231
|
+
// to show) lets it slide back down. Keyboard focus still raises it.
|
|
232
|
+
if (t.classList.contains('tb-btn'))
|
|
233
|
+
t.blur();
|
|
234
|
+
if (t.classList.contains('close')) {
|
|
235
|
+
this.hide();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (t.classList.contains('errors') || t.classList.contains('toggle')) {
|
|
239
|
+
if (this.overlay.hidden)
|
|
240
|
+
this.show();
|
|
241
|
+
else
|
|
242
|
+
this.hide();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (t.classList.contains('reload')) {
|
|
246
|
+
location.reload();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (t.classList.contains('loc') || t.classList.contains('open')) {
|
|
250
|
+
this.openInEditor(t.getAttribute('data-file') || '', t.getAttribute('data-line') || '');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
openInEditor(file, line) {
|
|
254
|
+
if (!file)
|
|
255
|
+
return;
|
|
256
|
+
const url = this.cfg.open + '?file=' + encodeURIComponent(file) + '&line=' + encodeURIComponent(line);
|
|
257
|
+
fetch(url).catch(() => { });
|
|
258
|
+
}
|
|
259
|
+
/** Keep the toolbar route label in sync with SPA (history) navigation. */
|
|
260
|
+
watchLocation() {
|
|
261
|
+
const update = () => {
|
|
262
|
+
if (this.routeEl)
|
|
263
|
+
this.routeEl.textContent = location.pathname;
|
|
264
|
+
};
|
|
265
|
+
window.addEventListener('popstate', update);
|
|
266
|
+
const h = history;
|
|
267
|
+
const push = h.pushState.bind(history);
|
|
268
|
+
h.pushState = function (...args) {
|
|
269
|
+
const r = push(...args);
|
|
270
|
+
update();
|
|
271
|
+
return r;
|
|
272
|
+
};
|
|
273
|
+
const replace = h.replaceState.bind(history);
|
|
274
|
+
h.replaceState = function (...args) {
|
|
275
|
+
const r = replace(...args);
|
|
276
|
+
update();
|
|
277
|
+
return r;
|
|
278
|
+
};
|
|
279
|
+
// Safety net for routers that swap content without touching history.
|
|
280
|
+
setInterval(update, 500);
|
|
281
|
+
}
|
|
282
|
+
connectSSE() {
|
|
283
|
+
let sse;
|
|
284
|
+
try {
|
|
285
|
+
sse = new EventSource(this.cfg.sse);
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
sse.addEventListener('reload', (e) => {
|
|
291
|
+
try {
|
|
292
|
+
const d = JSON.parse(e.data);
|
|
293
|
+
if (d.pages &&
|
|
294
|
+
Array.isArray(d.pages) &&
|
|
295
|
+
!d.pages.some((p) => routeMatches(p, location.pathname, this.cfg.basePath))) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
/* full reload */
|
|
301
|
+
}
|
|
302
|
+
location.reload();
|
|
303
|
+
});
|
|
304
|
+
sse.addEventListener('build-error', (e) => {
|
|
305
|
+
try {
|
|
306
|
+
const d = JSON.parse(e.data);
|
|
307
|
+
this.buildDiags = Array.isArray(d.diagnostics) ? d.diagnostics : [];
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
this.buildDiags = [];
|
|
311
|
+
}
|
|
312
|
+
this.setStatus(this.buildDiags.length === 0);
|
|
313
|
+
this.render();
|
|
314
|
+
});
|
|
315
|
+
sse.addEventListener('client-error', (e) => {
|
|
316
|
+
try {
|
|
317
|
+
const d = JSON.parse(e.data);
|
|
318
|
+
if (d.clientError)
|
|
319
|
+
this.addRuntime('Runtime Error', d.clientError.message, d.clientError.stack);
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
/* ignore */
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
captureRuntimeErrors() {
|
|
327
|
+
window.addEventListener('error', (e) => {
|
|
328
|
+
const stack = e.error?.stack || '';
|
|
329
|
+
this.report({
|
|
330
|
+
message: e.message || 'Unknown error',
|
|
331
|
+
stack,
|
|
332
|
+
url: e.filename,
|
|
333
|
+
line: e.lineno,
|
|
334
|
+
col: e.colno,
|
|
335
|
+
kind: 'error',
|
|
336
|
+
});
|
|
337
|
+
this.addRuntime('Runtime Error', e.message || 'Unknown error', stack);
|
|
338
|
+
});
|
|
339
|
+
window.addEventListener('unhandledrejection', (e) => {
|
|
340
|
+
const reason = e.reason;
|
|
341
|
+
const message = reason instanceof Error ? reason.message : String(reason);
|
|
342
|
+
const stack = reason instanceof Error ? reason.stack : '';
|
|
343
|
+
this.report({ message, stack, url: location.href, kind: 'unhandledrejection' });
|
|
344
|
+
this.addRuntime('Unhandled Promise Rejection', message, stack);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
report(payload) {
|
|
348
|
+
const key = payload.message + '|' + (payload.stack || '');
|
|
349
|
+
if (this.seen.has(key))
|
|
350
|
+
return;
|
|
351
|
+
this.seen.add(key);
|
|
352
|
+
try {
|
|
353
|
+
const body = JSON.stringify(payload);
|
|
354
|
+
if (navigator.sendBeacon) {
|
|
355
|
+
navigator.sendBeacon(this.cfg.errors, new Blob([body], { type: 'application/json' }));
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
fetch(this.cfg.errors, { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, keepalive: true });
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
/* reporting is best-effort */
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
addRuntime(title, message, stack) {
|
|
366
|
+
this.runtimeErrors.push({ title, message, stack });
|
|
367
|
+
this.setStatus(false);
|
|
368
|
+
this.render();
|
|
369
|
+
}
|
|
370
|
+
setStatus(ok) {
|
|
371
|
+
if (!this.toolbar)
|
|
372
|
+
return;
|
|
373
|
+
this.toolbar.classList.toggle('bad', !ok);
|
|
374
|
+
}
|
|
375
|
+
frameHTML(d) {
|
|
376
|
+
if (!d.source)
|
|
377
|
+
return '';
|
|
378
|
+
const col = Math.max(1, d.col || 1);
|
|
379
|
+
const caret = ' '.repeat(col - 1) + '^';
|
|
380
|
+
return '<div class="frame"><span class="src">' + esc(d.source) + '</span>\n<span class="caret">' + esc(caret) + '</span></div>';
|
|
381
|
+
}
|
|
382
|
+
diagHTML(d) {
|
|
383
|
+
let actions = '';
|
|
384
|
+
if (d.file) {
|
|
385
|
+
const loc = d.file + (d.line ? ':' + d.line + (d.col ? ':' + d.col : '') : '');
|
|
386
|
+
actions =
|
|
387
|
+
'<div class="actions">' +
|
|
388
|
+
'<button class="loc" data-file="' + esc(d.file) + '" data-line="' + esc(d.line || '') + '">' + esc(loc) + '</button>' +
|
|
389
|
+
'<button class="btn open" data-file="' + esc(d.file) + '" data-line="' + esc(d.line || '') + '">Open in editor</button>' +
|
|
390
|
+
'</div>';
|
|
391
|
+
}
|
|
392
|
+
const hint = d.hint ? '<div class="hint">hint: ' + esc(d.hint) + '</div>' : '';
|
|
393
|
+
return '<div class="diag">' + actions + '<div class="msg">' + esc(d.message) + '</div>' + this.frameHTML(d) + hint + '</div>';
|
|
394
|
+
}
|
|
395
|
+
runtimeHTML(r) {
|
|
396
|
+
const stack = r.stack ? '<div class="stack">' + esc(r.stack) + '</div>' : '';
|
|
397
|
+
return '<div class="diag"><div class="msg">' + esc(r.message) + '</div>' + stack + '</div>';
|
|
398
|
+
}
|
|
399
|
+
render() {
|
|
400
|
+
const total = this.buildDiags.length + this.runtimeErrors.length;
|
|
401
|
+
if (total === 0) {
|
|
402
|
+
this.hide();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
this.title.textContent = this.buildDiags.length > 0 ? 'Compilation failed' : 'Runtime error';
|
|
406
|
+
let html = '';
|
|
407
|
+
for (const d of this.buildDiags)
|
|
408
|
+
html += this.diagHTML(d);
|
|
409
|
+
for (const r of this.runtimeErrors)
|
|
410
|
+
html += this.runtimeHTML(r);
|
|
411
|
+
this.list.innerHTML = html;
|
|
412
|
+
this.overlay.querySelector('.count').textContent = total > 1 ? String(total) : '';
|
|
413
|
+
if (this.cfg.overlay)
|
|
414
|
+
this.overlay.hidden = false;
|
|
415
|
+
}
|
|
416
|
+
show() {
|
|
417
|
+
if (this.buildDiags.length + this.runtimeErrors.length > 0)
|
|
418
|
+
this.overlay.hidden = false;
|
|
419
|
+
}
|
|
420
|
+
hide() {
|
|
421
|
+
this.overlay.hidden = true;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
/** Initialise the dev overlay. Safe to call more than once. */
|
|
425
|
+
export function initDevOverlay() {
|
|
426
|
+
if (typeof window === 'undefined' || typeof document === 'undefined')
|
|
427
|
+
return;
|
|
428
|
+
if (window.__krate_dev_overlay)
|
|
429
|
+
return;
|
|
430
|
+
window.__krate_dev_overlay = true;
|
|
431
|
+
try {
|
|
432
|
+
new KrateDev().init();
|
|
433
|
+
}
|
|
434
|
+
catch (err) {
|
|
435
|
+
// Never let the dev tooling break the app.
|
|
436
|
+
console.error('[krate] dev overlay failed to initialise', err);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
440
|
+
if (document.readyState === 'loading') {
|
|
441
|
+
document.addEventListener('DOMContentLoaded', initDevOverlay, { once: true });
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
initDevOverlay();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll } from 'vitest';
|
|
2
|
+
class FakeEventSource {
|
|
3
|
+
constructor(url) {
|
|
4
|
+
this.url = url;
|
|
5
|
+
this.listeners = {};
|
|
6
|
+
FakeEventSource.last = this;
|
|
7
|
+
}
|
|
8
|
+
addEventListener(type, fn) {
|
|
9
|
+
var _a;
|
|
10
|
+
((_a = this.listeners)[type] || (_a[type] = [])).push(fn);
|
|
11
|
+
}
|
|
12
|
+
emit(type, data) {
|
|
13
|
+
for (const fn of this.listeners[type] || [])
|
|
14
|
+
fn({ data: JSON.stringify(data) });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
FakeEventSource.last = null;
|
|
18
|
+
let routeMatches;
|
|
19
|
+
// File-level setup: globals must exist BEFORE the overlay module auto-inits on
|
|
20
|
+
// import, and the export is captured from the same module instance.
|
|
21
|
+
beforeAll(async () => {
|
|
22
|
+
globalThis.EventSource = FakeEventSource;
|
|
23
|
+
window.__KRATE_DEV__ = {
|
|
24
|
+
sse: '/__krate/hotreload',
|
|
25
|
+
errors: '/__krate/client-error',
|
|
26
|
+
open: '/__krate/open',
|
|
27
|
+
overlay: true,
|
|
28
|
+
toolbar: true,
|
|
29
|
+
};
|
|
30
|
+
const mod = await import('./dev-overlay');
|
|
31
|
+
routeMatches = mod.routeMatches;
|
|
32
|
+
});
|
|
33
|
+
function devRoot() {
|
|
34
|
+
const host = document.getElementById('krate-dev-root');
|
|
35
|
+
return host ? host.shadowRoot : null;
|
|
36
|
+
}
|
|
37
|
+
describe('dev-overlay', () => {
|
|
38
|
+
it('mounts a shadow-root host with a toolbar', () => {
|
|
39
|
+
const shadow = devRoot();
|
|
40
|
+
expect(shadow).toBeTruthy();
|
|
41
|
+
expect(shadow.querySelector('.toolbar')).toBeTruthy();
|
|
42
|
+
expect(shadow.querySelector('.overlay')).toBeTruthy();
|
|
43
|
+
});
|
|
44
|
+
it('renders structured build diagnostics with location, frame and hint', () => {
|
|
45
|
+
FakeEventSource.last.emit('build-error', {
|
|
46
|
+
diagnostics: [
|
|
47
|
+
{
|
|
48
|
+
file: 'a.tsx',
|
|
49
|
+
line: 3,
|
|
50
|
+
col: 5,
|
|
51
|
+
message: 'boom',
|
|
52
|
+
hint: 'fix it',
|
|
53
|
+
source: 'const x = 1;',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
buildOk: false,
|
|
57
|
+
});
|
|
58
|
+
const overlay = devRoot().querySelector('.overlay');
|
|
59
|
+
expect(overlay.hidden).toBe(false);
|
|
60
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
61
|
+
expect(text).toContain('boom');
|
|
62
|
+
expect(text).toContain('a.tsx:3:5');
|
|
63
|
+
expect(text).toContain('fix it');
|
|
64
|
+
expect(text).toContain('^');
|
|
65
|
+
const open = devRoot().querySelector('.open');
|
|
66
|
+
expect(open).toBeTruthy();
|
|
67
|
+
expect(open.textContent).toContain('Open in editor');
|
|
68
|
+
});
|
|
69
|
+
it('updates the toolbar route on SPA (history) navigation', () => {
|
|
70
|
+
history.pushState({}, '', '/some/other/route');
|
|
71
|
+
const route = devRoot().querySelector('.route');
|
|
72
|
+
expect(route.textContent).toBe('/some/other/route');
|
|
73
|
+
});
|
|
74
|
+
it('shows runtime errors from client-error events', () => {
|
|
75
|
+
FakeEventSource.last.emit('client-error', {
|
|
76
|
+
clientError: { message: 'kaboom', stack: 'at foo (a.tsx:1:1)' },
|
|
77
|
+
});
|
|
78
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
79
|
+
expect(text).toContain('kaboom');
|
|
80
|
+
});
|
|
81
|
+
it('hides the overlay on Escape', () => {
|
|
82
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
|
|
83
|
+
const overlay = devRoot().querySelector('.overlay');
|
|
84
|
+
expect(overlay.hidden).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
describe('routeMatches', () => {
|
|
88
|
+
it('matches unprefixed patterns against a base-path URL', () => {
|
|
89
|
+
expect(routeMatches('/about', '/docs/about', '/docs')).toBe(true);
|
|
90
|
+
expect(routeMatches('/about', '/docs/other', '/docs')).toBe(false);
|
|
91
|
+
expect(routeMatches('/', '/docs/', '/docs')).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
it('matches root deployment and dynamic segments', () => {
|
|
94
|
+
expect(routeMatches('/about', '/about', '')).toBe(true);
|
|
95
|
+
expect(routeMatches('/blog/[slug]', '/blog/hello', '')).toBe(true);
|
|
96
|
+
expect(routeMatches('/blog/[slug]', '/blog/hello/x', '')).toBe(false);
|
|
97
|
+
expect(routeMatches('/docs/[...rest]', '/docs/a/b/c', '')).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ export { clearPersisted, readPersisted, writePersisted } from './storage.js';
|
|
|
4
4
|
export type { PersistOptions, PersistSpec, PersistStore, StorageLike } from './storage.js';
|
|
5
5
|
export { h, mount, hydrate, insert, clearNodes, disposeNode } from './dom.js';
|
|
6
6
|
export { Fragment, jsx, jsxs } from './jsx-runtime.js';
|
|
7
|
-
export { initRouter, reinitRouter } from './router.js';
|
|
7
|
+
export { initRouter, reinitRouter, navigate } from './router.js';
|
|
8
|
+
export type { NavigateOptions } from './router.js';
|
|
8
9
|
export { clsx, cn, cva, twMerge, Slot, cloneElement } from './class-helpers.js';
|
|
9
10
|
export type { ClassValue, CVAConfig } from './class-helpers.js';
|
|
10
11
|
export { createContext, resetContexts } from './context.js';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export { createSignal, createReducer, createCSSChoice, createCSSToggle, createCS
|
|
|
2
2
|
export { clearPersisted, readPersisted, writePersisted } from './storage.js';
|
|
3
3
|
export { h, mount, hydrate, insert, clearNodes, disposeNode } from './dom.js';
|
|
4
4
|
export { Fragment, jsx, jsxs } from './jsx-runtime.js';
|
|
5
|
-
export { initRouter, reinitRouter } from './router.js';
|
|
5
|
+
export { initRouter, reinitRouter, navigate } from './router.js';
|
|
6
6
|
export { clsx, cn, cva, twMerge, Slot, cloneElement } from './class-helpers.js';
|
|
7
7
|
export { createContext, resetContexts } from './context.js';
|
|
8
8
|
export { createResource } from './resource.js';
|