@fluixi/compiler 1.0.0-alpha.92 → 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 (68) hide show
  1. package/dist/analyze/intrinsics.cjs +18 -0
  2. package/dist/analyze/intrinsics.d.ts.map +1 -1
  3. package/dist/analyze/intrinsics.js +18 -0
  4. package/dist/analyze/intrinsics.mjs +18 -0
  5. package/dist/analyze/reactive-bindings.cjs +18 -0
  6. package/dist/analyze/reactive-bindings.mjs +18 -0
  7. package/dist/analyze/static-graph.cjs +22 -4
  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 +22 -4
  11. package/dist/{babel-GI375YZ2.mjs → babel-RJHJQVEM.mjs} +4 -4
  12. package/dist/{chunk-QZ4CJPYP.mjs → chunk-56QSOVVL.mjs} +1 -1
  13. package/dist/{chunk-XJ5H2MXY.mjs → chunk-OD53TMER.mjs} +19 -1
  14. package/dist/{chunk-DWNWN5RH.mjs → chunk-R7PPFBCL.mjs} +1 -1
  15. package/dist/{chunk-VOUI6XPV.mjs → chunk-TJIH7TLX.mjs} +1 -1
  16. package/dist/{chunk-VC6X44FS.mjs → chunk-U7HCFLZ2.mjs} +153 -48
  17. package/dist/codegen/backends/imperative.cjs +24 -6
  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 +24 -6
  21. package/dist/frontend/babel/build-ir-lit.cjs +120 -43
  22. package/dist/frontend/babel/build-ir-lit.mjs +120 -43
  23. package/dist/frontend/babel/build-ir.cjs +120 -43
  24. package/dist/frontend/babel/build-ir.mjs +120 -43
  25. package/dist/frontend/babel/index.cjs +121 -44
  26. package/dist/frontend/babel/index.mjs +121 -44
  27. package/dist/frontend/babel/lower-template.cjs +120 -43
  28. package/dist/frontend/babel/lower-template.mjs +120 -43
  29. package/dist/frontend/babel/plugin.cjs +120 -43
  30. package/dist/frontend/babel/plugin.mjs +120 -43
  31. package/dist/index.cjs +41 -38
  32. package/dist/index.mjs +41 -38
  33. package/dist/integrations.cjs +176 -52
  34. package/dist/integrations.mjs +8 -8
  35. package/dist/lower/compile-template.cjs +41 -38
  36. package/dist/lower/compile-template.mjs +41 -38
  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 +40 -37
  42. package/dist/lower/template.d.ts.map +1 -1
  43. package/dist/lower/template.js +22 -30
  44. package/dist/lower/template.mjs +40 -37
  45. package/dist/resolve/builtins.cjs +1 -1
  46. package/dist/resolve/builtins.mjs +1 -1
  47. package/dist/resolve/component-globals.cjs +14 -0
  48. package/dist/resolve/component-globals.mjs +14 -0
  49. package/dist/resolve/write-globals.cjs +14 -0
  50. package/dist/resolve/write-globals.mjs +14 -0
  51. package/dist/{server-fns-BBZRZUPP.mjs → server-fns-ZFRDIQ2X.mjs} +4 -4
  52. package/dist/{server-functions-VEGLIFCE.mjs → server-functions-CTDXX67X.mjs} +3 -3
  53. package/dist/transform/index.cjs +170 -47
  54. package/dist/transform/index.mjs +170 -47
  55. package/dist/transform/intrinsics.cjs +18 -0
  56. package/dist/transform/intrinsics.mjs +18 -0
  57. package/dist/transform/source-locations.cjs +64 -3
  58. package/dist/transform/source-locations.d.cts +7 -2
  59. package/dist/transform/source-locations.d.ts +7 -2
  60. package/dist/transform/source-locations.d.ts.map +1 -1
  61. package/dist/transform/source-locations.js +68 -3
  62. package/dist/transform/source-locations.mjs +64 -3
  63. package/dist/transform/templates.cjs +169 -46
  64. package/dist/transform/templates.js +2 -2
  65. package/dist/transform/templates.mjs +169 -46
  66. package/dist/{transform-UWZ7ZGJ2.mjs → transform-QWWQDCXE.mjs} +7 -7
  67. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  68. package/package.json +9 -3
