@fluixi/compiler 1.0.0-alpha.91 → 1.0.0-alpha.93

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 (81) hide show
  1. package/dist/analyze/intrinsics.cjs +21 -1
  2. package/dist/analyze/intrinsics.d.ts.map +1 -1
  3. package/dist/analyze/intrinsics.js +20 -0
  4. package/dist/analyze/intrinsics.mjs +21 -1
  5. package/dist/analyze/reactive-bindings.cjs +21 -1
  6. package/dist/analyze/reactive-bindings.mjs +21 -1
  7. package/dist/analyze/static-graph.cjs +25 -5
  8. package/dist/analyze/static-graph.d.ts.map +1 -1
  9. package/dist/analyze/static-graph.js +6 -4
  10. package/dist/analyze/static-graph.mjs +25 -5
  11. package/dist/{babel-VBLQRHQ2.mjs → babel-RJHJQVEM.mjs} +4 -4
  12. package/dist/{chunk-D53J7ICY.mjs → chunk-56QSOVVL.mjs} +1 -1
  13. package/dist/{chunk-S4J4OCWK.mjs → chunk-OD53TMER.mjs} +22 -2
  14. package/dist/{chunk-TWDJUXWZ.mjs → chunk-R7PPFBCL.mjs} +1 -1
  15. package/dist/{chunk-QX6Y3OAR.mjs → chunk-TJIH7TLX.mjs} +1 -1
  16. package/dist/{chunk-3JWMCK4A.mjs → chunk-U7HCFLZ2.mjs} +153 -48
  17. package/dist/codegen/backends/imperative.cjs +27 -7
  18. package/dist/codegen/backends/imperative.d.ts.map +1 -1
  19. package/dist/codegen/backends/imperative.js +19 -4
  20. package/dist/codegen/backends/imperative.mjs +27 -7
  21. package/dist/frontend/babel/build-ir-lit.cjs +123 -44
  22. package/dist/frontend/babel/build-ir-lit.mjs +123 -44
  23. package/dist/frontend/babel/build-ir.cjs +123 -44
  24. package/dist/frontend/babel/build-ir.mjs +123 -44
  25. package/dist/frontend/babel/index.cjs +124 -45
  26. package/dist/frontend/babel/index.mjs +124 -45
  27. package/dist/frontend/babel/lower-template.cjs +123 -44
  28. package/dist/frontend/babel/lower-template.mjs +123 -44
  29. package/dist/frontend/babel/plugin.cjs +123 -44
  30. package/dist/frontend/babel/plugin.mjs +123 -44
  31. package/dist/index.cjs +86 -46
  32. package/dist/index.mjs +88 -48
  33. package/dist/integrations.cjs +246 -61
  34. package/dist/integrations.mjs +53 -19
  35. package/dist/lower/compile-template.cjs +44 -39
  36. package/dist/lower/compile-template.mjs +44 -39
  37. package/dist/lower/jsx.cjs +509 -6
  38. package/dist/lower/jsx.d.ts.map +1 -1
  39. package/dist/lower/jsx.js +114 -6
  40. package/dist/lower/jsx.mjs +509 -6
  41. package/dist/lower/template.cjs +43 -38
  42. package/dist/lower/template.d.ts.map +1 -1
  43. package/dist/lower/template.js +22 -30
  44. package/dist/lower/template.mjs +43 -38
  45. package/dist/resolve/builtins.cjs +1 -1
  46. package/dist/resolve/builtins.mjs +1 -1
  47. package/dist/resolve/component-globals.cjs +33 -8
  48. package/dist/resolve/component-globals.d.cts +8 -0
  49. package/dist/resolve/component-globals.d.ts +8 -0
  50. package/dist/resolve/component-globals.d.ts.map +1 -1
  51. package/dist/resolve/component-globals.js +21 -12
  52. package/dist/resolve/component-globals.mjs +33 -8
  53. package/dist/resolve/write-globals.cjs +59 -8
  54. package/dist/resolve/write-globals.d.cts +6 -0
  55. package/dist/resolve/write-globals.d.ts +6 -0
  56. package/dist/resolve/write-globals.d.ts.map +1 -1
  57. package/dist/resolve/write-globals.js +35 -2
  58. package/dist/resolve/write-globals.mjs +61 -10
  59. package/dist/{server-fns-J56G3PFP.mjs → server-fns-ZFRDIQ2X.mjs} +4 -4
  60. package/dist/{server-functions-7PUFLRGF.mjs → server-functions-CTDXX67X.mjs} +3 -3
  61. package/dist/transform/index.cjs +199 -49
  62. package/dist/transform/index.mjs +199 -49
  63. package/dist/transform/intrinsics.cjs +47 -2
  64. package/dist/transform/intrinsics.d.cts +12 -0
  65. package/dist/transform/intrinsics.d.ts +12 -0
  66. package/dist/transform/intrinsics.d.ts.map +1 -1
  67. package/dist/transform/intrinsics.js +43 -1
  68. package/dist/transform/intrinsics.mjs +47 -2
  69. package/dist/transform/source-locations.cjs +67 -4
  70. package/dist/transform/source-locations.d.cts +7 -2
  71. package/dist/transform/source-locations.d.ts +7 -2
  72. package/dist/transform/source-locations.d.ts.map +1 -1
  73. package/dist/transform/source-locations.js +68 -3
  74. package/dist/transform/source-locations.mjs +67 -4
  75. package/dist/transform/templates.cjs +198 -48
  76. package/dist/transform/templates.d.ts.map +1 -1
  77. package/dist/transform/templates.js +5 -3
  78. package/dist/transform/templates.mjs +198 -48
  79. package/dist/{transform-D536WXGM.mjs → transform-QWWQDCXE.mjs} +33 -8
  80. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  81. package/package.json +9 -3
