brumate 0.4.0 → 0.4.2

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 (62) hide show
  1. package/dist/{boot-DLlRmD9z.d.ts → boot-D3NjfyUX.d.ts} +2 -2
  2. package/dist/cli.js +2 -2
  3. package/dist/{client-BvDsC6Qr.js → client-Cwr2AfP4.js} +133 -35
  4. package/dist/client-Cwr2AfP4.js.map +1 -0
  5. package/dist/{client-DBwL2wKQ.d.ts → client-DNva05jp.d.ts} +20 -7
  6. package/dist/{defer-CKWZLv2K.js → defer-CxjvJXD_.js} +2 -2
  7. package/dist/{defer-CKWZLv2K.js.map → defer-CxjvJXD_.js.map} +1 -1
  8. package/dist/{devtools-4uTPwR_v.js → devtools-BdB_9WB8.js} +4 -4
  9. package/dist/{devtools-4uTPwR_v.js.map → devtools-BdB_9WB8.js.map} +1 -1
  10. package/dist/{devtools-panel-Be9pxYgv.js → devtools-panel-Bp6LE4nM.js} +2 -2
  11. package/dist/{devtools-panel-Be9pxYgv.js.map → devtools-panel-Bp6LE4nM.js.map} +1 -1
  12. package/dist/{files-WW4RlajL.js → files-BzVNgGOL.js} +2 -2
  13. package/dist/{files-WW4RlajL.js.map → files-BzVNgGOL.js.map} +1 -1
  14. package/dist/{html-BtKRyi7g.js → html-t1JpkODI.js} +15 -7
  15. package/dist/html-t1JpkODI.js.map +1 -0
  16. package/dist/index.d.ts +7 -1
  17. package/dist/index.js +1 -1
  18. package/dist/{inspect-Y6y4uNZp.js → inspect-DaZtoKgg.js} +290 -70
  19. package/dist/inspect-DaZtoKgg.js.map +1 -0
  20. package/dist/{loader-BjKGJzLN.js → loader-BX8uIUav.js} +2 -2
  21. package/dist/{loader-BjKGJzLN.js.map → loader-BX8uIUav.js.map} +1 -1
  22. package/dist/produce-CLABZ8k2.d.ts +11 -0
  23. package/dist/{produce-B7X9ori9.js → produce-DS-dQaUX.js} +5 -3
  24. package/dist/produce-DS-dQaUX.js.map +1 -0
  25. package/dist/{rpc-Q-XkLwFh.js → rpc-t5M5dqs9.js} +3 -3
  26. package/dist/{rpc-Q-XkLwFh.js.map → rpc-t5M5dqs9.js.map} +1 -1
  27. package/dist/runtime/client.d.ts +4 -4
  28. package/dist/runtime/client.js +4 -4
  29. package/dist/runtime/devtools.d.ts +2 -2
  30. package/dist/runtime/devtools.js +1 -1
  31. package/dist/runtime/hot.d.ts +1 -1
  32. package/dist/runtime/html.d.ts +2 -2
  33. package/dist/runtime/html.js +1 -1
  34. package/dist/runtime/loader.d.ts +2 -2
  35. package/dist/runtime/loader.js +2 -2
  36. package/dist/runtime/server.d.ts +3 -3
  37. package/dist/runtime/server.js +3 -3
  38. package/dist/runtime/static-loader.d.ts +2 -2
  39. package/dist/runtime/static-loader.js +1 -1
  40. package/dist/runtime/testing.d.ts +2 -2
  41. package/dist/runtime/testing.js +2 -2
  42. package/dist/{server-BvPIg9pT.d.ts → server-BXMH3Z9T.d.ts} +23 -3
  43. package/dist/{server-DjyOVJtJ.js → server-C6c-GXF4.js} +54 -16
  44. package/dist/server-C6c-GXF4.js.map +1 -0
  45. package/dist/shared-CzbnWkVA.d.ts +255 -0
  46. package/dist/{static-loader-BGgdp_Y5.d.ts → static-loader-BiUaZaag.d.ts} +2 -2
  47. package/dist/{static-loader-BZR17hSw.js → static-loader-CqQRu-p9.js} +4 -3
  48. package/dist/{static-loader-BZR17hSw.js.map → static-loader-CqQRu-p9.js.map} +1 -1
  49. package/dist/{stream-BP5AVmg5.js → stream-CqhuQJwK.js} +4 -4
  50. package/dist/stream-CqhuQJwK.js.map +1 -0
  51. package/dist/vite.d.ts +1 -1
  52. package/dist/vite.js +72 -12
  53. package/dist/vite.js.map +1 -1
  54. package/package.json +1 -1
  55. package/dist/client-BvDsC6Qr.js.map +0 -1
  56. package/dist/html-BtKRyi7g.js.map +0 -1
  57. package/dist/html-Bw9Qwu6F.d.ts +0 -107
  58. package/dist/inspect-Y6y4uNZp.js.map +0 -1
  59. package/dist/produce-B7X9ori9.js.map +0 -1
  60. package/dist/produce-DIgHBVwL.d.ts +0 -148
  61. package/dist/server-DjyOVJtJ.js.map +0 -1
  62. package/dist/stream-BP5AVmg5.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { i as paragraphComponents, t as checkNesting } from "./nesting-DA5vJw9X.js";
2
2
  import { t as KEY_NAMES } from "./events-LgLdp1R7.js";
3
- import { p as insertValue } from "./html-BtKRyi7g.js";
3
+ import { p as insertValue } from "./html-t1JpkODI.js";
4
4
  import { parseSync } from "oxc-parser";
5
5
  import { parse } from "postcss";
6
6
  import selectorParser from "postcss-selector-parser";
@@ -1524,6 +1524,14 @@ var Resolver = class {
1524
1524
  if (target?.kind === "server-function") target.refreshed = true;
1525
1525
  }
