solid-optimizer 0.4.0 → 1.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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-D3caSUtw.mjs → compile-Cz6hbiQv.mjs} +943 -670
- package/dist/compile-Cz6hbiQv.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-D3caSUtw.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.
|
|
@@ -1043,6 +1071,189 @@ function innerText(s, node) {
|
|
|
1043
1071
|
return s.slice(node.openingElement.end, node.closingElement.start);
|
|
1044
1072
|
}
|
|
1045
1073
|
//#endregion
|
|
1074
|
+
//#region src/size.ts
|
|
1075
|
+
/** A template's declaration, and the function that clones and returns it. */
|
|
1076
|
+
const TEMPLATE = 40;
|
|
1077
|
+
/** A walk to an element, like `e.firstChild.nextSibling`. */
|
|
1078
|
+
const WALK = 12;
|
|
1079
|
+
/** An `insert()` of a dynamic child. */
|
|
1080
|
+
const INSERT = 12;
|
|
1081
|
+
/** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
|
|
1082
|
+
const EFFECT = 24;
|
|
1083
|
+
/** An event handler assignment, like `e.$$click = ...`. */
|
|
1084
|
+
const HANDLER = 10;
|
|
1085
|
+
/** A `spread()` of props onto an element. */
|
|
1086
|
+
const SPREAD = 16;
|
|
1087
|
+
/** A `createComponent(Name, {})` call. */
|
|
1088
|
+
const COMPONENT = 8;
|
|
1089
|
+
/** A getter, `get name() { return ... }`. */
|
|
1090
|
+
const GETTER = 16;
|
|
1091
|
+
/** A `mergeProps()` around the props of a component with a spread. */
|
|
1092
|
+
const MERGE = 6;
|
|
1093
|
+
/**
|
|
1094
|
+
* The length of `text` once a minifier removes the whitespace it does not need.
|
|
1095
|
+
*/
|
|
1096
|
+
function compactLength(text) {
|
|
1097
|
+
return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
|
|
1098
|
+
}
|
|
1099
|
+
function nameLength(name) {
|
|
1100
|
+
switch (name.type) {
|
|
1101
|
+
case "JSXIdentifier": return name.name.length;
|
|
1102
|
+
case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
|
|
1103
|
+
case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
|
|
1104
|
+
default: return 4;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
/** Whether an expression lowers to text in the template, like a literal. */
|
|
1108
|
+
function isStaticText(node) {
|
|
1109
|
+
if (node.type === "JSXEmptyExpression") return false;
|
|
1110
|
+
const inner = unwrap(node);
|
|
1111
|
+
if (inner.type === "Literal") return typeof inner.value === "string" || typeof inner.value === "number";
|
|
1112
|
+
if (inner.type === "TemplateLiteral") return inner.expressions.length === 0;
|
|
1113
|
+
return inner.type === "UnaryExpression" && inner.operator === "-" && inner.argument.type === "Literal" && typeof inner.argument.value === "number";
|
|
1114
|
+
}
|
|
1115
|
+
function isIntrinsic(element) {
|
|
1116
|
+
const { name } = element.openingElement;
|
|
1117
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
1118
|
+
}
|
|
1119
|
+
function attributeName(attribute) {
|
|
1120
|
+
const { name } = attribute;
|
|
1121
|
+
return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Estimates lowered sizes in one piece of code.
|
|
1125
|
+
*/
|
|
1126
|
+
var SizeEstimator = class {
|
|
1127
|
+
constructor(code) {
|
|
1128
|
+
this.code = code;
|
|
1129
|
+
}
|
|
1130
|
+
text(node) {
|
|
1131
|
+
return compactLength(this.code.slice(node.start, node.end));
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* The size of any node: JSX as lowered, other code as its compact text
|
|
1135
|
+
* with the JSX in it as lowered.
|
|
1136
|
+
*/
|
|
1137
|
+
node(node) {
|
|
1138
|
+
if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
|
|
1139
|
+
let size = this.text(node);
|
|
1140
|
+
const visit = (child) => {
|
|
1141
|
+
if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
1142
|
+
size += this.jsx(child, false) - this.text(child);
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
forEachChild(child, visit);
|
|
1146
|
+
};
|
|
1147
|
+
forEachChild(node, visit);
|
|
1148
|
+
return size;
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* The size of a JSX element or fragment. `inTemplate` is whether it sits
|
|
1152
|
+
* directly in an element, whose template it joins.
|
|
1153
|
+
*/
|
|
1154
|
+
jsx(node, inTemplate) {
|
|
1155
|
+
if (node.type === "JSXFragment") return this.children(node.children) + 2;
|
|
1156
|
+
if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
|
|
1157
|
+
const tag = nameLength(node.openingElement.name);
|
|
1158
|
+
let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
|
|
1159
|
+
let dynamic = false;
|
|
1160
|
+
for (const attribute of node.openingElement.attributes) {
|
|
1161
|
+
const cost = this.elementAttribute(attribute);
|
|
1162
|
+
size += cost.size;
|
|
1163
|
+
dynamic ||= cost.dynamic;
|
|
1164
|
+
}
|
|
1165
|
+
for (const child of node.children) {
|
|
1166
|
+
if (isInsignificant(child)) continue;
|
|
1167
|
+
if (child.type === "JSXText") size += compactLength(child.value);
|
|
1168
|
+
else if (child.type === "JSXExpressionContainer" && isStaticText(child.expression)) size += this.text(child.expression);
|
|
1169
|
+
else if (child.type === "JSXElement") size += this.jsx(child, true);
|
|
1170
|
+
else {
|
|
1171
|
+
size += INSERT + this.child(child);
|
|
1172
|
+
dynamic = true;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
return size + (dynamic ? WALK : 0);
|
|
1176
|
+
}
|
|
1177
|
+
elementAttribute(attribute) {
|
|
1178
|
+
if (attribute.type === "JSXSpreadAttribute") return {
|
|
1179
|
+
size: SPREAD + this.node(attribute.argument),
|
|
1180
|
+
dynamic: true
|
|
1181
|
+
};
|
|
1182
|
+
const name = attributeName(attribute);
|
|
1183
|
+
const { value } = attribute;
|
|
1184
|
+
if (!value || value.type === "Literal") return {
|
|
1185
|
+
size: name.length + (value ? this.text(value) : 0) + 1,
|
|
1186
|
+
dynamic: false
|
|
1187
|
+
};
|
|
1188
|
+
if (value.type !== "JSXExpressionContainer") return {
|
|
1189
|
+
size: name.length + 1 + this.jsx(value, false),
|
|
1190
|
+
dynamic: true
|
|
1191
|
+
};
|
|
1192
|
+
const { expression } = value;
|
|
1193
|
+
if (expression.type === "JSXEmptyExpression") return {
|
|
1194
|
+
size: 0,
|
|
1195
|
+
dynamic: false
|
|
1196
|
+
};
|
|
1197
|
+
const inner = unwrap(expression);
|
|
1198
|
+
if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
|
|
1199
|
+
size: name.length + this.text(inner) + 1,
|
|
1200
|
+
dynamic: false
|
|
1201
|
+
};
|
|
1202
|
+
return {
|
|
1203
|
+
size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
|
|
1204
|
+
dynamic: true
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
child(child) {
|
|
1208
|
+
if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
|
|
1209
|
+
if (child.type === "JSXSpreadChild") return this.node(child.expression);
|
|
1210
|
+
if (child.type === "JSXText") return compactLength(child.value) + 2;
|
|
1211
|
+
return this.jsx(child, false);
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Children outside a template, which lower to an array of values.
|
|
1215
|
+
*/
|
|
1216
|
+
children(children) {
|
|
1217
|
+
let size = 0;
|
|
1218
|
+
for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
|
|
1219
|
+
return size;
|
|
1220
|
+
}
|
|
1221
|
+
component(element) {
|
|
1222
|
+
let size = COMPONENT + nameLength(element.openingElement.name);
|
|
1223
|
+
let spread = false;
|
|
1224
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1225
|
+
if (attribute.type === "JSXSpreadAttribute") {
|
|
1226
|
+
spread = true;
|
|
1227
|
+
size += this.node(attribute.argument) + 1;
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1230
|
+
const name = attributeName(attribute);
|
|
1231
|
+
const { value } = attribute;
|
|
1232
|
+
if (!value || value.type === "Literal") size += name.length + 1 + (value ? this.text(value) : 4);
|
|
1233
|
+
else if (value.type === "JSXExpressionContainer") {
|
|
1234
|
+
const { expression } = value;
|
|
1235
|
+
if (expression.type !== "JSXEmptyExpression") {
|
|
1236
|
+
const inner = unwrap(expression);
|
|
1237
|
+
const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
1238
|
+
size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
|
|
1239
|
+
}
|
|
1240
|
+
} else size += name.length + GETTER + this.jsx(value, false);
|
|
1241
|
+
}
|
|
1242
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
1243
|
+
if (significant.length > 0) size += 24 + this.children(significant);
|
|
1244
|
+
return size + (spread ? MERGE : 0);
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* The size of what a component returns. JSX lowers as JSX, and anything
|
|
1248
|
+
* else is inserted when it sits in an element.
|
|
1249
|
+
*/
|
|
1250
|
+
root(node, inTemplate) {
|
|
1251
|
+
const inner = unwrap(node);
|
|
1252
|
+
if (inner.type === "JSXElement" || inner.type === "JSXFragment") return this.jsx(inner, inTemplate);
|
|
1253
|
+
return (inTemplate ? 24 : 0) + this.node(inner);
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
//#endregion
|
|
1046
1257
|
//#region src/fold.ts
|
|
1047
1258
|
/**
|
|
1048
1259
|
* The control-flow components this pass resolves.
|
|
@@ -1051,7 +1262,7 @@ function innerText(s, node) {
|
|
|
1051
1262
|
const FOLDABLE_FLOW = /* @__PURE__ */ new Set([
|
|
1052
1263
|
"Show",
|
|
1053
1264
|
"For",
|
|
1054
|
-
"
|
|
1265
|
+
"Repeat",
|
|
1055
1266
|
"Switch",
|
|
1056
1267
|
"Dynamic"
|
|
1057
1268
|
]);
|
|
@@ -1112,9 +1323,51 @@ function containsHoistedDeclaration(statement) {
|
|
|
1112
1323
|
* Whether a statement starting with `text` could join the statement before
|
|
1113
1324
|
* it when that one has no semicolon.
|
|
1114
1325
|
*/
|
|
1115
|
-
function continuesPreviousStatement
|
|
1326
|
+
function continuesPreviousStatement(text) {
|
|
1116
1327
|
return /^[([`+\-/]/.test(text);
|
|
1117
1328
|
}
|
|
1329
|
+
/** The name a property key or a computed member's string literal gives. */
|
|
1330
|
+
function keyName(node) {
|
|
1331
|
+
if (node.type === "Identifier") return node.name;
|
|
1332
|
+
return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* The text of an expression that always gives the same primitive: a literal
|
|
1336
|
+
* other than a regular expression, a template literal without expressions,
|
|
1337
|
+
* or a negated number.
|
|
1338
|
+
*/
|
|
1339
|
+
function primitiveText(context, node) {
|
|
1340
|
+
let expression = node;
|
|
1341
|
+
while (expression.type === "ParenthesizedExpression") expression = expression.expression;
|
|
1342
|
+
switch (expression.type) {
|
|
1343
|
+
case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
|
|
1344
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
|
|
1345
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
|
|
1346
|
+
default: return;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
/** Whether a child of `parent` is inserted into its template, not passed as a prop. */
|
|
1350
|
+
function joinsTemplate(parent) {
|
|
1351
|
+
if (parent.type === "JSXFragment") return true;
|
|
1352
|
+
const { name } = parent.openingElement;
|
|
1353
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
1354
|
+
}
|
|
1355
|
+
/**
|
|
1356
|
+
* Whether JSX holds a `ref` or a spread, which run while the JSX is made.
|
|
1357
|
+
*/
|
|
1358
|
+
function hasRefOrSpread(root) {
|
|
1359
|
+
let found = false;
|
|
1360
|
+
const visit = (node) => {
|
|
1361
|
+
if (found) return;
|
|
1362
|
+
if (node.type === "JSXSpreadAttribute" || node.type === "JSXSpreadChild" || node.type === "JSXAttribute" && node.name.type === "JSXIdentifier" && node.name.name === "ref") {
|
|
1363
|
+
found = true;
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
forEachChild(node, visit);
|
|
1367
|
+
};
|
|
1368
|
+
visit(root);
|
|
1369
|
+
return found;
|
|
1370
|
+
}
|
|
1118
1371
|
var Folder = class {
|
|
1119
1372
|
constructor(context) {
|
|
1120
1373
|
this.context = context;
|
|
@@ -1273,6 +1526,26 @@ var Folder = class {
|
|
|
1273
1526
|
if (identity === void 0 || !this.context.options.builtIns.has(identity)) return;
|
|
1274
1527
|
return identity;
|
|
1275
1528
|
}
|
|
1529
|
+
/**
|
|
1530
|
+
* The name of the component an identifier refers to, when it is a
|
|
1531
|
+
* capitalized top-level binding nothing reassigns, holding a function or a
|
|
1532
|
+
* `lazy` component. A JSX tag with that name calls the same component. An
|
|
1533
|
+
* import could hold anything, such as a tag name string, so it is left out.
|
|
1534
|
+
*/
|
|
1535
|
+
componentName(identifier) {
|
|
1536
|
+
const binding = this.context.scopes.references.get(identifier);
|
|
1537
|
+
if (!binding || binding.mutated || binding.scope.parent !== void 0 || !/^[A-Z]/.test(binding.name)) return;
|
|
1538
|
+
const { declaration } = binding;
|
|
1539
|
+
if (declaration.type === "FunctionDeclaration") return declaration.async || declaration.generator ? void 0 : binding.name;
|
|
1540
|
+
if (declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
|
|
1541
|
+
let init = declaration.init;
|
|
1542
|
+
while (init.type === "ParenthesizedExpression") init = init.expression;
|
|
1543
|
+
if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") return init.async || init.generator ? void 0 : binding.name;
|
|
1544
|
+
if (init.type === "CallExpression" && init.callee.type === "Identifier") {
|
|
1545
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
1546
|
+
return callee && solidExport(callee, this.context.options) === "lazy" ? binding.name : void 0;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1276
1549
|
flowTag(element) {
|
|
1277
1550
|
const tag = this.builtInTag(element);
|
|
1278
1551
|
return tag !== void 0 && FOLDABLE_FLOW.has(tag) ? tag : void 0;
|
|
@@ -1282,8 +1555,8 @@ var Folder = class {
|
|
|
1282
1555
|
if (tag === void 0 || hasSpreadAttribute(element)) return;
|
|
1283
1556
|
switch (tag) {
|
|
1284
1557
|
case "Show": return this.foldShow(element);
|
|
1285
|
-
case "For":
|
|
1286
|
-
case "
|
|
1558
|
+
case "For": return this.foldFor(element);
|
|
1559
|
+
case "Repeat": return this.foldRepeat(element);
|
|
1287
1560
|
case "Switch": return this.foldSwitch(element);
|
|
1288
1561
|
case "Dynamic": return this.foldDynamic(element);
|
|
1289
1562
|
default: return;
|
|
@@ -1354,7 +1627,187 @@ var Folder = class {
|
|
|
1354
1627
|
const each = findAttribute(element, "each");
|
|
1355
1628
|
if (each?.value?.type !== "JSXExpressionContainer" || each.value.expression.type === "JSXEmptyExpression") return;
|
|
1356
1629
|
const { expression } = each.value;
|
|
1357
|
-
if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false)
|
|
1630
|
+
if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false)) return this.unrollFor(element, expression);
|
|
1631
|
+
if (!isSideEffectFree(expression)) return;
|
|
1632
|
+
return this.fallback(element);
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Unrolls `<For>` over a constant list into one copy of its row per item.
|
|
1636
|
+
*
|
|
1637
|
+
* The list is an array literal, or a binding of one that only `<For>`
|
|
1638
|
+
* reads, so nothing can change it. Each item is a primitive literal, or an
|
|
1639
|
+
* object literal of them. The row is an arrow function whose body is JSX,
|
|
1640
|
+
* and reads the item as `item.key`, or as itself when it is a primitive.
|
|
1641
|
+
* Each row ran once, untracked, under its own owner. A copy is made with
|
|
1642
|
+
* the JSX around it instead, which runs untracked too, and the JSX
|
|
1643
|
+
* transform gives every dynamic part its own effect. A `ref` or a spread
|
|
1644
|
+
* would run while the copy is made, so either one keeps the loop.
|
|
1645
|
+
*/
|
|
1646
|
+
unrollFor(element, expression) {
|
|
1647
|
+
if (element.openingElement.attributes.some((attribute) => attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "keyed" && !(attribute.value?.type === "JSXExpressionContainer" && attribute.value.expression.type === "Literal" && attribute.value.expression.value === false))) return;
|
|
1648
|
+
const byIndex = findAttribute(element, "keyed") !== void 0;
|
|
1649
|
+
const list = this.constantList(expression);
|
|
1650
|
+
if (!list) return;
|
|
1651
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
1652
|
+
const only = significant.at(0);
|
|
1653
|
+
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return;
|
|
1654
|
+
let row = only.expression;
|
|
1655
|
+
while (row.type === "ParenthesizedExpression") row = row.expression;
|
|
1656
|
+
if (row.type !== "ArrowFunctionExpression" || row.async || !row.expression || row.params.length > 2) return;
|
|
1657
|
+
let body = row.body;
|
|
1658
|
+
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1659
|
+
if (body.type !== "JSXElement" && body.type !== "JSXFragment" || hasRefOrSpread(body)) return;
|
|
1660
|
+
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
1661
|
+
const scope = this.context.scopes.scopes.get(row);
|
|
1662
|
+
if (!scope) return;
|
|
1663
|
+
const params = [];
|
|
1664
|
+
for (const param of row.params) {
|
|
1665
|
+
const binding = param.type === "Identifier" ? scope.bindings.get(param.name) : void 0;
|
|
1666
|
+
if (!binding || binding.mutated) return;
|
|
1667
|
+
params.push(binding);
|
|
1668
|
+
}
|
|
1669
|
+
const itemParam = params.at(0);
|
|
1670
|
+
const indexParam = params.at(1);
|
|
1671
|
+
const rows = [];
|
|
1672
|
+
for (const [index, item] of list.items.entries()) {
|
|
1673
|
+
const copy = new MagicString(this.context.code);
|
|
1674
|
+
if (itemParam && !this.substituteItem(copy, itemParam, item, byIndex) || indexParam && !this.substituteIndex(copy, indexParam, index, !byIndex)) return;
|
|
1675
|
+
rows.push(copy.slice(body.start, body.end));
|
|
1676
|
+
}
|
|
1677
|
+
const text = `<>${rows.join("")}</>`;
|
|
1678
|
+
if (!this.unrollingPays(element, text, list.declaration)) return;
|
|
1679
|
+
return {
|
|
1680
|
+
kind: "jsx",
|
|
1681
|
+
text
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* The items of a constant list, and the declaration that goes once no
|
|
1686
|
+
* `<For>` reads it.
|
|
1687
|
+
*/
|
|
1688
|
+
constantList(expression) {
|
|
1689
|
+
let node = expression;
|
|
1690
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1691
|
+
let declaration;
|
|
1692
|
+
if (node.type === "Identifier") {
|
|
1693
|
+
const binding = this.context.scopes.references.get(node);
|
|
1694
|
+
if (binding?.declaration.type !== "VariableDeclarator" || binding.declaration.id !== binding.identifier || !binding.declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
|
|
1695
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
1696
|
+
if ((statement ? this.context.parents.get(statement) : void 0)?.type === "ExportNamedDeclaration" || !binding.references.every((reference) => this.isForEach(reference))) return;
|
|
1697
|
+
node = binding.declaration.init;
|
|
1698
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1699
|
+
declaration = binding.references.length === 1 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 ? statement : void 0;
|
|
1700
|
+
}
|
|
1701
|
+
if (node.type !== "ArrayExpression") return;
|
|
1702
|
+
const items = [];
|
|
1703
|
+
for (const element of node.elements) {
|
|
1704
|
+
const item = element && element.type !== "SpreadElement" ? this.constantItem(element) : void 0;
|
|
1705
|
+
if (!item) return;
|
|
1706
|
+
items.push(item);
|
|
1707
|
+
}
|
|
1708
|
+
return {
|
|
1709
|
+
items,
|
|
1710
|
+
declaration
|
|
1711
|
+
};
|
|
1712
|
+
}
|
|
1713
|
+
/** Whether a reference is the whole `each` of a Solid `<For>`. */
|
|
1714
|
+
isForEach(reference) {
|
|
1715
|
+
const container = this.context.parents.get(reference);
|
|
1716
|
+
const attribute = container ? this.context.parents.get(container) : void 0;
|
|
1717
|
+
const opening = attribute ? this.context.parents.get(attribute) : void 0;
|
|
1718
|
+
const element = opening ? this.context.parents.get(opening) : void 0;
|
|
1719
|
+
return container?.type === "JSXExpressionContainer" && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "each" && element?.type === "JSXElement" && this.builtInTag(element) === "For";
|
|
1720
|
+
}
|
|
1721
|
+
/** A list item whose value is known: a primitive literal, or an object literal of them. */
|
|
1722
|
+
constantItem(node) {
|
|
1723
|
+
const primitive = primitiveText(this.context, node);
|
|
1724
|
+
if (primitive !== void 0) return {
|
|
1725
|
+
kind: "primitive",
|
|
1726
|
+
text: primitive
|
|
1727
|
+
};
|
|
1728
|
+
if (node.type !== "ObjectExpression") return;
|
|
1729
|
+
const keys = /* @__PURE__ */ new Map();
|
|
1730
|
+
for (const property of node.properties) {
|
|
1731
|
+
if (property.type !== "Property" || property.kind !== "init" || property.method || property.computed) return;
|
|
1732
|
+
const key = keyName(property.key);
|
|
1733
|
+
const value = primitiveText(this.context, property.value);
|
|
1734
|
+
if (key === void 0 || value === void 0 || key === "__proto__") return;
|
|
1735
|
+
keys.set(key, value);
|
|
1736
|
+
}
|
|
1737
|
+
return {
|
|
1738
|
+
kind: "object",
|
|
1739
|
+
keys
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
/**
|
|
1743
|
+
* Writes an item's value over each read of the row's item parameter.
|
|
1744
|
+
* `false` when a read is not one the value can take the place of.
|
|
1745
|
+
*/
|
|
1746
|
+
substituteItem(copy, param, item, accessor) {
|
|
1747
|
+
for (const reference of param.references) {
|
|
1748
|
+
let read = reference;
|
|
1749
|
+
if (accessor) {
|
|
1750
|
+
const call = this.context.parents.get(reference);
|
|
1751
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
|
|
1752
|
+
read = call;
|
|
1753
|
+
}
|
|
1754
|
+
if (item.kind === "primitive") {
|
|
1755
|
+
copy.overwrite(read.start, read.end, this.valueAt(read, item.text));
|
|
1756
|
+
continue;
|
|
1757
|
+
}
|
|
1758
|
+
const member = this.context.parents.get(read);
|
|
1759
|
+
if (member?.type !== "MemberExpression" || member.object !== read || isWriteTarget(member, this.context.parents)) return false;
|
|
1760
|
+
let key;
|
|
1761
|
+
if (!member.computed) key = member.property.type === "Identifier" ? member.property.name : void 0;
|
|
1762
|
+
else if (member.property.type === "Literal") key = keyName(member.property);
|
|
1763
|
+
const parent = this.context.parents.get(member);
|
|
1764
|
+
if (key === void 0 || parent?.type === "CallExpression" && parent.callee === member || !item.keys.has(key)) return false;
|
|
1765
|
+
copy.overwrite(member.start, member.end, this.valueAt(member, item.keys.get(key) ?? "void 0"));
|
|
1766
|
+
}
|
|
1767
|
+
return true;
|
|
1768
|
+
}
|
|
1769
|
+
/** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
|
|
1770
|
+
valueAt(node, text) {
|
|
1771
|
+
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : `(${text})`;
|
|
1772
|
+
}
|
|
1773
|
+
/** Writes an item's index over each read of the row's index parameter. */
|
|
1774
|
+
substituteIndex(copy, param, index, accessor) {
|
|
1775
|
+
for (const reference of param.references) {
|
|
1776
|
+
let read = reference;
|
|
1777
|
+
if (accessor) {
|
|
1778
|
+
const call = this.context.parents.get(reference);
|
|
1779
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) return false;
|
|
1780
|
+
read = call;
|
|
1781
|
+
}
|
|
1782
|
+
copy.overwrite(read.start, read.end, String(index));
|
|
1783
|
+
}
|
|
1784
|
+
return true;
|
|
1785
|
+
}
|
|
1786
|
+
/**
|
|
1787
|
+
* Whether the copies are estimated to be no larger, once lowered, than
|
|
1788
|
+
* the loop and the list it no longer needs.
|
|
1789
|
+
*/
|
|
1790
|
+
unrollingPays(element, text, declaration) {
|
|
1791
|
+
const inTemplate = this.joinsParentTemplate(element);
|
|
1792
|
+
const loop = new SizeEstimator(this.context.code).jsx(element, inTemplate) + (declaration ? compactLength(this.context.code.slice(declaration.start, declaration.end)) : 0);
|
|
1793
|
+
const code = `const __so_rows__ = ${text};`;
|
|
1794
|
+
const statement = parse(this.context.filename, code).body.at(0);
|
|
1795
|
+
const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
|
|
1796
|
+
if (init?.type !== "JSXFragment") return false;
|
|
1797
|
+
const estimator = new SizeEstimator(code);
|
|
1798
|
+
return (inTemplate ? init.children.reduce((size, child) => size + (child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child)), 0) : estimator.jsx(init, false)) <= loop;
|
|
1799
|
+
}
|
|
1800
|
+
/** Whether an element sits directly in an element or fragment whose template it joins. */
|
|
1801
|
+
joinsParentTemplate(element) {
|
|
1802
|
+
const parent = this.context.parents.get(element);
|
|
1803
|
+
return parent?.type === "JSXElement" && joinsTemplate(parent) || parent?.type === "JSXFragment";
|
|
1804
|
+
}
|
|
1805
|
+
foldRepeat(element) {
|
|
1806
|
+
const count = findAttribute(element, "count");
|
|
1807
|
+
if (count?.value?.type !== "JSXExpressionContainer" || count.value.expression.type === "JSXEmptyExpression") return;
|
|
1808
|
+
const value = evaluate(count.value.expression, this.lookup);
|
|
1809
|
+
if (typeof value?.value !== "number") return;
|
|
1810
|
+
if (value.value >= 1) return;
|
|
1358
1811
|
return this.fallback(element);
|
|
1359
1812
|
}
|
|
1360
1813
|
/**
|
|
@@ -1393,17 +1846,27 @@ var Folder = class {
|
|
|
1393
1846
|
* `<Dynamic component="div">` is just `<div>`, which the JSX transform can
|
|
1394
1847
|
* put in a template instead of creating the element at runtime.
|
|
1395
1848
|
*/
|
|
1849
|
+
/**
|
|
1850
|
+
* `<Dynamic component="div">` becomes `<div>`. `<Dynamic component={Card}>`
|
|
1851
|
+
* becomes `<Card>` when `Card` is a component declared at the top level
|
|
1852
|
+
* that nothing reassigns. `<Dynamic>` then only renders `Card` with the
|
|
1853
|
+
* other props, through a memo that never changes.
|
|
1854
|
+
*/
|
|
1396
1855
|
foldDynamic(element) {
|
|
1397
1856
|
const component = findAttribute(element, "component");
|
|
1398
1857
|
if (!component?.value) return;
|
|
1399
1858
|
let tag;
|
|
1400
1859
|
const { value } = component;
|
|
1401
|
-
if (value.type === "Literal") tag = value.value;
|
|
1860
|
+
if (value.type === "Literal") tag = isIntrinsicTag(value.value) ? value.value : void 0;
|
|
1402
1861
|
else if (value.type === "JSXExpressionContainer" && value.expression.type !== "JSXEmptyExpression") {
|
|
1403
|
-
const
|
|
1404
|
-
if (
|
|
1862
|
+
const expression = value.expression.type === "ParenthesizedExpression" ? value.expression.expression : value.expression;
|
|
1863
|
+
if (expression.type === "Identifier") tag = this.componentName(expression);
|
|
1864
|
+
if (tag === void 0) {
|
|
1865
|
+
const result = evaluate(value.expression, this.lookup);
|
|
1866
|
+
if (typeof result?.value === "string" && isIntrinsicTag(result.value)) tag = result.value;
|
|
1867
|
+
}
|
|
1405
1868
|
}
|
|
1406
|
-
if (tag === void 0
|
|
1869
|
+
if (tag === void 0) return;
|
|
1407
1870
|
const attributes = element.openingElement.attributes.filter((attribute) => attribute !== component).map((attribute) => textOf(this.context, attribute));
|
|
1408
1871
|
const opening = [tag, ...attributes].join(" ");
|
|
1409
1872
|
return {
|
|
@@ -1448,6 +1911,12 @@ var Folder = class {
|
|
|
1448
1911
|
pieces.push(splicePiece(this.context.s, child, child.children));
|
|
1449
1912
|
continue;
|
|
1450
1913
|
}
|
|
1914
|
+
const reduced = child.type === "JSXExpressionContainer" && joinsTemplate(parent) ? this.reducedCall(child.expression) : void 0;
|
|
1915
|
+
if (reduced !== void 0) {
|
|
1916
|
+
folded = true;
|
|
1917
|
+
pieces.push(replacedPiece(child, reduced));
|
|
1918
|
+
continue;
|
|
1919
|
+
}
|
|
1451
1920
|
const resolved = child.type === "JSXElement" ? this.foldFlow(child) : void 0;
|
|
1452
1921
|
if (!resolved) {
|
|
1453
1922
|
pieces.push(keptPiece(child));
|
|
@@ -1469,6 +1938,64 @@ var Folder = class {
|
|
|
1469
1938
|
}
|
|
1470
1939
|
if (folded && writePieces(this.context.s, pieces)) this.changed = true;
|
|
1471
1940
|
}
|
|
1941
|
+
/**
|
|
1942
|
+
* The JSX a call like `((n) => <li>{n}</li>)(1)` makes, with each
|
|
1943
|
+
* argument in place of its parameter, or `undefined` when that could
|
|
1944
|
+
* behave differently.
|
|
1945
|
+
*
|
|
1946
|
+
* The child is inserted by an effect that makes the JSX. The JSX
|
|
1947
|
+
* transform wraps every dynamic part of JSX in an effect of its own, so
|
|
1948
|
+
* making it reads nothing the outer effect tracks, and the outer effect
|
|
1949
|
+
* runs once. A `ref` runs while the JSX is made, and a spread reads its
|
|
1950
|
+
* object there, so either one keeps the call. Each argument has to be a
|
|
1951
|
+
* constant or a binding nothing reassigns, so reading it again where the
|
|
1952
|
+
* parameter was gives the same value.
|
|
1953
|
+
*/
|
|
1954
|
+
reducedCall(expression) {
|
|
1955
|
+
if (expression.type !== "CallExpression" || expression.optional) return;
|
|
1956
|
+
let callee = expression.callee;
|
|
1957
|
+
while (callee.type === "ParenthesizedExpression") callee = callee.expression;
|
|
1958
|
+
if (callee.type !== "ArrowFunctionExpression" || callee.async || !callee.expression) return;
|
|
1959
|
+
let body = callee.body;
|
|
1960
|
+
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1961
|
+
if (body.type !== "JSXElement" && body.type !== "JSXFragment") return;
|
|
1962
|
+
const { params } = callee;
|
|
1963
|
+
const args = expression.arguments;
|
|
1964
|
+
if (params.length !== args.length || hasRefOrSpread(body)) return;
|
|
1965
|
+
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
1966
|
+
const scope = this.context.scopes.scopes.get(callee);
|
|
1967
|
+
if (!scope) return;
|
|
1968
|
+
const copy = new MagicString(this.context.code);
|
|
1969
|
+
for (const [index, param] of params.entries()) {
|
|
1970
|
+
const arg = args.at(index);
|
|
1971
|
+
if (param.type !== "Identifier" || !arg || arg.type === "SpreadElement") return;
|
|
1972
|
+
const binding = scope.bindings.get(param.name);
|
|
1973
|
+
const text = this.argumentText(arg);
|
|
1974
|
+
if (!binding || binding.mutated || text === void 0) return;
|
|
1975
|
+
for (const reference of binding.references) {
|
|
1976
|
+
if (arg.type === "Identifier" && lookup(scopeAt(this.context.scopes, this.context.parents, reference), arg.name) !== this.context.scopes.references.get(arg)) return;
|
|
1977
|
+
const parent = this.context.parents.get(reference);
|
|
1978
|
+
const shorthand = parent?.type === "Property" && parent.shorthand && parent.value === reference;
|
|
1979
|
+
copy.overwrite(reference.start, reference.end, shorthand ? `${param.name}: ${text}` : text);
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
return copy.slice(body.start, body.end);
|
|
1983
|
+
}
|
|
1984
|
+
/** The text of an argument that reads the same wherever it is copied. */
|
|
1985
|
+
argumentText(arg) {
|
|
1986
|
+
let node = arg;
|
|
1987
|
+
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1988
|
+
switch (node.type) {
|
|
1989
|
+
case "Literal": return "regex" in node ? void 0 : textOf(this.context, node);
|
|
1990
|
+
case "TemplateLiteral": return node.expressions.length === 0 ? textOf(this.context, node) : void 0;
|
|
1991
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${textOf(this.context, node)})` : void 0;
|
|
1992
|
+
case "Identifier": {
|
|
1993
|
+
const binding = this.context.scopes.references.get(node);
|
|
1994
|
+
return binding && !binding.mutated ? node.name : void 0;
|
|
1995
|
+
}
|
|
1996
|
+
default: return;
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1472
1999
|
eliminateDeadStatements(statements) {
|
|
1473
2000
|
let terminator = -1;
|
|
1474
2001
|
for (const [index, statement] of statements.entries()) {
|
|
@@ -1494,7 +2021,7 @@ var Folder = class {
|
|
|
1494
2021
|
this.changed = true;
|
|
1495
2022
|
return;
|
|
1496
2023
|
}
|
|
1497
|
-
this.replace(statement, continuesPreviousStatement
|
|
2024
|
+
this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
|
|
1498
2025
|
}
|
|
1499
2026
|
/**
|
|
1500
2027
|
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
@@ -1550,29 +2077,35 @@ const CONTEXT_SAFE_PRIMITIVES = [
|
|
|
1550
2077
|
"createMemo",
|
|
1551
2078
|
"createEffect",
|
|
1552
2079
|
"createRenderEffect",
|
|
1553
|
-
"
|
|
2080
|
+
"createStore",
|
|
2081
|
+
"createProjection",
|
|
2082
|
+
"createOptimistic",
|
|
1554
2083
|
"onCleanup",
|
|
1555
|
-
"
|
|
2084
|
+
"onSettled",
|
|
1556
2085
|
"untrack",
|
|
1557
|
-
"batch",
|
|
1558
2086
|
"useContext",
|
|
2087
|
+
"flush",
|
|
1559
2088
|
"mapArray",
|
|
1560
|
-
"
|
|
1561
|
-
"
|
|
1562
|
-
"
|
|
2089
|
+
"repeat",
|
|
2090
|
+
"merge",
|
|
2091
|
+
"omit"
|
|
1563
2092
|
];
|
|
1564
2093
|
/** Primitives that return a getter and a setter, as `[get, set]`. */
|
|
1565
|
-
const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
|
|
2094
|
+
const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
|
|
2095
|
+
"createSignal",
|
|
2096
|
+
"createStore",
|
|
2097
|
+
"createOptimistic"
|
|
2098
|
+
]);
|
|
1566
2099
|
/**
|
|
1567
|
-
* The provider a JSX element creates:
|
|
1568
|
-
*
|
|
1569
|
-
*
|
|
2100
|
+
* The provider a JSX element creates: a tag that refers to a top-level
|
|
2101
|
+
* `createContext(...)` from Solid, nothing writes to, with one `value`
|
|
2102
|
+
* attribute holding a value that is never `undefined`.
|
|
1570
2103
|
*
|
|
1571
|
-
* `useContext`
|
|
1572
|
-
* value that could be `undefined` keeps its provider.
|
|
2104
|
+
* `useContext` throws when the value and the default are both `undefined`,
|
|
2105
|
+
* so a value that could be `undefined` keeps its provider.
|
|
1573
2106
|
*/
|
|
1574
2107
|
function providerOf(context, element) {
|
|
1575
|
-
const binding =
|
|
2108
|
+
const binding = contextBinding(context, element.openingElement.name);
|
|
1576
2109
|
if (!binding || element.openingElement.selfClosing) return;
|
|
1577
2110
|
const { attributes } = element.openingElement;
|
|
1578
2111
|
const attribute = attributes.at(0);
|
|
@@ -1589,14 +2122,7 @@ function providerOf(context, element) {
|
|
|
1589
2122
|
};
|
|
1590
2123
|
}
|
|
1591
2124
|
/**
|
|
1592
|
-
* The context a
|
|
1593
|
-
*/
|
|
1594
|
-
function providerContext(context, name) {
|
|
1595
|
-
if (name.type !== "JSXMemberExpression" || name.object.type !== "JSXIdentifier" || name.property.name !== "Provider") return;
|
|
1596
|
-
return contextBinding(context, name.object);
|
|
1597
|
-
}
|
|
1598
|
-
/**
|
|
1599
|
-
* The top-level context binding an identifier refers to.
|
|
2125
|
+
* The top-level context binding a tag or identifier refers to.
|
|
1600
2126
|
*/
|
|
1601
2127
|
function contextBinding(context, name) {
|
|
1602
2128
|
if (name.type !== "JSXIdentifier" && name.type !== "Identifier") return;
|
|
@@ -1785,8 +2311,8 @@ function runsOutsideCode(context, binding, root, options, depth) {
|
|
|
1785
2311
|
return found;
|
|
1786
2312
|
}
|
|
1787
2313
|
/**
|
|
1788
|
-
* Whether an expression is a `
|
|
1789
|
-
*
|
|
2314
|
+
* Whether an expression is a `merge()` or `omit()` view of a component's
|
|
2315
|
+
* own props, with only literal objects and keys besides.
|
|
1790
2316
|
*/
|
|
1791
2317
|
function isOwnView(context, node, props, depth) {
|
|
1792
2318
|
const expression = unwrap(node);
|
|
@@ -1795,33 +2321,23 @@ function isOwnView(context, node, props, depth) {
|
|
|
1795
2321
|
if (expression.type === "Identifier") {
|
|
1796
2322
|
const binding = context.scopes.references.get(expression);
|
|
1797
2323
|
if (binding === props) return true;
|
|
1798
|
-
|
|
2324
|
+
const init = binding ? constantInit(context, binding) : void 0;
|
|
2325
|
+
call = init ? unwrap(init) : void 0;
|
|
1799
2326
|
}
|
|
1800
2327
|
if (call?.type !== "CallExpression") return false;
|
|
1801
2328
|
const callee = solidCallee(context, call);
|
|
1802
|
-
if (callee !== "
|
|
2329
|
+
if (callee !== "merge" && callee !== "omit") return false;
|
|
1803
2330
|
return call.arguments.every((argument) => {
|
|
1804
2331
|
if (argument.type === "SpreadElement") return false;
|
|
1805
2332
|
const inner = unwrap(argument);
|
|
1806
|
-
return inner.type === "ObjectExpression" || inner.type === "
|
|
2333
|
+
return inner.type === "ObjectExpression" || inner.type === "Literal" && typeof inner.value === "string" || isOwnView(context, inner, props, depth + 1);
|
|
1807
2334
|
});
|
|
1808
2335
|
}
|
|
1809
|
-
/**
|
|
1810
|
-
* The call a view binding comes from: `const view = mergeProps(...)`, or
|
|
1811
|
-
* one of the names in `const [a, b] = splitProps(...)`.
|
|
1812
|
-
*/
|
|
1813
|
-
function viewInit(context, binding) {
|
|
1814
|
-
const init = constantInit(context, binding);
|
|
1815
|
-
if (init) return unwrap(init);
|
|
1816
|
-
const { declaration } = binding;
|
|
1817
|
-
if (binding.mutated || binding.kind !== "const" || declaration.type !== "VariableDeclarator" || declaration.id.type !== "ArrayPattern" || !declaration.id.elements.some((element) => element === binding.identifier) || !declaration.init) return;
|
|
1818
|
-
return unwrap(declaration.init);
|
|
1819
|
-
}
|
|
1820
2336
|
function isVisibleElement(context, element, options) {
|
|
1821
2337
|
const { name } = element.openingElement;
|
|
1822
|
-
if (providerContext(context, name)) return true;
|
|
1823
2338
|
if (name.type !== "JSXIdentifier") return false;
|
|
1824
2339
|
if (isIntrinsicTag(name.name)) return true;
|
|
2340
|
+
if (contextBinding(context, name)) return true;
|
|
1825
2341
|
const binding = context.scopes.references.get(name);
|
|
1826
2342
|
const identity = binding ? solidExport(binding, context.options) : name.name;
|
|
1827
2343
|
if (identity !== void 0 && identity !== "Dynamic" && context.options.builtIns.has(identity)) return true;
|
|
@@ -1920,7 +2436,7 @@ function contextReads(context, root, target) {
|
|
|
1920
2436
|
const reads = [];
|
|
1921
2437
|
const visit = (node) => {
|
|
1922
2438
|
if (node.type === "JSXAttribute" && isHandlerFunction(node)) return;
|
|
1923
|
-
if (node !== root && node.type === "JSXElement" &&
|
|
2439
|
+
if (node !== root && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === target) return;
|
|
1924
2440
|
if (node.type === "CallExpression" && solidCallee(context, node) === "useContext") {
|
|
1925
2441
|
const argument = node.arguments.at(0);
|
|
1926
2442
|
if (node.arguments.length === 1 && argument?.type !== "SpreadElement" && argument !== void 0 && contextBinding(context, unwrap(argument)) === target) reads.push(node);
|
|
@@ -1938,7 +2454,7 @@ function hasNestedProvider(context, provider) {
|
|
|
1938
2454
|
let nested = false;
|
|
1939
2455
|
const visit = (node) => {
|
|
1940
2456
|
if (nested) return;
|
|
1941
|
-
if (node !== provider.element && node.type === "JSXElement" &&
|
|
2457
|
+
if (node !== provider.element && node.type === "JSXElement" && contextBinding(context, node.openingElement.name) === provider.context) {
|
|
1942
2458
|
nested = true;
|
|
1943
2459
|
return;
|
|
1944
2460
|
}
|
|
@@ -1987,230 +2503,71 @@ var ProviderRemover = class {
|
|
|
1987
2503
|
reportShapes() {
|
|
1988
2504
|
const visit = (node) => {
|
|
1989
2505
|
if (node.type === "JSXElement") {
|
|
1990
|
-
const binding =
|
|
2506
|
+
const binding = contextBinding(this.context, node.openingElement.name);
|
|
1991
2507
|
if (binding && !providerOf(this.context, node)) {
|
|
1992
2508
|
const { attributes } = node.openingElement;
|
|
1993
2509
|
const attribute = attributes.at(0);
|
|
1994
2510
|
const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
|
|
1995
2511
|
recordSkip(this.context, "contexts", onlyValue && !node.openingElement.selfClosing ? "provider-undefined" : "provider-shape", binding.name);
|
|
1996
2512
|
}
|
|
1997
|
-
}
|
|
1998
|
-
forEachChild(node, visit);
|
|
1999
|
-
};
|
|
2000
|
-
visit(this.context.program);
|
|
2001
|
-
}
|
|
2002
|
-
tryRemove(provider) {
|
|
2003
|
-
const { element } = provider;
|
|
2004
|
-
const children = element.children;
|
|
2005
|
-
let reason = "open-other";
|
|
2006
|
-
if (!children.every((child) => isClosed(this.context, child, {
|
|
2007
|
-
component: () => false,
|
|
2008
|
-
contextValue: (target) => target === provider.context ? provider.value : void 0,
|
|
2009
|
-
onOpen: (open) => {
|
|
2010
|
-
reason = open;
|
|
2011
|
-
}
|
|
2012
|
-
}))) {
|
|
2013
|
-
this.skip(provider, reason);
|
|
2014
|
-
return false;
|
|
2015
|
-
}
|
|
2016
|
-
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
2017
|
-
const texts = [];
|
|
2018
|
-
for (const read of reads) {
|
|
2019
|
-
const text = stableValueText(this.context, provider, read);
|
|
2020
|
-
if (text === void 0) {
|
|
2021
|
-
this.skip(provider, "provider-value");
|
|
2022
|
-
return false;
|
|
2023
|
-
}
|
|
2024
|
-
texts.push(text);
|
|
2025
|
-
}
|
|
2026
|
-
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
2027
|
-
if (stableValueText(this.context, provider, element) === void 0) {
|
|
2028
|
-
this.skip(provider, "provider-value");
|
|
2029
|
-
return false;
|
|
2030
|
-
}
|
|
2031
|
-
}
|
|
2032
|
-
const { s } = this.context;
|
|
2033
|
-
for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
|
|
2034
|
-
const { openingElement, closingElement } = element;
|
|
2035
|
-
s.overwrite(openingElement.start, openingElement.end, "<>");
|
|
2036
|
-
if (closingElement) s.overwrite(closingElement.start, closingElement.end, "</>");
|
|
2037
|
-
this.changed = true;
|
|
2038
|
-
return true;
|
|
2039
|
-
}
|
|
2040
|
-
};
|
|
2041
|
-
/**
|
|
2042
|
-
* Runs the provider pass over a parsed program. Returns whether anything changed.
|
|
2043
|
-
*/
|
|
2044
|
-
function removeProviders(context) {
|
|
2045
|
-
const remover = new ProviderRemover(context);
|
|
2046
|
-
remover.run();
|
|
2047
|
-
return remover.changed;
|
|
2048
|
-
}
|
|
2049
|
-
//#endregion
|
|
2050
|
-
//#region src/size.ts
|
|
2051
|
-
/** A template's declaration, and the function that clones and returns it. */
|
|
2052
|
-
const TEMPLATE = 40;
|
|
2053
|
-
/** A walk to an element, like `e.firstChild.nextSibling`. */
|
|
2054
|
-
const WALK = 12;
|
|
2055
|
-
/** An `insert()` of a dynamic child. */
|
|
2056
|
-
const INSERT = 12;
|
|
2057
|
-
/** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
|
|
2058
|
-
const EFFECT = 24;
|
|
2059
|
-
/** An event handler assignment, like `e.$$click = ...`. */
|
|
2060
|
-
const HANDLER = 10;
|
|
2061
|
-
/** A `spread()` of props onto an element. */
|
|
2062
|
-
const SPREAD = 16;
|
|
2063
|
-
/** A `createComponent(Name, {})` call. */
|
|
2064
|
-
const COMPONENT = 8;
|
|
2065
|
-
/** A getter, `get name() { return ... }`. */
|
|
2066
|
-
const GETTER = 16;
|
|
2067
|
-
/** A `mergeProps()` around the props of a component with a spread. */
|
|
2068
|
-
const MERGE = 6;
|
|
2069
|
-
/**
|
|
2070
|
-
* The length of `text` once a minifier removes the whitespace it does not need.
|
|
2071
|
-
*/
|
|
2072
|
-
function compactLength(text) {
|
|
2073
|
-
return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
|
|
2074
|
-
}
|
|
2075
|
-
function nameLength(name) {
|
|
2076
|
-
switch (name.type) {
|
|
2077
|
-
case "JSXIdentifier": return name.name.length;
|
|
2078
|
-
case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
|
|
2079
|
-
case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
|
|
2080
|
-
default: return 4;
|
|
2081
|
-
}
|
|
2082
|
-
}
|
|
2083
|
-
function isIntrinsic(element) {
|
|
2084
|
-
const { name } = element.openingElement;
|
|
2085
|
-
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
2086
|
-
}
|
|
2087
|
-
function attributeName(attribute) {
|
|
2088
|
-
const { name } = attribute;
|
|
2089
|
-
return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
2090
|
-
}
|
|
2091
|
-
/**
|
|
2092
|
-
* Estimates lowered sizes in one piece of code.
|
|
2093
|
-
*/
|
|
2094
|
-
var SizeEstimator = class {
|
|
2095
|
-
constructor(code) {
|
|
2096
|
-
this.code = code;
|
|
2097
|
-
}
|
|
2098
|
-
text(node) {
|
|
2099
|
-
return compactLength(this.code.slice(node.start, node.end));
|
|
2100
|
-
}
|
|
2101
|
-
/**
|
|
2102
|
-
* The size of any node: JSX as lowered, other code as its compact text
|
|
2103
|
-
* with the JSX in it as lowered.
|
|
2104
|
-
*/
|
|
2105
|
-
node(node) {
|
|
2106
|
-
if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
|
|
2107
|
-
let size = this.text(node);
|
|
2108
|
-
const visit = (child) => {
|
|
2109
|
-
if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
2110
|
-
size += this.jsx(child, false) - this.text(child);
|
|
2111
|
-
return;
|
|
2112
|
-
}
|
|
2113
|
-
forEachChild(child, visit);
|
|
2114
|
-
};
|
|
2115
|
-
forEachChild(node, visit);
|
|
2116
|
-
return size;
|
|
2117
|
-
}
|
|
2118
|
-
/**
|
|
2119
|
-
* The size of a JSX element or fragment. `inTemplate` is whether it sits
|
|
2120
|
-
* directly in an element, whose template it joins.
|
|
2121
|
-
*/
|
|
2122
|
-
jsx(node, inTemplate) {
|
|
2123
|
-
if (node.type === "JSXFragment") return this.children(node.children) + 2;
|
|
2124
|
-
if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
|
|
2125
|
-
const tag = nameLength(node.openingElement.name);
|
|
2126
|
-
let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
|
|
2127
|
-
let dynamic = false;
|
|
2128
|
-
for (const attribute of node.openingElement.attributes) {
|
|
2129
|
-
const cost = this.elementAttribute(attribute);
|
|
2130
|
-
size += cost.size;
|
|
2131
|
-
dynamic ||= cost.dynamic;
|
|
2132
|
-
}
|
|
2133
|
-
for (const child of node.children) {
|
|
2134
|
-
if (isInsignificant(child)) continue;
|
|
2135
|
-
if (child.type === "JSXText") size += compactLength(child.value);
|
|
2136
|
-
else if (child.type === "JSXElement") size += this.jsx(child, true);
|
|
2137
|
-
else {
|
|
2138
|
-
size += INSERT + this.child(child);
|
|
2139
|
-
dynamic = true;
|
|
2140
|
-
}
|
|
2141
|
-
}
|
|
2142
|
-
return size + (dynamic ? WALK : 0);
|
|
2143
|
-
}
|
|
2144
|
-
elementAttribute(attribute) {
|
|
2145
|
-
if (attribute.type === "JSXSpreadAttribute") return {
|
|
2146
|
-
size: SPREAD + this.node(attribute.argument),
|
|
2147
|
-
dynamic: true
|
|
2148
|
-
};
|
|
2149
|
-
const name = attributeName(attribute);
|
|
2150
|
-
const { value } = attribute;
|
|
2151
|
-
if (!value || value.type === "Literal") return {
|
|
2152
|
-
size: name.length + (value ? this.text(value) : 0) + 1,
|
|
2153
|
-
dynamic: false
|
|
2154
|
-
};
|
|
2155
|
-
if (value.type !== "JSXExpressionContainer") return {
|
|
2156
|
-
size: name.length + 1 + this.jsx(value, false),
|
|
2157
|
-
dynamic: true
|
|
2158
|
-
};
|
|
2159
|
-
const { expression } = value;
|
|
2160
|
-
if (expression.type === "JSXEmptyExpression") return {
|
|
2161
|
-
size: 0,
|
|
2162
|
-
dynamic: false
|
|
2163
|
-
};
|
|
2164
|
-
const inner = unwrap(expression);
|
|
2165
|
-
if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
|
|
2166
|
-
size: name.length + this.text(inner) + 1,
|
|
2167
|
-
dynamic: false
|
|
2168
|
-
};
|
|
2169
|
-
return {
|
|
2170
|
-
size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
|
|
2171
|
-
dynamic: true
|
|
2172
|
-
};
|
|
2173
|
-
}
|
|
2174
|
-
child(child) {
|
|
2175
|
-
if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
|
|
2176
|
-
if (child.type === "JSXSpreadChild") return this.node(child.expression);
|
|
2177
|
-
if (child.type === "JSXText") return compactLength(child.value) + 2;
|
|
2178
|
-
return this.jsx(child, false);
|
|
2179
|
-
}
|
|
2180
|
-
/**
|
|
2181
|
-
* Children outside a template, which lower to an array of values.
|
|
2182
|
-
*/
|
|
2183
|
-
children(children) {
|
|
2184
|
-
let size = 0;
|
|
2185
|
-
for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
|
|
2186
|
-
return size;
|
|
2513
|
+
}
|
|
2514
|
+
forEachChild(node, visit);
|
|
2515
|
+
};
|
|
2516
|
+
visit(this.context.program);
|
|
2187
2517
|
}
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2518
|
+
tryRemove(provider) {
|
|
2519
|
+
const { element } = provider;
|
|
2520
|
+
const children = element.children;
|
|
2521
|
+
let reason = "open-other";
|
|
2522
|
+
if (!children.every((child) => isClosed(this.context, child, {
|
|
2523
|
+
component: () => false,
|
|
2524
|
+
contextValue: (target) => target === provider.context ? provider.value : void 0,
|
|
2525
|
+
onOpen: (open) => {
|
|
2526
|
+
reason = open;
|
|
2196
2527
|
}
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
else if (value.type === "JSXExpressionContainer") {
|
|
2201
|
-
const { expression } = value;
|
|
2202
|
-
if (expression.type !== "JSXEmptyExpression") {
|
|
2203
|
-
const inner = unwrap(expression);
|
|
2204
|
-
const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2205
|
-
size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
|
|
2206
|
-
}
|
|
2207
|
-
} else size += name.length + GETTER + this.jsx(value, false);
|
|
2528
|
+
}))) {
|
|
2529
|
+
this.skip(provider, reason);
|
|
2530
|
+
return false;
|
|
2208
2531
|
}
|
|
2209
|
-
const
|
|
2210
|
-
|
|
2211
|
-
|
|
2532
|
+
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
2533
|
+
const texts = [];
|
|
2534
|
+
for (const read of reads) {
|
|
2535
|
+
const text = stableValueText(this.context, provider, read);
|
|
2536
|
+
if (text === void 0) {
|
|
2537
|
+
this.skip(provider, "provider-value");
|
|
2538
|
+
return false;
|
|
2539
|
+
}
|
|
2540
|
+
texts.push(text);
|
|
2541
|
+
}
|
|
2542
|
+
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
2543
|
+
if (stableValueText(this.context, provider, element) === void 0) {
|
|
2544
|
+
this.skip(provider, "provider-value");
|
|
2545
|
+
return false;
|
|
2546
|
+
}
|
|
2547
|
+
}
|
|
2548
|
+
const { s } = this.context;
|
|
2549
|
+
for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
|
|
2550
|
+
const { openingElement, closingElement } = element;
|
|
2551
|
+
s.overwrite(openingElement.start, openingElement.end, "<>");
|
|
2552
|
+
if (closingElement) s.overwrite(closingElement.start, closingElement.end, "</>");
|
|
2553
|
+
this.changed = true;
|
|
2554
|
+
return true;
|
|
2212
2555
|
}
|
|
2213
2556
|
};
|
|
2557
|
+
/**
|
|
2558
|
+
* Runs the provider pass over a parsed program. Returns whether anything changed.
|
|
2559
|
+
*/
|
|
2560
|
+
function removeProviders(context) {
|
|
2561
|
+
const remover = new ProviderRemover(context);
|
|
2562
|
+
remover.run();
|
|
2563
|
+
return remover.changed;
|
|
2564
|
+
}
|
|
2565
|
+
//#endregion
|
|
2566
|
+
//#region src/copy-size.ts
|
|
2567
|
+
/**
|
|
2568
|
+
* Measures a component's copy at a call site: folds it as the passes after
|
|
2569
|
+
* inlining would, then estimates its size with `SizeEstimator`.
|
|
2570
|
+
*/
|
|
2214
2571
|
/** The name of the function a copy is wrapped in to be folded and measured. */
|
|
2215
2572
|
const WRAPPER = "__so_size__";
|
|
2216
2573
|
/**
|
|
@@ -2245,9 +2602,7 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2245
2602
|
size += estimator.node(statement);
|
|
2246
2603
|
continue;
|
|
2247
2604
|
}
|
|
2248
|
-
|
|
2249
|
-
if (argument?.type === "JSXElement" || argument?.type === "JSXFragment") size += estimator.jsx(argument, inTemplate);
|
|
2250
|
-
else if (argument) size += (inTemplate ? 24 : 0) + estimator.node(argument);
|
|
2605
|
+
if (statement.argument) size += estimator.root(statement.argument, inTemplate);
|
|
2251
2606
|
}
|
|
2252
2607
|
return size;
|
|
2253
2608
|
}
|
|
@@ -2270,8 +2625,9 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2270
2625
|
* function whose body is JSX.
|
|
2271
2626
|
* - It does not use `this`, `arguments`, `new.target`, or `super`.
|
|
2272
2627
|
*
|
|
2273
|
-
* A call site inlines when it passes plain attributes: no spread
|
|
2274
|
-
*
|
|
2628
|
+
* A call site inlines when it passes plain attributes: no spread and no
|
|
2629
|
+
* namespaced attribute. A `ref` moves to the element the component forwards
|
|
2630
|
+
* `props.ref` to. See `refValue`.
|
|
2275
2631
|
*
|
|
2276
2632
|
* # How props keep their meaning
|
|
2277
2633
|
*
|
|
@@ -2291,58 +2647,38 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2291
2647
|
*
|
|
2292
2648
|
* Statements, and stored props, have to run where the component would have
|
|
2293
2649
|
* been created. Solid runs a component untracked, so the statements move into
|
|
2294
|
-
* the function whose returned JSX holds the call
|
|
2295
|
-
*
|
|
2296
|
-
*
|
|
2297
|
-
*
|
|
2650
|
+
* the function whose returned JSX holds the call, when that function also
|
|
2651
|
+
* runs untracked:
|
|
2652
|
+
*
|
|
2653
|
+
* - a component;
|
|
2654
|
+
* - the child callback of `<For>` or `<Repeat>`, which runs once per item
|
|
2655
|
+
* under its own owner;
|
|
2656
|
+
* - the child callback of `<Show>` or `<Match>` that takes the value, which
|
|
2657
|
+
* runs once each time the condition turns true;
|
|
2658
|
+
* - the callback given to `render`, which runs once under its root.
|
|
2659
|
+
*
|
|
2660
|
+
* The call has to be in that returned JSX with only intrinsic elements and
|
|
2661
|
+
* fragments around it, so it is created exactly once each time that function
|
|
2662
|
+
* runs.
|
|
2298
2663
|
*
|
|
2299
2664
|
* A context provider is a component, so statements do not move out of it,
|
|
2300
2665
|
* and `useContext` still runs under it. The exception is a provider whose
|
|
2301
2666
|
* subtree only runs visible code. Statements move out through it, and each
|
|
2302
2667
|
* `useContext` of its context in a copy becomes its value. See `provider.ts`.
|
|
2303
2668
|
*
|
|
2304
|
-
* The props can also be read through `
|
|
2305
|
-
*
|
|
2306
|
-
*
|
|
2307
|
-
*
|
|
2308
|
-
* holds.
|
|
2669
|
+
* The props can also be read through `merge()` and `omit()` views declared
|
|
2670
|
+
* in the component. Each call site knows which props it passes, so a read
|
|
2671
|
+
* of a view resolves to a prop, a literal default, or `undefined`, and a
|
|
2672
|
+
* spread of a view becomes the attributes it holds.
|
|
2309
2673
|
*
|
|
2310
2674
|
* # Accepted differences
|
|
2311
2675
|
*
|
|
2312
2676
|
* - A prop that is a function and is called as `props.name()` runs with
|
|
2313
2677
|
* `this` set to its own receiver instead of the props object.
|
|
2314
2678
|
* - Statements moved into the host run before the host's JSX is created,
|
|
2315
|
-
* instead of while it is created. The order of `
|
|
2679
|
+
* instead of while it is created. The order of `onSettled` and effect
|
|
2316
2680
|
* registration across siblings can change.
|
|
2317
2681
|
*/
|
|
2318
|
-
/**
|
|
2319
|
-
* A prop read that falls back to a default when the prop is `undefined`,
|
|
2320
|
-
* the way `mergeProps()` reads it.
|
|
2321
|
-
*/
|
|
2322
|
-
function withFallback(text, fallback) {
|
|
2323
|
-
return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
|
|
2324
|
-
}
|
|
2325
|
-
function allows(source, key) {
|
|
2326
|
-
return !source.hidden.has(key) && (source.only === void 0 || source.only.has(key));
|
|
2327
|
-
}
|
|
2328
|
-
/**
|
|
2329
|
-
* Whether an expression passed as a prop is never `undefined`.
|
|
2330
|
-
*/
|
|
2331
|
-
function isDefinedExpression(node) {
|
|
2332
|
-
const inner = unwrap(node);
|
|
2333
|
-
switch (inner.type) {
|
|
2334
|
-
case "ObjectExpression":
|
|
2335
|
-
case "ArrayExpression":
|
|
2336
|
-
case "ArrowFunctionExpression":
|
|
2337
|
-
case "FunctionExpression":
|
|
2338
|
-
case "ClassExpression":
|
|
2339
|
-
case "NewExpression":
|
|
2340
|
-
case "TemplateLiteral":
|
|
2341
|
-
case "JSXElement":
|
|
2342
|
-
case "JSXFragment": return true;
|
|
2343
|
-
default: return evaluate(inner, () => void 0)?.value !== void 0;
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
2682
|
function isWithin(node, container) {
|
|
2347
2683
|
return node.start >= container.start && node.end <= container.end;
|
|
2348
2684
|
}
|
|
@@ -2489,12 +2825,55 @@ function functionOf(binding, options) {
|
|
|
2489
2825
|
function attributesOf(element) {
|
|
2490
2826
|
const attributes = /* @__PURE__ */ new Map();
|
|
2491
2827
|
for (const attribute of element.openingElement.attributes) {
|
|
2492
|
-
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" || attributes.has(attribute.name.name)) return;
|
|
2828
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(attribute) || attributes.has(attribute.name.name)) return;
|
|
2493
2829
|
attributes.set(attribute.name.name, attribute);
|
|
2494
2830
|
}
|
|
2495
2831
|
return attributes;
|
|
2496
2832
|
}
|
|
2497
2833
|
/**
|
|
2834
|
+
* Whether a top-level statement declares only constants made of literals,
|
|
2835
|
+
* like `const ROWS = [{ name: 'a' }]`. An export keeps its `export`, since
|
|
2836
|
+
* the fold pass treats an exported list differently.
|
|
2837
|
+
*/
|
|
2838
|
+
function isDataDeclaration(statement) {
|
|
2839
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
2840
|
+
return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && declaration.declarations.every((declarator) => declarator.id.type === "Identifier" && declarator.init !== null && isDataLiteral(declarator.init));
|
|
2841
|
+
}
|
|
2842
|
+
/** Whether an expression is a literal, or an array or object literal of them. */
|
|
2843
|
+
function isDataLiteral(node) {
|
|
2844
|
+
switch (node.type) {
|
|
2845
|
+
case "Literal": return !("regex" in node);
|
|
2846
|
+
case "TemplateLiteral": return node.expressions.length === 0;
|
|
2847
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal";
|
|
2848
|
+
case "ParenthesizedExpression":
|
|
2849
|
+
case "TSAsExpression":
|
|
2850
|
+
case "TSSatisfiesExpression": return isDataLiteral(node.expression);
|
|
2851
|
+
case "ArrayExpression": return node.elements.every((element) => element !== null && element.type !== "SpreadElement" && isDataLiteral(element));
|
|
2852
|
+
case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && property.kind === "init" && !property.method && !property.computed && isDataLiteral(property.value));
|
|
2853
|
+
default: return false;
|
|
2854
|
+
}
|
|
2855
|
+
}
|
|
2856
|
+
function isMemberRead(read) {
|
|
2857
|
+
return "member" in read;
|
|
2858
|
+
}
|
|
2859
|
+
/**
|
|
2860
|
+
* Whether a `ref` at a call site can move to the element the component
|
|
2861
|
+
* forwards it to: a name, a function, or an array of them. A name is
|
|
2862
|
+
* assigned the element, so it has to stay the name itself. Anything else
|
|
2863
|
+
* would be evaluated when the element is made, outside the `untrack` the
|
|
2864
|
+
* component's `ref` runs it in.
|
|
2865
|
+
*/
|
|
2866
|
+
function isForwardableRef(attribute) {
|
|
2867
|
+
const { value } = attribute;
|
|
2868
|
+
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
|
|
2869
|
+
const isTarget = (node) => {
|
|
2870
|
+
const inner = unwrap(node);
|
|
2871
|
+
if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element));
|
|
2872
|
+
return inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2873
|
+
};
|
|
2874
|
+
return isTarget(value.expression);
|
|
2875
|
+
}
|
|
2876
|
+
/**
|
|
2498
2877
|
* Why `attributesOf` turns down a call site, for the report.
|
|
2499
2878
|
*/
|
|
2500
2879
|
function callSiteReason(element) {
|
|
@@ -2714,9 +3093,9 @@ var Inliner = class {
|
|
|
2714
3093
|
return false;
|
|
2715
3094
|
}
|
|
2716
3095
|
/**
|
|
2717
|
-
* Finds the `
|
|
2718
|
-
*
|
|
2719
|
-
*
|
|
3096
|
+
* Finds the `merge()` and `omit()` views of the props declared in a
|
|
3097
|
+
* component's statements, and how each is used. A view can only be read
|
|
3098
|
+
* as `view.name`, spread onto an element, or passed to another view.
|
|
2720
3099
|
* `undefined` when a view is used any other way.
|
|
2721
3100
|
*/
|
|
2722
3101
|
analyzeViews(fn, scope, statements, param) {
|
|
@@ -2725,32 +3104,22 @@ var Inliner = class {
|
|
|
2725
3104
|
const declarations = [];
|
|
2726
3105
|
for (const statement of statements) {
|
|
2727
3106
|
const declarator = statement.type === "VariableDeclaration" ? statement.declarations.at(0) : void 0;
|
|
2728
|
-
if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || !declarator
|
|
3107
|
+
if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || declarator?.id.type !== "Identifier" || !declarator.init) continue;
|
|
2729
3108
|
const init = unwrap(declarator.init);
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
if (
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
else continue;
|
|
2738
|
-
if (declared === null) continue;
|
|
2739
|
-
if (!declared) return;
|
|
2740
|
-
for (const [identifier, view] of declared) {
|
|
2741
|
-
if (identifier?.type !== "Identifier") continue;
|
|
2742
|
-
const binding = scope.bindings.get(identifier.name);
|
|
2743
|
-
if (!binding || binding.mutated) return;
|
|
2744
|
-
views.set(binding, view);
|
|
2745
|
-
}
|
|
3109
|
+
const callee = init.type === "CallExpression" ? solidCallee(this.context, init) : void 0;
|
|
3110
|
+
if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
|
|
3111
|
+
const view = this.viewOf(callee, init, param, views, args);
|
|
3112
|
+
if (view === null) continue;
|
|
3113
|
+
const binding = scope.bindings.get(declarator.id.name);
|
|
3114
|
+
if (!view || !binding || binding.mutated) return;
|
|
3115
|
+
views.set(binding, view);
|
|
2746
3116
|
declarations.push(statement);
|
|
2747
3117
|
}
|
|
2748
3118
|
const reads = [];
|
|
2749
3119
|
const spreads = [];
|
|
2750
3120
|
const whole = { sources: [{
|
|
2751
3121
|
defaults: void 0,
|
|
2752
|
-
hidden: /* @__PURE__ */ new Set()
|
|
2753
|
-
only: void 0
|
|
3122
|
+
hidden: /* @__PURE__ */ new Set()
|
|
2754
3123
|
}] };
|
|
2755
3124
|
for (const reference of param?.references ?? []) {
|
|
2756
3125
|
const spread = this.spreadOnElement(reference);
|
|
@@ -2802,35 +3171,49 @@ var Inliner = class {
|
|
|
2802
3171
|
} : void 0;
|
|
2803
3172
|
}
|
|
2804
3173
|
/**
|
|
2805
|
-
* The
|
|
2806
|
-
* `
|
|
2807
|
-
|
|
2808
|
-
viewSources(argument, param, views, args) {
|
|
2809
|
-
if (argument.type === "CallExpression") {
|
|
2810
|
-
if (solidCallee(this.context, argument) !== "mergeProps") return;
|
|
2811
|
-
return this.viewOf(argument, param, views, args)?.sources;
|
|
2812
|
-
}
|
|
2813
|
-
if (argument.type !== "Identifier") return;
|
|
2814
|
-
const binding = this.context.scopes.references.get(argument);
|
|
2815
|
-
if (binding && binding === param) return [{
|
|
2816
|
-
defaults: void 0,
|
|
2817
|
-
hidden: /* @__PURE__ */ new Set(),
|
|
2818
|
-
only: void 0
|
|
2819
|
-
}];
|
|
2820
|
-
return binding ? views.get(binding)?.sources : void 0;
|
|
2821
|
-
}
|
|
2822
|
-
/**
|
|
2823
|
-
* The view a `mergeProps()` call makes. `null` when it does not take the
|
|
2824
|
-
* props or another view, and `undefined` when it does but its other
|
|
2825
|
-
* arguments are not literal objects.
|
|
3174
|
+
* The view a `merge()` or `omit()` call makes. `null` when it does not
|
|
3175
|
+
* take the props or another view, and `undefined` when it does but its
|
|
3176
|
+
* other arguments are not literal.
|
|
2826
3177
|
*/
|
|
2827
|
-
viewOf(call, param, views, args) {
|
|
2828
|
-
|
|
3178
|
+
viewOf(callee, call, param, views, args) {
|
|
3179
|
+
const sourcesOf = (argument) => {
|
|
3180
|
+
if (argument.type === "CallExpression") {
|
|
3181
|
+
const nested = solidCallee(this.context, argument);
|
|
3182
|
+
if (nested !== "merge" && nested !== "omit") return;
|
|
3183
|
+
return this.viewOf(nested, argument, param, views, args)?.sources;
|
|
3184
|
+
}
|
|
3185
|
+
if (argument.type !== "Identifier") return;
|
|
3186
|
+
const binding = this.context.scopes.references.get(argument);
|
|
3187
|
+
if (binding && binding === param) return [{
|
|
3188
|
+
defaults: void 0,
|
|
3189
|
+
hidden: /* @__PURE__ */ new Set()
|
|
3190
|
+
}];
|
|
3191
|
+
return binding ? views.get(binding)?.sources : void 0;
|
|
3192
|
+
};
|
|
3193
|
+
if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && sourcesOf(unwrap(argument)) !== void 0)) return null;
|
|
3194
|
+
if (callee === "omit") {
|
|
3195
|
+
const first = call.arguments.at(0);
|
|
3196
|
+
const keys = call.arguments.slice(1);
|
|
3197
|
+
if (first === void 0 || first.type === "SpreadElement") return;
|
|
3198
|
+
const target = unwrap(first);
|
|
3199
|
+
const base = sourcesOf(target);
|
|
3200
|
+
const hidden = [];
|
|
3201
|
+
for (const key of keys) {
|
|
3202
|
+
if (key.type !== "Literal" || typeof key.value !== "string") return;
|
|
3203
|
+
hidden.push(key.value);
|
|
3204
|
+
}
|
|
3205
|
+
if (!base) return;
|
|
3206
|
+
args.add(target);
|
|
3207
|
+
return { sources: base.map((source) => ({
|
|
3208
|
+
defaults: source.defaults,
|
|
3209
|
+
hidden: /* @__PURE__ */ new Set([...source.hidden, ...hidden])
|
|
3210
|
+
})) };
|
|
3211
|
+
}
|
|
2829
3212
|
const sources = [];
|
|
2830
3213
|
for (const argument of call.arguments) {
|
|
2831
3214
|
if (argument.type === "SpreadElement") return;
|
|
2832
3215
|
const inner = unwrap(argument);
|
|
2833
|
-
const existing =
|
|
3216
|
+
const existing = sourcesOf(inner);
|
|
2834
3217
|
if (existing) {
|
|
2835
3218
|
args.add(inner);
|
|
2836
3219
|
sources.push(...existing);
|
|
@@ -2851,52 +3234,12 @@ var Inliner = class {
|
|
|
2851
3234
|
}
|
|
2852
3235
|
sources.push({
|
|
2853
3236
|
defaults,
|
|
2854
|
-
hidden: /* @__PURE__ */ new Set()
|
|
2855
|
-
only: void 0
|
|
3237
|
+
hidden: /* @__PURE__ */ new Set()
|
|
2856
3238
|
});
|
|
2857
3239
|
}
|
|
2858
3240
|
return { sources };
|
|
2859
3241
|
}
|
|
2860
3242
|
/**
|
|
2861
|
-
* The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
|
|
2862
|
-
* each with its identifier. A key belongs to the first group that names
|
|
2863
|
-
* it, and the last view holds the keys no group names. `null` when the
|
|
2864
|
-
* call does not take the props or a view.
|
|
2865
|
-
*/
|
|
2866
|
-
splitViews(call, elements, param, views, args) {
|
|
2867
|
-
const first = call.arguments.at(0);
|
|
2868
|
-
if (first === void 0 || first.type === "SpreadElement") return null;
|
|
2869
|
-
const target = unwrap(first);
|
|
2870
|
-
const base = this.viewSources(target, param, views, args);
|
|
2871
|
-
if (!base) return null;
|
|
2872
|
-
args.add(target);
|
|
2873
|
-
const groups = [];
|
|
2874
|
-
for (const group of call.arguments.slice(1)) {
|
|
2875
|
-
if (group.type !== "ArrayExpression") return;
|
|
2876
|
-
const keys = [];
|
|
2877
|
-
for (const key of group.elements) {
|
|
2878
|
-
if (key?.type !== "Literal" || typeof key.value !== "string") return;
|
|
2879
|
-
keys.push(key.value);
|
|
2880
|
-
}
|
|
2881
|
-
groups.push(keys);
|
|
2882
|
-
}
|
|
2883
|
-
if (elements.length > groups.length + 1 || elements.some((element) => element?.type === "RestElement")) return;
|
|
2884
|
-
const claimed = /* @__PURE__ */ new Set();
|
|
2885
|
-
const filters = groups.map((keys) => {
|
|
2886
|
-
const owned = keys.filter((key) => !claimed.has(key));
|
|
2887
|
-
for (const key of owned) claimed.add(key);
|
|
2888
|
-
return new Set(owned);
|
|
2889
|
-
});
|
|
2890
|
-
return elements.map((element, index) => {
|
|
2891
|
-
const only = filters.at(index);
|
|
2892
|
-
return [element, { sources: base.map((source) => ({
|
|
2893
|
-
defaults: source.defaults,
|
|
2894
|
-
hidden: only ? source.hidden : /* @__PURE__ */ new Set([...source.hidden, ...claimed]),
|
|
2895
|
-
only: only ? new Set([...only].filter((key) => source.only === void 0 || source.only.has(key))) : source.only
|
|
2896
|
-
})) }];
|
|
2897
|
-
});
|
|
2898
|
-
}
|
|
2899
|
-
/**
|
|
2900
3243
|
* Whether a function is a component, which Solid runs untracked. Only a
|
|
2901
3244
|
* capitalized function bound at its declaration counts.
|
|
2902
3245
|
*/
|
|
@@ -2909,6 +3252,41 @@ var Inliner = class {
|
|
|
2909
3252
|
return parent?.type === "VariableDeclarator" && parent.id.type === "Identifier" && isCapitalized(parent.id.name);
|
|
2910
3253
|
}
|
|
2911
3254
|
/**
|
|
3255
|
+
* Whether a function runs untracked, once each time the JSX it returns is
|
|
3256
|
+
* created, so it can take a component's statements. See "Where a component
|
|
3257
|
+
* body can go" above.
|
|
3258
|
+
*/
|
|
3259
|
+
isHostFunction(fn) {
|
|
3260
|
+
if (this.isComponentFunction(fn)) return true;
|
|
3261
|
+
if (fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression" || fn.async || fn.generator) return false;
|
|
3262
|
+
let node = fn;
|
|
3263
|
+
let parent = this.context.parents.get(node);
|
|
3264
|
+
while (parent?.type === "ParenthesizedExpression") {
|
|
3265
|
+
node = parent;
|
|
3266
|
+
parent = this.context.parents.get(node);
|
|
3267
|
+
}
|
|
3268
|
+
if (parent?.type === "CallExpression") return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === "render";
|
|
3269
|
+
const element = parent?.type === "JSXExpressionContainer" ? this.context.parents.get(parent) : void 0;
|
|
3270
|
+
if (element?.type !== "JSXElement") return false;
|
|
3271
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
3272
|
+
if (significant.length !== 1 || significant.at(0) !== parent) return false;
|
|
3273
|
+
switch (this.solidCallee(element.openingElement.name)) {
|
|
3274
|
+
case "For":
|
|
3275
|
+
case "Repeat": return true;
|
|
3276
|
+
case "Show":
|
|
3277
|
+
case "Match": return fn.params.length > 0;
|
|
3278
|
+
default: return false;
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
/** The Solid export a callee or tag refers to. */
|
|
3282
|
+
solidCallee(name) {
|
|
3283
|
+
if (name.type !== "Identifier" && name.type !== "JSXIdentifier") return;
|
|
3284
|
+
const { references } = this.context.scopes;
|
|
3285
|
+
if (!references.has(name)) return;
|
|
3286
|
+
const binding = references.get(name);
|
|
3287
|
+
return binding ? solidExport(binding, this.context.options) : void 0;
|
|
3288
|
+
}
|
|
3289
|
+
/**
|
|
2912
3290
|
* Where the statements of a component created at `element` can go: the
|
|
2913
3291
|
* returned JSX of a component, with only intrinsic elements, fragments,
|
|
2914
3292
|
* and closed providers between it and the call.
|
|
@@ -2920,7 +3298,7 @@ var Inliner = class {
|
|
|
2920
3298
|
if (!parent) return;
|
|
2921
3299
|
if (parent.type === "JSXElement") {
|
|
2922
3300
|
const { name } = parent.openingElement;
|
|
2923
|
-
if (!(this.
|
|
3301
|
+
if (!(this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && (isIntrinsicTag(name.name) || this.transparent.has(parent)))) return;
|
|
2924
3302
|
const child = node;
|
|
2925
3303
|
if (!parent.children.some((item) => item === child)) return;
|
|
2926
3304
|
} else if (parent.type === "ReturnStatement") {
|
|
@@ -2928,7 +3306,7 @@ var Inliner = class {
|
|
|
2928
3306
|
if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
|
|
2929
3307
|
let fn = this.context.parents.get(list);
|
|
2930
3308
|
while (fn && !isFunctionNode(fn)) fn = this.context.parents.get(fn);
|
|
2931
|
-
if (!fn || !this.
|
|
3309
|
+
if (!fn || !this.isHostFunction(fn)) return;
|
|
2932
3310
|
return {
|
|
2933
3311
|
fn,
|
|
2934
3312
|
host: {
|
|
@@ -2937,7 +3315,7 @@ var Inliner = class {
|
|
|
2937
3315
|
}
|
|
2938
3316
|
};
|
|
2939
3317
|
} else if (parent.type === "ArrowFunctionExpression") {
|
|
2940
|
-
if (parent.body !== node || !this.
|
|
3318
|
+
if (parent.body !== node || !this.isHostFunction(parent)) return;
|
|
2941
3319
|
return {
|
|
2942
3320
|
fn: parent,
|
|
2943
3321
|
host: {
|
|
@@ -2978,6 +3356,29 @@ var Inliner = class {
|
|
|
2978
3356
|
};
|
|
2979
3357
|
}
|
|
2980
3358
|
/**
|
|
3359
|
+
* How a `ref` from the call site reads in a copy. Solid passes a `ref` to
|
|
3360
|
+
* a component as a function that sets the call site's ref to the element
|
|
3361
|
+
* it is given. The component applies it with its own `ref`, which runs
|
|
3362
|
+
* while the element is made, so the call site's `ref` can move to that
|
|
3363
|
+
* element. Every read has to be the whole `ref` of an element or of
|
|
3364
|
+
* another component, or a key of a spread, which becomes one.
|
|
3365
|
+
*/
|
|
3366
|
+
refValue(attribute, reads) {
|
|
3367
|
+
const value = attribute?.value;
|
|
3368
|
+
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return;
|
|
3369
|
+
for (const read of reads) {
|
|
3370
|
+
if (!isMemberRead(read)) continue;
|
|
3371
|
+
const container = this.context.parents.get(read.member);
|
|
3372
|
+
const owner = container ? this.context.parents.get(container) : void 0;
|
|
3373
|
+
if (container?.type !== "JSXExpressionContainer" || owner?.type !== "JSXAttribute" || owner.name.type !== "JSXIdentifier" || owner.name.name !== "ref") return;
|
|
3374
|
+
}
|
|
3375
|
+
return {
|
|
3376
|
+
kind: "expression",
|
|
3377
|
+
text: textOf(this.context, value.expression),
|
|
3378
|
+
primary: true
|
|
3379
|
+
};
|
|
3380
|
+
}
|
|
3381
|
+
/**
|
|
2981
3382
|
* How a prop reads in a copy of the component, and the `const` that stores
|
|
2982
3383
|
* it first when the value has to be evaluated once.
|
|
2983
3384
|
*/
|
|
@@ -3058,16 +3459,17 @@ var Inliner = class {
|
|
|
3058
3459
|
default: return false;
|
|
3059
3460
|
}
|
|
3060
3461
|
}
|
|
3061
|
-
freshName(
|
|
3462
|
+
freshName(name) {
|
|
3062
3463
|
const names = this.scratchNames ?? this.context.scopes.names;
|
|
3464
|
+
const base = name.replace(/\$\d+$/, "");
|
|
3063
3465
|
let index = 1;
|
|
3064
|
-
let
|
|
3065
|
-
while (names.has(
|
|
3466
|
+
let fresh = `${base}$${String(index)}`;
|
|
3467
|
+
while (names.has(fresh)) {
|
|
3066
3468
|
index += 1;
|
|
3067
|
-
|
|
3469
|
+
fresh = `${base}$${String(index)}`;
|
|
3068
3470
|
}
|
|
3069
|
-
names.add(
|
|
3070
|
-
return
|
|
3471
|
+
names.add(fresh);
|
|
3472
|
+
return fresh;
|
|
3071
3473
|
}
|
|
3072
3474
|
/**
|
|
3073
3475
|
* The copy of `component` that would replace `element`, or `undefined`
|
|
@@ -3092,7 +3494,7 @@ var Inliner = class {
|
|
|
3092
3494
|
this.reason = "call-this";
|
|
3093
3495
|
return;
|
|
3094
3496
|
}
|
|
3095
|
-
const views = this.resolveViews(component,
|
|
3497
|
+
const views = this.resolveViews(component, attributes, hasChildren);
|
|
3096
3498
|
if (!views) {
|
|
3097
3499
|
this.reason = "view-conflict";
|
|
3098
3500
|
return;
|
|
@@ -3106,15 +3508,15 @@ var Inliner = class {
|
|
|
3106
3508
|
readsByName.set(name, reads);
|
|
3107
3509
|
};
|
|
3108
3510
|
for (const read of views.propReads) count(read.name, read);
|
|
3109
|
-
for (const
|
|
3110
|
-
for (const spread of views.spreads) for (const [key, resolution] of spread.keys) if (resolution.kind !== "text") count(key, spread.spread);
|
|
3511
|
+
for (const spread of views.spreads) for (const [key, source] of spread.keys) if (source === void 0) count(key, spread.spread);
|
|
3111
3512
|
const values = /* @__PURE__ */ new Map();
|
|
3112
3513
|
for (const [name, reads] of readsByName) {
|
|
3113
3514
|
let value;
|
|
3114
3515
|
if (name === "children" && !attributes.has("children")) value = this.childrenValue(element, reads, fresh, snapshots);
|
|
3516
|
+
else if (name === "ref" && attributes.has("ref")) value = this.refValue(attributes.get("ref"), reads);
|
|
3115
3517
|
else value = this.propValue(attributes.get(name), reads, fresh, snapshots);
|
|
3116
3518
|
if (!value) {
|
|
3117
|
-
this.reason = "prop-value";
|
|
3519
|
+
this.reason = name === "ref" && attributes.has("ref") ? "ref-use" : "prop-value";
|
|
3118
3520
|
return;
|
|
3119
3521
|
}
|
|
3120
3522
|
values.set(name, value);
|
|
@@ -3187,7 +3589,7 @@ var Inliner = class {
|
|
|
3187
3589
|
const answering = [];
|
|
3188
3590
|
const seen = /* @__PURE__ */ new Set();
|
|
3189
3591
|
for (let node = this.context.parents.get(element); node; node = this.context.parents.get(node)) {
|
|
3190
|
-
const target = node.type === "JSXElement" ?
|
|
3592
|
+
const target = node.type === "JSXElement" ? contextBinding(this.context, node.openingElement.name) : void 0;
|
|
3191
3593
|
if (node.type !== "JSXElement" || !target || seen.has(target)) continue;
|
|
3192
3594
|
seen.add(target);
|
|
3193
3595
|
const entry = this.transparent.get(node);
|
|
@@ -3254,68 +3656,44 @@ var Inliner = class {
|
|
|
3254
3656
|
return changed;
|
|
3255
3657
|
}
|
|
3256
3658
|
/**
|
|
3257
|
-
* Resolves each view read and spread of a component at a call site.
|
|
3258
|
-
*
|
|
3259
|
-
*
|
|
3260
|
-
*
|
|
3261
|
-
*
|
|
3262
|
-
* holds `children` for an element that has some.
|
|
3659
|
+
* Resolves each view read and spread of a component at a call site. A
|
|
3660
|
+
* read becomes a read of the props, or the text of a default. A spread
|
|
3661
|
+
* becomes the keys it holds, in the order `merge()` gives them, and its
|
|
3662
|
+
* `children` become the element's children. `undefined` when a spread
|
|
3663
|
+
* repeats an attribute, or holds `children` for an element that has some.
|
|
3263
3664
|
*/
|
|
3264
|
-
resolveViews(component,
|
|
3665
|
+
resolveViews(component, attributes, hasChildren) {
|
|
3265
3666
|
const has = (key) => attributes.has(key) || key === "children" && hasChildren;
|
|
3266
|
-
const defined = (key) => {
|
|
3267
|
-
const attribute = attributes.get(key);
|
|
3268
|
-
if (attribute) {
|
|
3269
|
-
const { value } = attribute;
|
|
3270
|
-
if (value?.type !== "JSXExpressionContainer") return true;
|
|
3271
|
-
return value.expression.type !== "JSXEmptyExpression" && isDefinedExpression(value.expression);
|
|
3272
|
-
}
|
|
3273
|
-
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
3274
|
-
const only = significant.at(0);
|
|
3275
|
-
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return true;
|
|
3276
|
-
return only.expression.type !== "JSXEmptyExpression" && isDefinedExpression(only.expression);
|
|
3277
|
-
};
|
|
3278
3667
|
const resolve = (view, key) => {
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
const text = source
|
|
3283
|
-
if (text !== void 0) return
|
|
3284
|
-
|
|
3285
|
-
text
|
|
3286
|
-
};
|
|
3287
|
-
if (source && !source.defaults && has(key)) {
|
|
3288
|
-
if (defined(key)) return { kind: "props" };
|
|
3289
|
-
const fallback = sources.slice(0, index).findLast((earlier) => earlier.defaults?.has(key))?.defaults?.get(key);
|
|
3290
|
-
return fallback === void 0 ? { kind: "props" } : {
|
|
3291
|
-
kind: "fallback",
|
|
3292
|
-
text: fallback
|
|
3293
|
-
};
|
|
3294
|
-
}
|
|
3668
|
+
for (let index = view.sources.length - 1; index >= 0; index -= 1) {
|
|
3669
|
+
const source = view.sources.at(index);
|
|
3670
|
+
if (!source || source.hidden.has(key)) continue;
|
|
3671
|
+
const text = source.defaults?.get(key);
|
|
3672
|
+
if (text !== void 0) return text;
|
|
3673
|
+
if (!source.defaults && has(key)) return;
|
|
3295
3674
|
}
|
|
3296
|
-
return
|
|
3297
|
-
kind: "text",
|
|
3298
|
-
text: "void 0"
|
|
3299
|
-
};
|
|
3675
|
+
return "void 0";
|
|
3300
3676
|
};
|
|
3301
3677
|
const propReads = [...component.props];
|
|
3302
|
-
const fallbacks = /* @__PURE__ */ new Map();
|
|
3303
3678
|
const texts = /* @__PURE__ */ new Map();
|
|
3304
3679
|
for (const read of component.viewReads) {
|
|
3305
|
-
const
|
|
3306
|
-
if (
|
|
3307
|
-
else
|
|
3308
|
-
else texts.set(read.member, resolution.text);
|
|
3680
|
+
const text = resolve(read.view, read.name);
|
|
3681
|
+
if (text === void 0) propReads.push(read);
|
|
3682
|
+
else texts.set(read.member, text);
|
|
3309
3683
|
}
|
|
3310
3684
|
const callKeys = [...attributes.keys()];
|
|
3311
3685
|
if (hasChildren && !attributes.has("children")) callKeys.push("children");
|
|
3312
3686
|
const spreads = [];
|
|
3313
3687
|
const taken = /* @__PURE__ */ new Map();
|
|
3314
3688
|
for (const spread of component.viewSpreads) {
|
|
3315
|
-
const order =
|
|
3689
|
+
const order = [];
|
|
3316
3690
|
for (const source of spread.view.sources) {
|
|
3317
3691
|
const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
|
|
3318
|
-
for (const key of keys.filter((name) =>
|
|
3692
|
+
for (const key of keys.filter((name) => !source.hidden.has(name))) {
|
|
3693
|
+
const index = order.indexOf(key);
|
|
3694
|
+
if (index !== -1) order.splice(index, 1);
|
|
3695
|
+
order.push(key);
|
|
3696
|
+
}
|
|
3319
3697
|
}
|
|
3320
3698
|
let names = taken.get(spread.element);
|
|
3321
3699
|
if (!names) {
|
|
@@ -3325,10 +3703,9 @@ var Inliner = class {
|
|
|
3325
3703
|
}
|
|
3326
3704
|
const keys = /* @__PURE__ */ new Map();
|
|
3327
3705
|
for (const key of order) {
|
|
3328
|
-
|
|
3329
|
-
if (!(key === "children" ? spread.element.openingElement.selfClosing && resolution.kind !== "fallback" : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
|
|
3706
|
+
if (!(key === "children" ? spread.element.openingElement.selfClosing : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
|
|
3330
3707
|
names.add(key);
|
|
3331
|
-
keys.set(key,
|
|
3708
|
+
keys.set(key, resolve(spread.view, key));
|
|
3332
3709
|
}
|
|
3333
3710
|
spreads.push({
|
|
3334
3711
|
spread,
|
|
@@ -3337,7 +3714,6 @@ var Inliner = class {
|
|
|
3337
3714
|
}
|
|
3338
3715
|
return {
|
|
3339
3716
|
propReads,
|
|
3340
|
-
fallbacks,
|
|
3341
3717
|
texts,
|
|
3342
3718
|
spreads
|
|
3343
3719
|
};
|
|
@@ -3351,23 +3727,13 @@ var Inliner = class {
|
|
|
3351
3727
|
const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
3352
3728
|
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
3353
3729
|
}
|
|
3354
|
-
for (const [read, fallback] of views.fallbacks) {
|
|
3355
|
-
const text = values.get(read.name)?.text ?? "void 0";
|
|
3356
|
-
copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
|
|
3357
|
-
}
|
|
3358
3730
|
for (const { spread, keys } of views.spreads) {
|
|
3359
3731
|
const parts = [];
|
|
3360
3732
|
let children;
|
|
3361
|
-
for (const [key,
|
|
3733
|
+
for (const [key, text] of keys) {
|
|
3362
3734
|
const value = values.get(key);
|
|
3363
|
-
if (key === "children")
|
|
3364
|
-
|
|
3365
|
-
continue;
|
|
3366
|
-
}
|
|
3367
|
-
let text = value?.text ?? "void 0";
|
|
3368
|
-
if (resolution.kind === "text") text = resolution.text;
|
|
3369
|
-
else if (resolution.kind === "fallback") text = withFallback(text, resolution.text);
|
|
3370
|
-
parts.push(`${key}={${text}}`);
|
|
3735
|
+
if (key === "children") children = this.spreadChildren(element, text, value);
|
|
3736
|
+
else parts.push(`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
3371
3737
|
}
|
|
3372
3738
|
const { attribute } = spread;
|
|
3373
3739
|
if (parts.length === 0) copy.remove(attribute.start, attribute.end);
|
|
@@ -3385,8 +3751,8 @@ var Inliner = class {
|
|
|
3385
3751
|
* The JSX children a spread's `children` becomes: the call site's own
|
|
3386
3752
|
* children, or the value of a single expression or default.
|
|
3387
3753
|
*/
|
|
3388
|
-
spreadChildren(element,
|
|
3389
|
-
if (
|
|
3754
|
+
spreadChildren(element, text, value) {
|
|
3755
|
+
if (text !== void 0) return text === "void 0" ? void 0 : `{${text}}`;
|
|
3390
3756
|
if (!value) return;
|
|
3391
3757
|
if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
3392
3758
|
return `{${value.text}}`;
|
|
@@ -3547,7 +3913,7 @@ var Inliner = class {
|
|
|
3547
3913
|
*/
|
|
3548
3914
|
sizeHeader() {
|
|
3549
3915
|
if (this.header === void 0) {
|
|
3550
|
-
const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration").map((statement) => this.context.code.slice(statement.start, statement.end));
|
|
3916
|
+
const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration" || isDataDeclaration(statement)).map((statement) => this.context.code.slice(statement.start, statement.end));
|
|
3551
3917
|
const aliases = [...this.context.options.builtInAliases.keys()];
|
|
3552
3918
|
if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
|
|
3553
3919
|
this.header = imports.join("\n");
|
|
@@ -3646,7 +4012,6 @@ var Inliner = class {
|
|
|
3646
4012
|
}
|
|
3647
4013
|
callReason(element, rejected, siteReasons) {
|
|
3648
4014
|
const tag = element.openingElement.name;
|
|
3649
|
-
if (providerContext(this.context, tag)) return;
|
|
3650
4015
|
if (tag.type === "JSXMemberExpression") return {
|
|
3651
4016
|
reason: "member-tag",
|
|
3652
4017
|
name: textOf(this.context, tag)
|
|
@@ -3655,7 +4020,7 @@ var Inliner = class {
|
|
|
3655
4020
|
const { name } = tag;
|
|
3656
4021
|
const binding = this.context.scopes.references.get(tag);
|
|
3657
4022
|
const identity = binding ? solidExport(binding, this.context.options) : name;
|
|
3658
|
-
if (identity !== void 0 && this.context.options.builtIns.has(identity)) return;
|
|
4023
|
+
if (identity !== void 0 && this.context.options.builtIns.has(identity) || contextBinding(this.context, tag)) return;
|
|
3659
4024
|
if (!binding) return {
|
|
3660
4025
|
reason: "global",
|
|
3661
4026
|
name
|
|
@@ -3709,7 +4074,7 @@ const TRACKED_PROPS = /* @__PURE__ */ new Map([
|
|
|
3709
4074
|
["Show", "when"],
|
|
3710
4075
|
["Match", "when"],
|
|
3711
4076
|
["For", "each"],
|
|
3712
|
-
["
|
|
4077
|
+
["Repeat", "count"]
|
|
3713
4078
|
]);
|
|
3714
4079
|
/**
|
|
3715
4080
|
* Whether evaluating `node` always creates a new value, which reference
|
|
@@ -3735,8 +4100,19 @@ function isFresh(node) {
|
|
|
3735
4100
|
}
|
|
3736
4101
|
}
|
|
3737
4102
|
/**
|
|
3738
|
-
* The
|
|
4103
|
+
* The text of an expression that always gives the same primitive: a literal
|
|
4104
|
+
* other than a regular expression, a template literal without expressions,
|
|
4105
|
+
* or a negated number.
|
|
3739
4106
|
*/
|
|
4107
|
+
function constantText(context, node) {
|
|
4108
|
+
const expression = unwrap(node);
|
|
4109
|
+
switch (expression.type) {
|
|
4110
|
+
case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
|
|
4111
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
|
|
4112
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
|
|
4113
|
+
default: return;
|
|
4114
|
+
}
|
|
4115
|
+
}
|
|
3740
4116
|
function returnedExpression(fn) {
|
|
3741
4117
|
if (fn.async || fn.generator || fn.params.length > 0 || !fn.body) return;
|
|
3742
4118
|
if (fn.body.type !== "BlockStatement") return fn.body;
|
|
@@ -3751,22 +4127,29 @@ var MemoInliner = class {
|
|
|
3751
4127
|
this.changed = false;
|
|
3752
4128
|
this.reason = "";
|
|
3753
4129
|
}
|
|
3754
|
-
|
|
3755
|
-
|
|
4130
|
+
/** Whether a callee is Solid's `createMemo` or `children`. */
|
|
4131
|
+
memoKind(callee) {
|
|
4132
|
+
if (callee.type !== "Identifier") return;
|
|
3756
4133
|
const binding = this.context.scopes.references.get(callee);
|
|
3757
|
-
|
|
4134
|
+
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
4135
|
+
return identity === "createMemo" || identity === "children" ? identity : void 0;
|
|
3758
4136
|
}
|
|
3759
4137
|
/**
|
|
3760
|
-
* The
|
|
4138
|
+
* The expression a memo's computation returns, and whether `equals: false`
|
|
4139
|
+
* makes it notify on every run.
|
|
3761
4140
|
*/
|
|
3762
|
-
|
|
4141
|
+
computation(declarator) {
|
|
3763
4142
|
const init = declarator.init ? unwrap(declarator.init) : void 0;
|
|
3764
|
-
|
|
4143
|
+
const kind = init?.type === "CallExpression" ? this.memoKind(init.callee) : void 0;
|
|
4144
|
+
if (init?.type !== "CallExpression" || kind === void 0) return;
|
|
3765
4145
|
const args = init.arguments;
|
|
4146
|
+
if (kind === "children" && args.length > 1) {
|
|
4147
|
+
this.reason = "memo-options";
|
|
4148
|
+
return;
|
|
4149
|
+
}
|
|
3766
4150
|
const compute = args.at(0);
|
|
3767
|
-
const
|
|
3768
|
-
|
|
3769
|
-
if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) {
|
|
4151
|
+
const options = args.at(1);
|
|
4152
|
+
if (args.length > 2 || compute === void 0 || compute.type === "SpreadElement") {
|
|
3770
4153
|
this.reason = "memo-computation";
|
|
3771
4154
|
return;
|
|
3772
4155
|
}
|
|
@@ -3793,8 +4176,40 @@ var MemoInliner = class {
|
|
|
3793
4176
|
}
|
|
3794
4177
|
alwaysNotifies = true;
|
|
3795
4178
|
}
|
|
3796
|
-
|
|
3797
|
-
|
|
4179
|
+
return {
|
|
4180
|
+
expression,
|
|
4181
|
+
alwaysNotifies,
|
|
4182
|
+
kind
|
|
4183
|
+
};
|
|
4184
|
+
}
|
|
4185
|
+
/**
|
|
4186
|
+
* Replaces every read of a memo whose computation is a literal with the
|
|
4187
|
+
* literal. Each reference has to be a call after the declaration, since a
|
|
4188
|
+
* call before it would throw.
|
|
4189
|
+
*/
|
|
4190
|
+
inlineConstant(declarator, binding, text) {
|
|
4191
|
+
const statement = this.context.parents.get(declarator);
|
|
4192
|
+
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
|
|
4193
|
+
this.reason = "memo-declaration";
|
|
4194
|
+
return;
|
|
4195
|
+
}
|
|
4196
|
+
const calls = [];
|
|
4197
|
+
for (const reference of binding.references) {
|
|
4198
|
+
const call = this.context.parents.get(reference);
|
|
4199
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
|
|
4200
|
+
this.reason = "memo-constant";
|
|
4201
|
+
return;
|
|
4202
|
+
}
|
|
4203
|
+
calls.push(call);
|
|
4204
|
+
}
|
|
4205
|
+
for (const call of calls) {
|
|
4206
|
+
const parent = this.context.parents.get(call);
|
|
4207
|
+
const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
|
|
4208
|
+
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
4209
|
+
}
|
|
4210
|
+
this.context.s.remove(statement.start, statement.end);
|
|
4211
|
+
this.changed = true;
|
|
4212
|
+
return true;
|
|
3798
4213
|
}
|
|
3799
4214
|
/**
|
|
3800
4215
|
* The function a node runs in.
|
|
@@ -3804,6 +4219,12 @@ var MemoInliner = class {
|
|
|
3804
4219
|
while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
|
|
3805
4220
|
return current;
|
|
3806
4221
|
}
|
|
4222
|
+
/** The JSX expression container a call is the whole of. */
|
|
4223
|
+
containerOf(call) {
|
|
4224
|
+
let parent = this.context.parents.get(call);
|
|
4225
|
+
while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
|
|
4226
|
+
return parent?.type === "JSXExpressionContainer" ? parent : void 0;
|
|
4227
|
+
}
|
|
3807
4228
|
/**
|
|
3808
4229
|
* Whether a call is the whole of an expression that Solid tracks, and that
|
|
3809
4230
|
* runs once each time `fn` runs.
|
|
@@ -3893,8 +4314,20 @@ var MemoInliner = class {
|
|
|
3893
4314
|
if (this.inline(declarator, binding) === void 0 && this.reason !== "") recordSkip(this.context, "memos", this.reason, binding.name);
|
|
3894
4315
|
}
|
|
3895
4316
|
inline(declarator, binding) {
|
|
3896
|
-
const
|
|
3897
|
-
if (!
|
|
4317
|
+
const found = this.computation(declarator);
|
|
4318
|
+
if (!found) return;
|
|
4319
|
+
const { expression, alwaysNotifies, kind } = found;
|
|
4320
|
+
if (kind === "children") {
|
|
4321
|
+
const root = unwrap(expression);
|
|
4322
|
+
if (root.type !== "JSXElement" && root.type !== "JSXFragment") {
|
|
4323
|
+
this.reason = "children-dynamic";
|
|
4324
|
+
return;
|
|
4325
|
+
}
|
|
4326
|
+
} else {
|
|
4327
|
+
const constant = constantText(this.context, expression);
|
|
4328
|
+
if (constant !== void 0) return this.inlineConstant(declarator, binding, constant);
|
|
4329
|
+
if (!alwaysNotifies && !isFresh(expression)) return;
|
|
4330
|
+
}
|
|
3898
4331
|
if (binding.references.length !== 1) {
|
|
3899
4332
|
this.reason = "memo-reads";
|
|
3900
4333
|
return;
|
|
@@ -3915,11 +4348,18 @@ var MemoInliner = class {
|
|
|
3915
4348
|
this.reason = "memo-read-place";
|
|
3916
4349
|
return;
|
|
3917
4350
|
}
|
|
4351
|
+
const container = this.containerOf(call);
|
|
4352
|
+
const owner = container ? this.context.parents.get(container) : void 0;
|
|
4353
|
+
if (kind === "children" && (!container || owner?.type !== "JSXElement" && owner?.type !== "JSXFragment")) {
|
|
4354
|
+
this.reason = "memo-read-place";
|
|
4355
|
+
return;
|
|
4356
|
+
}
|
|
3918
4357
|
if (!this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) {
|
|
3919
4358
|
this.reason = "memo-scope";
|
|
3920
4359
|
return;
|
|
3921
4360
|
}
|
|
3922
|
-
this.context.s.overwrite(
|
|
4361
|
+
if (kind === "children" && container) this.context.s.overwrite(container.start, container.end, textOf(this.context, expression));
|
|
4362
|
+
else this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
|
|
3923
4363
|
this.context.s.remove(statement.start, statement.end);
|
|
3924
4364
|
this.changed = true;
|
|
3925
4365
|
return true;
|
|
@@ -3943,181 +4383,20 @@ function inlineMemos(context) {
|
|
|
3943
4383
|
return inliner.changed;
|
|
3944
4384
|
}
|
|
3945
4385
|
//#endregion
|
|
3946
|
-
//#region src/server.ts
|
|
3947
|
-
/** Calls the server does not run. */
|
|
3948
|
-
const REMOVED = /* @__PURE__ */ new Set(["createEffect", "onMount"]);
|
|
3949
|
-
/** Calls the server runs once, in place. */
|
|
3950
|
-
const RUN_ONCE = /* @__PURE__ */ new Set(["createRenderEffect", "createComputed"]);
|
|
3951
|
-
/**
|
|
3952
|
-
* Whether a statement starting with `text` could join the statement before
|
|
3953
|
-
* it when that one has no semicolon.
|
|
3954
|
-
*/
|
|
3955
|
-
function continuesPreviousStatement(text) {
|
|
3956
|
-
return /^[([`+\-/]/.test(text);
|
|
3957
|
-
}
|
|
3958
|
-
var ServerSimplifier = class {
|
|
3959
|
-
constructor(context) {
|
|
3960
|
-
this.context = context;
|
|
3961
|
-
this.changed = false;
|
|
3962
|
-
}
|
|
3963
|
-
/**
|
|
3964
|
-
* The Solid export a callee refers to, through a named import, an alias,
|
|
3965
|
-
* or a namespace import like `solid.untrack`.
|
|
3966
|
-
*/
|
|
3967
|
-
calleeName(callee) {
|
|
3968
|
-
const { references } = this.context.scopes;
|
|
3969
|
-
if (callee.type === "Identifier") {
|
|
3970
|
-
const binding = references.get(callee);
|
|
3971
|
-
return binding ? solidExport(binding, this.context.options) : void 0;
|
|
3972
|
-
}
|
|
3973
|
-
if (callee.type !== "MemberExpression" || callee.object.type !== "Identifier") return;
|
|
3974
|
-
const binding = references.get(callee.object);
|
|
3975
|
-
if (binding?.kind !== "import" || binding.imported !== "*" || binding.source === void 0 || !this.context.options.moduleSources.includes(binding.source)) return;
|
|
3976
|
-
if (!callee.computed && callee.property.type === "Identifier") return callee.property.name;
|
|
3977
|
-
if (callee.computed && callee.property.type === "Literal" && typeof callee.property.value === "string") return callee.property.value;
|
|
3978
|
-
}
|
|
3979
|
-
/**
|
|
3980
|
-
* Whether dropping `node` loses nothing observable. Solid's `on` only
|
|
3981
|
-
* creates a function, so `createEffect(on(source, fn))` can go too.
|
|
3982
|
-
*/
|
|
3983
|
-
isDiscardable(node) {
|
|
3984
|
-
if (isSideEffectFree(node)) return true;
|
|
3985
|
-
const inner = node.type === "ParenthesizedExpression" ? unwrap(node) : node;
|
|
3986
|
-
return inner.type === "CallExpression" && this.calleeName(inner.callee) === "on" && inner.arguments.every((argument) => argument.type !== "SpreadElement" && this.isDiscardable(argument));
|
|
3987
|
-
}
|
|
3988
|
-
/**
|
|
3989
|
-
* The text of calling `fn` with `args`, the way Solid's server calls it,
|
|
3990
|
-
* and whether it binds as tightly as a call does.
|
|
3991
|
-
*/
|
|
3992
|
-
callText(fn, args) {
|
|
3993
|
-
const inner = unwrap(fn);
|
|
3994
|
-
const argsText = args.map((argument) => textOf(this.context, argument)).join(", ");
|
|
3995
|
-
if (inner.type === "ArrowFunctionExpression" && inner.expression && inner.params.length === 0 && args.length === 0 && inner.body.type !== "BlockStatement") {
|
|
3996
|
-
const { body } = inner;
|
|
3997
|
-
return {
|
|
3998
|
-
text: textOf(this.context, body),
|
|
3999
|
-
call: isPrimary(body) || body.type === "CallExpression"
|
|
4000
|
-
};
|
|
4001
|
-
}
|
|
4002
|
-
const text = textOf(this.context, fn);
|
|
4003
|
-
if (inner.type === "Identifier") return {
|
|
4004
|
-
text: `${text}(${argsText})`,
|
|
4005
|
-
call: true
|
|
4006
|
-
};
|
|
4007
|
-
if (inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression" || fn.type === "ParenthesizedExpression") return {
|
|
4008
|
-
text: `(${text})(${argsText})`,
|
|
4009
|
-
call: true
|
|
4010
|
-
};
|
|
4011
|
-
return {
|
|
4012
|
-
text: `(0, ${text})(${argsText})`,
|
|
4013
|
-
call: true
|
|
4014
|
-
};
|
|
4015
|
-
}
|
|
4016
|
-
/**
|
|
4017
|
-
* Whether a statement at `position` could be read as a continuation of
|
|
4018
|
-
* the one before it, which happens when that one has no semicolon.
|
|
4019
|
-
*/
|
|
4020
|
-
followsOpenStatement(position) {
|
|
4021
|
-
const before = this.context.code.slice(0, position).trimEnd();
|
|
4022
|
-
return before !== "" && !before.endsWith(";") && !before.endsWith("{");
|
|
4023
|
-
}
|
|
4024
|
-
replace(call, text, primary = false) {
|
|
4025
|
-
const parent = this.context.parents.get(call);
|
|
4026
|
-
if (parent?.type === "ExpressionStatement") {
|
|
4027
|
-
if (text === void 0) {
|
|
4028
|
-
const list = this.context.parents.get(parent)?.type;
|
|
4029
|
-
if (list === "Program" || list === "BlockStatement" || list === "StaticBlock" || list === "SwitchCase") this.context.s.remove(parent.start, parent.end);
|
|
4030
|
-
else this.context.s.overwrite(parent.start, parent.end, ";");
|
|
4031
|
-
} else {
|
|
4032
|
-
let statement = text;
|
|
4033
|
-
if (/^(\{|function\b|async function\b|class\b)/.test(statement)) statement = `(${statement})`;
|
|
4034
|
-
const guarded = continuesPreviousStatement(statement) && this.followsOpenStatement(parent.start);
|
|
4035
|
-
this.context.s.overwrite(call.start, call.end, guarded ? `;${statement}` : statement);
|
|
4036
|
-
}
|
|
4037
|
-
} else {
|
|
4038
|
-
const value = text ?? "void 0";
|
|
4039
|
-
const bare = !(parent?.type === "NewExpression" && parent.callee === call) && (primary || acceptsAnyExpression(this.context, call));
|
|
4040
|
-
this.context.s.overwrite(call.start, call.end, bare ? value : `(${value})`);
|
|
4041
|
-
}
|
|
4042
|
-
this.changed = true;
|
|
4043
|
-
}
|
|
4044
|
-
simplify(call) {
|
|
4045
|
-
const name = this.calleeName(call.callee);
|
|
4046
|
-
if (name === void 0 || call.arguments.some((arg) => arg.type === "SpreadElement")) return;
|
|
4047
|
-
const args = call.arguments.filter((arg) => arg.type !== "SpreadElement");
|
|
4048
|
-
const first = args.at(0);
|
|
4049
|
-
const second = args.at(1);
|
|
4050
|
-
const rest = args.slice(2);
|
|
4051
|
-
const inStatement = this.context.parents.get(call)?.type === "ExpressionStatement";
|
|
4052
|
-
if (REMOVED.has(name)) {
|
|
4053
|
-
if (args.every((arg) => this.isDiscardable(arg))) this.replace(call, void 0);
|
|
4054
|
-
return;
|
|
4055
|
-
}
|
|
4056
|
-
switch (name) {
|
|
4057
|
-
case "getListener":
|
|
4058
|
-
if (args.length === 0) this.replace(call, "null");
|
|
4059
|
-
return;
|
|
4060
|
-
case "untrack":
|
|
4061
|
-
case "batch":
|
|
4062
|
-
if (first && args.length === 1) {
|
|
4063
|
-
const run = this.callText(first, []);
|
|
4064
|
-
this.replace(call, run.text, run.call);
|
|
4065
|
-
}
|
|
4066
|
-
return;
|
|
4067
|
-
case "startTransition":
|
|
4068
|
-
if (first && args.length === 1) {
|
|
4069
|
-
const { text } = this.callText(first, []);
|
|
4070
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
4071
|
-
}
|
|
4072
|
-
return;
|
|
4073
|
-
case "createDeferred":
|
|
4074
|
-
if (first && args.slice(1).every((arg) => this.isDiscardable(arg))) {
|
|
4075
|
-
const text = textOf(this.context, first);
|
|
4076
|
-
this.replace(call, text, isPrimary(first));
|
|
4077
|
-
}
|
|
4078
|
-
return;
|
|
4079
|
-
case "createMemo":
|
|
4080
|
-
if (first && rest.every((arg) => this.isDiscardable(arg))) {
|
|
4081
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
4082
|
-
this.replace(call, `((value) => () => value)(${text})`, true);
|
|
4083
|
-
}
|
|
4084
|
-
return;
|
|
4085
|
-
default: if (RUN_ONCE.has(name) && first && rest.length === 0) {
|
|
4086
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
4087
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
4088
|
-
}
|
|
4089
|
-
}
|
|
4090
|
-
}
|
|
4091
|
-
walk(node) {
|
|
4092
|
-
forEachChild(node, (child) => {
|
|
4093
|
-
this.walk(child);
|
|
4094
|
-
});
|
|
4095
|
-
if (node.type === "CallExpression") this.simplify(node);
|
|
4096
|
-
}
|
|
4097
|
-
};
|
|
4098
|
-
/**
|
|
4099
|
-
* Runs the server pass over a parsed program. Returns whether anything changed.
|
|
4100
|
-
*/
|
|
4101
|
-
function simplifyServer(context) {
|
|
4102
|
-
const simplifier = new ServerSimplifier(context);
|
|
4103
|
-
simplifier.walk(context.program);
|
|
4104
|
-
return simplifier.changed;
|
|
4105
|
-
}
|
|
4106
|
-
//#endregion
|
|
4107
4386
|
//#region src/compile.ts
|
|
4108
4387
|
const DEFAULT_BUILT_INS = [
|
|
4109
4388
|
"For",
|
|
4110
4389
|
"Show",
|
|
4111
4390
|
"Switch",
|
|
4112
4391
|
"Match",
|
|
4113
|
-
"
|
|
4114
|
-
"
|
|
4392
|
+
"Loading",
|
|
4393
|
+
"Reveal",
|
|
4115
4394
|
"Portal",
|
|
4116
|
-
"
|
|
4395
|
+
"Repeat",
|
|
4117
4396
|
"Dynamic",
|
|
4118
|
-
"
|
|
4397
|
+
"Errored"
|
|
4119
4398
|
];
|
|
4120
|
-
const DEFAULT_MODULE_SOURCES = ["solid-js", "
|
|
4399
|
+
const DEFAULT_MODULE_SOURCES = ["solid-js", "@solidjs/web"];
|
|
4121
4400
|
function resolveOptions(options) {
|
|
4122
4401
|
return {
|
|
4123
4402
|
builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
|
|
@@ -4125,6 +4404,7 @@ function resolveOptions(options) {
|
|
|
4125
4404
|
builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
|
|
4126
4405
|
constantVars: options.constantVars ?? false,
|
|
4127
4406
|
importedConstants: resolveImportedConstants(options.importedConstants),
|
|
4407
|
+
globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
|
|
4128
4408
|
importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
|
|
4129
4409
|
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
4130
4410
|
alwaysInline: options.alwaysInline ?? false
|
|
@@ -4206,13 +4486,6 @@ function compile(code, options = {}) {
|
|
|
4206
4486
|
}
|
|
4207
4487
|
if (!changed) break;
|
|
4208
4488
|
}
|
|
4209
|
-
if (options.server ?? false) {
|
|
4210
|
-
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, simplifyServer, void 0);
|
|
4211
|
-
if (result) {
|
|
4212
|
-
current = result.code;
|
|
4213
|
-
if (result.map !== void 0) maps.push(result.map);
|
|
4214
|
-
}
|
|
4215
|
-
}
|
|
4216
4489
|
const report = skipped ? { skipped } : {};
|
|
4217
4490
|
if (maps.length === 0) return {
|
|
4218
4491
|
code: current,
|
|
@@ -4229,7 +4502,7 @@ function compile(code, options = {}) {
|
|
|
4229
4502
|
* Reads what a module imports and the constants it exports, so that
|
|
4230
4503
|
* `compile` can fold them in the modules that import it.
|
|
4231
4504
|
*
|
|
4232
|
-
* Only `filename` and `
|
|
4505
|
+
* Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
|
|
4233
4506
|
*/
|
|
4234
4507
|
function readModuleConstants(code, options = {}) {
|
|
4235
4508
|
const program = parse(options.filename ?? "input.jsx", code);
|
|
@@ -4241,4 +4514,4 @@ function readModuleConstants(code, options = {}) {
|
|
|
4241
4514
|
//#endregion
|
|
4242
4515
|
export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
|
|
4243
4516
|
|
|
4244
|
-
//# sourceMappingURL=compile-
|
|
4517
|
+
//# sourceMappingURL=compile-Cz6hbiQv.mjs.map
|