@verajs/renderer 0.1.2 → 0.1.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/README.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # @verajs/renderer
2
2
 
3
- A keyed, template-identity renderer (~3.6 KB gzip): tagged templates, `.prop`/`?bool`/`@event`
3
+ A keyed, template-identity renderer (<!--size:renderer.gzip-->3.54 KB<!--/size:renderer.gzip--> gzip): tagged templates, `.prop`/`?bool`/`@event`
4
4
  and React-style `onClick` bindings, element refs, `keyed()` lists, `hold()` DOM preservation.
5
5
  `@verajs/renderer/hydrate` is a drop-in superset whose first render ADOPTS server-rendered DOM —
6
6
  markerless hydration (no framework comments in server HTML). SSR apps import from `/hydrate`;
7
7
  everyone else pays zero hydration bytes.
8
8
 
9
+ `@verajs/renderer/profiler` is a development-only entry that reports how many templates were
10
+ committed in place versus torn down and rebuilt, naming the template pairs that churn and where.
11
+ `showProfiler()` puts the same information in a live panel in the corner of the page. It costs
12
+ production nothing — the instrumentation is removed by the build, not merely unused.
13
+
9
14
  Wire once: `setRenderer(render)` from `@verajs/core`.
@@ -473,7 +473,54 @@ class AttrPart {
473
473
  this._element.setAttribute(this._name, value);
474
474
  }
475
475
  else if (kind === PROPERTY) {
476
- this._element[this._name] = value;
476
+ const target = this._element;
477
+ const name = this._name;
478
+ target[name] = value;
479
+ /**
480
+ * Detection only, and deliberately not repair.
481
+ *
482
+ * A property set on a custom element that has not upgraded yet lands as an own property on
483
+ * the instance. When the definition arrives — lazily imported, code-split, or a module that
484
+ * simply had not run — `customElements.define` upgrades synchronously and the class's field
485
+ * initializers execute. At target ES2022 a field declaration is a `[[Define]]`, so
486
+ * `item?: T` emits `item;` and overwrites the bound value with `undefined`. Nothing throws;
487
+ * it reads as broken reactivity.
488
+ *
489
+ * Repairing it was tried and removed. Re-applying when the slot came back `undefined`
490
+ * covered `item?: T` but not `item = someDefault`, which overwrites with the default and
491
+ * never looks clobbered — so the repair was silently partial, and made the bug intermittent
492
+ * across two spellings of the same mistake. It also cost 74 B in every app while leaving
493
+ * `declare` mandatory anyway, because a property assigned imperatively is unrecoverable:
494
+ * the renderer never saw it, and by the time `init()` runs the value is already gone.
495
+ *
496
+ * So this is `true`-only and production pays nothing — no check, no message, no
497
+ * `whenDefined` subscription. Lit reached the same place from the other direction and
498
+ * throws (`lit.dev/msg/class-field-shadowing`); a warning suffices here because Vera has no
499
+ * prototype accessors to shadow permanently — the damage is one lost value, not a property
500
+ * that never updates again.
501
+ */
502
+ if (true) {
503
+ const tag = this._element.localName;
504
+ if (tag.indexOf('-') > 0 && !customElements.get(tag)) {
505
+ customElements.whenDefined(tag).then(() => {
506
+ /**
507
+ * States what was observed rather than diagnosing it. A class field is the usual
508
+ * cause by a wide margin, but a component that assigns the property itself during
509
+ * upgrade produces the same observation, and that is a legitimate — if confusing —
510
+ * thing to do, since the renderer's dirty check will not re-apply the bound value on
511
+ * the next render either.
512
+ */
513
+ if (target[name] !== value)
514
+ console.warn(`@verajs/renderer: the value bound by \`.${name}=\${…}\` on <${tag}> was replaced ` +
515
+ `while the element upgraded. A class field is the usual cause: at ES2022 ` +
516
+ `\`${name}?: …\` emits \`${name};\`, which runs during upgrade and overwrites ` +
517
+ `whatever was set beforehand — write it \`declare ${name}?: …\` instead, which ` +
518
+ `emits nothing. A plain field also wipes properties assigned imperatively, ` +
519
+ `which cannot be detected at all. Ignore this if the component replaced the ` +
520
+ `value on purpose.`);
521
+ });
522
+ }
523
+ }
477
524
  }
