@matthesketh/utopia-compiler 0.0.3 → 0.0.5

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.cjs CHANGED
@@ -381,6 +381,7 @@ var CodeGenerator = class {
381
381
  varCounter = 0;
382
382
  helpers = /* @__PURE__ */ new Set();
383
383
  scopeId;
384
+ deferredCallsStack = [];
384
385
  generate(ast) {
385
386
  const scope = /* @__PURE__ */ new Set();
386
387
  const rootElements = ast.filter(
@@ -466,6 +467,7 @@ ${fnBody}
466
467
  if (dir.kind === "if" || dir.kind === "for") continue;
467
468
  this.genDirective(elVar, dir, scope);
468
469
  }
470
+ this.deferredCallsStack.push([]);
469
471
  for (const child of node.children) {
470
472
  const childVar = this.genNode(child, scope);
471
473
  if (childVar) {
@@ -473,6 +475,10 @@ ${fnBody}
473
475
  this.emit(`appendChild(${elVar}, ${childVar})`);
474
476
  }
475
477
  }
478
+ const deferred = this.deferredCallsStack.pop();
479
+ for (const line of deferred) {
480
+ this.emit(line);
481
+ }
476
482
  return elVar;
477
483
  }
478
484
  // ---- Text & Interpolation -----------------------------------------------
@@ -554,7 +560,7 @@ ${fnBody}
554
560
  }
555
561
  this.emit(` return ${innerVar}`);
556
562
  this.emit(`}`);
557
- this.emit(`createIf(${anchorVar}, () => Boolean(${condition}), ${trueFnVar})`);
563
+ this.emitOrDefer(`createIf(${anchorVar}, () => Boolean(${condition}), ${trueFnVar})`);
558
564
  return anchorVar;
559
565
  }
560
566
  // ---- Structural: u-for --------------------------------------------------
@@ -588,7 +594,7 @@ ${fnBody}
588
594
  }
589
595
  this.emit(` return ${innerVar}`);
590
596
  this.emit(`}`);
591
- this.emit(`createFor(${anchorVar}, () => ${listExpr}, ${renderFnVar})`);
597
+ this.emitOrDefer(`createFor(${anchorVar}, () => ${listExpr}, ${renderFnVar})`);
592
598
  return anchorVar;
593
599
  }
594
600
  // ---- Component generation -----------------------------------------------
@@ -631,6 +637,14 @@ ${fnBody}
631
637
  emit(line) {
632
638
  this.code.push(line);
633
639
  }
640
+ emitOrDefer(line) {
641
+ const stack = this.deferredCallsStack;
642
+ if (stack.length > 0) {
643
+ stack[stack.length - 1].push(line);
644
+ } else {
645
+ this.emit(line);
646
+ }
647
+ }
634
648
  };
635
649
  function isComponentTag(tag) {
636
650
  return /^[A-Z]/.test(tag);
package/dist/index.d.cts CHANGED
@@ -144,7 +144,8 @@ interface CompileResult {
144
144
  *
145
145
  * // <script> block contents (user code) inlined here
146
146
  *
147
- * export default function render(_ctx) { ... }
147
+ * function __render() { ... }
148
+ * export default { render: __render }
148
149
  * ```
149
150
  *
150
151
  * The caller (e.g. the Vite plugin) is responsible for injecting the CSS
package/dist/index.d.ts CHANGED
@@ -144,7 +144,8 @@ interface CompileResult {
144
144
  *
145
145
  * // <script> block contents (user code) inlined here
146
146
  *
147
- * export default function render(_ctx) { ... }
147
+ * function __render() { ... }
148
+ * export default { render: __render }
148
149
  * ```
149
150
  *
150
151
  * The caller (e.g. the Vite plugin) is responsible for injecting the CSS
package/dist/index.js CHANGED
@@ -349,6 +349,7 @@ var CodeGenerator = class {
349
349
  varCounter = 0;
350
350
  helpers = /* @__PURE__ */ new Set();
351
351
  scopeId;
352
+ deferredCallsStack = [];
352
353
  generate(ast) {
353
354
  const scope = /* @__PURE__ */ new Set();
354
355
  const rootElements = ast.filter(
@@ -434,6 +435,7 @@ ${fnBody}
434
435
  if (dir.kind === "if" || dir.kind === "for") continue;
435
436
  this.genDirective(elVar, dir, scope);
436
437
  }
438
+ this.deferredCallsStack.push([]);
437
439
  for (const child of node.children) {
438
440
  const childVar = this.genNode(child, scope);
439
441
  if (childVar) {
@@ -441,6 +443,10 @@ ${fnBody}
441
443
  this.emit(`appendChild(${elVar}, ${childVar})`);
442
444
  }
443
445
  }
446
+ const deferred = this.deferredCallsStack.pop();
447
+ for (const line of deferred) {
448
+ this.emit(line);
449
+ }
444
450
  return elVar;
445
451
  }
446
452
  // ---- Text & Interpolation -----------------------------------------------
@@ -522,7 +528,7 @@ ${fnBody}
522
528
  }
523
529
  this.emit(` return ${innerVar}`);
524
530
  this.emit(`}`);
525
- this.emit(`createIf(${anchorVar}, () => Boolean(${condition}), ${trueFnVar})`);
531
+ this.emitOrDefer(`createIf(${anchorVar}, () => Boolean(${condition}), ${trueFnVar})`);
526
532
  return anchorVar;
527
533
  }
528
534
  // ---- Structural: u-for --------------------------------------------------
@@ -556,7 +562,7 @@ ${fnBody}
556
562
  }
557
563
  this.emit(` return ${innerVar}`);
558
564
  this.emit(`}`);
559
- this.emit(`createFor(${anchorVar}, () => ${listExpr}, ${renderFnVar})`);
565
+ this.emitOrDefer(`createFor(${anchorVar}, () => ${listExpr}, ${renderFnVar})`);
560
566
  return anchorVar;
561
567
  }
562
568
  // ---- Component generation -----------------------------------------------
@@ -599,6 +605,14 @@ ${fnBody}
599
605
  emit(line) {
600
606
  this.code.push(line);
601
607
  }
608
+ emitOrDefer(line) {
609
+ const stack = this.deferredCallsStack;
610
+ if (stack.length > 0) {
611
+ stack[stack.length - 1].push(line);
612
+ } else {
613
+ this.emit(line);
614
+ }
615
+ }
602
616
  };
603
617
  function isComponentTag(tag) {
604
618
  return /^[A-Z]/.test(tag);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthesketh/utopia-compiler",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Compiler for .utopia single-file components",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@matthesketh/utopia-core": "0.0.3"
42
+ "@matthesketh/utopia-core": "0.0.5"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "tsup src/index.ts --format esm,cjs --dts",