solid-optimizer 0.4.1 → 1.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -47
- package/dist/{compile-BQuBDA0Q.d.mts → compile-BRCj4UN7.d.mts} +15 -22
- package/dist/compile-BRCj4UN7.d.mts.map +1 -0
- package/dist/{compile-CqhOfBvG.mjs → compile-BpoiBa6b.mjs} +943 -670
- package/dist/compile-BpoiBa6b.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +3 -10
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +305 -204
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -11
- package/src/compile.ts +16 -40
- package/src/constants.ts +42 -2
- package/src/context.ts +3 -0
- package/src/copy-size.ts +73 -0
- package/src/fold.ts +619 -17
- package/src/inline.ts +352 -292
- package/src/memo.ts +161 -30
- package/src/provider.ts +40 -75
- package/src/scope.ts +14 -1
- package/src/size.ts +35 -75
- package/src/vite/globals.ts +67 -0
- package/src/vite/index.ts +279 -264
- package/src/vite/inject.ts +14 -4
- package/src/vite/runtime.ts +23 -3
- package/dist/compile-BQuBDA0Q.d.mts.map +0 -1
- package/dist/compile-CqhOfBvG.mjs.map +0 -1
- package/src/server.ts +0 -275
|
@@ -435,6 +435,7 @@ var Analyzer = class {
|
|
|
435
435
|
this.reference(node, false);
|
|
436
436
|
break;
|
|
437
437
|
case "VariableDeclaration": {
|
|
438
|
+
if (node.declare) break;
|
|
438
439
|
let kind = "other";
|
|
439
440
|
if (node.kind === "var" || node.kind === "let" || node.kind === "const") kind = node.kind;
|
|
440
441
|
for (const declarator of node.declarations) {
|
|
@@ -444,10 +445,12 @@ var Analyzer = class {
|
|
|
444
445
|
break;
|
|
445
446
|
}
|
|
446
447
|
case "FunctionDeclaration":
|
|
447
|
-
case "TSDeclareFunction":
|
|
448
448
|
if (node.id) this.declare(node.id, "function", node);
|
|
449
449
|
this.visitFunction(node);
|
|
450
450
|
break;
|
|
451
|
+
case "TSDeclareFunction":
|
|
452
|
+
this.visitFunction(node);
|
|
453
|
+
break;
|
|
451
454
|
case "FunctionExpression":
|
|
452
455
|
this.visitFunction(node);
|
|
453
456
|
break;
|
|
@@ -461,6 +464,7 @@ var Analyzer = class {
|
|
|
461
464
|
});
|
|
462
465
|
break;
|
|
463
466
|
case "ClassDeclaration":
|
|
467
|
+
if (node.declare) break;
|
|
464
468
|
if (node.id) this.declare(node.id, "class", node);
|
|
465
469
|
this.visitClass(node);
|
|
466
470
|
break;
|
|
@@ -822,12 +826,36 @@ var ConstantTable = class {
|
|
|
822
826
|
this.constants = /* @__PURE__ */ new Map();
|
|
823
827
|
this.evaluating = /* @__PURE__ */ new Set();
|
|
824
828
|
this.lookup = (reference) => {
|
|
825
|
-
if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference);
|
|
829
|
+
if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference) ?? this.globalMember(scopes, reference);
|
|
826
830
|
if (!scopes.references.has(reference) || reference.type !== "Identifier") return;
|
|
827
831
|
const binding = scopes.references.get(reference);
|
|
828
|
-
|
|
832
|
+
if (binding) return this.constantOf(binding);
|
|
833
|
+
return this.globalConstant(reference.name) ?? globalValue(reference.name);
|
|
829
834
|
};
|
|
830
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
|
+
}
|
|
831
859
|
/**
|
|
832
860
|
* The value of `ns.NAME`, where `ns` is `import * as ns` from a module
|
|
833
861
|
* whose constants are known.
|
|
@@ -1059,6 +1087,189 @@ function innerText(s, node) {
|
|
|
1059
1087
|
return s.slice(node.openingElement.end, node.closingElement.start);
|
|
1060
1088
|
}
|
|
1061
1089
|
//#endregion
|
|
1090
|
+
//#region src/size.ts
|
|
1091
|
+
/** A template's declaration, and the function that clones and returns it. */
|
|
1092
|
+
const TEMPLATE = 40;
|
|
1093
|
+
/** A walk to an element, like `e.firstChild.nextSibling`. */
|
|
1094
|
+
const WALK = 12;
|
|
1095
|
+
/** An `insert()` of a dynamic child. */
|
|
1096
|
+
const INSERT = 12;
|
|
1097
|
+
/** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
|
|
1098
|
+
const EFFECT = 24;
|
|
1099
|
+
/** An event handler assignment, like `e.$$click = ...`. */
|
|
1100
|
+
const HANDLER = 10;
|
|
1101
|
+
/** A `spread()` of props onto an element. */
|
|
1102
|
+
const SPREAD = 16;
|
|
1103
|
+
/** A `createComponent(Name, {})` call. */
|
|
1104
|
+
const COMPONENT = 8;
|
|
1105
|
+
/** A getter, `get name() { return ... }`. */
|
|
1106
|
+
const GETTER = 16;
|
|
1107
|
+
/** A `mergeProps()` around the props of a component with a spread. */
|
|
1108
|
+
const MERGE = 6;
|
|
1109
|
+
/**
|
|
1110
|
+
* The length of `text` once a minifier removes the whitespace it does not need.
|
|
1111
|
+
*/
|
|
1112
|
+
function compactLength(text) {
|
|
1113
|
+
return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
|
|
1114
|
+
}
|
|
1115
|
+
function nameLength(name) {
|
|
1116
|
+
switch (name.type) {
|
|
1117
|
+
case "JSXIdentifier": return name.name.length;
|
|
1118
|
+
case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
|
|
1119
|
+
case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
|
|
1120
|
+
default: return 4;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
/** Whether an expression lowers to text in the template, like a literal. */
|
|
1124
|
+
function isStaticText(node) {
|
|
1125
|
+
if (node.type === "JSXEmptyExpression") return false;
|
|
1126
|
+
const inner = unwrap(node);
|
|
1127
|
+
if (inner.type === "Literal") return typeof inner.value === "string" || typeof inner.value === "number";
|
|
1128
|
+
if (inner.type === "TemplateLiteral") return inner.expressions.length === 0;
|
|
1129
|
+
return inner.type === "UnaryExpression" && inner.operator === "-" && inner.argument.type === "Literal" && typeof inner.argument.value === "number";
|
|
1130
|
+
}
|
|
1131
|
+
function isIntrinsic(element) {
|
|
1132
|
+
const { name } = element.openingElement;
|
|
1133
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
1134
|
+
}
|
|
1135
|
+
function attributeName(attribute) {
|
|
1136
|
+
const { name } = attribute;
|
|
1137
|
+
return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Estimates lowered sizes in one piece of code.
|
|
1141
|
+
*/
|
|
1142
|
+
var SizeEstimator = class {
|
|
1143
|
+
constructor(code) {
|
|
1144
|
+
this.code = code;
|
|
1145
|
+
}
|
|
1146
|
+
text(node) {
|
|
1147
|
+
return compactLength(this.code.slice(node.start, node.end));
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* The size of any node: JSX as lowered, other code as its compact text
|
|
1151
|
+
* with the JSX in it as lowered.
|
|
1152
|
+
*/
|
|
1153
|
+
node(node) {
|
|
1154
|
+
if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
|
|
1155
|
+
let size = this.text(node);
|
|
1156
|
+
const visit = (child) => {
|
|
1157
|
+
if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
1158
|
+
size += this.jsx(child, false) - this.text(child);
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
forEachChild(child, visit);
|
|
1162
|
+
};
|
|
1163
|
+
forEachChild(node, visit);
|
|
1164
|
+
return size;
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* The size of a JSX element or fragment. `inTemplate` is whether it sits
|
|
1168
|
+
* directly in an element, whose template it joins.
|
|
1169
|
+
*/
|
|
1170
|
+
jsx(node, inTemplate) {
|
|
1171
|
+
if (node.type === "JSXFragment") return this.children(node.children) + 2;
|
|
1172
|
+
if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
|
|
1173
|
+
const tag = nameLength(node.openingElement.name);
|
|
1174
|
+
let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
|
|
1175
|
+
let dynamic = false;
|
|
1176
|
+
for (const attribute of node.openingElement.attributes) {
|
|
1177
|
+
const cost = this.elementAttribute(attribute);
|
|
1178
|
+
size += cost.size;
|
|
1179
|
+
dynamic ||= cost.dynamic;
|
|
1180
|
+
}
|
|
1181
|
+
for (const child of node.children) {
|
|
1182
|
+
if (isInsignificant(child)) continue;
|
|
1183
|
+
if (child.type === "JSXText") size += compactLength(child.value);
|
|
1184
|
+
else if (child.type === "JSXExpressionContainer" && isStaticText(child.expression)) size += this.text(child.expression);
|
|
1185
|
+
else if (child.type === "JSXElement") size += this.jsx(child, true);
|
|
1186
|
+
else {
|
|
1187
|
+
size += INSERT + this.child(child);
|
|
1188
|
+
dynamic = true;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
return size + (dynamic ? WALK : 0);
|
|
1192
|
+
}
|
|
1193
|
+
elementAttribute(attribute) {
|
|
1194
|
+
if (attribute.type === "JSXSpreadAttribute") return {
|
|
1195
|
+
size: SPREAD + this.node(attribute.argument),
|
|
1196
|
+
dynamic: true
|
|
1197
|
+
};
|
|
1198
|
+
const name = attributeName(attribute);
|
|
1199
|
+
const { value } = attribute;
|
|
1200
|
+
if (!value || value.type === "Literal") return {
|
|
1201
|
+
size: name.length + (value ? this.text(value) : 0) + 1,
|
|
1202
|
+
dynamic: false
|
|
1203
|
+
};
|
|
1204
|
+
if (value.type !== "JSXExpressionContainer") return {
|
|
1205
|
+
size: name.length + 1 + this.jsx(value, false),
|
|
1206
|
+
dynamic: true
|
|
1207
|
+
};
|
|
1208
|
+
const { expression } = value;
|
|
1209
|
+
if (expression.type === "JSXEmptyExpression") return {
|
|
1210
|
+
size: 0,
|
|
1211
|
+
dynamic: false
|
|
1212
|
+
};
|
|
1213
|
+
const inner = unwrap(expression);
|
|
1214
|
+
if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
|
|
1215
|
+
size: name.length + this.text(inner) + 1,
|
|
1216
|
+
dynamic: false
|
|
1217
|
+
};
|
|
1218
|
+
return {
|
|
1219
|
+
size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
|
|
1220
|
+
dynamic: true
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
child(child) {
|
|
1224
|
+
if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
|
|
1225
|
+
if (child.type === "JSXSpreadChild") return this.node(child.expression);
|
|
1226
|
+
if (child.type === "JSXText") return compactLength(child.value) + 2;
|
|
1227
|
+
return this.jsx(child, false);
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Children outside a template, which lower to an array of values.
|
|
1231
|
+
*/
|
|
1232
|
+
children(children) {
|
|
1233
|
+
let size = 0;
|
|
1234
|
+
for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
|
|
1235
|
+
return size;
|
|
1236
|
+
}
|
|
1237
|
+
component(element) {
|
|
1238
|
+
let size = COMPONENT + nameLength(element.openingElement.name);
|
|
1239
|
+
let spread = false;
|
|
1240
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1241
|
+
if (attribute.type === "JSXSpreadAttribute") {
|
|
1242
|
+
spread = true;
|
|
1243
|
+
size += this.node(attribute.argument) + 1;
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
const name = attributeName(attribute);
|
|
1247
|
+
const { value } = attribute;
|
|
1248
|
+
if (!value || value.type === "Literal") size += name.length + 1 + (value ? this.text(value) : 4);
|
|
1249
|
+
else if (value.type === "JSXExpressionContainer") {
|
|
1250
|
+
const { expression } = value;
|
|
1251
|
+
if (expression.type !== "JSXEmptyExpression") {
|
|
1252
|
+
const inner = unwrap(expression);
|
|
1253
|
+
const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
1254
|
+
size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
|
|
1255
|
+
}
|
|
1256
|
+
} else size += name.length + GETTER + this.jsx(value, false);
|
|
1257
|
+
}
|
|
1258
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
1259
|
+
if (significant.length > 0) size += 24 + this.children(significant);
|
|
1260
|
+
return size + (spread ? MERGE : 0);
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* The size of what a component returns. JSX lowers as JSX, and anything
|
|
1264
|
+
* else is inserted when it sits in an element.
|
|
1265
|
+
*/
|
|
1266
|
+
root(node, inTemplate) {
|
|
1267
|
+
const inner = unwrap(node);
|
|
1268
|
+
if (inner.type === "JSXElement" || inner.type === "JSXFragment") return this.jsx(inner, inTemplate);
|
|
1269
|
+
return (inTemplate ? 24 : 0) + this.node(inner);
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
//#endregion
|
|
1062
1273
|
//#region src/fold.ts
|
|
1063
1274
|
/**
|
|
1064
1275
|
* The control-flow components this pass resolves.
|
|
@@ -1067,7 +1278,7 @@ function innerText(s, node) {
|
|
|
1067
1278
|
const FOLDABLE_FLOW = /* @__PURE__ */ new Set([
|
|
1068
1279
|
"Show",
|
|
1069
1280
|
"For",
|
|
1070
|
-
"
|
|
1281
|
+
"Repeat",
|
|
1071
1282
|
"Switch",
|
|
1072
1283
|
"Dynamic"
|
|
1073
1284
|
]);
|
|
@@ -1128,9 +1339,51 @@ function containsHoistedDeclaration(statement) {
|
|
|
1128
1339
|
* Whether a statement starting with `text` could join the statement before
|
|
1129
1340
|
* it when that one has no semicolon.
|
|
1130
1341
|
*/
|
|
1131
|
-
function continuesPreviousStatement
|
|
1342
|
+
function continuesPreviousStatement(text) {
|
|
1132
1343
|
return /^[([`+\-/]/.test(text);
|
|
1133
1344
|
}
|
|
1345
|
+
/** The name a property key or a computed member's string literal gives. */
|
|
1346
|
+
function keyName(node) {
|
|
1347
|
+
if (node.type === "Identifier") return node.name;
|
|
1348
|
+
return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* The text of an expression that always gives the same primitive: a literal
|
|
1352
|
+
* other than a regular expression, a template literal without expressions,
|
|
1353
|
+
* or a negated number.
|
|
1354
|
+
*/
|
|
1355
|
+
function primitiveText(context, node) {
|
|
1356
|
+
let expression = node;
|
|
1357
|
+
while (expression.type === "ParenthesizedExpression") expression = expression.expression;
|
|
1358
|
+
switch (expression.type) {
|
|
1359
|
+
case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
|
|
1360
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
|
|
1361
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
|
|
1362
|
+
default: return;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
/** Whether a child of `parent` is inserted into its template, not passed as a prop. */
|
|
1366
|
+
function joinsTemplate(parent) {
|
|
1367
|
+
if (parent.type === "JSXFragment") return true;
|
|
1368
|
+
const { name } = parent.openingElement;
|
|
1369
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Whether JSX holds a `ref` or a spread, which run while the JSX is made.
|
|
1373
|
+
*/
|
|
1374
|
+
function hasRefOrSpread(root) {
|
|
1375
|
+
let found = false;
|
|
1376
|
+
const visit = (node) => {
|
|
1377
|
+
if (found) return;
|
|
1378
|
+
if (node.type === "JSXSpreadAttribute" || node.type === "JSXSpreadChild" || node.type === "JSXAttribute" && node.name.type === "JSXIdentifier" && node.name.name === "ref") {
|
|
1379
|
+
found = true;
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
forEachChild(node, visit);
|
|
1383
|
+
};
|
|
1384
|
+
visit(root);
|
|
1385
|
+
return found;
|
|
1386
|
+
}
|
|
1134
1387
|
var Folder = class {
|
|
1135
1388
|
constructor(context) {
|
|
1136
1389
|
this.context = context;
|
|
@@ -1289,6 +1542,26 @@ var Folder = class {
|
|
|
1289
1542
|
if (identity === void 0 || !this.context.options.builtIns.has(identity)) return;
|
|
1290
1543
|
return identity;
|
|
1291
1544
|
}
|
|
1545
|
+
/**
|
|
1546
|
+
* The name of the component an identifier refers to, when it is a
|
|
1547
|
+
* capitalized top-level binding nothing reassigns, holding a function or a
|
|
1548
|
+
* `lazy` component. A JSX tag with that name calls the same component. An
|
|
1549
|
+
* import could hold anything, such as a tag name string, so it is left out.
|
|
1550
|
+
*/
|
|
1551
|
+
componentName(identifier) {
|
|
1552
|
+
const binding = this.context.scopes.references.get(identifier);
|
|
1553
|
+
if (!binding || binding.mutated || binding.scope.parent !== void 0 || !/^[A-Z]/.test(binding.name)) return;
|
|
1554
|
+
const { declaration } = binding;
|
|
1555
|
+
if (declaration.type === "FunctionDeclaration") return declaration.async || declaration.generator ? void 0 : binding.name;
|
|
1556
|
+
if (declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
|
|
1557
|
+
let init = declaration.init;
|
|
1558
|
+
while (init.type === "ParenthesizedExpression") init = init.expression;
|
|
1559
|
+
if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") return init.async || init.generator ? void 0 : binding.name;
|
|
1560
|
+
if (init.type === "CallExpression" && init.callee.type === "Identifier") {
|
|
1561
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
1562
|
+
return callee && solidExport(callee, this.context.options) === "lazy" ? binding.name : void 0;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1292
1565
|
flowTag(element) {
|
|
1293
1566
|
const tag = this.builtInTag(element);
|
|
1294
1567
|
return tag !== void 0 && FOLDABLE_FLOW.has(tag) ? tag : void 0;
|
|
@@ -1298,8 +1571,8 @@ var Folder = class {
|
|
|
1298
1571
|
if (tag === void 0 || hasSpreadAttribute(element)) return;
|
|
1299
1572
|
switch (tag) {
|
|
1300
1573
|
case "Show": return this.foldShow(element);
|
|
1301
|
-
case "For":
|
|
1302
|
-
case "
|
|
1574
|
+
case "For": return this.foldFor(element);
|
|
1575
|
+
case "Repeat": return this.foldRepeat(element);
|
|
1303
1576
|
case "Switch": return this.foldSwitch(element);
|
|
1304
1577
|
case "Dynamic": return this.foldDynamic(element);
|
|
1305
1578
|
default: return;
|
|
@@ -1370,7 +1643,187 @@ var Folder = class {
|
|
|
1370
1643
|
const each = findAttribute(element, "each");
|
|
1371
1644
|
if (each?.value?.type !== "JSXExpressionContainer" || each.value.expression.type === "JSXEmptyExpression") return;
|
|
1372
1645
|
const { expression } = each.value;
|
|
1373
|
-
if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false)
|
|
1646
|
+
if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false)) return this.unrollFor(element, expression);
|
|
1647
|
+
if (!isSideEffectFree(expression)) return;
|
|
1648
|
+
return this.fallback(element);
|
|
1649
|
+
}
|
|
1650
|
+
/**
|
|
1651
|
+
* Unrolls `<For>` over a constant list into one copy of its row per item.
|
|
1652
|
+
*
|
|
1653
|
+
* The list is an array literal, or a binding of one that only `<For>`
|
|
1654
|
+
* reads, so nothing can change it. Each item is a primitive literal, or an
|
|
1655
|
+
* object literal of them. The row is an arrow function whose body is JSX,
|
|
1656
|
+
* and reads the item as `item.key`, or as itself when it is a primitive.
|
|
1657
|
+
* Each row ran once, untracked, under its own owner. A copy is made with
|
|
1658
|
+
* the JSX around it instead, which runs untracked too, and the JSX
|
|
1659
|
+
* transform gives every dynamic part its own effect. A `ref` or a spread
|
|
1660
|
+
* would run while the copy is made, so either one keeps the loop.
|
|
1661
|
+
*/
|
|
1662
|
+
unrollFor(element, expression) {
|
|
1663
|
+
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;
|
|
1664
|
+
const byIndex = findAttribute(element, "keyed") !== void 0;
|
|
1665
|
+
const list = this.constantList(expression);
|
|
1666
|
+
if (!list) return;
|
|
1667
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
1668
|
+
const only = significant.at(0);
|
|
1669
|
+
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return;
|
|
1670
|
+
let row = only.expression;
|
|
1671
|
+
while (row.type === "ParenthesizedExpression") row = row.expression;
|
|
1672
|
+
if (row.type !== "ArrowFunctionExpression" || row.async || !row.expression || row.params.length > 2) return;
|
|
1673
|
+
let body = row.body;
|
|
1674
|
+
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1675
|
+
if (body.type !== "JSXElement" && body.type !== "JSXFragment" || hasRefOrSpread(body)) return;
|
|
1676
|
+
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
1677
|
+
const scope = this.context.scopes.scopes.get(row);
|
|
1678
|
+
if (!scope) return;
|
|
1679
|
+
const params = [];
|
|
1680
|
+
for (const param of row.params) {
|
|
1681
|
+
const binding = param.type === "Identifier" ? scope.bindings.get(param.name) : void 0;
|
|
1682
|
+
if (!binding || binding.mutated) return;
|
|
1683
|
+
params.push(binding);
|
|
1684
|
+
}
|
|
1685
|
+
const itemParam = params.at(0);
|
|
1686
|
+
const indexParam = params.at(1);
|
|
1687
|
+
const rows = [];
|
|
1688
|
+
for (const [index, item] of list.items.entries()) {
|
|
1689
|
+
const copy = new MagicString(this.context.code);
|
|
1690
|
+
if (itemParam && !this.substituteItem(copy, itemParam, item, byIndex) || indexParam && !this.substituteIndex(copy, indexParam, index, !byIndex)) return;
|
|
1691
|
+
rows.push(copy.slice(body.start, body.end));
|
|
1692
|
+
}
|
|
1693
|
+
const text = `<>${rows.join("")}</>`;
|
|
1694
|
+
if (!this.unrollingPays(element, text, list.declaration)) return;
|
|
1695
|
+
return {
|
|
1696
|
+
kind: "jsx",
|
|
1697
|
+
text
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* The items of a constant list, and the declaration that goes once no
|
|
1702
|
+
* `<For>` reads it.
|
|
1703
|
+
*/
|
|
1704
|
+
constantList(expression) {
|
|
1705
|
+
let node = expression;
|
|
1706
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1707
|
+
let declaration;
|
|
1708
|
+
if (node.type === "Identifier") {
|
|
1709
|
+
const binding = this.context.scopes.references.get(node);
|
|
1710
|
+
if (binding?.declaration.type !== "VariableDeclarator" || binding.declaration.id !== binding.identifier || !binding.declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
|
|
1711
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
1712
|
+
if ((statement ? this.context.parents.get(statement) : void 0)?.type === "ExportNamedDeclaration" || !binding.references.every((reference) => this.isForEach(reference))) return;
|
|
1713
|
+
node = binding.declaration.init;
|
|
1714
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1715
|
+
declaration = binding.references.length === 1 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 ? statement : void 0;
|
|
1716
|
+
}
|
|
1717
|
+
if (node.type !== "ArrayExpression") return;
|
|
1718
|
+
const items = [];
|
|
1719
|
+
for (const element of node.elements) {
|
|
1720
|
+
const item = element && element.type !== "SpreadElement" ? this.constantItem(element) : void 0;
|
|
1721
|
+
if (!item) return;
|
|
1722
|
+
items.push(item);
|
|
1723
|
+
}
|
|
1724
|
+
return {
|
|
1725
|
+
items,
|
|
1726
|
+
declaration
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
/** Whether a reference is the whole `each` of a Solid `<For>`. */
|
|
1730
|
+
isForEach(reference) {
|
|
1731
|
+
const container = this.context.parents.get(reference);
|
|
1732
|
+
const attribute = container ? this.context.parents.get(container) : void 0;
|
|
1733
|
+
const opening = attribute ? this.context.parents.get(attribute) : void 0;
|
|
1734
|
+
const element = opening ? this.context.parents.get(opening) : void 0;
|
|
1735
|
+
return container?.type === "JSXExpressionContainer" && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "each" && element?.type === "JSXElement" && this.builtInTag(element) === "For";
|
|
1736
|
+
}
|
|
1737
|
+
/** A list item whose value is known: a primitive literal, or an object literal of them. */
|
|
1738
|
+
constantItem(node) {
|
|
1739
|
+
const primitive = primitiveText(this.context, node);
|
|
1740
|
+
if (primitive !== void 0) return {
|
|
1741
|
+
kind: "primitive",
|
|
1742
|
+
text: primitive
|
|
1743
|
+
};
|
|
1744
|
+
if (node.type !== "ObjectExpression") return;
|
|
1745
|
+
const keys = /* @__PURE__ */ new Map();
|
|
1746
|
+
for (const property of node.properties) {
|
|
1747
|
+
if (property.type !== "Property" || property.kind !== "init" || property.method || property.computed) return;
|
|
1748
|
+
const key = keyName(property.key);
|
|
1749
|
+
const value = primitiveText(this.context, property.value);
|
|
1750
|
+
if (key === void 0 || value === void 0 || key === "__proto__") return;
|
|
1751
|
+
keys.set(key, value);
|
|
1752
|
+
}
|
|
1753
|
+
return {
|
|
1754
|
+
kind: "object",
|
|
1755
|
+
keys
|
|
1756
|
+
};
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Writes an item's value over each read of the row's item parameter.
|
|
1760
|
+
* `false` when a read is not one the value can take the place of.
|
|
1761
|
+
*/
|
|
1762
|
+
substituteItem(copy, param, item, accessor) {
|
|
1763
|
+
for (const reference of param.references) {
|
|
1764
|
+
let read = reference;
|
|
1765
|
+
if (accessor) {
|
|
1766
|
+
const call = this.context.parents.get(reference);
|
|
1767
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
|
|
1768
|
+
read = call;
|
|
1769
|
+
}
|
|
1770
|
+
if (item.kind === "primitive") {
|
|
1771
|
+
copy.overwrite(read.start, read.end, this.valueAt(read, item.text));
|
|
1772
|
+
continue;
|
|
1773
|
+
}
|
|
1774
|
+
const member = this.context.parents.get(read);
|
|
1775
|
+
if (member?.type !== "MemberExpression" || member.object !== read || isWriteTarget(member, this.context.parents)) return false;
|
|
1776
|
+
let key;
|
|
1777
|
+
if (!member.computed) key = member.property.type === "Identifier" ? member.property.name : void 0;
|
|
1778
|
+
else if (member.property.type === "Literal") key = keyName(member.property);
|
|
1779
|
+
const parent = this.context.parents.get(member);
|
|
1780
|
+
if (key === void 0 || parent?.type === "CallExpression" && parent.callee === member || !item.keys.has(key)) return false;
|
|
1781
|
+
copy.overwrite(member.start, member.end, this.valueAt(member, item.keys.get(key) ?? "void 0"));
|
|
1782
|
+
}
|
|
1783
|
+
return true;
|
|
1784
|
+
}
|
|
1785
|
+
/** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
|
|
1786
|
+
valueAt(node, text) {
|
|
1787
|
+
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : `(${text})`;
|
|
1788
|
+
}
|
|
1789
|
+
/** Writes an item's index over each read of the row's index parameter. */
|
|
1790
|
+
substituteIndex(copy, param, index, accessor) {
|
|
1791
|
+
for (const reference of param.references) {
|
|
1792
|
+
let read = reference;
|
|
1793
|
+
if (accessor) {
|
|
1794
|
+
const call = this.context.parents.get(reference);
|
|
1795
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
|
|
1796
|
+
read = call;
|
|
1797
|
+
}
|
|
1798
|
+
copy.overwrite(read.start, read.end, String(index));
|
|
1799
|
+
}
|
|
1800
|
+
return true;
|
|
1801
|
+
}
|
|
1802
|
+
/**
|
|
1803
|
+
* Whether the copies are estimated to be no larger, once lowered, than
|
|
1804
|
+
* the loop and the list it no longer needs.
|
|
1805
|
+
*/
|
|
1806
|
+
unrollingPays(element, text, declaration) {
|
|
1807
|
+
const inTemplate = this.joinsParentTemplate(element);
|
|
1808
|
+
const loop = new SizeEstimator(this.context.code).jsx(element, inTemplate) + (declaration ? compactLength(this.context.code.slice(declaration.start, declaration.end)) : 0);
|
|
1809
|
+
const code = `const __so_rows__ = ${text};`;
|
|
1810
|
+
const statement = parse(this.context.filename, code).body.at(0);
|
|
1811
|
+
const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
|
|
1812
|
+
if (init?.type !== "JSXFragment") return false;
|
|
1813
|
+
const estimator = new SizeEstimator(code);
|
|
1814
|
+
return (inTemplate ? init.children.reduce((size, child) => size + (child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child)), 0) : estimator.jsx(init, false)) <= loop;
|
|
1815
|
+
}
|
|
1816
|
+
/** Whether an element sits directly in an element or fragment whose template it joins. */
|
|
1817
|
+
joinsParentTemplate(element) {
|
|
1818
|
+
const parent = this.context.parents.get(element);
|
|
1819
|
+
return parent?.type === "JSXElement" && joinsTemplate(parent) || parent?.type === "JSXFragment";
|
|
1820
|
+
}
|
|
1821
|
+
foldRepeat(element) {
|
|
1822
|
+
const count = findAttribute(element, "count");
|
|
1823
|
+
if (count?.value?.type !== "JSXExpressionContainer" || count.value.expression.type === "JSXEmptyExpression") return;
|
|
1824
|
+
const value = evaluate(count.value.expression, this.lookup);
|
|
1825
|
+
if (typeof value?.value !== "number") return;
|
|
1826
|
+
if (value.value >= 1) return;
|
|
1374
1827
|
return this.fallback(element);
|
|
1375
1828
|
}
|
|
1376
1829
|
/**
|
|
@@ -1409,17 +1862,27 @@ var Folder = class {
|
|
|
1409
1862
|
* `<Dynamic component="div">` is just `<div>`, which the JSX transform can
|
|
1410
1863
|
* put in a template instead of creating the element at runtime.
|
|
1411
1864
|
*/
|
|
1865
|
+
/**
|
|
1866
|
+
* `<Dynamic component="div">` becomes `<div>`. `<Dynamic component={Card}>`
|
|
1867
|
+
* becomes `<Card>` when `Card` is a component declared at the top level
|
|
1868
|
+
* that nothing reassigns. `<Dynamic>` then only renders `Card` with the
|
|
1869
|
+
* other props, through a memo that never changes.
|
|
1870
|
+
*/
|
|
1412
1871
|
foldDynamic(element) {
|
|
1413
1872
|
const component = findAttribute(element, "component");
|
|
1414
1873
|
if (!component?.value) return;
|
|
1415
1874
|
let tag;
|
|
1416
1875
|
const { value } = component;
|
|
1417
|
-
if (value.type === "Literal") tag = value.value;
|
|
1876
|
+
if (value.type === "Literal") tag = isIntrinsicTag(value.value) ? value.value : void 0;
|
|
1418
1877
|
else if (value.type === "JSXExpressionContainer" && value.expression.type !== "JSXEmptyExpression") {
|
|
1419
|
-
const
|
|
1420
|
-
if (
|
|
1878
|
+
const expression = value.expression.type === "ParenthesizedExpression" ? value.expression.expression : value.expression;
|
|
1879
|
+
if (expression.type === "Identifier") tag = this.componentName(expression);
|
|
1880
|
+
if (tag === void 0) {
|
|
1881
|
+
const result = evaluate(value.expression, this.lookup);
|
|
1882
|
+
if (typeof result?.value === "string" && isIntrinsicTag(result.value)) tag = result.value;
|
|
1883
|
+
}
|
|
1421
1884
|
}
|
|
1422
|
-
if (tag === void 0
|
|
1885
|
+
if (tag === void 0) return;
|
|
1423
1886
|
const attributes = element.openingElement.attributes.filter((attribute) => attribute !== component).map((attribute) => textOf(this.context, attribute));
|
|
1424
1887
|
const opening = [tag, ...attributes].join(" ");
|
|
1425
1888
|
return {
|
|
@@ -1464,6 +1927,12 @@ var Folder = class {
|
|
|
1464
1927
|
pieces.push(splicePiece(this.context.s, child, child.children));
|
|
1465
1928
|
continue;
|
|
1466
1929
|
}
|
|
1930
|
+
const reduced = child.type === "JSXExpressionContainer" && joinsTemplate(parent) ? this.reducedCall(child.expression) : void 0;
|
|
1931
|
+
if (reduced !== void 0) {
|
|
1932
|
+
folded = true;
|
|
1933
|
+
pieces.push(replacedPiece(child, reduced));
|
|
1934
|
+
continue;
|
|
1935
|
+
}
|
|
1467
1936
|
const resolved = child.type === "JSXElement" ? this.foldFlow(child) : void 0;
|
|
1468
1937
|
if (!resolved) {
|
|
1469
1938
|
pieces.push(keptPiece(child));
|
|
@@ -1485,6 +1954,64 @@ var Folder = class {
|
|
|
1485
1954
|
}
|
|
1486
1955
|
if (folded && writePieces(this.context.s, pieces)) this.changed = true;
|
|
1487
1956
|
}
|
|
1957
|
+
/**
|
|
1958
|
+
* The JSX a call like `((n) => <li>{n}</li>)(1)` makes, with each
|
|
1959
|
+
* argument in place of its parameter, or `undefined` when that could
|
|
1960
|
+
* behave differently.
|
|
1961
|
+
*
|
|
1962
|
+
* The child is inserted by an effect that makes the JSX. The JSX
|
|
1963
|
+
* transform wraps every dynamic part of JSX in an effect of its own, so
|
|
1964
|
+
* making it reads nothing the outer effect tracks, and the outer effect
|
|
1965
|
+
* runs once. A `ref` runs while the JSX is made, and a spread reads its
|
|
1966
|
+
* object there, so either one keeps the call. Each argument has to be a
|
|
1967
|
+
* constant or a binding nothing reassigns, so reading it again where the
|
|
1968
|
+
* parameter was gives the same value.
|
|
1969
|
+
*/
|
|
1970
|
+
reducedCall(expression) {
|
|
1971
|
+
if (expression.type !== "CallExpression" || expression.optional) return;
|
|
1972
|
+
let callee = expression.callee;
|
|
1973
|
+
while (callee.type === "ParenthesizedExpression") callee = callee.expression;
|
|
1974
|
+
if (callee.type !== "ArrowFunctionExpression" || callee.async || !callee.expression) return;
|
|
1975
|
+
let body = callee.body;
|
|
1976
|
+
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1977
|
+
if (body.type !== "JSXElement" && body.type !== "JSXFragment") return;
|
|
1978
|
+
const { params } = callee;
|
|
1979
|
+
const args = expression.arguments;
|
|
1980
|
+
if (params.length !== args.length || hasRefOrSpread(body)) return;
|
|
1981
|
+
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
1982
|
+
const scope = this.context.scopes.scopes.get(callee);
|
|
1983
|
+
if (!scope) return;
|
|
1984
|
+
const copy = new MagicString(this.context.code);
|
|
1985
|
+
for (const [index, param] of params.entries()) {
|
|
1986
|
+
const arg = args.at(index);
|
|
1987
|
+
if (param.type !== "Identifier" || !arg || arg.type === "SpreadElement") return;
|
|
1988
|
+
const binding = scope.bindings.get(param.name);
|
|
1989
|
+
const text = this.argumentText(arg);
|
|
1990
|
+
if (!binding || binding.mutated || text === void 0) return;
|
|
1991
|
+
for (const reference of binding.references) {
|
|
1992
|
+
if (arg.type === "Identifier" && lookup(scopeAt(this.context.scopes, this.context.parents, reference), arg.name) !== this.context.scopes.references.get(arg)) return;
|
|
1993
|
+
const parent = this.context.parents.get(reference);
|
|
1994
|
+
const shorthand = parent?.type === "Property" && parent.shorthand && parent.value === reference;
|
|
1995
|
+
copy.overwrite(reference.start, reference.end, shorthand ? `${param.name}: ${text}` : text);
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
return copy.slice(body.start, body.end);
|
|
1999
|
+
}
|
|
2000
|
+
/** The text of an argument that reads the same wherever it is copied. */
|
|
2001
|
+
argumentText(arg) {
|
|
2002
|
+
let node = arg;
|
|
2003
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
2004
|
+
switch (node.type) {
|
|
2005
|
+
case "Literal": return "regex" in node ? void 0 : textOf(this.context, node);
|
|
2006
|
+
case "TemplateLiteral": return node.expressions.length === 0 ? textOf(this.context, node) : void 0;
|
|
2007
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${textOf(this.context, node)})` : void 0;
|
|
2008
|
+
case "Identifier": {
|
|
2009
|
+
const binding = this.context.scopes.references.get(node);
|
|
2010
|
+
return binding && !binding.mutated ? node.name : void 0;
|
|
2011
|
+
}
|
|
2012
|
+
default: return;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
1488
2015
|
eliminateDeadStatements(statements) {
|
|
1489
2016
|
let terminator = -1;
|
|
1490
2017
|
for (const [index, statement] of statements.entries()) {
|
|
@@ -1510,7 +2037,7 @@ var Folder = class {
|
|
|
1510
2037
|
this.changed = true;
|
|
1511
2038
|
return;
|
|
1512
2039
|
}
|
|
1513
|
-
this.replace(statement, continuesPreviousStatement
|
|
2040
|
+
this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
|
|
1514
2041
|
}
|
|
1515
2042
|
/**
|
|
1516
2043
|
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
@@ -1566,29 +2093,35 @@ const CONTEXT_SAFE_PRIMITIVES = [
|
|
|
1566
2093
|
"createMemo",
|
|
1567
2094
|
"createEffect",
|
|
1568
2095
|
"createRenderEffect",
|
|
1569
|
-
"
|
|
2096
|
+
"createStore",
|
|
2097
|
+
"createProjection",
|
|
2098
|
+
"createOptimistic",
|
|
1570
2099
|
"onCleanup",
|
|
1571
|
-
"
|
|
2100
|
+
"onSettled",
|
|
1572
2101
|
"untrack",
|
|
1573
|
-
"batch",
|
|
1574
2102
|
"useContext",
|
|
2103
|
+
"flush",
|
|
1575
2104
|
"mapArray",
|
|
1576
|
-
"
|
|
1577
|
-
"
|
|
1578
|
-
"
|
|
2105
|
+
"repeat",
|
|
2106
|
+
"merge",
|
|
2107
|
+
"omit"
|
|
1579
2108
|
];
|
|
1580
2109
|
/** Primitives that return a getter and a setter, as `[get, set]`. */
|
|
1581
|
-
const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
|
|
2110
|
+
const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
|
|
2111
|
+
"createSignal",
|
|
2112
|
+
"createStore",
|
|
2113
|
+
"createOptimistic"
|
|
2114
|
+
]);
|
|
1582
2115
|
/**
|
|
1583
|
-
* The provider a JSX element creates:
|
|
1584
|
-
*
|
|
1585
|
-
*
|
|
2116
|
+
* The provider a JSX element creates: a tag that refers to a top-level
|
|
2117
|
+
* `createContext(...)` from Solid, nothing writes to, with one `value`
|
|
2118
|
+
* attribute holding a value that is never `undefined`.
|
|
1586
2119
|
*
|
|
1587
|
-
* `useContext`
|
|
1588
|
-
* value that could be `undefined` keeps its provider.
|
|
2120
|
+
* `useContext` throws when the value and the default are both `undefined`,
|
|
2121
|
+
* so a value that could be `undefined` keeps its provider.
|
|
1589
2122
|
*/
|
|
1590
2123
|
function providerOf(context, element) {
|
|
1591
|
-
const binding =
|
|
2124
|
+
const binding = contextBinding(context, element.openingElement.name);
|
|
1592
2125
|
if (!binding || element.openingElement.selfClosing) return;
|
|
1593
2126
|
const { attributes } = element.openingElement;
|
|
1594
2127
|
const attribute = attributes.at(0);
|
|
@@ -1605,14 +2138,7 @@ function providerOf(context, element) {
|
|
|
1605
2138
|
};
|
|
1606
2139
|
}
|
|
1607
2140
|
/**
|
|
1608
|
-
* The context a
|
|
1609
|
-
*/
|
|
1610
|
-
function providerContext(context, name) {
|
|
1611
|
-
if (name.type !== "JSXMemberExpression" || name.object.type !== "JSXIdentifier" || name.property.name !== "Provider") return;
|
|
1612
|
-
return contextBinding(context, name.object);
|
|
1613
|
-
}
|
|
1614
|
-
/**
|
|
1615
|
-
* The top-level context binding an identifier refers to.
|
|
2141
|
+
* The top-level context binding a tag or identifier refers to.
|
|
1616
2142
|
*/
|
|
1617
2143
|
function contextBinding(context, name) {
|
|
1618
2144
|
if (name.type !== "JSXIdentifier" && name.type !== "Identifier") return;
|
|
@@ -1801,8 +2327,8 @@ function runsOutsideCode(context, binding, root, options, depth) {
|
|
|
1801
2327
|
return found;
|
|
1802
2328
|
}
|
|
1803
2329
|
/**
|
|
1804
|
-
* Whether an expression is a `
|
|
1805
|
-
*
|
|
2330
|
+
* Whether an expression is a `merge()` or `omit()` view of a component's
|
|
2331
|
+
* own props, with only literal objects and keys besides.
|
|
1806
2332
|
*/
|
|
1807
2333
|
function isOwnView(context, node, props, depth) {
|
|
1808
2334
|
const expression = unwrap(node);
|
|
@@ -1811,33 +2337,23 @@ function isOwnView(context, node, props, depth) {
|
|
|
1811
2337
|
if (expression.type === "Identifier") {
|
|
1812
2338
|
const binding = context.scopes.references.get(expression);
|
|
1813
2339
|
if (binding === props) return true;
|
|
1814
|
-
|
|
2340
|
+
const init = binding ? constantInit(context, binding) : void 0;
|
|
2341
|
+
call = init ? unwrap(init) : void 0;
|
|
1815
2342
|
}
|
|
1816
2343
|
if (call?.type !== "CallExpression") return false;
|
|
1817
2344
|
const callee = solidCallee(context, call);
|
|
1818
|
-
if (callee !== "
|
|
2345
|
+
if (callee !== "merge" && callee !== "omit") return false;
|
|
1819
2346
|
return call.arguments.every((argument) => {
|
|
1820
2347
|
if (argument.type === "SpreadElement") return false;
|
|
1821
2348
|
const inner = unwrap(argument);
|
|
1822
|
-
return inner.type === "ObjectExpression" || inner.type === "
|
|
2349
|
+
return inner.type === "ObjectExpression" || inner.type === "Literal" && typeof inner.value === "string" || isOwnView(context, inner, props, depth + 1);
|
|
1823
2350
|
});
|
|
1824
2351
|
}
|
|
1825
|
-
/**
|
|
1826
|
-
* The call a view binding comes from: `const view = mergeProps(...)`, or
|
|
1827
|
-
* one of the names in `const [a, b] = splitProps(...)`.
|
|
1828
|
-
*/
|
|
1829
|
-
function viewInit(context, binding) {
|
|
1830
|
-
const init = constantInit(context, binding);
|
|
1831
|
-
if (init) return unwrap(init);
|
|
1832
|
-
const { declaration } = binding;
|
|
1833
|
-
if (binding.mutated || binding.kind !== "const" || declaration.type !== "VariableDeclarator" || declaration.id.type !== "ArrayPattern" || !declaration.id.elements.some((element) => element === binding.identifier) || !declaration.init) return;
|
|
1834
|
-
return unwrap(declaration.init);
|
|
1835
|
-
}
|
|
1836
2352
|
function isVisibleElement(context, element, options) {
|
|
1837
2353
|
const { name } = element.openingElement;
|
|
1838
|
-
if (providerContext(context, name)) return true;
|
|
1839
2354
|
if (name.type !== "JSXIdentifier") return false;
|
|
1840
2355
|
if (isIntrinsicTag(name.name)) return true;
|
|
2356
|
+
if (contextBinding(context, name)) return true;
|
|
1841
2357
|
const binding = context.scopes.references.get(name);
|
|
1842
2358
|
const identity = binding ? solidExport(binding, context.options) : name.name;
|
|
1843
2359
|
if (identity !== void 0 && identity !== "Dynamic" && context.options.builtIns.has(identity)) return true;
|
|
@@ -1936,7 +2452,7 @@ function contextReads(context, root, target) {
|
|
|
1936
2452
|
const reads = [];
|
|
1937
2453
|
const visit = (node) => {
|
|
1938
2454
|
if (node.type === "JSXAttribute" && isHandlerFunction(node)) return;
|
|
1939
|
-
if (node !== root && node.type === "JSXElement" &&
|
|
2455
|
+
if (node !== root && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === target) return;
|
|
1940
2456
|
if (node.type === "CallExpression" && solidCallee(context, node) === "useContext") {
|
|
1941
2457
|
const argument = node.arguments.at(0);
|
|
1942
2458
|
if (node.arguments.length === 1 && argument?.type !== "SpreadElement" && argument !== void 0 && contextBinding(context, unwrap(argument)) === target) reads.push(node);
|
|
@@ -1954,7 +2470,7 @@ function hasNestedProvider(context, provider) {
|
|
|
1954
2470
|
let nested = false;
|
|
1955
2471
|
const visit = (node) => {
|
|
1956
2472
|
if (nested) return;
|
|
1957
|
-
if (node !== provider.element && node.type === "JSXElement" &&
|
|
2473
|
+
if (node !== provider.element && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === provider.context) {
|
|
1958
2474
|
nested = true;
|
|
1959
2475
|
return;
|
|
1960
2476
|
}
|
|
@@ -2003,230 +2519,71 @@ var ProviderRemover = class {
|
|
|
2003
2519
|
reportShapes() {
|
|
2004
2520
|
const visit = (node) => {
|
|
2005
2521
|
if (node.type === "JSXElement") {
|
|
2006
|
-
const binding =
|
|
2522
|
+
const binding = contextBinding(this.context, node.openingElement.name);
|
|
2007
2523
|
if (binding && !providerOf(this.context, node)) {
|
|
2008
2524
|
const { attributes } = node.openingElement;
|
|
2009
2525
|
const attribute = attributes.at(0);
|
|
2010
2526
|
const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
|
|
2011
2527
|
recordSkip(this.context, "contexts", onlyValue && !node.openingElement.selfClosing ? "provider-undefined" : "provider-shape", binding.name);
|
|
2012
2528
|
}
|
|
2013
|
-
}
|
|
2014
|
-
forEachChild(node, visit);
|
|
2015
|
-
};
|
|
2016
|
-
visit(this.context.program);
|
|
2017
|
-
}
|
|
2018
|
-
tryRemove(provider) {
|
|
2019
|
-
const { element } = provider;
|
|
2020
|
-
const children = element.children;
|
|
2021
|
-
let reason = "open-other";
|
|
2022
|
-
if (!children.every((child) => isClosed(this.context, child, {
|
|
2023
|
-
component: () => false,
|
|
2024
|
-
contextValue: (target) => target === provider.context ? provider.value : void 0,
|
|
2025
|
-
onOpen: (open) => {
|
|
2026
|
-
reason = open;
|
|
2027
|
-
}
|
|
2028
|
-
}))) {
|
|
2029
|
-
this.skip(provider, reason);
|
|
2030
|
-
return false;
|
|
2031
|
-
}
|
|
2032
|
-
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
2033
|
-
const texts = [];
|
|
2034
|
-
for (const read of reads) {
|
|
2035
|
-
const text = stableValueText(this.context, provider, read);
|
|
2036
|
-
if (text === void 0) {
|
|
2037
|
-
this.skip(provider, "provider-value");
|
|
2038
|
-
return false;
|
|
2039
|
-
}
|
|
2040
|
-
texts.push(text);
|
|
2041
|
-
}
|
|
2042
|
-
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
2043
|
-
if (stableValueText(this.context, provider, element) === void 0) {
|
|
2044
|
-
this.skip(provider, "provider-value");
|
|
2045
|
-
return false;
|
|
2046
|
-
}
|
|
2047
|
-
}
|
|
2048
|
-
const { s } = this.context;
|
|
2049
|
-
for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
|
|
2050
|
-
const { openingElement, closingElement } = element;
|
|
2051
|
-
s.overwrite(openingElement.start, openingElement.end, "<>");
|
|
2052
|
-
if (closingElement) s.overwrite(closingElement.start, closingElement.end, "</>");
|
|
2053
|
-
this.changed = true;
|
|
2054
|
-
return true;
|
|
2055
|
-
}
|
|
2056
|
-
};
|
|
2057
|
-
/**
|
|
2058
|
-
* Runs the provider pass over a parsed program. Returns whether anything changed.
|
|
2059
|
-
*/
|
|
2060
|
-
function removeProviders(context) {
|
|
2061
|
-
const remover = new ProviderRemover(context);
|
|
2062
|
-
remover.run();
|
|
2063
|
-
return remover.changed;
|
|
2064
|
-
}
|
|
2065
|
-
//#endregion
|
|
2066
|
-
//#region src/size.ts
|
|
2067
|
-
/** A template's declaration, and the function that clones and returns it. */
|
|
2068
|
-
const TEMPLATE = 40;
|
|
2069
|
-
/** A walk to an element, like `e.firstChild.nextSibling`. */
|
|
2070
|
-
const WALK = 12;
|
|
2071
|
-
/** An `insert()` of a dynamic child. */
|
|
2072
|
-
const INSERT = 12;
|
|
2073
|
-
/** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
|
|
2074
|
-
const EFFECT = 24;
|
|
2075
|
-
/** An event handler assignment, like `e.$$click = ...`. */
|
|
2076
|
-
const HANDLER = 10;
|
|
2077
|
-
/** A `spread()` of props onto an element. */
|
|
2078
|
-
const SPREAD = 16;
|
|
2079
|
-
/** A `createComponent(Name, {})` call. */
|
|
2080
|
-
const COMPONENT = 8;
|
|
2081
|
-
/** A getter, `get name() { return ... }`. */
|
|
2082
|
-
const GETTER = 16;
|
|
2083
|
-
/** A `mergeProps()` around the props of a component with a spread. */
|
|
2084
|
-
const MERGE = 6;
|
|
2085
|
-
/**
|
|
2086
|
-
* The length of `text` once a minifier removes the whitespace it does not need.
|
|
2087
|
-
*/
|
|
2088
|
-
function compactLength(text) {
|
|
2089
|
-
return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
|
|
2090
|
-
}
|
|
2091
|
-
function nameLength(name) {
|
|
2092
|
-
switch (name.type) {
|
|
2093
|
-
case "JSXIdentifier": return name.name.length;
|
|
2094
|
-
case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
|
|
2095
|
-
case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
|
|
2096
|
-
default: return 4;
|
|
2097
|
-
}
|
|
2098
|
-
}
|
|
2099
|
-
function isIntrinsic(element) {
|
|
2100
|
-
const { name } = element.openingElement;
|
|
2101
|
-
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
2102
|
-
}
|
|
2103
|
-
function attributeName(attribute) {
|
|
2104
|
-
const { name } = attribute;
|
|
2105
|
-
return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
2106
|
-
}
|
|
2107
|
-
/**
|
|
2108
|
-
* Estimates lowered sizes in one piece of code.
|
|
2109
|
-
*/
|
|
2110
|
-
var SizeEstimator = class {
|
|
2111
|
-
constructor(code) {
|
|
2112
|
-
this.code = code;
|
|
2113
|
-
}
|
|
2114
|
-
text(node) {
|
|
2115
|
-
return compactLength(this.code.slice(node.start, node.end));
|
|
2116
|
-
}
|
|
2117
|
-
/**
|
|
2118
|
-
* The size of any node: JSX as lowered, other code as its compact text
|
|
2119
|
-
* with the JSX in it as lowered.
|
|
2120
|
-
*/
|
|
2121
|
-
node(node) {
|
|
2122
|
-
if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
|
|
2123
|
-
let size = this.text(node);
|
|
2124
|
-
const visit = (child) => {
|
|
2125
|
-
if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
2126
|
-
size += this.jsx(child, false) - this.text(child);
|
|
2127
|
-
return;
|
|
2128
|
-
}
|
|
2129
|
-
forEachChild(child, visit);
|
|
2130
|
-
};
|
|
2131
|
-
forEachChild(node, visit);
|
|
2132
|
-
return size;
|
|
2133
|
-
}
|
|
2134
|
-
/**
|
|
2135
|
-
* The size of a JSX element or fragment. `inTemplate` is whether it sits
|
|
2136
|
-
* directly in an element, whose template it joins.
|
|
2137
|
-
*/
|
|
2138
|
-
jsx(node, inTemplate) {
|
|
2139
|
-
if (node.type === "JSXFragment") return this.children(node.children) + 2;
|
|
2140
|
-
if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
|
|
2141
|
-
const tag = nameLength(node.openingElement.name);
|
|
2142
|
-
let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
|
|
2143
|
-
let dynamic = false;
|
|
2144
|
-
for (const attribute of node.openingElement.attributes) {
|
|
2145
|
-
const cost = this.elementAttribute(attribute);
|
|
2146
|
-
size += cost.size;
|
|
2147
|
-
dynamic ||= cost.dynamic;
|
|
2148
|
-
}
|
|
2149
|
-
for (const child of node.children) {
|
|
2150
|
-
if (isInsignificant(child)) continue;
|
|
2151
|
-
if (child.type === "JSXText") size += compactLength(child.value);
|
|
2152
|
-
else if (child.type === "JSXElement") size += this.jsx(child, true);
|
|
2153
|
-
else {
|
|
2154
|
-
size += INSERT + this.child(child);
|
|
2155
|
-
dynamic = true;
|
|
2156
|
-
}
|
|
2157
|
-
}
|
|
2158
|
-
return size + (dynamic ? WALK : 0);
|
|
2159
|
-
}
|
|
2160
|
-
elementAttribute(attribute) {
|
|
2161
|
-
if (attribute.type === "JSXSpreadAttribute") return {
|
|
2162
|
-
size: SPREAD + this.node(attribute.argument),
|
|
2163
|
-
dynamic: true
|
|
2164
|
-
};
|
|
2165
|
-
const name = attributeName(attribute);
|
|
2166
|
-
const { value } = attribute;
|
|
2167
|
-
if (!value || value.type === "Literal") return {
|
|
2168
|
-
size: name.length + (value ? this.text(value) : 0) + 1,
|
|
2169
|
-
dynamic: false
|
|
2170
|
-
};
|
|
2171
|
-
if (value.type !== "JSXExpressionContainer") return {
|
|
2172
|
-
size: name.length + 1 + this.jsx(value, false),
|
|
2173
|
-
dynamic: true
|
|
2174
|
-
};
|
|
2175
|
-
const { expression } = value;
|
|
2176
|
-
if (expression.type === "JSXEmptyExpression") return {
|
|
2177
|
-
size: 0,
|
|
2178
|
-
dynamic: false
|
|
2179
|
-
};
|
|
2180
|
-
const inner = unwrap(expression);
|
|
2181
|
-
if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
|
|
2182
|
-
size: name.length + this.text(inner) + 1,
|
|
2183
|
-
dynamic: false
|
|
2184
|
-
};
|
|
2185
|
-
return {
|
|
2186
|
-
size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
|
|
2187
|
-
dynamic: true
|
|
2188
|
-
};
|
|
2189
|
-
}
|
|
2190
|
-
child(child) {
|
|
2191
|
-
if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
|
|
2192
|
-
if (child.type === "JSXSpreadChild") return this.node(child.expression);
|
|
2193
|
-
if (child.type === "JSXText") return compactLength(child.value) + 2;
|
|
2194
|
-
return this.jsx(child, false);
|
|
2195
|
-
}
|
|
2196
|
-
/**
|
|
2197
|
-
* Children outside a template, which lower to an array of values.
|
|
2198
|
-
*/
|
|
2199
|
-
children(children) {
|
|
2200
|
-
let size = 0;
|
|
2201
|
-
for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
|
|
2202
|
-
return size;
|
|
2529
|
+
}
|
|
2530
|
+
forEachChild(node, visit);
|
|
2531
|
+
};
|
|
2532
|
+
visit(this.context.program);
|
|
2203
2533
|
}
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2534
|
+
tryRemove(provider) {
|
|
2535
|
+
const { element } = provider;
|
|
2536
|
+
const children = element.children;
|
|
2537
|
+
let reason = "open-other";
|
|
2538
|
+
if (!children.every((child) => isClosed(this.context, child, {
|
|
2539
|
+
component: () => false,
|
|
2540
|
+
contextValue: (target) => target === provider.context ? provider.value : void 0,
|
|
2541
|
+
onOpen: (open) => {
|
|
2542
|
+
reason = open;
|
|
2212
2543
|
}
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
else if (value.type === "JSXExpressionContainer") {
|
|
2217
|
-
const { expression } = value;
|
|
2218
|
-
if (expression.type !== "JSXEmptyExpression") {
|
|
2219
|
-
const inner = unwrap(expression);
|
|
2220
|
-
const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2221
|
-
size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
|
|
2222
|
-
}
|
|
2223
|
-
} else size += name.length + GETTER + this.jsx(value, false);
|
|
2544
|
+
}))) {
|
|
2545
|
+
this.skip(provider, reason);
|
|
2546
|
+
return false;
|
|
2224
2547
|
}
|
|
2225
|
-
const
|
|
2226
|
-
|
|
2227
|
-
|
|
2548
|
+
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
2549
|
+
const texts = [];
|
|
2550
|
+
for (const read of reads) {
|
|
2551
|
+
const text = stableValueText(this.context, provider, read);
|
|
2552
|
+
if (text === void 0) {
|
|
2553
|
+
this.skip(provider, "provider-value");
|
|
2554
|
+
return false;
|
|
2555
|
+
}
|
|
2556
|
+
texts.push(text);
|
|
2557
|
+
}
|
|
2558
|
+
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
2559
|
+
if (stableValueText(this.context, provider, element) === void 0) {
|
|
2560
|
+
this.skip(provider, "provider-value");
|
|
2561
|
+
return false;
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
const { s } = this.context;
|
|
2565
|
+
for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
|
|
2566
|
+
const { openingElement, closingElement } = element;
|
|
2567
|
+
s.overwrite(openingElement.start, openingElement.end, "<>");
|
|
2568
|
+
if (closingElement) s.overwrite(closingElement.start, closingElement.end, "</>");
|
|
2569
|
+
this.changed = true;
|
|
2570
|
+
return true;
|
|
2228
2571
|
}
|
|
2229
2572
|
};
|
|
2573
|
+
/**
|
|
2574
|
+
* Runs the provider pass over a parsed program. Returns whether anything changed.
|
|
2575
|
+
*/
|
|
2576
|
+
function removeProviders(context) {
|
|
2577
|
+
const remover = new ProviderRemover(context);
|
|
2578
|
+
remover.run();
|
|
2579
|
+
return remover.changed;
|
|
2580
|
+
}
|
|
2581
|
+
//#endregion
|
|
2582
|
+
//#region src/copy-size.ts
|
|
2583
|
+
/**
|
|
2584
|
+
* Measures a component's copy at a call site: folds it as the passes after
|
|
2585
|
+
* inlining would, then estimates its size with `SizeEstimator`.
|
|
2586
|
+
*/
|
|
2230
2587
|
/** The name of the function a copy is wrapped in to be folded and measured. */
|
|
2231
2588
|
const WRAPPER = "__so_size__";
|
|
2232
2589
|
/**
|
|
@@ -2261,9 +2618,7 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2261
2618
|
size += estimator.node(statement);
|
|
2262
2619
|
continue;
|
|
2263
2620
|
}
|
|
2264
|
-
|
|
2265
|
-
if (argument?.type === "JSXElement" || argument?.type === "JSXFragment") size += estimator.jsx(argument, inTemplate);
|
|
2266
|
-
else if (argument) size += (inTemplate ? 24 : 0) + estimator.node(argument);
|
|
2621
|
+
if (statement.argument) size += estimator.root(statement.argument, inTemplate);
|
|
2267
2622
|
}
|
|
2268
2623
|
return size;
|
|
2269
2624
|
}
|
|
@@ -2286,8 +2641,9 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2286
2641
|
* function whose body is JSX.
|
|
2287
2642
|
* - It does not use `this`, `arguments`, `new.target`, or `super`.
|
|
2288
2643
|
*
|
|
2289
|
-
* A call site inlines when it passes plain attributes: no spread
|
|
2290
|
-
*
|
|
2644
|
+
* A call site inlines when it passes plain attributes: no spread and no
|
|
2645
|
+
* namespaced attribute. A `ref` moves to the element the component forwards
|
|
2646
|
+
* `props.ref` to. See `refValue`.
|
|
2291
2647
|
*
|
|
2292
2648
|
* # How props keep their meaning
|
|
2293
2649
|
*
|
|
@@ -2307,58 +2663,38 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2307
2663
|
*
|
|
2308
2664
|
* Statements, and stored props, have to run where the component would have
|
|
2309
2665
|
* been created. Solid runs a component untracked, so the statements move into
|
|
2310
|
-
* the function whose returned JSX holds the call
|
|
2311
|
-
*
|
|
2312
|
-
*
|
|
2313
|
-
*
|
|
2666
|
+
* the function whose returned JSX holds the call, when that function also
|
|
2667
|
+
* runs untracked:
|
|
2668
|
+
*
|
|
2669
|
+
* - a component;
|
|
2670
|
+
* - the child callback of `<For>` or `<Repeat>`, which runs once per item
|
|
2671
|
+
* under its own owner;
|
|
2672
|
+
* - the child callback of `<Show>` or `<Match>` that takes the value, which
|
|
2673
|
+
* runs once each time the condition turns true;
|
|
2674
|
+
* - the callback given to `render`, which runs once under its root.
|
|
2675
|
+
*
|
|
2676
|
+
* The call has to be in that returned JSX with only intrinsic elements and
|
|
2677
|
+
* fragments around it, so it is created exactly once each time that function
|
|
2678
|
+
* runs.
|
|
2314
2679
|
*
|
|
2315
2680
|
* A context provider is a component, so statements do not move out of it,
|
|
2316
2681
|
* and `useContext` still runs under it. The exception is a provider whose
|
|
2317
2682
|
* subtree only runs visible code. Statements move out through it, and each
|
|
2318
2683
|
* `useContext` of its context in a copy becomes its value. See `provider.ts`.
|
|
2319
2684
|
*
|
|
2320
|
-
* The props can also be read through `
|
|
2321
|
-
*
|
|
2322
|
-
*
|
|
2323
|
-
*
|
|
2324
|
-
* holds.
|
|
2685
|
+
* The props can also be read through `merge()` and `omit()` views declared
|
|
2686
|
+
* in the component. Each call site knows which props it passes, so a read
|
|
2687
|
+
* of a view resolves to a prop, a literal default, or `undefined`, and a
|
|
2688
|
+
* spread of a view becomes the attributes it holds.
|
|
2325
2689
|
*
|
|
2326
2690
|
* # Accepted differences
|
|
2327
2691
|
*
|
|
2328
2692
|
* - A prop that is a function and is called as `props.name()` runs with
|
|
2329
2693
|
* `this` set to its own receiver instead of the props object.
|
|
2330
2694
|
* - Statements moved into the host run before the host's JSX is created,
|
|
2331
|
-
* instead of while it is created. The order of `
|
|
2695
|
+
* instead of while it is created. The order of `onSettled` and effect
|
|
2332
2696
|
* registration across siblings can change.
|
|
2333
2697
|
*/
|
|
2334
|
-
/**
|
|
2335
|
-
* A prop read that falls back to a default when the prop is `undefined`,
|
|
2336
|
-
* the way `mergeProps()` reads it.
|
|
2337
|
-
*/
|
|
2338
|
-
function withFallback(text, fallback) {
|
|
2339
|
-
return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
|
|
2340
|
-
}
|
|
2341
|
-
function allows(source, key) {
|
|
2342
|
-
return !source.hidden.has(key) && (source.only === void 0 || source.only.has(key));
|
|
2343
|
-
}
|
|
2344
|
-
/**
|
|
2345
|
-
* Whether an expression passed as a prop is never `undefined`.
|
|
2346
|
-
*/
|
|
2347
|
-
function isDefinedExpression(node) {
|
|
2348
|
-
const inner = unwrap(node);
|
|
2349
|
-
switch (inner.type) {
|
|
2350
|
-
case "ObjectExpression":
|
|
2351
|
-
case "ArrayExpression":
|
|
2352
|
-
case "ArrowFunctionExpression":
|
|
2353
|
-
case "FunctionExpression":
|
|
2354
|
-
case "ClassExpression":
|
|
2355
|
-
case "NewExpression":
|
|
2356
|
-
case "TemplateLiteral":
|
|
2357
|
-
case "JSXElement":
|
|
2358
|
-
case "JSXFragment": return true;
|
|
2359
|
-
default: return evaluate(inner, () => void 0)?.value !== void 0;
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
2698
|
function isWithin(node, container) {
|
|
2363
2699
|
return node.start >= container.start && node.end <= container.end;
|
|
2364
2700
|
}
|
|
@@ -2505,12 +2841,55 @@ function functionOf(binding, options) {
|
|
|
2505
2841
|
function attributesOf(element) {
|
|
2506
2842
|
const attributes = /* @__PURE__ */ new Map();
|
|
2507
2843
|
for (const attribute of element.openingElement.attributes) {
|
|
2508
|
-
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" || attributes.has(attribute.name.name)) return;
|
|
2844
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(attribute) || attributes.has(attribute.name.name)) return;
|
|
2509
2845
|
attributes.set(attribute.name.name, attribute);
|
|
2510
2846
|
}
|
|
2511
2847
|
return attributes;
|
|
2512
2848
|
}
|
|
2513
2849
|
/**
|
|
2850
|
+
* Whether a top-level statement declares only constants made of literals,
|
|
2851
|
+
* like `const ROWS = [{ name: 'a' }]`. An export keeps its `export`, since
|
|
2852
|
+
* the fold pass treats an exported list differently.
|
|
2853
|
+
*/
|
|
2854
|
+
function isDataDeclaration(statement) {
|
|
2855
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
2856
|
+
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));
|
|
2857
|
+
}
|
|
2858
|
+
/** Whether an expression is a literal, or an array or object literal of them. */
|
|
2859
|
+
function isDataLiteral(node) {
|
|
2860
|
+
switch (node.type) {
|
|
2861
|
+
case "Literal": return !("regex" in node);
|
|
2862
|
+
case "TemplateLiteral": return node.expressions.length === 0;
|
|
2863
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal";
|
|
2864
|
+
case "ParenthesizedExpression":
|
|
2865
|
+
case "TSAsExpression":
|
|
2866
|
+
case "TSSatisfiesExpression": return isDataLiteral(node.expression);
|
|
2867
|
+
case "ArrayExpression": return node.elements.every((element) => element !== null && element.type !== "SpreadElement" && isDataLiteral(element));
|
|
2868
|
+
case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && property.kind === "init" && !property.method && !property.computed && isDataLiteral(property.value));
|
|
2869
|
+
default: return false;
|
|
2870
|
+
}
|
|
2871
|
+
}
|
|
2872
|
+
function isMemberRead(read) {
|
|
2873
|
+
return "member" in read;
|
|
2874
|
+
}
|
|
2875
|
+
/**
|
|
2876
|
+
* Whether a `ref` at a call site can move to the element the component
|
|
2877
|
+
* forwards it to: a name, a function, or an array of them. A name is
|
|
2878
|
+
* assigned the element, so it has to stay the name itself. Anything else
|
|
2879
|
+
* would be evaluated when the element is made, outside the `untrack` the
|
|
2880
|
+
* component's `ref` runs it in.
|
|
2881
|
+
*/
|
|
2882
|
+
function isForwardableRef(attribute) {
|
|
2883
|
+
const { value } = attribute;
|
|
2884
|
+
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
|
|
2885
|
+
const isTarget = (node) => {
|
|
2886
|
+
const inner = unwrap(node);
|
|
2887
|
+
if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element));
|
|
2888
|
+
return inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2889
|
+
};
|
|
2890
|
+
return isTarget(value.expression);
|
|
2891
|
+
}
|
|
2892
|
+
/**
|
|
2514
2893
|
* Why `attributesOf` turns down a call site, for the report.
|
|
2515
2894
|
*/
|
|
2516
2895
|
function callSiteReason(element) {
|
|
@@ -2730,9 +3109,9 @@ var Inliner = class {
|
|
|
2730
3109
|
return false;
|
|
2731
3110
|
}
|
|
2732
3111
|
/**
|
|
2733
|
-
* Finds the `
|
|
2734
|
-
*
|
|
2735
|
-
*
|
|
3112
|
+
* Finds the `merge()` and `omit()` views of the props declared in a
|
|
3113
|
+
* component's statements, and how each is used. A view can only be read
|
|
3114
|
+
* as `view.name`, spread onto an element, or passed to another view.
|
|
2736
3115
|
* `undefined` when a view is used any other way.
|
|
2737
3116
|
*/
|
|
2738
3117
|
analyzeViews(fn, scope, statements, param) {
|
|
@@ -2741,32 +3120,22 @@ var Inliner = class {
|
|
|
2741
3120
|
const declarations = [];
|
|
2742
3121
|
for (const statement of statements) {
|
|
2743
3122
|
const declarator = statement.type === "VariableDeclaration" ? statement.declarations.at(0) : void 0;
|
|
2744
|
-
if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || !declarator
|
|
3123
|
+
if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || declarator?.id.type !== "Identifier" || !declarator.init) continue;
|
|
2745
3124
|
const init = unwrap(declarator.init);
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
if (
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
else continue;
|
|
2754
|
-
if (declared === null) continue;
|
|
2755
|
-
if (!declared) return;
|
|
2756
|
-
for (const [identifier, view] of declared) {
|
|
2757
|
-
if (identifier?.type !== "Identifier") continue;
|
|
2758
|
-
const binding = scope.bindings.get(identifier.name);
|
|
2759
|
-
if (!binding || binding.mutated) return;
|
|
2760
|
-
views.set(binding, view);
|
|
2761
|
-
}
|
|
3125
|
+
const callee = init.type === "CallExpression" ? solidCallee(this.context, init) : void 0;
|
|
3126
|
+
if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
|
|
3127
|
+
const view = this.viewOf(callee, init, param, views, args);
|
|
3128
|
+
if (view === null) continue;
|
|
3129
|
+
const binding = scope.bindings.get(declarator.id.name);
|
|
3130
|
+
if (!view || !binding || binding.mutated) return;
|
|
3131
|
+
views.set(binding, view);
|
|
2762
3132
|
declarations.push(statement);
|
|
2763
3133
|
}
|
|
2764
3134
|
const reads = [];
|
|
2765
3135
|
const spreads = [];
|
|
2766
3136
|
const whole = { sources: [{
|
|
2767
3137
|
defaults: void 0,
|
|
2768
|
-
hidden: /* @__PURE__ */ new Set()
|
|
2769
|
-
only: void 0
|
|
3138
|
+
hidden: /* @__PURE__ */ new Set()
|
|
2770
3139
|
}] };
|
|
2771
3140
|
for (const reference of param?.references ?? []) {
|
|
2772
3141
|
const spread = this.spreadOnElement(reference);
|
|
@@ -2818,35 +3187,49 @@ var Inliner = class {
|
|
|
2818
3187
|
} : void 0;
|
|
2819
3188
|
}
|
|
2820
3189
|
/**
|
|
2821
|
-
* The
|
|
2822
|
-
* `
|
|
2823
|
-
|
|
2824
|
-
viewSources(argument, param, views, args) {
|
|
2825
|
-
if (argument.type === "CallExpression") {
|
|
2826
|
-
if (solidCallee(this.context, argument) !== "mergeProps") return;
|
|
2827
|
-
return this.viewOf(argument, param, views, args)?.sources;
|
|
2828
|
-
}
|
|
2829
|
-
if (argument.type !== "Identifier") return;
|
|
2830
|
-
const binding = this.context.scopes.references.get(argument);
|
|
2831
|
-
if (binding && binding === param) return [{
|
|
2832
|
-
defaults: void 0,
|
|
2833
|
-
hidden: /* @__PURE__ */ new Set(),
|
|
2834
|
-
only: void 0
|
|
2835
|
-
}];
|
|
2836
|
-
return binding ? views.get(binding)?.sources : void 0;
|
|
2837
|
-
}
|
|
2838
|
-
/**
|
|
2839
|
-
* The view a `mergeProps()` call makes. `null` when it does not take the
|
|
2840
|
-
* props or another view, and `undefined` when it does but its other
|
|
2841
|
-
* arguments are not literal objects.
|
|
3190
|
+
* The view a `merge()` or `omit()` call makes. `null` when it does not
|
|
3191
|
+
* take the props or another view, and `undefined` when it does but its
|
|
3192
|
+
* other arguments are not literal.
|
|
2842
3193
|
*/
|
|
2843
|
-
viewOf(call, param, views, args) {
|
|
2844
|
-
|
|
3194
|
+
viewOf(callee, call, param, views, args) {
|
|
3195
|
+
const sourcesOf = (argument) => {
|
|
3196
|
+
if (argument.type === "CallExpression") {
|
|
3197
|
+
const nested = solidCallee(this.context, argument);
|
|
3198
|
+
if (nested !== "merge" && nested !== "omit") return;
|
|
3199
|
+
return this.viewOf(nested, argument, param, views, args)?.sources;
|
|
3200
|
+
}
|
|
3201
|
+
if (argument.type !== "Identifier") return;
|
|
3202
|
+
const binding = this.context.scopes.references.get(argument);
|
|
3203
|
+
if (binding && binding === param) return [{
|
|
3204
|
+
defaults: void 0,
|
|
3205
|
+
hidden: /* @__PURE__ */ new Set()
|
|
3206
|
+
}];
|
|
3207
|
+
return binding ? views.get(binding)?.sources : void 0;
|
|
3208
|
+
};
|
|
3209
|
+
if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && sourcesOf(unwrap(argument)) !== void 0)) return null;
|
|
3210
|
+
if (callee === "omit") {
|
|
3211
|
+
const first = call.arguments.at(0);
|
|
3212
|
+
const keys = call.arguments.slice(1);
|
|
3213
|
+
if (first === void 0 || first.type === "SpreadElement") return;
|
|
3214
|
+
const target = unwrap(first);
|
|
3215
|
+
const base = sourcesOf(target);
|
|
3216
|
+
const hidden = [];
|
|
3217
|
+
for (const key of keys) {
|
|
3218
|
+
if (key.type !== "Literal" || typeof key.value !== "string") return;
|
|
3219
|
+
hidden.push(key.value);
|
|
3220
|
+
}
|
|
3221
|
+
if (!base) return;
|
|
3222
|
+
args.add(target);
|
|
3223
|
+
return { sources: base.map((source) => ({
|
|
3224
|
+
defaults: source.defaults,
|
|
3225
|
+
hidden: /* @__PURE__ */ new Set([...source.hidden, ...hidden])
|
|
3226
|
+
})) };
|
|
3227
|
+
}
|
|
2845
3228
|
const sources = [];
|
|
2846
3229
|
for (const argument of call.arguments) {
|
|
2847
3230
|
if (argument.type === "SpreadElement") return;
|
|
2848
3231
|
const inner = unwrap(argument);
|
|
2849
|
-
const existing =
|
|
3232
|
+
const existing = sourcesOf(inner);
|
|
2850
3233
|
if (existing) {
|
|
2851
3234
|
args.add(inner);
|
|
2852
3235
|
sources.push(...existing);
|
|
@@ -2867,52 +3250,12 @@ var Inliner = class {
|
|
|
2867
3250
|
}
|
|
2868
3251
|
sources.push({
|
|
2869
3252
|
defaults,
|
|
2870
|
-
hidden: /* @__PURE__ */ new Set()
|
|
2871
|
-
only: void 0
|
|
3253
|
+
hidden: /* @__PURE__ */ new Set()
|
|
2872
3254
|
});
|
|
2873
3255
|
}
|
|
2874
3256
|
return { sources };
|
|
2875
3257
|
}
|
|
2876
3258
|
/**
|
|
2877
|
-
* The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
|
|
2878
|
-
* each with its identifier. A key belongs to the first group that names
|
|
2879
|
-
* it, and the last view holds the keys no group names. `null` when the
|
|
2880
|
-
* call does not take the props or a view.
|
|
2881
|
-
*/
|
|
2882
|
-
splitViews(call, elements, param, views, args) {
|
|
2883
|
-
const first = call.arguments.at(0);
|
|
2884
|
-
if (first === void 0 || first.type === "SpreadElement") return null;
|
|
2885
|
-
const target = unwrap(first);
|
|
2886
|
-
const base = this.viewSources(target, param, views, args);
|
|
2887
|
-
if (!base) return null;
|
|
2888
|
-
args.add(target);
|
|
2889
|
-
const groups = [];
|
|
2890
|
-
for (const group of call.arguments.slice(1)) {
|
|
2891
|
-
if (group.type !== "ArrayExpression") return;
|
|
2892
|
-
const keys = [];
|
|
2893
|
-
for (const key of group.elements) {
|
|
2894
|
-
if (key?.type !== "Literal" || typeof key.value !== "string") return;
|
|
2895
|
-
keys.push(key.value);
|
|
2896
|
-
}
|
|
2897
|
-
groups.push(keys);
|
|
2898
|
-
}
|
|
2899
|
-
if (elements.length > groups.length + 1 || elements.some((element) => element?.type === "RestElement")) return;
|
|
2900
|
-
const claimed = /* @__PURE__ */ new Set();
|
|
2901
|
-
const filters = groups.map((keys) => {
|
|
2902
|
-
const owned = keys.filter((key) => !claimed.has(key));
|
|
2903
|
-
for (const key of owned) claimed.add(key);
|
|
2904
|
-
return new Set(owned);
|
|
2905
|
-
});
|
|
2906
|
-
return elements.map((element, index) => {
|
|
2907
|
-
const only = filters.at(index);
|
|
2908
|
-
return [element, { sources: base.map((source) => ({
|
|
2909
|
-
defaults: source.defaults,
|
|
2910
|
-
hidden: only ? source.hidden : /* @__PURE__ */ new Set([...source.hidden, ...claimed]),
|
|
2911
|
-
only: only ? new Set([...only].filter((key) => source.only === void 0 || source.only.has(key))) : source.only
|
|
2912
|
-
})) }];
|
|
2913
|
-
});
|
|
2914
|
-
}
|
|
2915
|
-
/**
|
|
2916
3259
|
* Whether a function is a component, which Solid runs untracked. Only a
|
|
2917
3260
|
* capitalized function bound at its declaration counts.
|
|
2918
3261
|
*/
|
|
@@ -2925,6 +3268,41 @@ var Inliner = class {
|
|
|
2925
3268
|
return parent?.type === "VariableDeclarator" && parent.id.type === "Identifier" && isCapitalized(parent.id.name);
|
|
2926
3269
|
}
|
|
2927
3270
|
/**
|
|
3271
|
+
* Whether a function runs untracked, once each time the JSX it returns is
|
|
3272
|
+
* created, so it can take a component's statements. See "Where a component
|
|
3273
|
+
* body can go" above.
|
|
3274
|
+
*/
|
|
3275
|
+
isHostFunction(fn) {
|
|
3276
|
+
if (this.isComponentFunction(fn)) return true;
|
|
3277
|
+
if (fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression" || fn.async || fn.generator) return false;
|
|
3278
|
+
let node = fn;
|
|
3279
|
+
let parent = this.context.parents.get(node);
|
|
3280
|
+
while (parent?.type === "ParenthesizedExpression") {
|
|
3281
|
+
node = parent;
|
|
3282
|
+
parent = this.context.parents.get(node);
|
|
3283
|
+
}
|
|
3284
|
+
if (parent?.type === "CallExpression") return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === "render";
|
|
3285
|
+
const element = parent?.type === "JSXExpressionContainer" ? this.context.parents.get(parent) : void 0;
|
|
3286
|
+
if (element?.type !== "JSXElement") return false;
|
|
3287
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
3288
|
+
if (significant.length !== 1 || significant.at(0) !== parent) return false;
|
|
3289
|
+
switch (this.solidCallee(element.openingElement.name)) {
|
|
3290
|
+
case "For":
|
|
3291
|
+
case "Repeat": return true;
|
|
3292
|
+
case "Show":
|
|
3293
|
+
case "Match": return fn.params.length > 0;
|
|
3294
|
+
default: return false;
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
/** The Solid export a callee or tag refers to. */
|
|
3298
|
+
solidCallee(name) {
|
|
3299
|
+
if (name.type !== "Identifier" && name.type !== "JSXIdentifier") return;
|
|
3300
|
+
const { references } = this.context.scopes;
|
|
3301
|
+
if (!references.has(name)) return;
|
|
3302
|
+
const binding = references.get(name);
|
|
3303
|
+
return binding ? solidExport(binding, this.context.options) : void 0;
|
|
3304
|
+
}
|
|
3305
|
+
/**
|
|
2928
3306
|
* Where the statements of a component created at `element` can go: the
|
|
2929
3307
|
* returned JSX of a component, with only intrinsic elements, fragments,
|
|
2930
3308
|
* and closed providers between it and the call.
|
|
@@ -2936,7 +3314,7 @@ var Inliner = class {
|
|
|
2936
3314
|
if (!parent) return;
|
|
2937
3315
|
if (parent.type === "JSXElement") {
|
|
2938
3316
|
const { name } = parent.openingElement;
|
|
2939
|
-
if (!(this.
|
|
3317
|
+
if (!(this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && (isIntrinsicTag(name.name) || this.transparent.has(parent)))) return;
|
|
2940
3318
|
const child = node;
|
|
2941
3319
|
if (!parent.children.some((item) => item === child)) return;
|
|
2942
3320
|
} else if (parent.type === "ReturnStatement") {
|
|
@@ -2944,7 +3322,7 @@ var Inliner = class {
|
|
|
2944
3322
|
if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
|
|
2945
3323
|
let fn = this.context.parents.get(list);
|
|
2946
3324
|
while (fn && !isFunctionNode(fn)) fn = this.context.parents.get(fn);
|
|
2947
|
-
if (!fn || !this.
|
|
3325
|
+
if (!fn || !this.isHostFunction(fn)) return;
|
|
2948
3326
|
return {
|
|
2949
3327
|
fn,
|
|
2950
3328
|
host: {
|
|
@@ -2953,7 +3331,7 @@ var Inliner = class {
|
|
|
2953
3331
|
}
|
|
2954
3332
|
};
|
|
2955
3333
|
} else if (parent.type === "ArrowFunctionExpression") {
|
|
2956
|
-
if (parent.body !== node || !this.
|
|
3334
|
+
if (parent.body !== node || !this.isHostFunction(parent)) return;
|
|
2957
3335
|
return {
|
|
2958
3336
|
fn: parent,
|
|
2959
3337
|
host: {
|
|
@@ -2994,6 +3372,29 @@ var Inliner = class {
|
|
|
2994
3372
|
};
|
|
2995
3373
|
}
|
|
2996
3374
|
/**
|
|
3375
|
+
* How a `ref` from the call site reads in a copy. Solid passes a `ref` to
|
|
3376
|
+
* a component as a function that sets the call site's ref to the element
|
|
3377
|
+
* it is given. The component applies it with its own `ref`, which runs
|
|
3378
|
+
* while the element is made, so the call site's `ref` can move to that
|
|
3379
|
+
* element. Every read has to be the whole `ref` of an element or of
|
|
3380
|
+
* another component, or a key of a spread, which becomes one.
|
|
3381
|
+
*/
|
|
3382
|
+
refValue(attribute, reads) {
|
|
3383
|
+
const value = attribute?.value;
|
|
3384
|
+
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return;
|
|
3385
|
+
for (const read of reads) {
|
|
3386
|
+
if (!isMemberRead(read)) continue;
|
|
3387
|
+
const container = this.context.parents.get(read.member);
|
|
3388
|
+
const owner = container ? this.context.parents.get(container) : void 0;
|
|
3389
|
+
if (container?.type !== "JSXExpressionContainer" || owner?.type !== "JSXAttribute" || owner.name.type !== "JSXIdentifier" || owner.name.name !== "ref") return;
|
|
3390
|
+
}
|
|
3391
|
+
return {
|
|
3392
|
+
kind: "expression",
|
|
3393
|
+
text: textOf(this.context, value.expression),
|
|
3394
|
+
primary: true
|
|
3395
|
+
};
|
|
3396
|
+
}
|
|
3397
|
+
/**
|
|
2997
3398
|
* How a prop reads in a copy of the component, and the `const` that stores
|
|
2998
3399
|
* it first when the value has to be evaluated once.
|
|
2999
3400
|
*/
|
|
@@ -3074,16 +3475,17 @@ var Inliner = class {
|
|
|
3074
3475
|
default: return false;
|
|
3075
3476
|
}
|
|
3076
3477
|
}
|
|
3077
|
-
freshName(
|
|
3478
|
+
freshName(name) {
|
|
3078
3479
|
const names = this.scratchNames ?? this.context.scopes.names;
|
|
3480
|
+
const base = name.replace(/\$\d+$/, "");
|
|
3079
3481
|
let index = 1;
|
|
3080
|
-
let
|
|
3081
|
-
while (names.has(
|
|
3482
|
+
let fresh = `${base}$${String(index)}`;
|
|
3483
|
+
while (names.has(fresh)) {
|
|
3082
3484
|
index += 1;
|
|
3083
|
-
|
|
3485
|
+
fresh = `${base}$${String(index)}`;
|
|
3084
3486
|
}
|
|
3085
|
-
names.add(
|
|
3086
|
-
return
|
|
3487
|
+
names.add(fresh);
|
|
3488
|
+
return fresh;
|
|
3087
3489
|
}
|
|
3088
3490
|
/**
|
|
3089
3491
|
* The copy of `component` that would replace `element`, or `undefined`
|
|
@@ -3108,7 +3510,7 @@ var Inliner = class {
|
|
|
3108
3510
|
this.reason = "call-this";
|
|
3109
3511
|
return;
|
|
3110
3512
|
}
|
|
3111
|
-
const views = this.resolveViews(component,
|
|
3513
|
+
const views = this.resolveViews(component, attributes, hasChildren);
|
|
3112
3514
|
if (!views) {
|
|
3113
3515
|
this.reason = "view-conflict";
|
|
3114
3516
|
return;
|
|
@@ -3122,15 +3524,15 @@ var Inliner = class {
|
|
|
3122
3524
|
readsByName.set(name, reads);
|
|
3123
3525
|
};
|
|
3124
3526
|
for (const read of views.propReads) count(read.name, read);
|
|
3125
|
-
for (const
|
|
3126
|
-
for (const spread of views.spreads) for (const [key, resolution] of spread.keys) if (resolution.kind !== "text") count(key, spread.spread);
|
|
3527
|
+
for (const spread of views.spreads) for (const [key, source] of spread.keys) if (source === void 0) count(key, spread.spread);
|
|
3127
3528
|
const values = /* @__PURE__ */ new Map();
|
|
3128
3529
|
for (const [name, reads] of readsByName) {
|
|
3129
3530
|
let value;
|
|
3130
3531
|
if (name === "children" && !attributes.has("children")) value = this.childrenValue(element, reads, fresh, snapshots);
|
|
3532
|
+
else if (name === "ref" && attributes.has("ref")) value = this.refValue(attributes.get("ref"), reads);
|
|
3131
3533
|
else value = this.propValue(attributes.get(name), reads, fresh, snapshots);
|
|
3132
3534
|
if (!value) {
|
|
3133
|
-
this.reason = "prop-value";
|
|
3535
|
+
this.reason = name === "ref" && attributes.has("ref") ? "ref-use" : "prop-value";
|
|
3134
3536
|
return;
|
|
3135
3537
|
}
|
|
3136
3538
|
values.set(name, value);
|
|
@@ -3203,7 +3605,7 @@ var Inliner = class {
|
|
|
3203
3605
|
const answering = [];
|
|
3204
3606
|
const seen = /* @__PURE__ */ new Set();
|
|
3205
3607
|
for (let node = this.context.parents.get(element); node; node = this.context.parents.get(node)) {
|
|
3206
|
-
const target = node.type === "JSXElement" ?
|
|
3608
|
+
const target = node.type === "JSXElement" ? contextBinding(this.context, node.openingElement.name) : void 0;
|
|
3207
3609
|
if (node.type !== "JSXElement" || !target || seen.has(target)) continue;
|
|
3208
3610
|
seen.add(target);
|
|
3209
3611
|
const entry = this.transparent.get(node);
|
|
@@ -3270,68 +3672,44 @@ var Inliner = class {
|
|
|
3270
3672
|
return changed;
|
|
3271
3673
|
}
|
|
3272
3674
|
/**
|
|
3273
|
-
* Resolves each view read and spread of a component at a call site.
|
|
3274
|
-
*
|
|
3275
|
-
*
|
|
3276
|
-
*
|
|
3277
|
-
*
|
|
3278
|
-
* holds `children` for an element that has some.
|
|
3675
|
+
* Resolves each view read and spread of a component at a call site. A
|
|
3676
|
+
* read becomes a read of the props, or the text of a default. A spread
|
|
3677
|
+
* becomes the keys it holds, in the order `merge()` gives them, and its
|
|
3678
|
+
* `children` become the element's children. `undefined` when a spread
|
|
3679
|
+
* repeats an attribute, or holds `children` for an element that has some.
|
|
3279
3680
|
*/
|
|
3280
|
-
resolveViews(component,
|
|
3681
|
+
resolveViews(component, attributes, hasChildren) {
|
|
3281
3682
|
const has = (key) => attributes.has(key) || key === "children" && hasChildren;
|
|
3282
|
-
const defined = (key) => {
|
|
3283
|
-
const attribute = attributes.get(key);
|
|
3284
|
-
if (attribute) {
|
|
3285
|
-
const { value } = attribute;
|
|
3286
|
-
if (value?.type !== "JSXExpressionContainer") return true;
|
|
3287
|
-
return value.expression.type !== "JSXEmptyExpression" && isDefinedExpression(value.expression);
|
|
3288
|
-
}
|
|
3289
|
-
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
3290
|
-
const only = significant.at(0);
|
|
3291
|
-
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return true;
|
|
3292
|
-
return only.expression.type !== "JSXEmptyExpression" && isDefinedExpression(only.expression);
|
|
3293
|
-
};
|
|
3294
3683
|
const resolve = (view, key) => {
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
const text = source
|
|
3299
|
-
if (text !== void 0) return
|
|
3300
|
-
|
|
3301
|
-
text
|
|
3302
|
-
};
|
|
3303
|
-
if (source && !source.defaults && has(key)) {
|
|
3304
|
-
if (defined(key)) return { kind: "props" };
|
|
3305
|
-
const fallback = sources.slice(0, index).findLast((earlier) => earlier.defaults?.has(key))?.defaults?.get(key);
|
|
3306
|
-
return fallback === void 0 ? { kind: "props" } : {
|
|
3307
|
-
kind: "fallback",
|
|
3308
|
-
text: fallback
|
|
3309
|
-
};
|
|
3310
|
-
}
|
|
3684
|
+
for (let index = view.sources.length - 1; index >= 0; index -= 1) {
|
|
3685
|
+
const source = view.sources.at(index);
|
|
3686
|
+
if (!source || source.hidden.has(key)) continue;
|
|
3687
|
+
const text = source.defaults?.get(key);
|
|
3688
|
+
if (text !== void 0) return text;
|
|
3689
|
+
if (!source.defaults && has(key)) return;
|
|
3311
3690
|
}
|
|
3312
|
-
return
|
|
3313
|
-
kind: "text",
|
|
3314
|
-
text: "void 0"
|
|
3315
|
-
};
|
|
3691
|
+
return "void 0";
|
|
3316
3692
|
};
|
|
3317
3693
|
const propReads = [...component.props];
|
|
3318
|
-
const fallbacks = /* @__PURE__ */ new Map();
|
|
3319
3694
|
const texts = /* @__PURE__ */ new Map();
|
|
3320
3695
|
for (const read of component.viewReads) {
|
|
3321
|
-
const
|
|
3322
|
-
if (
|
|
3323
|
-
else
|
|
3324
|
-
else texts.set(read.member, resolution.text);
|
|
3696
|
+
const text = resolve(read.view, read.name);
|
|
3697
|
+
if (text === void 0) propReads.push(read);
|
|
3698
|
+
else texts.set(read.member, text);
|
|
3325
3699
|
}
|
|
3326
3700
|
const callKeys = [...attributes.keys()];
|
|
3327
3701
|
if (hasChildren && !attributes.has("children")) callKeys.push("children");
|
|
3328
3702
|
const spreads = [];
|
|
3329
3703
|
const taken = /* @__PURE__ */ new Map();
|
|
3330
3704
|
for (const spread of component.viewSpreads) {
|
|
3331
|
-
const order =
|
|
3705
|
+
const order = [];
|
|
3332
3706
|
for (const source of spread.view.sources) {
|
|
3333
3707
|
const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
|
|
3334
|
-
for (const key of keys.filter((name) =>
|
|
3708
|
+
for (const key of keys.filter((name) => !source.hidden.has(name))) {
|
|
3709
|
+
const index = order.indexOf(key);
|
|
3710
|
+
if (index !== -1) order.splice(index, 1);
|
|
3711
|
+
order.push(key);
|
|
3712
|
+
}
|
|
3335
3713
|
}
|
|
3336
3714
|
let names = taken.get(spread.element);
|
|
3337
3715
|
if (!names) {
|
|
@@ -3341,10 +3719,9 @@ var Inliner = class {
|
|
|
3341
3719
|
}
|
|
3342
3720
|
const keys = /* @__PURE__ */ new Map();
|
|
3343
3721
|
for (const key of order) {
|
|
3344
|
-
|
|
3345
|
-
if (!(key === "children" ? spread.element.openingElement.selfClosing && resolution.kind !== "fallback" : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
|
|
3722
|
+
if (!(key === "children" ? spread.element.openingElement.selfClosing : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
|
|
3346
3723
|
names.add(key);
|
|
3347
|
-
keys.set(key,
|
|
3724
|
+
keys.set(key, resolve(spread.view, key));
|
|
3348
3725
|
}
|
|
3349
3726
|
spreads.push({
|
|
3350
3727
|
spread,
|
|
@@ -3353,7 +3730,6 @@ var Inliner = class {
|
|
|
3353
3730
|
}
|
|
3354
3731
|
return {
|
|
3355
3732
|
propReads,
|
|
3356
|
-
fallbacks,
|
|
3357
3733
|
texts,
|
|
3358
3734
|
spreads
|
|
3359
3735
|
};
|
|
@@ -3367,23 +3743,13 @@ var Inliner = class {
|
|
|
3367
3743
|
const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
3368
3744
|
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
3369
3745
|
}
|
|
3370
|
-
for (const [read, fallback] of views.fallbacks) {
|
|
3371
|
-
const text = values.get(read.name)?.text ?? "void 0";
|
|
3372
|
-
copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
|
|
3373
|
-
}
|
|
3374
3746
|
for (const { spread, keys } of views.spreads) {
|
|
3375
3747
|
const parts = [];
|
|
3376
3748
|
let children;
|
|
3377
|
-
for (const [key,
|
|
3749
|
+
for (const [key, text] of keys) {
|
|
3378
3750
|
const value = values.get(key);
|
|
3379
|
-
if (key === "children")
|
|
3380
|
-
|
|
3381
|
-
continue;
|
|
3382
|
-
}
|
|
3383
|
-
let text = value?.text ?? "void 0";
|
|
3384
|
-
if (resolution.kind === "text") text = resolution.text;
|
|
3385
|
-
else if (resolution.kind === "fallback") text = withFallback(text, resolution.text);
|
|
3386
|
-
parts.push(`${key}={${text}}`);
|
|
3751
|
+
if (key === "children") children = this.spreadChildren(element, text, value);
|
|
3752
|
+
else parts.push(`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
3387
3753
|
}
|
|
3388
3754
|
const { attribute } = spread;
|
|
3389
3755
|
if (parts.length === 0) copy.remove(attribute.start, attribute.end);
|
|
@@ -3401,8 +3767,8 @@ var Inliner = class {
|
|
|
3401
3767
|
* The JSX children a spread's `children` becomes: the call site's own
|
|
3402
3768
|
* children, or the value of a single expression or default.
|
|
3403
3769
|
*/
|
|
3404
|
-
spreadChildren(element,
|
|
3405
|
-
if (
|
|
3770
|
+
spreadChildren(element, text, value) {
|
|
3771
|
+
if (text !== void 0) return text === "void 0" ? void 0 : `{${text}}`;
|
|
3406
3772
|
if (!value) return;
|
|
3407
3773
|
if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
3408
3774
|
return `{${value.text}}`;
|
|
@@ -3563,7 +3929,7 @@ var Inliner = class {
|
|
|
3563
3929
|
*/
|
|
3564
3930
|
sizeHeader() {
|
|
3565
3931
|
if (this.header === void 0) {
|
|
3566
|
-
const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration").map((statement) => this.context.code.slice(statement.start, statement.end));
|
|
3932
|
+
const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration" || isDataDeclaration(statement)).map((statement) => this.context.code.slice(statement.start, statement.end));
|
|
3567
3933
|
const aliases = [...this.context.options.builtInAliases.keys()];
|
|
3568
3934
|
if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
|
|
3569
3935
|
this.header = imports.join("\n");
|
|
@@ -3662,7 +4028,6 @@ var Inliner = class {
|
|
|
3662
4028
|
}
|
|
3663
4029
|
callReason(element, rejected, siteReasons) {
|
|
3664
4030
|
const tag = element.openingElement.name;
|
|
3665
|
-
if (providerContext(this.context, tag)) return;
|
|
3666
4031
|
if (tag.type === "JSXMemberExpression") return {
|
|
3667
4032
|
reason: "member-tag",
|
|
3668
4033
|
name: textOf(this.context, tag)
|
|
@@ -3671,7 +4036,7 @@ var Inliner = class {
|
|
|
3671
4036
|
const { name } = tag;
|
|
3672
4037
|
const binding = this.context.scopes.references.get(tag);
|
|
3673
4038
|
const identity = binding ? solidExport(binding, this.context.options) : name;
|
|
3674
|
-
if (identity !== void 0 && this.context.options.builtIns.has(identity)) return;
|
|
4039
|
+
if (identity !== void 0 && this.context.options.builtIns.has(identity) || contextBinding(this.context, tag)) return;
|
|
3675
4040
|
if (!binding) return {
|
|
3676
4041
|
reason: "global",
|
|
3677
4042
|
name
|
|
@@ -3725,7 +4090,7 @@ const TRACKED_PROPS = /* @__PURE__ */ new Map([
|
|
|
3725
4090
|
["Show", "when"],
|
|
3726
4091
|
["Match", "when"],
|
|
3727
4092
|
["For", "each"],
|
|
3728
|
-
["
|
|
4093
|
+
["Repeat", "count"]
|
|
3729
4094
|
]);
|
|
3730
4095
|
/**
|
|
3731
4096
|
* Whether evaluating `node` always creates a new value, which reference
|
|
@@ -3751,8 +4116,19 @@ function isFresh(node) {
|
|
|
3751
4116
|
}
|
|
3752
4117
|
}
|
|
3753
4118
|
/**
|
|
3754
|
-
* The
|
|
4119
|
+
* The text of an expression that always gives the same primitive: a literal
|
|
4120
|
+
* other than a regular expression, a template literal without expressions,
|
|
4121
|
+
* or a negated number.
|
|
3755
4122
|
*/
|
|
4123
|
+
function constantText(context, node) {
|
|
4124
|
+
const expression = unwrap(node);
|
|
4125
|
+
switch (expression.type) {
|
|
4126
|
+
case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
|
|
4127
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
|
|
4128
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
|
|
4129
|
+
default: return;
|
|
4130
|
+
}
|
|
4131
|
+
}
|
|
3756
4132
|
function returnedExpression(fn) {
|
|
3757
4133
|
if (fn.async || fn.generator || fn.params.length > 0 || !fn.body) return;
|
|
3758
4134
|
if (fn.body.type !== "BlockStatement") return fn.body;
|
|
@@ -3767,22 +4143,29 @@ var MemoInliner = class {
|
|
|
3767
4143
|
this.changed = false;
|
|
3768
4144
|
this.reason = "";
|
|
3769
4145
|
}
|
|
3770
|
-
|
|
3771
|
-
|
|
4146
|
+
/** Whether a callee is Solid's `createMemo` or `children`. */
|
|
4147
|
+
memoKind(callee) {
|
|
4148
|
+
if (callee.type !== "Identifier") return;
|
|
3772
4149
|
const binding = this.context.scopes.references.get(callee);
|
|
3773
|
-
|
|
4150
|
+
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
4151
|
+
return identity === "createMemo" || identity === "children" ? identity : void 0;
|
|
3774
4152
|
}
|
|
3775
4153
|
/**
|
|
3776
|
-
* The
|
|
4154
|
+
* The expression a memo's computation returns, and whether `equals: false`
|
|
4155
|
+
* makes it notify on every run.
|
|
3777
4156
|
*/
|
|
3778
|
-
|
|
4157
|
+
computation(declarator) {
|
|
3779
4158
|
const init = declarator.init ? unwrap(declarator.init) : void 0;
|
|
3780
|
-
|
|
4159
|
+
const kind = init?.type === "CallExpression" ? this.memoKind(init.callee) : void 0;
|
|
4160
|
+
if (init?.type !== "CallExpression" || kind === void 0) return;
|
|
3781
4161
|
const args = init.arguments;
|
|
4162
|
+
if (kind === "children" && args.length > 1) {
|
|
4163
|
+
this.reason = "memo-options";
|
|
4164
|
+
return;
|
|
4165
|
+
}
|
|
3782
4166
|
const compute = args.at(0);
|
|
3783
|
-
const
|
|
3784
|
-
|
|
3785
|
-
if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) {
|
|
4167
|
+
const options = args.at(1);
|
|
4168
|
+
if (args.length > 2 || compute === void 0 || compute.type === "SpreadElement") {
|
|
3786
4169
|
this.reason = "memo-computation";
|
|
3787
4170
|
return;
|
|
3788
4171
|
}
|
|
@@ -3809,8 +4192,40 @@ var MemoInliner = class {
|
|
|
3809
4192
|
}
|
|
3810
4193
|
alwaysNotifies = true;
|
|
3811
4194
|
}
|
|
3812
|
-
|
|
3813
|
-
|
|
4195
|
+
return {
|
|
4196
|
+
expression,
|
|
4197
|
+
alwaysNotifies,
|
|
4198
|
+
kind
|
|
4199
|
+
};
|
|
4200
|
+
}
|
|
4201
|
+
/**
|
|
4202
|
+
* Replaces every read of a memo whose computation is a literal with the
|
|
4203
|
+
* literal. Each reference has to be a call after the declaration, since a
|
|
4204
|
+
* call before it would throw.
|
|
4205
|
+
*/
|
|
4206
|
+
inlineConstant(declarator, binding, text) {
|
|
4207
|
+
const statement = this.context.parents.get(declarator);
|
|
4208
|
+
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
|
|
4209
|
+
this.reason = "memo-declaration";
|
|
4210
|
+
return;
|
|
4211
|
+
}
|
|
4212
|
+
const calls = [];
|
|
4213
|
+
for (const reference of binding.references) {
|
|
4214
|
+
const call = this.context.parents.get(reference);
|
|
4215
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
|
|
4216
|
+
this.reason = "memo-constant";
|
|
4217
|
+
return;
|
|
4218
|
+
}
|
|
4219
|
+
calls.push(call);
|
|
4220
|
+
}
|
|
4221
|
+
for (const call of calls) {
|
|
4222
|
+
const parent = this.context.parents.get(call);
|
|
4223
|
+
const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
|
|
4224
|
+
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
4225
|
+
}
|
|
4226
|
+
this.context.s.remove(statement.start, statement.end);
|
|
4227
|
+
this.changed = true;
|
|
4228
|
+
return true;
|
|
3814
4229
|
}
|
|
3815
4230
|
/**
|
|
3816
4231
|
* The function a node runs in.
|
|
@@ -3820,6 +4235,12 @@ var MemoInliner = class {
|
|
|
3820
4235
|
while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
|
|
3821
4236
|
return current;
|
|
3822
4237
|
}
|
|
4238
|
+
/** The JSX expression container a call is the whole of. */
|
|
4239
|
+
containerOf(call) {
|
|
4240
|
+
let parent = this.context.parents.get(call);
|
|
4241
|
+
while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
|
|
4242
|
+
return parent?.type === "JSXExpressionContainer" ? parent : void 0;
|
|
4243
|
+
}
|
|
3823
4244
|
/**
|
|
3824
4245
|
* Whether a call is the whole of an expression that Solid tracks, and that
|
|
3825
4246
|
* runs once each time `fn` runs.
|
|
@@ -3909,8 +4330,20 @@ var MemoInliner = class {
|
|
|
3909
4330
|
if (this.inline(declarator, binding) === void 0 && this.reason !== "") recordSkip(this.context, "memos", this.reason, binding.name);
|
|
3910
4331
|
}
|
|
3911
4332
|
inline(declarator, binding) {
|
|
3912
|
-
const
|
|
3913
|
-
if (!
|
|
4333
|
+
const found = this.computation(declarator);
|
|
4334
|
+
if (!found) return;
|
|
4335
|
+
const { expression, alwaysNotifies, kind } = found;
|
|
4336
|
+
if (kind === "children") {
|
|
4337
|
+
const root = unwrap(expression);
|
|
4338
|
+
if (root.type !== "JSXElement" && root.type !== "JSXFragment") {
|
|
4339
|
+
this.reason = "children-dynamic";
|
|
4340
|
+
return;
|
|
4341
|
+
}
|
|
4342
|
+
} else {
|
|
4343
|
+
const constant = constantText(this.context, expression);
|
|
4344
|
+
if (constant !== void 0) return this.inlineConstant(declarator, binding, constant);
|
|
4345
|
+
if (!alwaysNotifies && !isFresh(expression)) return;
|
|
4346
|
+
}
|
|
3914
4347
|
if (binding.references.length !== 1) {
|
|
3915
4348
|
this.reason = "memo-reads";
|
|
3916
4349
|
return;
|
|
@@ -3931,11 +4364,18 @@ var MemoInliner = class {
|
|
|
3931
4364
|
this.reason = "memo-read-place";
|
|
3932
4365
|
return;
|
|
3933
4366
|
}
|
|
4367
|
+
const container = this.containerOf(call);
|
|
4368
|
+
const owner = container ? this.context.parents.get(container) : void 0;
|
|
4369
|
+
if (kind === "children" && (!container || owner?.type !== "JSXElement" && owner?.type !== "JSXFragment")) {
|
|
4370
|
+
this.reason = "memo-read-place";
|
|
4371
|
+
return;
|
|
4372
|
+
}
|
|
3934
4373
|
if (!this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) {
|
|
3935
4374
|
this.reason = "memo-scope";
|
|
3936
4375
|
return;
|
|
3937
4376
|
}
|
|
3938
|
-
this.context.s.overwrite(
|
|
4377
|
+
if (kind === "children" && container) this.context.s.overwrite(container.start, container.end, textOf(this.context, expression));
|
|
4378
|
+
else this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
|
|
3939
4379
|
this.context.s.remove(statement.start, statement.end);
|
|
3940
4380
|
this.changed = true;
|
|
3941
4381
|
return true;
|
|
@@ -3959,181 +4399,20 @@ function inlineMemos(context) {
|
|
|
3959
4399
|
return inliner.changed;
|
|
3960
4400
|
}
|
|
3961
4401
|
//#endregion
|
|
3962
|
-
//#region src/server.ts
|
|
3963
|
-
/** Calls the server does not run. */
|
|
3964
|
-
const REMOVED = /* @__PURE__ */ new Set(["createEffect", "onMount"]);
|
|
3965
|
-
/** Calls the server runs once, in place. */
|
|
3966
|
-
const RUN_ONCE = /* @__PURE__ */ new Set(["createRenderEffect", "createComputed"]);
|
|
3967
|
-
/**
|
|
3968
|
-
* Whether a statement starting with `text` could join the statement before
|
|
3969
|
-
* it when that one has no semicolon.
|
|
3970
|
-
*/
|
|
3971
|
-
function continuesPreviousStatement(text) {
|
|
3972
|
-
return /^[([`+\-/]/.test(text);
|
|
3973
|
-
}
|
|
3974
|
-
var ServerSimplifier = class {
|
|
3975
|
-
constructor(context) {
|
|
3976
|
-
this.context = context;
|
|
3977
|
-
this.changed = false;
|
|
3978
|
-
}
|
|
3979
|
-
/**
|
|
3980
|
-
* The Solid export a callee refers to, through a named import, an alias,
|
|
3981
|
-
* or a namespace import like `solid.untrack`.
|
|
3982
|
-
*/
|
|
3983
|
-
calleeName(callee) {
|
|
3984
|
-
const { references } = this.context.scopes;
|
|
3985
|
-
if (callee.type === "Identifier") {
|
|
3986
|
-
const binding = references.get(callee);
|
|
3987
|
-
return binding ? solidExport(binding, this.context.options) : void 0;
|
|
3988
|
-
}
|
|
3989
|
-
if (callee.type !== "MemberExpression" || callee.object.type !== "Identifier") return;
|
|
3990
|
-
const binding = references.get(callee.object);
|
|
3991
|
-
if (binding?.kind !== "import" || binding.imported !== "*" || binding.source === void 0 || !this.context.options.moduleSources.includes(binding.source)) return;
|
|
3992
|
-
if (!callee.computed && callee.property.type === "Identifier") return callee.property.name;
|
|
3993
|
-
if (callee.computed && callee.property.type === "Literal" && typeof callee.property.value === "string") return callee.property.value;
|
|
3994
|
-
}
|
|
3995
|
-
/**
|
|
3996
|
-
* Whether dropping `node` loses nothing observable. Solid's `on` only
|
|
3997
|
-
* creates a function, so `createEffect(on(source, fn))` can go too.
|
|
3998
|
-
*/
|
|
3999
|
-
isDiscardable(node) {
|
|
4000
|
-
if (isSideEffectFree(node)) return true;
|
|
4001
|
-
const inner = node.type === "ParenthesizedExpression" ? unwrap(node) : node;
|
|
4002
|
-
return inner.type === "CallExpression" && this.calleeName(inner.callee) === "on" && inner.arguments.every((argument) => argument.type !== "SpreadElement" && this.isDiscardable(argument));
|
|
4003
|
-
}
|
|
4004
|
-
/**
|
|
4005
|
-
* The text of calling `fn` with `args`, the way Solid's server calls it,
|
|
4006
|
-
* and whether it binds as tightly as a call does.
|
|
4007
|
-
*/
|
|
4008
|
-
callText(fn, args) {
|
|
4009
|
-
const inner = unwrap(fn);
|
|
4010
|
-
const argsText = args.map((argument) => textOf(this.context, argument)).join(", ");
|
|
4011
|
-
if (inner.type === "ArrowFunctionExpression" && inner.expression && inner.params.length === 0 && args.length === 0 && inner.body.type !== "BlockStatement") {
|
|
4012
|
-
const { body } = inner;
|
|
4013
|
-
return {
|
|
4014
|
-
text: textOf(this.context, body),
|
|
4015
|
-
call: isPrimary(body) || body.type === "CallExpression"
|
|
4016
|
-
};
|
|
4017
|
-
}
|
|
4018
|
-
const text = textOf(this.context, fn);
|
|
4019
|
-
if (inner.type === "Identifier") return {
|
|
4020
|
-
text: `${text}(${argsText})`,
|
|
4021
|
-
call: true
|
|
4022
|
-
};
|
|
4023
|
-
if (inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression" || fn.type === "ParenthesizedExpression") return {
|
|
4024
|
-
text: `(${text})(${argsText})`,
|
|
4025
|
-
call: true
|
|
4026
|
-
};
|
|
4027
|
-
return {
|
|
4028
|
-
text: `(0, ${text})(${argsText})`,
|
|
4029
|
-
call: true
|
|
4030
|
-
};
|
|
4031
|
-
}
|
|
4032
|
-
/**
|
|
4033
|
-
* Whether a statement at `position` could be read as a continuation of
|
|
4034
|
-
* the one before it, which happens when that one has no semicolon.
|
|
4035
|
-
*/
|
|
4036
|
-
followsOpenStatement(position) {
|
|
4037
|
-
const before = this.context.code.slice(0, position).trimEnd();
|
|
4038
|
-
return before !== "" && !before.endsWith(";") && !before.endsWith("{");
|
|
4039
|
-
}
|
|
4040
|
-
replace(call, text, primary = false) {
|
|
4041
|
-
const parent = this.context.parents.get(call);
|
|
4042
|
-
if (parent?.type === "ExpressionStatement") {
|
|
4043
|
-
if (text === void 0) {
|
|
4044
|
-
const list = this.context.parents.get(parent)?.type;
|
|
4045
|
-
if (list === "Program" || list === "BlockStatement" || list === "StaticBlock" || list === "SwitchCase") this.context.s.remove(parent.start, parent.end);
|
|
4046
|
-
else this.context.s.overwrite(parent.start, parent.end, ";");
|
|
4047
|
-
} else {
|
|
4048
|
-
let statement = text;
|
|
4049
|
-
if (/^(\{|function\b|async function\b|class\b)/.test(statement)) statement = `(${statement})`;
|
|
4050
|
-
const guarded = continuesPreviousStatement(statement) && this.followsOpenStatement(parent.start);
|
|
4051
|
-
this.context.s.overwrite(call.start, call.end, guarded ? `;${statement}` : statement);
|
|
4052
|
-
}
|
|
4053
|
-
} else {
|
|
4054
|
-
const value = text ?? "void 0";
|
|
4055
|
-
const bare = !(parent?.type === "NewExpression" && parent.callee === call) && (primary || acceptsAnyExpression(this.context, call));
|
|
4056
|
-
this.context.s.overwrite(call.start, call.end, bare ? value : `(${value})`);
|
|
4057
|
-
}
|
|
4058
|
-
this.changed = true;
|
|
4059
|
-
}
|
|
4060
|
-
simplify(call) {
|
|
4061
|
-
const name = this.calleeName(call.callee);
|
|
4062
|
-
if (name === void 0 || call.arguments.some((arg) => arg.type === "SpreadElement")) return;
|
|
4063
|
-
const args = call.arguments.filter((arg) => arg.type !== "SpreadElement");
|
|
4064
|
-
const first = args.at(0);
|
|
4065
|
-
const second = args.at(1);
|
|
4066
|
-
const rest = args.slice(2);
|
|
4067
|
-
const inStatement = this.context.parents.get(call)?.type === "ExpressionStatement";
|
|
4068
|
-
if (REMOVED.has(name)) {
|
|
4069
|
-
if (args.every((arg) => this.isDiscardable(arg))) this.replace(call, void 0);
|
|
4070
|
-
return;
|
|
4071
|
-
}
|
|
4072
|
-
switch (name) {
|
|
4073
|
-
case "getListener":
|
|
4074
|
-
if (args.length === 0) this.replace(call, "null");
|
|
4075
|
-
return;
|
|
4076
|
-
case "untrack":
|
|
4077
|
-
case "batch":
|
|
4078
|
-
if (first && args.length === 1) {
|
|
4079
|
-
const run = this.callText(first, []);
|
|
4080
|
-
this.replace(call, run.text, run.call);
|
|
4081
|
-
}
|
|
4082
|
-
return;
|
|
4083
|
-
case "startTransition":
|
|
4084
|
-
if (first && args.length === 1) {
|
|
4085
|
-
const { text } = this.callText(first, []);
|
|
4086
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
4087
|
-
}
|
|
4088
|
-
return;
|
|
4089
|
-
case "createDeferred":
|
|
4090
|
-
if (first && args.slice(1).every((arg) => this.isDiscardable(arg))) {
|
|
4091
|
-
const text = textOf(this.context, first);
|
|
4092
|
-
this.replace(call, text, isPrimary(first));
|
|
4093
|
-
}
|
|
4094
|
-
return;
|
|
4095
|
-
case "createMemo":
|
|
4096
|
-
if (first && rest.every((arg) => this.isDiscardable(arg))) {
|
|
4097
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
4098
|
-
this.replace(call, `((value) => () => value)(${text})`, true);
|
|
4099
|
-
}
|
|
4100
|
-
return;
|
|
4101
|
-
default: if (RUN_ONCE.has(name) && first && rest.length === 0) {
|
|
4102
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
4103
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
4104
|
-
}
|
|
4105
|
-
}
|
|
4106
|
-
}
|
|
4107
|
-
walk(node) {
|
|
4108
|
-
forEachChild(node, (child) => {
|
|
4109
|
-
this.walk(child);
|
|
4110
|
-
});
|
|
4111
|
-
if (node.type === "CallExpression") this.simplify(node);
|
|
4112
|
-
}
|
|
4113
|
-
};
|
|
4114
|
-
/**
|
|
4115
|
-
* Runs the server pass over a parsed program. Returns whether anything changed.
|
|
4116
|
-
*/
|
|
4117
|
-
function simplifyServer(context) {
|
|
4118
|
-
const simplifier = new ServerSimplifier(context);
|
|
4119
|
-
simplifier.walk(context.program);
|
|
4120
|
-
return simplifier.changed;
|
|
4121
|
-
}
|
|
4122
|
-
//#endregion
|
|
4123
4402
|
//#region src/compile.ts
|
|
4124
4403
|
const DEFAULT_BUILT_INS = [
|
|
4125
4404
|
"For",
|
|
4126
4405
|
"Show",
|
|
4127
4406
|
"Switch",
|
|
4128
4407
|
"Match",
|
|
4129
|
-
"
|
|
4130
|
-
"
|
|
4408
|
+
"Loading",
|
|
4409
|
+
"Reveal",
|
|
4131
4410
|
"Portal",
|
|
4132
|
-
"
|
|
4411
|
+
"Repeat",
|
|
4133
4412
|
"Dynamic",
|
|
4134
|
-
"
|
|
4413
|
+
"Errored"
|
|
4135
4414
|
];
|
|
4136
|
-
const DEFAULT_MODULE_SOURCES = ["solid-js", "
|
|
4415
|
+
const DEFAULT_MODULE_SOURCES = ["solid-js", "@solidjs/web"];
|
|
4137
4416
|
function resolveOptions(options) {
|
|
4138
4417
|
return {
|
|
4139
4418
|
builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
|
|
@@ -4141,6 +4420,7 @@ function resolveOptions(options) {
|
|
|
4141
4420
|
builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
|
|
4142
4421
|
constantVars: options.constantVars ?? false,
|
|
4143
4422
|
importedConstants: resolveImportedConstants(options.importedConstants),
|
|
4423
|
+
globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
|
|
4144
4424
|
importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
|
|
4145
4425
|
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
4146
4426
|
alwaysInline: options.alwaysInline ?? false
|
|
@@ -4222,13 +4502,6 @@ function compile(code, options = {}) {
|
|
|
4222
4502
|
}
|
|
4223
4503
|
if (!changed) break;
|
|
4224
4504
|
}
|
|
4225
|
-
if (options.server ?? false) {
|
|
4226
|
-
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, simplifyServer, void 0);
|
|
4227
|
-
if (result) {
|
|
4228
|
-
current = result.code;
|
|
4229
|
-
if (result.map !== void 0) maps.push(result.map);
|
|
4230
|
-
}
|
|
4231
|
-
}
|
|
4232
4505
|
const report = skipped ? { skipped } : {};
|
|
4233
4506
|
if (maps.length === 0) return {
|
|
4234
4507
|
code: current,
|
|
@@ -4245,7 +4518,7 @@ function compile(code, options = {}) {
|
|
|
4245
4518
|
* Reads what a module imports and the constants it exports, so that
|
|
4246
4519
|
* `compile` can fold them in the modules that import it.
|
|
4247
4520
|
*
|
|
4248
|
-
* Only `filename` and `
|
|
4521
|
+
* Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
|
|
4249
4522
|
*/
|
|
4250
4523
|
function readModuleConstants(code, options = {}) {
|
|
4251
4524
|
const program = parse(options.filename ?? "input.jsx", code);
|
|
@@ -4258,4 +4531,4 @@ function readModuleConstants(code, options = {}) {
|
|
|
4258
4531
|
//#endregion
|
|
4259
4532
|
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 };
|
|
4260
4533
|
|
|
4261
|
-
//# sourceMappingURL=compile-
|
|
4534
|
+
//# sourceMappingURL=compile-BpoiBa6b.mjs.map
|