what-compiler 0.6.0 → 0.6.1

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/index.js CHANGED
@@ -503,6 +503,27 @@ function whatBabelPlugin({ types: t }) {
503
503
  continue;
504
504
  }
505
505
  const attrName = getAttrName(attr);
506
+ if (attrName === "ref") {
507
+ const refExpr = getAttributeValue(attr.value);
508
+ statements.push(
509
+ t.expressionStatement(
510
+ t.conditionalExpression(
511
+ t.binaryExpression(
512
+ "===",
513
+ t.unaryExpression("typeof", refExpr),
514
+ t.stringLiteral("function")
515
+ ),
516
+ t.callExpression(t.cloneNode(refExpr), [t.identifier(elId)]),
517
+ t.assignmentExpression(
518
+ "=",
519
+ t.memberExpression(t.cloneNode(refExpr), t.identifier("current")),
520
+ t.identifier(elId)
521
+ )
522
+ )
523
+ )
524
+ );
525
+ continue;
526
+ }
506
527
  if (attrName.startsWith("on") && !attrName.includes("|")) {
507
528
  const event = attrName.slice(2).toLowerCase();
508
529
  const handler = getAttributeValue(attr.value);
@@ -653,6 +674,7 @@ function whatBabelPlugin({ types: t }) {
653
674
  }
654
675
  }
