solid-optimizer 0.3.0 → 1.0.0-next.0

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.
@@ -1,6 +1,6 @@
1
1
  import remapping from "@jridgewell/remapping";
2
2
  import MagicString from "magic-string";
3
- import { parseSync, visitorKeys } from "oxc-parser";
3
+ import { parseSync, rawTransferSupported, visitorKeys } from "oxc-parser";
4
4
  //#region src/ast.ts
5
5
  /**
6
6
  * Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
@@ -60,12 +60,19 @@ function collectParents(program) {
60
60
  visit(program);
61
61
  return parents;
62
62
  }
63
+ /**
64
+ * Raw transfer hands the AST over in shared memory instead of as JSON, which
65
+ * is several times faster to read. It is not in the options' type yet.
66
+ */
67
+ const RAW_TRANSFER = rawTransferSupported();
63
68
  function parse(filename, code) {
64
- const result = parseSync(filename, code, {
69
+ const options = {
65
70
  lang: languageOf(filename),
66
71
  sourceType: "module",
67
- preserveParens: true
68
- });
72
+ preserveParens: true,
73
+ experimentalRawTransfer: RAW_TRANSFER
74
+ };
75
+ const result = parseSync(filename, code, options);
69
76
  const error = result.errors.at(0);
70
77
  if (error) throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
71
78
  return result.program;
@@ -202,6 +209,16 @@ function solidExport(binding, options) {
202
209
  if (binding.kind === "import" && binding.source !== void 0 && options.moduleSources.includes(binding.source)) return binding.imported;
203
210
  }
204
211
  /**
212
+ * Records that a pass left something alone, when the caller asked for a report.
213
+ */
214
+ function recordSkip(context, pass, reason, name) {
215
+ context.skips?.push({
216
+ pass,
217
+ reason,
218
+ name
219
+ });
220
+ }
221
+ /**
205
222
  * The current text of `node`, including the edits made inside it so far.
206
223
  */
207
224
  function textOf(context, node) {
@@ -418,6 +435,7 @@ var Analyzer = class {
418
435
  this.reference(node, false);
419
436
  break;
420
437
  case "VariableDeclaration": {
438
+ if (node.declare) break;
421
439
  let kind = "other";
422
440
  if (node.kind === "var" || node.kind === "let" || node.kind === "const") kind = node.kind;
423
441
  for (const declarator of node.declarations) {
@@ -427,10 +445,12 @@ var Analyzer = class {
427
445
  break;
428
446
  }
429
447
  case "FunctionDeclaration":
430
- case "TSDeclareFunction":
431
448
  if (node.id) this.declare(node.id, "function", node);
432
449
  this.visitFunction(node);
433
450
  break;
451
+ case "TSDeclareFunction":
452
+ this.visitFunction(node);
453
+ break;
434
454
  case "FunctionExpression":
435
455
  this.visitFunction(node);
436
456
  break;
@@ -444,6 +464,7 @@ var Analyzer = class {
444
464
  });
445
465
  break;
446
466
  case "ClassDeclaration":
467
+ if (node.declare) break;
447
468
  if (node.id) this.declare(node.id, "class", node);
448
469
  this.visitClass(node);
449
470
  break;
@@ -805,12 +826,36 @@ var ConstantTable = class {
805
826
  this.constants = /* @__PURE__ */ new Map();
806
827
  this.evaluating = /* @__PURE__ */ new Set();
807
828
  this.lookup = (reference) => {
808
- if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference);
829
+ if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference) ?? this.globalMember(scopes, reference);
809
830
  if (!scopes.references.has(reference) || reference.type !== "Identifier") return;
810
831
  const binding = scopes.references.get(reference);
811
- return binding ? this.constantOf(binding) : globalValue(reference.name);
832
+ if (binding) return this.constantOf(binding);
833
+ return this.globalConstant(reference.name) ?? globalValue(reference.name);
812
834
  };
813
835
  }
836
+ globalConstant(path) {
837
+ const { globalConstants } = this.options;
838
+ return globalConstants.has(path) ? { value: globalConstants.get(path) } : void 0;
839
+ }
840
+ /**
841
+ * The value of a path like `import.meta.env.MODE` or `process.env.X` that
842
+ * `globalConstants` holds. The path starts at `import.meta` or at a name
843
+ * nothing declares, and goes through plain property reads.
844
+ */
845
+ globalMember(scopes, node) {
846
+ if (this.options.globalConstants.size === 0) return;
847
+ const names = [];
848
+ let current = node;
849
+ while (current.type === "MemberExpression") {
850
+ if (current.computed || current.optional || current.property.type !== "Identifier") return;
851
+ names.unshift(current.property.name);
852
+ current = current.object;
853
+ }
854
+ if (current.type === "MetaProperty") names.unshift(`${current.meta.name}.${current.property.name}`);
855
+ else if (current.type === "Identifier" && scopes.references.has(current) && scopes.references.get(current) === void 0) names.unshift(current.name);
856
+ else return;
857
+ return this.globalConstant(names.join("."));
858
+ }
814
859
  /**
815
860
  * The value of `ns.NAME`, where `ns` is `import * as ns` from a module
816
861
  * whose constants are known.
@@ -1026,6 +1071,189 @@ function innerText(s, node) {
1026
1071
  return s.slice(node.openingElement.end, node.closingElement.start);
1027
1072
  }
1028
1073
  //#endregion
1074
+ //#region src/size.ts
1075
+ /** A template's declaration, and the function that clones and returns it. */
1076
+ const TEMPLATE = 40;
1077
+ /** A walk to an element, like `e.firstChild.nextSibling`. */
1078
+ const WALK = 12;
1079
+ /** An `insert()` of a dynamic child. */
1080
+ const INSERT = 12;
1081
+ /** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
1082
+ const EFFECT = 24;
1083
+ /** An event handler assignment, like `e.$$click = ...`. */
1084
+ const HANDLER = 10;
1085
+ /** A `spread()` of props onto an element. */
1086
+ const SPREAD = 16;
1087
+ /** A `createComponent(Name, {})` call. */
1088
+ const COMPONENT = 8;
1089
+ /** A getter, `get name() { return ... }`. */
1090
+ const GETTER = 16;
1091
+ /** A `mergeProps()` around the props of a component with a spread. */
1092
+ const MERGE = 6;
1093
+ /**
1094
+ * The length of `text` once a minifier removes the whitespace it does not need.
1095
+ */
1096
+ function compactLength(text) {
1097
+ return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
1098
+ }
1099
+ function nameLength(name) {
1100
+ switch (name.type) {
1101
+ case "JSXIdentifier": return name.name.length;
1102
+ case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
1103
+ case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
1104
+ default: return 4;
1105
+ }
1106
+ }
1107
+ /** Whether an expression lowers to text in the template, like a literal. */
1108
+ function isStaticText(node) {
1109
+ if (node.type === "JSXEmptyExpression") return false;
1110
+ const inner = unwrap(node);
1111
+ if (inner.type === "Literal") return typeof inner.value === "string" || typeof inner.value === "number";
1112
+ if (inner.type === "TemplateLiteral") return inner.expressions.length === 0;
1113
+ return inner.type === "UnaryExpression" && inner.operator === "-" && inner.argument.type === "Literal" && typeof inner.argument.value === "number";
1114
+ }
1115
+ function isIntrinsic(element) {
1116
+ const { name } = element.openingElement;
1117
+ return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
1118
+ }
1119
+ function attributeName(attribute) {
1120
+ const { name } = attribute;
1121
+ return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
1122
+ }
1123
+ /**
1124
+ * Estimates lowered sizes in one piece of code.
1125
+ */
1126
+ var SizeEstimator = class {
1127
+ constructor(code) {
1128
+ this.code = code;
1129
+ }
1130
+ text(node) {
1131
+ return compactLength(this.code.slice(node.start, node.end));
1132
+ }
1133
+ /**
1134
+ * The size of any node: JSX as lowered, other code as its compact text
1135
+ * with the JSX in it as lowered.
1136
+ */
1137
+ node(node) {
1138
+ if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
1139
+ let size = this.text(node);
1140
+ const visit = (child) => {
1141
+ if (child.type === "JSXElement" || child.type === "JSXFragment") {
1142
+ size += this.jsx(child, false) - this.text(child);
1143
+ return;
1144
+ }
1145
+ forEachChild(child, visit);
1146
+ };
1147
+ forEachChild(node, visit);
1148
+ return size;
1149
+ }
1150
+ /**
1151
+ * The size of a JSX element or fragment. `inTemplate` is whether it sits
1152
+ * directly in an element, whose template it joins.
1153
+ */
1154
+ jsx(node, inTemplate) {
1155
+ if (node.type === "JSXFragment") return this.children(node.children) + 2;
1156
+ if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
1157
+ const tag = nameLength(node.openingElement.name);
1158
+ let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
1159
+ let dynamic = false;
1160
+ for (const attribute of node.openingElement.attributes) {
1161
+ const cost = this.elementAttribute(attribute);
1162
+ size += cost.size;
1163
+ dynamic ||= cost.dynamic;
1164
+ }
1165
+ for (const child of node.children) {
1166
+ if (isInsignificant(child)) continue;
1167
+ if (child.type === "JSXText") size += compactLength(child.value);
1168
+ else if (child.type === "JSXExpressionContainer" && isStaticText(child.expression)) size += this.text(child.expression);
1169
+ else if (child.type === "JSXElement") size += this.jsx(child, true);
1170
+ else {
1171
+ size += INSERT + this.child(child);
1172
+ dynamic = true;
1173
+ }
1174
+ }
1175
+ return size + (dynamic ? WALK : 0);
1176
+ }
1177
+ elementAttribute(attribute) {
1178
+ if (attribute.type === "JSXSpreadAttribute") return {
1179
+ size: SPREAD + this.node(attribute.argument),
1180
+ dynamic: true
1181
+ };
1182
+ const name = attributeName(attribute);
1183
+ const { value } = attribute;
1184
+ if (!value || value.type === "Literal") return {
1185
+ size: name.length + (value ? this.text(value) : 0) + 1,
1186
+ dynamic: false
1187
+ };
1188
+ if (value.type !== "JSXExpressionContainer") return {
1189
+ size: name.length + 1 + this.jsx(value, false),
1190
+ dynamic: true
1191
+ };
1192
+ const { expression } = value;
1193
+ if (expression.type === "JSXEmptyExpression") return {
1194
+ size: 0,
1195
+ dynamic: false
1196
+ };
1197
+ const inner = unwrap(expression);
1198
+ if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
1199
+ size: name.length + this.text(inner) + 1,
1200
+ dynamic: false
1201
+ };
1202
+ return {
1203
+ size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
1204
+ dynamic: true
1205
+ };
1206
+ }
1207
+ child(child) {
1208
+ if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
1209
+ if (child.type === "JSXSpreadChild") return this.node(child.expression);
1210
+ if (child.type === "JSXText") return compactLength(child.value) + 2;
1211
+ return this.jsx(child, false);
1212
+ }
1213
+ /**
1214
+ * Children outside a template, which lower to an array of values.
1215
+ */
1216
+ children(children) {
1217
+ let size = 0;
1218
+ for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
1219
+ return size;
1220
+ }
1221
+ component(element) {
1222
+ let size = COMPONENT + nameLength(element.openingElement.name);
1223
+ let spread = false;
1224
+ for (const attribute of element.openingElement.attributes) {
1225
+ if (attribute.type === "JSXSpreadAttribute") {
1226
+ spread = true;
1227
+ size += this.node(attribute.argument) + 1;
1228
+ continue;
1229
+ }
1230
+ const name = attributeName(attribute);
1231
+ const { value } = attribute;
1232
+ if (!value || value.type === "Literal") size += name.length + 1 + (value ? this.text(value) : 4);
1233
+ else if (value.type === "JSXExpressionContainer") {
1234
+ const { expression } = value;
1235
+ if (expression.type !== "JSXEmptyExpression") {
1236
+ const inner = unwrap(expression);
1237
+ const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
1238
+ size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
1239
+ }
1240
+ } else size += name.length + GETTER + this.jsx(value, false);
1241
+ }
1242
+ const significant = element.children.filter((child) => !isInsignificant(child));
1243
+ if (significant.length > 0) size += 24 + this.children(significant);
1244
+ return size + (spread ? MERGE : 0);
1245
+ }
1246
+ /**
1247
+ * The size of what a component returns. JSX lowers as JSX, and anything
1248
+ * else is inserted when it sits in an element.
1249
+ */
1250
+ root(node, inTemplate) {
1251
+ const inner = unwrap(node);
1252
+ if (inner.type === "JSXElement" || inner.type === "JSXFragment") return this.jsx(inner, inTemplate);
1253
+ return (inTemplate ? 24 : 0) + this.node(inner);
1254
+ }
1255
+ };
1256
+ //#endregion
1029
1257
  //#region src/fold.ts
1030
1258
  /**
1031
1259
  * The control-flow components this pass resolves.
@@ -1034,7 +1262,7 @@ function innerText(s, node) {
1034
1262
  const FOLDABLE_FLOW = /* @__PURE__ */ new Set([
1035
1263
  "Show",
1036
1264
  "For",
1037
- "Index",
1265
+ "Repeat",
1038
1266
  "Switch",
1039
1267
  "Dynamic"
1040
1268
  ]);
@@ -1095,9 +1323,51 @@ function containsHoistedDeclaration(statement) {
1095
1323
  * Whether a statement starting with `text` could join the statement before
1096
1324
  * it when that one has no semicolon.
1097
1325
  */
1098
- function continuesPreviousStatement$1(text) {
1326
+ function continuesPreviousStatement(text) {
1099
1327
  return /^[([`+\-/]/.test(text);
1100
1328
  }
1329
+ /** The name a property key or a computed member's string literal gives. */
1330
+ function keyName(node) {
1331
+ if (node.type === "Identifier") return node.name;
1332
+ return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
1333
+ }
1334
+ /**
1335
+ * The text of an expression that always gives the same primitive: a literal
1336
+ * other than a regular expression, a template literal without expressions,
1337
+ * or a negated number.
1338
+ */
1339
+ function primitiveText(context, node) {
1340
+ let expression = node;
1341
+ while (expression.type === "ParenthesizedExpression") expression = expression.expression;
1342
+ switch (expression.type) {
1343
+ case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
1344
+ case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
1345
+ case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
1346
+ default: return;
1347
+ }
1348
+ }
1349
+ /** Whether a child of `parent` is inserted into its template, not passed as a prop. */
1350
+ function joinsTemplate(parent) {
1351
+ if (parent.type === "JSXFragment") return true;
1352
+ const { name } = parent.openingElement;
1353
+ return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
1354
+ }
1355
+ /**
1356
+ * Whether JSX holds a `ref` or a spread, which run while the JSX is made.
1357
+ */
1358
+ function hasRefOrSpread(root) {
1359
+ let found = false;
1360
+ const visit = (node) => {
1361
+ if (found) return;
1362
+ if (node.type === "JSXSpreadAttribute" || node.type === "JSXSpreadChild" || node.type === "JSXAttribute" && node.name.type === "JSXIdentifier" && node.name.name === "ref") {
1363
+ found = true;
1364
+ return;
1365
+ }
1366
+ forEachChild(node, visit);
1367
+ };
1368
+ visit(root);
1369
+ return found;
1370
+ }
1101
1371
  var Folder = class {
1102
1372
  constructor(context) {
1103
1373
  this.context = context;
@@ -1256,6 +1526,26 @@ var Folder = class {
1256
1526
  if (identity === void 0 || !this.context.options.builtIns.has(identity)) return;
1257
1527
  return identity;
1258
1528
  }
1529
+ /**
1530
+ * The name of the component an identifier refers to, when it is a
1531
+ * capitalized top-level binding nothing reassigns, holding a function or a
1532
+ * `lazy` component. A JSX tag with that name calls the same component. An
1533
+ * import could hold anything, such as a tag name string, so it is left out.
1534
+ */
1535
+ componentName(identifier) {
1536
+ const binding = this.context.scopes.references.get(identifier);
1537
+ if (!binding || binding.mutated || binding.scope.parent !== void 0 || !/^[A-Z]/.test(binding.name)) return;
1538
+ const { declaration } = binding;
1539
+ if (declaration.type === "FunctionDeclaration") return declaration.async || declaration.generator ? void 0 : binding.name;
1540
+ if (declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
1541
+ let init = declaration.init;
1542
+ while (init.type === "ParenthesizedExpression") init = init.expression;
1543
+ if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") return init.async || init.generator ? void 0 : binding.name;
1544
+ if (init.type === "CallExpression" && init.callee.type === "Identifier") {
1545
+ const callee = this.context.scopes.references.get(init.callee);
1546
+ return callee && solidExport(callee, this.context.options) === "lazy" ? binding.name : void 0;
1547
+ }
1548
+ }
1259
1549
  flowTag(element) {
1260
1550
  const tag = this.builtInTag(element);
1261
1551
  return tag !== void 0 && FOLDABLE_FLOW.has(tag) ? tag : void 0;
@@ -1265,8 +1555,8 @@ var Folder = class {
1265
1555
  if (tag === void 0 || hasSpreadAttribute(element)) return;
1266
1556
  switch (tag) {
1267
1557
  case "Show": return this.foldShow(element);
1268
- case "For":
1269
- case "Index": return this.foldFor(element);
1558
+ case "For": return this.foldFor(element);
1559
+ case "Repeat": return this.foldRepeat(element);
1270
1560
  case "Switch": return this.foldSwitch(element);
1271
1561
  case "Dynamic": return this.foldDynamic(element);
1272
1562
  default: return;
@@ -1337,56 +1627,246 @@ var Folder = class {
1337
1627
  const each = findAttribute(element, "each");
1338
1628
  if (each?.value?.type !== "JSXExpressionContainer" || each.value.expression.type === "JSXEmptyExpression") return;
1339
1629
  const { expression } = each.value;
1340
- if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false) || !isSideEffectFree(expression)) return;
1630
+ if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false)) return this.unrollFor(element, expression);
1631
+ if (!isSideEffectFree(expression)) return;
1341
1632
  return this.fallback(element);
1342
1633
  }
1343
1634
  /**
1344
- * Resolves a `<Switch>` as far as its `<Match when>` conditions allow.
1345
- * Statically false matches are dropped, a statically true match with no
1346
- * undecided match before it wins, and a switch whose every match is false
1347
- * renders its fallback.
1635
+ * Unrolls `<For>` over a constant list into one copy of its row per item.
1636
+ *
1637
+ * The list is an array literal, or a binding of one that only `<For>`
1638
+ * reads, so nothing can change it. Each item is a primitive literal, or an
1639
+ * object literal of them. The row is an arrow function whose body is JSX,
1640
+ * and reads the item as `item.key`, or as itself when it is a primitive.
1641
+ * Each row ran once, untracked, under its own owner. A copy is made with
1642
+ * the JSX around it instead, which runs untracked too, and the JSX
1643
+ * transform gives every dynamic part its own effect. A `ref` or a spread
1644
+ * would run while the copy is made, so either one keeps the loop.
1348
1645
  */
1349
- foldSwitch(element) {
1350
- const matches = [];
1351
- for (const child of element.children) {
1352
- if (isInsignificant(child)) continue;
1353
- if (child.type !== "JSXElement" || this.builtInTag(child) !== "Match" || hasSpreadAttribute(child) || !findAttribute(child, "when")) return;
1354
- matches.push({
1355
- element: child,
1356
- when: this.attributeTruthiness(child, "when")
1357
- });
1358
- }
1359
- if (matches.length === 0) return;
1360
- const winner = matches.findIndex((item) => item.when === true);
1361
- if (winner !== -1 && matches.slice(0, winner).every((item) => item.when === false)) {
1362
- const match = matches.at(winner);
1363
- if (!match || hasCallbackChild(match.element.children)) return;
1364
- return {
1365
- kind: "children",
1366
- children: match.element.children
1367
- };
1646
+ unrollFor(element, expression) {
1647
+ if (element.openingElement.attributes.some((attribute) => attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "keyed" && !(attribute.value?.type === "JSXExpressionContainer" && attribute.value.expression.type === "Literal" && attribute.value.expression.value === false))) return;
1648
+ const byIndex = findAttribute(element, "keyed") !== void 0;
1649
+ const list = this.constantList(expression);
1650
+ if (!list) return;
1651
+ const significant = element.children.filter((child) => !isInsignificant(child));
1652
+ const only = significant.at(0);
1653
+ if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return;
1654
+ let row = only.expression;
1655
+ while (row.type === "ParenthesizedExpression") row = row.expression;
1656
+ if (row.type !== "ArrowFunctionExpression" || row.async || !row.expression || row.params.length > 2) return;
1657
+ let body = row.body;
1658
+ while (body.type === "ParenthesizedExpression") body = body.expression;
1659
+ if (body.type !== "JSXElement" && body.type !== "JSXFragment" || hasRefOrSpread(body)) return;
1660
+ if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
1661
+ const scope = this.context.scopes.scopes.get(row);
1662
+ if (!scope) return;
1663
+ const params = [];
1664
+ for (const param of row.params) {
1665
+ const binding = param.type === "Identifier" ? scope.bindings.get(param.name) : void 0;
1666
+ if (!binding || binding.mutated) return;
1667
+ params.push(binding);
1368
1668
  }
1369
- if (matches.every((item) => item.when === false)) return this.fallback(element);
1370
- for (const item of matches) if (item.when === false) {
1371
- this.context.s.remove(item.element.start, item.element.end);
1372
- this.changed = true;
1669
+ const itemParam = params.at(0);
1670
+ const indexParam = params.at(1);
1671
+ const rows = [];
1672
+ for (const [index, item] of list.items.entries()) {
1673
+ const copy = new MagicString(this.context.code);
1674
+ if (itemParam && !this.substituteItem(copy, itemParam, item, byIndex) || indexParam && !this.substituteIndex(copy, indexParam, index, !byIndex)) return;
1675
+ rows.push(copy.slice(body.start, body.end));
1373
1676
  }
1677
+ const text = `<>${rows.join("")}</>`;
1678
+ if (!this.unrollingPays(element, text, list.declaration)) return;
1679
+ return {
1680
+ kind: "jsx",
1681
+ text
1682
+ };
1374
1683
  }
1375
1684
  /**
1376
- * `<Dynamic component="div">` is just `<div>`, which the JSX transform can
1377
- * put in a template instead of creating the element at runtime.
1685
+ * The items of a constant list, and the declaration that goes once no
1686
+ * `<For>` reads it.
1378
1687
  */
1379
- foldDynamic(element) {
1380
- const component = findAttribute(element, "component");
1688
+ constantList(expression) {
1689
+ let node = expression;
1690
+ while (node.type === "ParenthesizedExpression") node = node.expression;
1691
+ let declaration;
1692
+ if (node.type === "Identifier") {
1693
+ const binding = this.context.scopes.references.get(node);
1694
+ if (binding?.declaration.type !== "VariableDeclarator" || binding.declaration.id !== binding.identifier || !binding.declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
1695
+ const statement = this.context.parents.get(binding.declaration);
1696
+ if ((statement ? this.context.parents.get(statement) : void 0)?.type === "ExportNamedDeclaration" || !binding.references.every((reference) => this.isForEach(reference))) return;
1697
+ node = binding.declaration.init;
1698
+ while (node.type === "ParenthesizedExpression") node = node.expression;
1699
+ declaration = binding.references.length === 1 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 ? statement : void 0;
1700
+ }
1701
+ if (node.type !== "ArrayExpression") return;
1702
+ const items = [];
1703
+ for (const element of node.elements) {
1704
+ const item = element && element.type !== "SpreadElement" ? this.constantItem(element) : void 0;
1705
+ if (!item) return;
1706
+ items.push(item);
1707
+ }
1708
+ return {
1709
+ items,
1710
+ declaration
1711
+ };
1712
+ }
1713
+ /** Whether a reference is the whole `each` of a Solid `<For>`. */
1714
+ isForEach(reference) {
1715
+ const container = this.context.parents.get(reference);
1716
+ const attribute = container ? this.context.parents.get(container) : void 0;
1717
+ const opening = attribute ? this.context.parents.get(attribute) : void 0;
1718
+ const element = opening ? this.context.parents.get(opening) : void 0;
1719
+ return container?.type === "JSXExpressionContainer" && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "each" && element?.type === "JSXElement" && this.builtInTag(element) === "For";
1720
+ }
1721
+ /** A list item whose value is known: a primitive literal, or an object literal of them. */
1722
+ constantItem(node) {
1723
+ const primitive = primitiveText(this.context, node);
1724
+ if (primitive !== void 0) return {
1725
+ kind: "primitive",
1726
+ text: primitive
1727
+ };
1728
+ if (node.type !== "ObjectExpression") return;
1729
+ const keys = /* @__PURE__ */ new Map();
1730
+ for (const property of node.properties) {
1731
+ if (property.type !== "Property" || property.kind !== "init" || property.method || property.computed) return;
1732
+ const key = keyName(property.key);
1733
+ const value = primitiveText(this.context, property.value);
1734
+ if (key === void 0 || value === void 0 || key === "__proto__") return;
1735
+ keys.set(key, value);
1736
+ }
1737
+ return {
1738
+ kind: "object",
1739
+ keys
1740
+ };
1741
+ }
1742
+ /**
1743
+ * Writes an item's value over each read of the row's item parameter.
1744
+ * `false` when a read is not one the value can take the place of.
1745
+ */
1746
+ substituteItem(copy, param, item, accessor) {
1747
+ for (const reference of param.references) {
1748
+ let read = reference;
1749
+ if (accessor) {
1750
+ const call = this.context.parents.get(reference);
1751
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
1752
+ read = call;
1753
+ }
1754
+ if (item.kind === "primitive") {
1755
+ copy.overwrite(read.start, read.end, this.valueAt(read, item.text));
1756
+ continue;
1757
+ }
1758
+ const member = this.context.parents.get(read);
1759
+ if (member?.type !== "MemberExpression" || member.object !== read || isWriteTarget(member, this.context.parents)) return false;
1760
+ let key;
1761
+ if (!member.computed) key = member.property.type === "Identifier" ? member.property.name : void 0;
1762
+ else if (member.property.type === "Literal") key = keyName(member.property);
1763
+ const parent = this.context.parents.get(member);
1764
+ if (key === void 0 || parent?.type === "CallExpression" && parent.callee === member || !item.keys.has(key)) return false;
1765
+ copy.overwrite(member.start, member.end, this.valueAt(member, item.keys.get(key) ?? "void 0"));
1766
+ }
1767
+ return true;
1768
+ }
1769
+ /** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
1770
+ valueAt(node, text) {
1771
+ return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : `(${text})`;
1772
+ }
1773
+ /** Writes an item's index over each read of the row's index parameter. */
1774
+ substituteIndex(copy, param, index, accessor) {
1775
+ for (const reference of param.references) {
1776
+ let read = reference;
1777
+ if (accessor) {
1778
+ const call = this.context.parents.get(reference);
1779
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
1780
+ read = call;
1781
+ }
1782
+ copy.overwrite(read.start, read.end, String(index));
1783
+ }
1784
+ return true;
1785
+ }
1786
+ /**
1787
+ * Whether the copies are estimated to be no larger, once lowered, than
1788
+ * the loop and the list it no longer needs.
1789
+ */
1790
+ unrollingPays(element, text, declaration) {
1791
+ const inTemplate = this.joinsParentTemplate(element);
1792
+ const loop = new SizeEstimator(this.context.code).jsx(element, inTemplate) + (declaration ? compactLength(this.context.code.slice(declaration.start, declaration.end)) : 0);
1793
+ const code = `const __so_rows__ = ${text};`;
1794
+ const statement = parse(this.context.filename, code).body.at(0);
1795
+ const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
1796
+ if (init?.type !== "JSXFragment") return false;
1797
+ const estimator = new SizeEstimator(code);
1798
+ return (inTemplate ? init.children.reduce((size, child) => size + (child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child)), 0) : estimator.jsx(init, false)) <= loop;
1799
+ }
1800
+ /** Whether an element sits directly in an element or fragment whose template it joins. */
1801
+ joinsParentTemplate(element) {
1802
+ const parent = this.context.parents.get(element);
1803
+ return parent?.type === "JSXElement" && joinsTemplate(parent) || parent?.type === "JSXFragment";
1804
+ }
1805
+ foldRepeat(element) {
1806
+ const count = findAttribute(element, "count");
1807
+ if (count?.value?.type !== "JSXExpressionContainer" || count.value.expression.type === "JSXEmptyExpression") return;
1808
+ const value = evaluate(count.value.expression, this.lookup);
1809
+ if (typeof value?.value !== "number") return;
1810
+ if (value.value >= 1) return;
1811
+ return this.fallback(element);
1812
+ }
1813
+ /**
1814
+ * Resolves a `<Switch>` as far as its `<Match when>` conditions allow.
1815
+ * Statically false matches are dropped, a statically true match with no
1816
+ * undecided match before it wins, and a switch whose every match is false
1817
+ * renders its fallback.
1818
+ */
1819
+ foldSwitch(element) {
1820
+ const matches = [];
1821
+ for (const child of element.children) {
1822
+ if (isInsignificant(child)) continue;
1823
+ if (child.type !== "JSXElement" || this.builtInTag(child) !== "Match" || hasSpreadAttribute(child) || !findAttribute(child, "when")) return;
1824
+ matches.push({
1825
+ element: child,
1826
+ when: this.attributeTruthiness(child, "when")
1827
+ });
1828
+ }
1829
+ if (matches.length === 0) return;
1830
+ const winner = matches.findIndex((item) => item.when === true);
1831
+ if (winner !== -1 && matches.slice(0, winner).every((item) => item.when === false)) {
1832
+ const match = matches.at(winner);
1833
+ if (!match || hasCallbackChild(match.element.children)) return;
1834
+ return {
1835
+ kind: "children",
1836
+ children: match.element.children
1837
+ };
1838
+ }
1839
+ if (matches.every((item) => item.when === false)) return this.fallback(element);
1840
+ for (const item of matches) if (item.when === false) {
1841
+ this.context.s.remove(item.element.start, item.element.end);
1842
+ this.changed = true;
1843
+ }
1844
+ }
1845
+ /**
1846
+ * `<Dynamic component="div">` is just `<div>`, which the JSX transform can
1847
+ * put in a template instead of creating the element at runtime.
1848
+ */
1849
+ /**
1850
+ * `<Dynamic component="div">` becomes `<div>`. `<Dynamic component={Card}>`
1851
+ * becomes `<Card>` when `Card` is a component declared at the top level
1852
+ * that nothing reassigns. `<Dynamic>` then only renders `Card` with the
1853
+ * other props, through a memo that never changes.
1854
+ */
1855
+ foldDynamic(element) {
1856
+ const component = findAttribute(element, "component");
1381
1857
  if (!component?.value) return;
1382
1858
  let tag;
1383
1859
  const { value } = component;
1384
- if (value.type === "Literal") tag = value.value;
1860
+ if (value.type === "Literal") tag = isIntrinsicTag(value.value) ? value.value : void 0;
1385
1861
  else if (value.type === "JSXExpressionContainer" && value.expression.type !== "JSXEmptyExpression") {
1386
- const result = evaluate(value.expression, this.lookup);
1387
- if (typeof result?.value === "string") tag = result.value;
1862
+ const expression = value.expression.type === "ParenthesizedExpression" ? value.expression.expression : value.expression;
1863
+ if (expression.type === "Identifier") tag = this.componentName(expression);
1864
+ if (tag === void 0) {
1865
+ const result = evaluate(value.expression, this.lookup);
1866
+ if (typeof result?.value === "string" && isIntrinsicTag(result.value)) tag = result.value;
1867
+ }
1388
1868
  }
1389
- if (tag === void 0 || !isIntrinsicTag(tag)) return;
1869
+ if (tag === void 0) return;
1390
1870
  const attributes = element.openingElement.attributes.filter((attribute) => attribute !== component).map((attribute) => textOf(this.context, attribute));
1391
1871
  const opening = [tag, ...attributes].join(" ");
1392
1872
  return {
@@ -1431,6 +1911,12 @@ var Folder = class {
1431
1911
  pieces.push(splicePiece(this.context.s, child, child.children));
1432
1912
  continue;
1433
1913
  }
1914
+ const reduced = child.type === "JSXExpressionContainer" && joinsTemplate(parent) ? this.reducedCall(child.expression) : void 0;
1915
+ if (reduced !== void 0) {
1916
+ folded = true;
1917
+ pieces.push(replacedPiece(child, reduced));
1918
+ continue;
1919
+ }
1434
1920
  const resolved = child.type === "JSXElement" ? this.foldFlow(child) : void 0;
1435
1921
  if (!resolved) {
1436
1922
  pieces.push(keptPiece(child));
@@ -1452,6 +1938,64 @@ var Folder = class {
1452
1938
  }
1453
1939
  if (folded && writePieces(this.context.s, pieces)) this.changed = true;
1454
1940
  }
1941
+ /**
1942
+ * The JSX a call like `((n) => <li>{n}</li>)(1)` makes, with each
1943
+ * argument in place of its parameter, or `undefined` when that could
1944
+ * behave differently.
1945
+ *
1946
+ * The child is inserted by an effect that makes the JSX. The JSX
1947
+ * transform wraps every dynamic part of JSX in an effect of its own, so
1948
+ * making it reads nothing the outer effect tracks, and the outer effect
1949
+ * runs once. A `ref` runs while the JSX is made, and a spread reads its
1950
+ * object there, so either one keeps the call. Each argument has to be a
1951
+ * constant or a binding nothing reassigns, so reading it again where the
1952
+ * parameter was gives the same value.
1953
+ */
1954
+ reducedCall(expression) {
1955
+ if (expression.type !== "CallExpression" || expression.optional) return;
1956
+ let callee = expression.callee;
1957
+ while (callee.type === "ParenthesizedExpression") callee = callee.expression;
1958
+ if (callee.type !== "ArrowFunctionExpression" || callee.async || !callee.expression) return;
1959
+ let body = callee.body;
1960
+ while (body.type === "ParenthesizedExpression") body = body.expression;
1961
+ if (body.type !== "JSXElement" && body.type !== "JSXFragment") return;
1962
+ const { params } = callee;
1963
+ const args = expression.arguments;
1964
+ if (params.length !== args.length || hasRefOrSpread(body)) return;
1965
+ if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
1966
+ const scope = this.context.scopes.scopes.get(callee);
1967
+ if (!scope) return;
1968
+ const copy = new MagicString(this.context.code);
1969
+ for (const [index, param] of params.entries()) {
1970
+ const arg = args.at(index);
1971
+ if (param.type !== "Identifier" || !arg || arg.type === "SpreadElement") return;
1972
+ const binding = scope.bindings.get(param.name);
1973
+ const text = this.argumentText(arg);
1974
+ if (!binding || binding.mutated || text === void 0) return;
1975
+ for (const reference of binding.references) {
1976
+ if (arg.type === "Identifier" && lookup(scopeAt(this.context.scopes, this.context.parents, reference), arg.name) !== this.context.scopes.references.get(arg)) return;
1977
+ const parent = this.context.parents.get(reference);
1978
+ const shorthand = parent?.type === "Property" && parent.shorthand && parent.value === reference;
1979
+ copy.overwrite(reference.start, reference.end, shorthand ? `${param.name}: ${text}` : text);
1980
+ }
1981
+ }
1982
+ return copy.slice(body.start, body.end);
1983
+ }
1984
+ /** The text of an argument that reads the same wherever it is copied. */
1985
+ argumentText(arg) {
1986
+ let node = arg;
1987
+ while (node.type === "ParenthesizedExpression") node = node.expression;
1988
+ switch (node.type) {
1989
+ case "Literal": return "regex" in node ? void 0 : textOf(this.context, node);
1990
+ case "TemplateLiteral": return node.expressions.length === 0 ? textOf(this.context, node) : void 0;
1991
+ case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${textOf(this.context, node)})` : void 0;
1992
+ case "Identifier": {
1993
+ const binding = this.context.scopes.references.get(node);
1994
+ return binding && !binding.mutated ? node.name : void 0;
1995
+ }
1996
+ default: return;
1997
+ }
1998
+ }
1455
1999
  eliminateDeadStatements(statements) {
1456
2000
  let terminator = -1;
1457
2001
  for (const [index, statement] of statements.entries()) {
@@ -1477,7 +2021,7 @@ var Folder = class {
1477
2021
  this.changed = true;
1478
2022
  return;
1479
2023
  }
1480
- this.replace(statement, continuesPreviousStatement$1(text) ? `;${text}` : text);
2024
+ this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
1481
2025
  }
1482
2026
  /**
1483
2027
  * A statement that keeps a discarded test's side effects, or `undefined`
@@ -1533,29 +2077,35 @@ const CONTEXT_SAFE_PRIMITIVES = [
1533
2077
  "createMemo",
1534
2078
  "createEffect",
1535
2079
  "createRenderEffect",
1536
- "createComputed",
2080
+ "createStore",
2081
+ "createProjection",
2082
+ "createOptimistic",
1537
2083
  "onCleanup",
1538
- "onMount",
2084
+ "onSettled",
1539
2085
  "untrack",
1540
- "batch",
1541
2086
  "useContext",
2087
+ "flush",
1542
2088
  "mapArray",
1543
- "indexArray",
1544
- "mergeProps",
1545
- "splitProps"
2089
+ "repeat",
2090
+ "merge",
2091
+ "omit"
1546
2092
  ];
1547
2093
  /** Primitives that return a getter and a setter, as `[get, set]`. */
1548
- const ACCESSOR_PAIRS = /* @__PURE__ */ new Set(["createSignal"]);
2094
+ const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
2095
+ "createSignal",
2096
+ "createStore",
2097
+ "createOptimistic"
2098
+ ]);
1549
2099
  /**
1550
- * The provider a JSX element creates: `Ctx.Provider`, where `Ctx` is a
1551
- * top-level `createContext(...)` from Solid that nothing writes to, with one
1552
- * `value` attribute holding a value that is never `undefined`.
2100
+ * The provider a JSX element creates: a tag that refers to a top-level
2101
+ * `createContext(...)` from Solid, nothing writes to, with one `value`
2102
+ * attribute holding a value that is never `undefined`.
1553
2103
  *
1554
- * `useContext` returns the default when the value is `undefined`, so a
1555
- * value that could be `undefined` keeps its provider.
2104
+ * `useContext` throws when the value and the default are both `undefined`,
2105
+ * so a value that could be `undefined` keeps its provider.
1556
2106
  */
1557
2107
  function providerOf(context, element) {
1558
- const binding = providerContext(context, element.openingElement.name);
2108
+ const binding = contextBinding(context, element.openingElement.name);
1559
2109
  if (!binding || element.openingElement.selfClosing) return;
1560
2110
  const { attributes } = element.openingElement;
1561
2111
  const attribute = attributes.at(0);
@@ -1572,14 +2122,7 @@ function providerOf(context, element) {
1572
2122
  };
1573
2123
  }
1574
2124
  /**
1575
- * The context a `Ctx.Provider` tag provides.
1576
- */
1577
- function providerContext(context, name) {
1578
- if (name.type !== "JSXMemberExpression" || name.object.type !== "JSXIdentifier" || name.property.name !== "Provider") return;
1579
- return contextBinding(context, name.object);
1580
- }
1581
- /**
1582
- * The top-level context binding an identifier refers to.
2125
+ * The top-level context binding a tag or identifier refers to.
1583
2126
  */
1584
2127
  function contextBinding(context, name) {
1585
2128
  if (name.type !== "JSXIdentifier" && name.type !== "Identifier") return;
@@ -1683,6 +2226,10 @@ function isAccessor(context, binding) {
1683
2226
  */
1684
2227
  function isClosed(context, root, options) {
1685
2228
  let closed = true;
2229
+ const open = (reason) => {
2230
+ closed = false;
2231
+ options.onOpen?.(reason);
2232
+ };
1686
2233
  const visit = (node) => {
1687
2234
  if (!closed) return;
1688
2235
  switch (node.type) {
@@ -1691,32 +2238,34 @@ function isClosed(context, root, options) {
1691
2238
  break;
1692
2239
  case "JSXSpreadAttribute":
1693
2240
  if (!isOwnView(context, node.argument, options.props, 0)) {
1694
- closed = false;
2241
+ open("open-spread");
1695
2242
  return;
1696
2243
  }
1697
2244
  break;
1698
- case "JSXSpreadChild":
1699
2245
  case "NewExpression":
2246
+ open("open-new");
2247
+ return;
2248
+ case "JSXSpreadChild":
1700
2249
  case "TaggedTemplateExpression":
1701
2250
  case "ImportExpression":
1702
- closed = false;
2251
+ open("open-other");
1703
2252
  return;
1704
2253
  case "JSXElement":
1705
2254
  if (!isVisibleElement(context, node, options)) {
1706
- closed = false;
2255
+ open("open-component");
1707
2256
  return;
1708
2257
  }
1709
2258
  break;
1710
2259
  case "CallExpression":
1711
2260
  if (!isVisibleCall(context, node, options)) {
1712
- closed = false;
2261
+ open("open-call");
1713
2262
  return;
1714
2263
  }
1715
2264
  break;
1716
2265
  case "Identifier": {
1717
2266
  const binding = context.scopes.references.get(node);
1718
2267
  if (binding && runsOutsideCode(context, binding, root, options, 0)) {
1719
- closed = false;
2268
+ open("open-outer");
1720
2269
  return;
1721
2270
  }
1722
2271
  break;
@@ -1762,8 +2311,8 @@ function runsOutsideCode(context, binding, root, options, depth) {
1762
2311
  return found;
1763
2312
  }
1764
2313
  /**
1765
- * Whether an expression is a `mergeProps()` or `splitProps()` view of a
1766
- * component's own props, with only literal objects and keys besides.
2314
+ * Whether an expression is a `merge()` or `omit()` view of a component's
2315
+ * own props, with only literal objects and keys besides.
1767
2316
  */
1768
2317
  function isOwnView(context, node, props, depth) {
1769
2318
  const expression = unwrap(node);
@@ -1772,33 +2321,23 @@ function isOwnView(context, node, props, depth) {
1772
2321
  if (expression.type === "Identifier") {
1773
2322
  const binding = context.scopes.references.get(expression);
1774
2323
  if (binding === props) return true;
1775
- call = binding ? viewInit(context, binding) : void 0;
2324
+ const init = binding ? constantInit(context, binding) : void 0;
2325
+ call = init ? unwrap(init) : void 0;
1776
2326
  }
1777
2327
  if (call?.type !== "CallExpression") return false;
1778
2328
  const callee = solidCallee(context, call);
1779
- if (callee !== "mergeProps" && callee !== "splitProps") return false;
2329
+ if (callee !== "merge" && callee !== "omit") return false;
1780
2330
  return call.arguments.every((argument) => {
1781
2331
  if (argument.type === "SpreadElement") return false;
1782
2332
  const inner = unwrap(argument);
1783
- return inner.type === "ObjectExpression" || inner.type === "ArrayExpression" && inner.elements.every((key) => key?.type === "Literal" && typeof key.value === "string") || isOwnView(context, inner, props, depth + 1);
2333
+ return inner.type === "ObjectExpression" || inner.type === "Literal" && typeof inner.value === "string" || isOwnView(context, inner, props, depth + 1);
1784
2334
  });
1785
2335
  }
1786
- /**
1787
- * The call a view binding comes from: `const view = mergeProps(...)`, or
1788
- * one of the names in `const [a, b] = splitProps(...)`.
1789
- */
1790
- function viewInit(context, binding) {
1791
- const init = constantInit(context, binding);
1792
- if (init) return unwrap(init);
1793
- const { declaration } = binding;
1794
- if (binding.mutated || binding.kind !== "const" || declaration.type !== "VariableDeclarator" || declaration.id.type !== "ArrayPattern" || !declaration.id.elements.some((element) => element === binding.identifier) || !declaration.init) return;
1795
- return unwrap(declaration.init);
1796
- }
1797
2336
  function isVisibleElement(context, element, options) {
1798
2337
  const { name } = element.openingElement;
1799
- if (providerContext(context, name)) return true;
1800
2338
  if (name.type !== "JSXIdentifier") return false;
1801
2339
  if (isIntrinsicTag(name.name)) return true;
2340
+ if (contextBinding(context, name)) return true;
1802
2341
  const binding = context.scopes.references.get(name);
1803
2342
  const identity = binding ? solidExport(binding, context.options) : name.name;
1804
2343
  if (identity !== void 0 && identity !== "Dynamic" && context.options.builtIns.has(identity)) return true;
@@ -1897,7 +2436,7 @@ function contextReads(context, root, target) {
1897
2436
  const reads = [];
1898
2437
  const visit = (node) => {
1899
2438
  if (node.type === "JSXAttribute" && isHandlerFunction(node)) return;
1900
- if (node !== root && node.type === "JSXElement" && providerContext(context, node.openingElement.name) === target) return;
2439
+ if (node !== root && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === target) return;
1901
2440
  if (node.type === "CallExpression" && solidCallee(context, node) === "useContext") {
1902
2441
  const argument = node.arguments.at(0);
1903
2442
  if (node.arguments.length === 1 && argument?.type !== "SpreadElement" && argument !== void 0 && contextBinding(context, unwrap(argument)) === target) reads.push(node);
@@ -1915,7 +2454,7 @@ function hasNestedProvider(context, provider) {
1915
2454
  let nested = false;
1916
2455
  const visit = (node) => {
1917
2456
  if (nested) return;
1918
- if (node !== provider.element && node.type === "JSXElement" && providerContext(context, node.openingElement.name) === provider.context) {
2457
+ if (node !== provider.element && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === provider.context) {
1919
2458
  nested = true;
1920
2459
  return;
1921
2460
  }
@@ -1948,25 +2487,63 @@ var ProviderRemover = class {
1948
2487
  run() {
1949
2488
  for (const provider of findProviders(this.context)) {
1950
2489
  const { element } = provider;
1951
- if (!this.removed.some((other) => element.start < other.end && other.start < element.end) && !hasNestedProvider(this.context, provider) && this.tryRemove(provider)) this.removed.push(element);
2490
+ if (this.removed.some((other) => element.start < other.end && other.start < element.end)) continue;
2491
+ if (hasNestedProvider(this.context, provider)) this.skip(provider, "provider-nested");
2492
+ else if (this.tryRemove(provider)) this.removed.push(element);
1952
2493
  }
2494
+ if (this.context.skips) this.reportShapes();
2495
+ }
2496
+ skip(provider, reason) {
2497
+ recordSkip(this.context, "contexts", reason, provider.context.name);
2498
+ }
2499
+ /**
2500
+ * Records each context tag that is not a provider this pass understands,
2501
+ * such as one with more attributes than `value`.
2502
+ */
2503
+ reportShapes() {
2504
+ const visit = (node) => {
2505
+ if (node.type === "JSXElement") {
2506
+ const binding = contextBinding(this.context, node.openingElement.name);
2507
+ if (binding && !providerOf(this.context, node)) {
2508
+ const { attributes } = node.openingElement;
2509
+ const attribute = attributes.at(0);
2510
+ const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
2511
+ recordSkip(this.context, "contexts", onlyValue && !node.openingElement.selfClosing ? "provider-undefined" : "provider-shape", binding.name);
2512
+ }
2513
+ }
2514
+ forEachChild(node, visit);
2515
+ };
2516
+ visit(this.context.program);
1953
2517
  }
1954
2518
  tryRemove(provider) {
1955
2519
  const { element } = provider;
1956
2520
  const children = element.children;
2521
+ let reason = "open-other";
1957
2522
  if (!children.every((child) => isClosed(this.context, child, {
1958
2523
  component: () => false,
1959
- contextValue: (target) => target === provider.context ? provider.value : void 0
1960
- }))) return false;
2524
+ contextValue: (target) => target === provider.context ? provider.value : void 0,
2525
+ onOpen: (open) => {
2526
+ reason = open;
2527
+ }
2528
+ }))) {
2529
+ this.skip(provider, reason);
2530
+ return false;
2531
+ }
1961
2532
  const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
1962
2533
  const texts = [];
1963
2534
  for (const read of reads) {
1964
2535
  const text = stableValueText(this.context, provider, read);
1965
- if (text === void 0) return false;
2536
+ if (text === void 0) {
2537
+ this.skip(provider, "provider-value");
2538
+ return false;
2539
+ }
1966
2540
  texts.push(text);
1967
2541
  }
1968
2542
  if (reads.length === 0 && !isSideEffectFree(provider.value)) {
1969
- if (stableValueText(this.context, provider, element) === void 0) return false;
2543
+ if (stableValueText(this.context, provider, element) === void 0) {
2544
+ this.skip(provider, "provider-value");
2545
+ return false;
2546
+ }
1970
2547
  }
1971
2548
  const { s } = this.context;
1972
2549
  for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
@@ -1986,170 +2563,11 @@ function removeProviders(context) {
1986
2563
  return remover.changed;
1987
2564
  }
1988
2565
  //#endregion
1989
- //#region src/size.ts
1990
- /** A template's declaration, and the function that clones and returns it. */
1991
- const TEMPLATE = 40;
1992
- /** A walk to an element, like `e.firstChild.nextSibling`. */
1993
- const WALK = 12;
1994
- /** An `insert()` of a dynamic child. */
1995
- const INSERT = 12;
1996
- /** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
1997
- const EFFECT = 24;
1998
- /** An event handler assignment, like `e.$$click = ...`. */
1999
- const HANDLER = 10;
2000
- /** A `spread()` of props onto an element. */
2001
- const SPREAD = 16;
2002
- /** A `createComponent(Name, {})` call. */
2003
- const COMPONENT = 8;
2004
- /** A getter, `get name() { return ... }`. */
2005
- const GETTER = 16;
2006
- /** A `mergeProps()` around the props of a component with a spread. */
2007
- const MERGE = 6;
2566
+ //#region src/copy-size.ts
2008
2567
  /**
2009
- * The length of `text` once a minifier removes the whitespace it does not need.
2568
+ * Measures a component's copy at a call site: folds it as the passes after
2569
+ * inlining would, then estimates its size with `SizeEstimator`.
2010
2570
  */
2011
- function compactLength(text) {
2012
- return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
2013
- }
2014
- function nameLength(name) {
2015
- switch (name.type) {
2016
- case "JSXIdentifier": return name.name.length;
2017
- case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
2018
- case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
2019
- default: return 4;
2020
- }
2021
- }
2022
- function isIntrinsic(element) {
2023
- const { name } = element.openingElement;
2024
- return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
2025
- }
2026
- function attributeName(attribute) {
2027
- const { name } = attribute;
2028
- return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
2029
- }
2030
- /**
2031
- * Estimates lowered sizes in one piece of code.
2032
- */
2033
- var SizeEstimator = class {
2034
- constructor(code) {
2035
- this.code = code;
2036
- }
2037
- text(node) {
2038
- return compactLength(this.code.slice(node.start, node.end));
2039
- }
2040
- /**
2041
- * The size of any node: JSX as lowered, other code as its compact text
2042
- * with the JSX in it as lowered.
2043
- */
2044
- node(node) {
2045
- if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
2046
- let size = this.text(node);
2047
- const visit = (child) => {
2048
- if (child.type === "JSXElement" || child.type === "JSXFragment") {
2049
- size += this.jsx(child, false) - this.text(child);
2050
- return;
2051
- }
2052
- forEachChild(child, visit);
2053
- };
2054
- forEachChild(node, visit);
2055
- return size;
2056
- }
2057
- /**
2058
- * The size of a JSX element or fragment. `inTemplate` is whether it sits
2059
- * directly in an element, whose template it joins.
2060
- */
2061
- jsx(node, inTemplate) {
2062
- if (node.type === "JSXFragment") return this.children(node.children) + 2;
2063
- if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
2064
- const tag = nameLength(node.openingElement.name);
2065
- let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
2066
- let dynamic = false;
2067
- for (const attribute of node.openingElement.attributes) {
2068
- const cost = this.elementAttribute(attribute);
2069
- size += cost.size;
2070
- dynamic ||= cost.dynamic;
2071
- }
2072
- for (const child of node.children) {
2073
- if (isInsignificant(child)) continue;
2074
- if (child.type === "JSXText") size += compactLength(child.value);
2075
- else if (child.type === "JSXElement") size += this.jsx(child, true);
2076
- else {
2077
- size += INSERT + this.child(child);
2078
- dynamic = true;
2079
- }
2080
- }
2081
- return size + (dynamic ? WALK : 0);
2082
- }
2083
- elementAttribute(attribute) {
2084
- if (attribute.type === "JSXSpreadAttribute") return {
2085
- size: SPREAD + this.node(attribute.argument),
2086
- dynamic: true
2087
- };
2088
- const name = attributeName(attribute);
2089
- const { value } = attribute;
2090
- if (!value || value.type === "Literal") return {
2091
- size: name.length + (value ? this.text(value) : 0) + 1,
2092
- dynamic: false
2093
- };
2094
- if (value.type !== "JSXExpressionContainer") return {
2095
- size: name.length + 1 + this.jsx(value, false),
2096
- dynamic: true
2097
- };
2098
- const { expression } = value;
2099
- if (expression.type === "JSXEmptyExpression") return {
2100
- size: 0,
2101
- dynamic: false
2102
- };
2103
- const inner = unwrap(expression);
2104
- if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
2105
- size: name.length + this.text(inner) + 1,
2106
- dynamic: false
2107
- };
2108
- return {
2109
- size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
2110
- dynamic: true
2111
- };
2112
- }
2113
- child(child) {
2114
- if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
2115
- if (child.type === "JSXSpreadChild") return this.node(child.expression);
2116
- if (child.type === "JSXText") return compactLength(child.value) + 2;
2117
- return this.jsx(child, false);
2118
- }
2119
- /**
2120
- * Children outside a template, which lower to an array of values.
2121
- */
2122
- children(children) {
2123
- let size = 0;
2124
- for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
2125
- return size;
2126
- }
2127
- component(element) {
2128
- let size = COMPONENT + nameLength(element.openingElement.name);
2129
- let spread = false;
2130
- for (const attribute of element.openingElement.attributes) {
2131
- if (attribute.type === "JSXSpreadAttribute") {
2132
- spread = true;
2133
- size += this.node(attribute.argument) + 1;
2134
- continue;
2135
- }
2136
- const name = attributeName(attribute);
2137
- const { value } = attribute;
2138
- if (!value || value.type === "Literal") size += name.length + 1 + (value ? this.text(value) : 4);
2139
- else if (value.type === "JSXExpressionContainer") {
2140
- const { expression } = value;
2141
- if (expression.type !== "JSXEmptyExpression") {
2142
- const inner = unwrap(expression);
2143
- const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
2144
- size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
2145
- }
2146
- } else size += name.length + GETTER + this.jsx(value, false);
2147
- }
2148
- const significant = element.children.filter((child) => !isInsignificant(child));
2149
- if (significant.length > 0) size += 24 + this.children(significant);
2150
- return size + (spread ? MERGE : 0);
2151
- }
2152
- };
2153
2571
  /** The name of the function a copy is wrapped in to be folded and measured. */
2154
2572
  const WRAPPER = "__so_size__";
2155
2573
  /**
@@ -2170,7 +2588,8 @@ function copySize(filename, header, copy, inTemplate, options) {
2170
2588
  program,
2171
2589
  parents: collectParents(program),
2172
2590
  scopes: analyzeScopes(program),
2173
- options
2591
+ options,
2592
+ skips: void 0
2174
2593
  })) break;
2175
2594
  code = s.toString();
2176
2595
  }
@@ -2183,9 +2602,7 @@ function copySize(filename, header, copy, inTemplate, options) {
2183
2602
  size += estimator.node(statement);
2184
2603
  continue;
2185
2604
  }
2186
- const argument = statement.argument ? unwrap(statement.argument) : void 0;
2187
- if (argument?.type === "JSXElement" || argument?.type === "JSXFragment") size += estimator.jsx(argument, inTemplate);
2188
- else if (argument) size += (inTemplate ? 24 : 0) + estimator.node(argument);
2605
+ if (statement.argument) size += estimator.root(statement.argument, inTemplate);
2189
2606
  }
2190
2607
  return size;
2191
2608
  }
@@ -2202,13 +2619,15 @@ function copySize(filename, header, copy, inTemplate, options) {
2202
2619
  *
2203
2620
  * - It is declared once at the top level of the module, with a capitalized
2204
2621
  * name, as a function declaration or a `const` bound to a function.
2205
- * - It takes no parameter, or one identifier that is only read as `props.name`.
2622
+ * - It takes no parameter, or one identifier that is only read as `props.name`,
2623
+ * or spread onto an element as `{...props}`.
2206
2624
  * - Its body is statements followed by one final `return` of JSX, or an arrow
2207
2625
  * function whose body is JSX.
2208
2626
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
2209
2627
  *
2210
- * A call site inlines when it passes plain attributes: no spread, no `ref`,
2211
- * and no namespaced attribute.
2628
+ * A call site inlines when it passes plain attributes: no spread and no
2629
+ * namespaced attribute. A `ref` moves to the element the component forwards
2630
+ * `props.ref` to. See `refValue`.
2212
2631
  *
2213
2632
  * # How props keep their meaning
2214
2633
  *
@@ -2228,58 +2647,38 @@ function copySize(filename, header, copy, inTemplate, options) {
2228
2647
  *
2229
2648
  * Statements, and stored props, have to run where the component would have
2230
2649
  * been created. Solid runs a component untracked, so the statements move into
2231
- * the function whose returned JSX holds the call: a component itself, which
2232
- * also runs untracked. The call has to be in that returned JSX with only
2233
- * intrinsic elements and fragments around it, so it is created exactly once
2234
- * each time that function runs.
2650
+ * the function whose returned JSX holds the call, when that function also
2651
+ * runs untracked:
2652
+ *
2653
+ * - a component;
2654
+ * - the child callback of `<For>` or `<Repeat>`, which runs once per item
2655
+ * under its own owner;
2656
+ * - the child callback of `<Show>` or `<Match>` that takes the value, which
2657
+ * runs once each time the condition turns true;
2658
+ * - the callback given to `render`, which runs once under its root.
2659
+ *
2660
+ * The call has to be in that returned JSX with only intrinsic elements and
2661
+ * fragments around it, so it is created exactly once each time that function
2662
+ * runs.
2235
2663
  *
2236
2664
  * A context provider is a component, so statements do not move out of it,
2237
2665
  * and `useContext` still runs under it. The exception is a provider whose
2238
2666
  * subtree only runs visible code. Statements move out through it, and each
2239
2667
  * `useContext` of its context in a copy becomes its value. See `provider.ts`.
2240
2668
  *
2241
- * The props can also be read through `mergeProps()` and `splitProps()` views
2242
- * declared in the component. Each call site knows which props it passes, so
2243
- * a read of a view resolves to a prop, a prop with a literal default, a
2244
- * default, or `undefined`, and a spread of a view becomes the attributes it
2245
- * holds.
2669
+ * The props can also be read through `merge()` and `omit()` views declared
2670
+ * in the component. Each call site knows which props it passes, so a read
2671
+ * of a view resolves to a prop, a literal default, or `undefined`, and a
2672
+ * spread of a view becomes the attributes it holds.
2246
2673
  *
2247
2674
  * # Accepted differences
2248
2675
  *
2249
2676
  * - A prop that is a function and is called as `props.name()` runs with
2250
2677
  * `this` set to its own receiver instead of the props object.
2251
2678
  * - Statements moved into the host run before the host's JSX is created,
2252
- * instead of while it is created. The order of `onMount` and effect
2679
+ * instead of while it is created. The order of `onSettled` and effect
2253
2680
  * registration across siblings can change.
2254
2681
  */
2255
- /**
2256
- * A prop read that falls back to a default when the prop is `undefined`,
2257
- * the way `mergeProps()` reads it.
2258
- */
2259
- function withFallback(text, fallback) {
2260
- return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
2261
- }
2262
- function allows(source, key) {
2263
- return !source.hidden.has(key) && (source.only === void 0 || source.only.has(key));
2264
- }
2265
- /**
2266
- * Whether an expression passed as a prop is never `undefined`.
2267
- */
2268
- function isDefinedExpression(node) {
2269
- const inner = unwrap(node);
2270
- switch (inner.type) {
2271
- case "ObjectExpression":
2272
- case "ArrayExpression":
2273
- case "ArrowFunctionExpression":
2274
- case "FunctionExpression":
2275
- case "ClassExpression":
2276
- case "NewExpression":
2277
- case "TemplateLiteral":
2278
- case "JSXElement":
2279
- case "JSXFragment": return true;
2280
- default: return evaluate(inner, () => void 0)?.value !== void 0;
2281
- }
2282
- }
2283
2682
  function isWithin(node, container) {
2284
2683
  return node.start >= container.start && node.end <= container.end;
2285
2684
  }
@@ -2426,11 +2825,68 @@ function functionOf(binding, options) {
2426
2825
  function attributesOf(element) {
2427
2826
  const attributes = /* @__PURE__ */ new Map();
2428
2827
  for (const attribute of element.openingElement.attributes) {
2429
- if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" || attributes.has(attribute.name.name)) return;
2828
+ if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(attribute) || attributes.has(attribute.name.name)) return;
2430
2829
  attributes.set(attribute.name.name, attribute);
2431
2830
  }
2432
2831
  return attributes;
2433
2832
  }
2833
+ /**
2834
+ * Whether a top-level statement declares only constants made of literals,
2835
+ * like `const ROWS = [{ name: 'a' }]`. An export keeps its `export`, since
2836
+ * the fold pass treats an exported list differently.
2837
+ */
2838
+ function isDataDeclaration(statement) {
2839
+ const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
2840
+ return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && declaration.declarations.every((declarator) => declarator.id.type === "Identifier" && declarator.init !== null && isDataLiteral(declarator.init));
2841
+ }
2842
+ /** Whether an expression is a literal, or an array or object literal of them. */
2843
+ function isDataLiteral(node) {
2844
+ switch (node.type) {
2845
+ case "Literal": return !("regex" in node);
2846
+ case "TemplateLiteral": return node.expressions.length === 0;
2847
+ case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal";
2848
+ case "ParenthesizedExpression":
2849
+ case "TSAsExpression":
2850
+ case "TSSatisfiesExpression": return isDataLiteral(node.expression);
2851
+ case "ArrayExpression": return node.elements.every((element) => element !== null && element.type !== "SpreadElement" && isDataLiteral(element));
2852
+ case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && property.kind === "init" && !property.method && !property.computed && isDataLiteral(property.value));
2853
+ default: return false;
2854
+ }
2855
+ }
2856
+ function isMemberRead(read) {
2857
+ return "member" in read;
2858
+ }
2859
+ /**
2860
+ * Whether a `ref` at a call site can move to the element the component
2861
+ * forwards it to: a name, a function, or an array of them. A name is
2862
+ * assigned the element, so it has to stay the name itself. Anything else
2863
+ * would be evaluated when the element is made, outside the `untrack` the
2864
+ * component's `ref` runs it in.
2865
+ */
2866
+ function isForwardableRef(attribute) {
2867
+ const { value } = attribute;
2868
+ if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
2869
+ const isTarget = (node) => {
2870
+ const inner = unwrap(node);
2871
+ if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element));
2872
+ return inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
2873
+ };
2874
+ return isTarget(value.expression);
2875
+ }
2876
+ /**
2877
+ * Why `attributesOf` turns down a call site, for the report.
2878
+ */
2879
+ function callSiteReason(element) {
2880
+ const names = /* @__PURE__ */ new Set();
2881
+ for (const attribute of element.openingElement.attributes) {
2882
+ if (attribute.type !== "JSXAttribute") return "call-spread";
2883
+ if (attribute.name.type !== "JSXIdentifier") return "call-namespace";
2884
+ if (attribute.name.name === "ref") return "call-ref";
2885
+ if (names.has(attribute.name.name)) break;
2886
+ names.add(attribute.name.name);
2887
+ }
2888
+ return "call-duplicate";
2889
+ }
2434
2890
  function isAssignmentTarget(context, node) {
2435
2891
  const parent = context.parents.get(node);
2436
2892
  if (!parent) return false;
@@ -2455,6 +2911,28 @@ var Inliner = class {
2455
2911
  this.changed = false;
2456
2912
  this.hosts = /* @__PURE__ */ new Map();
2457
2913
  this.transparent = /* @__PURE__ */ new Map();
2914
+ this.reason = "";
2915
+ }
2916
+ /**
2917
+ * Whether a binding holds a component made by Solid's `lazy`, for the
2918
+ * report. In a chunk, `lazy` can be a local declaration under another
2919
+ * name, so a call given a function with a dynamic import counts too.
2920
+ */
2921
+ isLazy(binding) {
2922
+ const { declaration } = binding;
2923
+ const init = declaration.type === "VariableDeclarator" && declaration.init ? unwrap(declaration.init) : void 0;
2924
+ if (init?.type !== "CallExpression" || init.callee.type !== "Identifier") return false;
2925
+ const callee = this.context.scopes.references.get(init.callee);
2926
+ if (callee && solidExport(callee, this.context.options) === "lazy") return true;
2927
+ const loader = init.arguments.at(0);
2928
+ if (!loader || !isFunctionNode(loader)) return false;
2929
+ let imports = false;
2930
+ const visit = (node) => {
2931
+ if (node.type === "ImportExpression") imports = true;
2932
+ if (!imports) forEachChild(node, visit);
2933
+ };
2934
+ visit(loader);
2935
+ return imports;
2458
2936
  }
2459
2937
  scopeOf(fn) {
2460
2938
  return this.context.scopes.scopes.get(fn);
@@ -2470,41 +2948,87 @@ var Inliner = class {
2470
2948
  if (statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program") return statement;
2471
2949
  }
2472
2950
  analyzeComponent(binding) {
2473
- if (!isCapitalized(binding.name) || binding.mutated) return;
2951
+ if (!isCapitalized(binding.name)) return;
2952
+ if (binding.mutated) {
2953
+ this.reason = "component-mutated";
2954
+ return;
2955
+ }
2474
2956
  const fn = functionOf(binding, this.context.options);
2475
- if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) return;
2957
+ if (!fn?.body) {
2958
+ this.reason = this.isLazy(binding) ? "lazy" : "not-a-function";
2959
+ return;
2960
+ }
2961
+ if (fn.async || fn.generator) {
2962
+ this.reason = "component-async";
2963
+ return;
2964
+ }
2965
+ if (fn.params.length > 1) {
2966
+ this.reason = "component-params";
2967
+ return;
2968
+ }
2476
2969
  const scope = this.scopeOf(fn);
2477
- if (!scope) return;
2970
+ if (!scope) {
2971
+ this.reason = "not-a-function";
2972
+ return;
2973
+ }
2478
2974
  let statements = [];
2479
2975
  let root;
2480
2976
  if (fn.body.type === "BlockStatement") {
2481
2977
  const body = fn.body.body;
2482
2978
  const last = body.at(-1);
2483
- if (last?.type !== "ReturnStatement" || !last.argument) return;
2979
+ if (last?.type !== "ReturnStatement" || !last.argument) {
2980
+ this.reason = "component-body";
2981
+ return;
2982
+ }
2484
2983
  const rest = [];
2485
2984
  for (const statement of body.slice(0, -1)) {
2486
- if (typeof ("directive" in statement ? statement.directive : null) === "string" || containsReturn(statement)) return;
2985
+ if (typeof ("directive" in statement ? statement.directive : null) === "string") {
2986
+ this.reason = "component-directive";
2987
+ return;
2988
+ }
2989
+ if (containsReturn(statement)) {
2990
+ this.reason = "component-return";
2991
+ return;
2992
+ }
2487
2993
  rest.push(statement);
2488
2994
  }
2489
2995
  statements = rest;
2490
2996
  root = unwrap(last.argument);
2491
2997
  } else root = unwrap(fn.body);
2492
- if (root.type !== "JSXElement" && root.type !== "JSXFragment") return;
2493
- if (usesFunctionContext(this.context, fn.body)) return;
2998
+ if (root.type !== "JSXElement" && root.type !== "JSXFragment") {
2999
+ this.reason = "component-body";
3000
+ return;
3001
+ }
3002
+ if (usesFunctionContext(this.context, fn.body)) {
3003
+ this.reason = "component-this";
3004
+ return;
3005
+ }
2494
3006
  const param = fn.params.at(0);
2495
3007
  let paramBinding;
2496
3008
  if (param) {
2497
- if (param.type !== "Identifier") return;
3009
+ if (param.type !== "Identifier") {
3010
+ this.reason = "component-params";
3011
+ return;
3012
+ }
2498
3013
  paramBinding = scope.bindings.get(param.name);
2499
- if (!paramBinding || paramBinding.mutated) return;
3014
+ if (!paramBinding || paramBinding.mutated) {
3015
+ this.reason = "props-use";
3016
+ return;
3017
+ }
2500
3018
  }
2501
3019
  const views = this.analyzeViews(fn, scope, statements, paramBinding);
2502
- if (!views) return;
3020
+ if (!views) {
3021
+ this.reason = "view-use";
3022
+ return;
3023
+ }
2503
3024
  const props = [];
2504
3025
  for (const reference of paramBinding?.references ?? []) {
2505
3026
  if (views.arguments.has(reference)) continue;
2506
3027
  const read = this.propRead(fn, reference);
2507
- if (!read) return;
3028
+ if (!read) {
3029
+ this.reason = "props-use";
3030
+ return;
3031
+ }
2508
3032
  props.push(read);
2509
3033
  }
2510
3034
  statements = statements.filter((statement) => !views.declarations.includes(statement));
@@ -2518,7 +3042,10 @@ var Inliner = class {
2518
3042
  const outer = [];
2519
3043
  for (const [reference, target] of this.context.scopes.references) {
2520
3044
  if (reference.start < fn.start || reference.end > fn.end) continue;
2521
- if (target === binding) return;
3045
+ if (target === binding) {
3046
+ this.reason = "component-recursive";
3047
+ return;
3048
+ }
2522
3049
  if (target && isWithinScope(target.scope, scope)) continue;
2523
3050
  if (reference.type === "Identifier" || reference.type === "JSXIdentifier") outer.push({
2524
3051
  name: reference.name,
@@ -2566,9 +3093,9 @@ var Inliner = class {
2566
3093
  return false;
2567
3094
  }
2568
3095
  /**
2569
- * Finds the `mergeProps()` and `splitProps()` views of the props declared
2570
- * in a component's statements, and how each is used. A view can only be
2571
- * read as `view.name`, spread onto an element, or passed to another view.
3096
+ * Finds the `merge()` and `omit()` views of the props declared in a
3097
+ * component's statements, and how each is used. A view can only be read
3098
+ * as `view.name`, spread onto an element, or passed to another view.
2572
3099
  * `undefined` when a view is used any other way.
2573
3100
  */
2574
3101
  analyzeViews(fn, scope, statements, param) {
@@ -2577,39 +3104,44 @@ var Inliner = class {
2577
3104
  const declarations = [];
2578
3105
  for (const statement of statements) {
2579
3106
  const declarator = statement.type === "VariableDeclaration" ? statement.declarations.at(0) : void 0;
2580
- if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || !declarator?.init) continue;
3107
+ if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || declarator?.id.type !== "Identifier" || !declarator.init) continue;
2581
3108
  const init = unwrap(declarator.init);
2582
- if (init.type !== "CallExpression") continue;
2583
- const callee = solidCallee(this.context, init);
2584
- let declared;
2585
- if (callee === "mergeProps" && declarator.id.type === "Identifier") {
2586
- const view = this.viewOf(init, param, views, args);
2587
- declared = view ? [[declarator.id, view]] : view;
2588
- } else if (callee === "splitProps" && declarator.id.type === "ArrayPattern") declared = this.splitViews(init, declarator.id.elements, param, views, args);
2589
- else continue;
2590
- if (declared === null) continue;
2591
- if (!declared) return;
2592
- for (const [identifier, view] of declared) {
2593
- if (identifier?.type !== "Identifier") continue;
2594
- const binding = scope.bindings.get(identifier.name);
2595
- if (!binding || binding.mutated) return;
2596
- views.set(binding, view);
2597
- }
3109
+ const callee = init.type === "CallExpression" ? solidCallee(this.context, init) : void 0;
3110
+ if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
3111
+ const view = this.viewOf(callee, init, param, views, args);
3112
+ if (view === null) continue;
3113
+ const binding = scope.bindings.get(declarator.id.name);
3114
+ if (!view || !binding || binding.mutated) return;
3115
+ views.set(binding, view);
2598
3116
  declarations.push(statement);
2599
3117
  }
2600
3118
  const reads = [];
2601
3119
  const spreads = [];
3120
+ const whole = { sources: [{
3121
+ defaults: void 0,
3122
+ hidden: /* @__PURE__ */ new Set()
3123
+ }] };
3124
+ for (const reference of param?.references ?? []) {
3125
+ const spread = this.spreadOnElement(reference);
3126
+ if (spread) {
3127
+ args.add(reference);
3128
+ spreads.push({
3129
+ attribute: spread.attribute,
3130
+ element: spread.element,
3131
+ view: whole,
3132
+ repeated: this.isRepeated(fn, spread.attribute)
3133
+ });
3134
+ }
3135
+ }
2602
3136
  for (const [binding, view] of views) for (const reference of binding.references) {
2603
3137
  if (args.has(reference)) continue;
2604
- const parent = this.context.parents.get(reference);
2605
- const opening = parent ? this.context.parents.get(parent) : void 0;
2606
- const element = opening ? this.context.parents.get(opening) : void 0;
2607
- if (parent?.type === "JSXSpreadAttribute" && opening?.type === "JSXOpeningElement" && element?.type === "JSXElement") {
3138
+ const spread = this.spreadOnElement(reference);
3139
+ if (spread) {
2608
3140
  spreads.push({
2609
- attribute: parent,
2610
- element,
3141
+ attribute: spread.attribute,
3142
+ element: spread.element,
2611
3143
  view,
2612
- repeated: this.isRepeated(fn, parent)
3144
+ repeated: this.isRepeated(fn, spread.attribute)
2613
3145
  });
2614
3146
  continue;
2615
3147
  }
@@ -2628,36 +3160,60 @@ var Inliner = class {
2628
3160
  spreads
2629
3161
  };
2630
3162
  }
2631
- /**
2632
- * The sources a view argument stands for: the props, a view, or a nested
2633
- * `mergeProps()` call.
2634
- */
2635
- viewSources(argument, param, views, args) {
2636
- if (argument.type === "CallExpression") {
2637
- if (solidCallee(this.context, argument) !== "mergeProps") return;
2638
- return this.viewOf(argument, param, views, args)?.sources;
2639
- }
2640
- if (argument.type !== "Identifier") return;
2641
- const binding = this.context.scopes.references.get(argument);
2642
- if (binding && binding === param) return [{
2643
- defaults: void 0,
2644
- hidden: /* @__PURE__ */ new Set(),
2645
- only: void 0
2646
- }];
2647
- return binding ? views.get(binding)?.sources : void 0;
3163
+ /** The spread a reference is the whole argument of, and the element it is on. */
3164
+ spreadOnElement(reference) {
3165
+ const parent = this.context.parents.get(reference);
3166
+ const opening = parent ? this.context.parents.get(parent) : void 0;
3167
+ const element = opening ? this.context.parents.get(opening) : void 0;
3168
+ return parent?.type === "JSXSpreadAttribute" && opening?.type === "JSXOpeningElement" && element?.type === "JSXElement" ? {
3169
+ attribute: parent,
3170
+ element
3171
+ } : void 0;
2648
3172
  }
2649
3173
  /**
2650
- * The view a `mergeProps()` call makes. `null` when it does not take the
2651
- * props or another view, and `undefined` when it does but its other
2652
- * arguments are not literal objects.
3174
+ * The view a `merge()` or `omit()` call makes. `null` when it does not
3175
+ * take the props or another view, and `undefined` when it does but its
3176
+ * other arguments are not literal.
2653
3177
  */
2654
- viewOf(call, param, views, args) {
2655
- if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && this.viewSources(unwrap(argument), param, views, /* @__PURE__ */ new Set()) !== void 0)) return null;
3178
+ viewOf(callee, call, param, views, args) {
3179
+ const sourcesOf = (argument) => {
3180
+ if (argument.type === "CallExpression") {
3181
+ const nested = solidCallee(this.context, argument);
3182
+ if (nested !== "merge" && nested !== "omit") return;
3183
+ return this.viewOf(nested, argument, param, views, args)?.sources;
3184
+ }
3185
+ if (argument.type !== "Identifier") return;
3186
+ const binding = this.context.scopes.references.get(argument);
3187
+ if (binding && binding === param) return [{
3188
+ defaults: void 0,
3189
+ hidden: /* @__PURE__ */ new Set()
3190
+ }];
3191
+ return binding ? views.get(binding)?.sources : void 0;
3192
+ };
3193
+ if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && sourcesOf(unwrap(argument)) !== void 0)) return null;
3194
+ if (callee === "omit") {
3195
+ const first = call.arguments.at(0);
3196
+ const keys = call.arguments.slice(1);
3197
+ if (first === void 0 || first.type === "SpreadElement") return;
3198
+ const target = unwrap(first);
3199
+ const base = sourcesOf(target);
3200
+ const hidden = [];
3201
+ for (const key of keys) {
3202
+ if (key.type !== "Literal" || typeof key.value !== "string") return;
3203
+ hidden.push(key.value);
3204
+ }
3205
+ if (!base) return;
3206
+ args.add(target);
3207
+ return { sources: base.map((source) => ({
3208
+ defaults: source.defaults,
3209
+ hidden: /* @__PURE__ */ new Set([...source.hidden, ...hidden])
3210
+ })) };
3211
+ }
2656
3212
  const sources = [];
2657
3213
  for (const argument of call.arguments) {
2658
3214
  if (argument.type === "SpreadElement") return;
2659
3215
  const inner = unwrap(argument);
2660
- const existing = this.viewSources(inner, param, views, args);
3216
+ const existing = sourcesOf(inner);
2661
3217
  if (existing) {
2662
3218
  args.add(inner);
2663
3219
  sources.push(...existing);
@@ -2678,52 +3234,12 @@ var Inliner = class {
2678
3234
  }
2679
3235
  sources.push({
2680
3236
  defaults,
2681
- hidden: /* @__PURE__ */ new Set(),
2682
- only: void 0
3237
+ hidden: /* @__PURE__ */ new Set()
2683
3238
  });
2684
3239
  }
2685
3240
  return { sources };
2686
3241
  }
2687
3242
  /**
2688
- * The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
2689
- * each with its identifier. A key belongs to the first group that names
2690
- * it, and the last view holds the keys no group names. `null` when the
2691
- * call does not take the props or a view.
2692
- */
2693
- splitViews(call, elements, param, views, args) {
2694
- const first = call.arguments.at(0);
2695
- if (first === void 0 || first.type === "SpreadElement") return null;
2696
- const target = unwrap(first);
2697
- const base = this.viewSources(target, param, views, args);
2698
- if (!base) return null;
2699
- args.add(target);
2700
- const groups = [];
2701
- for (const group of call.arguments.slice(1)) {
2702
- if (group.type !== "ArrayExpression") return;
2703
- const keys = [];
2704
- for (const key of group.elements) {
2705
- if (key?.type !== "Literal" || typeof key.value !== "string") return;
2706
- keys.push(key.value);
2707
- }
2708
- groups.push(keys);
2709
- }
2710
- if (elements.length > groups.length + 1 || elements.some((element) => element?.type === "RestElement")) return;
2711
- const claimed = /* @__PURE__ */ new Set();
2712
- const filters = groups.map((keys) => {
2713
- const owned = keys.filter((key) => !claimed.has(key));
2714
- for (const key of owned) claimed.add(key);
2715
- return new Set(owned);
2716
- });
2717
- return elements.map((element, index) => {
2718
- const only = filters.at(index);
2719
- return [element, { sources: base.map((source) => ({
2720
- defaults: source.defaults,
2721
- hidden: only ? source.hidden : /* @__PURE__ */ new Set([...source.hidden, ...claimed]),
2722
- only: only ? new Set([...only].filter((key) => source.only === void 0 || source.only.has(key))) : source.only
2723
- })) }];
2724
- });
2725
- }
2726
- /**
2727
3243
  * Whether a function is a component, which Solid runs untracked. Only a
2728
3244
  * capitalized function bound at its declaration counts.
2729
3245
  */
@@ -2736,6 +3252,41 @@ var Inliner = class {
2736
3252
  return parent?.type === "VariableDeclarator" && parent.id.type === "Identifier" && isCapitalized(parent.id.name);
2737
3253
  }
2738
3254
  /**
3255
+ * Whether a function runs untracked, once each time the JSX it returns is
3256
+ * created, so it can take a component's statements. See "Where a component
3257
+ * body can go" above.
3258
+ */
3259
+ isHostFunction(fn) {
3260
+ if (this.isComponentFunction(fn)) return true;
3261
+ if (fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression" || fn.async || fn.generator) return false;
3262
+ let node = fn;
3263
+ let parent = this.context.parents.get(node);
3264
+ while (parent?.type === "ParenthesizedExpression") {
3265
+ node = parent;
3266
+ parent = this.context.parents.get(node);
3267
+ }
3268
+ if (parent?.type === "CallExpression") return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === "render";
3269
+ const element = parent?.type === "JSXExpressionContainer" ? this.context.parents.get(parent) : void 0;
3270
+ if (element?.type !== "JSXElement") return false;
3271
+ const significant = element.children.filter((child) => !isInsignificant(child));
3272
+ if (significant.length !== 1 || significant.at(0) !== parent) return false;
3273
+ switch (this.solidCallee(element.openingElement.name)) {
3274
+ case "For":
3275
+ case "Repeat": return true;
3276
+ case "Show":
3277
+ case "Match": return fn.params.length > 0;
3278
+ default: return false;
3279
+ }
3280
+ }
3281
+ /** The Solid export a callee or tag refers to. */
3282
+ solidCallee(name) {
3283
+ if (name.type !== "Identifier" && name.type !== "JSXIdentifier") return;
3284
+ const { references } = this.context.scopes;
3285
+ if (!references.has(name)) return;
3286
+ const binding = references.get(name);
3287
+ return binding ? solidExport(binding, this.context.options) : void 0;
3288
+ }
3289
+ /**
2739
3290
  * Where the statements of a component created at `element` can go: the
2740
3291
  * returned JSX of a component, with only intrinsic elements, fragments,
2741
3292
  * and closed providers between it and the call.
@@ -2747,7 +3298,7 @@ var Inliner = class {
2747
3298
  if (!parent) return;
2748
3299
  if (parent.type === "JSXElement") {
2749
3300
  const { name } = parent.openingElement;
2750
- if (!(this.transparent.has(parent) || this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && isIntrinsicTag(name.name))) return;
3301
+ if (!(this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && (isIntrinsicTag(name.name) || this.transparent.has(parent)))) return;
2751
3302
  const child = node;
2752
3303
  if (!parent.children.some((item) => item === child)) return;
2753
3304
  } else if (parent.type === "ReturnStatement") {
@@ -2755,7 +3306,7 @@ var Inliner = class {
2755
3306
  if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
2756
3307
  let fn = this.context.parents.get(list);
2757
3308
  while (fn && !isFunctionNode(fn)) fn = this.context.parents.get(fn);
2758
- if (!fn || !this.isComponentFunction(fn)) return;
3309
+ if (!fn || !this.isHostFunction(fn)) return;
2759
3310
  return {
2760
3311
  fn,
2761
3312
  host: {
@@ -2764,7 +3315,7 @@ var Inliner = class {
2764
3315
  }
2765
3316
  };
2766
3317
  } else if (parent.type === "ArrowFunctionExpression") {
2767
- if (parent.body !== node || !this.isComponentFunction(parent)) return;
3318
+ if (parent.body !== node || !this.isHostFunction(parent)) return;
2768
3319
  return {
2769
3320
  fn: parent,
2770
3321
  host: {
@@ -2805,6 +3356,29 @@ var Inliner = class {
2805
3356
  };
2806
3357
  }
2807
3358
  /**
3359
+ * How a `ref` from the call site reads in a copy. Solid passes a `ref` to
3360
+ * a component as a function that sets the call site's ref to the element
3361
+ * it is given. The component applies it with its own `ref`, which runs
3362
+ * while the element is made, so the call site's `ref` can move to that
3363
+ * element. Every read has to be the whole `ref` of an element or of
3364
+ * another component, or a key of a spread, which becomes one.
3365
+ */
3366
+ refValue(attribute, reads) {
3367
+ const value = attribute?.value;
3368
+ if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return;
3369
+ for (const read of reads) {
3370
+ if (!isMemberRead(read)) continue;
3371
+ const container = this.context.parents.get(read.member);
3372
+ const owner = container ? this.context.parents.get(container) : void 0;
3373
+ if (container?.type !== "JSXExpressionContainer" || owner?.type !== "JSXAttribute" || owner.name.type !== "JSXIdentifier" || owner.name.name !== "ref") return;
3374
+ }
3375
+ return {
3376
+ kind: "expression",
3377
+ text: textOf(this.context, value.expression),
3378
+ primary: true
3379
+ };
3380
+ }
3381
+ /**
2808
3382
  * How a prop reads in a copy of the component, and the `const` that stores
2809
3383
  * it first when the value has to be evaluated once.
2810
3384
  */
@@ -2885,16 +3459,17 @@ var Inliner = class {
2885
3459
  default: return false;
2886
3460
  }
2887
3461
  }
2888
- freshName(base) {
3462
+ freshName(name) {
2889
3463
  const names = this.scratchNames ?? this.context.scopes.names;
3464
+ const base = name.replace(/\$\d+$/, "");
2890
3465
  let index = 1;
2891
- let name = `${base}$${String(index)}`;
2892
- while (names.has(name)) {
3466
+ let fresh = `${base}$${String(index)}`;
3467
+ while (names.has(fresh)) {
2893
3468
  index += 1;
2894
- name = `${base}$${String(index)}`;
3469
+ fresh = `${base}$${String(index)}`;
2895
3470
  }
2896
- names.add(name);
2897
- return name;
3471
+ names.add(fresh);
3472
+ return fresh;
2898
3473
  }
2899
3474
  /**
2900
3475
  * The copy of `component` that would replace `element`, or `undefined`
@@ -2902,13 +3477,28 @@ var Inliner = class {
2902
3477
  */
2903
3478
  prepare(component, element) {
2904
3479
  const attributes = attributesOf(element);
2905
- if (!attributes) return;
3480
+ if (!attributes) {
3481
+ this.reason = callSiteReason(element);
3482
+ return;
3483
+ }
2906
3484
  const hasChildren = element.children.some((child) => !isInsignificant(child));
2907
- if (hasChildren && attributes.has("children")) return;
2908
- for (const attribute of attributes.values()) if (attribute.value && usesFunctionContext(this.context, attribute.value)) return;
2909
- for (const child of element.children) if (usesFunctionContext(this.context, child)) return;
2910
- const views = this.resolveViews(component, element, attributes, hasChildren);
2911
- if (!views) return;
3485
+ if (hasChildren && attributes.has("children")) {
3486
+ this.reason = "call-children";
3487
+ return;
3488
+ }
3489
+ for (const attribute of attributes.values()) if (attribute.value && usesFunctionContext(this.context, attribute.value)) {
3490
+ this.reason = "call-this";
3491
+ return;
3492
+ }
3493
+ for (const child of element.children) if (usesFunctionContext(this.context, child)) {
3494
+ this.reason = "call-this";
3495
+ return;
3496
+ }
3497
+ const views = this.resolveViews(component, attributes, hasChildren);
3498
+ if (!views) {
3499
+ this.reason = "view-conflict";
3500
+ return;
3501
+ }
2912
3502
  const snapshots = [];
2913
3503
  const fresh = (base) => this.freshName(base);
2914
3504
  const readsByName = /* @__PURE__ */ new Map();
@@ -2918,23 +3508,35 @@ var Inliner = class {
2918
3508
  readsByName.set(name, reads);
2919
3509
  };
2920
3510
  for (const read of views.propReads) count(read.name, read);
2921
- for (const read of views.fallbacks.keys()) count(read.name, read);
2922
- for (const spread of views.spreads) for (const [key, resolution] of spread.keys) if (resolution.kind !== "text") count(key, spread.spread);
3511
+ for (const spread of views.spreads) for (const [key, source] of spread.keys) if (source === void 0) count(key, spread.spread);
2923
3512
  const values = /* @__PURE__ */ new Map();
2924
3513
  for (const [name, reads] of readsByName) {
2925
3514
  let value;
2926
3515
  if (name === "children" && !attributes.has("children")) value = this.childrenValue(element, reads, fresh, snapshots);
3516
+ else if (name === "ref" && attributes.has("ref")) value = this.refValue(attributes.get("ref"), reads);
2927
3517
  else value = this.propValue(attributes.get(name), reads, fresh, snapshots);
2928
- if (!value) return;
3518
+ if (!value) {
3519
+ this.reason = name === "ref" && attributes.has("ref") ? "ref-use" : "prop-value";
3520
+ return;
3521
+ }
2929
3522
  values.set(name, value);
2930
3523
  }
2931
3524
  const scope = scopeAt(this.context.scopes, this.context.parents, element);
2932
- for (const reference of component.outer) if (lookup(scope, reference.name) !== reference.binding) return;
3525
+ for (const reference of component.outer) if (lookup(scope, reference.name) !== reference.binding) {
3526
+ this.reason = "shadowed";
3527
+ return;
3528
+ }
2933
3529
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
2934
3530
  const host = hoisted ? this.hostOf(element) : void 0;
2935
- if (hoisted && !host) return;
3531
+ if (hoisted && !host) {
3532
+ this.reason = "no-host";
3533
+ return;
3534
+ }
2936
3535
  const contextTexts = this.contextTexts(component, element);
2937
- if (!contextTexts) return;
3536
+ if (!contextTexts) {
3537
+ this.reason = "context-value";
3538
+ return;
3539
+ }
2938
3540
  const copy = new MagicString(this.context.code);
2939
3541
  this.renameLocals(copy, component, fresh);
2940
3542
  this.substituteProps(copy, views.propReads, values);
@@ -2987,7 +3589,7 @@ var Inliner = class {
2987
3589
  const answering = [];
2988
3590
  const seen = /* @__PURE__ */ new Set();
2989
3591
  for (let node = this.context.parents.get(element); node; node = this.context.parents.get(node)) {
2990
- const target = node.type === "JSXElement" ? providerContext(this.context, node.openingElement.name) : void 0;
3592
+ const target = node.type === "JSXElement" ? contextBinding(this.context, node.openingElement.name) : void 0;
2991
3593
  if (node.type !== "JSXElement" || !target || seen.has(target)) continue;
2992
3594
  seen.add(target);
2993
3595
  const entry = this.transparent.get(node);
@@ -3054,68 +3656,44 @@ var Inliner = class {
3054
3656
  return changed;
3055
3657
  }
3056
3658
  /**
3057
- * Resolves each view read and spread of a component at a call site.
3058
- * `mergeProps()` reads the last source whose value is not `undefined`, so
3059
- * a prop that can be `undefined` falls back to the default behind it. A
3060
- * spread becomes the keys it holds, and its `children` become the
3061
- * element's children. `undefined` when a spread repeats an attribute, or
3062
- * holds `children` for an element that has some.
3659
+ * Resolves each view read and spread of a component at a call site. A
3660
+ * read becomes a read of the props, or the text of a default. A spread
3661
+ * becomes the keys it holds, in the order `merge()` gives them, and its
3662
+ * `children` become the element's children. `undefined` when a spread
3663
+ * repeats an attribute, or holds `children` for an element that has some.
3063
3664
  */
3064
- resolveViews(component, element, attributes, hasChildren) {
3665
+ resolveViews(component, attributes, hasChildren) {
3065
3666
  const has = (key) => attributes.has(key) || key === "children" && hasChildren;
3066
- const defined = (key) => {
3067
- const attribute = attributes.get(key);
3068
- if (attribute) {
3069
- const { value } = attribute;
3070
- if (value?.type !== "JSXExpressionContainer") return true;
3071
- return value.expression.type !== "JSXEmptyExpression" && isDefinedExpression(value.expression);
3072
- }
3073
- const significant = element.children.filter((child) => !isInsignificant(child));
3074
- const only = significant.at(0);
3075
- if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return true;
3076
- return only.expression.type !== "JSXEmptyExpression" && isDefinedExpression(only.expression);
3077
- };
3078
3667
  const resolve = (view, key) => {
3079
- const sources = view.sources.filter((source) => allows(source, key));
3080
- for (let index = sources.length - 1; index >= 0; index -= 1) {
3081
- const source = sources.at(index);
3082
- const text = source?.defaults?.get(key);
3083
- if (text !== void 0) return {
3084
- kind: "text",
3085
- text
3086
- };
3087
- if (source && !source.defaults && has(key)) {
3088
- if (defined(key)) return { kind: "props" };
3089
- const fallback = sources.slice(0, index).findLast((earlier) => earlier.defaults?.has(key))?.defaults?.get(key);
3090
- return fallback === void 0 ? { kind: "props" } : {
3091
- kind: "fallback",
3092
- text: fallback
3093
- };
3094
- }
3668
+ for (let index = view.sources.length - 1; index >= 0; index -= 1) {
3669
+ const source = view.sources.at(index);
3670
+ if (!source || source.hidden.has(key)) continue;
3671
+ const text = source.defaults?.get(key);
3672
+ if (text !== void 0) return text;
3673
+ if (!source.defaults && has(key)) return;
3095
3674
  }
3096
- return {
3097
- kind: "text",
3098
- text: "void 0"
3099
- };
3675
+ return "void 0";
3100
3676
  };
3101
3677
  const propReads = [...component.props];
3102
- const fallbacks = /* @__PURE__ */ new Map();
3103
3678
  const texts = /* @__PURE__ */ new Map();
3104
3679
  for (const read of component.viewReads) {
3105
- const resolution = resolve(read.view, read.name);
3106
- if (resolution.kind === "props") propReads.push(read);
3107
- else if (resolution.kind === "fallback") fallbacks.set(read, resolution.text);
3108
- else texts.set(read.member, resolution.text);
3680
+ const text = resolve(read.view, read.name);
3681
+ if (text === void 0) propReads.push(read);
3682
+ else texts.set(read.member, text);
3109
3683
  }
3110
3684
  const callKeys = [...attributes.keys()];
3111
3685
  if (hasChildren && !attributes.has("children")) callKeys.push("children");
3112
3686
  const spreads = [];
3113
3687
  const taken = /* @__PURE__ */ new Map();
3114
3688
  for (const spread of component.viewSpreads) {
3115
- const order = /* @__PURE__ */ new Set();
3689
+ const order = [];
3116
3690
  for (const source of spread.view.sources) {
3117
3691
  const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
3118
- for (const key of keys.filter((name) => allows(source, name))) order.add(key);
3692
+ for (const key of keys.filter((name) => !source.hidden.has(name))) {
3693
+ const index = order.indexOf(key);
3694
+ if (index !== -1) order.splice(index, 1);
3695
+ order.push(key);
3696
+ }
3119
3697
  }
3120
3698
  let names = taken.get(spread.element);
3121
3699
  if (!names) {
@@ -3125,10 +3703,9 @@ var Inliner = class {
3125
3703
  }
3126
3704
  const keys = /* @__PURE__ */ new Map();
3127
3705
  for (const key of order) {
3128
- const resolution = resolve(spread.view, key);
3129
- if (!(key === "children" ? spread.element.openingElement.selfClosing && resolution.kind !== "fallback" : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
3706
+ if (!(key === "children" ? spread.element.openingElement.selfClosing : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
3130
3707
  names.add(key);
3131
- keys.set(key, resolution);
3708
+ keys.set(key, resolve(spread.view, key));
3132
3709
  }
3133
3710
  spreads.push({
3134
3711
  spread,
@@ -3137,7 +3714,6 @@ var Inliner = class {
3137
3714
  }
3138
3715
  return {
3139
3716
  propReads,
3140
- fallbacks,
3141
3717
  texts,
3142
3718
  spreads
3143
3719
  };
@@ -3151,23 +3727,13 @@ var Inliner = class {
3151
3727
  const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
3152
3728
  copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
3153
3729
  }
3154
- for (const [read, fallback] of views.fallbacks) {
3155
- const text = values.get(read.name)?.text ?? "void 0";
3156
- copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
3157
- }
3158
3730
  for (const { spread, keys } of views.spreads) {
3159
3731
  const parts = [];
3160
3732
  let children;
3161
- for (const [key, resolution] of keys) {
3733
+ for (const [key, text] of keys) {
3162
3734
  const value = values.get(key);
3163
- if (key === "children") {
3164
- children = this.spreadChildren(element, resolution, value);
3165
- continue;
3166
- }
3167
- let text = value?.text ?? "void 0";
3168
- if (resolution.kind === "text") text = resolution.text;
3169
- else if (resolution.kind === "fallback") text = withFallback(text, resolution.text);
3170
- parts.push(`${key}={${text}}`);
3735
+ if (key === "children") children = this.spreadChildren(element, text, value);
3736
+ else parts.push(`${key}={${text ?? value?.text ?? "void 0"}}`);
3171
3737
  }
3172
3738
  const { attribute } = spread;
3173
3739
  if (parts.length === 0) copy.remove(attribute.start, attribute.end);
@@ -3185,8 +3751,8 @@ var Inliner = class {
3185
3751
  * The JSX children a spread's `children` becomes: the call site's own
3186
3752
  * children, or the value of a single expression or default.
3187
3753
  */
3188
- spreadChildren(element, resolution, value) {
3189
- if (resolution.kind === "text") return resolution.text === "void 0" ? void 0 : `{${resolution.text}}`;
3754
+ spreadChildren(element, text, value) {
3755
+ if (text !== void 0) return text === "void 0" ? void 0 : `{${text}}`;
3190
3756
  if (!value) return;
3191
3757
  if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
3192
3758
  return `{${value.text}}`;
@@ -3347,7 +3913,7 @@ var Inliner = class {
3347
3913
  */
3348
3914
  sizeHeader() {
3349
3915
  if (this.header === void 0) {
3350
- const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration").map((statement) => this.context.code.slice(statement.start, statement.end));
3916
+ const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration" || isDataDeclaration(statement)).map((statement) => this.context.code.slice(statement.start, statement.end));
3351
3917
  const aliases = [...this.context.options.builtInAliases.keys()];
3352
3918
  if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
3353
3919
  this.header = imports.join("\n");
@@ -3356,11 +3922,18 @@ var Inliner = class {
3356
3922
  }
3357
3923
  run() {
3358
3924
  const components = [];
3925
+ const rejected = /* @__PURE__ */ new Map();
3359
3926
  for (const binding of this.context.scopes.root.bindings.values()) {
3927
+ this.reason = "not-a-function";
3360
3928
  const component = this.analyzeComponent(binding);
3361
3929
  if (component) components.push(component);
3930
+ else rejected.set(binding, this.reason);
3931
+ }
3932
+ const siteReasons = /* @__PURE__ */ new Map();
3933
+ if (components.length === 0) {
3934
+ this.report(rejected, siteReasons);
3935
+ return;
3362
3936
  }
3363
- if (components.length === 0) return;
3364
3937
  const sites = [];
3365
3938
  const otherUses = /* @__PURE__ */ new Map();
3366
3939
  for (const component of components) {
@@ -3391,12 +3964,16 @@ var Inliner = class {
3391
3964
  const elements = inlined.get(site.component) ?? [];
3392
3965
  elements.push(site.element);
3393
3966
  inlined.set(site.component, elements);
3394
- } else otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
3967
+ } else {
3968
+ otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
3969
+ if (this.context.skips && !postponed) siteReasons.set(site.element, this.siteReason(site, worthwhile));
3970
+ }
3395
3971
  if (done || postponed) {
3396
3972
  for (const component of components) if (isWithin(site.element, component.fn)) changedBodies.add(component);
3397
3973
  }
3398
3974
  }
3399
3975
  this.writeHosts();
3976
+ this.report(rejected, siteReasons);
3400
3977
  const copied = [...inlined.keys()];
3401
3978
  for (const [component, elements] of inlined) {
3402
3979
  const insideCopy = elements.some((element) => copied.some((other) => other !== component && element.start >= other.fn.start && element.end <= other.fn.end));
@@ -3404,6 +3981,65 @@ var Inliner = class {
3404
3981
  }
3405
3982
  }
3406
3983
  /**
3984
+ * Why a call site of a component was not inlined. A component that is
3985
+ * not worth inlining is only turned down for its size when the call site
3986
+ * could be inlined.
3987
+ */
3988
+ siteReason(site, worthwhile) {
3989
+ if (worthwhile.has(site.component)) return this.reason;
3990
+ this.scratchNames = new Set(this.context.scopes.names);
3991
+ try {
3992
+ return this.prepare(site.component, site.element) ? "size" : this.reason;
3993
+ } finally {
3994
+ this.scratchNames = void 0;
3995
+ }
3996
+ }
3997
+ /**
3998
+ * Records each component call that stays a call, and why. Solid's
3999
+ * built-ins and context providers are left out. The provider pass
4000
+ * reports providers.
4001
+ */
4002
+ report(rejected, siteReasons) {
4003
+ if (!this.context.skips) return;
4004
+ const visit = (node) => {
4005
+ if (node.type === "JSXElement") {
4006
+ const reason = this.callReason(node, rejected, siteReasons);
4007
+ if (reason) recordSkip(this.context, "inline", reason.reason, reason.name);
4008
+ }
4009
+ forEachChild(node, visit);
4010
+ };
4011
+ visit(this.context.program);
4012
+ }
4013
+ callReason(element, rejected, siteReasons) {
4014
+ const tag = element.openingElement.name;
4015
+ if (tag.type === "JSXMemberExpression") return {
4016
+ reason: "member-tag",
4017
+ name: textOf(this.context, tag)
4018
+ };
4019
+ if (tag.type !== "JSXIdentifier" || !isCapitalized(tag.name)) return;
4020
+ const { name } = tag;
4021
+ const binding = this.context.scopes.references.get(tag);
4022
+ const identity = binding ? solidExport(binding, this.context.options) : name;
4023
+ if (identity !== void 0 && this.context.options.builtIns.has(identity) || contextBinding(this.context, tag)) return;
4024
+ if (!binding) return {
4025
+ reason: "global",
4026
+ name
4027
+ };
4028
+ if (binding.kind === "import") return {
4029
+ reason: "imported",
4030
+ name: `${name} (${(binding.source ?? "").replaceAll(/!~\{\w+\}~/g, "*")})`
4031
+ };
4032
+ if (binding.scope.parent !== void 0) return {
4033
+ reason: "nested",
4034
+ name
4035
+ };
4036
+ const reason = rejected.get(binding) ?? siteReasons.get(element);
4037
+ return reason === void 0 ? void 0 : {
4038
+ reason,
4039
+ name
4040
+ };
4041
+ }
4042
+ /**
3407
4043
  * The whitespace before `position` on its line.
3408
4044
  */
3409
4045
  indentationAt(position) {
@@ -3438,7 +4074,7 @@ const TRACKED_PROPS = /* @__PURE__ */ new Map([
3438
4074
  ["Show", "when"],
3439
4075
  ["Match", "when"],
3440
4076
  ["For", "each"],
3441
- ["Index", "each"]
4077
+ ["Repeat", "count"]
3442
4078
  ]);
3443
4079
  /**
3444
4080
  * Whether evaluating `node` always creates a new value, which reference
@@ -3464,8 +4100,19 @@ function isFresh(node) {
3464
4100
  }
3465
4101
  }
3466
4102
  /**
3467
- * The expression a compute function returns, when that is all it does.
4103
+ * The text of an expression that always gives the same primitive: a literal
4104
+ * other than a regular expression, a template literal without expressions,
4105
+ * or a negated number.
3468
4106
  */
4107
+ function constantText(context, node) {
4108
+ const expression = unwrap(node);
4109
+ switch (expression.type) {
4110
+ case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
4111
+ case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
4112
+ case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
4113
+ default: return;
4114
+ }
4115
+ }
3469
4116
  function returnedExpression(fn) {
3470
4117
  if (fn.async || fn.generator || fn.params.length > 0 || !fn.body) return;
3471
4118
  if (fn.body.type !== "BlockStatement") return fn.body;
@@ -3478,35 +4125,91 @@ var MemoInliner = class {
3478
4125
  constructor(context) {
3479
4126
  this.context = context;
3480
4127
  this.changed = false;
4128
+ this.reason = "";
3481
4129
  }
3482
- isCreateMemo(callee) {
3483
- if (callee.type !== "Identifier") return false;
4130
+ /** Whether a callee is Solid's `createMemo` or `children`. */
4131
+ memoKind(callee) {
4132
+ if (callee.type !== "Identifier") return;
3484
4133
  const binding = this.context.scopes.references.get(callee);
3485
- return binding !== void 0 && solidExport(binding, this.context.options) === "createMemo";
4134
+ const identity = binding ? solidExport(binding, this.context.options) : void 0;
4135
+ return identity === "createMemo" || identity === "children" ? identity : void 0;
3486
4136
  }
3487
4137
  /**
3488
- * The computation of a memo whose result is never equal to its last one.
4138
+ * The expression a memo's computation returns, and whether `equals: false`
4139
+ * makes it notify on every run.
3489
4140
  */
3490
- unstableComputation(declarator) {
4141
+ computation(declarator) {
3491
4142
  const init = declarator.init ? unwrap(declarator.init) : void 0;
3492
- if (init?.type !== "CallExpression" || !this.isCreateMemo(init.callee)) return;
4143
+ const kind = init?.type === "CallExpression" ? this.memoKind(init.callee) : void 0;
4144
+ if (init?.type !== "CallExpression" || kind === void 0) return;
3493
4145
  const args = init.arguments;
4146
+ if (kind === "children" && args.length > 1) {
4147
+ this.reason = "memo-options";
4148
+ return;
4149
+ }
3494
4150
  const compute = args.at(0);
3495
- const initialValue = args.at(1);
3496
- const options = args.at(2);
3497
- if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) return;
4151
+ const options = args.at(1);
4152
+ if (args.length > 2 || compute === void 0 || compute.type === "SpreadElement") {
4153
+ this.reason = "memo-computation";
4154
+ return;
4155
+ }
3498
4156
  const fn = unwrap(compute);
3499
- if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return;
4157
+ if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") {
4158
+ this.reason = "memo-computation";
4159
+ return;
4160
+ }
3500
4161
  const expression = returnedExpression(fn);
3501
- if (!expression) return;
4162
+ if (!expression) {
4163
+ this.reason = "memo-computation";
4164
+ return;
4165
+ }
3502
4166
  let alwaysNotifies = false;
3503
4167
  if (options !== void 0) {
3504
- if (options.type !== "ObjectExpression" || options.properties.length !== 1) return;
4168
+ if (options.type !== "ObjectExpression" || options.properties.length !== 1) {
4169
+ this.reason = "memo-options";
4170
+ return;
4171
+ }
3505
4172
  const property = options.properties.at(0);
3506
- if (property?.type !== "Property" || property.computed || property.key.type !== "Identifier" || property.key.name !== "equals" || property.value.type !== "Literal" || property.value.value !== false) return;
4173
+ if (property?.type !== "Property" || property.computed || property.key.type !== "Identifier" || property.key.name !== "equals" || property.value.type !== "Literal" || property.value.value !== false) {
4174
+ this.reason = "memo-options";
4175
+ return;
4176
+ }
3507
4177
  alwaysNotifies = true;
3508
4178
  }
3509
- return alwaysNotifies || isFresh(expression) ? expression : void 0;
4179
+ return {
4180
+ expression,
4181
+ alwaysNotifies,
4182
+ kind
4183
+ };
4184
+ }
4185
+ /**
4186
+ * Replaces every read of a memo whose computation is a literal with the
4187
+ * literal. Each reference has to be a call after the declaration, since a
4188
+ * call before it would throw.
4189
+ */
4190
+ inlineConstant(declarator, binding, text) {
4191
+ const statement = this.context.parents.get(declarator);
4192
+ if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
4193
+ this.reason = "memo-declaration";
4194
+ return;
4195
+ }
4196
+ const calls = [];
4197
+ for (const reference of binding.references) {
4198
+ const call = this.context.parents.get(reference);
4199
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
4200
+ this.reason = "memo-constant";
4201
+ return;
4202
+ }
4203
+ calls.push(call);
4204
+ }
4205
+ for (const call of calls) {
4206
+ const parent = this.context.parents.get(call);
4207
+ const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
4208
+ this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
4209
+ }
4210
+ this.context.s.remove(statement.start, statement.end);
4211
+ this.changed = true;
4212
+ return true;
3510
4213
  }
3511
4214
  /**
3512
4215
  * The function a node runs in.
@@ -3516,6 +4219,12 @@ var MemoInliner = class {
3516
4219
  while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
3517
4220
  return current;
3518
4221
  }
4222
+ /** The JSX expression container a call is the whole of. */
4223
+ containerOf(call) {
4224
+ let parent = this.context.parents.get(call);
4225
+ while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
4226
+ return parent?.type === "JSXExpressionContainer" ? parent : void 0;
4227
+ }
3519
4228
  /**
3520
4229
  * Whether a call is the whole of an expression that Solid tracks, and that
3521
4230
  * runs once each time `fn` runs.
@@ -3601,17 +4310,59 @@ var MemoInliner = class {
3601
4310
  return true;
3602
4311
  }
3603
4312
  tryInline(declarator, binding) {
3604
- const expression = this.unstableComputation(declarator);
3605
- if (!expression || binding.references.length !== 1) return;
4313
+ this.reason = "";
4314
+ if (this.inline(declarator, binding) === void 0 && this.reason !== "") recordSkip(this.context, "memos", this.reason, binding.name);
4315
+ }
4316
+ inline(declarator, binding) {
4317
+ const found = this.computation(declarator);
4318
+ if (!found) return;
4319
+ const { expression, alwaysNotifies, kind } = found;
4320
+ if (kind === "children") {
4321
+ const root = unwrap(expression);
4322
+ if (root.type !== "JSXElement" && root.type !== "JSXFragment") {
4323
+ this.reason = "children-dynamic";
4324
+ return;
4325
+ }
4326
+ } else {
4327
+ const constant = constantText(this.context, expression);
4328
+ if (constant !== void 0) return this.inlineConstant(declarator, binding, constant);
4329
+ if (!alwaysNotifies && !isFresh(expression)) return;
4330
+ }
4331
+ if (binding.references.length !== 1) {
4332
+ this.reason = "memo-reads";
4333
+ return;
4334
+ }
3606
4335
  const statement = this.context.parents.get(declarator);
3607
4336
  const fn = this.functionOf(declarator);
3608
- if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) return;
4337
+ if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) {
4338
+ this.reason = "memo-declaration";
4339
+ return;
4340
+ }
3609
4341
  const reference = binding.references.at(0);
3610
4342
  const call = reference ? this.context.parents.get(reference) : void 0;
3611
- if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || !this.isSingleTrackedRead(call, fn) || !this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) return;
3612
- this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
4343
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) {
4344
+ this.reason = "memo-read";
4345
+ return;
4346
+ }
4347
+ if (!this.isSingleTrackedRead(call, fn)) {
4348
+ this.reason = "memo-read-place";
4349
+ return;
4350
+ }
4351
+ const container = this.containerOf(call);
4352
+ const owner = container ? this.context.parents.get(container) : void 0;
4353
+ if (kind === "children" && (!container || owner?.type !== "JSXElement" && owner?.type !== "JSXFragment")) {
4354
+ this.reason = "memo-read-place";
4355
+ return;
4356
+ }
4357
+ if (!this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) {
4358
+ this.reason = "memo-scope";
4359
+ return;
4360
+ }
4361
+ if (kind === "children" && container) this.context.s.overwrite(container.start, container.end, textOf(this.context, expression));
4362
+ else this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
3613
4363
  this.context.s.remove(statement.start, statement.end);
3614
4364
  this.changed = true;
4365
+ return true;
3615
4366
  }
3616
4367
  run() {
3617
4368
  for (const scope of new Set(this.context.scopes.scopes.values())) {
@@ -3632,181 +4383,20 @@ function inlineMemos(context) {
3632
4383
  return inliner.changed;
3633
4384
  }
3634
4385
  //#endregion
3635
- //#region src/server.ts
3636
- /** Calls the server does not run. */
3637
- const REMOVED = /* @__PURE__ */ new Set(["createEffect", "onMount"]);
3638
- /** Calls the server runs once, in place. */
3639
- const RUN_ONCE = /* @__PURE__ */ new Set(["createRenderEffect", "createComputed"]);
3640
- /**
3641
- * Whether a statement starting with `text` could join the statement before
3642
- * it when that one has no semicolon.
3643
- */
3644
- function continuesPreviousStatement(text) {
3645
- return /^[([`+\-/]/.test(text);
3646
- }
3647
- var ServerSimplifier = class {
3648
- constructor(context) {
3649
- this.context = context;
3650
- this.changed = false;
3651
- }
3652
- /**
3653
- * The Solid export a callee refers to, through a named import, an alias,
3654
- * or a namespace import like `solid.untrack`.
3655
- */
3656
- calleeName(callee) {
3657
- const { references } = this.context.scopes;
3658
- if (callee.type === "Identifier") {
3659
- const binding = references.get(callee);
3660
- return binding ? solidExport(binding, this.context.options) : void 0;
3661
- }
3662
- if (callee.type !== "MemberExpression" || callee.object.type !== "Identifier") return;
3663
- const binding = references.get(callee.object);
3664
- if (binding?.kind !== "import" || binding.imported !== "*" || binding.source === void 0 || !this.context.options.moduleSources.includes(binding.source)) return;
3665
- if (!callee.computed && callee.property.type === "Identifier") return callee.property.name;
3666
- if (callee.computed && callee.property.type === "Literal" && typeof callee.property.value === "string") return callee.property.value;
3667
- }
3668
- /**
3669
- * Whether dropping `node` loses nothing observable. Solid's `on` only
3670
- * creates a function, so `createEffect(on(source, fn))` can go too.
3671
- */
3672
- isDiscardable(node) {
3673
- if (isSideEffectFree(node)) return true;
3674
- const inner = node.type === "ParenthesizedExpression" ? unwrap(node) : node;
3675
- return inner.type === "CallExpression" && this.calleeName(inner.callee) === "on" && inner.arguments.every((argument) => argument.type !== "SpreadElement" && this.isDiscardable(argument));
3676
- }
3677
- /**
3678
- * The text of calling `fn` with `args`, the way Solid's server calls it,
3679
- * and whether it binds as tightly as a call does.
3680
- */
3681
- callText(fn, args) {
3682
- const inner = unwrap(fn);
3683
- const argsText = args.map((argument) => textOf(this.context, argument)).join(", ");
3684
- if (inner.type === "ArrowFunctionExpression" && inner.expression && inner.params.length === 0 && args.length === 0 && inner.body.type !== "BlockStatement") {
3685
- const { body } = inner;
3686
- return {
3687
- text: textOf(this.context, body),
3688
- call: isPrimary(body) || body.type === "CallExpression"
3689
- };
3690
- }
3691
- const text = textOf(this.context, fn);
3692
- if (inner.type === "Identifier") return {
3693
- text: `${text}(${argsText})`,
3694
- call: true
3695
- };
3696
- if (inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression" || fn.type === "ParenthesizedExpression") return {
3697
- text: `(${text})(${argsText})`,
3698
- call: true
3699
- };
3700
- return {
3701
- text: `(0, ${text})(${argsText})`,
3702
- call: true
3703
- };
3704
- }
3705
- /**
3706
- * Whether a statement at `position` could be read as a continuation of
3707
- * the one before it, which happens when that one has no semicolon.
3708
- */
3709
- followsOpenStatement(position) {
3710
- const before = this.context.code.slice(0, position).trimEnd();
3711
- return before !== "" && !before.endsWith(";") && !before.endsWith("{");
3712
- }
3713
- replace(call, text, primary = false) {
3714
- const parent = this.context.parents.get(call);
3715
- if (parent?.type === "ExpressionStatement") {
3716
- if (text === void 0) {
3717
- const list = this.context.parents.get(parent)?.type;
3718
- if (list === "Program" || list === "BlockStatement" || list === "StaticBlock" || list === "SwitchCase") this.context.s.remove(parent.start, parent.end);
3719
- else this.context.s.overwrite(parent.start, parent.end, ";");
3720
- } else {
3721
- let statement = text;
3722
- if (/^(\{|function\b|async function\b|class\b)/.test(statement)) statement = `(${statement})`;
3723
- const guarded = continuesPreviousStatement(statement) && this.followsOpenStatement(parent.start);
3724
- this.context.s.overwrite(call.start, call.end, guarded ? `;${statement}` : statement);
3725
- }
3726
- } else {
3727
- const value = text ?? "void 0";
3728
- const bare = !(parent?.type === "NewExpression" && parent.callee === call) && (primary || acceptsAnyExpression(this.context, call));
3729
- this.context.s.overwrite(call.start, call.end, bare ? value : `(${value})`);
3730
- }
3731
- this.changed = true;
3732
- }
3733
- simplify(call) {
3734
- const name = this.calleeName(call.callee);
3735
- if (name === void 0 || call.arguments.some((arg) => arg.type === "SpreadElement")) return;
3736
- const args = call.arguments.filter((arg) => arg.type !== "SpreadElement");
3737
- const first = args.at(0);
3738
- const second = args.at(1);
3739
- const rest = args.slice(2);
3740
- const inStatement = this.context.parents.get(call)?.type === "ExpressionStatement";
3741
- if (REMOVED.has(name)) {
3742
- if (args.every((arg) => this.isDiscardable(arg))) this.replace(call, void 0);
3743
- return;
3744
- }
3745
- switch (name) {
3746
- case "getListener":
3747
- if (args.length === 0) this.replace(call, "null");
3748
- return;
3749
- case "untrack":
3750
- case "batch":
3751
- if (first && args.length === 1) {
3752
- const run = this.callText(first, []);
3753
- this.replace(call, run.text, run.call);
3754
- }
3755
- return;
3756
- case "startTransition":
3757
- if (first && args.length === 1) {
3758
- const { text } = this.callText(first, []);
3759
- this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
3760
- }
3761
- return;
3762
- case "createDeferred":
3763
- if (first && args.slice(1).every((arg) => this.isDiscardable(arg))) {
3764
- const text = textOf(this.context, first);
3765
- this.replace(call, text, isPrimary(first));
3766
- }
3767
- return;
3768
- case "createMemo":
3769
- if (first && rest.every((arg) => this.isDiscardable(arg))) {
3770
- const { text } = this.callText(first, second ? [second] : []);
3771
- this.replace(call, `((value) => () => value)(${text})`, true);
3772
- }
3773
- return;
3774
- default: if (RUN_ONCE.has(name) && first && rest.length === 0) {
3775
- const { text } = this.callText(first, second ? [second] : []);
3776
- this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
3777
- }
3778
- }
3779
- }
3780
- walk(node) {
3781
- forEachChild(node, (child) => {
3782
- this.walk(child);
3783
- });
3784
- if (node.type === "CallExpression") this.simplify(node);
3785
- }
3786
- };
3787
- /**
3788
- * Runs the server pass over a parsed program. Returns whether anything changed.
3789
- */
3790
- function simplifyServer(context) {
3791
- const simplifier = new ServerSimplifier(context);
3792
- simplifier.walk(context.program);
3793
- return simplifier.changed;
3794
- }
3795
- //#endregion
3796
4386
  //#region src/compile.ts
3797
4387
  const DEFAULT_BUILT_INS = [
3798
4388
  "For",
3799
4389
  "Show",
3800
4390
  "Switch",
3801
4391
  "Match",
3802
- "Suspense",
3803
- "SuspenseList",
4392
+ "Loading",
4393
+ "Reveal",
3804
4394
  "Portal",
3805
- "Index",
4395
+ "Repeat",
3806
4396
  "Dynamic",
3807
- "ErrorBoundary"
4397
+ "Errored"
3808
4398
  ];
3809
- const DEFAULT_MODULE_SOURCES = ["solid-js", "solid-js/web"];
4399
+ const DEFAULT_MODULE_SOURCES = ["solid-js", "@solidjs/web"];
3810
4400
  function resolveOptions(options) {
3811
4401
  return {
3812
4402
  builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
@@ -3814,21 +4404,35 @@ function resolveOptions(options) {
3814
4404
  builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
3815
4405
  constantVars: options.constantVars ?? false,
3816
4406
  importedConstants: resolveImportedConstants(options.importedConstants),
4407
+ globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
3817
4408
  importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
3818
4409
  sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
3819
4410
  alwaysInline: options.alwaysInline ?? false
3820
4411
  };
3821
4412
  }
3822
- function runPass(code, filename, options, sourceMap, pass) {
4413
+ function parseCode(code, filename) {
3823
4414
  const program = parse(filename, code);
4415
+ return {
4416
+ code,
4417
+ program,
4418
+ parents: collectParents(program),
4419
+ scopes: analyzeScopes(program)
4420
+ };
4421
+ }
4422
+ function runPass(parsed, filename, options, sourceMap, pass, skips) {
4423
+ const { code } = parsed;
3824
4424
  const context = {
3825
4425
  code,
3826
4426
  filename,
3827
4427
  s: new MagicString(code),
3828
- program,
3829
- parents: collectParents(program),
3830
- scopes: analyzeScopes(program),
3831
- options
4428
+ program: parsed.program,
4429
+ parents: parsed.parents,
4430
+ scopes: {
4431
+ ...parsed.scopes,
4432
+ names: new Set(parsed.scopes.names)
4433
+ },
4434
+ options,
4435
+ skips
3832
4436
  };
3833
4437
  if (!pass(context)) return;
3834
4438
  return {
@@ -3862,11 +4466,18 @@ function compile(code, options = {}) {
3862
4466
  if (options.memos ?? true) passes.push(inlineMemos);
3863
4467
  const maps = [];
3864
4468
  let current = code;
4469
+ let parsed;
4470
+ const parsedCurrent = () => {
4471
+ if (parsed?.code !== current) parsed = parseCode(current, filename);
4472
+ return parsed;
4473
+ };
3865
4474
  const maxPasses = options.maxPasses ?? 10;
4475
+ let skipped;
3866
4476
  for (let round = 0; round < maxPasses; round += 1) {
3867
4477
  let changed = false;
4478
+ skipped = options.report === true ? [] : void 0;
3868
4479
  for (const pass of passes) {
3869
- const result = runPass(current, filename, resolved, sourceMap, pass);
4480
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, pass, skipped);
3870
4481
  if (result) {
3871
4482
  changed = true;
3872
4483
  current = result.code;
@@ -3875,27 +4486,23 @@ function compile(code, options = {}) {
3875
4486
  }
3876
4487
  if (!changed) break;
3877
4488
  }
3878
- if (options.server ?? false) {
3879
- const result = runPass(current, filename, resolved, sourceMap, simplifyServer);
3880
- if (result) {
3881
- current = result.code;
3882
- if (result.map !== void 0) maps.push(result.map);
3883
- }
3884
- }
4489
+ const report = skipped ? { skipped } : {};
3885
4490
  if (maps.length === 0) return {
3886
4491
  code: current,
3887
- map: null
4492
+ map: null,
4493
+ ...report
3888
4494
  };
3889
4495
  return {
3890
4496
  code: current,
3891
- map: remapping(maps.reverse(), () => null)
4497
+ map: remapping(maps.reverse(), () => null),
4498
+ ...report
3892
4499
  };
3893
4500
  }
3894
4501
  /**
3895
4502
  * Reads what a module imports and the constants it exports, so that
3896
4503
  * `compile` can fold them in the modules that import it.
3897
4504
  *
3898
- * Only `filename` and `importedConstants` of the options are used.
4505
+ * Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
3899
4506
  */
3900
4507
  function readModuleConstants(code, options = {}) {
3901
4508
  const program = parse(options.filename ?? "input.jsx", code);
@@ -3905,6 +4512,6 @@ function readModuleConstants(code, options = {}) {
3905
4512
  };
3906
4513
  }
3907
4514
  //#endregion
3908
- export { lookup as a, forEachChild as c, analyzeScopes as i, parse as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, unwrap as u };
4515
+ export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
3909
4516
 
3910
- //# sourceMappingURL=compile-ocDYvJUW.mjs.map
4517
+ //# sourceMappingURL=compile-Cz6hbiQv.mjs.map