solid-optimizer 1.0.0-next.8 → 1.0.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 +5 -5
- package/dist/{compile-O1Z60VsW.mjs → compile-Cvjy2e11.mjs} +430 -172
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-X_SMDFr2.d.mts → compile-ioIkBKzR.d.mts} +1 -2
- package/dist/compile-ioIkBKzR.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +55 -14
- package/dist/vite.mjs.map +1 -1
- package/package.json +6 -6
- package/src/compile.ts +6 -8
- package/src/conditional.ts +29 -22
- package/src/context.ts +3 -3
- package/src/copy-size.ts +6 -5
- package/src/fold.ts +161 -53
- package/src/inline.ts +70 -60
- package/src/jsx.ts +16 -14
- package/src/memo.ts +12 -6
- package/src/scope.ts +115 -9
- package/src/text.ts +297 -0
- package/src/vite/index.ts +66 -12
- package/dist/compile-O1Z60VsW.mjs.map +0 -1
- package/dist/compile-X_SMDFr2.d.mts.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import remapping from "@jridgewell/remapping";
|
|
2
|
-
import MagicString from "magic-string";
|
|
3
2
|
import { parseSync, rawTransferSupported, visitorKeys } from "oxc-parser";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
4
|
import { parse, parseFragment, serialize } from "parse5";
|
|
5
5
|
//#region src/ast.ts
|
|
6
6
|
/**
|
|
@@ -258,6 +258,30 @@ function isWithinScope(scope, ancestor) {
|
|
|
258
258
|
}
|
|
259
259
|
return false;
|
|
260
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Whether a parameter list holds an expression: a default value or a computed
|
|
263
|
+
* key, at any depth.
|
|
264
|
+
*/
|
|
265
|
+
function hasParameterExpressions(params) {
|
|
266
|
+
for (const param of params) if (hasPatternExpression(param)) return true;
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
function hasPatternExpression(pattern) {
|
|
270
|
+
switch (pattern.type) {
|
|
271
|
+
case "AssignmentPattern": return true;
|
|
272
|
+
case "ObjectPattern":
|
|
273
|
+
for (const property of pattern.properties) if (property.type === "RestElement") {
|
|
274
|
+
if (hasPatternExpression(property.argument)) return true;
|
|
275
|
+
} else if (property.computed || hasPatternExpression(property.value)) return true;
|
|
276
|
+
return false;
|
|
277
|
+
case "ArrayPattern":
|
|
278
|
+
for (const element of pattern.elements) if (element && hasPatternExpression(element)) return true;
|
|
279
|
+
return false;
|
|
280
|
+
case "RestElement": return hasPatternExpression(pattern.argument);
|
|
281
|
+
case "TSParameterProperty": return hasPatternExpression(pattern.parameter);
|
|
282
|
+
default: return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
261
285
|
function exportName$1(name) {
|
|
262
286
|
return name.type === "Literal" ? name.value : name.name;
|
|
263
287
|
}
|
|
@@ -266,6 +290,7 @@ var Analyzer = class {
|
|
|
266
290
|
this.scopes = /* @__PURE__ */ new Map();
|
|
267
291
|
this.names = /* @__PURE__ */ new Set();
|
|
268
292
|
this.pending = [];
|
|
293
|
+
this.bodies = /* @__PURE__ */ new Set();
|
|
269
294
|
this.root = {
|
|
270
295
|
node: program,
|
|
271
296
|
parent: void 0,
|
|
@@ -303,7 +328,10 @@ var Analyzer = class {
|
|
|
303
328
|
}
|
|
304
329
|
declare(identifier, kind, declaration, source, imported) {
|
|
305
330
|
let target = this.scope;
|
|
306
|
-
if (kind === "var")
|
|
331
|
+
if (kind === "var") {
|
|
332
|
+
while (!target.isFunction && target.parent) target = target.parent;
|
|
333
|
+
if (this.bodies.has(target.node) && target.parent?.bindings.has(identifier.name)) target = target.parent;
|
|
334
|
+
}
|
|
307
335
|
this.names.add(identifier.name);
|
|
308
336
|
const existing = target.bindings.get(identifier.name);
|
|
309
337
|
if (existing) {
|
|
@@ -399,10 +427,23 @@ var Analyzer = class {
|
|
|
399
427
|
this.withScope(node, true, () => {
|
|
400
428
|
if (node.type === "FunctionExpression" && node.id) this.declare(node.id, "other", node);
|
|
401
429
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
402
|
-
if (node.body)
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
430
|
+
if (node.body) this.visitBody(node.body, node.params);
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Visits the block body of a function. When a parameter holds an expression,
|
|
435
|
+
* the body gets its own scope, so the expression cannot see the body's
|
|
436
|
+
* declarations.
|
|
437
|
+
*/
|
|
438
|
+
visitBody(body, params) {
|
|
439
|
+
if (!hasParameterExpressions(params)) {
|
|
440
|
+
this.scopes.set(body, this.scope);
|
|
441
|
+
this.visitStatements(body.body);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.bodies.add(body);
|
|
445
|
+
this.withScope(body, true, () => {
|
|
446
|
+
this.visitStatements(body.body);
|
|
406
447
|
});
|
|
407
448
|
}
|
|
408
449
|
visitClass(node) {
|
|
@@ -458,10 +499,8 @@ var Analyzer = class {
|
|
|
458
499
|
case "ArrowFunctionExpression":
|
|
459
500
|
this.withScope(node, true, () => {
|
|
460
501
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
461
|
-
if (node.body.type === "BlockStatement")
|
|
462
|
-
|
|
463
|
-
this.visitStatements(node.body.body);
|
|
464
|
-
} else this.visit(node.body);
|
|
502
|
+
if (node.body.type === "BlockStatement") this.visitBody(node.body, node.params);
|
|
503
|
+
else this.visit(node.body);
|
|
465
504
|
});
|
|
466
505
|
break;
|
|
467
506
|
case "ClassDeclaration":
|
|
@@ -562,13 +601,27 @@ var Analyzer = class {
|
|
|
562
601
|
for (const child of node.children) this.visit(child);
|
|
563
602
|
if (node.closingElement) this.visitJSXName(node.closingElement.name);
|
|
564
603
|
break;
|
|
565
|
-
case "TSEnumDeclaration":
|
|
566
604
|
case "TSImportEqualsDeclaration":
|
|
567
605
|
this.declare(node.id, "other", node);
|
|
568
606
|
break;
|
|
569
|
-
case "
|
|
570
|
-
|
|
607
|
+
case "TSEnumDeclaration":
|
|
608
|
+
this.declare(node.id, "other", node);
|
|
609
|
+
if (node.declare) break;
|
|
610
|
+
this.withScope(node, false, () => {
|
|
611
|
+
for (const member of node.body.members) if (member.id.type === "Identifier") this.declare(member.id, "other", member);
|
|
612
|
+
for (const member of node.body.members) if (member.initializer) this.visit(member.initializer);
|
|
613
|
+
});
|
|
571
614
|
break;
|
|
615
|
+
case "TSModuleDeclaration": {
|
|
616
|
+
let id = node.id;
|
|
617
|
+
while (id.type === "TSQualifiedName") id = id.left;
|
|
618
|
+
if (id.type === "Identifier") this.declare(id, "other", node);
|
|
619
|
+
const { body } = node;
|
|
620
|
+
if (!node.declare && body) this.withScope(node, true, () => {
|
|
621
|
+
this.visitStatements(body.body);
|
|
622
|
+
});
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
572
625
|
default: forEachChild(node, (child) => {
|
|
573
626
|
this.visit(child);
|
|
574
627
|
});
|
|
@@ -1002,6 +1055,238 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
|
|
|
1002
1055
|
return result;
|
|
1003
1056
|
}
|
|
1004
1057
|
//#endregion
|
|
1058
|
+
//#region src/text.ts
|
|
1059
|
+
const WRITTEN = 2147483648;
|
|
1060
|
+
const LOOSE = 4294967295;
|
|
1061
|
+
const LOOSE_UNITS = "";
|
|
1062
|
+
function encodeOrigin(value) {
|
|
1063
|
+
return String.fromCharCode(value >>> 16, value & 65535);
|
|
1064
|
+
}
|
|
1065
|
+
function decodeOrigin(origins, index) {
|
|
1066
|
+
return (origins.charCodeAt(index * 2) << 16 | origins.charCodeAt(index * 2 + 1)) >>> 0;
|
|
1067
|
+
}
|
|
1068
|
+
function repeatOrigin(value, length) {
|
|
1069
|
+
return encodeOrigin(value).repeat(length);
|
|
1070
|
+
}
|
|
1071
|
+
/** The line of the original that holds an offset. */
|
|
1072
|
+
function findLine(lineStarts, offset) {
|
|
1073
|
+
let low = 0;
|
|
1074
|
+
let high = lineStarts.length - 1;
|
|
1075
|
+
while (low < high) {
|
|
1076
|
+
const middle = low + high + 1 >> 1;
|
|
1077
|
+
if ((lineStarts[middle] ?? 0) <= offset) low = middle;
|
|
1078
|
+
else high = middle - 1;
|
|
1079
|
+
}
|
|
1080
|
+
return low;
|
|
1081
|
+
}
|
|
1082
|
+
let identityCode;
|
|
1083
|
+
let identity = "";
|
|
1084
|
+
/** The origins of unedited code: each character its own offset. */
|
|
1085
|
+
function createIdentity(code) {
|
|
1086
|
+
if (identityCode === code) return identity;
|
|
1087
|
+
const chunks = [];
|
|
1088
|
+
const units = new Uint16Array(Math.min(code.length, 4096) * 2);
|
|
1089
|
+
for (let start = 0; start < code.length; start += 4096) {
|
|
1090
|
+
const end = Math.min(code.length, start + 4096);
|
|
1091
|
+
for (let index = start; index < end; index += 1) {
|
|
1092
|
+
units[(index - start) * 2] = index >>> 16;
|
|
1093
|
+
units[(index - start) * 2 + 1] = index & 65535;
|
|
1094
|
+
}
|
|
1095
|
+
chunks.push(String.fromCharCode(...units.subarray(0, (end - start) * 2)));
|
|
1096
|
+
}
|
|
1097
|
+
identityCode = code;
|
|
1098
|
+
identity = chunks.join("");
|
|
1099
|
+
return identity;
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Code with the origin of each of its characters, so that the source map
|
|
1103
|
+
* can point a copied character back at the one it copies.
|
|
1104
|
+
*
|
|
1105
|
+
* `origins` is `undefined` when the compile writes no source map.
|
|
1106
|
+
*/
|
|
1107
|
+
var Text = class {
|
|
1108
|
+
constructor(code, origins) {
|
|
1109
|
+
this.code = code;
|
|
1110
|
+
this.origins = origins;
|
|
1111
|
+
}
|
|
1112
|
+
toString() {
|
|
1113
|
+
return this.code;
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
/** The code of a content. */
|
|
1117
|
+
function getCode(content) {
|
|
1118
|
+
return typeof content === "string" ? content : content.code;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Joins contents into one text. The characters of a string take the offset
|
|
1122
|
+
* of the edit that writes the text.
|
|
1123
|
+
*/
|
|
1124
|
+
function join(parts, separator = "") {
|
|
1125
|
+
let code = "";
|
|
1126
|
+
let origins = "";
|
|
1127
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
1128
|
+
const part = parts[index] ?? "";
|
|
1129
|
+
if (index > 0 && separator) {
|
|
1130
|
+
code += separator;
|
|
1131
|
+
if (origins !== void 0) origins += repeatOrigin(LOOSE, separator.length);
|
|
1132
|
+
}
|
|
1133
|
+
if (typeof part === "string") {
|
|
1134
|
+
code += part;
|
|
1135
|
+
if (origins !== void 0) origins += repeatOrigin(LOOSE, part.length);
|
|
1136
|
+
} else {
|
|
1137
|
+
code += part.code;
|
|
1138
|
+
origins = part.origins === void 0 ? void 0 : origins + part.origins;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
return new Text(code, origins);
|
|
1142
|
+
}
|
|
1143
|
+
/** Builds a text from a template, keeping the origins of the texts it holds. */
|
|
1144
|
+
function joinText(strings, ...values) {
|
|
1145
|
+
const parts = [];
|
|
1146
|
+
for (let index = 0; index < strings.length; index += 1) {
|
|
1147
|
+
parts.push(strings[index] ?? "");
|
|
1148
|
+
if (index < values.length) parts.push(values[index] ?? "");
|
|
1149
|
+
}
|
|
1150
|
+
return join(parts);
|
|
1151
|
+
}
|
|
1152
|
+
/** The origins of a content written at an offset. */
|
|
1153
|
+
function resolveOrigins(content, at) {
|
|
1154
|
+
if (typeof content === "string") return repeatOrigin(WRITTEN | at, content.length);
|
|
1155
|
+
const { origins } = content;
|
|
1156
|
+
if (origins === void 0) return repeatOrigin(WRITTEN | at, content.code.length);
|
|
1157
|
+
let result = "";
|
|
1158
|
+
let from = 0;
|
|
1159
|
+
const written = encodeOrigin(WRITTEN | at);
|
|
1160
|
+
let found = origins.indexOf(LOOSE_UNITS);
|
|
1161
|
+
while (found !== -1) {
|
|
1162
|
+
if (found % 2 === 1) {
|
|
1163
|
+
found = origins.indexOf(LOOSE_UNITS, found + 1);
|
|
1164
|
+
continue;
|
|
1165
|
+
}
|
|
1166
|
+
result += origins.slice(from, found) + written;
|
|
1167
|
+
from = found + 2;
|
|
1168
|
+
found = origins.indexOf(LOOSE_UNITS, from);
|
|
1169
|
+
}
|
|
1170
|
+
return from === 0 ? origins : result + origins.slice(from);
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Edits code like `MagicString`, and keeps the origin of each character of
|
|
1174
|
+
* the result.
|
|
1175
|
+
*
|
|
1176
|
+
* A second `MagicString` holds the origins, two code units per character,
|
|
1177
|
+
* and takes every edit the first one takes.
|
|
1178
|
+
*/
|
|
1179
|
+
var Editor = class Editor {
|
|
1180
|
+
constructor(original, tracksOrigins) {
|
|
1181
|
+
this.original = original;
|
|
1182
|
+
this.tracksOrigins = tracksOrigins;
|
|
1183
|
+
this.s = new MagicString(original);
|
|
1184
|
+
}
|
|
1185
|
+
/** A new editor on the same original code, which keeps origins when this one does. */
|
|
1186
|
+
fresh() {
|
|
1187
|
+
return new Editor(this.original, this.tracksOrigins);
|
|
1188
|
+
}
|
|
1189
|
+
/** The origins to edit, or `undefined` when this editor keeps none. */
|
|
1190
|
+
editOrigins() {
|
|
1191
|
+
if (this.tracksOrigins && !this.o) this.o = new MagicString(createIdentity(this.original));
|
|
1192
|
+
return this.o;
|
|
1193
|
+
}
|
|
1194
|
+
/** Code that a pass writes in place of what is at `at`, mapped there. */
|
|
1195
|
+
written(code, at) {
|
|
1196
|
+
return new Text(code, this.tracksOrigins ? repeatOrigin(WRITTEN | at, code.length) : void 0);
|
|
1197
|
+
}
|
|
1198
|
+
overwrite(start, end, content) {
|
|
1199
|
+
this.s.overwrite(start, end, getCode(content));
|
|
1200
|
+
this.editOrigins()?.overwrite(start * 2, end * 2, resolveOrigins(content, start));
|
|
1201
|
+
return this;
|
|
1202
|
+
}
|
|
1203
|
+
remove(start, end) {
|
|
1204
|
+
this.s.remove(start, end);
|
|
1205
|
+
this.editOrigins()?.remove(start * 2, end * 2);
|
|
1206
|
+
return this;
|
|
1207
|
+
}
|
|
1208
|
+
prependLeft(index, content) {
|
|
1209
|
+
this.s.prependLeft(index, getCode(content));
|
|
1210
|
+
this.editOrigins()?.prependLeft(index * 2, resolveOrigins(content, index));
|
|
1211
|
+
return this;
|
|
1212
|
+
}
|
|
1213
|
+
prependRight(index, content) {
|
|
1214
|
+
this.s.prependRight(index, getCode(content));
|
|
1215
|
+
this.editOrigins()?.prependRight(index * 2, resolveOrigins(content, index));
|
|
1216
|
+
return this;
|
|
1217
|
+
}
|
|
1218
|
+
appendLeft(index, content) {
|
|
1219
|
+
this.s.appendLeft(index, getCode(content));
|
|
1220
|
+
this.editOrigins()?.appendLeft(index * 2, resolveOrigins(content, index));
|
|
1221
|
+
return this;
|
|
1222
|
+
}
|
|
1223
|
+
/** The edited code of characters `start` to `end` of the original, with origins. */
|
|
1224
|
+
slice(start, end) {
|
|
1225
|
+
let origins;
|
|
1226
|
+
if (this.o) origins = this.o.slice(start * 2, end * 2);
|
|
1227
|
+
else if (this.tracksOrigins) origins = createIdentity(this.original).slice(start * 2, end * 2);
|
|
1228
|
+
return new Text(this.s.slice(start, end), origins);
|
|
1229
|
+
}
|
|
1230
|
+
toString() {
|
|
1231
|
+
return this.s.toString();
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* A map from the edited code to the original. A copied character maps to
|
|
1235
|
+
* the one it copies, and a written run maps to where it was written.
|
|
1236
|
+
*/
|
|
1237
|
+
generateMap(source) {
|
|
1238
|
+
const code = this.s.toString();
|
|
1239
|
+
const origins = this.o?.toString() ?? createIdentity(this.original);
|
|
1240
|
+
const lineStarts = [0];
|
|
1241
|
+
for (let index = 0; index < this.original.length; index += 1) if (this.original.charCodeAt(index) === 10) lineStarts.push(index + 1);
|
|
1242
|
+
const mappings = [[]];
|
|
1243
|
+
let line = mappings[0] ?? [];
|
|
1244
|
+
let column = 0;
|
|
1245
|
+
let previous = LOOSE;
|
|
1246
|
+
let sourceLine = 0;
|
|
1247
|
+
let sourceColumn = 0;
|
|
1248
|
+
for (let index = 0; index < code.length; index += 1) {
|
|
1249
|
+
const origin = decodeOrigin(origins, index);
|
|
1250
|
+
if (origin < WRITTEN) {
|
|
1251
|
+
if (origin === previous + 1 && this.original.charCodeAt(previous) !== 10) sourceColumn += 1;
|
|
1252
|
+
else {
|
|
1253
|
+
sourceLine = findLine(lineStarts, origin);
|
|
1254
|
+
sourceColumn = origin - (lineStarts[sourceLine] ?? 0);
|
|
1255
|
+
}
|
|
1256
|
+
line.push([
|
|
1257
|
+
column,
|
|
1258
|
+
0,
|
|
1259
|
+
sourceLine,
|
|
1260
|
+
sourceColumn
|
|
1261
|
+
]);
|
|
1262
|
+
} else if (origin !== previous || column === 0) {
|
|
1263
|
+
const offset = origin - WRITTEN;
|
|
1264
|
+
const written = findLine(lineStarts, offset);
|
|
1265
|
+
line.push([
|
|
1266
|
+
column,
|
|
1267
|
+
0,
|
|
1268
|
+
written,
|
|
1269
|
+
offset - (lineStarts[written] ?? 0)
|
|
1270
|
+
]);
|
|
1271
|
+
}
|
|
1272
|
+
previous = origin;
|
|
1273
|
+
if (code.charCodeAt(index) === 10) {
|
|
1274
|
+
line = [];
|
|
1275
|
+
mappings.push(line);
|
|
1276
|
+
column = 0;
|
|
1277
|
+
} else column += 1;
|
|
1278
|
+
}
|
|
1279
|
+
return {
|
|
1280
|
+
version: 3,
|
|
1281
|
+
file: null,
|
|
1282
|
+
names: [],
|
|
1283
|
+
sources: [source],
|
|
1284
|
+
sourcesContent: [this.original],
|
|
1285
|
+
mappings
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
//#endregion
|
|
1005
1290
|
//#region src/jsx.ts
|
|
1006
1291
|
function keptPiece(node) {
|
|
1007
1292
|
const text = node.type === "JSXText" ? node.value : void 0;
|
|
@@ -1031,10 +1316,11 @@ function splicePiece(s, node, children) {
|
|
|
1031
1316
|
* mark where the text ends.
|
|
1032
1317
|
*/
|
|
1033
1318
|
function replacedPiece(node, text) {
|
|
1034
|
-
const
|
|
1035
|
-
const
|
|
1036
|
-
const
|
|
1037
|
-
const
|
|
1319
|
+
const code = getCode(text);
|
|
1320
|
+
const start = code.search(/[<{]/);
|
|
1321
|
+
const end = Math.max(code.lastIndexOf(">"), code.lastIndexOf("}"));
|
|
1322
|
+
const startText = start === -1 ? code : code.slice(0, start);
|
|
1323
|
+
const endText = code.slice(end + 1);
|
|
1038
1324
|
return {
|
|
1039
1325
|
node,
|
|
1040
1326
|
text,
|
|
@@ -1058,12 +1344,12 @@ function mergeChangesText(before, after) {
|
|
|
1058
1344
|
*/
|
|
1059
1345
|
function separate(previous, next, emptiesBetween) {
|
|
1060
1346
|
if (next.text !== void 0) {
|
|
1061
|
-
next.text = `{}${next.text}`;
|
|
1347
|
+
next.text = joinText`{}${next.text}`;
|
|
1062
1348
|
next.startText = void 0;
|
|
1063
1349
|
return;
|
|
1064
1350
|
}
|
|
1065
1351
|
if (previous.text !== void 0) {
|
|
1066
|
-
previous.text = `${previous.text}{}`;
|
|
1352
|
+
previous.text = joinText`${previous.text}{}`;
|
|
1067
1353
|
previous.endText = void 0;
|
|
1068
1354
|
return;
|
|
1069
1355
|
}
|
|
@@ -1079,7 +1365,7 @@ function writePieces(s, pieces) {
|
|
|
1079
1365
|
let previous;
|
|
1080
1366
|
let emptiesSincePrevious = [];
|
|
1081
1367
|
for (const piece of pieces) {
|
|
1082
|
-
if (piece.text === "") {
|
|
1368
|
+
if (piece.text !== void 0 && getCode(piece.text) === "") {
|
|
1083
1369
|
emptiesSincePrevious.push(piece);
|
|
1084
1370
|
continue;
|
|
1085
1371
|
}
|
|
@@ -1090,7 +1376,7 @@ function writePieces(s, pieces) {
|
|
|
1090
1376
|
for (const piece of pieces) {
|
|
1091
1377
|
if (piece.text === void 0) continue;
|
|
1092
1378
|
changed = true;
|
|
1093
|
-
if (piece.text === "") s.remove(piece.node.start, piece.node.end);
|
|
1379
|
+
if (getCode(piece.text) === "") s.remove(piece.node.start, piece.node.end);
|
|
1094
1380
|
else s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
1095
1381
|
}
|
|
1096
1382
|
return changed;
|
|
@@ -1467,9 +1753,11 @@ var Folder = class {
|
|
|
1467
1753
|
constructor(context) {
|
|
1468
1754
|
this.context = context;
|
|
1469
1755
|
this.changed = false;
|
|
1756
|
+
this.removed = /* @__PURE__ */ new Set();
|
|
1470
1757
|
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
1471
1758
|
}
|
|
1472
1759
|
walk(node) {
|
|
1760
|
+
if (this.removed.has(node)) return;
|
|
1473
1761
|
forEachChild(node, (child) => {
|
|
1474
1762
|
this.walk(child);
|
|
1475
1763
|
});
|
|
@@ -1521,7 +1809,7 @@ var Folder = class {
|
|
|
1521
1809
|
return isJSXChild(node, this.context.parents) || this.context.parents.get(node)?.type === "JSXAttribute";
|
|
1522
1810
|
}
|
|
1523
1811
|
replace(node, text) {
|
|
1524
|
-
if (getText(this.context, node) === text) return;
|
|
1812
|
+
if (getText(this.context, node).code === getCode(text)) return;
|
|
1525
1813
|
this.context.s.overwrite(node.start, node.end, text);
|
|
1526
1814
|
this.changed = true;
|
|
1527
1815
|
}
|
|
@@ -1587,11 +1875,11 @@ var Folder = class {
|
|
|
1587
1875
|
const parts = discarded ? effectfulParts(discarded) : [];
|
|
1588
1876
|
if (parts.length === 0) {
|
|
1589
1877
|
const text = getText(this.context, value);
|
|
1590
|
-
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
1878
|
+
return needsParensAt(target, value, this.context.parents) ? joinText`(${text})` : text;
|
|
1591
1879
|
}
|
|
1592
1880
|
const texts = getTexts(this.context, parts);
|
|
1593
1881
|
texts.push(getText(this.context, value));
|
|
1594
|
-
return `(${
|
|
1882
|
+
return joinText`(${join(texts, ", ")})`;
|
|
1595
1883
|
}
|
|
1596
1884
|
foldLogical(node) {
|
|
1597
1885
|
const left = truthiness(node.left, this.lookup);
|
|
@@ -1708,7 +1996,7 @@ var Folder = class {
|
|
|
1708
1996
|
if (value.value.includes("&")) return;
|
|
1709
1997
|
return {
|
|
1710
1998
|
kind: "expression",
|
|
1711
|
-
text: JSON.stringify(value.value),
|
|
1999
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
1712
2000
|
primary: true
|
|
1713
2001
|
};
|
|
1714
2002
|
case "JSXElement":
|
|
@@ -1730,7 +2018,7 @@ var Folder = class {
|
|
|
1730
2018
|
*/
|
|
1731
2019
|
foldedJSX(node) {
|
|
1732
2020
|
const text = getText(this.context, node);
|
|
1733
|
-
if (text.startsWith("<")) return {
|
|
2021
|
+
if (text.code.startsWith("<")) return {
|
|
1734
2022
|
kind: "jsx",
|
|
1735
2023
|
text
|
|
1736
2024
|
};
|
|
@@ -1784,7 +2072,7 @@ var Folder = class {
|
|
|
1784
2072
|
let body = row.body;
|
|
1785
2073
|
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1786
2074
|
if (body.type !== "JSXElement" && body.type !== "JSXFragment" || hasRefOrSpread(body)) return;
|
|
1787
|
-
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
2075
|
+
if (this.context.s.slice(body.start, body.end).code !== this.context.code.slice(body.start, body.end)) return;
|
|
1788
2076
|
const scope = this.context.scopes.scopes.get(row);
|
|
1789
2077
|
if (!scope) return;
|
|
1790
2078
|
const params = [];
|
|
@@ -1797,11 +2085,11 @@ var Folder = class {
|
|
|
1797
2085
|
const indexParam = params.at(1);
|
|
1798
2086
|
const rows = [];
|
|
1799
2087
|
for (const [index, item] of list.items.entries()) {
|
|
1800
|
-
const copy =
|
|
2088
|
+
const copy = this.context.s.fresh();
|
|
1801
2089
|
if (itemParam && !this.substituteItem(copy, itemParam, item, byIndex) || indexParam && !this.substituteIndex(copy, indexParam, index, !byIndex)) return;
|
|
1802
2090
|
rows.push(copy.slice(body.start, body.end));
|
|
1803
2091
|
}
|
|
1804
|
-
const text = `<>${
|
|
2092
|
+
const text = joinText`<>${join(rows)}</>`;
|
|
1805
2093
|
if (!this.unrollingPays(element, text, list.declaration)) return;
|
|
1806
2094
|
return {
|
|
1807
2095
|
kind: "jsx",
|
|
@@ -1899,7 +2187,7 @@ var Folder = class {
|
|
|
1899
2187
|
}
|
|
1900
2188
|
/** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
|
|
1901
2189
|
valueAt(node, text) {
|
|
1902
|
-
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : `(${text})`;
|
|
2190
|
+
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : joinText`(${text})`;
|
|
1903
2191
|
}
|
|
1904
2192
|
/** Writes an item's index over each read of the row's index parameter. */
|
|
1905
2193
|
substituteIndex(copy, param, index, accessor) {
|
|
@@ -1921,7 +2209,7 @@ var Folder = class {
|
|
|
1921
2209
|
unrollingPays(element, text, declaration) {
|
|
1922
2210
|
const inTemplate = this.joinsParentTemplate(element);
|
|
1923
2211
|
const loop = new SizeEstimator(this.context.code).jsx(element, inTemplate) + (declaration ? compactLength(this.context.code.slice(declaration.start, declaration.end)) : 0);
|
|
1924
|
-
const code = `const __so_rows__ = ${text};`;
|
|
2212
|
+
const code = `const __so_rows__ = ${text.code};`;
|
|
1925
2213
|
const statement = parse$1(this.context.filename, code).body.at(0);
|
|
1926
2214
|
const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
|
|
1927
2215
|
if (init?.type !== "JSXFragment") return false;
|
|
@@ -2006,12 +2294,12 @@ var Folder = class {
|
|
|
2006
2294
|
}
|
|
2007
2295
|
}
|
|
2008
2296
|
if (tag === void 0) return;
|
|
2009
|
-
const attributes = [];
|
|
2297
|
+
const attributes = [tag];
|
|
2010
2298
|
for (const attribute of element.openingElement.attributes) if (attribute !== component) attributes.push(getText(this.context, attribute));
|
|
2011
|
-
const opening =
|
|
2299
|
+
const opening = join(attributes, " ");
|
|
2012
2300
|
return {
|
|
2013
2301
|
kind: "jsx",
|
|
2014
|
-
text: element.closingElement ? `<${opening}>${innerText(this.context.s, element)}</${tag}>` : `<${opening} />`
|
|
2302
|
+
text: element.closingElement ? joinText`<${opening}>${innerText(this.context.s, element)}</${tag}>` : joinText`<${opening} />`
|
|
2015
2303
|
};
|
|
2016
2304
|
}
|
|
2017
2305
|
/**
|
|
@@ -2026,7 +2314,7 @@ var Folder = class {
|
|
|
2026
2314
|
switch (resolved.kind) {
|
|
2027
2315
|
case "empty": return "null";
|
|
2028
2316
|
case "jsx": return resolved.text;
|
|
2029
|
-
case "expression": return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
2317
|
+
case "expression": return resolved.primary ? resolved.text : joinText`(${resolved.text})`;
|
|
2030
2318
|
default: {
|
|
2031
2319
|
const significant = getSignificant(resolved.children);
|
|
2032
2320
|
const only = significant.at(0);
|
|
@@ -2035,7 +2323,7 @@ var Folder = class {
|
|
|
2035
2323
|
const first = resolved.children.at(0);
|
|
2036
2324
|
const last = resolved.children.at(-1);
|
|
2037
2325
|
if (!first || !last) return "null";
|
|
2038
|
-
return `<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
2326
|
+
return joinText`<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
2039
2327
|
}
|
|
2040
2328
|
}
|
|
2041
2329
|
}
|
|
@@ -2071,7 +2359,7 @@ var Folder = class {
|
|
|
2071
2359
|
pieces.push(replacedPiece(child, resolved.text));
|
|
2072
2360
|
break;
|
|
2073
2361
|
case "expression":
|
|
2074
|
-
pieces.push(replacedPiece(child, `{${resolved.text}}`));
|
|
2362
|
+
pieces.push(replacedPiece(child, joinText`{${resolved.text}}`));
|
|
2075
2363
|
break;
|
|
2076
2364
|
default: pieces.push(replacedPiece(child, ""));
|
|
2077
2365
|
}
|
|
@@ -2102,10 +2390,10 @@ var Folder = class {
|
|
|
2102
2390
|
const { params } = callee;
|
|
2103
2391
|
const args = expression.arguments;
|
|
2104
2392
|
if (params.length !== args.length || hasRefOrSpread(body)) return;
|
|
2105
|
-
if (this.context.s.slice(body.start, body.end) !== this.context.code.slice(body.start, body.end)) return;
|
|
2393
|
+
if (this.context.s.slice(body.start, body.end).code !== this.context.code.slice(body.start, body.end)) return;
|
|
2106
2394
|
const scope = this.context.scopes.scopes.get(callee);
|
|
2107
2395
|
if (!scope) return;
|
|
2108
|
-
const copy =
|
|
2396
|
+
const copy = this.context.s.fresh();
|
|
2109
2397
|
for (const [index, param] of params.entries()) {
|
|
2110
2398
|
const arg = args.at(index);
|
|
2111
2399
|
if (param.type !== "Identifier" || !arg || arg.type === "SpreadElement") return;
|
|
@@ -2116,7 +2404,7 @@ var Folder = class {
|
|
|
2116
2404
|
if (arg.type === "Identifier" && lookup(scopeAt(this.context.scopes, this.context.parents, reference), arg.name) !== this.context.scopes.references.get(arg)) return;
|
|
2117
2405
|
const parent = this.context.parents.get(reference);
|
|
2118
2406
|
const shorthand = parent?.type === "Property" && parent.shorthand && parent.value === reference;
|
|
2119
|
-
copy.overwrite(reference.start, reference.end, shorthand ? `${param.name}: ${text}` : text);
|
|
2407
|
+
copy.overwrite(reference.start, reference.end, shorthand ? joinText`${param.name}: ${text}` : text);
|
|
2120
2408
|
}
|
|
2121
2409
|
}
|
|
2122
2410
|
return copy.slice(body.start, body.end);
|
|
@@ -2128,7 +2416,7 @@ var Folder = class {
|
|
|
2128
2416
|
switch (node.type) {
|
|
2129
2417
|
case "Literal": return "regex" in node ? void 0 : getText(this.context, node);
|
|
2130
2418
|
case "TemplateLiteral": return node.expressions.length === 0 ? getText(this.context, node) : void 0;
|
|
2131
|
-
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${getText(this.context, node)})` : void 0;
|
|
2419
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? joinText`(${getText(this.context, node)})` : void 0;
|
|
2132
2420
|
case "Identifier": {
|
|
2133
2421
|
const binding = this.context.scopes.references.get(node);
|
|
2134
2422
|
return binding && !binding.mutated ? node.name : void 0;
|
|
@@ -2160,13 +2448,63 @@ var Folder = class {
|
|
|
2160
2448
|
this.changed = true;
|
|
2161
2449
|
}
|
|
2162
2450
|
}
|
|
2451
|
+
/**
|
|
2452
|
+
* Removes each declaration inside a function that nothing references. The
|
|
2453
|
+
* references are those of the code this pass parsed, so a binding whose last
|
|
2454
|
+
* read this pass folds is removed in the next round.
|
|
2455
|
+
*/
|
|
2456
|
+
removeUnusedDeclarations() {
|
|
2457
|
+
for (const scope of this.context.scopes.scopes.values()) {
|
|
2458
|
+
if (scope === this.context.scopes.root) continue;
|
|
2459
|
+
for (const binding of scope.bindings.values()) {
|
|
2460
|
+
const declaration = this.unusedDeclaration(binding);
|
|
2461
|
+
if (declaration && !this.removed.has(declaration)) {
|
|
2462
|
+
this.removed.add(declaration);
|
|
2463
|
+
this.context.s.remove(declaration.start, declaration.end);
|
|
2464
|
+
this.changed = true;
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* The declaration statement of `binding` when every binding it declares is
|
|
2471
|
+
* unused and every initializer can be skipped.
|
|
2472
|
+
*/
|
|
2473
|
+
unusedDeclaration(binding) {
|
|
2474
|
+
if (binding.declaration.type !== "VariableDeclarator") return;
|
|
2475
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
2476
|
+
if (statement?.type !== "VariableDeclaration" || statement.kind !== "const" && statement.kind !== "let" || statement.declare) return;
|
|
2477
|
+
const container = this.context.parents.get(statement)?.type;
|
|
2478
|
+
if (container !== "BlockStatement" && container !== "StaticBlock" && container !== "SwitchCase") return;
|
|
2479
|
+
for (const declarator of statement.declarations) {
|
|
2480
|
+
if (declarator.id.type !== "Identifier") return;
|
|
2481
|
+
const declared = binding.scope.bindings.get(declarator.id.name);
|
|
2482
|
+
if (!declared || declared.mutated || declared.references.length > 0) return;
|
|
2483
|
+
if (declarator.init && !this.isSkippableInit(declarator.init)) return;
|
|
2484
|
+
}
|
|
2485
|
+
return statement;
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* Whether an unused initializer can be dropped: an expression with no side
|
|
2489
|
+
* effects, or a Solid call that only creates a value from such arguments.
|
|
2490
|
+
*/
|
|
2491
|
+
isSkippableInit(init) {
|
|
2492
|
+
if (isSideEffectFree(init)) return true;
|
|
2493
|
+
const call = unwrap(init);
|
|
2494
|
+
if (call.type !== "CallExpression" || call.callee.type !== "Identifier") return false;
|
|
2495
|
+
const callee = this.context.scopes.references.get(call.callee);
|
|
2496
|
+
const identity = callee ? solidExport(callee, this.context.options) : void 0;
|
|
2497
|
+
if (identity === void 0 || !PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return false;
|
|
2498
|
+
for (const argument of call.arguments) if (argument.type === "SpreadElement" || !isSideEffectFree(argument)) return false;
|
|
2499
|
+
return true;
|
|
2500
|
+
}
|
|
2163
2501
|
replaceStatement(statement, text) {
|
|
2164
|
-
if (text === "") {
|
|
2502
|
+
if (getCode(text) === "") {
|
|
2165
2503
|
this.context.s.remove(statement.start, statement.end);
|
|
2166
2504
|
this.changed = true;
|
|
2167
2505
|
return;
|
|
2168
2506
|
}
|
|
2169
|
-
this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
|
|
2507
|
+
this.replace(statement, continuesPreviousStatement(getCode(text)) ? joinText`;${text}` : text);
|
|
2170
2508
|
}
|
|
2171
2509
|
/**
|
|
2172
2510
|
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
@@ -2176,8 +2514,8 @@ var Folder = class {
|
|
|
2176
2514
|
const parts = effectfulParts(test);
|
|
2177
2515
|
const first = parts.at(0);
|
|
2178
2516
|
if (first === void 0) return;
|
|
2179
|
-
const text = getTexts(this.context, parts)
|
|
2180
|
-
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
2517
|
+
const text = join(getTexts(this.context, parts), ", ");
|
|
2518
|
+
return startsLikeStatement(first) ? joinText`(${text});` : joinText`${text};`;
|
|
2181
2519
|
}
|
|
2182
2520
|
/**
|
|
2183
2521
|
* Resolves an `if` with a constant test. Returns the kept branch, `null`
|
|
@@ -2194,7 +2532,7 @@ var Folder = class {
|
|
|
2194
2532
|
const effect = this.discardedTest(statement.test);
|
|
2195
2533
|
if (effect !== void 0) parts.push(effect);
|
|
2196
2534
|
if (kept) parts.push(getText(this.context, kept));
|
|
2197
|
-
this.replaceStatement(statement,
|
|
2535
|
+
this.replaceStatement(statement, join(parts, " "));
|
|
2198
2536
|
return kept;
|
|
2199
2537
|
}
|
|
2200
2538
|
resolveWhile(statement) {
|
|
@@ -2208,6 +2546,7 @@ var Folder = class {
|
|
|
2208
2546
|
*/
|
|
2209
2547
|
function fold(context) {
|
|
2210
2548
|
const folder = new Folder(context);
|
|
2549
|
+
folder.removeUnusedDeclarations();
|
|
2211
2550
|
folder.walk(context.program);
|
|
2212
2551
|
return folder.changed;
|
|
2213
2552
|
}
|
|
@@ -2727,11 +3066,6 @@ function removeProviders(context) {
|
|
|
2727
3066
|
//#endregion
|
|
2728
3067
|
//#region src/copy-size.ts
|
|
2729
3068
|
/**
|
|
2730
|
-
* Measures a component's copy at a call site: folds it, and inlines the
|
|
2731
|
-
* components it calls, as the passes after inlining would. Then it estimates
|
|
2732
|
-
* its size with `SizeEstimator`.
|
|
2733
|
-
*/
|
|
2734
|
-
/**
|
|
2735
3069
|
* The name of the function a copy is wrapped in to be folded and measured.
|
|
2736
3070
|
* It is capitalized, so the inline pass can move statements into it.
|
|
2737
3071
|
*/
|
|
@@ -2752,12 +3086,12 @@ const MAX_DEPTH = 3;
|
|
|
2752
3086
|
*/
|
|
2753
3087
|
function copySize(filename, header, copy, inTemplate, options, called, measured) {
|
|
2754
3088
|
const calls = depth < MAX_DEPTH ? called : void 0;
|
|
2755
|
-
let code = `${header}\n${calls?.declarations ?? ""}\nfunction ${WRAPPER}() {\n${copy.statements
|
|
3089
|
+
let code = `${header}\n${calls?.declarations ?? ""}\nfunction ${WRAPPER}() {\n${join(copy.statements, "\n").code}\nreturn (${getCode(copy.root)});\n}\n`;
|
|
2756
3090
|
for (let round = 0; round < 4; round += 1) {
|
|
2757
3091
|
let changed = false;
|
|
2758
3092
|
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
2759
3093
|
const program = parse$1(filename, code);
|
|
2760
|
-
const s = new
|
|
3094
|
+
const s = new Editor(code, false);
|
|
2761
3095
|
depth += 1;
|
|
2762
3096
|
let passChanged;
|
|
2763
3097
|
try {
|
|
@@ -2797,79 +3131,6 @@ function copySize(filename, header, copy, inTemplate, options, called, measured)
|
|
|
2797
3131
|
}
|
|
2798
3132
|
//#endregion
|
|
2799
3133
|
//#region src/inline.ts
|
|
2800
|
-
/**
|
|
2801
|
-
* The inline pass: replaces a component call with the component's body.
|
|
2802
|
-
*
|
|
2803
|
-
* `<Card title="Hi" />` with `function Card(props) { return <div>{props.title}</div>; }`
|
|
2804
|
-
* becomes `<div>{"Hi"}</div>`. The JSX transform then sees one tree instead of
|
|
2805
|
-
* two, so it emits one template, one clone, and fewer hydration markers.
|
|
2806
|
-
*
|
|
2807
|
-
* A component inlines when its shape keeps the result equivalent:
|
|
2808
|
-
*
|
|
2809
|
-
* - It is declared once at the top level of the module, with a capitalized
|
|
2810
|
-
* name, as a function declaration or a `const` bound to a function. A
|
|
2811
|
-
* tag of an alias, like `const Separator = SeparatorRoot`, is renamed to
|
|
2812
|
-
* the component first. See `renameAliasTags`.
|
|
2813
|
-
* - It takes no parameter, or one identifier that is only read as `props.name`,
|
|
2814
|
-
* or spread onto an element as `{...props}`.
|
|
2815
|
-
* - Its body is statements followed by one final `return` of JSX, or an arrow
|
|
2816
|
-
* function whose body is JSX.
|
|
2817
|
-
* - It does not use `this`, `arguments`, `new.target`, or `super`.
|
|
2818
|
-
*
|
|
2819
|
-
* A call site inlines when it passes plain attributes: no spread and no
|
|
2820
|
-
* namespaced attribute. A `ref` moves to the element the component forwards
|
|
2821
|
-
* `props.ref` to. See `refValue`.
|
|
2822
|
-
*
|
|
2823
|
-
* # How props keep their meaning
|
|
2824
|
-
*
|
|
2825
|
-
* Solid passes a dynamic prop as a getter, so every read of `props.name`
|
|
2826
|
-
* evaluates the attribute's expression again. Inlining replaces each read
|
|
2827
|
-
* with the expression itself, which does the same.
|
|
2828
|
-
*
|
|
2829
|
-
* A static prop, such as an identifier or a function, is evaluated once when
|
|
2830
|
-
* the component is created. Its expression is substituted directly when that
|
|
2831
|
-
* gives the same value, such as a literal or a binding nothing reassigns, or
|
|
2832
|
-
* when the prop is read exactly once. Otherwise its value is stored in a
|
|
2833
|
-
* `const` before the component body runs.
|
|
2834
|
-
*
|
|
2835
|
-
* # Where a component body can go
|
|
2836
|
-
*
|
|
2837
|
-
* A component whose body is only JSX inlines anywhere JSX can go.
|
|
2838
|
-
*
|
|
2839
|
-
* Statements, and stored props, have to run where the component would have
|
|
2840
|
-
* been created. Solid runs a component untracked, so the statements move into
|
|
2841
|
-
* the function whose returned JSX holds the call, when that function also
|
|
2842
|
-
* runs untracked:
|
|
2843
|
-
*
|
|
2844
|
-
* - a component;
|
|
2845
|
-
* - the child callback of `<For>` or `<Repeat>`, which runs once per item
|
|
2846
|
-
* under its own owner;
|
|
2847
|
-
* - the child callback of `<Show>` or `<Match>` that takes the value, which
|
|
2848
|
-
* runs once each time the condition turns true;
|
|
2849
|
-
* - the callback given to `render`, which runs once under its root.
|
|
2850
|
-
*
|
|
2851
|
-
* The call has to be in that returned JSX with only intrinsic elements and
|
|
2852
|
-
* fragments around it, so it is created exactly once each time that function
|
|
2853
|
-
* runs.
|
|
2854
|
-
*
|
|
2855
|
-
* A context provider is a component, so statements do not move out of it,
|
|
2856
|
-
* and `useContext` still runs under it. The exception is a provider whose
|
|
2857
|
-
* subtree only runs visible code. Statements move out through it, and each
|
|
2858
|
-
* `useContext` of its context in a copy becomes its value. See `provider.ts`.
|
|
2859
|
-
*
|
|
2860
|
-
* The props can also be read through `merge()` and `omit()` views declared
|
|
2861
|
-
* in the component. Each call site knows which props it passes, so a read
|
|
2862
|
-
* of a view resolves to a prop, a literal default, or `undefined`, and a
|
|
2863
|
-
* spread of a view becomes the attributes it holds.
|
|
2864
|
-
*
|
|
2865
|
-
* # Accepted differences
|
|
2866
|
-
*
|
|
2867
|
-
* - A prop that is a function and is called as `props.name()` runs with
|
|
2868
|
-
* `this` set to its own receiver instead of the props object.
|
|
2869
|
-
* - Statements moved into the host run before the host's JSX is created,
|
|
2870
|
-
* instead of while it is created. The order of `onSettled` and effect
|
|
2871
|
-
* registration across siblings can change.
|
|
2872
|
-
*/
|
|
2873
3134
|
function isWithin(node, container) {
|
|
2874
3135
|
return node.start >= container.start && node.end <= container.end;
|
|
2875
3136
|
}
|
|
@@ -3387,7 +3648,7 @@ var Inliner = class {
|
|
|
3387
3648
|
if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
|
|
3388
3649
|
const view = this.resolveView(callee, init, param, views, args);
|
|
3389
3650
|
if (view === null) continue;
|
|
3390
|
-
const binding = scope.bindings.get(declarator.id.name);
|
|
3651
|
+
const binding = ((fn.body && this.context.scopes.scopes.get(fn.body)) ?? scope).bindings.get(declarator.id.name);
|
|
3391
3652
|
if (!view || !binding || binding.mutated) return;
|
|
3392
3653
|
views.set(binding, view);
|
|
3393
3654
|
declarations.push(statement);
|
|
@@ -3514,7 +3775,7 @@ var Inliner = class {
|
|
|
3514
3775
|
const value = evaluate(property.value, () => void 0);
|
|
3515
3776
|
const text = value ? literalText(value.value) : void 0;
|
|
3516
3777
|
if (text === void 0 || isReservedPropName(key)) return;
|
|
3517
|
-
defaults.set(key, text);
|
|
3778
|
+
defaults.set(key, this.context.s.written(text, property.value.start));
|
|
3518
3779
|
}
|
|
3519
3780
|
sources.push({
|
|
3520
3781
|
defaults,
|
|
@@ -3624,11 +3885,11 @@ var Inliner = class {
|
|
|
3624
3885
|
};
|
|
3625
3886
|
let text;
|
|
3626
3887
|
let primary = true;
|
|
3627
|
-
if (significant.length > 1 || only.type === "JSXText") text = `<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
3888
|
+
if (significant.length > 1 || only.type === "JSXText") text = joinText`<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
3628
3889
|
else if (only.type === "JSXExpressionContainer") {
|
|
3629
3890
|
if (only.expression.type === "JSXEmptyExpression") return;
|
|
3630
3891
|
const value = this.expressionValue(only.expression, "children", reads, fresh, snapshots);
|
|
3631
|
-
if (value.kind !== "expression" || value.text !== getText(this.context, only.expression)) return value;
|
|
3892
|
+
if (value.kind !== "expression" || getCode(value.text) !== getText(this.context, only.expression).code) return value;
|
|
3632
3893
|
text = value.text;
|
|
3633
3894
|
primary = value.primary;
|
|
3634
3895
|
} else text = getText(this.context, only);
|
|
@@ -3679,7 +3940,7 @@ var Inliner = class {
|
|
|
3679
3940
|
if (value.value.includes("&")) return;
|
|
3680
3941
|
return {
|
|
3681
3942
|
kind: "expression",
|
|
3682
|
-
text: JSON.stringify(value.value),
|
|
3943
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
3683
3944
|
primary: true
|
|
3684
3945
|
};
|
|
3685
3946
|
case "JSXElement":
|
|
@@ -3718,7 +3979,7 @@ var Inliner = class {
|
|
|
3718
3979
|
primary
|
|
3719
3980
|
};
|
|
3720
3981
|
const local = fresh(name);
|
|
3721
|
-
snapshots.push(`const ${local} = ${text};`);
|
|
3982
|
+
snapshots.push(joinText`const ${local} = ${text};`);
|
|
3722
3983
|
return {
|
|
3723
3984
|
kind: "expression",
|
|
3724
3985
|
text: local,
|
|
@@ -3821,7 +4082,7 @@ var Inliner = class {
|
|
|
3821
4082
|
this.reason = "context-value";
|
|
3822
4083
|
return;
|
|
3823
4084
|
}
|
|
3824
|
-
const copy =
|
|
4085
|
+
const copy = this.context.s.fresh();
|
|
3825
4086
|
this.renameLocals(copy, component, fresh);
|
|
3826
4087
|
this.substituteProps(copy, views.propReads, values);
|
|
3827
4088
|
this.substituteViews(copy, component, element, views, values);
|
|
@@ -3831,7 +4092,7 @@ var Inliner = class {
|
|
|
3831
4092
|
const last = component.statements.at(-1);
|
|
3832
4093
|
if (first && last) {
|
|
3833
4094
|
const text = copy.slice(first.start, last.end);
|
|
3834
|
-
statements.push(text.endsWith(";") ? text : `${text};`);
|
|
4095
|
+
statements.push(text.code.endsWith(";") ? text : joinText`${text};`);
|
|
3835
4096
|
}
|
|
3836
4097
|
return {
|
|
3837
4098
|
element,
|
|
@@ -3935,7 +4196,7 @@ var Inliner = class {
|
|
|
3935
4196
|
const host = this.findHost(element);
|
|
3936
4197
|
if (!host) continue;
|
|
3937
4198
|
const name = this.freshName(`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`);
|
|
3938
|
-
this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
4199
|
+
this.addToHost(host, [joinText`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
3939
4200
|
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
3940
4201
|
this.transparent.set(element, {
|
|
3941
4202
|
provider,
|
|
@@ -3990,7 +4251,7 @@ var Inliner = class {
|
|
|
3990
4251
|
let names = taken.get(spread.element);
|
|
3991
4252
|
if (!names) {
|
|
3992
4253
|
names = /* @__PURE__ */ new Set();
|
|
3993
|
-
for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.name : getText(this.context, attribute.name));
|
|
4254
|
+
for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.name : getText(this.context, attribute.name).code);
|
|
3994
4255
|
taken.set(spread.element, names);
|
|
3995
4256
|
}
|
|
3996
4257
|
const keys = /* @__PURE__ */ new Map();
|
|
@@ -4016,8 +4277,8 @@ var Inliner = class {
|
|
|
4016
4277
|
*/
|
|
4017
4278
|
substituteViews(copy, component, element, views, values) {
|
|
4018
4279
|
for (const [member, text] of views.texts) {
|
|
4019
|
-
const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
4020
|
-
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
4280
|
+
const bare = getCode(text) !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
4281
|
+
copy.overwrite(member.start, member.end, bare ? text : joinText`(${text})`);
|
|
4021
4282
|
}
|
|
4022
4283
|
for (const { spread, keys } of views.spreads) {
|
|
4023
4284
|
const parts = [];
|
|
@@ -4025,16 +4286,16 @@ var Inliner = class {
|
|
|
4025
4286
|
for (const [key, text] of keys) {
|
|
4026
4287
|
const value = values.get(key);
|
|
4027
4288
|
if (key === "children") children = this.spreadChildren(element, text, value);
|
|
4028
|
-
else parts.push(`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
4289
|
+
else parts.push(joinText`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
4029
4290
|
}
|
|
4030
4291
|
const { attribute } = spread;
|
|
4031
4292
|
if (parts.length === 0) copy.remove(attribute.start, attribute.end);
|
|
4032
|
-
else copy.overwrite(attribute.start, attribute.end,
|
|
4293
|
+
else copy.overwrite(attribute.start, attribute.end, join(parts, " "));
|
|
4033
4294
|
if (children !== void 0) {
|
|
4034
4295
|
const opening = spread.element.openingElement;
|
|
4035
4296
|
const tag = copy.slice(opening.name.start, opening.name.end);
|
|
4036
4297
|
const close = this.context.code.lastIndexOf("/>", opening.end);
|
|
4037
|
-
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
4298
|
+
copy.overwrite(close, opening.end, joinText`>${children}</${tag}>`);
|
|
4038
4299
|
}
|
|
4039
4300
|
}
|
|
4040
4301
|
for (const declaration of component.viewDeclarations) copy.remove(declaration.start, declaration.end);
|
|
@@ -4044,10 +4305,10 @@ var Inliner = class {
|
|
|
4044
4305
|
* children, or the value of a single expression or default.
|
|
4045
4306
|
*/
|
|
4046
4307
|
spreadChildren(element, text, value) {
|
|
4047
|
-
if (text !== void 0) return text === "void 0" ? void 0 : `{${text}}`;
|
|
4308
|
+
if (text !== void 0) return getCode(text) === "void 0" ? void 0 : joinText`{${text}}`;
|
|
4048
4309
|
if (!value) return;
|
|
4049
4310
|
if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
4050
|
-
return `{${value.text}}`;
|
|
4311
|
+
return joinText`{${value.text}}`;
|
|
4051
4312
|
}
|
|
4052
4313
|
renameLocals(copy, component, fresh) {
|
|
4053
4314
|
for (const local of component.locals) {
|
|
@@ -4077,7 +4338,7 @@ var Inliner = class {
|
|
|
4077
4338
|
continue;
|
|
4078
4339
|
}
|
|
4079
4340
|
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
4080
|
-
copy.overwrite(read.member.start, read.member.end, bare ? value.text : `(${value.text})`);
|
|
4341
|
+
copy.overwrite(read.member.start, read.member.end, bare ? value.text : joinText`(${value.text})`);
|
|
4081
4342
|
}
|
|
4082
4343
|
for (const [parent, pieces] of spliced) {
|
|
4083
4344
|
const children = [];
|
|
@@ -4228,7 +4489,7 @@ var Inliner = class {
|
|
|
4228
4489
|
const texts = [];
|
|
4229
4490
|
for (const component of components) {
|
|
4230
4491
|
const statement = this.topLevelStatement(component.binding.declaration);
|
|
4231
|
-
if (statement) texts.push(getText(this.context, statement));
|
|
4492
|
+
if (statement) texts.push(getText(this.context, statement).code);
|
|
4232
4493
|
}
|
|
4233
4494
|
return texts.join("\n");
|
|
4234
4495
|
}
|
|
@@ -4500,7 +4761,7 @@ var Inliner = class {
|
|
|
4500
4761
|
const tag = element.openingElement.name;
|
|
4501
4762
|
if (tag.type === "JSXMemberExpression") return {
|
|
4502
4763
|
reason: "member-tag",
|
|
4503
|
-
name: getText(this.context, tag)
|
|
4764
|
+
name: getText(this.context, tag).code
|
|
4504
4765
|
};
|
|
4505
4766
|
if (tag.type !== "JSXIdentifier" || !isCapitalized(tag.name)) return;
|
|
4506
4767
|
const { name } = tag;
|
|
@@ -4536,11 +4797,11 @@ var Inliner = class {
|
|
|
4536
4797
|
writeHosts() {
|
|
4537
4798
|
for (const { host, texts } of this.hosts.values()) if (host.kind === "return") {
|
|
4538
4799
|
const indent = this.indentationAt(host.statement.start);
|
|
4539
|
-
|
|
4540
|
-
for (const text of texts) written
|
|
4541
|
-
this.context.s.prependRight(host.statement.start, written);
|
|
4800
|
+
const written = [];
|
|
4801
|
+
for (const text of texts) written.push(text, `\n${indent}`);
|
|
4802
|
+
this.context.s.prependRight(host.statement.start, join(written));
|
|
4542
4803
|
} else {
|
|
4543
|
-
this.context.s.prependRight(host.body.start, `{ ${
|
|
4804
|
+
this.context.s.prependRight(host.body.start, joinText`{ ${join(texts, " ")} return `);
|
|
4544
4805
|
this.context.s.appendLeft(host.body.end, "; }");
|
|
4545
4806
|
}
|
|
4546
4807
|
}
|
|
@@ -4805,7 +5066,7 @@ var MemoInliner = class {
|
|
|
4805
5066
|
for (const call of calls) {
|
|
4806
5067
|
const parent = this.context.parents.get(call);
|
|
4807
5068
|
const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
|
|
4808
|
-
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
5069
|
+
this.context.s.overwrite(call.start, call.end, bare ? text : joinText`(${text})`);
|
|
4809
5070
|
this.edited.push([call.start, call.end]);
|
|
4810
5071
|
}
|
|
4811
5072
|
this.context.s.remove(statement.start, statement.end);
|
|
@@ -5028,14 +5289,14 @@ var MemoInliner = class {
|
|
|
5028
5289
|
if (calls.length === 0) return;
|
|
5029
5290
|
for (const [start, end] of this.edited) if (start < statement.end && end > statement.start) return;
|
|
5030
5291
|
const text = getText(this.context, body);
|
|
5031
|
-
const length = compactLength(text);
|
|
5292
|
+
const length = compactLength(text.code);
|
|
5032
5293
|
if ((calls.length - 1) * length > calls.length * 3 + 13) return;
|
|
5033
5294
|
if (usesFunctionContext(this.context, fn)) return;
|
|
5034
5295
|
for (const call of calls) if (!this.resolvesSameAt(body, call, binding)) return;
|
|
5035
5296
|
const bare = isPrimary(body);
|
|
5036
5297
|
for (const call of calls) {
|
|
5037
5298
|
const inContainer = this.context.parents.get(call)?.type === "JSXExpressionContainer" && body.type !== "SequenceExpression";
|
|
5038
|
-
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
|
|
5299
|
+
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : joinText`(${text})`);
|
|
5039
5300
|
this.edited.push([call.start, call.end]);
|
|
5040
5301
|
}
|
|
5041
5302
|
this.context.s.remove(statement.start, statement.end);
|
|
@@ -5305,7 +5566,7 @@ function lowerConditionals(context) {
|
|
|
5305
5566
|
const ternary = ternaryOf(context, node);
|
|
5306
5567
|
const form = ternary === void 0 ? void 0 : formAt(context, node);
|
|
5307
5568
|
if (ternary !== void 0 && form !== void 0) {
|
|
5308
|
-
context.s.overwrite(node.start, node.end, form === "container" ? `{${ternary}}` : `<>{${ternary}}</>`);
|
|
5569
|
+
context.s.overwrite(node.start, node.end, form === "container" ? joinText`{${ternary}}` : joinText`<>{${ternary}}</>`);
|
|
5309
5570
|
changed = true;
|
|
5310
5571
|
return;
|
|
5311
5572
|
}
|
|
@@ -5334,7 +5595,7 @@ function ternaryOf(context, element) {
|
|
|
5334
5595
|
case "Show": {
|
|
5335
5596
|
const branch = branchOf(context, element, ["when", "fallback"]);
|
|
5336
5597
|
if (branch === void 0) return;
|
|
5337
|
-
return `${branch.when} ? ${branch.children} : ${branch.fallback ?? "undefined"}`;
|
|
5598
|
+
return joinText`${branch.when} ? ${branch.children} : ${branch.fallback ?? "undefined"}`;
|
|
5338
5599
|
}
|
|
5339
5600
|
case "Switch": return switchTernary(context, element);
|
|
5340
5601
|
default: return;
|
|
@@ -5346,17 +5607,17 @@ function ternaryOf(context, element) {
|
|
|
5346
5607
|
function switchTernary(context, element) {
|
|
5347
5608
|
const props = propsOf(context, element, ["fallback"]);
|
|
5348
5609
|
if (props === void 0) return;
|
|
5349
|
-
|
|
5610
|
+
const chain = [];
|
|
5350
5611
|
let count = 0;
|
|
5351
5612
|
for (const child of significantChildren(element.children)) {
|
|
5352
5613
|
if (child.type !== "JSXElement" || builtInOf(context, child) !== "Match") return;
|
|
5353
5614
|
const branch = branchOf(context, child, ["when"]);
|
|
5354
5615
|
if (branch === void 0) return;
|
|
5355
|
-
chain
|
|
5616
|
+
chain.push(branch.when, " ? ", branch.children, " : ");
|
|
5356
5617
|
count += 1;
|
|
5357
5618
|
}
|
|
5358
5619
|
if (count === 0) return;
|
|
5359
|
-
return
|
|
5620
|
+
return join([...chain, props.get("fallback") ?? "undefined"]);
|
|
5360
5621
|
}
|
|
5361
5622
|
/**
|
|
5362
5623
|
* How a `<Show>` or `<Switch>` at this position is written once lowered, or `undefined`
|
|
@@ -5433,7 +5694,7 @@ function branchOf(context, element, names) {
|
|
|
5433
5694
|
const inner = closingElement ? context.s.slice(element.openingElement.end, closingElement.start) : "";
|
|
5434
5695
|
return {
|
|
5435
5696
|
when,
|
|
5436
|
-
children: significant.length > 0 ? `<>${inner}</>` : "undefined",
|
|
5697
|
+
children: significant.length > 0 ? joinText`<>${inner}</>` : "undefined",
|
|
5437
5698
|
fallback: props.get("fallback")
|
|
5438
5699
|
};
|
|
5439
5700
|
}
|
|
@@ -5449,7 +5710,7 @@ function callbackBody(context, element, callback) {
|
|
|
5449
5710
|
const value = findAttribute(element, "when")?.value;
|
|
5450
5711
|
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression" || !isPlainRead(context, value.expression)) return;
|
|
5451
5712
|
const when = value.expression;
|
|
5452
|
-
const whenText = `(${context.s.slice(when.start, when.end)})`;
|
|
5713
|
+
const whenText = joinText`(${context.s.slice(when.start, when.end)})`;
|
|
5453
5714
|
const names = [];
|
|
5454
5715
|
collectReferences(context, when, names);
|
|
5455
5716
|
const binding = lookup(scopeAt(context.scopes, context.parents, param), param.name);
|
|
@@ -5469,17 +5730,18 @@ function callbackBody(context, element, callback) {
|
|
|
5469
5730
|
else edits.push({
|
|
5470
5731
|
start: reference.start,
|
|
5471
5732
|
end: reference.end,
|
|
5472
|
-
text: `(() => ${whenText})`
|
|
5733
|
+
text: joinText`(() => ${whenText})`
|
|
5473
5734
|
});
|
|
5474
5735
|
}
|
|
5475
5736
|
edits.sort((a, b) => a.start - b.start);
|
|
5476
|
-
|
|
5737
|
+
const parts = [];
|
|
5477
5738
|
let at = body.start;
|
|
5478
5739
|
for (const edit of edits) {
|
|
5479
|
-
|
|
5740
|
+
parts.push(context.s.slice(at, edit.start), edit.text);
|
|
5480
5741
|
at = edit.end;
|
|
5481
5742
|
}
|
|
5482
|
-
|
|
5743
|
+
parts.push(context.s.slice(at, body.end));
|
|
5744
|
+
return join(parts);
|
|
5483
5745
|
}
|
|
5484
5746
|
/**
|
|
5485
5747
|
* Whether reading an expression again gives the same value, with nothing run
|
|
@@ -5578,7 +5840,7 @@ function expressionText(context, attribute) {
|
|
|
5578
5840
|
switch (value.type) {
|
|
5579
5841
|
case "JSXExpressionContainer":
|
|
5580
5842
|
if (value.expression.type === "JSXEmptyExpression") return;
|
|
5581
|
-
return `(${context.s.slice(value.expression.start, value.expression.end)})`;
|
|
5843
|
+
return joinText`(${context.s.slice(value.expression.start, value.expression.end)})`;
|
|
5582
5844
|
case "JSXElement":
|
|
5583
5845
|
case "JSXFragment": return context.s.slice(value.start, value.end);
|
|
5584
5846
|
default: return;
|
|
@@ -5681,7 +5943,7 @@ function runPass(parsed, filename, options, sourceMap, pass, skips) {
|
|
|
5681
5943
|
const context = {
|
|
5682
5944
|
code,
|
|
5683
5945
|
filename,
|
|
5684
|
-
s: new
|
|
5946
|
+
s: new Editor(code, sourceMap),
|
|
5685
5947
|
program: parsed.program,
|
|
5686
5948
|
parents: parsed.parents,
|
|
5687
5949
|
scopes: {
|
|
@@ -5694,11 +5956,7 @@ function runPass(parsed, filename, options, sourceMap, pass, skips) {
|
|
|
5694
5956
|
if (!pass(context)) return;
|
|
5695
5957
|
return {
|
|
5696
5958
|
code: context.s.toString(),
|
|
5697
|
-
map: sourceMap ? context.s.generateMap(
|
|
5698
|
-
source: filename,
|
|
5699
|
-
hires: true,
|
|
5700
|
-
includeContent: true
|
|
5701
|
-
}).toString() : void 0
|
|
5959
|
+
map: sourceMap ? context.s.generateMap(filename) : void 0
|
|
5702
5960
|
};
|
|
5703
5961
|
}
|
|
5704
5962
|
/**
|
|
@@ -5796,4 +6054,4 @@ function readModuleConstants(code, options = {}) {
|
|
|
5796
6054
|
//#endregion
|
|
5797
6055
|
export { analyzeScopes as a, collectParents as c, parse$1 as d, unwrap as f, runtimeImportSources as i, forEachChild as l, readModuleConstants as n, lookup as o, CONTEXT_SAFE_PRIMITIVES as r, scopeAt as s, compile as t, isIntrinsicTag as u };
|
|
5798
6056
|
|
|
5799
|
-
//# sourceMappingURL=compile-
|
|
6057
|
+
//# sourceMappingURL=compile-Cvjy2e11.mjs.map
|