@@ -305,6 +305,435 @@ function paren(code, position) {
305
305
  return topLevelComma(src) === false ? code : `(${code})`;
306
306
  }
307
307
 
308
+ // src/codegen/serialize-static.ts
309
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
310
+ "area",
311
+ "base",
312
+ "br",
313
+ "col",
314
+ "embed",
315
+ "hr",
316
+ "img",
317
+ "input",
318
+ "link",
319
+ "meta",
320
+ "param",
321
+ "source",
322
+ "track",
323
+ "wbr"
324
+ ]);
325
+ var HTML_NAME = {
326
+ className: "class",
327
+ htmlFor: "for"
328
+ };
329
+ var PROPERTY_ONLY = /* @__PURE__ */ new Set([
330
+ "innerHTML",
331
+ "outerHTML",
332
+ "innerText",
333
+ "textContent",
334
+ "value",
335
+ "checked",
336
+ "selected",
337
+ "muted",
338
+ "defaultValue",
339
+ "defaultChecked",
340
+ "children",
341
+ "ref"
342
+ ]);
343
+ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
344
+ function isRawTextElement(tag) {
345
+ return RAW_TEXT_ELEMENTS.has(tag);
346
+ }
347
+ function htmlAttrName(name) {
348
+ return HTML_NAME[name] ?? name;
349
+ }
350
+ function isPlainLiteralAttr(prop) {
351
+ if (prop.kind !== "attr") return false;
352
+ if (prop.expr !== void 0) return false;
353
+ if (prop.name.includes(":")) return false;
354
+ return !PROPERTY_ONLY.has(prop.name);
355
+ }
356
+ function isSerializable(node) {
357
+ if (!node.static) return false;
358
+ if (isRawTextElement(node.tag)) return false;
359
+ for (const prop of node.props) {
360
+ if (!isPlainLiteralAttr(prop)) return false;
361
+ }
362
+ for (const child of node.children) {
363
+ if (child.kind === "text") continue;
364
+ if (child.kind === "element" && isSerializable(child)) continue;
365
+ return false;
366
+ }
367
+ return true;
368
+ }
369
+ function serializeStatic(node) {
370
+ const attrs = node.props.map(serializeProp).filter(Boolean).join("");
371
+ const open = `<${node.tag}${attrs}>`;
372
+ if (VOID_ELEMENTS.has(node.tag)) return open;
373
+ return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
374
+ }
375
+ function serializeChild(child) {
376
+ if (child.kind === "text") return escapeText(child.value);
377
+ if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
378
+ return serializeStatic(child);
379
+ }
380
+ function serializeProp(prop) {
381
+ const name = HTML_NAME[prop.name] ?? prop.name;
382
+ const value = prop.literal;
383
+ if (value === true || value === void 0) return ` ${name}`;
384
+ if (value === false || value === null) return "";
385
+ return ` ${name}="${escapeAttr(String(value))}"`;
386
+ }
387
+ function escapeAttr(value) {
388
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
389
+ }
390
+ function escapeText(value) {
391
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
392
+ }
393
+ function templatePositions(root) {
394
+ const pos = [];
395
+ let file;
396
+ const walk = (el) => {
397
+ file ??= el.at?.file;
398
+ pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
399
+ for (const child of el.children) if (child.kind === "element") walk(child);
400
+ };
401
+ walk(root);
402
+ return file && pos.some((n) => n !== 0) ? { file, pos } : void 0;
403
+ }
404
+
405
+ // src/codegen/partial-template.ts
406
+ var HOLE_START = "<!--fx-->";
407
+ var HOLE_END = "<!--fx/-->";
408
+ function isHole(node) {
409
+ return node.kind === "expr" || node.kind === "component" || node.kind === "control";
410
+ }
411
+ function isTemplateChild(node) {
412
+ return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
413
+ }
414
+ function isTemplateElement(node) {
415
+ if (node.svg) return false;
416
+ if (isRawTextElement(node.tag)) return false;
417
+ if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
418
+ return node.children.every((c) => isTemplateChild(c) || isHole(c));
419
+ }
420
+ function containsHole(node) {
421
+ if (isHole(node)) return true;
422
+ const children = node.children;
423
+ return children ? children.some(containsHole) : false;
424
+ }
425
+ function addressable(el) {
426
+ for (let i = 0; i < el.children.length; i++) {
427
+ const child = el.children[i];
428
+ if (child.kind === "text") {
429
+ if (child.value === "") return false;
430
+ if (el.children[i + 1]?.kind === "text") return false;
431
+ }
432
+ }
433
+ return el.children.every((c) => c.kind !== "element" || addressable(c));
434
+ }
435
+ function planPartialTemplate(root) {
436
+ if (!isTemplateElement(root)) return null;
437
+ if (!addressable(root)) return null;
438
+ if (!root.children.some(containsHole)) return null;
439
+ const steps = [];
440
+ const holes = [];
441
+ let refCount = 0;
442
+ const declare = (expr) => {
443
+ const ref = `_n$${refCount++}`;
444
+ steps.push({ ref, expr });
445
+ return ref;
446
+ };
447
+ walk(root, "_el$");
448
+ return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
449
+ function walk(el, elementRef) {
450
+ let last = -1;
451
+ el.children.forEach((child, i) => {
452
+ if (containsHole(child)) last = i;
453
+ });
454
+ let previous = null;
455
+ for (let i = 0; i <= last; i++) {
456
+ const child = el.children[i];
457
+ const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
458
+ if (isHole(child)) {
459
+ const startRef = declare(expr);
460
+ const endRef = declare(`holeEnd(${startRef})`);
461
+ holes.push({ parentRef: elementRef, startRef, endRef, node: child });
462
+ previous = endRef;
463
+ continue;
464
+ }
465
+ const ref = declare(expr);
466
+ if (child.kind === "element" && containsHole(child)) walk(child, ref);
467
+ previous = ref;
468
+ }
469
+ }
470
+ }
471
+ function serializeWithMarkers(el) {
472
+ return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
473
+ }
474
+ function withSentinels(el) {
475
+ return {
476
+ ...el,
477
+ children: el.children.map(
478
+ (child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
479
+ )
480
+ };
481
+ }
482
+ var SENTINEL = "\0fx-hole\0";
483
+
484
+ // src/codegen/contract.ts
485
+ var RUNTIME_CONTRACT_V1 = {
486
+ version: 1,
487
+ module: "@fluixi/dom",
488
+ symbols: [
489
+ "createNativeElement",
490
+ "insert",
491
+ "spread",
492
+ "setAttribute",
493
+ "delegateEvents",
494
+ "createComponent",
495
+ "createMemo",
496
+ "untrack",
497
+ "Show",
498
+ "For",
499
+ "Index",
500
+ "Switch",
501
+ "Match",
502
+ "Dynamic",
503
+ "ErrorBoundary"
504
+ ]
505
+ };
506
+ var RUNTIME_CONTRACT_V2 = {
507
+ version: 2,
508
+ module: "@fluixi/dom",
509
+ symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
510
+ };
511
+
512
+ // src/analyze/intrinsics.ts
513
+ var REACTIVE_MODULE = "@fluixi/reactive/signal";
514
+ var INTRINSICS = {
515
+ $signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
516
+ $memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
517
+ $effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
518
+ $store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
519
+ $resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
520
+ $cachedResource: { export: "cachedResource", module: "@fluixi/reactive/cached-resource", returns: "resource-handle" },
521
+ // No short runtime form for these yet, so the intrinsic stands for the `create*`
522
+ // name. Still worth having: no import to write, and when a shorter form is
523
+ // designed, the change is this line rather than a compiler pass.
524
+ $selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
525
+ $deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
526
+ // Not value constructors, but the compiler has to know them: they change what a read
527
+ // does. `untrack` suppresses dependency collection, which is exactly the kind of thing
528
+ // dependency analysis must not guess at.
529
+ $untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
530
+ $untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" },
531
+ // Scopes, scheduling and lifecycle: no value to classify, but the same authoring promise,
532
+ // no import to write. `$cleanup` registers a callback on the current owner (`onCleanup`).
533
+ $batch: { export: "batch", module: REACTIVE_MODULE, returns: "plain" },
534
+ $cleanup: { export: "onCleanup", module: REACTIVE_MODULE, returns: "plain" },
535
+ $root: { export: "root", module: REACTIVE_MODULE, returns: "plain" },
536
+ $context: { export: "context", module: REACTIVE_MODULE, returns: "plain" },
537
+ $renderEffect: { export: "renderEffect", module: REACTIVE_MODULE, returns: "effect" },
538
+ $computed: { export: "computed", module: REACTIVE_MODULE, returns: "effect" },
539
+ // Storage for where the code runs, and a signal kept in it. `$persisted` is a signal
540
+ // handle as far as reads go: the same accessor with `set` on it.
541
+ $storage: { export: "useStorage", module: "@fluixi/core/storage", returns: "plain" },
542
+ $asyncStorage: { export: "useAsyncStorage", module: "@fluixi/core/storage", returns: "plain" },
543
+ $persisted: { export: "persisted", module: "@fluixi/core/storage", returns: "signal-handle" },
544
+ // An environment variable, public or server-only; the browser build refuses a secret.
545
+ $env: { export: "env", module: "@fluixi/start/env", returns: "plain" }
546
+ };
547
+ var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n) => n.slice(1)).join("|")})\\s*\\(`);
548
+
549
+ // src/transform/source-locations.ts
550
+ var SOURCE_MARKER = "__fx_source";
551
+ var HOLE_MARKER = "__fx_hole";
552
+ var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
553
+ var PROPS_MARKER = "__fx_props";
554
+ var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
555
+
556
+ // src/codegen/backends/imperative.ts
557
+ var CONTROL_COMPONENT = {
558
+ show: "Show",
559
+ for: "For",
560
+ index: "Index",
561
+ switch: "Switch",
562
+ match: "Match",
563
+ dynamic: "Dynamic",
564
+ errorBoundary: "ErrorBoundary",
565
+ suspense: "Suspense"
566
+ };
567
+ function isIdent(name) {
568
+ return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
569
+ }
570
+ function propKey(name) {
571
+ return isIdent(name) ? name : JSON.stringify(name);
572
+ }
573
+ function propValue(p) {
574
+ if (p.expr !== void 0) {
575
+ if (p.name === "ref" && p.at)
576
+ return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
577
+ if (!p.reactive) return paren(p.expr, "value");
578
+ const accessor = `() => ${paren(p.expr, "arrowBody")}`;
579
+ return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
580
+ }
581
+ return JSON.stringify(p.literal ?? true);
582
+ }
583
+ function componentPropValue(p) {
584
+ if (p.expr !== void 0) return paren(p.expr, "value");
585
+ return JSON.stringify(p.literal ?? true);
586
+ }
587
+ function emitProps(props, childrenExpr, used) {
588
+ const spreads = props.filter((p) => p.kind === "spread");
589
+ const normal = props.filter((p) => p.kind !== "spread");
590
+ const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
591
+ if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
592
+ const obj = `{ ${entries.join(", ")} }`;
593
+ if (spreads.length > 0) {
594
+ used.add("mergeProps");
595
+ return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
596
+ }
597
+ return obj;
598
+ }
599
+ function markBind(code, at) {
600
+ return at ? markHole(code, at.line, at.column) : code;
601
+ }
602
+ function emitChildren(children, used, templates) {
603
+ if (children.length === 1) return emitNode(children[0], used, templates);
604
+ return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
605
+ }
606
+ function emitComponent(name, props, children, used, templates, at) {
607
+ used.add("createMemo");
608
+ used.add("createComponent");
609
+ const spreads = props.filter((p) => p.kind === "spread");
610
+ const normal = props.filter((p) => p.kind !== "spread");
611
+ const bodies = normal.map((p) => [p.name, componentPropValue(p)]);
612
+ if (children.length > 0) {
613
+ bodies.push(["children", emitChildren(children, used, templates)]);
614
+ }
615
+ let obj = `{ ${bodies.map(([name2, body]) => `get ${propKey(name2)}() { return ${body}; }`).join(", ")} }`;
616
+ if (bodies.some(([, body]) => /\bthis\b/.test(body))) {
617
+ const thunks = bodies.map(([name2, body]) => `${JSON.stringify(name2)}: () => ${body}`);
618
+ const getters = bodies.map(([name2]) => `get ${propKey(name2)}() { return _$t[${JSON.stringify(name2)}](); }`);
619
+ obj = `((_$t) => ({ ${getters.join(", ")} }))({ ${thunks.join(", ")} })`;
620
+ }
621
+ if (spreads.length > 0) {
622
+ used.add("mergeProps");
623
+ obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
624
+ }
625
+ const propAt = {};
626
+ for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
627
+ if (Object.keys(propAt).length) obj = markProps(obj, propAt);
628
+ const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
629
+ return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
630
+ }
631
+ function staticAttrAssignment(el, prop) {
632
+ const name = htmlAttrName(prop.name);
633
+ const value = prop.literal;
634
+ if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
635
+ if (value === false || value === null) return "";
636
+ return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
637
+ }
638
+ var partial = false;
639
+ function emitNode(node, used, templates) {
640
+ switch (node.kind) {
641
+ case "text":
642
+ return JSON.stringify(node.value);
643
+ case "expr": {
644
+ if (!node.reactive) return paren(node.code, "value");
645
+ const accessor = `() => ${paren(node.code, "arrowBody")}`;
646
+ return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
647
+ }
648
+ case "fragment":
649
+ if (node.children.length === 0) return "null";
650
+ return emitChildren(node.children, used, templates);
651
+ case "component":
652
+ return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
653
+ case "control": {
654
+ const name = CONTROL_COMPONENT[node.control] ?? node.control;
655
+ used.add(name);
656
+ return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
657
+ }
658
+ case "element": {
659
+ if (templates && node.static && !node.svg && isSerializable(node)) {
660
+ used.add("templateNode");
661
+ const id = `_tmpl$${templates.length}`;
662
+ templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
663
+ const at = templatePositions(node);
664
+ return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
665
+ }
666
+ if (templates && partial) {
667
+ const plan = planPartialTemplate(node);
668
+ if (plan) {
669
+ used.add("templateNode");
670
+ used.add("insert");
671
+ used.add("holeEnd");
672
+ used.add("holeContent");
673
+ used.add("holeScope");
674
+ const id = `_tmpl$${templates.length}`;
675
+ templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
676
+ const at = templatePositions(node);
677
+ const lines = [
678
+ `const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
679
+ ];
680
+ for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
681
+ for (const hole of plan.holes) {
682
+ lines.push(
683
+ `insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
684
+ );
685
+ }
686
+ lines.push("return _el$;");
687
+ return `(() => { ${lines.join(" ")} })()`;
688
+ }
689
+ }
690
+ used.add("createNativeElement");
691
+ const tag = JSON.stringify(node.tag);
692
+ const el = "_el$";
693
+ const parts = [];
694
+ const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
695
+ parts.push(`const ${el} = createNativeElement(${createArgs});`);
696
+ if (node.props.length > 0) {
697
+ if (!node.svg && node.props.every(isPlainLiteralAttr)) {
698
+ for (const p of node.props) parts.push(staticAttrAssignment(el, p));
699
+ } else {
700
+ used.add("spread");
701
+ const svgFlag = node.svg ? ", isSVG: true" : "";
702
+ const accessors = node.props.filter((p) => p.kind !== "spread" && p.expr !== void 0 && p.reactive && p.name !== "ref").map((p) => p.name);
703
+ const customFlag = node.tag.includes("-") ? `, isCustomElement: true, accessors: ${JSON.stringify(accessors)}` : "";
704
+ parts.push(
705
+ `spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag}${customFlag} });`
706
+ );
707
+ }
708
+ }
709
+ for (const child of node.children) {
710
+ used.add("insert");
711
+ const childCode = emitNode(child, used, templates);
712
+ const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
713
+ parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
714
+ }
715
+ parts.push(`return ${el};`);
716
+ return `(() => { ${parts.join(" ")} })()`;
717
+ }
718
+ }
719
+ }
720
+ function inlineTemplates(code, templates) {
721
+ if (!templates || templates.length === 0) return code;
722
+ const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
723
+ return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
724
+ }
725
+ var imperativeBackend = {
726
+ name: "imperative",
727
+ contract: RUNTIME_CONTRACT_V1,
728
+ emit(node, options) {
729
+ const used = /* @__PURE__ */ new Set();
730
+ const templates = options?.templateClone !== false ? [] : void 0;
731
+ partial = options?.partialTemplates === true;
732
+ const code = emitNode(node, used, templates);
733
+ return { code, imports: Array.from(used), templates };
734
+ }
735
+ };
736
+
308
737
  // src/lower/jsx.ts
309
738
  var SVG_TAGS = /* @__PURE__ */ new Set([
310
739
  "svg",
@@ -336,6 +765,22 @@ var DELEGATED = /* @__PURE__ */ new Set([
336
765
  "mouseup"
337
766
  ]);
338
767
  var isReactive = (node) => isReactiveShape(shapeOf(node));
768
+ var CONTAINER = "container";
769
+ function directive(node, name) {
770
+ if (node.type !== "JSXElement") return void 0;
771
+ const opening = node.openingElement;
772
+ if (opening.name.type !== "JSXIdentifier" || opening.name.name[0] !== opening.name.name[0].toLowerCase()) return void 0;
773
+ return opening.attributes.find((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === name);
774
+ }
775
+ function directiveExpr(attr) {
776
+ const v = attr.value;
777
+ return v?.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression" ? v.expression : void 0;
778
+ }
779
+ function emitIR(ir, ctx) {
780
+ const { code, imports, templates } = imperativeBackend.emit(ir, {});
781
+ for (const s of imports) ctx.used.add(s);
782
+ return inlineTemplates(code, templates);
783
+ }
339
784
  var posOf = (node, ctx) => {
340
785
  if (!ctx.sourceFile) return void 0;
341
786
  const at = node.loc?.start;
@@ -444,7 +889,16 @@ function buildProps(attrs, ctx) {
444
889
  }
445
890
  function buildChildren(children, ctx) {
446
891
  const out = [];
447
- for (const child of children) {
892
+ for (let i = 0; i < children.length; i++) {
893
+ const child = children[i];
894
+ if (directive(child, "if")) {
895
+ let next = i + 1;
896
+ while (next < children.length && children[next].type === "JSXText" && !cleanTemplateText(children[next].value)) next++;
897
+ const fallback = directive(children[next], "else") ? children[next] : null;
898
+ out.push(buildIf(child, fallback, ctx));
899
+ if (fallback) i = next;
900
+ continue;
901
+ }
448
902
  if (child.type === "JSXText") {
449
903
  const text = cleanTemplateText(child.value);
450
904
  if (text) out.push({ kind: "text", value: text });
@@ -481,19 +935,68 @@ function readLoad(attrs) {
481
935
  }
482
936
  return void 0;
483
937
  }
938
+ function buildIf(node, fallback, ctx) {
939
+ const cond = directiveExpr(directive(node, "if"));
940
+ const props = [
941
+ cond ? { name: "when", kind: "attr", expr: ctx.code(cond), reactive: isReactive(cond), at: posOf(cond, ctx) } : { name: "when", kind: "attr", literal: true }
942
+ ];
943
+ if (fallback) {
944
+ props.push({ name: "fallback", kind: "attr", expr: emitIR(buildElement(fallback, ctx, ["else"]), ctx), jsxElement: true });
945
+ }
946
+ return { kind: "component", name: "Show", props, children: [buildElement(node, ctx, ["if"])] };
947
+ }
948
+ function buildEach(node, ctx) {
949
+ const attr = directive(node, "each");
950
+ if (!attr) return null;
951
+ const list = directiveExpr(attr);
952
+ const props = [
953
+ list ? { name: "each", kind: "attr", expr: ctx.code(list), reactive: isReactive(list), at: posOf(list, ctx) } : { name: "each", kind: "attr", expr: "[]" }
954
+ ];
955
+ const key = directive(node, "key");
956
+ if (key) {
957
+ const keyExpr = directiveExpr(key);
958
+ const by = isString(key.value) ? `(item) => item[${JSON.stringify(literalOf(key.value))}]` : keyExpr ? ctx.code(keyExpr) : void 0;
959
+ if (by) props.push({ name: "by", kind: "attr", expr: by });
960
+ }
961
+ const content = node.children.filter((c) => !(c.type === "JSXText" && !cleanTemplateText(c.value)));
962
+ const only = content.length === 1 && content[0].type === "JSXExpressionContainer" ? content[0].expression : null;
963
+ let render;
964
+ if (only && (only.type === "ArrowFunctionExpression" || only.type === "FunctionExpression")) {
965
+ const fn = ctx.code(only);
966
+ const live = only.type === "ArrowFunctionExpression" && only.body.type !== "BlockStatement" && isReactive(only.body);
967
+ render = live ? `(...args) => () => (${fn})(...args)` : fn;
968
+ } else {
969
+ const built = buildChildren(node.children, ctx);
970
+ const ir = built.length === 1 ? built[0] : { kind: "fragment", children: built };
971
+ render = `() => ${paren(emitIR(ir, ctx), "arrowBody")}`;
972
+ }
973
+ return { kind: "component", name: "For", props, children: [{ kind: "expr", code: render, reactive: false }] };
974
+ }
975
+ function buildElement(node, ctx, handled) {
976
+ const skip = new Set(handled);
977
+ const directives = /* @__PURE__ */ new Set(["if", "else", "each", ...directive(node, "each") ? ["key"] : []]);
978
+ const attributes = node.openingElement.attributes.filter(
979
+ (a) => !(a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && (skip.has(a.name.name) || directives.has(a.name.name)))
980
+ );
981
+ const { tag } = tagInfo(node.openingElement.name, ctx);
982
+ const each = buildEach(node, ctx);
983
+ const children = each ? [each] : buildChildren(node.children, ctx);
984
+ if (tag === CONTAINER) return children.length === 1 ? children[0] : { kind: "fragment", children };
985
+ const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
986
+ return { kind: "element", tag, svg: SVG_TAGS.has(tag), props: buildProps(attributes, ctx), children, static: false, ...at ? { at } : {} };
987
+ }
484
988
  function buildNode(node, ctx) {
485
989
  if (node.type === "JSXFragment") {
486
990
  return { kind: "fragment", children: buildChildren(node.children, ctx) };
487
991
  }
992
+ if (directive(node, "if")) return buildIf(node, null, ctx);
488
993
  const { tag, component } = tagInfo(node.openingElement.name, ctx);
994
+ if (!component) return buildElement(node, ctx, []);
489
995
  const props = buildProps(node.openingElement.attributes, ctx);
490
996
  const children = buildChildren(node.children, ctx);
491
997
  const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
492
- if (component) {
493
- const load = readLoad(node.openingElement.attributes);
494
- return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
495
- }
496
- return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
998
+ const load = readLoad(node.openingElement.attributes);
999
+ return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
497
1000
  }
498
1001
  function buildJsxIR(node, ctx) {
499
1002
  const ir = buildNode(node, ctx);