@@ -332,6 +332,435 @@ function paren(code, position) {
332
332
  return topLevelComma(src) === false ? code : `(${code})`;
333
333
  }
334
334
 
335
+ // src/codegen/serialize-static.ts
336
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
337
+ "area",
338
+ "base",
339
+ "br",
340
+ "col",
341
+ "embed",
342
+ "hr",
343
+ "img",
344
+ "input",
345
+ "link",
346
+ "meta",
347
+ "param",
348
+ "source",
349
+ "track",
350
+ "wbr"
351
+ ]);
352
+ var HTML_NAME = {
353
+ className: "class",
354
+ htmlFor: "for"
355
+ };
356
+ var PROPERTY_ONLY = /* @__PURE__ */ new Set([
357
+ "innerHTML",
358
+ "outerHTML",
359
+ "innerText",
360
+ "textContent",
361
+ "value",
362
+ "checked",
363
+ "selected",
364
+ "muted",
365
+ "defaultValue",
366
+ "defaultChecked",
367
+ "children",
368
+ "ref"
369
+ ]);
370
+ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
371
+ function isRawTextElement(tag) {
372
+ return RAW_TEXT_ELEMENTS.has(tag);
373
+ }
374
+ function htmlAttrName(name) {
375
+ return HTML_NAME[name] ?? name;
376
+ }
377
+ function isPlainLiteralAttr(prop) {
378
+ if (prop.kind !== "attr") return false;
379
+ if (prop.expr !== void 0) return false;
380
+ if (prop.name.includes(":")) return false;
381
+ return !PROPERTY_ONLY.has(prop.name);
382
+ }
383
+ function isSerializable(node) {
384
+ if (!node.static) return false;
385
+ if (isRawTextElement(node.tag)) return false;
386
+ for (const prop of node.props) {
387
+ if (!isPlainLiteralAttr(prop)) return false;
388
+ }
389
+ for (const child of node.children) {
390
+ if (child.kind === "text") continue;
391
+ if (child.kind === "element" && isSerializable(child)) continue;
392
+ return false;
393
+ }
394
+ return true;
395
+ }
396
+ function serializeStatic(node) {
397
+ const attrs = node.props.map(serializeProp).filter(Boolean).join("");
398
+ const open = `<${node.tag}${attrs}>`;
399
+ if (VOID_ELEMENTS.has(node.tag)) return open;
400
+ return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
401
+ }
402
+ function serializeChild(child) {
403
+ if (child.kind === "text") return escapeText(child.value);
404
+ if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
405
+ return serializeStatic(child);
406
+ }
407
+ function serializeProp(prop) {
408
+ const name = HTML_NAME[prop.name] ?? prop.name;
409
+ const value = prop.literal;
410
+ if (value === true || value === void 0) return ` ${name}`;
411
+ if (value === false || value === null) return "";
412
+ return ` ${name}="${escapeAttr(String(value))}"`;
413
+ }
414
+ function escapeAttr(value) {
415
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
416
+ }
417
+ function escapeText(value) {
418
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
419
+ }
420
+ function templatePositions(root) {
421
+ const pos = [];
422
+ let file;
423
+ const walk = (el) => {
424
+ file ??= el.at?.file;
425
+ pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
426
+ for (const child of el.children) if (child.kind === "element") walk(child);
427
+ };
428
+ walk(root);
429
+ return file && pos.some((n) => n !== 0) ? { file, pos } : void 0;
430
+ }
431
+
432
+ // src/codegen/partial-template.ts
433
+ var HOLE_START = "<!--fx-->";
434
+ var HOLE_END = "<!--fx/-->";
435
+ function isHole(node) {
436
+ return node.kind === "expr" || node.kind === "component" || node.kind === "control";
437
+ }
438
+ function isTemplateChild(node) {
439
+ return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
440
+ }
441
+ function isTemplateElement(node) {
442
+ if (node.svg) return false;
443
+ if (isRawTextElement(node.tag)) return false;
444
+ if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
445
+ return node.children.every((c) => isTemplateChild(c) || isHole(c));
446
+ }
447
+ function containsHole(node) {
448
+ if (isHole(node)) return true;
449
+ const children = node.children;
450
+ return children ? children.some(containsHole) : false;
451
+ }
452
+ function addressable(el) {
453
+ for (let i = 0; i < el.children.length; i++) {
454
+ const child = el.children[i];
455
+ if (child.kind === "text") {
456
+ if (child.value === "") return false;
457
+ if (el.children[i + 1]?.kind === "text") return false;
458
+ }
459
+ }
460
+ return el.children.every((c) => c.kind !== "element" || addressable(c));
461
+ }
462
+ function planPartialTemplate(root) {
463
+ if (!isTemplateElement(root)) return null;
464
+ if (!addressable(root)) return null;
465
+ if (!root.children.some(containsHole)) return null;
466
+ const steps = [];
467
+ const holes = [];
468
+ let refCount = 0;
469
+ const declare = (expr) => {
470
+ const ref = `_n$${refCount++}`;
471
+ steps.push({ ref, expr });
472
+ return ref;
473
+ };
474
+ walk(root, "_el$");
475
+ return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
476
+ function walk(el, elementRef) {
477
+ let last = -1;
478
+ el.children.forEach((child, i) => {
479
+ if (containsHole(child)) last = i;
480
+ });
481
+ let previous = null;
482
+ for (let i = 0; i <= last; i++) {
483
+ const child = el.children[i];
484
+ const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
485
+ if (isHole(child)) {
486
+ const startRef = declare(expr);
487
+ const endRef = declare(`holeEnd(${startRef})`);
488
+ holes.push({ parentRef: elementRef, startRef, endRef, node: child });
489
+ previous = endRef;
490
+ continue;
491
+ }
492
+ const ref = declare(expr);
493
+ if (child.kind === "element" && containsHole(child)) walk(child, ref);
494
+ previous = ref;
495
+ }
496
+ }
497
+ }
498
+ function serializeWithMarkers(el) {
499
+ return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
500
+ }
501
+ function withSentinels(el) {
502
+ return {
503
+ ...el,
504
+ children: el.children.map(
505
+ (child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
506
+ )
507
+ };
508
+ }
509
+ var SENTINEL = "\0fx-hole\0";
510
+
511
+ // src/codegen/contract.ts
512
+ var RUNTIME_CONTRACT_V1 = {
513
+ version: 1,
514
+ module: "@fluixi/dom",
515
+ symbols: [
516
+ "createNativeElement",
517
+ "insert",
518
+ "spread",
519
+ "setAttribute",
520
+ "delegateEvents",
521
+ "createComponent",
522
+ "createMemo",
523
+ "untrack",
524
+ "Show",
525
+ "For",
526
+ "Index",
527
+ "Switch",
528
+ "Match",
529
+ "Dynamic",
530
+ "ErrorBoundary"
531
+ ]
532
+ };
533
+ var RUNTIME_CONTRACT_V2 = {
534
+ version: 2,
535
+ module: "@fluixi/dom",
536
+ symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
537
+ };
538
+
539
+ // src/analyze/intrinsics.ts
540
+ var REACTIVE_MODULE = "@fluixi/reactive/signal";
541
+ var INTRINSICS = {
542
+ $signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
543
+ $memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
544
+ $effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
545
+ $store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
546
+ $resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
547
+ $cachedResource: { export: "cachedResource", module: "@fluixi/reactive/cached-resource", returns: "resource-handle" },
548
+ // No short runtime form for these yet, so the intrinsic stands for the `create*`
549
+ // name. Still worth having: no import to write, and when a shorter form is
550
+ // designed, the change is this line rather than a compiler pass.
551
+ $selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
552
+ $deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
553
+ // Not value constructors, but the compiler has to know them: they change what a read
554
+ // does. `untrack` suppresses dependency collection, which is exactly the kind of thing
555
+ // dependency analysis must not guess at.
556
+ $untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
557
+ $untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" },
558
+ // Scopes, scheduling and lifecycle: no value to classify, but the same authoring promise,
559
+ // no import to write. `$cleanup` registers a callback on the current owner (`onCleanup`).
560
+ $batch: { export: "batch", module: REACTIVE_MODULE, returns: "plain" },
561
+ $cleanup: { export: "onCleanup", module: REACTIVE_MODULE, returns: "plain" },
562
+ $root: { export: "root", module: REACTIVE_MODULE, returns: "plain" },
563
+ $context: { export: "context", module: REACTIVE_MODULE, returns: "plain" },
564
+ $renderEffect: { export: "renderEffect", module: REACTIVE_MODULE, returns: "effect" },
565
+ $computed: { export: "computed", module: REACTIVE_MODULE, returns: "effect" },
566
+ // Storage for where the code runs, and a signal kept in it. `$persisted` is a signal
567
+ // handle as far as reads go: the same accessor with `set` on it.
568
+ $storage: { export: "useStorage", module: "@fluixi/core/storage", returns: "plain" },
569
+ $asyncStorage: { export: "useAsyncStorage", module: "@fluixi/core/storage", returns: "plain" },
570
+ $persisted: { export: "persisted", module: "@fluixi/core/storage", returns: "signal-handle" },
571
+ // An environment variable, public or server-only; the browser build refuses a secret.
572
+ $env: { export: "env", module: "@fluixi/start/env", returns: "plain" }
573
+ };
574
+ var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
575
+
576
+ // src/transform/source-locations.ts
577
+ var SOURCE_MARKER = "__fx_source";
578
+ var HOLE_MARKER = "__fx_hole";
579
+ var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
580
+ var PROPS_MARKER = "__fx_props";
581
+ var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
582
+
583
+ // src/codegen/backends/imperative.ts
584
+ var CONTROL_COMPONENT = {
585
+ show: "Show",
586
+ for: "For",
587
+ index: "Index",
588
+ switch: "Switch",
589
+ match: "Match",
590
+ dynamic: "Dynamic",
591
+ errorBoundary: "ErrorBoundary",
592
+ suspense: "Suspense"
593
+ };
594
+ function isIdent(name) {
595
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
596
+ }
597
+ function propKey(name) {
598
+ return isIdent(name) ? name : JSON.stringify(name);
599
+ }
600
+ function propValue(p) {
601
+ if (p.expr !== void 0) {
602
+ if (p.name === "ref" && p.at)
603
+ return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
604
+ if (!p.reactive) return paren(p.expr, "value");
605
+ const accessor = `() => ${paren(p.expr, "arrowBody")}`;
606
+ return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
607
+ }
608
+ return JSON.stringify(p.literal ?? true);
609
+ }
610
+ function componentPropValue(p) {
611
+ if (p.expr !== void 0) return paren(p.expr, "value");
612
+ return JSON.stringify(p.literal ?? true);
613
+ }
614
+ function emitProps(props, childrenExpr, used) {
615
+ const spreads = props.filter((p) => p.kind === "spread");
616
+ const normal = props.filter((p) => p.kind !== "spread");
617
+ const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
618
+ if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
619
+ const obj = `{ ${entries.join(", ")} }`;
620
+ if (spreads.length > 0) {
621
+ used.add("mergeProps");
622
+ return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
623
+ }
624
+ return obj;
625
+ }
626
+ function markBind(code, at) {
627
+ return at ? markHole(code, at.line, at.column) : code;
628
+ }
629
+ function emitChildren(children, used, templates) {
630
+ if (children.length === 1) return emitNode(children[0], used, templates);
631
+ return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
632
+ }
633
+ function emitComponent(name, props, children, used, templates, at) {
634
+ used.add("createMemo");
635
+ used.add("createComponent");
636
+ const spreads = props.filter((p) => p.kind === "spread");
637
+ const normal = props.filter((p) => p.kind !== "spread");
638
+ const bodies = normal.map((p) => [p.name, componentPropValue(p)]);
639
+ if (children.length > 0) {
640
+ bodies.push(["children", emitChildren(children, used, templates)]);
641
+ }
642
+ let obj = `{ ${bodies.map(([name2, body]) => `get ${propKey(name2)}() { return ${body}; }`).join(", ")} }`;
643
+ if (bodies.some(([, body]) => /\bthis\b/.test(body))) {
644
+ const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => ${body}`);
645
+ const getters = bodies.map(([name2]) => `get ${propKey(name2)}() { return _$t[${JSON.stringify(name2)}](); }`);
646
+ obj = `((_$t) => ({ ${getters.join(", ")} }))({ ${thunks.join(", ")} })`;
647
+ }
648
+ if (spreads.length > 0) {
649
+ used.add("mergeProps");
650
+ obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
651
+ }
652
+ const propAt = {};
653
+ for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
654
+ if (Object.keys(propAt).length) obj = markProps(obj, propAt);
655
+ const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
656
+ return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
657
+ }
658
+ function staticAttrAssignment(el, prop) {
659
+ const name = htmlAttrName(prop.name);
660
+ const value = prop.literal;
661
+ if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
662
+ if (value === false || value === null) return "";
663
+ return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
664
+ }
665
+ var partial = false;
666
+ function emitNode(node, used, templates) {
667
+ switch (node.kind) {
668
+ case "text":
669
+ return JSON.stringify(node.value);
670
+ case "expr": {
671
+ if (!node.reactive) return paren(node.code, "value");
672
+ const accessor = `() => ${paren(node.code, "arrowBody")}`;
673
+ return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
674
+ }
675
+ case "fragment":
676
+ if (node.children.length === 0) return "null";
677
+ return emitChildren(node.children, used, templates);
678
+ case "component":
679
+ return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
680
+ case "control": {
681
+ const name = CONTROL_COMPONENT[node.control] ?? node.control;
682
+ used.add(name);
683
+ return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
684
+ }
685
+ case "element": {
686
+ if (templates && node.static && !node.svg && isSerializable(node)) {
687
+ used.add("templateNode");
688
+ const id = `_tmpl$${templates.length}`;
689
+ templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
690
+ const at = templatePositions(node);
691
+ return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
692
+ }
693
+ if (templates && partial) {
694
+ const plan = planPartialTemplate(node);
695
+ if (plan) {
696
+ used.add("templateNode");
697
+ used.add("insert");
698
+ used.add("holeEnd");
699
+ used.add("holeContent");
700
+ used.add("holeScope");
701
+ const id = `_tmpl$${templates.length}`;
702
+ templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
703
+ const at = templatePositions(node);
704
+ const lines = [
705
+ `const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
706
+ ];
707
+ for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
708
+ for (const hole of plan.holes) {
709
+ lines.push(
710
+ `insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
711
+ );
712
+ }
713
+ lines.push("return _el$;");
714
+ return `(() => { ${lines.join(" ")} })()`;
715
+ }
716
+ }
717
+ used.add("createNativeElement");
718
+ const tag = JSON.stringify(node.tag);
719
+ const el = "_el$";
720
+ const parts = [];
721
+ const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
722
+ parts.push(`const ${el} = createNativeElement(${createArgs});`);
723
+ if (node.props.length > 0) {
724
+ if (!node.svg && node.props.every(isPlainLiteralAttr)) {
725
+ for (const p of node.props) parts.push(staticAttrAssignment(el, p));
726
+ } else {
727
+ used.add("spread");
728
+ const svgFlag = node.svg ? ", isSVG: true" : "";
729
+ const accessors = node.props.filter((p) => p.kind !== "spread" && p.expr !== void 0 && p.reactive && p.name !== "ref").map((p) => p.name);
730
+ const customFlag = node.tag.includes("-") ? `, isCustomElement: true, accessors: ${JSON.stringify(accessors)}` : "";
731
+ parts.push(
732
+ `spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag}${customFlag} });`
733
+ );
734
+ }
735
+ }
736
+ for (const child of node.children) {
737
+ used.add("insert");
738
+ const childCode = emitNode(child, used, templates);
739
+ const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
740
+ parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
741
+ }
742
+ parts.push(`return ${el};`);
743
+ return `(() => { ${parts.join(" ")} })()`;
744
+ }
745
+ }
746
+ }
747
+ function inlineTemplates(code, templates) {
748
+ if (!templates || templates.length === 0) return code;
749
+ const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
750
+ return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
751
+ }
752
+ var imperativeBackend = {
753
+ name: "imperative",
754
+ contract: RUNTIME_CONTRACT_V1,
755
+ emit(node, options) {
756
+ const used = /* @__PURE__ */ new Set();
757
+ const templates = options?.templateClone !== false ? [] : void 0;
758
+ partial = options?.partialTemplates === true;
759
+ const code = emitNode(node, used, templates);
760
+ return { code, imports: Array.from(used), templates };
761
+ }
762
+ };
763
+
335
764
  // src/lower/jsx.ts
336
765
  var SVG_TAGS = /* @__PURE__ */ new Set([
337
766
  "svg",
@@ -363,6 +792,22 @@ var DELEGATED = /* @__PURE__ */ new Set([
363
792
  "mouseup"
364
793
  ]);
365
794
  var isReactive = (node) => isReactiveShape(shapeOf(node));
795
+ var CONTAINER = "container";
796
+ function directive(node, name) {
797
+ if (node.type !== "JSXElement") return void 0;
798
+ const opening = node.openingElement;
799
+ if (opening.name.type !== "JSXIdentifier" || opening.name.name[0] !== opening.name.name[0].toLowerCase()) return void 0;
800
+ return opening.attributes.find((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === name);
801
+ }
802
+ function directiveExpr(attr) {
803
+ const v = attr.value;
804
+ return v?.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression" ? v.expression : void 0;
805
+ }
806
+ function emitIR(ir, ctx) {
807
+ const { code, imports, templates } = imperativeBackend.emit(ir, {});
808
+ for (const s of imports) ctx.used.add(s);
809
+ return inlineTemplates(code, templates);
810
+ }
366
811
  var posOf = (node, ctx) => {
367
812
  if (!ctx.sourceFile) return void 0;
368
813
  const at = node.loc?.start;
@@ -471,7 +916,16 @@ function buildProps(attrs, ctx) {
471
916
  }
472
917
  function buildChildren(children, ctx) {
473
918
  const out = [];
474
- for (const child of children) {
919
+ for (let i = 0; i < children.length; i++) {
920
+ const child = children[i];
921
+ if (directive(child, "if")) {
922
+ let next = i + 1;
923
+ while (next < children.length && children[next].type === "JSXText" && !cleanTemplateText(children[next].value)) next++;
924
+ const fallback = directive(children[next], "else") ? children[next] : null;
925
+ out.push(buildIf(child, fallback, ctx));
926
+ if (fallback) i = next;
927
+ continue;
928
+ }
475
929
  if (child.type === "JSXText") {
476
930
  const text = cleanTemplateText(child.value);
477
931
  if (text) out.push({ kind: "text", value: text });
@@ -508,19 +962,68 @@ function readLoad(attrs) {
508
962
  }
509
963
  return void 0;
510
964
  }
965
+ function buildIf(node, fallback, ctx) {
966
+ const cond = directiveExpr(directive(node, "if"));
967
+ const props = [
968
+ cond ? { name: "when", kind: "attr", expr: ctx.code(cond), reactive: isReactive(cond), at: posOf(cond, ctx) } : { name: "when", kind: "attr", literal: true }
969
+ ];
970
+ if (fallback) {
971
+ props.push({ name: "fallback", kind: "attr", expr: emitIR(buildElement(fallback, ctx, ["else"]), ctx), jsxElement: true });
972
+ }
973
+ return { kind: "component", name: "Show", props, children: [buildElement(node, ctx, ["if"])] };
974
+ }
975
+ function buildEach(node, ctx) {
976
+ const attr = directive(node, "each");
977
+ if (!attr) return null;
978
+ const list = directiveExpr(attr);
979
+ const props = [
980
+ list ? { name: "each", kind: "attr", expr: ctx.code(list), reactive: isReactive(list), at: posOf(list, ctx) } : { name: "each", kind: "attr", expr: "[]" }
981
+ ];
982
+ const key = directive(node, "key");
983
+ if (key) {
984
+ const keyExpr = directiveExpr(key);
985
+ const by = isString(key.value) ? `(item) => item[${JSON.stringify(literalOf(key.value))}]` : keyExpr ? ctx.code(keyExpr) : void 0;
986
+ if (by) props.push({ name: "by", kind: "attr", expr: by });
987
+ }
988
+ const content = node.children.filter((c) => !(c.type === "JSXText" && !cleanTemplateText(c.value)));
989
+ const only = content.length === 1 && content[0].type === "JSXExpressionContainer" ? content[0].expression : null;
990
+ let render;
991
+ if (only && (only.type === "ArrowFunctionExpression" || only.type === "FunctionExpression")) {
992
+ const fn = ctx.code(only);
993
+ const live = only.type === "ArrowFunctionExpression" && only.body.type !== "BlockStatement" && isReactive(only.body);
994
+ render = live ? `(...args) => () => (${fn})(...args)` : fn;
995
+ } else {
996
+ const built = buildChildren(node.children, ctx);
997
+ const ir = built.length === 1 ? built[0] : { kind: "fragment", children: built };
998
+ render = `() => ${paren(emitIR(ir, ctx), "arrowBody")}`;
999
+ }
1000
+ return { kind: "component", name: "For", props, children: [{ kind: "expr", code: render, reactive: false }] };
1001
+ }
1002
+ function buildElement(node, ctx, handled) {
1003
+ const skip = new Set(handled);
1004
+ const directives = /* @__PURE__ */ new Set(["if", "else", "each", ...directive(node, "each") ? ["key"] : []]);
1005
+ const attributes = node.openingElement.attributes.filter(
1006
+ (a) => !(a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && (skip.has(a.name.name) || directives.has(a.name.name)))
1007
+ );
1008
+ const { tag } = tagInfo(node.openingElement.name, ctx);
1009
+ const each = buildEach(node, ctx);
1010
+ const children = each ? [each] : buildChildren(node.children, ctx);
1011
+ if (tag === CONTAINER) return children.length === 1 ? children[0] : { kind: "fragment", children };
1012
+ const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
1013
+ return { kind: "element", tag, svg: SVG_TAGS.has(tag), props: buildProps(attributes, ctx), children, static: false, ...at ? { at } : {} };
1014
+ }
511
1015
  function buildNode(node, ctx) {
512
1016
  if (node.type === "JSXFragment") {
513
1017
  return { kind: "fragment", children: buildChildren(node.children, ctx) };
514
1018
  }
1019
+ if (directive(node, "if")) return buildIf(node, null, ctx);
515
1020
  const { tag, component } = tagInfo(node.openingElement.name, ctx);
1021
+ if (!component) return buildElement(node, ctx, []);
516
1022
  const props = buildProps(node.openingElement.attributes, ctx);
517
1023
  const children = buildChildren(node.children, ctx);
518
1024
  const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
519
- if (component) {
520
- const load = readLoad(node.openingElement.attributes);
521
- return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
522
- }
523
- return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
1025
+ const load = readLoad(node.openingElement.attributes);
1026
+ return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
524
1027
  }
525
1028
  function buildJsxIR(node, ctx) {
526
1029
  const ir = buildNode(node, ctx);
@@ -1 +1 @@
1
- {"version":3,"file":"jsx.d.ts","sourceRoot":"","sources":["../../src/lower/jsx.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAkB,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AAQjF,UAAU,IAAI;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,+EAA+E;IAC/E,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAAC;IACzB,mFAAmF;IACnF,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAClB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,QAAQ,aAGnB,CAAC;AA4MH,iDAAiD;AACjD,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAM9D"}
1
+ {"version":3,"file":"jsx.d.ts","sourceRoot":"","sources":["../../src/lower/jsx.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAkB,MAAM,EAAsB,MAAM,gBAAgB,CAAC;AASjF,UAAU,IAAI;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,+EAA+E;IAC/E,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAAC;IACzB,mFAAmF;IACnF,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAClB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,QAAQ,aAGnB,CAAC;AAsTH,iDAAiD;AACjD,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAM9D"}