1526
1526
  if (node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && MUTATORS.has(node.callee.property.name)) this.checkMutation(node.callee.object, node);
1527
+ if (this.untracked === 0) {
1528
+ let root = node.callee;
1529
+ while (root.type === "MemberExpression" || root.type === "TSNonNullExpression" || root.type === "ChainExpression") root = root.type === "MemberExpression" ? root.object : root.expression;
1530
+ if (root.type === "Identifier" && !this.isLocal(root.name)) {
1531
+ const target = lookup(this.site.scope, root.name);
1532
+ if (target?.kind === "context" || target?.kind === "prop" || target?.kind === "props-rest") this.site.opaqueCalls = true;
1533
+ }
1534
+ }
1527
1535
  const name = node.callee.type === "Identifier" ? node.callee.name : void 0;
1528
1536
  if (name && !this.isLocal(name) && !lookup(this.site.scope, name)) {
1529
1537
  if (name === "$untrack") {
@@ -2047,6 +2055,47 @@ function createFail(source, filename) {
2047
2055
  };
2048
2056
  }
2049
2057
  //#endregion
2058
+ //#region src/ast.ts
2059
+ /** The markup inside a node: the children of an element, or the bodies of a block. */
2060
+ function getChildren(node) {
2061
+ switch (node.kind) {
2062
+ case "element":
2063
+ case "component":
2064
+ case "dynamic":
2065
+ case "fragment": return node.children;
2066
+ case "if": {
2067
+ const children = [];
2068
+ for (const branch of node.branches) children.push(...branch.body.children);
2069
+ children.push(...node.fallback?.children ?? []);
2070
+ return children;
2071
+ }
2072
+ case "switch": {
2073
+ const children = [];
2074
+ for (const branch of node.cases) children.push(...branch.body.children);
2075
+ children.push(...node.fallback?.children ?? []);
2076
+ return children;
2077
+ }
2078
+ case "for":
2079
+ case "repeat":
2080
+ case "loading":
2081
+ case "client":
2082
+ case "defer": return [...node.body.children, ...node.fallback?.children ?? []];
2083
+ case "try": return [...node.body.children, ...node.fallback.children];
2084
+ case "key":
2085
+ case "portal":
2086
+ case "head":
2087
+ case "offscreen": return node.body.children;
2088
+ default: return [];
2089
+ }
2090
+ }
2091
+ /** Visits every markup node of a file, including blocks and templates. */
2092
+ function forEachNode(nodes, visit) {
2093
+ for (const node of nodes) {
2094
+ visit(node);
2095
+ forEachNode(getChildren(node), visit);
2096
+ }
2097
+ }
2098
+ //#endregion
2050
2099
  //#region src/graph.ts
2051
2100
  /** Memos that a server function refreshes are live, so they have client code. */
2052
2101
  function isMemoKind(kind) {
@@ -2110,18 +2159,69 @@ const CLIENT_CODE = /* @__PURE__ */ new Set([
2110
2159
  "effect",
2111
2160
  "client"
2112
2161
  ]);
2162
+ /** True if one of the markup nodes holds a component or a dynamic tag, whose code this file cannot see. */
2163
+ function holdsComponent(nodes) {
2164
+ let found = false;
2165
+ forEachNode(nodes, (node) => {
2166
+ found ||= node.kind === "component" || node.kind === "dynamic";
2167
+ });
2168
+ return found;
2169
+ }
2170
+ /** True if `scope` is inside one of `ancestors`. */
2171
+ function isWithinAny$1(scope, ancestors) {
2172
+ for (const ancestor of ancestors) if (isWithin(scope, ancestor)) return true;
2173
+ return false;
2174
+ }
2175
+ /**
2176
+ * The scopes of the templates declared outside the body of a `#try` that are
2177
+ * called in it, in another such template, or in a function that one of them
2178
+ * calls. A template's scope links to where it is called at runtime, so the
2179
+ * `#try` catches the errors of these calls.
2180
+ */
2181
+ function calledTemplates(model, block) {
2182
+ const body = findBodyScope(model, block);
2183
+ if (body === void 0) return [];
2184
+ const inside = [body];
2185
+ const found = [];
2186
+ const called = /* @__PURE__ */ new Set();
2187
+ let added = true;
2188
+ while (added) {
2189
+ added = false;
2190
+ for (const site of model.sites) {
2191
+ if (!isWithinAny$1(site.scope, inside) && (site.declaration === void 0 || !called.has(site.declaration))) continue;
2192
+ for (const declaration of site.calls) if ((declaration.kind === "function" || declaration.kind === "template") && !called.has(declaration)) {
2193
+ called.add(declaration);
2194
+ added = true;
2195
+ }
2196
+ }
2197
+ for (const scope of model.scopes) {
2198
+ if (scope.kind !== "template" || isWithinAny$1(scope, inside)) continue;
2199
+ const declaration = lookup(scope.parent, scope.template.name);
2200
+ if (declaration && called.has(declaration)) {
2201
+ inside.push(scope);
2202
+ found.push(scope);
2203
+ added = true;
2204
+ }
2205
+ }
2206
+ }
2207
+ return found;
2208
+ }
2113
2209
  /**
2114
2210
  * True if the body of a `#try` holds code that runs in the browser: a
2115
2211
  * handler, an effect, a `$client` callback, a memo that loads, or a read of a
2116
2212
  * memo, which throws when the memo holds an error. An error in it renders
2117
- * the `#catch` body in the browser, so both bodies ship.
2213
+ * the `#catch` body in the browser, so both bodies ship. A component in the
2214
+ * body counts too, since its code can be any of these.
2118
2215
  */
2119
2216
  function tryCatchesClient(model, block) {
2120
2217
  const body = findBodyScope(model, block);
2121
2218
  if (body === void 0) return false;
2122
- for (const site of model.sites) if ((CLIENT_CODE.has(site.kind) || site.kind === "memo" && site.declaration?.kind === "async-memo") && isWithin(site.scope, body)) return true;
2219
+ const inside = [body, ...calledTemplates(model, block)];
2220
+ if (holdsComponent(block.body.children)) return true;
2221
+ for (const scope of model.scopes) if (scope.template && isWithinAny$1(scope, inside) && holdsComponent(scope.template.body.children)) return true;
2222
+ for (const site of model.sites) if ((CLIENT_CODE.has(site.kind) || site.kind === "memo" && site.declaration?.kind === "async-memo") && isWithinAny$1(site.scope, inside)) return true;
2123
2223
  for (const declaration of model.declarations) if (isMemoKind(declaration.kind)) {
2124
- for (const read of declaration.reads) if (isWithin(read.site.scope, body)) return true;
2224
+ for (const read of declaration.reads) if (isWithinAny$1(read.site.scope, inside)) return true;
2125
2225
  }
2126
2226
  return false;
2127
2227
  }
@@ -2138,10 +2238,29 @@ function isPropValue(site) {
2138
2238
  function isTemplateValue(site) {
2139
2239
  return site.node.type === "Identifier" && lookup(site.scope, site.node.name)?.kind === "template";
2140
2240
  }
2241
+ function writes(site, seen = /* @__PURE__ */ new Set()) {
2242
+ if (seen.has(site)) return false;
2243
+ seen.add(site);
2244
+ if (site.opaqueCalls) return true;
2245
+ for (const write of site.writes) if (write.tracked) return true;
2246
+ for (const callee of site.trackedCalls) if (callee.site && writes(callee.site, seen)) return true;
2247
+ return false;
2248
+ }
2249
+ /** True for a function prop of a component that only reads values and is not an event. */
2250
+ function isDerivation(site) {
2251
+ const owner = site.owner;
2252
+ if (owner?.kind !== "component" || writes(site)) return false;
2253
+ for (const attribute of owner.attributes) if (attribute.kind === "value" && attribute.value.ast === site.node) return true;
2254
+ return false;
2255
+ }
2141
2256
  function isFunctionValue(site) {
2142
2257
  let node = site.node;
2143
2258
  for (;;) {
2144
2259
  while (node.type === "ParenthesizedExpression") node = node.expression;
2260
+ if (node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "$untrack" && !lookup(site.scope, node.callee.name) && node.arguments[0]) {
2261
+ node = node.arguments[0];
2262
+ continue;
2263
+ }
2145
2264
  if (node.type !== "CallExpression" || node.callee.type !== "Identifier" || lookup(site.scope, node.callee.name)?.imported?.source !== "brumate/events" || !node.arguments[0]) break;
2146
2265
  node = node.arguments[0];
2147
2266
  }
@@ -2224,7 +2343,7 @@ function buildGraph(model, options = {}) {
2224
2343
  return result;
2225
2344
  }
2226
2345
  for (const [site, node] of siteNodes) {
2227
- if (site.kind === "prop" && isFunctionValue(site)) continue;
2346
+ if (site.kind === "prop" && isFunctionValue(site) && !isDerivation(site)) continue;
2228
2347
  for (const declaration of dependencies(site)) addEdge(declarationNodes.get(declaration), node);
2229
2348
  }
2230
2349
  for (const [site, node] of siteNodes) {
@@ -3574,45 +3693,6 @@ function importedComponents(file) {
3574
3693
  for (const program of [file.moduleScript?.program, file.instanceScript?.program]) for (const statement of program?.body ?? []) if (statement.type === "ImportDeclaration" && statement.source.value.endsWith(".brm")) specifiers.push(statement.source.value);
3575
3694
  return specifiers;
3576
3695
  }
3577
- /** The markup inside a node: the children of an element, or the bodies of a block. */
3578
- function getChildren(node) {
3579
- switch (node.kind) {
3580
- case "element":
3581
- case "component":
3582
- case "dynamic":
3583
- case "fragment": return node.children;
3584
- case "if": {
3585
- const children = [];
3586
- for (const branch of node.branches) children.push(...branch.body.children);
3587
- children.push(...node.fallback?.children ?? []);
3588
- return children;
3589
- }
3590
- case "switch": {
3591
- const children = [];
3592
- for (const branch of node.cases) children.push(...branch.body.children);
3593
- children.push(...node.fallback?.children ?? []);
3594
- return children;
3595
- }
3596
- case "for":
3597
- case "repeat":
3598
- case "loading":
3599
- case "client":
3600
- case "defer": return [...node.body.children, ...node.fallback?.children ?? []];
3601
- case "try": return [...node.body.children, ...node.fallback.children];
3602
- case "key":
3603
- case "portal":
3604
- case "head":
3605
- case "offscreen": return node.body.children;
3606
- default: return [];
3607
- }
3608
- }
3609
- /** Visits every markup node of a file, including blocks and templates. */
3610
- function forEachNode(nodes, visit) {
3611
- for (const node of nodes) {
3612
- visit(node);
3613
- forEachNode(getChildren(node), visit);
3614
- }
3615
- }
3616
3696
  /**
3617
3697
  * The live prop that a dynamic tag gives to the component it names. A spread or a computed name
3618
3698
  * can hold any prop, but `children` come from the body of the tag, so they are left out: `~`.
@@ -6205,6 +6285,7 @@ function createPlan(file, graph, path, hideNames = false) {
6205
6285
  if (!anchors.has(holder)) anchor(holderScope, holder);
6206
6286
  }
6207
6287
  }
6288
+ if (block.kind === "try" && tryCatchesClient(model, block)) scopes.get(bodyScopes.get(block.body)).needsId = true;
6208
6289
  if (live && block.kind === "offscreen") scopes.get(bodyScopes.get(block.body)).needsId = true;
6209
6290
  if (block.kind === "portal" || block.kind === "head") scopes.get(bodyScopes.get(block.body)).needsId = true;
6210
6291
  const inner = block.kind === "portal" || block.kind === "head" ? void 0 : outer;
@@ -6583,7 +6664,8 @@ function clonedTemplate(parts) {
6583
6664
  add({
6584
6665
  kind: "attribute",
6585
6666
  name: attribute[1],
6586
- value: attribute[2]
6667
+ value: attribute[2],
6668
+ settled: part.settled
6587
6669
  });
6588
6670
  continue;
6589
6671
  }
@@ -6602,7 +6684,8 @@ function clonedTemplate(parts) {
6602
6684
  add({
6603
6685
  kind: "content",
6604
6686
  value: insert[1],
6605
- at: insert[2]
6687
+ at: insert[2],
6688
+ settled: part.settled
6606
6689
  });
6607
6690
  continue;
6608
6691
  }
@@ -6643,7 +6726,8 @@ function clonedTemplate(parts) {
6643
6726
  const mark = writes.length;
6644
6727
  writes.push([{
6645
6728
  kind: "component",
6646
- call: code.replace(COMPONENT, "$$$$b.componentNodes(")
6729
+ call: code.replace(COMPONENT, "$$$$b.componentNodes("),
6730
+ settled: part.settled
6647
6731
  }]);
6648
6732
  reader.append(`<!--x${mark}-->`);
6649
6733
  continue;
@@ -6653,7 +6737,8 @@ function clonedTemplate(parts) {
6653
6737
  const mark = writes.length;
6654
6738
  writes.push([{
6655
6739
  kind: "component",
6656
- call: nodes[1]
6740
+ call: nodes[1],
6741
+ settled: part.settled
6657
6742
  }]);
6658
6743
  reader.append(`<!--x${mark}-->`);
6659
6744
  continue;
@@ -6783,7 +6868,8 @@ function clonedTemplate(parts) {
6783
6868
  writes.push([{
6784
6869
  kind: "put",
6785
6870
  value: insert[1],
6786
- at: insert[2]
6871
+ at: insert[2],
6872
+ settled: part.settled
6787
6873
  }]);
6788
6874
  reader.append(`<!--x${mark}-->`);
6789
6875
  continue;
@@ -6873,6 +6959,24 @@ function cloneLines(emitter, hoist, fn = 0) {
6873
6959
  const foreign = found.foreign ? `, ${JSON.stringify(found.foreign)}` : "";
6874
6960
  const lines = [`${indent}const $$e = $$b.clone(${hoist(`$$b.template(${JSON.stringify(found.html)}, ${found.writes.length}${foreign})`)}), $$f = $$e[${found.writes.length}];`];
6875
6961
  const fills = [];
6962
+ const settled = /* @__PURE__ */ new Map();
6963
+ function addLine(line, memos) {
6964
+ if (!memos) {
6965
+ lines.push(`${indent}${line}`);
6966
+ return;
6967
+ }
6968
+ const key = `${memos.scope} ${memos.names.join(" ")}`;
6969
+ let group = settled.get(key);
6970
+ if (!group) {
6971
+ group = {
6972
+ scope: memos.scope,
6973
+ names: memos.names,
6974
+ writes: []
6975
+ };
6976
+ settled.set(key, group);
6977
+ }
6978
+ group.writes.push(line);
6979
+ }
6876
6980
  let missing = false;
6877
6981
  for (const [index, writes] of found.writes.entries()) {
6878
6982
  let anchored;
@@ -6887,17 +6991,21 @@ function cloneLines(emitter, hoist, fn = 0) {
6887
6991
  for (const write of ordered) if (write.kind === "anchor") lines.push(`${indent}$$b.keep(${write.scope}, ${write.anchor}, $$e[${index}]${write.named ? ", true" : ""});`);
6888
6992
  else if (write.kind === "last") lines.push(`${indent}$$b.appendText(${write.scope}, ${write.anchor}, $$e[${index}], ${write.value}${write.at ? `, ${write.at}` : ""});`);
6889
6993
  else if (write.kind === "text") lines.push(`${indent}$$b.keepText(${write.scope}, ${write.anchor}, $$e[${index}], ${write.value}${write.at ? `, ${write.at}` : ""});`);
6890
- else if (write.kind === "attribute") lines.push(`${indent}$$b.setAttr($$e[${index}], ${write.name}, ${write.value});`);
6891
- else if (write.kind === "component") lines.push(`${indent}$$b.place($$e[${index}], ${write.call});`);
6994
+ else if (write.kind === "attribute") addLine(`$$b.setAttr($$e[${index}], ${write.name}, ${write.value});`, write.settled);
6995
+ else if (write.kind === "component") addLine(`$$b.place($$e[${index}], ${write.call});`, write.settled);
6892
6996
  else if (write.kind === "comment") lines.push(`${indent}$$b.nameComment($$e[${index}], ${write.data});`);
6893
- else if (write.kind === "put") lines.push(`${indent}$$b.putText($$e[${index}], ${write.value}${write.at ? `, ${write.at}` : ""});`);
6997
+ else if (write.kind === "put") addLine(`$$b.putText($$e[${index}], ${write.value}${write.at ? `, ${write.at}` : ""});`, write.settled);
6894
6998
  else if (write.kind === "call") lines.push(`${indent}${write.call.replace("$$m", () => `$$e[${index}]`)};`);
6895
6999
  else if (write.kind === "marker") lines.push(`${indent}$$b.markBlock($$e[${index}], ${write.scope}, ${write.anchor}, ${JSON.stringify(write.suffix)});`);
6896
7000
  else if (write.kind === "block") {
6897
7001
  const call = emitter.fills.get(`${write.scope}:${write.anchor}`);
6898
7002
  if (call === void 0) missing = true;
6899
7003
  else fills.push(`${indent}${call.replace("$$m", () => `$$e[${index}]`)};`);
6900
- } else lines.push(`${indent}$$b.setContent($$e[${index}], ${write.value}${write.at ? `, ${write.at}` : at});`);
7004
+ } else addLine(`$$b.setContent($$e[${index}], ${write.value}${write.at ? `, ${write.at}` : at});`, write.settled);
7005
+ }
7006
+ for (const group of settled.values()) {
7007
+ const names = group.names.join(", ");
7008
+ lines.push(`${indent}$$b.fillSettled(${group.scope}, [${names}], (${names}) => { ${group.writes.join(" ")} });`);
6901
7009
  }
6902
7010
  if (missing) return false;
6903
7011
  const sizes = /* @__PURE__ */ new Map();
@@ -7255,6 +7363,12 @@ const TYPE_DECLARATIONS = /* @__PURE__ */ new Set([
7255
7363
  "TSDeclareFunction",
7256
7364
  "TSModuleDeclaration"
7257
7365
  ]);
7366
+ /** Expressions whose value is never a promise, as the memo of a list or a function. */
7367
+ const NEVER_PROMISE = /* @__PURE__ */ new Set([
7368
+ "ArrayExpression",
7369
+ "ArrowFunctionExpression",
7370
+ "FunctionExpression"
7371
+ ]);
7258
7372
  /** The static or value attribute named `style`. */
7259
7373
  function findStyleAttribute(attributes) {
7260
7374
  for (const attribute of attributes) if ((attribute.kind === "static" || attribute.kind === "value") && attribute.name === "style") return attribute;
@@ -7273,6 +7387,11 @@ function readsProps(site) {
7273
7387
  for (const read of site?.reads ?? []) if (read.declaration.kind === "prop" || read.declaration.kind === "props-rest") return true;
7274
7388
  return false;
7275
7389
  }
7390
+ /** True if a site calls a template, or a function, which can return the output of one. */
7391
+ function callsTemplate(site) {
7392
+ for (const declaration of site?.calls ?? []) if (declaration.kind === "template" || declaration.kind === "function") return true;
7393
+ return false;
7394
+ }
7276
7395
  /** The name of the compile-time function that a declarator calls, for `$getContext` and `$id`. */
7277
7396
  function specialCall(declarator) {
7278
7397
  const init = declarator.init;
@@ -7409,6 +7528,8 @@ var Emitter = class {
7409
7528
  blocking;
7410
7529
  /** Memos and signals that hold a primitive on the server, so it awaits none of them. */
7411
7530
  inline;
7531
+ /** The sites of the markup that update in the browser. */
7532
+ liveSites = /* @__PURE__ */ new Set();
7412
7533
  parts = [];
7413
7534
  depth = 0;
7414
7535
  /** How many of this component's elements contain the current node. */
@@ -7430,6 +7551,7 @@ var Emitter = class {
7430
7551
  this.blocking = blockingMemos(plan);
7431
7552
  this.inline = options.mode === "server" ? plainValues(plan, this.blocking) : /* @__PURE__ */ new Set();
7432
7553
  for (const site of plan.graph.model.sites) this.sitesByNode.set(site.node, site);
7554
+ for (const node of plan.graph.nodes) if (node.kind !== "source" && plan.graph.live.has(node)) this.liveSites.add(node.site);
7433
7555
  this.addHandlerEdits();
7434
7556
  this.addBindEdits();
7435
7557
  for (const declaration of plan.graph.model.declarations) for (const ref of declaration.refs) this.refTargets.set(ref.attribute, declaration);
@@ -7569,8 +7691,32 @@ var Emitter = class {
7569
7691
  if (last && "static" in last) last.static += text;
7570
7692
  else this.parts.push({ static: text });
7571
7693
  }
7572
- code(code) {
7573
- this.parts.push({ code });
7694
+ code(code, settled) {
7695
+ this.parts.push(settled ? {
7696
+ code,
7697
+ settled
7698
+ } : { code });
7699
+ }
7700
+ /**
7701
+ * In the browser, the memos that values of the markup read that never change and can be a
7702
+ * promise, such as an insert, the headers of a block or the props of a component. No update
7703
+ * writes them when the memos settle, so the clone holds back what reads them.
7704
+ */
7705
+ settledMemos(expressions, scope) {
7706
+ if (this.options.mode !== "client") return;
7707
+ const names = [];
7708
+ for (const expression of expressions) {
7709
+ const site = this.sitesByNode.get(expression.ast);
7710
+ if (!site || this.liveSites.has(site)) return;
7711
+ for (const reference of site.reads) {
7712
+ const declaration = reference.declaration;
7713
+ if ((declaration.kind === "memo" || declaration.kind === "async-memo") && !(declaration.site && isPrimitive(declaration.site.node)) && !NEVER_PROMISE.has(declaration.site?.node.type ?? "") && !names.includes(declaration.name)) names.push(declaration.name);
7714
+ }
7715
+ }
7716
+ return names.length > 0 ? {
7717
+ names,
7718
+ scope: this.scopeVar(instanceScope(this.plan, scope))
7719
+ } : void 0;
7574
7720
  }
7575
7721
  flush() {
7576
7722
  if (this.parts.length === 0) return;
@@ -7631,6 +7777,19 @@ var Emitter = class {
7631
7777
  const parent = instanceScope(this.plan, scope.parent);
7632
7778
  this.line(`const ${this.scopeVar(scope)} = ${this.helper("scope")}(${scope.id}, ${this.scopeVar(parent)});`);
7633
7779
  }
7780
+ /** On the server, records the bound props before the markup, for `refKey`. */
7781
+ emitBound(scope) {
7782
+ if (this.options.mode !== "server") return;
7783
+ const bindable = this.bindableProps(scope);
7784
+ if (bindable.length > 0) this.line(`$$ctx.bound(${this.scopeVar(scope)}, $$props, { ${bindable.join(", ")} });`);
7785
+ }
7786
+ /** The props that the component writes, as `"key": slot` entries. */
7787
+ bindableProps(scope) {
7788
+ const bindable = [];
7789
+ const slots = this.plan.scopes.get(scope)?.slots ?? [];
7790
+ for (const [index, slot] of slots.entries()) if (slot.kind === "prop" && slot.writable && slot.propKey !== void 0) bindable.push(`${JSON.stringify(slot.propKey)}: ${index}`);
7791
+ return bindable;
7792
+ }
7634
7793
  emitSlots(scope) {
7635
7794
  const slots = this.plan.scopes.get(scope)?.slots ?? [];
7636
7795
  if (slots.length > 0) {
@@ -7644,8 +7803,7 @@ var Emitter = class {
7644
7803
  this.line(`${this.helper("slots")}(${this.scopeVar(scope)}, [${values.join(", ")}]${names});`);
7645
7804
  }
7646
7805
  if (scope.kind === "component") {
7647
- const bindable = [];
7648
- for (const [index, slot] of slots.entries()) if (slot.kind === "prop" && slot.writable && slot.propKey !== void 0) bindable.push(`${JSON.stringify(slot.propKey)}: ${index}`);
7806
+ const bindable = this.bindableProps(scope);
7649
7807
  if (bindable.length > 0) this.line(`${this.helper("bindings")}(${this.scopeVar(scope)}, $$props, { ${bindable.join(", ")} });`);
7650
7808
  }
7651
7809
  }
@@ -7755,9 +7913,26 @@ var Emitter = class {
7755
7913
  if (!this.options.functions && this.isFunctionStatement(statement)) continue;
7756
7914
  }
7757
7915
  this.raw(this.patch.slice(statement.start, statement.end), statement.start);
7916
+ if (this.options.mode === "client" && statement.type === "VariableDeclaration") this.watchUnreadMemos(statement, scope);
7758
7917
  }
7759
7918
  endRun();
7760
7919
  }
7920
+ /**
7921
+ * In the browser, a memo that nothing reads has no slot, so no load takes its promise. Its
7922
+ * rejection goes to the nearest `#try`, which is where a memo that throws sends its error. A
7923
+ * memo that always gives a primitive, such as `count * 2`, is never a promise. `await memo`
7924
+ * compiles to a plain read of the promise, which drops it, unless the read is the whole value
7925
+ * of another memo, as in `$memo(await memo)`, which then holds the same promise.
7926
+ */
7927
+ watchUnreadMemos(statement, scope) {
7928
+ for (const declarator of statement.declarations) {
7929
+ const declaration = declarator.id.type === "Identifier" ? scope.declarations.get(declarator.id.name) : void 0;
7930
+ if (declaration && (declaration.kind === "memo" || declaration.kind === "async-memo") && declaration.reads.every((read) => read.await && (read.site.kind !== "memo" || read.site.node.start !== read.await.start || read.site.node.end !== read.await.end)) && slotIndex(this.plan, declaration) === -1 && !(declaration.site && isPrimitive(declaration.site.node))) {
7931
+ const target = this.scopeVar(instanceScope(this.plan, scope));
7932
+ this.line(`$$b.unreadMemo(${target}, ${declaration.name});`);
7933
+ }
7934
+ }
7935
+ }
7761
7936
  source(node) {
7762
7937
  return this.patch.slice(node.start, node.end);
7763
7938
  }
@@ -7827,10 +8002,29 @@ var Emitter = class {
7827
8002
  this.html(" b-near=\"");
7828
8003
  this.code(anchor.scope);
7829
8004
  this.html(`:${near}"`);
8005
+ } else if (site.trigger === "visible" && this.options.mode === "server") {
8006
+ const forwarded = this.forwardedSignal(site, scope);
8007
+ if (forwarded) this.code(`$$ctx.near(${anchor.scope}, ${slotIndex(this.plan, forwarded)})`);
7830
8008
  }
7831
8009
  this.html("></template>");
7832
8010
  }
7833
8011
  }
8012
+ /** The code of the `b-r` attribute of an element that has `ref=$bind(name)` with a prop. */
8013
+ forwardedRef(node) {
8014
+ if (this.options.mode !== "server") return;
8015
+ for (const attribute of node.attributes) {
8016
+ const declaration = attribute.kind === "ref" ? this.refTargets.get(attribute) : void 0;
8017
+ if (declaration?.kind === "prop") return `$$ctx.refKey(${this.scopeVar(instanceScope(this.plan, declaration.scope))}, ${slotIndex(this.plan, declaration)})`;
8018
+ }
8019
+ }
8020
+ /** The signal of `scope` that a site reads, which a component may forward its element to. */
8021
+ forwardedSignal(site, scope) {
8022
+ for (const read of site.reads) {
8023
+ const declaration = read.declaration;
8024
+ if (declaration.scope !== scope || declaration.kind !== "signal") continue;
8025
+ for (const binding of this.plan.graph.model.bindings) if (binding.declaration === declaration && !binding.setter) return declaration;
8026
+ }
8027
+ }
7834
8028
  /** The anchor of the first element of `scope` that a site reads through `ref`. */
7835
8029
  refElement(site, scope) {
7836
8030
  for (const read of site.reads) for (const ref of read.declaration.refs) {
@@ -7846,6 +8040,7 @@ var Emitter = class {
7846
8040
  else this.code(`${this.helper("style")}(${JSON.stringify(this.plan.id)}, $$css)`);
7847
8041
  }
7848
8042
  this.emitStatements(setup, component);
8043
+ this.emitBound(component);
7849
8044
  for (const template of this.file.templates) this.emitTemplate(template);
7850
8045
  this.emitClientAnchors(component);
7851
8046
  this.emitNodes(this.file.children, component);
@@ -7916,7 +8111,9 @@ var Emitter = class {
7916
8111
  this.line(`let $$html = '';`);
7917
8112
  this.atTopLevel(() => this.emitBody(template.body));
7918
8113
  this.flush();
7919
- this.line("return $$b.raw($$html);");
8114
+ const scope = this.plan.bodyScopes.get(template.body);
8115
+ const at = this.plan.scopes.get(scope).needsId ? `, ${this.scopeVar(scope)}` : "";
8116
+ this.line(`return $$b.raw($$html${at});`);
7920
8117
  this.close();
7921
8118
  }
7922
8119
  emitNodes(nodes, scope) {
@@ -7937,15 +8134,17 @@ var Emitter = class {
7937
8134
  return;
7938
8135
  case "insert": {
7939
8136
  const anchor = this.anchor(scope, node);
7940
- const readsProp = readsProps(this.sitesByNode.get(node.expression.ast));
8137
+ const site = this.sitesByNode.get(node.expression.ast);
8138
+ const readsProp = readsProps(site);
7941
8139
  const spacer = this.parentSpacer();
7942
8140
  const server = this.options.mode === "server" && readsProp;
7943
8141
  let rest = server && spacer !== void 0 ? `, ${JSON.stringify(spacer)}` : "";
7944
8142
  if (server && anchor) rest = `${rest || ", undefined"}, ${anchor.index}`;
7945
- const at = this.options.mode === "server" || readsProp ? `, ${this.ctxArgument}, ${this.scopeVar(instanceScope(this.plan, scope))}${rest}` : "";
8143
+ const at = this.options.mode === "server" || readsProp || callsTemplate(site) ? `, ${this.ctxArgument}, ${this.scopeVar(instanceScope(this.plan, scope))}${rest}` : "";
7946
8144
  const value = this.isPrimitiveInsert(node.expression) ? `$$b.insert(${this.expression(node.expression)})` : this.ready(`$$b.insert(${this.expression(node.expression)}${at})`);
7947
8145
  const text = literalText(node.expression.ast);
7948
- const emitValue = () => text === void 0 ? this.code(value) : this.html(text);
8146
+ const settled = this.settledMemos([node.expression], scope);
8147
+ const emitValue = () => text === void 0 ? this.code(value, settled) : this.html(text);
7949
8148
  if (anchor && !this.plan.contents.has(node)) {
7950
8149
  this.html("<!--t");
7951
8150
  this.code(anchor.scope);
@@ -8160,7 +8359,7 @@ var Emitter = class {
8160
8359
  this.emitClasses(classes);
8161
8360
  break;
8162
8361
  }
8163
- this.code(`$$b.attribute(${JSON.stringify(attribute.name)}, ${this.expression(attribute.value)})`);
8362
+ this.code(`$$b.attribute(${JSON.stringify(attribute.name)}, ${this.expression(attribute.value)})`, this.settledMemos([attribute.value], scope));
8164
8363
  const bound = this.bindEntry(node, attribute, scope);
8165
8364
  if (bound) events.unshift(bound);
8166
8365
  break;
@@ -8187,6 +8386,8 @@ var Emitter = class {
8187
8386
  this.code(anchor.scope);
8188
8387
  this.html(`:${anchor.index}"`);
8189
8388
  }
8389
+ const forwarded = this.forwardedRef(node);
8390
+ if (forwarded) this.code(forwarded);
8190
8391
  if (pageTitle) this.html(" b-t");
8191
8392
  if (spreadHandlers) this.code(`$$b.onAttribute(${spreadHandlers}, ${JSON.stringify(events.join(" "))})`);
8192
8393
  else if (events.length > 0) this.html(` b-on="${events.join(" ")}"`);
@@ -8424,7 +8625,17 @@ var Emitter = class {
8424
8625
  const at = `${this.file.filename}:${before.length}:${before.at(-1).length + 1}`;
8425
8626
  link = `${link || ", undefined"}, ${JSON.stringify(`<${node.name}> in ${at}`)}`;
8426
8627
  }
8427
- this.code(`${this.wait}$$b.component(${node.name}, ${props}, ${this.ctxArgument}, ${owner}${link})`);
8628
+ const values = [];
8629
+ for (const attribute of node.attributes) {
8630
+ const type = attribute.kind === "value" ? attribute.value.ast.type : "";
8631
+ if (type === "ArrowFunctionExpression" || type === "FunctionExpression") continue;
8632
+ if (attribute.kind === "value" || attribute.kind === "spread") values.push(attribute.value);
8633
+ else if (attribute.kind === "computed") {
8634
+ values.push(attribute.name);
8635
+ if (attribute.value !== true) values.push(attribute.value);
8636
+ }
8637
+ }
8638
+ this.code(`${this.wait}$$b.component(${node.name}, ${props}, ${this.ctxArgument}, ${owner}${link})`, this.settledMemos(values, scope));
8428
8639
  }
8429
8640
  emitDynamic(node, scope) {
8430
8641
  const childrenName = this.emitChildren(node, scope);
@@ -8678,7 +8889,9 @@ var Emitter = class {
8678
8889
  let index = other ? String(branches.length) : "-1";
8679
8890
  for (let at = branches.length - 1; at >= 0; at--) index = `${this.expression(branches[at].condition)} ? ${at} : ${index}`;
8680
8891
  const names = `[${[...bodies, ...other ? [other] : []].join(", ")}]`;
8681
- this.code(`$$b.nodes($$b.branchNodes(${marks}, ${index}, ${names}))`);
8892
+ const conditions = [];
8893
+ for (const branch of branches) conditions.push(branch.condition);
8894
+ this.code(`$$b.nodes($$b.branchNodes(${marks}, ${index}, ${names}))`, this.settledMemos(conditions, scope));
8682
8895
  return true;
8683
8896
  }
8684
8897
  if (block.kind === "for" || block.kind === "repeat") {
@@ -8688,7 +8901,7 @@ var Emitter = class {
8688
8901
  const fallback = block.fallback;
8689
8902
  const other = fallback && this.bodyFunction("", () => this.emitBody(fallback));
8690
8903
  const items = block.kind === "for" ? `$$b.toList(${this.expression(block.list)})` : `Array.from({ length: ${this.expression(block.count)} }, (_, index) => index)`;
8691
- this.code(`$$b.nodes($$b.listNodes(${marks}, ${items}, ${body}, ${other ?? "undefined"}))`);
8904
+ this.code(`$$b.nodes($$b.listNodes(${marks}, ${items}, ${body}, ${other ?? "undefined"}))`, this.settledMemos([block.kind === "for" ? block.list : block.count], scope));
8692
8905
  return true;
8693
8906
  }
8694
8907
  return false;
@@ -9927,6 +10140,7 @@ var ClientGenerator = class {
9927
10140
  }
9928
10141
  const updates = [];
9929
10142
  for (const site of this.plan.clientSites) if (!this.plan.symbols.has(site) && site.kind !== "function" && site.kind !== "server-function") updates.push(site);
10143
+ for (const node of this.plan.graph.nodes) if (node.kind === "binding" && node.site.kind === "prop" && this.plan.symbols.has(node.site) && this.plan.graph.live.has(node)) updates.push(node.site);
9930
10144
  for (const scope of this.renderedScopes()) for (const site of this.model.sites) if (isWithin(site.scope, scope) && !DEFERRED_SITES.has(site.kind)) updates.push(site);
9931
10145
  for (const site of this.childrenSites()) updates.push(site);
9932
10146
  this.reach.set("update", this.closure(updates));
@@ -10876,15 +11090,21 @@ var ClientGenerator = class {
10876
11090
  * body, and the kinds of both bodies, and the holder of a block in children.
10877
11091
  */
10878
11092
  triesTable() {
10879
- const entries = [];
11093
+ const bodies = /* @__PURE__ */ new Map();
10880
11094
  for (const scope of this.model.scopes) {
10881
11095
  const block = scope.block;
10882
11096
  if (block?.kind !== "try" || scope.body !== block.body || !this.plan.anchors.has(block) || !tryCatchesClient(this.model, block)) continue;
10883
11097
  const owner = instanceScope(this.plan, scope.parent);
10884
11098
  const fallback = this.plan.bodyScopes.get(block.fallback);
10885
11099
  const held = holderArguments(this.plan, block);
10886
- const entry = `[${owner.id}, ${this.plan.anchors.get(block)}, ${JSON.stringify(`r${scope.id}`)}, ${JSON.stringify(`r${fallback.id}`)}, [${this.kinds(blockBodies(block)).join(", ")}]${held}]`;
10887
- for (const inside of this.model.scopes) if (isWithin(inside, scope)) entries.push(`${inside.id}: ${entry}`);
11100
+ bodies.set(scope, `[${owner.id}, ${this.plan.anchors.get(block)}, ${JSON.stringify(`r${scope.id}`)}, ${JSON.stringify(`r${fallback.id}`)}, [${this.kinds(blockBodies(block)).join(", ")}]${held}]`);
11101
+ }
11102
+ const entries = [];
11103
+ for (const inside of this.model.scopes) {
11104
+ let current = inside;
11105
+ while (current && !bodies.has(current) && current.kind !== "template") current = current.parent;
11106
+ const entry = current && bodies.get(current);
11107
+ if (entry) entries.push(`${inside.id}: ${entry}`);
10888
11108
  }
10889
11109
  return entries.length > 0 ? `{ ${entries.join(", ")} }` : void 0;
10890
11110
  }
@@ -11410,6 +11630,6 @@ function inspect(source, options = {}) {
11410
11630
  };
11411
11631
  }
11412
11632
  //#endregion
11413
- export { analyzeProject as A, getLocation as B, isReactiveModule as C, unmarked as D, originalPosition as E, resolveRelative as F, isWithin as G, findError as H, summarize as I, walk as J, lookup as K, parse$1 as L, forEachNode as M, importSource as N, entryClasses as O, importedComponents as P, buildGraph as R, sharedSites as S, builtin as T, analyze$1 as U, errorLink as V, getPatternNames as W, functionId as _, scopeLabel as a, SHARED_QUERY as b, DEFAULT_MERGE as c, generateServer as d, instanceScope as f, createPlan as g, clientState as h, report as i, analyzeProjectProps as j, refreshedNames as k, generateClient as l, blockBodies as m, formatReport as n, CompileError as o, slotIndex as p, resolveMacros as q, inspect as r, compile as s, anchorLabel as t, generateRender as u, getComponentName as v, resolveFromDisk as w, generateShared as x, hash as y, BrumateError as z };
11633
+ export { analyzeProject as A, getLocation as B, isReactiveModule as C, unmarked as D, originalPosition as E, summarize as F, isWithin as G, findError as H, parse$1 as I, walk as J, lookup as K, buildGraph as L, importSource as M, importedComponents as N, entryClasses as O, resolveRelative as P, forEachNode as R, sharedSites as S, builtin as T, analyze$1 as U, errorLink as V, getPatternNames as W, functionId as _, scopeLabel as a, SHARED_QUERY as b, DEFAULT_MERGE as c, generateServer as d, instanceScope as f, createPlan as g, clientState as h, report as i, analyzeProjectProps as j, refreshedNames as k, generateClient as l, blockBodies as m, formatReport as n, CompileError as o, slotIndex as p, resolveMacros as q, inspect as r, compile as s, anchorLabel as t, generateRender as u, getComponentName as v, resolveFromDisk as w, generateShared as x, hash as y, BrumateError as z };
11414
11634
 
11415
- //# sourceMappingURL=inspect-Y6y4uNZp.js.map
11635
+ //# sourceMappingURL=inspect-DaZtoKgg.js.map