655
676
  function applyDynamicChildren(statements, elId, children, parentNode, state) {
677
+ const entries = [];
656
678
  let childIndex = 0;
657
679
  for (const child of children) {
658
680
  if (t.isJSXText(child)) {
@@ -662,8 +684,57 @@ function whatBabelPlugin({ types: t }) {
662
684
  }
663
685
  if (t.isJSXExpressionContainer(child)) {
664
686
  if (t.isJSXEmptyExpression(child.expression)) continue;
665
- const expr = child.expression;
666
- const marker = buildChildAccess(elId, childIndex);
687
+ entries.push({ type: "expression", child, childIndex });
688
+ childIndex++;
689
+ continue;
690
+ }
691
+ if (t.isJSXElement(child)) {
692
+ const childTag = child.openingElement.name.name;
693
+ if (isComponent(childTag) || childTag === "For" || childTag === "Show") {
694
+ entries.push({ type: "component", child, childIndex });
695
+ childIndex++;
696
+ } else {
697
+ const hasAnythingDynamic = child.openingElement.attributes.some(isDynamicAttr) || child.openingElement.attributes.some((a) => !t.isJSXSpreadAttribute(a) && getAttrName(a)?.startsWith("on")) || !child.children.every(isStaticChild);
698
+ entries.push({ type: "static", child, childIndex, hasAnythingDynamic });
699
+ childIndex++;
700
+ }
701
+ continue;
702
+ }
703
+ if (t.isJSXFragment(child)) {
704
+ entries.push({ type: "fragment", child });
705
+ }
706
+ }
707
+ const entriesNeedingRef = entries.filter(
708
+ (e) => e.type === "expression" || e.type === "component" || e.type === "static" && e.hasAnythingDynamic
709
+ );
710
+ const hasDynamicInsert = entries.some((e) => e.type === "expression" || e.type === "component");
711
+ const needsPreCapture = entriesNeedingRef.length >= 2 && hasDynamicInsert;
712
+ const markerVars = /* @__PURE__ */ new Map();
713
+ if (needsPreCapture) {
714
+ for (const entry of entriesNeedingRef) {
715
+ const varName = `_m$${entry.childIndex}`;
716
+ const markerVar = state.nextVarId();
717
+ markerVars.set(entry.childIndex, markerVar);
718
+ statements.push(
719
+ t.variableDeclaration("const", [
720
+ t.variableDeclarator(
721
+ t.identifier(markerVar),
722
+ buildChildAccess(elId, entry.childIndex)
723
+ )
724
+ ])
725
+ );
726
+ }
727
+ }
728
+ function getMarker(idx) {
729
+ if (markerVars.has(idx)) {
730
+ return t.identifier(markerVars.get(idx));
731
+ }
732
+ return buildChildAccess(elId, idx);
733
+ }
734
+ for (const entry of entries) {
735
+ if (entry.type === "expression") {
736
+ const expr = entry.child.expression;
737
+ const marker = getMarker(entry.childIndex);
667
738
  state.needsInsert = true;
668
739
  if (isPotentiallyReactive(expr, state.signalNames, state.importedIdentifiers)) {
669
740
  const insertCall = t.callExpression(t.identifier("_$insert"), [
@@ -691,46 +762,44 @@ function whatBabelPlugin({ types: t }) {
691
762
  )
692
763
  );
693
764
  }
694
- childIndex++;
695
765
  continue;
696
766
  }
697
- if (t.isJSXElement(child)) {
698
- const childTag = child.openingElement.name.name;
699
- if (isComponent(childTag) || childTag === "For" || childTag === "Show") {
700
- const transformed = transformElementFineGrained({ node: child }, state);
701
- const marker = buildChildAccess(elId, childIndex);
702
- state.needsInsert = true;
767
+ if (entry.type === "component") {
768
+ const transformed = transformElementFineGrained({ node: entry.child }, state);
769
+ const marker = getMarker(entry.childIndex);
770
+ state.needsInsert = true;
771
+ statements.push(
772
+ t.expressionStatement(
773
+ t.callExpression(t.identifier("_$insert"), [
774
+ t.identifier(elId),
775
+ transformed,
776
+ marker
777
+ ])
778
+ )
779
+ );
780
+ continue;
781
+ }
782
+ if (entry.type === "static" && entry.hasAnythingDynamic) {
783
+ let childElRef;
784
+ if (markerVars.has(entry.childIndex)) {
785
+ childElRef = markerVars.get(entry.childIndex);
786
+ } else {
787
+ childElRef = state.nextVarId();
703
788
  statements.push(
704
- t.expressionStatement(
705
- t.callExpression(t.identifier("_$insert"), [
706
- t.identifier(elId),
707
- transformed,
708
- marker
709
- ])
710
- )
789
+ t.variableDeclaration("const", [
790
+ t.variableDeclarator(
791
+ t.identifier(childElRef),
792
+ buildChildAccess(elId, entry.childIndex)
793
+ )
794
+ ])
711
795
  );
712
- childIndex++;
713
- } else {
714
- const hasAnythingDynamic = child.openingElement.attributes.some(isDynamicAttr) || child.openingElement.attributes.some((a) => !t.isJSXSpreadAttribute(a) && getAttrName(a)?.startsWith("on")) || !child.children.every(isStaticChild);
715
- if (hasAnythingDynamic) {
716
- const childElId = state.nextVarId();
717
- statements.push(
718
- t.variableDeclaration("const", [
719
- t.variableDeclarator(
720
- t.identifier(childElId),
721
- buildChildAccess(elId, childIndex)
722
- )
723
- ])
724
- );
725
- applyDynamicAttrs(statements, childElId, child.openingElement.attributes, state);
726
- applyDynamicChildren(statements, childElId, child.children, child, state);
727
- }
728
- childIndex++;
729
796
  }
797
+ applyDynamicAttrs(statements, childElRef, entry.child.openingElement.attributes, state);
798
+ applyDynamicChildren(statements, childElRef, entry.child.children, entry.child, state);
730
799
  continue;
731
800
  }
732
- if (t.isJSXFragment(child)) {
733
- for (const fChild of child.children) {
801
+ if (entry.type === "fragment") {
802
+ for (const fChild of entry.child.children) {
734
803
  if (t.isJSXExpressionContainer(fChild) && !t.isJSXEmptyExpression(fChild.expression)) {
735
804
  state.needsInsert = true;
736
805
  const expr = fChild.expression;