478
525
  else if (kind === BOOLEAN) {
479
526
  if (!isFirst || value)
@@ -589,6 +636,7 @@ const EMPTY = 0;
589
636
  const TEXT = 1;
590
637
  const TEMPLATE = 2;
591
638
  const LIST = 3;
639
+ let _profileHook = null;
592
640
  class ChildPart {
593
641
  _start;
594
642
  /** Exclusive end of this part's range; null means "to the end of the parent". */
@@ -616,14 +664,25 @@ class ChildPart {
616
664
  }
617
665
  _clear() {
618
666
  const parent = this._start.parentNode;
667
+ const end = this._end;
619
668
  /**
620
669
  * When this part owns its parent's entire contents, one `textContent = ''` replaces removing
621
670
  * every node individually. For a 1 000-row table body that is the difference between ~22 ms
622
671
  * (lit-html's per-node teardown) and ~5 ms.
672
+ *
673
+ * A part owns the whole parent when nothing precedes its start AND nothing follows its end —
674
+ * `_end === null` (a root part, which runs to the end by definition) or `_end` is the last
675
+ * child. The second case is the common one and used to miss this path entirely: every list
676
+ * written as `<tbody>${rows}</tbody>` sits inside a template, and since 0.1.2 a nested part
677
+ * always owns an end marker, so `_end === null` alone never held for it.
678
+ *
679
+ * Re-appending both anchors in order restores the part's boundary exactly as it was.
623
680
  */
624
- if (this._start.previousSibling === null && this._end === null) {
681
+ if (this._start.previousSibling === null && (end === null || end.nextSibling === null)) {
625
682
  parent.textContent = '';
626
683
  parent.appendChild(this._start);
684
+ if (end !== null)
685
+ parent.appendChild(end);
627
686
  }
628
687
  else {
629
688
  let node = this._start.nextSibling;
@@ -679,9 +738,17 @@ class ChildPart {
679
738
  * them, in practice), commit costs one identity compare before touching the values.
680
739
  */
681
740
  if (this._shape === value.strings) {
741
+ if (true && _profileHook)
742
+ ;
682
743
  this._instance._update(value.values);
683
744
  return;
684
745
  }
746
+ /**
747
+ * Reaching here with a template already committed means the identity changed, so the
748
+ * subtree below is about to be destroyed and rebuilt rather than updated. That is what
749
+ * `?hidden=${…}` over a swapped subtree exists to avoid, and it is otherwise invisible.
750
+ */
751
+ if (true && _profileHook) ;
685
752
  const template = getTemplate(value);
686
753
  if (this._mode !== EMPTY)
687
754
  this._clear();
@@ -984,6 +1051,8 @@ const rootParts = new WeakMap();
984
1051
  * this entry carries zero hydration code.
985
1052
  */
986
1053
  const render$1 = (result, container) => {
1054
+ if (true && _profileHook)
1055
+ ;
987
1056
  let part = rootParts.get(container);
988
1057
  if (part === undefined) {
989
1058
  const marker = comment();
@@ -991,6 +1060,8 @@ const render$1 = (result, container) => {
991
1060
  rootParts.set(container, (part = new ChildPart(marker, null)));
992
1061
  }
993
1062
  part._set(result);
1063
+ if (true && _profileHook)
1064
+ ;
994
1065
  };
995
1066
 
996
1067
  /**
@@ -1065,7 +1136,7 @@ const claimValueText = (cursor, text) => {
1065
1136
  }
1066
1137
  if (at === null || at.nodeType !== 3)
1067
1138
  throw MISMATCH;
1068
- let node = at;
1139
+ const node = at;
1069
1140
  if (node.data.length > text.length)
1070
1141
  node.splitText(text.length);
1071
1142
  if (node.data !== text)
@@ -1275,6 +1346,8 @@ const tryAdopt = (result, container) => {
1275
1346
  const clearPreservingStyles = (container) => {
1276
1347
  let node = container.firstChild;
1277
1348
  while (node !== null) {
1349
+ /** `nextSibling` already yields `ChildNode | null`; annotating it `Node` widened it and broke
1350
+ * the assignment back into `node`, which `firstChild` typed as `ChildNode | null`. */
1278
1351
  const next = node.nextSibling;
1279
1352
  if (!(node.nodeType === 1 && node.hasAttribute('vera-styles')))
1280
1353
  container.removeChild(node);