@svelte-vitals/vite 0.15.1 → 0.17.0

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 (3) hide show
  1. package/README.md +13 -3
  2. package/dist/index.js +5 -559
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -2,9 +2,12 @@
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/@svelte-vitals/vite)](https://www.npmjs.com/package/@svelte-vitals/vite)
4
4
 
5
- Vite/SvelteKit plugin for [svelte-vitals](https://github.com/oekazuma/svelte-vitals). It piggybacks on `vite build`, parses the **prerendered HTML's `<head>`**, and runs the same SEO rules as the CLI — library-agnostic, because it inspects the real output. Fails the build when findings reach `failOn`.
5
+ Vite/SvelteKit plugin for [svelte-vitals](https://github.com/oekazuma/svelte-vitals). Two things in one package:
6
6
 
7
- > **ESM-only** (Node 18+). Ships ES modules only; `require()` is unsupported by design.
7
+ - **Build-time gate** — piggybacks on `vite build`, parses the **prerendered HTML's `<head>`**, and runs the same rule engine as the CLI (SEO, Performance, Correctness, Security, Architecture) — library-agnostic, because it inspects the real output. Fails the build when findings reach `failOn`.
8
+ - **Live dev dashboard** — on by default during `vite dev`: a searchable, filterable code-health dashboard at `/__svelte-vitals/`, covering the whole project from startup via static analysis, refined to real rendered values as you browse.
9
+
10
+ > **ESM-only** (Node 22.13+). Ships ES modules only; `require()` is unsupported by design.
8
11
 
9
12
  ## Usage
10
13
 
@@ -18,15 +21,22 @@ export default {
18
21
  };
19
22
  ```
20
23
 
24
+ `vite dev` prints the dashboard's URL next to its own `Local:`/`Network:` lines. Add the `svelteVitalsHandle` hook (from `@svelte-vitals/vite/hooks`) to `src/hooks.server.ts` for per-route `measured` accuracy as you browse — the dashboard works without it too, from whole-project static analysis alone.
25
+
21
26
  ## Options
22
27
 
23
28
  - `failOn` — minimum severity that fails the build (default `critical`).
24
29
  - `report` — `'console' | 'json' | false` (default `console`).
25
30
  - `outFile` — write the JSON report to a path.
26
31
  - `rules` / `metaComponents` / `treatDynamicAs` — same as the CLI/core config.
32
+ - `weights` — per-category weight overrides for the combined Health score.
27
33
  - `prerenderDir` — override the prerendered-pages directory.
34
+ - `cwd` — project root (defaults to the Vite config root).
35
+ - `ui` — serve the live dev dashboard (default `true`); pass `false` to keep only the build-time gate.
36
+
37
+ Only **prerendered** routes are analyzed at build time; for SSR/dynamic routes use the `svelte-vitals` CLI, or browse them in the live dashboard during `vite dev`.
28
38
 
29
- Only **prerendered** routes are analyzed; for SSR/dynamic routes use the `svelte-vitals` CLI.
39
+ See [Live dashboard](https://oekazuma.github.io/svelte-vitals/guides/dev-dashboard/) and [Plugin mode](https://oekazuma.github.io/svelte-vitals/guides/plugin-mode/) for the full picture.
30
40
 
31
41
  ## License
32
42
 
package/dist/index.js CHANGED
@@ -183,6 +183,9 @@ Scanned ${components.length} component(s) under src/ for Correctness/Security/Ar
183
183
  };
184
184
  }
185
185
 
186
+ // src/ui/middleware.ts
187
+ import { renderAppShell } from "@svelte-vitals/core";
188
+
186
189
  // src/ui/snapshot.ts
187
190
  import { buildJsonReport, safeHref } from "@svelte-vitals/core";
188
191
  function sanitizeDocsUrl(issue) {
@@ -203,568 +206,11 @@ function buildSnapshot(store, config, meta) {
203
206
  badges: store.badges(),
204
207
  analyzing: store.isAnalyzing(),
205
208
  sequence: store.sequence(),
209
+ live: true,
206
210
  meta
207
211
  };
208
212
  }
209
213
 
210
- // src/ui/dashboard-style.ts
211
- var DASHBOARD_STYLE = `
212
- :root{--ground:#f6f7f9;--panel:#fff;--ink:#0c1322;--muted:#5a6472;--faint:#8c95a3;--line:#e4e7ec;--line-strong:#d3d8e0;--accent:#ff3e00;--good:#2fa968;--warn:#e8a317;--poor:#e5484d;--code-bg:#0e1525;--code-ink:#e7ecf4;--active-bg:#0c1322;--active-ink:#fff;--mono:ui-monospace,"SF Mono","JetBrains Mono",Menlo,Consolas,monospace;--sans:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif}
213
- :root[data-theme="dark"]{--ground:#0b0e14;--panel:#12161f;--ink:#e7ecf4;--muted:#9aa4b2;--faint:#6b7484;--line:#232838;--line-strong:#2d3345;--code-bg:#05070c;--code-ink:#e7ecf4;--active-bg:#e7ecf4;--active-ink:#0b0e14}
214
- @media (prefers-color-scheme:dark){:root:not([data-theme="light"]){--ground:#0b0e14;--panel:#12161f;--ink:#e7ecf4;--muted:#9aa4b2;--faint:#6b7484;--line:#232838;--line-strong:#2d3345;--code-bg:#05070c;--code-ink:#e7ecf4;--active-bg:#e7ecf4;--active-ink:#0b0e14}}
215
- *{box-sizing:border-box}
216
- html,body{margin:0;height:100%}
217
- body{background:var(--ground);color:var(--ink);font-family:var(--sans);line-height:1.5;-webkit-font-smoothing:antialiased}
218
- .dv-app{display:grid;grid-template-rows:auto 1fr;grid-template-columns:280px 1fr;grid-template-areas:"top top" "side main";height:100vh}
219
- .dv-topbar{grid-area:top;border-bottom:1px solid var(--line);background:var(--panel)}
220
- .dv-topbar-inner{display:flex;align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap;padding:12px 20px}
221
- .dv-brand{display:flex;align-items:center;background:none;border:none;padding:0;cursor:pointer;border-radius:6px}
222
- .dv-brand svg{height:28px;width:auto;display:block}
223
- .dv-brand:focus-visible{outline:2px solid var(--accent);outline-offset:4px}
224
- .dv-meta{font-family:var(--mono);font-size:12px;color:var(--muted);display:flex;gap:12px;flex-wrap:wrap}
225
- .dv-status{display:flex;align-items:center;gap:10px}
226
- .dv-analyzing{font-size:12px;color:var(--accent);font-weight:600}
227
- .dv-conn{width:8px;height:8px;border-radius:50%;background:var(--faint);display:inline-block}
228
- .dv-conn-connected{background:var(--good)}
229
- .dv-conn-reconnecting{background:var(--warn)}
230
- .dv-menu-toggle{display:none;border:1px solid var(--line-strong);background:var(--panel);color:var(--ink);border-radius:8px;width:28px;height:28px;cursor:pointer}
231
- .dv-theme-toggle{border:1px solid var(--line-strong);background:var(--panel);color:var(--ink);border-radius:999px;width:28px;height:28px;cursor:pointer}
232
- .dv-theme-toggle:focus-visible,.dv-menu-toggle:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
233
- .dv-sidebar{grid-area:side;border-right:1px solid var(--line);background:var(--panel);overflow-y:auto}
234
- .dv-sidebar-inner{display:flex;flex-direction:column;gap:10px;padding:14px}
235
- .dv-search{font:inherit;font-size:13px;padding:7px 10px;border:1px solid var(--line-strong);border-radius:8px;background:var(--ground);color:var(--ink)}
236
- .dv-sort{font:inherit;font-size:12.5px;padding:6px 8px;border:1px solid var(--line-strong);border-radius:8px;background:var(--ground);color:var(--ink)}
237
- .dv-nav{display:flex;flex-direction:column;gap:2px}
238
- .dv-nav-item{display:flex;flex-direction:column;gap:4px;padding:8px 10px;border-radius:8px;cursor:pointer}
239
- .dv-nav-item:hover{background:var(--ground)}
240
- .dv-nav-item.active{background:var(--active-bg);color:var(--active-ink)}
241
- .dv-nav-item:focus-visible{outline:2px solid var(--accent);outline-offset:-2px}
242
- .dv-nav-label{font-family:var(--mono);font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
243
- .dv-nav-meta{display:flex;align-items:center;gap:8px;font-size:11.5px;color:var(--muted)}
244
- .dv-nav-item.active .dv-nav-meta{color:inherit}
245
- .dv-nav-score{font-family:var(--mono);font-weight:700}
246
- .dv-badge{font-size:9.5px;font-weight:700;text-transform:uppercase;letter-spacing:.06em;padding:1px 6px;border-radius:999px}
247
- .dv-badge-measured{background:rgba(47,169,104,.16);color:var(--good)}
248
- .dv-badge-static{background:rgba(140,149,163,.2);color:var(--muted)}
249
- .dv-detail{grid-area:main;overflow-y:auto;padding:24px 28px 80px}
250
- .dv-gauge{position:relative;width:132px;height:132px;margin-bottom:20px}
251
- .dv-gauge svg{position:absolute;inset:0;transform:rotate(-90deg)}
252
- .dv-gauge-track{stroke:var(--line)}
253
- .dv-gauge-num{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center}
254
- .dv-gauge-num strong{font-family:var(--mono);font-size:36px;font-weight:600}
255
- .dv-gauge-num span{font-size:10px;text-transform:uppercase;letter-spacing:.14em;color:var(--muted)}
256
- .dv-cats{display:flex;gap:22px;flex-wrap:wrap;margin-bottom:20px}
257
- .dv-cat{min-width:180px;flex:1}
258
- .dv-cat-top{display:flex;justify-content:space-between;font-size:13px;margin-bottom:6px}
259
- .dv-bar{height:7px;border-radius:999px;background:var(--line);overflow:hidden}
260
- .dv-bar>i{display:block;height:100%;border-radius:999px}
261
- .dv-filters{display:flex;gap:8px;flex-wrap:wrap;margin:16px 0}
262
- .dv-chip{font:inherit;font-size:12.5px;font-weight:600;cursor:pointer;background:var(--panel);border:1px solid var(--line-strong);color:var(--muted);padding:5px 12px;border-radius:999px}
263
- .dv-chip[aria-pressed="true"]{background:var(--active-bg);border-color:var(--active-bg);color:var(--active-ink)}
264
- .dv-chip:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
265
- .dv-section h2{font-size:12px;text-transform:uppercase;letter-spacing:.12em;color:var(--muted);margin:24px 0 12px}
266
- .dv-detail-header{display:flex;align-items:center;gap:12px;margin-bottom:14px;flex-wrap:wrap}
267
- .dv-route-path{font-family:var(--mono);font-size:16px;font-weight:600}
268
- .dv-score-chip{font-family:var(--mono);font-weight:700}
269
- .dv-finding{background:var(--panel);border:1px solid var(--line);border-left-width:3px;border-radius:10px;padding:16px 18px;margin:0 0 12px}
270
- .dv-finding-critical{border-left-color:var(--poor)}
271
- .dv-finding-warning{border-left-color:var(--warn)}
272
- .dv-finding-info{border-left-color:var(--faint)}
273
- .dv-f-head{display:flex;align-items:center;gap:10px;flex-wrap:wrap}
274
- .dv-ruleid{font-family:var(--mono);font-size:12px;font-weight:600;background:var(--ground);padding:2px 8px;border-radius:6px}
275
- .dv-f-title{font-weight:650;font-size:15px}
276
- .dv-sev-tag{margin-left:auto;font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.08em}
277
- .dv-sev-critical{color:var(--poor)}
278
- .dv-sev-warning{color:var(--warn)}
279
- .dv-sev-info{color:var(--faint)}
280
- .dv-f-route{display:block;font:inherit;font-family:var(--mono);font-size:12.5px;font-weight:600;color:var(--accent);background:none;border:none;padding:0;margin:8px 0 0;cursor:pointer;text-align:left}
281
- .dv-f-route:hover{text-decoration:underline}
282
- .dv-f-route:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
283
- .dv-f-loc{font-family:var(--mono);font-size:12.5px;color:var(--muted);margin:8px 0 0}
284
- .dv-f-rec{font-size:14px;margin:10px 0 0}
285
- .dv-fix{margin:12px 0 0;background:var(--code-bg);border-radius:8px;overflow:hidden}
286
- .dv-fix-label{font-family:var(--mono);font-size:11px;letter-spacing:.1em;text-transform:uppercase;color:#8da0bd;padding:8px 14px 0}
287
- .dv-fix pre{margin:0;padding:8px 14px 14px;overflow-x:auto}
288
- .dv-fix code{font-family:var(--mono);font-size:12.5px;color:var(--code-ink);line-height:1.65;white-space:pre}
289
- .tok-kw{color:#ff7ab8}
290
- .tok-str{color:#9ece6a}
291
- .tok-num{color:#ff9e64}
292
- .tok-cm{color:#6b7280;font-style:italic}
293
- .tok-id{color:var(--code-ink)}
294
- .tok-pn{color:#8da0bd}
295
- .dv-f-link{display:inline-block;margin-top:12px;font-size:13px;font-weight:600;color:var(--accent);text-decoration:none}
296
- .dv-f-link:hover{text-decoration:underline}
297
- .dv-empty{color:var(--muted);font-size:13px}
298
- @media (max-width:640px){.dv-app{grid-template-columns:1fr;grid-template-areas:"top" "main"}.dv-menu-toggle{display:inline-flex}.dv-sidebar{position:fixed;inset:0 20% 0 0;transform:translateX(-100%);transition:transform .2s ease;z-index:10}.dv-sidebar.open{transform:translateX(0)}}
299
- @media (prefers-reduced-motion:reduce){*{transition:none!important}}
300
- `;
301
-
302
- // src/ui/dashboard-script.ts
303
- var DASHBOARD_SCRIPT = `
304
- (function(){
305
- var BAND_COLOR = { good: '#2fa968', warn: '#e8a317', poor: '#e5484d' };
306
- function scoreBand(score) { return score >= 90 ? 'good' : score >= 50 ? 'warn' : 'poor'; }
307
-
308
- // Same mark as the docs site's hero wordmark (docs/public/wordmark.svg) \u2014 an inline
309
- // copy, not an <img src>, since the dashboard is a single self-contained HTML response
310
- // with no other static assets to serve alongside it. Fixed brand colors (not CSS custom
311
- // properties), matching the docs usage: the wordmark reads the same in both themes.
312
- var WORDMARK_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 380 56" role="img" aria-labelledby="dv-wordmark-title"><title id="dv-wordmark-title">svelte-vitals</title><defs><clipPath id="dv-wordmark-clip"><rect x="2" y="2" width="52" height="52" rx="14"/></clipPath></defs><rect x="2" y="2" width="52" height="52" rx="14" fill="#FF3E00"/><polyline clip-path="url(#dv-wordmark-clip)" points="4,28 15,28 17.5,23.5 20,28 23,28 26,7 29,49 32,28 35,28 37,24.5 39.5,28 52,28" fill="none" stroke="#fff" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><text x="70" y="38" font-family="ui-sans-serif, system-ui, -apple-system, \\'Segoe UI\\', Roboto, sans-serif" font-size="30" font-weight="700" fill="#FF3E00">svelte-vitals</text></svg>';
313
-
314
- function h(tag, attrs, kids) {
315
- var n = document.createElement(tag);
316
- if (attrs) {
317
- for (var k in attrs) {
318
- if (!Object.prototype.hasOwnProperty.call(attrs, k)) continue;
319
- var v = attrs[k];
320
- if (v === undefined || v === null || v === false) continue;
321
- if (k === 'class') n.className = v;
322
- else if (k === 'text') n.textContent = v;
323
- else if (k.indexOf('on') === 0 && typeof v === 'function') n.addEventListener(k.slice(2), v);
324
- else n.setAttribute(k, v === true ? '' : String(v));
325
- }
326
- }
327
- (kids || []).forEach(function (c) {
328
- if (c === undefined || c === null || c === false) return;
329
- n.appendChild(typeof c === 'string' ? document.createTextNode(c) : c);
330
- });
331
- return n;
332
- }
333
- function clear(n) { while (n.firstChild) n.removeChild(n.firstChild); }
334
- function mount(id, node) { var el = document.getElementById(id); clear(el); el.appendChild(node); }
335
-
336
- var HL_KEYWORDS = ['import','export','from','const','let','var','function','return','if','else','for','while','class','new','await','async','default','type','interface','extends','implements','this','typeof','instanceof','of','in','true','false','null','undefined'];
337
- var HL_LANGS = { js: 1, javascript: 1, ts: 1, typescript: 1, svelte: 1, html: 1, css: 1 };
338
-
339
- function highlightTokens(code) {
340
- var tokens = [];
341
- var i = 0;
342
- var n = code.length;
343
- var reIdent = /[A-Za-z_$][A-Za-z0-9_$]*/y;
344
- var reNum = /\\d+(\\.\\d+)?/y;
345
- while (i < n) {
346
- var ch = code[i];
347
- if (ch === '/' && code[i + 1] === '/') {
348
- var end = code.indexOf('\\n', i);
349
- if (end === -1) end = n;
350
- tokens.push({ text: code.slice(i, end), cls: 'cm' });
351
- i = end;
352
- continue;
353
- }
354
- if (ch === '/' && code[i + 1] === '*') {
355
- var end2 = code.indexOf('*/', i + 2);
356
- end2 = end2 === -1 ? n : end2 + 2;
357
- tokens.push({ text: code.slice(i, end2), cls: 'cm' });
358
- i = end2;
359
- continue;
360
- }
361
- if (ch === '"' || ch === "'" || ch === '\`') {
362
- var quote = ch;
363
- var j = i + 1;
364
- while (j < n && code[j] !== quote) {
365
- if (code[j] === '\\\\') j++;
366
- j++;
367
- }
368
- j = Math.min(j + 1, n);
369
- tokens.push({ text: code.slice(i, j), cls: 'str' });
370
- i = j;
371
- continue;
372
- }
373
- reIdent.lastIndex = i;
374
- var mIdent = reIdent.exec(code);
375
- if (mIdent && mIdent.index === i) {
376
- var word = mIdent[0];
377
- tokens.push({ text: word, cls: HL_KEYWORDS.indexOf(word) !== -1 ? 'kw' : 'id' });
378
- i += word.length;
379
- continue;
380
- }
381
- reNum.lastIndex = i;
382
- var mNum = reNum.exec(code);
383
- if (mNum && mNum.index === i) {
384
- tokens.push({ text: mNum[0], cls: 'num' });
385
- i += mNum[0].length;
386
- continue;
387
- }
388
- tokens.push({ text: ch, cls: 'pn' });
389
- i += 1;
390
- }
391
- return tokens;
392
- }
393
-
394
- function renderFixSnippet(fix) {
395
- var pre = h('pre', null, []);
396
- var code = h('code', null, []);
397
- var lang = (fix.lang || 'svelte').toLowerCase();
398
- if (HL_LANGS[lang]) {
399
- highlightTokens(fix.snippet).forEach(function (t) {
400
- code.appendChild(h('span', { class: 'tok-' + t.cls, text: t.text }, []));
401
- });
402
- } else {
403
- code.textContent = fix.snippet;
404
- }
405
- pre.appendChild(code);
406
- return pre;
407
- }
408
-
409
- var state = {
410
- snapshot: null,
411
- selected: 'overview',
412
- search: '',
413
- sort: 'score-asc',
414
- filter: 'all',
415
- theme: initialTheme(),
416
- connection: 'connecting',
417
- routeBySlug: {}
418
- };
419
-
420
- function initialTheme() {
421
- try {
422
- var stored = localStorage.getItem('svelte-vitals-theme');
423
- if (stored === 'dark' || stored === 'light') return stored;
424
- } catch (e) {}
425
- return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
426
- }
427
- function applyTheme() { document.documentElement.setAttribute('data-theme', state.theme); }
428
- function toggleTheme() {
429
- state.theme = state.theme === 'dark' ? 'light' : 'dark';
430
- try { localStorage.setItem('svelte-vitals-theme', state.theme); } catch (e) {}
431
- applyTheme();
432
- renderTopbar();
433
- }
434
- function toggleSidebar() {
435
- var sb = document.getElementById('dv-sidebar');
436
- if (sb) sb.classList.toggle('open');
437
- }
438
-
439
- function brandEl() {
440
- var el = h('button', { type: 'button', class: 'dv-brand', 'aria-label': 'Go to Overview', onclick: function () { selectItem('overview'); } }, []);
441
- el.innerHTML = WORDMARK_SVG;
442
- return el;
443
- }
444
-
445
- function renderTopbar() {
446
- var s = state.snapshot;
447
- var findings = s.report.routes.reduce(function (n, r) { return n + r.issues.length; }, 0) + s.report.siteIssues.length;
448
- var kids = [
449
- h('button', { type: 'button', class: 'dv-menu-toggle', 'aria-label': 'Toggle route list', onclick: toggleSidebar, text: '\u2261' }, []),
450
- brandEl(),
451
- h('div', { class: 'dv-meta' }, [
452
- h('span', { text: 'v' + s.meta.version }, []),
453
- s.meta.coreVersion ? h('span', { title: '@svelte-vitals/core version', text: 'core v' + s.meta.coreVersion }, []) : null,
454
- h('span', { text: s.report.routes.length + ' routes' }, []),
455
- h('span', { text: findings + ' findings' }, [])
456
- ].filter(Boolean)),
457
- h('div', { class: 'dv-status' }, [
458
- s.analyzing ? h('span', { class: 'dv-analyzing', text: 'Analyzing\u2026' }, []) : null,
459
- h('span', { class: 'dv-conn dv-conn-' + state.connection, title: state.connection }, []),
460
- h('button', { type: 'button', class: 'dv-theme-toggle', 'aria-label': 'Toggle dark mode', onclick: toggleTheme, text: state.theme === 'dark' ? '\u2600' : '\u263E' }, [])
461
- ].filter(Boolean))
462
- ];
463
- mount('dv-topbar', h('div', { class: 'dv-topbar-inner' }, kids));
464
- }
465
-
466
- function slugify(route) {
467
- return 'route-' + route.replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-|-$/g, '').toLowerCase();
468
- }
469
-
470
- function matchesSearch(route, q) {
471
- if (!q) return true;
472
- q = q.toLowerCase();
473
- if (route.route.toLowerCase().indexOf(q) !== -1) return true;
474
- return route.issues.some(function (iss) {
475
- return (iss.id + ' ' + iss.title + ' ' + (iss.location || '')).toLowerCase().indexOf(q) !== -1;
476
- });
477
- }
478
-
479
- function sortedRoutes() {
480
- var s = state.snapshot;
481
- var q = state.search.trim();
482
- var list = s.report.routes.filter(function (r) { return matchesSearch(r, q); }).slice();
483
- var sort = state.sort;
484
- if (sort === 'score-asc') list.sort(function (a, b) { return a.score - b.score; });
485
- else if (sort === 'score-desc') list.sort(function (a, b) { return b.score - a.score; });
486
- else if (sort === 'alpha') list.sort(function (a, b) { return a.route.localeCompare(b.route); });
487
- else if (sort === 'most-findings') list.sort(function (a, b) { return b.issues.length - a.issues.length; });
488
- return list;
489
- }
490
-
491
- function renderNavItem(label, key, route, active) {
492
- var kids = [h('span', { class: 'dv-nav-label', text: label }, [])];
493
- if (route) {
494
- var band = scoreBand(route.score);
495
- var crit = route.issues.filter(function (i) { return i.severity === 'critical'; }).length;
496
- var warn = route.issues.filter(function (i) { return i.severity === 'warning'; }).length;
497
- var info = route.issues.filter(function (i) { return i.severity === 'info'; }).length;
498
- var summary = [];
499
- if (crit) summary.push(crit + ' critical');
500
- if (warn) summary.push(warn + ' warning' + (warn > 1 ? 's' : ''));
501
- if (info) summary.push(info + ' info');
502
- var badge = state.snapshot.badges[route.route];
503
- kids.push(h('span', { class: 'dv-nav-meta' }, [
504
- badge ? h('span', { class: 'dv-badge dv-badge-' + badge, text: badge }, []) : null,
505
- h('span', { class: 'dv-nav-score', style: 'color:' + BAND_COLOR[band], text: String(route.score) }, []),
506
- h('span', { class: 'dv-nav-sum', text: summary.length ? summary.join(' \xB7 ') : 'no issues' }, [])
507
- ].filter(Boolean)));
508
- }
509
- return h('div', {
510
- class: 'dv-nav-item' + (active ? ' active' : ''),
511
- role: 'option',
512
- 'aria-selected': active ? 'true' : 'false',
513
- tabindex: '0',
514
- onclick: function () { selectItem(key); },
515
- onkeydown: function (e) { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); selectItem(key); } }
516
- }, kids);
517
- }
518
-
519
- function selectItem(key) {
520
- state.selected = key;
521
- location.hash = key === 'overview' ? 'overview' : 'route/' + slugify(key);
522
- var sb = document.getElementById('dv-sidebar');
523
- if (sb) sb.classList.remove('open');
524
- renderSidebar();
525
- renderDetail();
526
- }
527
-
528
- function renderSidebar() {
529
- var s = state.snapshot;
530
- state.routeBySlug = {};
531
-
532
- // mount() clears and rebuilds the whole sidebar, including the <input> itself, so
533
- // a naive re-render on every keystroke drops focus and cursor position \u2014 capture
534
- // them before rebuilding and restore them on the freshly-created input afterward.
535
- var prevSearch = document.querySelector('.dv-search');
536
- var hadFocus = !!prevSearch && document.activeElement === prevSearch;
537
- var selStart = hadFocus ? prevSearch.selectionStart : null;
538
- var selEnd = hadFocus ? prevSearch.selectionEnd : null;
539
-
540
- var searchInput = h('input', {
541
- type: 'search',
542
- class: 'dv-search',
543
- placeholder: 'Search routes or rules\u2026',
544
- value: state.search,
545
- oninput: function (e) { state.search = e.target.value; renderSidebar(); }
546
- }, []);
547
-
548
- var sortSelect = h('select', { class: 'dv-sort', 'aria-label': 'Sort routes', onchange: function (e) { state.sort = e.target.value; renderSidebar(); } }, [
549
- h('option', { value: 'score-asc', selected: state.sort === 'score-asc' || undefined, text: 'Score (worst first)' }, []),
550
- h('option', { value: 'score-desc', selected: state.sort === 'score-desc' || undefined, text: 'Score (best first)' }, []),
551
- h('option', { value: 'alpha', selected: state.sort === 'alpha' || undefined, text: 'Alphabetical' }, []),
552
- h('option', { value: 'most-findings', selected: state.sort === 'most-findings' || undefined, text: 'Most findings' }, [])
553
- ]);
554
-
555
- var items = [renderNavItem('Overview', 'overview', null, state.selected === 'overview')];
556
- sortedRoutes().forEach(function (r) {
557
- var slug = slugify(r.route);
558
- state.routeBySlug[slug] = r.route;
559
- items.push(renderNavItem(r.route, r.route, r, state.selected === r.route));
560
- });
561
-
562
- var nav = h('div', { class: 'dv-nav', role: 'listbox', 'aria-label': 'Routes' }, items);
563
- mount('dv-sidebar', h('div', { class: 'dv-sidebar-inner' }, [searchInput, sortSelect, nav]));
564
-
565
- if (hadFocus) {
566
- searchInput.focus();
567
- if (selStart !== null && searchInput.setSelectionRange) {
568
- searchInput.setSelectionRange(selStart, selEnd);
569
- }
570
- }
571
- }
572
-
573
- function renderFilterChips(categories) {
574
- var chip = function (filter, label) {
575
- return h('button', {
576
- type: 'button', class: 'dv-chip', 'aria-pressed': state.filter === filter ? 'true' : 'false',
577
- onclick: function () { state.filter = filter; renderDetail(); },
578
- text: label
579
- }, []);
580
- };
581
- var catChips = Object.keys(categories).map(function (cat) {
582
- var name = cat === 'seo' ? 'SEO' : cat.charAt(0).toUpperCase() + cat.slice(1);
583
- return chip(cat, name);
584
- });
585
- return h('div', { class: 'dv-filters', role: 'group', 'aria-label': 'Filter findings' },
586
- [chip('all', 'All'), chip('critical', 'Critical'), chip('warning', 'Warning'), chip('info', 'Info')].concat(catChips));
587
- }
588
-
589
- function passesFilter(issue) {
590
- var f = state.filter;
591
- return f === 'all' || issue.severity === f || issue.category === f;
592
- }
593
-
594
- var SEVERITY_ORDER = { critical: 0, warning: 1, info: 2 };
595
-
596
- function renderFinding(issue, route) {
597
- var kids = [
598
- h('div', { class: 'dv-f-head' }, [
599
- h('span', { class: 'dv-ruleid', text: issue.id }, []),
600
- h('span', { class: 'dv-f-title', text: issue.title }, []),
601
- h('span', { class: 'dv-sev-tag dv-sev-' + issue.severity, text: issue.severity }, [])
602
- ])
603
- ];
604
- if (route) {
605
- kids.push(h('button', { type: 'button', class: 'dv-f-route', onclick: function () { selectItem(route); }, text: route }, []));
606
- }
607
- if (issue.location) {
608
- kids.push(h('p', { class: 'dv-f-loc', text: issue.location + (issue.line !== undefined ? ':' + issue.line : '') }, []));
609
- }
610
- if (issue.recommendation) {
611
- kids.push(h('p', { class: 'dv-f-rec', text: issue.recommendation }, []));
612
- }
613
- if (issue.fix && issue.fix.snippet) {
614
- kids.push(h('div', { class: 'dv-fix' }, [h('div', { class: 'dv-fix-label', text: 'fix' }, []), renderFixSnippet(issue.fix)]));
615
- }
616
- if (issue.docsUrl) {
617
- kids.push(h('a', { class: 'dv-f-link', href: issue.docsUrl, text: 'Learn more' }, []));
618
- }
619
- return h('article', { class: 'dv-finding dv-finding-' + issue.severity }, kids);
620
- }
621
-
622
- function renderGauge(score) {
623
- var band = scoreBand(score);
624
- var svgNs = 'http://www.w3.org/2000/svg';
625
- var C = 2 * Math.PI * 58;
626
- var offset = (C * (1 - score / 100)).toFixed(1);
627
- var svg = document.createElementNS(svgNs, 'svg');
628
- svg.setAttribute('width', '132');
629
- svg.setAttribute('height', '132');
630
- svg.setAttribute('viewBox', '0 0 132 132');
631
- var bg = document.createElementNS(svgNs, 'circle');
632
- bg.setAttribute('cx', '66'); bg.setAttribute('cy', '66'); bg.setAttribute('r', '58');
633
- bg.setAttribute('fill', 'none'); bg.setAttribute('class', 'dv-gauge-track'); bg.setAttribute('stroke-width', '11');
634
- var arc = document.createElementNS(svgNs, 'circle');
635
- arc.setAttribute('cx', '66'); arc.setAttribute('cy', '66'); arc.setAttribute('r', '58');
636
- arc.setAttribute('fill', 'none'); arc.setAttribute('stroke', BAND_COLOR[band]); arc.setAttribute('stroke-width', '11');
637
- arc.setAttribute('stroke-linecap', 'round');
638
- arc.setAttribute('stroke-dasharray', C.toFixed(1));
639
- arc.setAttribute('stroke-dashoffset', offset);
640
- svg.appendChild(bg);
641
- svg.appendChild(arc);
642
- var wrap = h('div', { class: 'dv-gauge' }, [h('div', { class: 'dv-gauge-num' }, [h('strong', { text: String(score) }, []), h('span', { text: 'Health' }, [])])]);
643
- wrap.insertBefore(svg, wrap.firstChild);
644
- return wrap;
645
- }
646
-
647
- function renderOverview(s) {
648
- var gauge = renderGauge(s.report.score);
649
- var cats = Object.keys(s.report.categories).map(function (cat) {
650
- var c = s.report.categories[cat];
651
- var band = scoreBand(c.score);
652
- var weight = s.report.weights[cat];
653
- var name = cat === 'seo' ? 'SEO' : cat.charAt(0).toUpperCase() + cat.slice(1);
654
- return h('div', { class: 'dv-cat' }, [
655
- h('div', { class: 'dv-cat-top' }, [
656
- h('span', { text: name + (weight !== undefined ? ' (weight ' + weight + ')' : '') }, []),
657
- h('span', { style: 'color:' + BAND_COLOR[band], text: String(c.score) }, [])
658
- ]),
659
- h('div', { class: 'dv-bar' }, [h('i', { style: 'width:' + c.score + '%;background:' + BAND_COLOR[band] }, [])])
660
- ]);
661
- });
662
- var chips = renderFilterChips(s.report.categories);
663
- var totalCount = s.report.routes.reduce(function (n, r) { return n + r.issues.length; }, 0) + s.report.siteIssues.length;
664
- var entries = [];
665
- s.report.routes.forEach(function (r) {
666
- r.issues.forEach(function (issue) { entries.push({ issue: issue, route: r.route }); });
667
- });
668
- s.report.siteIssues.forEach(function (issue) { entries.push({ issue: issue, route: null }); });
669
- entries = entries.filter(function (e) { return passesFilter(e.issue); });
670
- entries.sort(function (a, b) {
671
- var sd = SEVERITY_ORDER[a.issue.severity] - SEVERITY_ORDER[b.issue.severity];
672
- if (sd !== 0) return sd;
673
- return (a.route || '').localeCompare(b.route || '');
674
- });
675
- var body = entries.length
676
- ? entries.map(function (e) { return renderFinding(e.issue, e.route); })
677
- : [h('p', { class: 'dv-empty', text: totalCount ? 'No issues match the current filter.' : 'No issues found \u2014 nice work!' }, [])];
678
- var findings = h('section', { class: 'dv-section' }, [h('h2', { text: 'Findings' }, [])].concat(body));
679
- return h('div', { class: 'dv-overview' }, [gauge, h('div', { class: 'dv-cats' }, cats), chips, findings].filter(Boolean));
680
- }
681
-
682
- function renderRouteDetail(route) {
683
- var badge = state.snapshot.badges[route.route];
684
- var band = scoreBand(route.score);
685
- var header = h('div', { class: 'dv-detail-header' }, [
686
- h('span', { class: 'dv-route-path', text: route.route }, []),
687
- badge ? h('span', { class: 'dv-badge dv-badge-' + badge, text: badge }, []) : null,
688
- h('span', { class: 'dv-score-chip', style: 'color:' + BAND_COLOR[band], text: String(route.score) }, [])
689
- ].filter(Boolean));
690
- var chips = renderFilterChips(state.snapshot.report.categories);
691
- var findings = route.issues.filter(passesFilter);
692
- var body = findings.length
693
- ? findings.map(function (issue) { return renderFinding(issue); })
694
- : [h('p', { class: 'dv-empty', text: 'No issues match the current filter.' }, [])];
695
- return h('div', { class: 'dv-route-detail' }, [header, chips].concat(body));
696
- }
697
-
698
- function renderDetail() {
699
- var s = state.snapshot;
700
- if (state.selected === 'overview') {
701
- mount('dv-detail', renderOverview(s));
702
- return;
703
- }
704
- var route = s.report.routes.filter(function (r) { return r.route === state.selected; })[0];
705
- if (!route) {
706
- state.selected = 'overview';
707
- mount('dv-detail', renderOverview(s));
708
- return;
709
- }
710
- mount('dv-detail', renderRouteDetail(route));
711
- }
712
-
713
- function renderAll() {
714
- renderTopbar();
715
- renderSidebar();
716
- renderDetail();
717
- }
718
-
719
- function restoreSelectionFromHash() {
720
- var raw = location.hash.replace(/^#/, '');
721
- if (!raw || raw === 'overview') { state.selected = 'overview'; return; }
722
- var m = /^route\\/(.+)$/.exec(raw);
723
- if (m && state.routeBySlug[m[1]]) state.selected = state.routeBySlug[m[1]];
724
- }
725
-
726
- function fetchSnapshot() {
727
- fetch('/__svelte-vitals/data.json').then(function (r) { return r.json(); }).then(function (data) {
728
- if (state.snapshot && data.sequence <= state.snapshot.sequence) return;
729
- state.snapshot = data;
730
- renderAll();
731
- }).catch(function () {});
732
- }
733
-
734
- function boot() {
735
- var raw = document.getElementById('svelte-vitals-data');
736
- state.snapshot = JSON.parse(raw.textContent);
737
- applyTheme();
738
- renderSidebar(); // populates routeBySlug before the hash can be trusted
739
- restoreSelectionFromHash();
740
- renderAll();
741
-
742
- window.addEventListener('hashchange', function () {
743
- restoreSelectionFromHash();
744
- renderSidebar();
745
- renderDetail();
746
- });
747
-
748
- if (typeof EventSource !== 'undefined') {
749
- var es = new EventSource('/__svelte-vitals/events');
750
- es.addEventListener('open', function () { state.connection = 'connected'; renderTopbar(); fetchSnapshot(); });
751
- es.addEventListener('update', fetchSnapshot);
752
- es.addEventListener('error', function () { state.connection = 'reconnecting'; renderTopbar(); });
753
- }
754
- }
755
-
756
- boot();
757
- })();
758
- `;
759
-
760
- // src/ui/dashboard.ts
761
- function embedJson(value) {
762
- return JSON.stringify(value).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
763
- }
764
- function renderDashboardShell(snapshot) {
765
- return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>svelte-vitals dashboard</title><style>${DASHBOARD_STYLE}</style></head><body><div class="dv-app" id="dv-app"><header class="dv-topbar" id="dv-topbar"></header><nav class="dv-sidebar" id="dv-sidebar"></nav><main class="dv-detail" id="dv-detail"></main></div><script type="application/json" id="svelte-vitals-data">${embedJson(snapshot)}</script><script>${DASHBOARD_SCRIPT}</script></body></html>`;
766
- }
767
-
768
214
  // src/ui/middleware.ts
769
215
  var SEVERITIES = /* @__PURE__ */ new Set(["critical", "warning", "info"]);
770
216
  var CATEGORIES = /* @__PURE__ */ new Set(["seo", "performance", "correctness", "security", "architecture"]);
@@ -842,7 +288,7 @@ function installUiMiddleware(server, config, version, store, coreVersion) {
842
288
  return;
843
289
  }
844
290
  try {
845
- const html = renderDashboardShell(buildSnapshot(store, config, { version, coreVersion }));
291
+ const html = renderAppShell(buildSnapshot(store, config, { version, coreVersion }));
846
292
  res.setHeader("Content-Type", "text/html");
847
293
  res.end(html);
848
294
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svelte-vitals/vite",
3
- "version": "0.15.1",
3
+ "version": "0.17.0",
4
4
  "description": "Vite/SvelteKit plugin for svelte-vitals — analyzes prerendered HTML during vite build.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,8 +42,8 @@
42
42
  "esm-env": "^1.2.2",
43
43
  "node-html-parser": "^9.0.0",
44
44
  "tinyglobby": "^0.2.17",
45
- "@svelte-vitals/core": "0.24.0",
46
- "svelte-vitals": "0.26.0"
45
+ "@svelte-vitals/core": "0.25.0",
46
+ "svelte-vitals": "0.28.0"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "@sveltejs/kit": "^2.69.2",