solid-optimizer 1.0.0-next.0 → 1.0.0-next.10
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 +27 -13
- package/dist/{compile-Cz6hbiQv.mjs → compile-Cvjy2e11.mjs} +1925 -385
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -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 +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +8 -5
- package/src/ast.ts +4 -3
- package/src/compile.ts +96 -16
- package/src/conditional.ts +547 -0
- package/src/constants.ts +58 -13
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
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
|
+
import { parse, parseFragment, serialize } from "parse5";
|
|
4
5
|
//#region src/ast.ts
|
|
5
6
|
/**
|
|
6
7
|
* Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
|
|
@@ -65,9 +66,9 @@ function collectParents(program) {
|
|
|
65
66
|
* is several times faster to read. It is not in the options' type yet.
|
|
66
67
|
*/
|
|
67
68
|
const RAW_TRANSFER = rawTransferSupported();
|
|
68
|
-
function parse(filename, code) {
|
|
69
|
+
function parse$1(filename, code) {
|
|
69
70
|
const options = {
|
|
70
|
-
lang:
|
|
71
|
+
lang: getLanguage(filename),
|
|
71
72
|
sourceType: "module",
|
|
72
73
|
preserveParens: true,
|
|
73
74
|
experimentalRawTransfer: RAW_TRANSFER
|
|
@@ -77,7 +78,7 @@ function parse(filename, code) {
|
|
|
77
78
|
if (error) throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
|
|
78
79
|
return result.program;
|
|
79
80
|
}
|
|
80
|
-
function
|
|
81
|
+
function getLanguage(filename) {
|
|
81
82
|
if (/\.tsx$/i.test(filename)) return "tsx";
|
|
82
83
|
if (/\.[cm]?ts$/i.test(filename)) return "ts";
|
|
83
84
|
return "jsx";
|
|
@@ -87,7 +88,7 @@ function languageOf(filename) {
|
|
|
87
88
|
*/
|
|
88
89
|
function unwrap(node) {
|
|
89
90
|
let current = node;
|
|
90
|
-
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "TSTypeAssertion") current = current.expression;
|
|
91
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "TSTypeAssertion" || current.type === "TSInstantiationExpression") current = current.expression;
|
|
91
92
|
return current;
|
|
92
93
|
}
|
|
93
94
|
function isFunctionNode(node) {
|
|
@@ -221,7 +222,7 @@ function recordSkip(context, pass, reason, name) {
|
|
|
221
222
|
/**
|
|
222
223
|
* The current text of `node`, including the edits made inside it so far.
|
|
223
224
|
*/
|
|
224
|
-
function
|
|
225
|
+
function getText(context, node) {
|
|
225
226
|
return context.s.slice(node.start, node.end);
|
|
226
227
|
}
|
|
227
228
|
//#endregion
|
|
@@ -257,6 +258,30 @@ function isWithinScope(scope, ancestor) {
|
|
|
257
258
|
}
|
|
258
259
|
return false;
|
|
259
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
|
+
}
|
|
260
285
|
function exportName$1(name) {
|
|
261
286
|
return name.type === "Literal" ? name.value : name.name;
|
|
262
287
|
}
|
|
@@ -265,6 +290,7 @@ var Analyzer = class {
|
|
|
265
290
|
this.scopes = /* @__PURE__ */ new Map();
|
|
266
291
|
this.names = /* @__PURE__ */ new Set();
|
|
267
292
|
this.pending = [];
|
|
293
|
+
this.bodies = /* @__PURE__ */ new Set();
|
|
268
294
|
this.root = {
|
|
269
295
|
node: program,
|
|
270
296
|
parent: void 0,
|
|
@@ -302,7 +328,10 @@ var Analyzer = class {
|
|
|
302
328
|
}
|
|
303
329
|
declare(identifier, kind, declaration, source, imported) {
|
|
304
330
|
let target = this.scope;
|
|
305
|
-
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
|
+
}
|
|
306
335
|
this.names.add(identifier.name);
|
|
307
336
|
const existing = target.bindings.get(identifier.name);
|
|
308
337
|
if (existing) {
|
|
@@ -398,10 +427,23 @@ var Analyzer = class {
|
|
|
398
427
|
this.withScope(node, true, () => {
|
|
399
428
|
if (node.type === "FunctionExpression" && node.id) this.declare(node.id, "other", node);
|
|
400
429
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
401
|
-
if (node.body)
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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);
|
|
405
447
|
});
|
|
406
448
|
}
|
|
407
449
|
visitClass(node) {
|
|
@@ -457,10 +499,8 @@ var Analyzer = class {
|
|
|
457
499
|
case "ArrowFunctionExpression":
|
|
458
500
|
this.withScope(node, true, () => {
|
|
459
501
|
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
460
|
-
if (node.body.type === "BlockStatement")
|
|
461
|
-
|
|
462
|
-
this.visitStatements(node.body.body);
|
|
463
|
-
} else this.visit(node.body);
|
|
502
|
+
if (node.body.type === "BlockStatement") this.visitBody(node.body, node.params);
|
|
503
|
+
else this.visit(node.body);
|
|
464
504
|
});
|
|
465
505
|
break;
|
|
466
506
|
case "ClassDeclaration":
|
|
@@ -561,13 +601,27 @@ var Analyzer = class {
|
|
|
561
601
|
for (const child of node.children) this.visit(child);
|
|
562
602
|
if (node.closingElement) this.visitJSXName(node.closingElement.name);
|
|
563
603
|
break;
|
|
564
|
-
case "TSEnumDeclaration":
|
|
565
604
|
case "TSImportEqualsDeclaration":
|
|
566
605
|
this.declare(node.id, "other", node);
|
|
567
606
|
break;
|
|
568
|
-
case "
|
|
569
|
-
|
|
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
|
+
});
|
|
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
|
+
});
|
|
570
623
|
break;
|
|
624
|
+
}
|
|
571
625
|
default: forEachChild(node, (child) => {
|
|
572
626
|
this.visit(child);
|
|
573
627
|
});
|
|
@@ -728,6 +782,10 @@ function truthiness(node, lookup) {
|
|
|
728
782
|
const value = evaluate(node, lookup);
|
|
729
783
|
return value ? !!value.value : void 0;
|
|
730
784
|
}
|
|
785
|
+
function areSideEffectFree(nodes) {
|
|
786
|
+
for (const node of nodes) if (!isSideEffectFree(node)) return false;
|
|
787
|
+
return true;
|
|
788
|
+
}
|
|
731
789
|
/**
|
|
732
790
|
* Whether skipping `node` entirely loses nothing observable.
|
|
733
791
|
*
|
|
@@ -743,14 +801,18 @@ function isSideEffectFree(node) {
|
|
|
743
801
|
case "ArrowFunctionExpression":
|
|
744
802
|
case "FunctionExpression": return true;
|
|
745
803
|
case "ParenthesizedExpression": return isSideEffectFree(node.expression);
|
|
746
|
-
case "TemplateLiteral": return node.expressions
|
|
804
|
+
case "TemplateLiteral": return areSideEffectFree(node.expressions);
|
|
747
805
|
case "UnaryExpression": return node.operator !== "delete" && isSideEffectFree(node.argument);
|
|
748
806
|
case "BinaryExpression": return node.operator !== "in" && node.operator !== "instanceof" && isSideEffectFree(node.left) && isSideEffectFree(node.right);
|
|
749
807
|
case "LogicalExpression": return isSideEffectFree(node.left) && isSideEffectFree(node.right);
|
|
750
|
-
case "SequenceExpression": return node.expressions
|
|
808
|
+
case "SequenceExpression": return areSideEffectFree(node.expressions);
|
|
751
809
|
case "ConditionalExpression": return isSideEffectFree(node.test) && isSideEffectFree(node.consequent) && isSideEffectFree(node.alternate);
|
|
752
|
-
case "ArrayExpression":
|
|
753
|
-
|
|
810
|
+
case "ArrayExpression":
|
|
811
|
+
for (const element of node.elements) if (element !== null && (element.type === "SpreadElement" || !isSideEffectFree(element))) return false;
|
|
812
|
+
return true;
|
|
813
|
+
case "ObjectExpression":
|
|
814
|
+
for (const property of node.properties) if (property.type !== "Property" || property.computed && !isSideEffectFree(property.key) || !isSideEffectFree(property.value)) return false;
|
|
815
|
+
return true;
|
|
754
816
|
default: return false;
|
|
755
817
|
}
|
|
756
818
|
}
|
|
@@ -771,7 +833,7 @@ function effectfulParts(node, parts = []) {
|
|
|
771
833
|
function arrayLiteralLength(node) {
|
|
772
834
|
if (node.type === "ParenthesizedExpression") return arrayLiteralLength(node.expression);
|
|
773
835
|
if (node.type !== "ArrayExpression") return;
|
|
774
|
-
|
|
836
|
+
for (const element of node.elements) if (element?.type === "SpreadElement") return;
|
|
775
837
|
return node.elements.length;
|
|
776
838
|
}
|
|
777
839
|
/**
|
|
@@ -806,7 +868,11 @@ function isLiteralForm(node) {
|
|
|
806
868
|
//#region src/constants.ts
|
|
807
869
|
function resolveImportedConstants(constants) {
|
|
808
870
|
const result = /* @__PURE__ */ new Map();
|
|
809
|
-
for (const [source, exports] of Object.entries(constants ?? {}))
|
|
871
|
+
for (const [source, exports] of Object.entries(constants ?? {})) {
|
|
872
|
+
const entries = /* @__PURE__ */ new Map();
|
|
873
|
+
for (const [name, value] of Object.entries(exports)) entries.set(name, { value });
|
|
874
|
+
result.set(source, entries);
|
|
875
|
+
}
|
|
810
876
|
return result;
|
|
811
877
|
}
|
|
812
878
|
function globalValue(name) {
|
|
@@ -829,7 +895,7 @@ var ConstantTable = class {
|
|
|
829
895
|
if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference) ?? this.globalMember(scopes, reference);
|
|
830
896
|
if (!scopes.references.has(reference) || reference.type !== "Identifier") return;
|
|
831
897
|
const binding = scopes.references.get(reference);
|
|
832
|
-
if (binding) return this.
|
|
898
|
+
if (binding) return this.getConstant(binding);
|
|
833
899
|
return this.globalConstant(reference.name) ?? globalValue(reference.name);
|
|
834
900
|
};
|
|
835
901
|
}
|
|
@@ -877,7 +943,7 @@ var ConstantTable = class {
|
|
|
877
943
|
* A reference that runs before its declaration throws at runtime, and
|
|
878
944
|
* folding turns it into the value. Minifiers make the same trade.
|
|
879
945
|
*/
|
|
880
|
-
|
|
946
|
+
getConstant(binding) {
|
|
881
947
|
const cached = this.constants.get(binding);
|
|
882
948
|
if (cached !== void 0) return cached ?? void 0;
|
|
883
949
|
let value;
|
|
@@ -896,16 +962,42 @@ var ConstantTable = class {
|
|
|
896
962
|
/**
|
|
897
963
|
* The modules `program` imports or re-exports bindings from. Their
|
|
898
964
|
* constant exports are what `importedConstants` can hold.
|
|
965
|
+
*
|
|
966
|
+
* An import counts only when one of its bindings is read as a value. A
|
|
967
|
+
* binding read only in types has no constant to fold, and TypeScript drops its
|
|
968
|
+
* import, so the module it names may not even build for this target.
|
|
899
969
|
*/
|
|
900
|
-
function namedImportSources(program) {
|
|
970
|
+
function namedImportSources(program, scopes = analyzeScopes(program)) {
|
|
901
971
|
const sources = /* @__PURE__ */ new Set();
|
|
902
972
|
for (const statement of program.body) if (statement.type === "ImportDeclaration") {
|
|
903
|
-
if (statement.importKind !== "type" && statement.specifiers
|
|
973
|
+
if (statement.importKind !== "type" && readsAsValue(statement.specifiers, scopes)) sources.add(statement.source.value);
|
|
904
974
|
} else if (statement.type === "ExportNamedDeclaration" && statement.source || statement.type === "ExportAllDeclaration" && !statement.exported) {
|
|
905
975
|
if (statement.exportKind !== "type" && statement.source) sources.add(statement.source.value);
|
|
906
976
|
}
|
|
907
977
|
return [...sources];
|
|
908
978
|
}
|
|
979
|
+
/**
|
|
980
|
+
* The modules a module still imports once its types are stripped: the
|
|
981
|
+
* sources of `namedImportSources`, and imports for side effects only, like
|
|
982
|
+
* `import './setup'`.
|
|
983
|
+
*/
|
|
984
|
+
function runtimeImportSources(program, scopes = analyzeScopes(program)) {
|
|
985
|
+
const sources = new Set(namedImportSources(program, scopes));
|
|
986
|
+
for (const statement of program.body) if (statement.type === "ImportDeclaration" && statement.importKind !== "type" && statement.specifiers.length === 0) sources.add(statement.source.value);
|
|
987
|
+
return [...sources];
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Whether the module reads one of these import bindings as a value. Scope
|
|
991
|
+
* analysis skips type positions, so a binding used only in types has no
|
|
992
|
+
* references.
|
|
993
|
+
*/
|
|
994
|
+
function readsAsValue(specifiers, scopes) {
|
|
995
|
+
for (const specifier of specifiers) {
|
|
996
|
+
if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") continue;
|
|
997
|
+
if (scopes.root.bindings.get(specifier.local.name)?.references.length) return true;
|
|
998
|
+
}
|
|
999
|
+
return false;
|
|
1000
|
+
}
|
|
909
1001
|
function exportName(node) {
|
|
910
1002
|
if (node.type === "Identifier") return node.name;
|
|
911
1003
|
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
@@ -940,7 +1032,7 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
|
|
|
940
1032
|
if (declaration?.type === "VariableDeclaration") {
|
|
941
1033
|
for (const declarator of declaration.declarations) if (declarator.id.type === "Identifier") {
|
|
942
1034
|
const binding = topLevel.bindings.get(declarator.id.name);
|
|
943
|
-
add(declarator.id.name, binding ? table.
|
|
1035
|
+
add(declarator.id.name, binding ? table.getConstant(binding) : void 0);
|
|
944
1036
|
}
|
|
945
1037
|
continue;
|
|
946
1038
|
}
|
|
@@ -952,7 +1044,7 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
|
|
|
952
1044
|
if (statement.source) add(exported, options.importedConstants.get(statement.source.value)?.get(local));
|
|
953
1045
|
else {
|
|
954
1046
|
const binding = topLevel.bindings.get(local);
|
|
955
|
-
add(exported, binding ? table.
|
|
1047
|
+
add(exported, binding ? table.getConstant(binding) : void 0);
|
|
956
1048
|
}
|
|
957
1049
|
}
|
|
958
1050
|
}
|
|
@@ -963,9 +1055,241 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
|
|
|
963
1055
|
return result;
|
|
964
1056
|
}
|
|
965
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
|
|
966
1290
|
//#region src/jsx.ts
|
|
967
1291
|
function keptPiece(node) {
|
|
968
|
-
const text = node.type === "JSXText" ? node : void 0;
|
|
1292
|
+
const text = node.type === "JSXText" ? node.value : void 0;
|
|
969
1293
|
return {
|
|
970
1294
|
node,
|
|
971
1295
|
text: void 0,
|
|
@@ -980,39 +1304,52 @@ function splicePiece(s, node, children) {
|
|
|
980
1304
|
const first = children.at(0);
|
|
981
1305
|
const last = children.at(-1);
|
|
982
1306
|
if (!first || !last) return replacedPiece(node, "");
|
|
983
|
-
return
|
|
984
|
-
node,
|
|
985
|
-
text: s.slice(first.start, last.end),
|
|
986
|
-
startText: first.type === "JSXText" ? first : void 0,
|
|
987
|
-
endText: last.type === "JSXText" ? last : void 0
|
|
988
|
-
};
|
|
1307
|
+
return replacedPiece(node, s.slice(first.start, last.end));
|
|
989
1308
|
}
|
|
1309
|
+
/**
|
|
1310
|
+
* A piece that writes `text`, JSX children as source, in place of `node`.
|
|
1311
|
+
*
|
|
1312
|
+
* The texts it starts and ends with are read from `text` itself, not from
|
|
1313
|
+
* the children it came from. A fold inside them in the same round can have
|
|
1314
|
+
* turned a child that was an element into text, such as an inner `<Show>`.
|
|
1315
|
+
* JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
|
|
1316
|
+
* mark where the text ends.
|
|
1317
|
+
*/
|
|
990
1318
|
function replacedPiece(node, text) {
|
|
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);
|
|
991
1324
|
return {
|
|
992
1325
|
node,
|
|
993
1326
|
text,
|
|
994
|
-
startText: void 0,
|
|
995
|
-
endText: void 0
|
|
1327
|
+
startText: startText === "" ? void 0 : startText,
|
|
1328
|
+
endText: endText === "" ? void 0 : endText
|
|
996
1329
|
};
|
|
997
1330
|
}
|
|
1331
|
+
/** Whether JSX drops this text: whitespace that contains a line break. */
|
|
1332
|
+
function isDroppedText(text) {
|
|
1333
|
+
return /^\s*$/.test(text) && /[\r\n]/.test(text);
|
|
1334
|
+
}
|
|
998
1335
|
/**
|
|
999
1336
|
* Whether two texts render differently as one text node than apart.
|
|
1000
1337
|
* Two texts that JSX drops, like line breaks and indentation, still drop together.
|
|
1001
1338
|
*/
|
|
1002
1339
|
function mergeChangesText(before, after) {
|
|
1003
|
-
return before !== void 0 && after !== void 0 && !(
|
|
1340
|
+
return before !== void 0 && after !== void 0 && !(isDroppedText(before) && isDroppedText(after));
|
|
1004
1341
|
}
|
|
1005
1342
|
/**
|
|
1006
1343
|
* Puts `{}` between two pieces that meet as text, on whichever side is replaced.
|
|
1007
1344
|
*/
|
|
1008
1345
|
function separate(previous, next, emptiesBetween) {
|
|
1009
1346
|
if (next.text !== void 0) {
|
|
1010
|
-
next.text = `{}${next.text}`;
|
|
1347
|
+
next.text = joinText`{}${next.text}`;
|
|
1011
1348
|
next.startText = void 0;
|
|
1012
1349
|
return;
|
|
1013
1350
|
}
|
|
1014
1351
|
if (previous.text !== void 0) {
|
|
1015
|
-
previous.text = `${previous.text}{}`;
|
|
1352
|
+
previous.text = joinText`${previous.text}{}`;
|
|
1016
1353
|
previous.endText = void 0;
|
|
1017
1354
|
return;
|
|
1018
1355
|
}
|
|
@@ -1028,7 +1365,7 @@ function writePieces(s, pieces) {
|
|
|
1028
1365
|
let previous;
|
|
1029
1366
|
let emptiesSincePrevious = [];
|
|
1030
1367
|
for (const piece of pieces) {
|
|
1031
|
-
if (piece.text === "") {
|
|
1368
|
+
if (piece.text !== void 0 && getCode(piece.text) === "") {
|
|
1032
1369
|
emptiesSincePrevious.push(piece);
|
|
1033
1370
|
continue;
|
|
1034
1371
|
}
|
|
@@ -1039,7 +1376,7 @@ function writePieces(s, pieces) {
|
|
|
1039
1376
|
for (const piece of pieces) {
|
|
1040
1377
|
if (piece.text === void 0) continue;
|
|
1041
1378
|
changed = true;
|
|
1042
|
-
if (piece.text === "") s.remove(piece.node.start, piece.node.end);
|
|
1379
|
+
if (getCode(piece.text) === "") s.remove(piece.node.start, piece.node.end);
|
|
1043
1380
|
else s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
1044
1381
|
}
|
|
1045
1382
|
return changed;
|
|
@@ -1048,19 +1385,21 @@ function findAttribute(element, name) {
|
|
|
1048
1385
|
for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === name) return attribute;
|
|
1049
1386
|
}
|
|
1050
1387
|
function hasSpreadAttribute(element) {
|
|
1051
|
-
|
|
1388
|
+
for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXSpreadAttribute") return true;
|
|
1389
|
+
return false;
|
|
1052
1390
|
}
|
|
1053
1391
|
/**
|
|
1054
1392
|
* Whether any child is a function, which the runtime may call with the
|
|
1055
1393
|
* narrowed value. Folding one away would drop that call.
|
|
1056
1394
|
*/
|
|
1057
1395
|
function hasCallbackChild(children) {
|
|
1058
|
-
|
|
1059
|
-
if (child.type !== "JSXExpressionContainer")
|
|
1396
|
+
for (const child of children) {
|
|
1397
|
+
if (child.type !== "JSXExpressionContainer") continue;
|
|
1060
1398
|
let expression = child.expression;
|
|
1061
1399
|
while (expression.type === "ParenthesizedExpression") expression = expression.expression;
|
|
1062
|
-
|
|
1063
|
-
}
|
|
1400
|
+
if (expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression") return true;
|
|
1401
|
+
}
|
|
1402
|
+
return false;
|
|
1064
1403
|
}
|
|
1065
1404
|
/**
|
|
1066
1405
|
* The text between an element's tags, or an empty string for a self-closing element.
|
|
@@ -1239,7 +1578,8 @@ var SizeEstimator = class {
|
|
|
1239
1578
|
}
|
|
1240
1579
|
} else size += name.length + GETTER + this.jsx(value, false);
|
|
1241
1580
|
}
|
|
1242
|
-
const significant =
|
|
1581
|
+
const significant = [];
|
|
1582
|
+
for (const child of element.children) if (!isInsignificant(child)) significant.push(child);
|
|
1243
1583
|
if (significant.length > 0) size += 24 + this.children(significant);
|
|
1244
1584
|
return size + (spread ? MERGE : 0);
|
|
1245
1585
|
}
|
|
@@ -1270,6 +1610,18 @@ const EMPTY = { kind: "empty" };
|
|
|
1270
1610
|
/** Solid's calls that only create a value, so the bundler can drop an unused one. */
|
|
1271
1611
|
const PURE_CALLS = /* @__PURE__ */ new Set(["lazy", "createContext"]);
|
|
1272
1612
|
/**
|
|
1613
|
+
* Solid's calls that only create a value when their first argument is not a
|
|
1614
|
+
* function. Given a function, they create a computation, which runs it.
|
|
1615
|
+
*/
|
|
1616
|
+
const PURE_VALUE_CALLS = /* @__PURE__ */ new Set(["createSignal", "createStore"]);
|
|
1617
|
+
/** Expression types that are never a function. */
|
|
1618
|
+
const NOT_FUNCTION = /* @__PURE__ */ new Set([
|
|
1619
|
+
"Literal",
|
|
1620
|
+
"TemplateLiteral",
|
|
1621
|
+
"ObjectExpression",
|
|
1622
|
+
"ArrayExpression"
|
|
1623
|
+
]);
|
|
1624
|
+
/**
|
|
1273
1625
|
* The globals worth folding. They only apply to a reference that binds to
|
|
1274
1626
|
* nothing, which is when the global is what runs.
|
|
1275
1627
|
*/
|
|
@@ -1319,6 +1671,29 @@ function containsHoistedDeclaration(statement) {
|
|
|
1319
1671
|
visit(statement);
|
|
1320
1672
|
return found;
|
|
1321
1673
|
}
|
|
1674
|
+
/** The source text of each node. */
|
|
1675
|
+
function getTexts(context, nodes) {
|
|
1676
|
+
const texts = [];
|
|
1677
|
+
for (const node of nodes) texts.push(getText(context, node));
|
|
1678
|
+
return texts;
|
|
1679
|
+
}
|
|
1680
|
+
/** The children that are not whitespace-only text or an empty expression. */
|
|
1681
|
+
function getSignificant(children) {
|
|
1682
|
+
const significant = [];
|
|
1683
|
+
for (const child of children) if (!isInsignificant(child)) significant.push(child);
|
|
1684
|
+
return significant;
|
|
1685
|
+
}
|
|
1686
|
+
/** Whether the element has a `keyed` attribute that is not `{false}`. */
|
|
1687
|
+
function hasEnabledKeyed(element) {
|
|
1688
|
+
for (const attribute of element.openingElement.attributes) if (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 true;
|
|
1689
|
+
return false;
|
|
1690
|
+
}
|
|
1691
|
+
/** The summed size estimate of the children of a fragment spliced into a template. */
|
|
1692
|
+
function estimateChildren(estimator, children) {
|
|
1693
|
+
let size = 0;
|
|
1694
|
+
for (const child of children) size += child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child);
|
|
1695
|
+
return size;
|
|
1696
|
+
}
|
|
1322
1697
|
/**
|
|
1323
1698
|
* Whether a statement starting with `text` could join the statement before
|
|
1324
1699
|
* it when that one has no semicolon.
|
|
@@ -1331,6 +1706,12 @@ function keyName(node) {
|
|
|
1331
1706
|
if (node.type === "Identifier") return node.name;
|
|
1332
1707
|
return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
1333
1708
|
}
|
|
1709
|
+
/** Whether a call has no first argument, or one that is never a function. */
|
|
1710
|
+
function isValueArgument(call) {
|
|
1711
|
+
if (call.arguments.length === 0) return true;
|
|
1712
|
+
const first = call.arguments[0];
|
|
1713
|
+
return first.type !== "SpreadElement" && NOT_FUNCTION.has(unwrap(first).type);
|
|
1714
|
+
}
|
|
1334
1715
|
/**
|
|
1335
1716
|
* The text of an expression that always gives the same primitive: a literal
|
|
1336
1717
|
* other than a regular expression, a template literal without expressions,
|
|
@@ -1340,9 +1721,9 @@ function primitiveText(context, node) {
|
|
|
1340
1721
|
let expression = node;
|
|
1341
1722
|
while (expression.type === "ParenthesizedExpression") expression = expression.expression;
|
|
1342
1723
|
switch (expression.type) {
|
|
1343
|
-
case "Literal": return "regex" in expression ? void 0 :
|
|
1344
|
-
case "TemplateLiteral": return expression.expressions.length === 0 ?
|
|
1345
|
-
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ?
|
|
1724
|
+
case "Literal": return "regex" in expression ? void 0 : getText(context, expression);
|
|
1725
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? getText(context, expression) : void 0;
|
|
1726
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? getText(context, expression) : void 0;
|
|
1346
1727
|
default: return;
|
|
1347
1728
|
}
|
|
1348
1729
|
}
|
|
@@ -1372,9 +1753,11 @@ var Folder = class {
|
|
|
1372
1753
|
constructor(context) {
|
|
1373
1754
|
this.context = context;
|
|
1374
1755
|
this.changed = false;
|
|
1756
|
+
this.removed = /* @__PURE__ */ new Set();
|
|
1375
1757
|
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
1376
1758
|
}
|
|
1377
1759
|
walk(node) {
|
|
1760
|
+
if (this.removed.has(node)) return;
|
|
1378
1761
|
forEachChild(node, (child) => {
|
|
1379
1762
|
this.walk(child);
|
|
1380
1763
|
});
|
|
@@ -1417,14 +1800,16 @@ var Folder = class {
|
|
|
1417
1800
|
case "MemberExpression":
|
|
1418
1801
|
if (!isWriteTarget(node, this.context.parents)) this.foldConstant(node);
|
|
1419
1802
|
break;
|
|
1420
|
-
case "CallExpression":
|
|
1803
|
+
case "CallExpression":
|
|
1804
|
+
this.annotatePure(node);
|
|
1805
|
+
this.foldUntrack(node);
|
|
1421
1806
|
}
|
|
1422
1807
|
}
|
|
1423
1808
|
isJSXChildOrAttribute(node) {
|
|
1424
1809
|
return isJSXChild(node, this.context.parents) || this.context.parents.get(node)?.type === "JSXAttribute";
|
|
1425
1810
|
}
|
|
1426
1811
|
replace(node, text) {
|
|
1427
|
-
if (
|
|
1812
|
+
if (getText(this.context, node).code === getCode(text)) return;
|
|
1428
1813
|
this.context.s.overwrite(node.start, node.end, text);
|
|
1429
1814
|
this.changed = true;
|
|
1430
1815
|
}
|
|
@@ -1434,20 +1819,39 @@ var Folder = class {
|
|
|
1434
1819
|
* branch can leave it unused, and the annotation lets the bundler drop it
|
|
1435
1820
|
* along with its dynamic import. `createContext` only creates the context,
|
|
1436
1821
|
* which inlining can leave unused.
|
|
1822
|
+
*
|
|
1823
|
+
* `createSignal` and `createStore` with a first argument that is not a
|
|
1824
|
+
* function only create a node. It takes no owner and no hydration id, so
|
|
1825
|
+
* dropping an unused one changes nothing else.
|
|
1437
1826
|
*/
|
|
1438
1827
|
annotatePure(call) {
|
|
1439
1828
|
if (call.callee.type !== "Identifier") return;
|
|
1440
1829
|
const binding = this.context.scopes.references.get(call.callee);
|
|
1441
1830
|
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
1442
|
-
if (identity === void 0
|
|
1831
|
+
if (identity === void 0) return;
|
|
1832
|
+
if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return;
|
|
1443
1833
|
const before = this.context.code.slice(0, call.start).trimEnd();
|
|
1444
1834
|
if (/(#|@)__PURE__\s*\*\/$/.test(before)) return;
|
|
1445
1835
|
this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
|
|
1446
1836
|
this.changed = true;
|
|
1447
1837
|
}
|
|
1448
|
-
|
|
1838
|
+
/**
|
|
1839
|
+
* `untrack(() => value)` is the value, when the value is a constant.
|
|
1840
|
+
* Nothing is read, so there is nothing to untrack. A component inlined
|
|
1841
|
+
* with a literal prop leaves this, like Kobalte's `untrack(() => props.as)`.
|
|
1842
|
+
*/
|
|
1843
|
+
foldUntrack(call) {
|
|
1844
|
+
if (call.callee.type !== "Identifier" || call.arguments.length !== 1) return;
|
|
1845
|
+
const binding = this.context.scopes.references.get(call.callee);
|
|
1846
|
+
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
1847
|
+
const callback = call.arguments.at(0);
|
|
1848
|
+
if (identity !== "untrack" || callback?.type !== "ArrowFunctionExpression" || callback.async || callback.params.length > 0 || !callback.expression || callback.body.type === "BlockStatement") return;
|
|
1849
|
+
const value = evaluate(callback.body, this.lookup);
|
|
1850
|
+
if (value) this.foldConstant(call, value);
|
|
1851
|
+
}
|
|
1852
|
+
foldConstant(node, known) {
|
|
1449
1853
|
if (isLiteralForm(node)) return;
|
|
1450
|
-
const value = evaluate(node, this.lookup);
|
|
1854
|
+
const value = known ?? evaluate(node, this.lookup);
|
|
1451
1855
|
if (!value) return;
|
|
1452
1856
|
let text = literalText(value.value);
|
|
1453
1857
|
if (text === void 0) return;
|
|
@@ -1470,12 +1874,12 @@ var Folder = class {
|
|
|
1470
1874
|
keepAfter(target, discarded, value) {
|
|
1471
1875
|
const parts = discarded ? effectfulParts(discarded) : [];
|
|
1472
1876
|
if (parts.length === 0) {
|
|
1473
|
-
const text =
|
|
1474
|
-
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
1877
|
+
const text = getText(this.context, value);
|
|
1878
|
+
return needsParensAt(target, value, this.context.parents) ? joinText`(${text})` : text;
|
|
1475
1879
|
}
|
|
1476
|
-
const texts =
|
|
1477
|
-
texts.push(
|
|
1478
|
-
return `(${
|
|
1880
|
+
const texts = getTexts(this.context, parts);
|
|
1881
|
+
texts.push(getText(this.context, value));
|
|
1882
|
+
return joinText`(${join(texts, ", ")})`;
|
|
1479
1883
|
}
|
|
1480
1884
|
foldLogical(node) {
|
|
1481
1885
|
const left = truthiness(node.left, this.lookup);
|
|
@@ -1592,27 +1996,38 @@ var Folder = class {
|
|
|
1592
1996
|
if (value.value.includes("&")) return;
|
|
1593
1997
|
return {
|
|
1594
1998
|
kind: "expression",
|
|
1595
|
-
text: JSON.stringify(value.value),
|
|
1999
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
1596
2000
|
primary: true
|
|
1597
2001
|
};
|
|
1598
2002
|
case "JSXElement":
|
|
1599
|
-
case "JSXFragment": return
|
|
1600
|
-
kind: "jsx",
|
|
1601
|
-
text: textOf(this.context, value)
|
|
1602
|
-
};
|
|
2003
|
+
case "JSXFragment": return this.foldedJSX(value);
|
|
1603
2004
|
default:
|
|
1604
2005
|
if (value.expression.type === "JSXEmptyExpression") return EMPTY;
|
|
1605
|
-
if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return
|
|
1606
|
-
kind: "jsx",
|
|
1607
|
-
text: textOf(this.context, value.expression)
|
|
1608
|
-
};
|
|
2006
|
+
if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return this.foldedJSX(value.expression);
|
|
1609
2007
|
return {
|
|
1610
2008
|
kind: "expression",
|
|
1611
|
-
text:
|
|
2009
|
+
text: getText(this.context, value.expression),
|
|
1612
2010
|
primary: isPrimary(value.expression)
|
|
1613
2011
|
};
|
|
1614
2012
|
}
|
|
1615
2013
|
}
|
|
2014
|
+
/**
|
|
2015
|
+
* What a JSX value renders, read after the folds of this round. A fold
|
|
2016
|
+
* inside it can have replaced it with an expression already, such as an
|
|
2017
|
+
* inner `<Show>` that became its string `fallback`.
|
|
2018
|
+
*/
|
|
2019
|
+
foldedJSX(node) {
|
|
2020
|
+
const text = getText(this.context, node);
|
|
2021
|
+
if (text.code.startsWith("<")) return {
|
|
2022
|
+
kind: "jsx",
|
|
2023
|
+
text
|
|
2024
|
+
};
|
|
2025
|
+
return {
|
|
2026
|
+
kind: "expression",
|
|
2027
|
+
text,
|
|
2028
|
+
primary: false
|
|
2029
|
+
};
|
|
2030
|
+
}
|
|
1616
2031
|
foldShow(element) {
|
|
1617
2032
|
const when = this.attributeTruthiness(element, "when");
|
|
1618
2033
|
if (when === void 0) return;
|
|
@@ -1644,11 +2059,11 @@ var Folder = class {
|
|
|
1644
2059
|
* would run while the copy is made, so either one keeps the loop.
|
|
1645
2060
|
*/
|
|
1646
2061
|
unrollFor(element, expression) {
|
|
1647
|
-
if (element
|
|
2062
|
+
if (hasEnabledKeyed(element)) return;
|
|
1648
2063
|
const byIndex = findAttribute(element, "keyed") !== void 0;
|
|
1649
2064
|
const list = this.constantList(expression);
|
|
1650
2065
|
if (!list) return;
|
|
1651
|
-
const significant = element.children
|
|
2066
|
+
const significant = getSignificant(element.children);
|
|
1652
2067
|
const only = significant.at(0);
|
|
1653
2068
|
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return;
|
|
1654
2069
|
let row = only.expression;
|
|
@@ -1657,7 +2072,7 @@ var Folder = class {
|
|
|
1657
2072
|
let body = row.body;
|
|
1658
2073
|
while (body.type === "ParenthesizedExpression") body = body.expression;
|
|
1659
2074
|
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;
|
|
2075
|
+
if (this.context.s.slice(body.start, body.end).code !== this.context.code.slice(body.start, body.end)) return;
|
|
1661
2076
|
const scope = this.context.scopes.scopes.get(row);
|
|
1662
2077
|
if (!scope) return;
|
|
1663
2078
|
const params = [];
|
|
@@ -1670,11 +2085,11 @@ var Folder = class {
|
|
|
1670
2085
|
const indexParam = params.at(1);
|
|
1671
2086
|
const rows = [];
|
|
1672
2087
|
for (const [index, item] of list.items.entries()) {
|
|
1673
|
-
const copy =
|
|
2088
|
+
const copy = this.context.s.fresh();
|
|
1674
2089
|
if (itemParam && !this.substituteItem(copy, itemParam, item, byIndex) || indexParam && !this.substituteIndex(copy, indexParam, index, !byIndex)) return;
|
|
1675
2090
|
rows.push(copy.slice(body.start, body.end));
|
|
1676
2091
|
}
|
|
1677
|
-
const text = `<>${
|
|
2092
|
+
const text = joinText`<>${join(rows)}</>`;
|
|
1678
2093
|
if (!this.unrollingPays(element, text, list.declaration)) return;
|
|
1679
2094
|
return {
|
|
1680
2095
|
kind: "jsx",
|
|
@@ -1686,16 +2101,20 @@ var Folder = class {
|
|
|
1686
2101
|
* `<For>` reads it.
|
|
1687
2102
|
*/
|
|
1688
2103
|
constantList(expression) {
|
|
1689
|
-
let node = expression;
|
|
1690
|
-
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
2104
|
+
let node = unwrap(expression);
|
|
1691
2105
|
let declaration;
|
|
1692
2106
|
if (node.type === "Identifier") {
|
|
1693
2107
|
const binding = this.context.scopes.references.get(node);
|
|
1694
2108
|
if (binding?.declaration.type !== "VariableDeclarator" || binding.declaration.id !== binding.identifier || !binding.declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
|
|
1695
2109
|
const statement = this.context.parents.get(binding.declaration);
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
2110
|
+
const owner = statement ? this.context.parents.get(statement) : void 0;
|
|
2111
|
+
let onlyForEach = true;
|
|
2112
|
+
for (const reference of binding.references) if (!this.isForEach(reference)) {
|
|
2113
|
+
onlyForEach = false;
|
|
2114
|
+
break;
|
|
2115
|
+
}
|
|
2116
|
+
if (owner?.type === "ExportNamedDeclaration" || !onlyForEach) return;
|
|
2117
|
+
node = unwrap(binding.declaration.init);
|
|
1699
2118
|
declaration = binding.references.length === 1 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 ? statement : void 0;
|
|
1700
2119
|
}
|
|
1701
2120
|
if (node.type !== "ArrayExpression") return;
|
|
@@ -1768,7 +2187,7 @@ var Folder = class {
|
|
|
1768
2187
|
}
|
|
1769
2188
|
/** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
|
|
1770
2189
|
valueAt(node, text) {
|
|
1771
|
-
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : `(${text})`;
|
|
2190
|
+
return this.context.parents.get(node)?.type === "JSXExpressionContainer" ? text : joinText`(${text})`;
|
|
1772
2191
|
}
|
|
1773
2192
|
/** Writes an item's index over each read of the row's index parameter. */
|
|
1774
2193
|
substituteIndex(copy, param, index, accessor) {
|
|
@@ -1790,12 +2209,12 @@ var Folder = class {
|
|
|
1790
2209
|
unrollingPays(element, text, declaration) {
|
|
1791
2210
|
const inTemplate = this.joinsParentTemplate(element);
|
|
1792
2211
|
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);
|
|
2212
|
+
const code = `const __so_rows__ = ${text.code};`;
|
|
2213
|
+
const statement = parse$1(this.context.filename, code).body.at(0);
|
|
1795
2214
|
const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
|
|
1796
2215
|
if (init?.type !== "JSXFragment") return false;
|
|
1797
2216
|
const estimator = new SizeEstimator(code);
|
|
1798
|
-
return (inTemplate ?
|
|
2217
|
+
return (inTemplate ? estimateChildren(estimator, init.children) : estimator.jsx(init, false)) <= loop;
|
|
1799
2218
|
}
|
|
1800
2219
|
/** Whether an element sits directly in an element or fragment whose template it joins. */
|
|
1801
2220
|
joinsParentTemplate(element) {
|
|
@@ -1827,8 +2246,16 @@ var Folder = class {
|
|
|
1827
2246
|
});
|
|
1828
2247
|
}
|
|
1829
2248
|
if (matches.length === 0) return;
|
|
1830
|
-
|
|
1831
|
-
|
|
2249
|
+
let winner = -1;
|
|
2250
|
+
let allFalse = true;
|
|
2251
|
+
for (const [index, item] of matches.entries()) {
|
|
2252
|
+
if (item.when === true) {
|
|
2253
|
+
winner = index;
|
|
2254
|
+
break;
|
|
2255
|
+
}
|
|
2256
|
+
if (item.when !== false) allFalse = false;
|
|
2257
|
+
}
|
|
2258
|
+
if (winner !== -1 && allFalse) {
|
|
1832
2259
|
const match = matches.at(winner);
|
|
1833
2260
|
if (!match || hasCallbackChild(match.element.children)) return;
|
|
1834
2261
|
return {
|
|
@@ -1836,7 +2263,7 @@ var Folder = class {
|
|
|
1836
2263
|
children: match.element.children
|
|
1837
2264
|
};
|
|
1838
2265
|
}
|
|
1839
|
-
if (
|
|
2266
|
+
if (winner === -1 && allFalse) return this.fallback(element);
|
|
1840
2267
|
for (const item of matches) if (item.when === false) {
|
|
1841
2268
|
this.context.s.remove(item.element.start, item.element.end);
|
|
1842
2269
|
this.changed = true;
|
|
@@ -1867,11 +2294,12 @@ var Folder = class {
|
|
|
1867
2294
|
}
|
|
1868
2295
|
}
|
|
1869
2296
|
if (tag === void 0) return;
|
|
1870
|
-
const attributes =
|
|
1871
|
-
const
|
|
2297
|
+
const attributes = [tag];
|
|
2298
|
+
for (const attribute of element.openingElement.attributes) if (attribute !== component) attributes.push(getText(this.context, attribute));
|
|
2299
|
+
const opening = join(attributes, " ");
|
|
1872
2300
|
return {
|
|
1873
2301
|
kind: "jsx",
|
|
1874
|
-
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} />`
|
|
1875
2303
|
};
|
|
1876
2304
|
}
|
|
1877
2305
|
/**
|
|
@@ -1886,16 +2314,16 @@ var Folder = class {
|
|
|
1886
2314
|
switch (resolved.kind) {
|
|
1887
2315
|
case "empty": return "null";
|
|
1888
2316
|
case "jsx": return resolved.text;
|
|
1889
|
-
case "expression": return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
2317
|
+
case "expression": return resolved.primary ? resolved.text : joinText`(${resolved.text})`;
|
|
1890
2318
|
default: {
|
|
1891
|
-
const significant = resolved.children
|
|
2319
|
+
const significant = getSignificant(resolved.children);
|
|
1892
2320
|
const only = significant.at(0);
|
|
1893
2321
|
if (only === void 0) return "null";
|
|
1894
|
-
if (significant.length === 1 && (only.type === "JSXElement" || only.type === "JSXFragment")) return
|
|
2322
|
+
if (significant.length === 1 && (only.type === "JSXElement" || only.type === "JSXFragment")) return getText(this.context, only);
|
|
1895
2323
|
const first = resolved.children.at(0);
|
|
1896
2324
|
const last = resolved.children.at(-1);
|
|
1897
2325
|
if (!first || !last) return "null";
|
|
1898
|
-
return `<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
2326
|
+
return joinText`<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
1899
2327
|
}
|
|
1900
2328
|
}
|
|
1901
2329
|
}
|
|
@@ -1931,7 +2359,7 @@ var Folder = class {
|
|
|
1931
2359
|
pieces.push(replacedPiece(child, resolved.text));
|
|
1932
2360
|
break;
|
|
1933
2361
|
case "expression":
|
|
1934
|
-
pieces.push(replacedPiece(child, `{${resolved.text}}`));
|
|
2362
|
+
pieces.push(replacedPiece(child, joinText`{${resolved.text}}`));
|
|
1935
2363
|
break;
|
|
1936
2364
|
default: pieces.push(replacedPiece(child, ""));
|
|
1937
2365
|
}
|
|
@@ -1962,10 +2390,10 @@ var Folder = class {
|
|
|
1962
2390
|
const { params } = callee;
|
|
1963
2391
|
const args = expression.arguments;
|
|
1964
2392
|
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;
|
|
2393
|
+
if (this.context.s.slice(body.start, body.end).code !== this.context.code.slice(body.start, body.end)) return;
|
|
1966
2394
|
const scope = this.context.scopes.scopes.get(callee);
|
|
1967
2395
|
if (!scope) return;
|
|
1968
|
-
const copy =
|
|
2396
|
+
const copy = this.context.s.fresh();
|
|
1969
2397
|
for (const [index, param] of params.entries()) {
|
|
1970
2398
|
const arg = args.at(index);
|
|
1971
2399
|
if (param.type !== "Identifier" || !arg || arg.type === "SpreadElement") return;
|
|
@@ -1976,7 +2404,7 @@ var Folder = class {
|
|
|
1976
2404
|
if (arg.type === "Identifier" && lookup(scopeAt(this.context.scopes, this.context.parents, reference), arg.name) !== this.context.scopes.references.get(arg)) return;
|
|
1977
2405
|
const parent = this.context.parents.get(reference);
|
|
1978
2406
|
const shorthand = parent?.type === "Property" && parent.shorthand && parent.value === reference;
|
|
1979
|
-
copy.overwrite(reference.start, reference.end, shorthand ? `${param.name}: ${text}` : text);
|
|
2407
|
+
copy.overwrite(reference.start, reference.end, shorthand ? joinText`${param.name}: ${text}` : text);
|
|
1980
2408
|
}
|
|
1981
2409
|
}
|
|
1982
2410
|
return copy.slice(body.start, body.end);
|
|
@@ -1986,9 +2414,9 @@ var Folder = class {
|
|
|
1986
2414
|
let node = arg;
|
|
1987
2415
|
while (node.type === "ParenthesizedExpression") node = node.expression;
|
|
1988
2416
|
switch (node.type) {
|
|
1989
|
-
case "Literal": return "regex" in node ? void 0 :
|
|
1990
|
-
case "TemplateLiteral": return node.expressions.length === 0 ?
|
|
1991
|
-
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${
|
|
2417
|
+
case "Literal": return "regex" in node ? void 0 : getText(this.context, node);
|
|
2418
|
+
case "TemplateLiteral": return node.expressions.length === 0 ? getText(this.context, node) : void 0;
|
|
2419
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? joinText`(${getText(this.context, node)})` : void 0;
|
|
1992
2420
|
case "Identifier": {
|
|
1993
2421
|
const binding = this.context.scopes.references.get(node);
|
|
1994
2422
|
return binding && !binding.mutated ? node.name : void 0;
|
|
@@ -2010,18 +2438,73 @@ var Folder = class {
|
|
|
2010
2438
|
const unreachable = statements.slice(terminator + 1);
|
|
2011
2439
|
const first = unreachable.at(0);
|
|
2012
2440
|
const last = unreachable.at(-1);
|
|
2013
|
-
|
|
2441
|
+
let hoists = false;
|
|
2442
|
+
for (const statement of unreachable) if (containsHoistedDeclaration(statement)) {
|
|
2443
|
+
hoists = true;
|
|
2444
|
+
break;
|
|
2445
|
+
}
|
|
2446
|
+
if (first && last && !hoists) {
|
|
2014
2447
|
this.context.s.remove(first.start, last.end);
|
|
2015
2448
|
this.changed = true;
|
|
2016
2449
|
}
|
|
2017
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
|
+
}
|
|
2018
2501
|
replaceStatement(statement, text) {
|
|
2019
|
-
if (text === "") {
|
|
2502
|
+
if (getCode(text) === "") {
|
|
2020
2503
|
this.context.s.remove(statement.start, statement.end);
|
|
2021
2504
|
this.changed = true;
|
|
2022
2505
|
return;
|
|
2023
2506
|
}
|
|
2024
|
-
this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
|
|
2507
|
+
this.replace(statement, continuesPreviousStatement(getCode(text)) ? joinText`;${text}` : text);
|
|
2025
2508
|
}
|
|
2026
2509
|
/**
|
|
2027
2510
|
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
@@ -2031,8 +2514,8 @@ var Folder = class {
|
|
|
2031
2514
|
const parts = effectfulParts(test);
|
|
2032
2515
|
const first = parts.at(0);
|
|
2033
2516
|
if (first === void 0) return;
|
|
2034
|
-
const text =
|
|
2035
|
-
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
2517
|
+
const text = join(getTexts(this.context, parts), ", ");
|
|
2518
|
+
return startsLikeStatement(first) ? joinText`(${text});` : joinText`${text};`;
|
|
2036
2519
|
}
|
|
2037
2520
|
/**
|
|
2038
2521
|
* Resolves an `if` with a constant test. Returns the kept branch, `null`
|
|
@@ -2048,8 +2531,8 @@ var Folder = class {
|
|
|
2048
2531
|
const parts = [];
|
|
2049
2532
|
const effect = this.discardedTest(statement.test);
|
|
2050
2533
|
if (effect !== void 0) parts.push(effect);
|
|
2051
|
-
if (kept) parts.push(
|
|
2052
|
-
this.replaceStatement(statement,
|
|
2534
|
+
if (kept) parts.push(getText(this.context, kept));
|
|
2535
|
+
this.replaceStatement(statement, join(parts, " "));
|
|
2053
2536
|
return kept;
|
|
2054
2537
|
}
|
|
2055
2538
|
resolveWhile(statement) {
|
|
@@ -2063,6 +2546,7 @@ var Folder = class {
|
|
|
2063
2546
|
*/
|
|
2064
2547
|
function fold(context) {
|
|
2065
2548
|
const folder = new Folder(context);
|
|
2549
|
+
folder.removeUnusedDeclarations();
|
|
2066
2550
|
folder.walk(context.program);
|
|
2067
2551
|
return folder.changed;
|
|
2068
2552
|
}
|
|
@@ -2104,7 +2588,7 @@ const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
|
|
|
2104
2588
|
* `useContext` throws when the value and the default are both `undefined`,
|
|
2105
2589
|
* so a value that could be `undefined` keeps its provider.
|
|
2106
2590
|
*/
|
|
2107
|
-
function
|
|
2591
|
+
function findProvider(context, element) {
|
|
2108
2592
|
const binding = contextBinding(context, element.openingElement.name);
|
|
2109
2593
|
if (!binding || element.openingElement.selfClosing) return;
|
|
2110
2594
|
const { attributes } = element.openingElement;
|
|
@@ -2164,7 +2648,7 @@ function isDefined(context, node, depth) {
|
|
|
2164
2648
|
case "Identifier": {
|
|
2165
2649
|
const binding = context.scopes.references.get(expression);
|
|
2166
2650
|
if (!binding || binding.mutated || depth > 2) return false;
|
|
2167
|
-
if (binding.kind === "function" || binding.kind === "class" || isAccessor(context, binding)) return true;
|
|
2651
|
+
if (binding.kind === "function" || binding.kind === "class" || isAccessor$1(context, binding)) return true;
|
|
2168
2652
|
const { declaration } = binding;
|
|
2169
2653
|
return isConstantDeclaration(binding, context.options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init !== null && isDefined(context, declaration.init, depth + 1);
|
|
2170
2654
|
}
|
|
@@ -2202,7 +2686,7 @@ function isHandlerFunction(attribute) {
|
|
|
2202
2686
|
* Whether a binding is a signal or store getter or setter, or a memo, which
|
|
2203
2687
|
* run no code of the app when called.
|
|
2204
2688
|
*/
|
|
2205
|
-
function isAccessor(context, binding) {
|
|
2689
|
+
function isAccessor$1(context, binding) {
|
|
2206
2690
|
if (binding.mutated) return false;
|
|
2207
2691
|
const { declaration } = binding;
|
|
2208
2692
|
if (declaration.type !== "VariableDeclarator") return false;
|
|
@@ -2212,8 +2696,8 @@ function isAccessor(context, binding) {
|
|
|
2212
2696
|
const callee = solidCallee(context, call);
|
|
2213
2697
|
if (id === binding.identifier) return callee === "createMemo";
|
|
2214
2698
|
if (id.type !== "ArrayPattern" || callee === void 0 || !ACCESSOR_PAIRS.has(callee)) return false;
|
|
2215
|
-
const index
|
|
2216
|
-
return
|
|
2699
|
+
for (const [index, element] of id.elements.entries()) if (element === binding.identifier) return index === 0 || index === 1;
|
|
2700
|
+
return false;
|
|
2217
2701
|
}
|
|
2218
2702
|
/**
|
|
2219
2703
|
* Whether all code that can run under `root` is visible, so a provider
|
|
@@ -2290,7 +2774,7 @@ function isClosed(context, root, options) {
|
|
|
2290
2774
|
*/
|
|
2291
2775
|
function runsOutsideCode(context, binding, root, options, depth) {
|
|
2292
2776
|
const { identifier, declaration } = binding;
|
|
2293
|
-
if (binding === options.props || identifier.start >= root.start && identifier.end <= root.end || isAccessor(context, binding) || solidExport(binding, context.options) !== void 0) return false;
|
|
2777
|
+
if (binding === options.props || identifier.start >= root.start && identifier.end <= root.end || isAccessor$1(context, binding) || solidExport(binding, context.options) !== void 0) return false;
|
|
2294
2778
|
if (binding.kind === "param" || binding.kind === "function" || depth > 4) return true;
|
|
2295
2779
|
if (declaration.type !== "VariableDeclarator" || !declaration.init) return false;
|
|
2296
2780
|
const init = unwrap(declaration.init);
|
|
@@ -2327,11 +2811,12 @@ function isOwnView(context, node, props, depth) {
|
|
|
2327
2811
|
if (call?.type !== "CallExpression") return false;
|
|
2328
2812
|
const callee = solidCallee(context, call);
|
|
2329
2813
|
if (callee !== "merge" && callee !== "omit") return false;
|
|
2330
|
-
|
|
2814
|
+
for (const argument of call.arguments) {
|
|
2331
2815
|
if (argument.type === "SpreadElement") return false;
|
|
2332
2816
|
const inner = unwrap(argument);
|
|
2333
|
-
|
|
2334
|
-
}
|
|
2817
|
+
if (inner.type !== "ObjectExpression" && !(inner.type === "Literal" && typeof inner.value === "string") && !isOwnView(context, inner, props, depth + 1)) return false;
|
|
2818
|
+
}
|
|
2819
|
+
return true;
|
|
2335
2820
|
}
|
|
2336
2821
|
function isVisibleElement(context, element, options) {
|
|
2337
2822
|
const { name } = element.openingElement;
|
|
@@ -2362,13 +2847,13 @@ function isAccessorValue(context, node, options, depth) {
|
|
|
2362
2847
|
const expression = unwrap(node);
|
|
2363
2848
|
if (depth > 4) return false;
|
|
2364
2849
|
if (expression.type === "CallExpression") {
|
|
2365
|
-
const value =
|
|
2850
|
+
const value = findContextValue(context, expression, options);
|
|
2366
2851
|
return value !== void 0 && isAccessorValue(context, value, options, depth + 1);
|
|
2367
2852
|
}
|
|
2368
2853
|
if (expression.type !== "Identifier") return false;
|
|
2369
2854
|
const binding = context.scopes.references.get(expression);
|
|
2370
2855
|
if (!binding) return false;
|
|
2371
|
-
if (isAccessor(context, binding)) return true;
|
|
2856
|
+
if (isAccessor$1(context, binding)) return true;
|
|
2372
2857
|
const init = constantInit(context, binding);
|
|
2373
2858
|
return init !== void 0 && isAccessorValue(context, init, options, depth + 1);
|
|
2374
2859
|
}
|
|
@@ -2383,7 +2868,7 @@ function constantInit(context, binding) {
|
|
|
2383
2868
|
/**
|
|
2384
2869
|
* The value `useContext(Ctx)` returns, when the provider is known.
|
|
2385
2870
|
*/
|
|
2386
|
-
function
|
|
2871
|
+
function findContextValue(context, call, options) {
|
|
2387
2872
|
const argument = call.arguments.at(0);
|
|
2388
2873
|
if (solidCallee(context, call) !== "useContext" || call.arguments.length !== 1 || argument === void 0 || argument.type === "SpreadElement") return;
|
|
2389
2874
|
const target = contextBinding(context, unwrap(argument));
|
|
@@ -2396,10 +2881,14 @@ function contextValueOf(context, call, options) {
|
|
|
2396
2881
|
*/
|
|
2397
2882
|
function isVisibleMethod(context, member, options) {
|
|
2398
2883
|
if (member.computed || member.property.type !== "Identifier" || member.optional) return false;
|
|
2399
|
-
const object =
|
|
2400
|
-
if (!object
|
|
2884
|
+
const object = findObjectLiteral(context, member.object, options, 0);
|
|
2885
|
+
if (!object) return false;
|
|
2401
2886
|
const { name } = member.property;
|
|
2402
|
-
const matches =
|
|
2887
|
+
const matches = [];
|
|
2888
|
+
for (const property of object.properties) {
|
|
2889
|
+
if (property.type !== "Property") return false;
|
|
2890
|
+
if (!property.computed && property.key.type === "Identifier" && property.key.name === name) matches.push(property);
|
|
2891
|
+
}
|
|
2403
2892
|
const property = matches.at(0);
|
|
2404
2893
|
if (matches.length !== 1 || property?.type !== "Property" || property.kind !== "init") return false;
|
|
2405
2894
|
const value = unwrap(property.value);
|
|
@@ -2414,18 +2903,18 @@ function isVisibleMethod(context, member, options) {
|
|
|
2414
2903
|
* The object literal an expression evaluates to, through bindings that
|
|
2415
2904
|
* never change and reads of a context whose value is known.
|
|
2416
2905
|
*/
|
|
2417
|
-
function
|
|
2906
|
+
function findObjectLiteral(context, node, options, depth) {
|
|
2418
2907
|
const expression = unwrap(node);
|
|
2419
2908
|
if (depth > 4) return;
|
|
2420
2909
|
if (expression.type === "ObjectExpression") return expression;
|
|
2421
2910
|
if (expression.type === "CallExpression") {
|
|
2422
|
-
const value =
|
|
2423
|
-
return value ?
|
|
2911
|
+
const value = findContextValue(context, expression, options);
|
|
2912
|
+
return value ? findObjectLiteral(context, value, options, depth + 1) : void 0;
|
|
2424
2913
|
}
|
|
2425
2914
|
if (expression.type !== "Identifier") return;
|
|
2426
2915
|
const binding = context.scopes.references.get(expression);
|
|
2427
2916
|
const init = binding ? constantInit(context, binding) : void 0;
|
|
2428
|
-
return init ?
|
|
2917
|
+
return init ? findObjectLiteral(context, init, options, depth + 1) : void 0;
|
|
2429
2918
|
}
|
|
2430
2919
|
/**
|
|
2431
2920
|
* The `useContext(Ctx)` calls under `root` that run under its owner, so
|
|
@@ -2470,7 +2959,7 @@ function findProviders(context) {
|
|
|
2470
2959
|
const providers = [];
|
|
2471
2960
|
const visit = (node) => {
|
|
2472
2961
|
if (node.type === "JSXElement") {
|
|
2473
|
-
const provider =
|
|
2962
|
+
const provider = findProvider(context, node);
|
|
2474
2963
|
if (provider) providers.push(provider);
|
|
2475
2964
|
}
|
|
2476
2965
|
forEachChild(node, visit);
|
|
@@ -2487,7 +2976,12 @@ var ProviderRemover = class {
|
|
|
2487
2976
|
run() {
|
|
2488
2977
|
for (const provider of findProviders(this.context)) {
|
|
2489
2978
|
const { element } = provider;
|
|
2490
|
-
|
|
2979
|
+
let overlaps = false;
|
|
2980
|
+
for (const other of this.removed) if (element.start < other.end && other.start < element.end) {
|
|
2981
|
+
overlaps = true;
|
|
2982
|
+
break;
|
|
2983
|
+
}
|
|
2984
|
+
if (overlaps) continue;
|
|
2491
2985
|
if (hasNestedProvider(this.context, provider)) this.skip(provider, "provider-nested");
|
|
2492
2986
|
else if (this.tryRemove(provider)) this.removed.push(element);
|
|
2493
2987
|
}
|
|
@@ -2504,7 +2998,7 @@ var ProviderRemover = class {
|
|
|
2504
2998
|
const visit = (node) => {
|
|
2505
2999
|
if (node.type === "JSXElement") {
|
|
2506
3000
|
const binding = contextBinding(this.context, node.openingElement.name);
|
|
2507
|
-
if (binding && !
|
|
3001
|
+
if (binding && !findProvider(this.context, node)) {
|
|
2508
3002
|
const { attributes } = node.openingElement;
|
|
2509
3003
|
const attribute = attributes.at(0);
|
|
2510
3004
|
const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
|
|
@@ -2519,17 +3013,24 @@ var ProviderRemover = class {
|
|
|
2519
3013
|
const { element } = provider;
|
|
2520
3014
|
const children = element.children;
|
|
2521
3015
|
let reason = "open-other";
|
|
2522
|
-
|
|
3016
|
+
const closure = {
|
|
2523
3017
|
component: () => false,
|
|
2524
3018
|
contextValue: (target) => target === provider.context ? provider.value : void 0,
|
|
2525
3019
|
onOpen: (open) => {
|
|
2526
3020
|
reason = open;
|
|
2527
3021
|
}
|
|
2528
|
-
}
|
|
3022
|
+
};
|
|
3023
|
+
let closed = true;
|
|
3024
|
+
for (const child of children) if (!isClosed(this.context, child, closure)) {
|
|
3025
|
+
closed = false;
|
|
3026
|
+
break;
|
|
3027
|
+
}
|
|
3028
|
+
if (!closed) {
|
|
2529
3029
|
this.skip(provider, reason);
|
|
2530
3030
|
return false;
|
|
2531
3031
|
}
|
|
2532
|
-
const reads =
|
|
3032
|
+
const reads = [];
|
|
3033
|
+
for (const child of children) reads.push(...contextReads(this.context, child, provider.context));
|
|
2533
3034
|
const texts = [];
|
|
2534
3035
|
for (const read of reads) {
|
|
2535
3036
|
const text = stableValueText(this.context, provider, read);
|
|
@@ -2565,35 +3066,57 @@ function removeProviders(context) {
|
|
|
2565
3066
|
//#endregion
|
|
2566
3067
|
//#region src/copy-size.ts
|
|
2567
3068
|
/**
|
|
2568
|
-
*
|
|
2569
|
-
*
|
|
3069
|
+
* The name of the function a copy is wrapped in to be folded and measured.
|
|
3070
|
+
* It is capitalized, so the inline pass can move statements into it.
|
|
3071
|
+
*/
|
|
3072
|
+
const WRAPPER = "SoSize$$";
|
|
3073
|
+
/**
|
|
3074
|
+
* How many copies are being measured inside each other. Components that
|
|
3075
|
+
* call each other would otherwise measure each other without end.
|
|
2570
3076
|
*/
|
|
2571
|
-
|
|
2572
|
-
const
|
|
3077
|
+
let depth = 0;
|
|
3078
|
+
const MAX_DEPTH = 3;
|
|
2573
3079
|
/**
|
|
2574
3080
|
* The size of a component's copy at one call site, once its props have
|
|
2575
3081
|
* folded. `header` declares the names the fold pass needs to know, like
|
|
2576
|
-
* imports of Solid. `
|
|
2577
|
-
*
|
|
3082
|
+
* imports of Solid. `called` holds the module's components the copy calls,
|
|
3083
|
+
* which inline into it when its props resolve their call sites.
|
|
3084
|
+
* `inTemplate` is whether the call site sits directly in an element.
|
|
3085
|
+
* `measured` receives the code that was measured.
|
|
2578
3086
|
*/
|
|
2579
|
-
function copySize(filename, header, copy, inTemplate, options) {
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
code
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
3087
|
+
function copySize(filename, header, copy, inTemplate, options, called, measured) {
|
|
3088
|
+
const calls = depth < MAX_DEPTH ? called : void 0;
|
|
3089
|
+
let code = `${header}\n${calls?.declarations ?? ""}\nfunction ${WRAPPER}() {\n${join(copy.statements, "\n").code}\nreturn (${getCode(copy.root)});\n}\n`;
|
|
3090
|
+
for (let round = 0; round < 4; round += 1) {
|
|
3091
|
+
let changed = false;
|
|
3092
|
+
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
3093
|
+
const program = parse$1(filename, code);
|
|
3094
|
+
const s = new Editor(code, false);
|
|
3095
|
+
depth += 1;
|
|
3096
|
+
let passChanged;
|
|
3097
|
+
try {
|
|
3098
|
+
passChanged = pass({
|
|
3099
|
+
code,
|
|
3100
|
+
filename,
|
|
3101
|
+
s,
|
|
3102
|
+
program,
|
|
3103
|
+
parents: collectParents(program),
|
|
3104
|
+
scopes: analyzeScopes(program),
|
|
3105
|
+
options,
|
|
3106
|
+
skips: void 0
|
|
3107
|
+
});
|
|
3108
|
+
} finally {
|
|
3109
|
+
depth -= 1;
|
|
3110
|
+
}
|
|
3111
|
+
if (passChanged) {
|
|
3112
|
+
code = s.toString();
|
|
3113
|
+
changed = true;
|
|
3114
|
+
}
|
|
3115
|
+
}
|
|
3116
|
+
if (!changed) break;
|
|
3117
|
+
}
|
|
3118
|
+
if (measured) measured.code = code;
|
|
3119
|
+
const wrapper = parse$1(filename, code).body.at(-1);
|
|
2597
3120
|
if (wrapper?.type !== "FunctionDeclaration" || !wrapper.body) return Number.POSITIVE_INFINITY;
|
|
2598
3121
|
const estimator = new SizeEstimator(code);
|
|
2599
3122
|
let size = 0;
|
|
@@ -2608,77 +3131,6 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2608
3131
|
}
|
|
2609
3132
|
//#endregion
|
|
2610
3133
|
//#region src/inline.ts
|
|
2611
|
-
/**
|
|
2612
|
-
* The inline pass: replaces a component call with the component's body.
|
|
2613
|
-
*
|
|
2614
|
-
* `<Card title="Hi" />` with `function Card(props) { return <div>{props.title}</div>; }`
|
|
2615
|
-
* becomes `<div>{"Hi"}</div>`. The JSX transform then sees one tree instead of
|
|
2616
|
-
* two, so it emits one template, one clone, and fewer hydration markers.
|
|
2617
|
-
*
|
|
2618
|
-
* A component inlines when its shape keeps the result equivalent:
|
|
2619
|
-
*
|
|
2620
|
-
* - It is declared once at the top level of the module, with a capitalized
|
|
2621
|
-
* name, as a function declaration or a `const` bound to a function.
|
|
2622
|
-
* - It takes no parameter, or one identifier that is only read as `props.name`,
|
|
2623
|
-
* or spread onto an element as `{...props}`.
|
|
2624
|
-
* - Its body is statements followed by one final `return` of JSX, or an arrow
|
|
2625
|
-
* function whose body is JSX.
|
|
2626
|
-
* - It does not use `this`, `arguments`, `new.target`, or `super`.
|
|
2627
|
-
*
|
|
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`.
|
|
2631
|
-
*
|
|
2632
|
-
* # How props keep their meaning
|
|
2633
|
-
*
|
|
2634
|
-
* Solid passes a dynamic prop as a getter, so every read of `props.name`
|
|
2635
|
-
* evaluates the attribute's expression again. Inlining replaces each read
|
|
2636
|
-
* with the expression itself, which does the same.
|
|
2637
|
-
*
|
|
2638
|
-
* A static prop, such as an identifier or a function, is evaluated once when
|
|
2639
|
-
* the component is created. Its expression is substituted directly when that
|
|
2640
|
-
* gives the same value, such as a literal or a binding nothing reassigns, or
|
|
2641
|
-
* when the prop is read exactly once. Otherwise its value is stored in a
|
|
2642
|
-
* `const` before the component body runs.
|
|
2643
|
-
*
|
|
2644
|
-
* # Where a component body can go
|
|
2645
|
-
*
|
|
2646
|
-
* A component whose body is only JSX inlines anywhere JSX can go.
|
|
2647
|
-
*
|
|
2648
|
-
* Statements, and stored props, have to run where the component would have
|
|
2649
|
-
* been created. Solid runs a component untracked, so the statements move into
|
|
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.
|
|
2663
|
-
*
|
|
2664
|
-
* A context provider is a component, so statements do not move out of it,
|
|
2665
|
-
* and `useContext` still runs under it. The exception is a provider whose
|
|
2666
|
-
* subtree only runs visible code. Statements move out through it, and each
|
|
2667
|
-
* `useContext` of its context in a copy becomes its value. See `provider.ts`.
|
|
2668
|
-
*
|
|
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.
|
|
2673
|
-
*
|
|
2674
|
-
* # Accepted differences
|
|
2675
|
-
*
|
|
2676
|
-
* - A prop that is a function and is called as `props.name()` runs with
|
|
2677
|
-
* `this` set to its own receiver instead of the props object.
|
|
2678
|
-
* - Statements moved into the host run before the host's JSX is created,
|
|
2679
|
-
* instead of while it is created. The order of `onSettled` and effect
|
|
2680
|
-
* registration across siblings can change.
|
|
2681
|
-
*/
|
|
2682
3134
|
function isWithin(node, container) {
|
|
2683
3135
|
return node.start >= container.start && node.end <= container.end;
|
|
2684
3136
|
}
|
|
@@ -2692,7 +3144,8 @@ function inBodyOrder(sites) {
|
|
|
2692
3144
|
const pending = [...sites];
|
|
2693
3145
|
const ordered = [];
|
|
2694
3146
|
while (pending.length > 0) {
|
|
2695
|
-
const ready =
|
|
3147
|
+
const ready = [];
|
|
3148
|
+
for (const site of pending) if (!hasPendingDependency(pending, site)) ready.push(site);
|
|
2696
3149
|
const next = ready.length > 0 ? ready : pending;
|
|
2697
3150
|
for (const site of next) {
|
|
2698
3151
|
ordered.push(site);
|
|
@@ -2706,6 +3159,12 @@ const UNDEFINED = {
|
|
|
2706
3159
|
text: "void 0",
|
|
2707
3160
|
primary: false
|
|
2708
3161
|
};
|
|
3162
|
+
/** The function a copy is wrapped in when it is measured. See `copy-size.ts`. */
|
|
3163
|
+
const COPY_WRAPPER = "function SoSize$$(";
|
|
3164
|
+
/** Whether code has a JSX tag named `name`. */
|
|
3165
|
+
function hasTag(code, name) {
|
|
3166
|
+
return new RegExp(`<${name.replaceAll("$", String.raw`\$`)}[\\s/>]`).test(code);
|
|
3167
|
+
}
|
|
2709
3168
|
function isCapitalized(name) {
|
|
2710
3169
|
return /^[A-Z]/.test(name);
|
|
2711
3170
|
}
|
|
@@ -2761,6 +3220,38 @@ function containsReturn(node) {
|
|
|
2761
3220
|
return found;
|
|
2762
3221
|
}
|
|
2763
3222
|
/**
|
|
3223
|
+
* Collects the nodes an assignment target writes through: an identifier, or
|
|
3224
|
+
* the root of a member chain, like `cache` in `cache.items[0] = x`. A root
|
|
3225
|
+
* that is not a name, like a call, is collected as it is.
|
|
3226
|
+
*/
|
|
3227
|
+
function collectWriteRoots(target, roots) {
|
|
3228
|
+
switch (target.type) {
|
|
3229
|
+
case "MemberExpression":
|
|
3230
|
+
collectWriteRoots(target.object, roots);
|
|
3231
|
+
break;
|
|
3232
|
+
case "ParenthesizedExpression":
|
|
3233
|
+
case "TSAsExpression":
|
|
3234
|
+
case "TSSatisfiesExpression":
|
|
3235
|
+
case "TSNonNullExpression":
|
|
3236
|
+
case "TSTypeAssertion":
|
|
3237
|
+
collectWriteRoots(target.expression, roots);
|
|
3238
|
+
break;
|
|
3239
|
+
case "ObjectPattern":
|
|
3240
|
+
for (const property of target.properties) collectWriteRoots(property.type === "RestElement" ? property.argument : property.value, roots);
|
|
3241
|
+
break;
|
|
3242
|
+
case "ArrayPattern":
|
|
3243
|
+
for (const element of target.elements) if (element) collectWriteRoots(element, roots);
|
|
3244
|
+
break;
|
|
3245
|
+
case "AssignmentPattern":
|
|
3246
|
+
collectWriteRoots(target.left, roots);
|
|
3247
|
+
break;
|
|
3248
|
+
case "RestElement":
|
|
3249
|
+
collectWriteRoots(target.argument, roots);
|
|
3250
|
+
break;
|
|
3251
|
+
default: roots.push(target);
|
|
3252
|
+
}
|
|
3253
|
+
}
|
|
3254
|
+
/**
|
|
2764
3255
|
* Whether Solid would pass an attribute expression as a getter. The JSX
|
|
2765
3256
|
* transform wraps any expression that reads a member or calls a function.
|
|
2766
3257
|
*/
|
|
@@ -2811,7 +3302,7 @@ function acceptsAnyExpression(context, node) {
|
|
|
2811
3302
|
/**
|
|
2812
3303
|
* The function a top-level binding declares, if it declares one.
|
|
2813
3304
|
*/
|
|
2814
|
-
function
|
|
3305
|
+
function findComponentFunction(binding, options) {
|
|
2815
3306
|
const { declaration } = binding;
|
|
2816
3307
|
if (binding.kind === "function" && declaration.type === "FunctionDeclaration") return declaration;
|
|
2817
3308
|
if (isConstantDeclaration(binding, options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init) {
|
|
@@ -2822,10 +3313,10 @@ function functionOf(binding, options) {
|
|
|
2822
3313
|
/**
|
|
2823
3314
|
* The attributes of a call site by name, or `undefined` when one cannot be inlined.
|
|
2824
3315
|
*/
|
|
2825
|
-
function
|
|
3316
|
+
function getAttributes(context, element) {
|
|
2826
3317
|
const attributes = /* @__PURE__ */ new Map();
|
|
2827
3318
|
for (const attribute of element.openingElement.attributes) {
|
|
2828
|
-
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(attribute) || attributes.has(attribute.name.name)) return;
|
|
3319
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(context, attribute) || attributes.has(attribute.name.name)) return;
|
|
2829
3320
|
attributes.set(attribute.name.name, attribute);
|
|
2830
3321
|
}
|
|
2831
3322
|
return attributes;
|
|
@@ -2837,7 +3328,7 @@ function attributesOf(element) {
|
|
|
2837
3328
|
*/
|
|
2838
3329
|
function isDataDeclaration(statement) {
|
|
2839
3330
|
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
2840
|
-
return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && declaration.declarations
|
|
3331
|
+
return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && areDataDeclarators(declaration.declarations);
|
|
2841
3332
|
}
|
|
2842
3333
|
/** Whether an expression is a literal, or an array or object literal of them. */
|
|
2843
3334
|
function isDataLiteral(node) {
|
|
@@ -2848,8 +3339,8 @@ function isDataLiteral(node) {
|
|
|
2848
3339
|
case "ParenthesizedExpression":
|
|
2849
3340
|
case "TSAsExpression":
|
|
2850
3341
|
case "TSSatisfiesExpression": return isDataLiteral(node.expression);
|
|
2851
|
-
case "ArrayExpression": return node.elements
|
|
2852
|
-
case "ObjectExpression": return node.properties
|
|
3342
|
+
case "ArrayExpression": return areDataElements(node.elements);
|
|
3343
|
+
case "ObjectExpression": return areDataProperties(node.properties);
|
|
2853
3344
|
default: return false;
|
|
2854
3345
|
}
|
|
2855
3346
|
}
|
|
@@ -2862,19 +3353,36 @@ function isMemberRead(read) {
|
|
|
2862
3353
|
* assigned the element, so it has to stay the name itself. Anything else
|
|
2863
3354
|
* would be evaluated when the element is made, outside the `untrack` the
|
|
2864
3355
|
* component's `ref` runs it in.
|
|
3356
|
+
*
|
|
3357
|
+
* Inside an array, `void 0` and `untrack(() => target)` count too. Solid
|
|
3358
|
+
* skips an empty entry of a ref array, and evaluates the whole `ref`
|
|
3359
|
+
* untracked. They come from a component that forwards `props.ref` in an
|
|
3360
|
+
* array, once a call site that passes no `ref` is inlined.
|
|
2865
3361
|
*/
|
|
2866
|
-
function isForwardableRef(attribute) {
|
|
3362
|
+
function isForwardableRef(context, attribute) {
|
|
2867
3363
|
const { value } = attribute;
|
|
2868
3364
|
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
|
|
2869
|
-
const isTarget = (node) => {
|
|
3365
|
+
const isTarget = (node, inArray) => {
|
|
2870
3366
|
const inner = unwrap(node);
|
|
2871
|
-
if (inner.type === "ArrayExpression")
|
|
3367
|
+
if (inner.type === "ArrayExpression") {
|
|
3368
|
+
for (const element of inner.elements) if (element === null || element.type === "SpreadElement" || !isTarget(element, true)) return false;
|
|
3369
|
+
return true;
|
|
3370
|
+
}
|
|
3371
|
+
if (inArray && isVoidZero(inner)) return true;
|
|
3372
|
+
if (inArray && inner.type === "CallExpression" && solidCallee(context, inner) === "untrack") {
|
|
3373
|
+
const callback = inner.arguments.at(0);
|
|
3374
|
+
return inner.arguments.length === 1 && callback?.type === "ArrowFunctionExpression" && callback.expression && callback.params.length === 0 && callback.body.type !== "BlockStatement" && isTarget(callback.body, true);
|
|
3375
|
+
}
|
|
2872
3376
|
return inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2873
3377
|
};
|
|
2874
|
-
return isTarget(value.expression);
|
|
3378
|
+
return isTarget(value.expression, false);
|
|
3379
|
+
}
|
|
3380
|
+
/** Whether an expression is `void 0`. */
|
|
3381
|
+
function isVoidZero(node) {
|
|
3382
|
+
return node.type === "UnaryExpression" && node.operator === "void" && node.argument.type === "Literal" && node.argument.value === 0;
|
|
2875
3383
|
}
|
|
2876
3384
|
/**
|
|
2877
|
-
* Why `
|
|
3385
|
+
* Why `getAttributes` turns down a call site, for the report.
|
|
2878
3386
|
*/
|
|
2879
3387
|
function callSiteReason(element) {
|
|
2880
3388
|
const names = /* @__PURE__ */ new Set();
|
|
@@ -2912,6 +3420,9 @@ var Inliner = class {
|
|
|
2912
3420
|
this.hosts = /* @__PURE__ */ new Map();
|
|
2913
3421
|
this.transparent = /* @__PURE__ */ new Map();
|
|
2914
3422
|
this.reason = "";
|
|
3423
|
+
this.components = [];
|
|
3424
|
+
this.sites = [];
|
|
3425
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
2915
3426
|
}
|
|
2916
3427
|
/**
|
|
2917
3428
|
* Whether a binding holds a component made by Solid's `lazy`, for the
|
|
@@ -2934,7 +3445,7 @@ var Inliner = class {
|
|
|
2934
3445
|
visit(loader);
|
|
2935
3446
|
return imports;
|
|
2936
3447
|
}
|
|
2937
|
-
|
|
3448
|
+
getScope(fn) {
|
|
2938
3449
|
return this.context.scopes.scopes.get(fn);
|
|
2939
3450
|
}
|
|
2940
3451
|
/**
|
|
@@ -2947,13 +3458,34 @@ var Inliner = class {
|
|
|
2947
3458
|
const statement = this.context.parents.get(declaration);
|
|
2948
3459
|
if (statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program") return statement;
|
|
2949
3460
|
}
|
|
3461
|
+
/**
|
|
3462
|
+
* Whether statements assign, update, or delete through a binding declared
|
|
3463
|
+
* outside `scope`, or through a global. Writes inside nested functions
|
|
3464
|
+
* count too, since a function can run right away, like a memo's.
|
|
3465
|
+
*/
|
|
3466
|
+
writesOutside(statements, scope) {
|
|
3467
|
+
const roots = [];
|
|
3468
|
+
const visit = (node) => {
|
|
3469
|
+
if (node.type === "AssignmentExpression") collectWriteRoots(node.left, roots);
|
|
3470
|
+
else if (node.type === "UpdateExpression") collectWriteRoots(node.argument, roots);
|
|
3471
|
+
else if (node.type === "UnaryExpression" && node.operator === "delete") collectWriteRoots(node.argument, roots);
|
|
3472
|
+
forEachChild(node, visit);
|
|
3473
|
+
};
|
|
3474
|
+
for (const statement of statements) visit(statement);
|
|
3475
|
+
for (const root of roots) {
|
|
3476
|
+
if (root.type !== "Identifier") return true;
|
|
3477
|
+
const target = this.context.scopes.references.get(root);
|
|
3478
|
+
if (!target || !isWithinScope(target.scope, scope)) return true;
|
|
3479
|
+
}
|
|
3480
|
+
return false;
|
|
3481
|
+
}
|
|
2950
3482
|
analyzeComponent(binding) {
|
|
2951
3483
|
if (!isCapitalized(binding.name)) return;
|
|
2952
3484
|
if (binding.mutated) {
|
|
2953
3485
|
this.reason = "component-mutated";
|
|
2954
3486
|
return;
|
|
2955
3487
|
}
|
|
2956
|
-
const fn =
|
|
3488
|
+
const fn = findComponentFunction(binding, this.context.options);
|
|
2957
3489
|
if (!fn?.body) {
|
|
2958
3490
|
this.reason = this.isLazy(binding) ? "lazy" : "not-a-function";
|
|
2959
3491
|
return;
|
|
@@ -2966,7 +3498,7 @@ var Inliner = class {
|
|
|
2966
3498
|
this.reason = "component-params";
|
|
2967
3499
|
return;
|
|
2968
3500
|
}
|
|
2969
|
-
const scope = this.
|
|
3501
|
+
const scope = this.getScope(fn);
|
|
2970
3502
|
if (!scope) {
|
|
2971
3503
|
this.reason = "not-a-function";
|
|
2972
3504
|
return;
|
|
@@ -3003,6 +3535,10 @@ var Inliner = class {
|
|
|
3003
3535
|
this.reason = "component-this";
|
|
3004
3536
|
return;
|
|
3005
3537
|
}
|
|
3538
|
+
if (this.writesOutside(statements, scope)) {
|
|
3539
|
+
this.reason = "component-write";
|
|
3540
|
+
return;
|
|
3541
|
+
}
|
|
3006
3542
|
const param = fn.params.at(0);
|
|
3007
3543
|
let paramBinding;
|
|
3008
3544
|
if (param) {
|
|
@@ -3031,7 +3567,9 @@ var Inliner = class {
|
|
|
3031
3567
|
}
|
|
3032
3568
|
props.push(read);
|
|
3033
3569
|
}
|
|
3034
|
-
|
|
3570
|
+
const kept = [];
|
|
3571
|
+
for (const statement of statements) if (!views.declarations.includes(statement)) kept.push(statement);
|
|
3572
|
+
statements = kept;
|
|
3035
3573
|
const locals = [];
|
|
3036
3574
|
const seen = /* @__PURE__ */ new Set();
|
|
3037
3575
|
for (const inner of this.context.scopes.scopes.values()) {
|
|
@@ -3108,9 +3646,9 @@ var Inliner = class {
|
|
|
3108
3646
|
const init = unwrap(declarator.init);
|
|
3109
3647
|
const callee = init.type === "CallExpression" ? solidCallee(this.context, init) : void 0;
|
|
3110
3648
|
if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
|
|
3111
|
-
const view = this.
|
|
3649
|
+
const view = this.resolveView(callee, init, param, views, args);
|
|
3112
3650
|
if (view === null) continue;
|
|
3113
|
-
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);
|
|
3114
3652
|
if (!view || !binding || binding.mutated) return;
|
|
3115
3653
|
views.set(binding, view);
|
|
3116
3654
|
declarations.push(statement);
|
|
@@ -3175,12 +3713,12 @@ var Inliner = class {
|
|
|
3175
3713
|
* take the props or another view, and `undefined` when it does but its
|
|
3176
3714
|
* other arguments are not literal.
|
|
3177
3715
|
*/
|
|
3178
|
-
|
|
3179
|
-
const
|
|
3716
|
+
resolveView(callee, call, param, views, args) {
|
|
3717
|
+
const resolveSources = (argument) => {
|
|
3180
3718
|
if (argument.type === "CallExpression") {
|
|
3181
3719
|
const nested = solidCallee(this.context, argument);
|
|
3182
3720
|
if (nested !== "merge" && nested !== "omit") return;
|
|
3183
|
-
return this.
|
|
3721
|
+
return this.resolveView(nested, argument, param, views, args)?.sources;
|
|
3184
3722
|
}
|
|
3185
3723
|
if (argument.type !== "Identifier") return;
|
|
3186
3724
|
const binding = this.context.scopes.references.get(argument);
|
|
@@ -3190,13 +3728,18 @@ var Inliner = class {
|
|
|
3190
3728
|
}];
|
|
3191
3729
|
return binding ? views.get(binding)?.sources : void 0;
|
|
3192
3730
|
};
|
|
3193
|
-
|
|
3731
|
+
let involved = false;
|
|
3732
|
+
for (const argument of call.arguments) if (argument.type !== "SpreadElement" && resolveSources(unwrap(argument)) !== void 0) {
|
|
3733
|
+
involved = true;
|
|
3734
|
+
break;
|
|
3735
|
+
}
|
|
3736
|
+
if (!involved) return null;
|
|
3194
3737
|
if (callee === "omit") {
|
|
3195
3738
|
const first = call.arguments.at(0);
|
|
3196
3739
|
const keys = call.arguments.slice(1);
|
|
3197
3740
|
if (first === void 0 || first.type === "SpreadElement") return;
|
|
3198
3741
|
const target = unwrap(first);
|
|
3199
|
-
const base =
|
|
3742
|
+
const base = resolveSources(target);
|
|
3200
3743
|
const hidden = [];
|
|
3201
3744
|
for (const key of keys) {
|
|
3202
3745
|
if (key.type !== "Literal" || typeof key.value !== "string") return;
|
|
@@ -3204,16 +3747,18 @@ var Inliner = class {
|
|
|
3204
3747
|
}
|
|
3205
3748
|
if (!base) return;
|
|
3206
3749
|
args.add(target);
|
|
3207
|
-
|
|
3750
|
+
const hiddenSources = [];
|
|
3751
|
+
for (const source of base) hiddenSources.push({
|
|
3208
3752
|
defaults: source.defaults,
|
|
3209
3753
|
hidden: /* @__PURE__ */ new Set([...source.hidden, ...hidden])
|
|
3210
|
-
})
|
|
3754
|
+
});
|
|
3755
|
+
return { sources: hiddenSources };
|
|
3211
3756
|
}
|
|
3212
3757
|
const sources = [];
|
|
3213
3758
|
for (const argument of call.arguments) {
|
|
3214
3759
|
if (argument.type === "SpreadElement") return;
|
|
3215
3760
|
const inner = unwrap(argument);
|
|
3216
|
-
const existing =
|
|
3761
|
+
const existing = resolveSources(inner);
|
|
3217
3762
|
if (existing) {
|
|
3218
3763
|
args.add(inner);
|
|
3219
3764
|
sources.push(...existing);
|
|
@@ -3230,7 +3775,7 @@ var Inliner = class {
|
|
|
3230
3775
|
const value = evaluate(property.value, () => void 0);
|
|
3231
3776
|
const text = value ? literalText(value.value) : void 0;
|
|
3232
3777
|
if (text === void 0 || isReservedPropName(key)) return;
|
|
3233
|
-
defaults.set(key, text);
|
|
3778
|
+
defaults.set(key, this.context.s.written(text, property.value.start));
|
|
3234
3779
|
}
|
|
3235
3780
|
sources.push({
|
|
3236
3781
|
defaults,
|
|
@@ -3268,7 +3813,7 @@ var Inliner = class {
|
|
|
3268
3813
|
if (parent?.type === "CallExpression") return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === "render";
|
|
3269
3814
|
const element = parent?.type === "JSXExpressionContainer" ? this.context.parents.get(parent) : void 0;
|
|
3270
3815
|
if (element?.type !== "JSXElement") return false;
|
|
3271
|
-
const significant = element.children
|
|
3816
|
+
const significant = getSignificantChildren(element.children);
|
|
3272
3817
|
if (significant.length !== 1 || significant.at(0) !== parent) return false;
|
|
3273
3818
|
switch (this.solidCallee(element.openingElement.name)) {
|
|
3274
3819
|
case "For":
|
|
@@ -3291,7 +3836,7 @@ var Inliner = class {
|
|
|
3291
3836
|
* returned JSX of a component, with only intrinsic elements, fragments,
|
|
3292
3837
|
* and closed providers between it and the call.
|
|
3293
3838
|
*/
|
|
3294
|
-
|
|
3839
|
+
findHost(element) {
|
|
3295
3840
|
let node = element;
|
|
3296
3841
|
for (;;) {
|
|
3297
3842
|
const parent = this.context.parents.get(node);
|
|
@@ -3300,7 +3845,7 @@ var Inliner = class {
|
|
|
3300
3845
|
const { name } = parent.openingElement;
|
|
3301
3846
|
if (!(this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && (isIntrinsicTag(name.name) || this.transparent.has(parent)))) return;
|
|
3302
3847
|
const child = node;
|
|
3303
|
-
if (!parent.children
|
|
3848
|
+
if (!hasChild(parent.children, child)) return;
|
|
3304
3849
|
} else if (parent.type === "ReturnStatement") {
|
|
3305
3850
|
const list = this.context.parents.get(parent);
|
|
3306
3851
|
if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
|
|
@@ -3329,8 +3874,8 @@ var Inliner = class {
|
|
|
3329
3874
|
}
|
|
3330
3875
|
childrenValue(element, reads, fresh, snapshots) {
|
|
3331
3876
|
const { children } = element;
|
|
3332
|
-
if (children
|
|
3333
|
-
const significant = children
|
|
3877
|
+
if (hasSpreadChild(children)) return;
|
|
3878
|
+
const significant = getSignificantChildren(children);
|
|
3334
3879
|
const only = significant.at(0);
|
|
3335
3880
|
if (only === void 0) return {
|
|
3336
3881
|
kind: "children",
|
|
@@ -3340,14 +3885,14 @@ var Inliner = class {
|
|
|
3340
3885
|
};
|
|
3341
3886
|
let text;
|
|
3342
3887
|
let primary = true;
|
|
3343
|
-
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)}</>`;
|
|
3344
3889
|
else if (only.type === "JSXExpressionContainer") {
|
|
3345
3890
|
if (only.expression.type === "JSXEmptyExpression") return;
|
|
3346
3891
|
const value = this.expressionValue(only.expression, "children", reads, fresh, snapshots);
|
|
3347
|
-
if (value.kind !== "expression" || value.text !==
|
|
3892
|
+
if (value.kind !== "expression" || getCode(value.text) !== getText(this.context, only.expression).code) return value;
|
|
3348
3893
|
text = value.text;
|
|
3349
3894
|
primary = value.primary;
|
|
3350
|
-
} else text =
|
|
3895
|
+
} else text = getText(this.context, only);
|
|
3351
3896
|
return {
|
|
3352
3897
|
kind: "children",
|
|
3353
3898
|
children,
|
|
@@ -3374,7 +3919,7 @@ var Inliner = class {
|
|
|
3374
3919
|
}
|
|
3375
3920
|
return {
|
|
3376
3921
|
kind: "expression",
|
|
3377
|
-
text:
|
|
3922
|
+
text: getText(this.context, value.expression),
|
|
3378
3923
|
primary: true
|
|
3379
3924
|
};
|
|
3380
3925
|
}
|
|
@@ -3395,13 +3940,13 @@ var Inliner = class {
|
|
|
3395
3940
|
if (value.value.includes("&")) return;
|
|
3396
3941
|
return {
|
|
3397
3942
|
kind: "expression",
|
|
3398
|
-
text: JSON.stringify(value.value),
|
|
3943
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
3399
3944
|
primary: true
|
|
3400
3945
|
};
|
|
3401
3946
|
case "JSXElement":
|
|
3402
3947
|
case "JSXFragment": return {
|
|
3403
3948
|
kind: "jsx",
|
|
3404
|
-
text:
|
|
3949
|
+
text: getText(this.context, value),
|
|
3405
3950
|
primary: true
|
|
3406
3951
|
};
|
|
3407
3952
|
}
|
|
@@ -3414,7 +3959,7 @@ var Inliner = class {
|
|
|
3414
3959
|
* How an expression passed as a prop reads in a copy of the component.
|
|
3415
3960
|
*/
|
|
3416
3961
|
expressionValue(expression, name, reads, fresh, snapshots) {
|
|
3417
|
-
const text =
|
|
3962
|
+
const text = getText(this.context, expression);
|
|
3418
3963
|
const inner = unwrap(expression);
|
|
3419
3964
|
if (inner.type === "JSXElement" || inner.type === "JSXFragment") return {
|
|
3420
3965
|
kind: "jsx",
|
|
@@ -3434,7 +3979,7 @@ var Inliner = class {
|
|
|
3434
3979
|
primary
|
|
3435
3980
|
};
|
|
3436
3981
|
const local = fresh(name);
|
|
3437
|
-
snapshots.push(`const ${local} = ${text};`);
|
|
3982
|
+
snapshots.push(joinText`const ${local} = ${text};`);
|
|
3438
3983
|
return {
|
|
3439
3984
|
kind: "expression",
|
|
3440
3985
|
text: local,
|
|
@@ -3476,12 +4021,12 @@ var Inliner = class {
|
|
|
3476
4021
|
* when it cannot be inlined there. Nothing is written until `commit`.
|
|
3477
4022
|
*/
|
|
3478
4023
|
prepare(component, element) {
|
|
3479
|
-
const attributes =
|
|
4024
|
+
const attributes = getAttributes(this.context, element);
|
|
3480
4025
|
if (!attributes) {
|
|
3481
4026
|
this.reason = callSiteReason(element);
|
|
3482
4027
|
return;
|
|
3483
4028
|
}
|
|
3484
|
-
const hasChildren = element.children.
|
|
4029
|
+
const hasChildren = getSignificantChildren(element.children).length > 0;
|
|
3485
4030
|
if (hasChildren && attributes.has("children")) {
|
|
3486
4031
|
this.reason = "call-children";
|
|
3487
4032
|
return;
|
|
@@ -3527,7 +4072,7 @@ var Inliner = class {
|
|
|
3527
4072
|
return;
|
|
3528
4073
|
}
|
|
3529
4074
|
const hoisted = component.statements.length > 0 || snapshots.length > 0;
|
|
3530
|
-
const host = hoisted ? this.
|
|
4075
|
+
const host = hoisted ? this.findHost(element) : void 0;
|
|
3531
4076
|
if (hoisted && !host) {
|
|
3532
4077
|
this.reason = "no-host";
|
|
3533
4078
|
return;
|
|
@@ -3537,7 +4082,7 @@ var Inliner = class {
|
|
|
3537
4082
|
this.reason = "context-value";
|
|
3538
4083
|
return;
|
|
3539
4084
|
}
|
|
3540
|
-
const copy =
|
|
4085
|
+
const copy = this.context.s.fresh();
|
|
3541
4086
|
this.renameLocals(copy, component, fresh);
|
|
3542
4087
|
this.substituteProps(copy, views.propReads, values);
|
|
3543
4088
|
this.substituteViews(copy, component, element, views, values);
|
|
@@ -3545,7 +4090,10 @@ var Inliner = class {
|
|
|
3545
4090
|
const statements = [...snapshots];
|
|
3546
4091
|
const first = component.statements.at(0);
|
|
3547
4092
|
const last = component.statements.at(-1);
|
|
3548
|
-
if (first && last)
|
|
4093
|
+
if (first && last) {
|
|
4094
|
+
const text = copy.slice(first.start, last.end);
|
|
4095
|
+
statements.push(text.code.endsWith(";") ? text : joinText`${text};`);
|
|
4096
|
+
}
|
|
3549
4097
|
return {
|
|
3550
4098
|
element,
|
|
3551
4099
|
host,
|
|
@@ -3553,19 +4101,22 @@ var Inliner = class {
|
|
|
3553
4101
|
root: copy.slice(component.root.start, component.root.end)
|
|
3554
4102
|
};
|
|
3555
4103
|
}
|
|
4104
|
+
/** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
|
|
4105
|
+
addToHost(found, texts) {
|
|
4106
|
+
const key = found.host.kind === "return" ? found.host.statement : found.fn;
|
|
4107
|
+
const entry = this.hosts.get(key) ?? {
|
|
4108
|
+
host: found.host,
|
|
4109
|
+
texts: []
|
|
4110
|
+
};
|
|
4111
|
+
entry.texts.push(...texts);
|
|
4112
|
+
this.hosts.set(key, entry);
|
|
4113
|
+
}
|
|
3556
4114
|
/**
|
|
3557
4115
|
* Writes a prepared copy over its call site.
|
|
3558
4116
|
*/
|
|
3559
4117
|
commit(site) {
|
|
3560
4118
|
this.context.s.overwrite(site.element.start, site.element.end, site.root);
|
|
3561
|
-
if (site.host)
|
|
3562
|
-
const entry = this.hosts.get(site.host.fn) ?? {
|
|
3563
|
-
host: site.host.host,
|
|
3564
|
-
texts: []
|
|
3565
|
-
};
|
|
3566
|
-
entry.texts.push(...site.statements);
|
|
3567
|
-
this.hosts.set(site.host.fn, entry);
|
|
3568
|
-
}
|
|
4119
|
+
if (site.host) this.addToHost(site.host, site.statements);
|
|
3569
4120
|
}
|
|
3570
4121
|
/**
|
|
3571
4122
|
* The context reads in a component's copy at `element` that a closed
|
|
@@ -3604,7 +4155,8 @@ var Inliner = class {
|
|
|
3604
4155
|
* stored in a `const` of the host first, so every read gets the same one.
|
|
3605
4156
|
*/
|
|
3606
4157
|
findTransparentProviders(components, sites) {
|
|
3607
|
-
const byBinding = new Map(
|
|
4158
|
+
const byBinding = /* @__PURE__ */ new Map();
|
|
4159
|
+
for (const component of components) byBinding.set(component.binding, component);
|
|
3608
4160
|
const closedUnder = (provider) => {
|
|
3609
4161
|
const closedComponents = /* @__PURE__ */ new Map();
|
|
3610
4162
|
const options = {
|
|
@@ -3629,22 +4181,22 @@ var Inliner = class {
|
|
|
3629
4181
|
const changed = [];
|
|
3630
4182
|
for (const provider of findProviders(this.context)) {
|
|
3631
4183
|
const { element } = provider;
|
|
3632
|
-
if (!sites
|
|
4184
|
+
if (!hasSiteInside(sites, element)) continue;
|
|
3633
4185
|
const isClosedNode = closedUnder(provider);
|
|
3634
|
-
|
|
4186
|
+
let closed = true;
|
|
4187
|
+
for (const child of element.children) if (!isClosedNode(child)) {
|
|
4188
|
+
closed = false;
|
|
4189
|
+
break;
|
|
4190
|
+
}
|
|
4191
|
+
if (!closed) continue;
|
|
3635
4192
|
if (stableValueText(this.context, provider, element) !== void 0) {
|
|
3636
4193
|
this.transparent.set(element, { provider });
|
|
3637
4194
|
continue;
|
|
3638
4195
|
}
|
|
3639
|
-
const host = this.
|
|
4196
|
+
const host = this.findHost(element);
|
|
3640
4197
|
if (!host) continue;
|
|
3641
4198
|
const name = this.freshName(`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`);
|
|
3642
|
-
const
|
|
3643
|
-
host: host.host,
|
|
3644
|
-
texts: []
|
|
3645
|
-
};
|
|
3646
|
-
entry.texts.push(`const ${name} = ${textOf(this.context, provider.value)};`);
|
|
3647
|
-
this.hosts.set(host.fn, entry);
|
|
4199
|
+
this.addToHost(host, [joinText`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
3648
4200
|
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
3649
4201
|
this.transparent.set(element, {
|
|
3650
4202
|
provider,
|
|
@@ -3689,7 +4241,8 @@ var Inliner = class {
|
|
|
3689
4241
|
const order = [];
|
|
3690
4242
|
for (const source of spread.view.sources) {
|
|
3691
4243
|
const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
|
|
3692
|
-
for (const key of keys
|
|
4244
|
+
for (const key of keys) {
|
|
4245
|
+
if (source.hidden.has(key)) continue;
|
|
3693
4246
|
const index = order.indexOf(key);
|
|
3694
4247
|
if (index !== -1) order.splice(index, 1);
|
|
3695
4248
|
order.push(key);
|
|
@@ -3698,7 +4251,7 @@ var Inliner = class {
|
|
|
3698
4251
|
let names = taken.get(spread.element);
|
|
3699
4252
|
if (!names) {
|
|
3700
4253
|
names = /* @__PURE__ */ new Set();
|
|
3701
|
-
for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.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);
|
|
3702
4255
|
taken.set(spread.element, names);
|
|
3703
4256
|
}
|
|
3704
4257
|
const keys = /* @__PURE__ */ new Map();
|
|
@@ -3724,8 +4277,8 @@ var Inliner = class {
|
|
|
3724
4277
|
*/
|
|
3725
4278
|
substituteViews(copy, component, element, views, values) {
|
|
3726
4279
|
for (const [member, text] of views.texts) {
|
|
3727
|
-
const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
3728
|
-
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})`);
|
|
3729
4282
|
}
|
|
3730
4283
|
for (const { spread, keys } of views.spreads) {
|
|
3731
4284
|
const parts = [];
|
|
@@ -3733,16 +4286,16 @@ var Inliner = class {
|
|
|
3733
4286
|
for (const [key, text] of keys) {
|
|
3734
4287
|
const value = values.get(key);
|
|
3735
4288
|
if (key === "children") children = this.spreadChildren(element, text, value);
|
|
3736
|
-
else parts.push(`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
4289
|
+
else parts.push(joinText`${key}={${text ?? value?.text ?? "void 0"}}`);
|
|
3737
4290
|
}
|
|
3738
4291
|
const { attribute } = spread;
|
|
3739
4292
|
if (parts.length === 0) copy.remove(attribute.start, attribute.end);
|
|
3740
|
-
else copy.overwrite(attribute.start, attribute.end,
|
|
4293
|
+
else copy.overwrite(attribute.start, attribute.end, join(parts, " "));
|
|
3741
4294
|
if (children !== void 0) {
|
|
3742
4295
|
const opening = spread.element.openingElement;
|
|
3743
|
-
const tag =
|
|
4296
|
+
const tag = copy.slice(opening.name.start, opening.name.end);
|
|
3744
4297
|
const close = this.context.code.lastIndexOf("/>", opening.end);
|
|
3745
|
-
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
4298
|
+
copy.overwrite(close, opening.end, joinText`>${children}</${tag}>`);
|
|
3746
4299
|
}
|
|
3747
4300
|
}
|
|
3748
4301
|
for (const declaration of component.viewDeclarations) copy.remove(declaration.start, declaration.end);
|
|
@@ -3752,10 +4305,10 @@ var Inliner = class {
|
|
|
3752
4305
|
* children, or the value of a single expression or default.
|
|
3753
4306
|
*/
|
|
3754
4307
|
spreadChildren(element, text, value) {
|
|
3755
|
-
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}}`;
|
|
3756
4309
|
if (!value) return;
|
|
3757
4310
|
if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
3758
|
-
return `{${value.text}}`;
|
|
4311
|
+
return joinText`{${value.text}}`;
|
|
3759
4312
|
}
|
|
3760
4313
|
renameLocals(copy, component, fresh) {
|
|
3761
4314
|
for (const local of component.locals) {
|
|
@@ -3785,22 +4338,16 @@ var Inliner = class {
|
|
|
3785
4338
|
continue;
|
|
3786
4339
|
}
|
|
3787
4340
|
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
3788
|
-
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})`);
|
|
4342
|
+
}
|
|
4343
|
+
for (const [parent, pieces] of spliced) {
|
|
4344
|
+
const children = [];
|
|
4345
|
+
for (const child of parent.children) children.push(pieces.get(child) ?? keptPiece(child));
|
|
4346
|
+
writePieces(copy, children);
|
|
3789
4347
|
}
|
|
3790
|
-
for (const [parent, pieces] of spliced) writePieces(copy, parent.children.map((child) => pieces.get(child) ?? keptPiece(child)));
|
|
3791
4348
|
}
|
|
3792
4349
|
splicedPiece(container, value) {
|
|
3793
|
-
if (value.kind === "children")
|
|
3794
|
-
const first = value.children.at(0);
|
|
3795
|
-
const last = value.children.at(-1);
|
|
3796
|
-
if (!first || !last) return replacedPiece(container, "");
|
|
3797
|
-
return {
|
|
3798
|
-
node: container,
|
|
3799
|
-
text: this.context.s.slice(first.start, last.end),
|
|
3800
|
-
startText: first.type === "JSXText" ? first : void 0,
|
|
3801
|
-
endText: last.type === "JSXText" ? last : void 0
|
|
3802
|
-
};
|
|
3803
|
-
}
|
|
4350
|
+
if (value.kind === "children") return splicePiece(this.context.s, container, value.children);
|
|
3804
4351
|
return replacedPiece(container, value.text);
|
|
3805
4352
|
}
|
|
3806
4353
|
/**
|
|
@@ -3816,21 +4363,40 @@ var Inliner = class {
|
|
|
3816
4363
|
byComponent.set(site.component, elements);
|
|
3817
4364
|
}
|
|
3818
4365
|
const result = /* @__PURE__ */ new Set();
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
4366
|
+
const { alwaysInline, alwaysInlineComponents } = this.context.options;
|
|
4367
|
+
for (const component of byComponent.keys()) if (alwaysInline || alwaysInlineComponents.has(component.binding.name)) {
|
|
4368
|
+
result.add(component);
|
|
4369
|
+
byComponent.delete(component);
|
|
3822
4370
|
}
|
|
4371
|
+
if (byComponent.size === 0) return result;
|
|
3823
4372
|
this.scratchNames = new Set(this.context.scopes.names);
|
|
3824
4373
|
try {
|
|
3825
4374
|
const copiesOf = /* @__PURE__ */ new Map();
|
|
3826
|
-
for (const [component, elements] of byComponent)
|
|
4375
|
+
for (const [component, elements] of byComponent) {
|
|
4376
|
+
const prepared = /* @__PURE__ */ new Map();
|
|
4377
|
+
for (const element of elements) prepared.set(element, this.prepare(component, element));
|
|
4378
|
+
copiesOf.set(component, prepared);
|
|
4379
|
+
}
|
|
4380
|
+
this.sites = sites;
|
|
4381
|
+
this.otherUses = otherUses;
|
|
4382
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
4383
|
+
for (const [inner, byElement] of copiesOf) for (const [element, copy] of byElement) {
|
|
4384
|
+
const outer = copy ? void 0 : findOuterComponent(this.components, inner, element);
|
|
4385
|
+
if (outer) {
|
|
4386
|
+
const list = this.blocked.get(outer) ?? [];
|
|
4387
|
+
list.push(inner);
|
|
4388
|
+
this.blocked.set(outer, list);
|
|
4389
|
+
}
|
|
4390
|
+
}
|
|
3827
4391
|
for (const [component, byElement] of copiesOf) {
|
|
3828
|
-
const copies =
|
|
4392
|
+
const copies = collectPrepared(byElement.values());
|
|
3829
4393
|
if (copies.length === 0) continue;
|
|
3830
4394
|
if (this.inliningGain(component, copies, this.removedShare(component, byElement, otherUses)) + this.enabledGain(component, copies, copiesOf, otherUses) >= 0) result.add(component);
|
|
3831
4395
|
}
|
|
3832
4396
|
} finally {
|
|
3833
4397
|
this.scratchNames = void 0;
|
|
4398
|
+
this.otherUses = void 0;
|
|
4399
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
3834
4400
|
}
|
|
3835
4401
|
return result;
|
|
3836
4402
|
}
|
|
@@ -3841,7 +4407,7 @@ var Inliner = class {
|
|
|
3841
4407
|
* keeps it.
|
|
3842
4408
|
*/
|
|
3843
4409
|
removedShare(component, copies, otherUses) {
|
|
3844
|
-
if (component.declaration === void 0 || otherUses.get(component) !== 0 ||
|
|
4410
|
+
if (component.declaration === void 0 || otherUses.get(component) !== 0 || hasMissingCopy(copies.values())) return 0;
|
|
3845
4411
|
const users = this.context.options.sharedComponents.get(component.binding.name) ?? 1;
|
|
3846
4412
|
return users > 0 ? 1 / users : 0;
|
|
3847
4413
|
}
|
|
@@ -3851,7 +4417,8 @@ var Inliner = class {
|
|
|
3851
4417
|
* element around its call to hoist them to.
|
|
3852
4418
|
*/
|
|
3853
4419
|
enabledGain(component, copies, copiesOf, otherUses) {
|
|
3854
|
-
const elements =
|
|
4420
|
+
const elements = [];
|
|
4421
|
+
for (const copy of copies) elements.push(copy.element);
|
|
3855
4422
|
this.assumedHosts = new Set(elements);
|
|
3856
4423
|
let gain = 0;
|
|
3857
4424
|
try {
|
|
@@ -3859,14 +4426,14 @@ var Inliner = class {
|
|
|
3859
4426
|
if (other === component) continue;
|
|
3860
4427
|
const enabled = new Map(byElement);
|
|
3861
4428
|
let changed = false;
|
|
3862
|
-
for (const [element, copy] of byElement) if (!copy &&
|
|
4429
|
+
for (const [element, copy] of byElement) if (!copy && isWithinAny(element, elements)) {
|
|
3863
4430
|
const next = this.prepare(other, element);
|
|
3864
4431
|
enabled.set(element, next);
|
|
3865
4432
|
changed ||= next !== void 0;
|
|
3866
4433
|
}
|
|
3867
4434
|
if (!changed) continue;
|
|
3868
|
-
const now =
|
|
3869
|
-
const then =
|
|
4435
|
+
const now = collectPrepared(byElement.values());
|
|
4436
|
+
const then = collectPrepared(enabled.values());
|
|
3870
4437
|
const before = now.length > 0 ? Math.max(0, this.inliningGain(other, now, 0)) : 0;
|
|
3871
4438
|
const after = this.inliningGain(other, then, this.removedShare(other, enabled, otherUses));
|
|
3872
4439
|
gain += Math.max(0, after - before);
|
|
@@ -3885,14 +4452,54 @@ var Inliner = class {
|
|
|
3885
4452
|
const estimator = this.estimator();
|
|
3886
4453
|
let calls = share > 0 && component.declaration ? share * estimator.node(component.declaration) : 0;
|
|
3887
4454
|
let inlined = 0;
|
|
4455
|
+
const blocked = this.blockedWithin(component);
|
|
4456
|
+
const kept = /* @__PURE__ */ new Set();
|
|
3888
4457
|
for (const copy of copies) {
|
|
3889
4458
|
const inTemplate = this.sitsInTemplate(copy.element);
|
|
3890
4459
|
calls += estimator.jsx(copy.element, inTemplate);
|
|
3891
|
-
|
|
4460
|
+
const measured = { code: "" };
|
|
4461
|
+
inlined += copySize(this.context.filename, this.sizeHeader(), copy, inTemplate, this.context.options, blocked.length > 0 ? {
|
|
4462
|
+
declarations: this.createDeclarations(blocked),
|
|
4463
|
+
inline
|
|
4464
|
+
} : void 0, measured);
|
|
4465
|
+
const body = measured.code.slice(measured.code.lastIndexOf(COPY_WRAPPER));
|
|
4466
|
+
for (const inner of blocked) if (hasTag(body, inner.binding.name)) kept.add(inner);
|
|
4467
|
+
}
|
|
4468
|
+
if (share > 0) {
|
|
4469
|
+
for (const inner of blocked) if (!kept.has(inner) && inner.declaration && this.onlyUsedWithin(inner, component)) calls += share * estimator.node(inner.declaration);
|
|
3892
4470
|
}
|
|
3893
4471
|
return calls - inlined;
|
|
3894
4472
|
}
|
|
3895
4473
|
/**
|
|
4474
|
+
* The components whose calls in a component's body cannot be inlined
|
|
4475
|
+
* there, but may be in a copy, once the call site resolves the props
|
|
4476
|
+
* they are given. Those they call in turn count too.
|
|
4477
|
+
*/
|
|
4478
|
+
blockedWithin(component) {
|
|
4479
|
+
const found = [];
|
|
4480
|
+
const pending = [component];
|
|
4481
|
+
for (let outer = pending.pop(); outer; outer = pending.pop()) for (const inner of this.blocked.get(outer) ?? []) if (inner !== component && !found.includes(inner)) {
|
|
4482
|
+
found.push(inner);
|
|
4483
|
+
pending.push(inner);
|
|
4484
|
+
}
|
|
4485
|
+
return found;
|
|
4486
|
+
}
|
|
4487
|
+
/** The declarations of components, for a copy measured on its own. */
|
|
4488
|
+
createDeclarations(components) {
|
|
4489
|
+
const texts = [];
|
|
4490
|
+
for (const component of components) {
|
|
4491
|
+
const statement = this.topLevelStatement(component.binding.declaration);
|
|
4492
|
+
if (statement) texts.push(getText(this.context, statement).code);
|
|
4493
|
+
}
|
|
4494
|
+
return texts.join("\n");
|
|
4495
|
+
}
|
|
4496
|
+
/** Whether every call of `inner` is in `outer`, and nothing else uses it. */
|
|
4497
|
+
onlyUsedWithin(inner, outer) {
|
|
4498
|
+
if (this.otherUses?.get(inner) !== 0) return false;
|
|
4499
|
+
for (const site of this.sites) if (site.component === inner && !isWithin(site.element, outer.fn)) return false;
|
|
4500
|
+
return true;
|
|
4501
|
+
}
|
|
4502
|
+
/**
|
|
3896
4503
|
* Whether an element sits directly in an intrinsic element, whose
|
|
3897
4504
|
* template its copy joins. A provider this pass makes transparent is
|
|
3898
4505
|
* removed once its consumers are inlined, so the element above it counts.
|
|
@@ -3907,25 +4514,159 @@ var Inliner = class {
|
|
|
3907
4514
|
this.sizeEstimator ??= new SizeEstimator(this.context.code);
|
|
3908
4515
|
return this.sizeEstimator;
|
|
3909
4516
|
}
|
|
4517
|
+
/** The top-level statement a node is part of, without an `export`. */
|
|
4518
|
+
topLevelStatement(node) {
|
|
4519
|
+
let current = node;
|
|
4520
|
+
for (let parent = this.context.parents.get(current); parent;) {
|
|
4521
|
+
if (parent.type === "Program") return current.type === "ExportNamedDeclaration" || current.type === "ExportDefaultDeclaration" ? current.declaration ?? void 0 : current;
|
|
4522
|
+
current = parent;
|
|
4523
|
+
parent = this.context.parents.get(current);
|
|
4524
|
+
}
|
|
4525
|
+
}
|
|
3910
4526
|
/**
|
|
3911
4527
|
* What a copy measured on its own needs to know: the module's imports,
|
|
3912
4528
|
* and the names that alias Solid's exports.
|
|
3913
4529
|
*/
|
|
3914
4530
|
sizeHeader() {
|
|
3915
4531
|
if (this.header === void 0) {
|
|
3916
|
-
const imports =
|
|
4532
|
+
const imports = [];
|
|
4533
|
+
for (const statement of this.context.program.body) if (statement.type === "ImportDeclaration" || isDataDeclaration(statement)) imports.push(this.context.code.slice(statement.start, statement.end));
|
|
3917
4534
|
const aliases = [...this.context.options.builtInAliases.keys()];
|
|
3918
4535
|
if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
|
|
3919
4536
|
this.header = imports.join("\n");
|
|
3920
4537
|
}
|
|
3921
4538
|
return this.header;
|
|
3922
4539
|
}
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
4540
|
+
/**
|
|
4541
|
+
* The component a top-level alias like `const Button = ButtonRoot` names,
|
|
4542
|
+
* or `undefined` when the binding is not one.
|
|
4543
|
+
*/
|
|
4544
|
+
aliasTarget(binding) {
|
|
4545
|
+
const { declaration } = binding;
|
|
4546
|
+
if (!isCapitalized(binding.name) || !isConstantDeclaration(binding, this.context.options) || declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init) return;
|
|
4547
|
+
const init = unwrap(declaration.init);
|
|
4548
|
+
if (init.type !== "Identifier") return;
|
|
4549
|
+
const target = this.context.scopes.references.get(init);
|
|
4550
|
+
return target && target !== binding && target.scope.parent === void 0 && !target.mutated && isCapitalized(target.name) ? target : void 0;
|
|
4551
|
+
}
|
|
4552
|
+
/**
|
|
4553
|
+
* The parts of a compound component like Kobalte's
|
|
4554
|
+
* `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`,
|
|
4555
|
+
* or `undefined` when the binding is not one. `<Dialog>` is `<DialogRoot>`,
|
|
4556
|
+
* since `Object.assign` returns its first argument, and `<Dialog.Trigger>`
|
|
4557
|
+
* is `<DialogTrigger>` while nothing writes to a member of either.
|
|
4558
|
+
*/
|
|
4559
|
+
compoundTarget(binding) {
|
|
4560
|
+
const { declaration } = binding;
|
|
4561
|
+
if (!isCapitalized(binding.name) || !isConstantDeclaration(binding, this.context.options) || declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init) return;
|
|
4562
|
+
const init = unwrap(declaration.init);
|
|
4563
|
+
if (init.type !== "CallExpression" || init.optional || init.arguments.length !== 2 || !this.isObjectAssign(init.callee)) return;
|
|
4564
|
+
const [first, second] = init.arguments;
|
|
4565
|
+
if (first.type !== "Identifier" || second.type !== "ObjectExpression") return;
|
|
4566
|
+
const root = this.topLevelComponent(first);
|
|
4567
|
+
if (!root || root === binding || this.hasMemberWrite(binding) || this.hasMemberWrite(root)) return;
|
|
4568
|
+
const parts = /* @__PURE__ */ new Map();
|
|
4569
|
+
for (const property of second.properties) {
|
|
4570
|
+
if (property.type !== "Property" || property.kind !== "init" || property.computed || property.method || property.value.type !== "Identifier") return;
|
|
4571
|
+
const key = propertyKeyName(property.key);
|
|
4572
|
+
if (key === void 0) return;
|
|
4573
|
+
const part = this.topLevelComponent(property.value);
|
|
4574
|
+
if (part) parts.set(key, part);
|
|
4575
|
+
}
|
|
4576
|
+
return {
|
|
4577
|
+
root,
|
|
4578
|
+
parts
|
|
4579
|
+
};
|
|
4580
|
+
}
|
|
4581
|
+
/** Whether `callee` is the global `Object.assign`. */
|
|
4582
|
+
isObjectAssign(callee) {
|
|
4583
|
+
return callee.type === "MemberExpression" && !callee.computed && !callee.optional && callee.object.type === "Identifier" && callee.object.name === "Object" && !this.context.scopes.references.get(callee.object) && callee.property.type === "Identifier" && callee.property.name === "assign";
|
|
4584
|
+
}
|
|
4585
|
+
/** The top-level binding with a capitalized name that `reference` reads, when nothing reassigns it. */
|
|
4586
|
+
topLevelComponent(reference) {
|
|
4587
|
+
const target = this.context.scopes.references.get(reference);
|
|
4588
|
+
return target && target.scope.parent === void 0 && !target.mutated && isCapitalized(target.name) ? target : void 0;
|
|
4589
|
+
}
|
|
4590
|
+
/** Whether anything assigns to, updates, or deletes a member of the binding. */
|
|
4591
|
+
hasMemberWrite(binding) {
|
|
4592
|
+
for (const reference of binding.references) {
|
|
4593
|
+
const member = this.context.parents.get(reference);
|
|
4594
|
+
if (member?.type !== "MemberExpression" || member.object !== reference) continue;
|
|
4595
|
+
const use = this.context.parents.get(member);
|
|
4596
|
+
if (use?.type === "AssignmentExpression" && use.left === member || use?.type === "UpdateExpression" || use?.type === "UnaryExpression" && use.operator === "delete") return true;
|
|
4597
|
+
}
|
|
4598
|
+
return false;
|
|
4599
|
+
}
|
|
4600
|
+
/** Whether the code reads a member of the binding, like `DialogRoot.Trigger`. */
|
|
4601
|
+
hasMemberRead(binding) {
|
|
4602
|
+
for (const reference of binding.references) {
|
|
4603
|
+
const member = this.context.parents.get(reference);
|
|
4604
|
+
if ((member?.type === "MemberExpression" || member?.type === "JSXMemberExpression") && member.object === reference) return true;
|
|
4605
|
+
}
|
|
4606
|
+
return false;
|
|
4607
|
+
}
|
|
4608
|
+
/**
|
|
4609
|
+
* The statement of an alias nothing uses that `reference` is the value
|
|
4610
|
+
* of, like `const Separator = SeparatorRoot` once its tags are renamed.
|
|
4611
|
+
* It does not keep the component, and goes away with it. A compound
|
|
4612
|
+
* component counts too, when its tags are renamed and nothing reads the
|
|
4613
|
+
* members `Object.assign` gave its root.
|
|
4614
|
+
*/
|
|
4615
|
+
unusedAlias(reference) {
|
|
4616
|
+
let declarator = this.context.parents.get(reference);
|
|
4617
|
+
if (declarator?.type === "Property") declarator = this.context.parents.get(declarator);
|
|
4618
|
+
if (declarator?.type === "ObjectExpression" || declarator?.type === "CallExpression") {
|
|
4619
|
+
const call = declarator.type === "CallExpression" ? declarator : this.context.parents.get(declarator);
|
|
4620
|
+
declarator = call ? this.context.parents.get(call) : void 0;
|
|
4621
|
+
}
|
|
4622
|
+
if (declarator?.type !== "VariableDeclarator" || declarator.id.type !== "Identifier") return;
|
|
4623
|
+
const alias = this.context.scopes.root.bindings.get(declarator.id.name);
|
|
4624
|
+
if (alias?.declaration !== declarator || alias.references.length > 0) return;
|
|
4625
|
+
const compound = this.compoundTarget(alias);
|
|
4626
|
+
if (this.aliasTarget(alias) === void 0 && (!compound || this.hasMemberRead(compound.root))) return;
|
|
4627
|
+
const statement = this.context.parents.get(declarator);
|
|
4628
|
+
return statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program" ? statement : void 0;
|
|
4629
|
+
}
|
|
4630
|
+
/**
|
|
4631
|
+
* Renames each tag of an alias to the component it names, where that name
|
|
4632
|
+
* means the same. Libraries often export a component under a second name,
|
|
4633
|
+
* like `const Separator = SeparatorRoot`. The next pass inlines the calls.
|
|
4634
|
+
*/
|
|
4635
|
+
renameAliasTags() {
|
|
4636
|
+
let renamed = false;
|
|
4637
|
+
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
4638
|
+
const compound = this.compoundTarget(binding);
|
|
4639
|
+
const target = this.aliasTarget(binding) ?? compound?.root;
|
|
4640
|
+
if (!target) continue;
|
|
4641
|
+
for (const reference of binding.references) {
|
|
4642
|
+
const parent = this.context.parents.get(reference);
|
|
4643
|
+
let tag = reference;
|
|
4644
|
+
let renamedTo = target;
|
|
4645
|
+
if (parent?.type === "JSXMemberExpression" && parent.object === reference) {
|
|
4646
|
+
const part = compound?.parts.get(parent.property.name);
|
|
4647
|
+
if (!part) continue;
|
|
4648
|
+
tag = parent;
|
|
4649
|
+
renamedTo = part;
|
|
4650
|
+
}
|
|
4651
|
+
const element = this.context.parents.get(tag);
|
|
4652
|
+
if (element?.type !== "JSXOpeningElement" && element?.type !== "JSXClosingElement" || element.name !== tag) continue;
|
|
4653
|
+
if (lookup(scopeAt(this.context.scopes, this.context.parents, reference), renamedTo.name) !== renamedTo) continue;
|
|
4654
|
+
this.context.s.overwrite(tag.start, tag.end, renamedTo.name);
|
|
4655
|
+
renamed = true;
|
|
4656
|
+
}
|
|
4657
|
+
}
|
|
4658
|
+
return renamed;
|
|
4659
|
+
}
|
|
4660
|
+
run() {
|
|
4661
|
+
if (this.renameAliasTags()) {
|
|
4662
|
+
this.changed = true;
|
|
4663
|
+
return;
|
|
4664
|
+
}
|
|
4665
|
+
const { components } = this;
|
|
4666
|
+
const rejected = /* @__PURE__ */ new Map();
|
|
4667
|
+
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
4668
|
+
this.reason = "not-a-function";
|
|
4669
|
+
const component = this.analyzeComponent(binding);
|
|
3929
4670
|
if (component) components.push(component);
|
|
3930
4671
|
else rejected.set(binding, this.reason);
|
|
3931
4672
|
}
|
|
@@ -3936,16 +4677,19 @@ var Inliner = class {
|
|
|
3936
4677
|
}
|
|
3937
4678
|
const sites = [];
|
|
3938
4679
|
const otherUses = /* @__PURE__ */ new Map();
|
|
4680
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
3939
4681
|
for (const component of components) {
|
|
3940
4682
|
let uses = 0;
|
|
3941
4683
|
for (const reference of component.binding.references) {
|
|
3942
4684
|
const parent = this.context.parents.get(reference);
|
|
3943
4685
|
if (parent?.type === "JSXClosingElement") continue;
|
|
3944
4686
|
const element = parent ? this.context.parents.get(parent) : void 0;
|
|
4687
|
+
const alias = this.unusedAlias(reference);
|
|
3945
4688
|
if (parent?.type === "JSXOpeningElement" && element?.type === "JSXElement") sites.push({
|
|
3946
4689
|
component,
|
|
3947
4690
|
element
|
|
3948
4691
|
});
|
|
4692
|
+
else if (alias) aliases.set(component, [...aliases.get(component) ?? [], alias]);
|
|
3949
4693
|
else uses += 1;
|
|
3950
4694
|
}
|
|
3951
4695
|
otherUses.set(component, uses);
|
|
@@ -3976,8 +4720,11 @@ var Inliner = class {
|
|
|
3976
4720
|
this.report(rejected, siteReasons);
|
|
3977
4721
|
const copied = [...inlined.keys()];
|
|
3978
4722
|
for (const [component, elements] of inlined) {
|
|
3979
|
-
const insideCopy =
|
|
3980
|
-
if (component.declaration && otherUses.get(component) === 0 && !insideCopy)
|
|
4723
|
+
const insideCopy = isInsideOtherCopy(component, elements, copied);
|
|
4724
|
+
if (component.declaration && otherUses.get(component) === 0 && !insideCopy) {
|
|
4725
|
+
this.context.s.remove(component.declaration.start, component.declaration.end);
|
|
4726
|
+
for (const alias of aliases.get(component) ?? []) this.context.s.remove(alias.start, alias.end);
|
|
4727
|
+
}
|
|
3981
4728
|
}
|
|
3982
4729
|
}
|
|
3983
4730
|
/**
|
|
@@ -4014,7 +4761,7 @@ var Inliner = class {
|
|
|
4014
4761
|
const tag = element.openingElement.name;
|
|
4015
4762
|
if (tag.type === "JSXMemberExpression") return {
|
|
4016
4763
|
reason: "member-tag",
|
|
4017
|
-
name:
|
|
4764
|
+
name: getText(this.context, tag).code
|
|
4018
4765
|
};
|
|
4019
4766
|
if (tag.type !== "JSXIdentifier" || !isCapitalized(tag.name)) return;
|
|
4020
4767
|
const { name } = tag;
|
|
@@ -4050,9 +4797,11 @@ var Inliner = class {
|
|
|
4050
4797
|
writeHosts() {
|
|
4051
4798
|
for (const { host, texts } of this.hosts.values()) if (host.kind === "return") {
|
|
4052
4799
|
const indent = this.indentationAt(host.statement.start);
|
|
4053
|
-
|
|
4800
|
+
const written = [];
|
|
4801
|
+
for (const text of texts) written.push(text, `\n${indent}`);
|
|
4802
|
+
this.context.s.prependRight(host.statement.start, join(written));
|
|
4054
4803
|
} else {
|
|
4055
|
-
this.context.s.prependRight(host.body.start, `{ ${
|
|
4804
|
+
this.context.s.prependRight(host.body.start, joinText`{ ${join(texts, " ")} return `);
|
|
4056
4805
|
this.context.s.appendLeft(host.body.end, "; }");
|
|
4057
4806
|
}
|
|
4058
4807
|
}
|
|
@@ -4065,6 +4814,65 @@ function inline(context) {
|
|
|
4065
4814
|
inliner.run();
|
|
4066
4815
|
return inliner.changed;
|
|
4067
4816
|
}
|
|
4817
|
+
/** Whether another pending site must be done before this one. */
|
|
4818
|
+
function hasPendingDependency(pending, site) {
|
|
4819
|
+
for (const other of pending) if (other !== site && (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))) return true;
|
|
4820
|
+
return false;
|
|
4821
|
+
}
|
|
4822
|
+
function areDataDeclarators(declarators) {
|
|
4823
|
+
for (const declarator of declarators) if (declarator.id.type !== "Identifier" || declarator.init === null || !isDataLiteral(declarator.init)) return false;
|
|
4824
|
+
return true;
|
|
4825
|
+
}
|
|
4826
|
+
function areDataElements(elements) {
|
|
4827
|
+
for (const element of elements) if (element === null || element.type === "SpreadElement" || !isDataLiteral(element)) return false;
|
|
4828
|
+
return true;
|
|
4829
|
+
}
|
|
4830
|
+
function areDataProperties(properties) {
|
|
4831
|
+
for (const property of properties) if (property.type !== "Property" || property.kind !== "init" || property.method || property.computed || !isDataLiteral(property.value)) return false;
|
|
4832
|
+
return true;
|
|
4833
|
+
}
|
|
4834
|
+
function getSignificantChildren(children) {
|
|
4835
|
+
const significant = [];
|
|
4836
|
+
for (const child of children) if (!isInsignificant(child)) significant.push(child);
|
|
4837
|
+
return significant;
|
|
4838
|
+
}
|
|
4839
|
+
function hasSpreadChild(children) {
|
|
4840
|
+
for (const child of children) if (child.type === "JSXSpreadChild") return true;
|
|
4841
|
+
return false;
|
|
4842
|
+
}
|
|
4843
|
+
function hasSiteInside(sites, element) {
|
|
4844
|
+
for (const site of sites) if (isWithin(site.element, element) && site.element !== element) return true;
|
|
4845
|
+
return false;
|
|
4846
|
+
}
|
|
4847
|
+
function findOuterComponent(components, inner, element) {
|
|
4848
|
+
for (const item of components) if (item !== inner && isWithin(element, item.fn)) return item;
|
|
4849
|
+
}
|
|
4850
|
+
function collectPrepared(copies) {
|
|
4851
|
+
const prepared = [];
|
|
4852
|
+
for (const copy of copies) if (copy !== void 0) prepared.push(copy);
|
|
4853
|
+
return prepared;
|
|
4854
|
+
}
|
|
4855
|
+
function hasMissingCopy(copies) {
|
|
4856
|
+
for (const copy of copies) if (copy === void 0) return true;
|
|
4857
|
+
return false;
|
|
4858
|
+
}
|
|
4859
|
+
function isWithinAny(element, outers) {
|
|
4860
|
+
for (const outer of outers) if (isWithin(element, outer)) return true;
|
|
4861
|
+
return false;
|
|
4862
|
+
}
|
|
4863
|
+
function isInsideOtherCopy(component, elements, copied) {
|
|
4864
|
+
for (const element of elements) for (const other of copied) if (other !== component && element.start >= other.fn.start && element.end <= other.fn.end) return true;
|
|
4865
|
+
return false;
|
|
4866
|
+
}
|
|
4867
|
+
function hasChild(children, child) {
|
|
4868
|
+
for (const item of children) if (item === child) return true;
|
|
4869
|
+
return false;
|
|
4870
|
+
}
|
|
4871
|
+
/** The name a non-computed object key spells, like `Trigger` or `'Trigger'`. */
|
|
4872
|
+
function propertyKeyName(key) {
|
|
4873
|
+
if (key.type === "Identifier") return key.name;
|
|
4874
|
+
return key.type === "Literal" && typeof key.value === "string" ? key.value : void 0;
|
|
4875
|
+
}
|
|
4068
4876
|
//#endregion
|
|
4069
4877
|
//#region src/memo.ts
|
|
4070
4878
|
/**
|
|
@@ -4077,29 +4885,81 @@ const TRACKED_PROPS = /* @__PURE__ */ new Map([
|
|
|
4077
4885
|
["Repeat", "count"]
|
|
4078
4886
|
]);
|
|
4079
4887
|
/**
|
|
4888
|
+
* Global constructors whose instances are never thenable or async iterable.
|
|
4889
|
+
*/
|
|
4890
|
+
const SYNC_CONSTRUCTORS = /* @__PURE__ */ new Set([
|
|
4891
|
+
"Array",
|
|
4892
|
+
"Date",
|
|
4893
|
+
"Error",
|
|
4894
|
+
"Map",
|
|
4895
|
+
"RegExp",
|
|
4896
|
+
"Set",
|
|
4897
|
+
"URL",
|
|
4898
|
+
"URLSearchParams",
|
|
4899
|
+
"WeakMap",
|
|
4900
|
+
"WeakSet"
|
|
4901
|
+
]);
|
|
4902
|
+
/**
|
|
4903
|
+
* Whether an object literal can have a `then` method or `Symbol.asyncIterator`,
|
|
4904
|
+
* its own or from a `__proto__` it sets.
|
|
4905
|
+
*/
|
|
4906
|
+
function mayBeThenable(node) {
|
|
4907
|
+
for (const property of node.properties) {
|
|
4908
|
+
if (property.type === "SpreadElement" || property.computed) return true;
|
|
4909
|
+
const { key } = property;
|
|
4910
|
+
let name;
|
|
4911
|
+
if (key.type === "Identifier") name = key.name;
|
|
4912
|
+
else if ("value" in key) name = key.value;
|
|
4913
|
+
if (name === "then" || name === "__proto__") return true;
|
|
4914
|
+
}
|
|
4915
|
+
return false;
|
|
4916
|
+
}
|
|
4917
|
+
/**
|
|
4080
4918
|
* Whether evaluating `node` always creates a new value, which reference
|
|
4081
|
-
* equality never finds equal to the last one.
|
|
4919
|
+
* equality never finds equal to the last one. The value is never a promise
|
|
4920
|
+
* or an async iterable, which a memo waits for and an inlined read does not.
|
|
4921
|
+
* `isGlobal` tells whether a name is one the module does not declare.
|
|
4082
4922
|
*/
|
|
4083
|
-
function isFresh(node) {
|
|
4923
|
+
function isFresh(node, isGlobal) {
|
|
4084
4924
|
switch (node.type) {
|
|
4085
|
-
case "ObjectExpression":
|
|
4086
4925
|
case "ArrayExpression":
|
|
4087
4926
|
case "ArrowFunctionExpression":
|
|
4088
4927
|
case "FunctionExpression":
|
|
4089
|
-
case "ClassExpression":
|
|
4090
|
-
case "NewExpression":
|
|
4091
4928
|
case "JSXElement":
|
|
4092
4929
|
case "JSXFragment": return true;
|
|
4093
|
-
case "
|
|
4094
|
-
case "
|
|
4930
|
+
case "ObjectExpression": return !mayBeThenable(node);
|
|
4931
|
+
case "NewExpression": return node.callee.type === "Identifier" && SYNC_CONSTRUCTORS.has(node.callee.name) && isGlobal(node.callee);
|
|
4932
|
+
case "ParenthesizedExpression": return isFresh(node.expression, isGlobal);
|
|
4933
|
+
case "ConditionalExpression": return isFresh(node.consequent, isGlobal) && isFresh(node.alternate, isGlobal);
|
|
4095
4934
|
case "SequenceExpression": {
|
|
4096
4935
|
const last = node.expressions.at(-1);
|
|
4097
|
-
return last !== void 0 && isFresh(last);
|
|
4936
|
+
return last !== void 0 && isFresh(last, isGlobal);
|
|
4098
4937
|
}
|
|
4099
4938
|
default: return false;
|
|
4100
4939
|
}
|
|
4101
4940
|
}
|
|
4102
4941
|
/**
|
|
4942
|
+
* Whether `node` evaluates to a value that is never a promise or an async
|
|
4943
|
+
* iterable. A memo waits for one of those, and an inlined read does not.
|
|
4944
|
+
*/
|
|
4945
|
+
function isSync(node, isGlobal) {
|
|
4946
|
+
switch (node.type) {
|
|
4947
|
+
case "Literal":
|
|
4948
|
+
case "TemplateLiteral":
|
|
4949
|
+
case "UnaryExpression":
|
|
4950
|
+
case "UpdateExpression":
|
|
4951
|
+
case "BinaryExpression": return true;
|
|
4952
|
+
case "ParenthesizedExpression": return isSync(node.expression, isGlobal);
|
|
4953
|
+
case "ConditionalExpression": return isSync(node.consequent, isGlobal) && isSync(node.alternate, isGlobal);
|
|
4954
|
+
case "LogicalExpression": return isSync(node.left, isGlobal) && isSync(node.right, isGlobal);
|
|
4955
|
+
case "SequenceExpression": {
|
|
4956
|
+
const last = node.expressions.at(-1);
|
|
4957
|
+
return last !== void 0 && isSync(last, isGlobal);
|
|
4958
|
+
}
|
|
4959
|
+
default: return isFresh(node, isGlobal);
|
|
4960
|
+
}
|
|
4961
|
+
}
|
|
4962
|
+
/**
|
|
4103
4963
|
* The text of an expression that always gives the same primitive: a literal
|
|
4104
4964
|
* other than a regular expression, a template literal without expressions,
|
|
4105
4965
|
* or a negated number.
|
|
@@ -4107,9 +4967,9 @@ function isFresh(node) {
|
|
|
4107
4967
|
function constantText(context, node) {
|
|
4108
4968
|
const expression = unwrap(node);
|
|
4109
4969
|
switch (expression.type) {
|
|
4110
|
-
case "Literal": return "regex" in expression ? void 0 :
|
|
4111
|
-
case "TemplateLiteral": return expression.expressions.length === 0 ?
|
|
4112
|
-
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ?
|
|
4970
|
+
case "Literal": return "regex" in expression ? void 0 : getText(context, expression);
|
|
4971
|
+
case "TemplateLiteral": return expression.expressions.length === 0 ? getText(context, expression) : void 0;
|
|
4972
|
+
case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? getText(context, expression) : void 0;
|
|
4113
4973
|
default: return;
|
|
4114
4974
|
}
|
|
4115
4975
|
}
|
|
@@ -4125,6 +4985,7 @@ var MemoInliner = class {
|
|
|
4125
4985
|
constructor(context) {
|
|
4126
4986
|
this.context = context;
|
|
4127
4987
|
this.changed = false;
|
|
4988
|
+
this.edited = [];
|
|
4128
4989
|
this.reason = "";
|
|
4129
4990
|
}
|
|
4130
4991
|
/** Whether a callee is Solid's `createMemo` or `children`. */
|
|
@@ -4183,21 +5044,21 @@ var MemoInliner = class {
|
|
|
4183
5044
|
};
|
|
4184
5045
|
}
|
|
4185
5046
|
/**
|
|
4186
|
-
* Replaces every read of a memo whose
|
|
5047
|
+
* Replaces every read of a memo or signal whose value is a literal with the
|
|
4187
5048
|
* literal. Each reference has to be a call after the declaration, since a
|
|
4188
5049
|
* call before it would throw.
|
|
4189
5050
|
*/
|
|
4190
|
-
inlineConstant(declarator, binding, text) {
|
|
5051
|
+
inlineConstant(declarator, binding, text, kind = "memo") {
|
|
4191
5052
|
const statement = this.context.parents.get(declarator);
|
|
4192
5053
|
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
|
|
4193
|
-
this.reason =
|
|
5054
|
+
this.reason = `${kind}-declaration`;
|
|
4194
5055
|
return;
|
|
4195
5056
|
}
|
|
4196
5057
|
const calls = [];
|
|
4197
5058
|
for (const reference of binding.references) {
|
|
4198
5059
|
const call = this.context.parents.get(reference);
|
|
4199
5060
|
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
|
|
4200
|
-
this.reason =
|
|
5061
|
+
this.reason = `${kind}-constant`;
|
|
4201
5062
|
return;
|
|
4202
5063
|
}
|
|
4203
5064
|
calls.push(call);
|
|
@@ -4205,22 +5066,24 @@ var MemoInliner = class {
|
|
|
4205
5066
|
for (const call of calls) {
|
|
4206
5067
|
const parent = this.context.parents.get(call);
|
|
4207
5068
|
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})`);
|
|
5069
|
+
this.context.s.overwrite(call.start, call.end, bare ? text : joinText`(${text})`);
|
|
5070
|
+
this.edited.push([call.start, call.end]);
|
|
4209
5071
|
}
|
|
4210
5072
|
this.context.s.remove(statement.start, statement.end);
|
|
5073
|
+
this.edited.push([statement.start, statement.end]);
|
|
4211
5074
|
this.changed = true;
|
|
4212
5075
|
return true;
|
|
4213
5076
|
}
|
|
4214
5077
|
/**
|
|
4215
5078
|
* The function a node runs in.
|
|
4216
5079
|
*/
|
|
4217
|
-
|
|
5080
|
+
findEnclosingFunction(node) {
|
|
4218
5081
|
let current = this.context.parents.get(node);
|
|
4219
5082
|
while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
|
|
4220
5083
|
return current;
|
|
4221
5084
|
}
|
|
4222
5085
|
/** The JSX expression container a call is the whole of. */
|
|
4223
|
-
|
|
5086
|
+
findContainer(call) {
|
|
4224
5087
|
let parent = this.context.parents.get(call);
|
|
4225
5088
|
while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
|
|
4226
5089
|
return parent?.type === "JSXExpressionContainer" ? parent : void 0;
|
|
@@ -4253,11 +5116,11 @@ var MemoInliner = class {
|
|
|
4253
5116
|
const tag = element.openingElement.name.name;
|
|
4254
5117
|
if (isIntrinsicTag(tag)) {
|
|
4255
5118
|
if (attribute.startsWith("on") || attribute === "ref") return false;
|
|
4256
|
-
} else if (this.
|
|
5119
|
+
} else if (this.getBuiltIn(element.openingElement.name) !== tag || TRACKED_PROPS.get(tag) !== attribute) return false;
|
|
4257
5120
|
} else return false;
|
|
4258
5121
|
return this.isStaticJSX(element, fn);
|
|
4259
5122
|
}
|
|
4260
|
-
|
|
5123
|
+
getBuiltIn(name) {
|
|
4261
5124
|
if (name.type !== "JSXIdentifier") return;
|
|
4262
5125
|
const { references } = this.context.scopes;
|
|
4263
5126
|
if (!references.has(name)) return;
|
|
@@ -4282,7 +5145,7 @@ var MemoInliner = class {
|
|
|
4282
5145
|
}
|
|
4283
5146
|
case "JSXFragment":
|
|
4284
5147
|
case "ParenthesizedExpression": break;
|
|
4285
|
-
case "ReturnStatement": return this.
|
|
5148
|
+
case "ReturnStatement": return this.findEnclosingFunction(parent) === fn && !this.inLoop(parent, fn);
|
|
4286
5149
|
default: return false;
|
|
4287
5150
|
}
|
|
4288
5151
|
node = parent;
|
|
@@ -4326,14 +5189,19 @@ var MemoInliner = class {
|
|
|
4326
5189
|
} else {
|
|
4327
5190
|
const constant = constantText(this.context, expression);
|
|
4328
5191
|
if (constant !== void 0) return this.inlineConstant(declarator, binding, constant);
|
|
4329
|
-
|
|
5192
|
+
const isGlobal = (node) => this.context.scopes.references.get(node) === void 0;
|
|
5193
|
+
if (!alwaysNotifies && !isFresh(expression, isGlobal)) return;
|
|
5194
|
+
if (alwaysNotifies && !isSync(expression, isGlobal)) {
|
|
5195
|
+
this.reason = "memo-async";
|
|
5196
|
+
return;
|
|
5197
|
+
}
|
|
4330
5198
|
}
|
|
4331
5199
|
if (binding.references.length !== 1) {
|
|
4332
5200
|
this.reason = "memo-reads";
|
|
4333
5201
|
return;
|
|
4334
5202
|
}
|
|
4335
5203
|
const statement = this.context.parents.get(declarator);
|
|
4336
|
-
const fn = this.
|
|
5204
|
+
const fn = this.findEnclosingFunction(declarator);
|
|
4337
5205
|
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) {
|
|
4338
5206
|
this.reason = "memo-declaration";
|
|
4339
5207
|
return;
|
|
@@ -4348,7 +5216,7 @@ var MemoInliner = class {
|
|
|
4348
5216
|
this.reason = "memo-read-place";
|
|
4349
5217
|
return;
|
|
4350
5218
|
}
|
|
4351
|
-
const container = this.
|
|
5219
|
+
const container = this.findContainer(call);
|
|
4352
5220
|
const owner = container ? this.context.parents.get(container) : void 0;
|
|
4353
5221
|
if (kind === "children" && (!container || owner?.type !== "JSXElement" && owner?.type !== "JSXFragment")) {
|
|
4354
5222
|
this.reason = "memo-read-place";
|
|
@@ -4358,18 +5226,91 @@ var MemoInliner = class {
|
|
|
4358
5226
|
this.reason = "memo-scope";
|
|
4359
5227
|
return;
|
|
4360
5228
|
}
|
|
4361
|
-
if (kind === "children" && container)
|
|
4362
|
-
|
|
5229
|
+
if (kind === "children" && container) {
|
|
5230
|
+
this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
|
|
5231
|
+
this.edited.push([container.start, container.end]);
|
|
5232
|
+
} else {
|
|
5233
|
+
this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
|
|
5234
|
+
this.edited.push([call.start, call.end]);
|
|
5235
|
+
}
|
|
4363
5236
|
this.context.s.remove(statement.start, statement.end);
|
|
5237
|
+
this.edited.push([statement.start, statement.end]);
|
|
4364
5238
|
this.changed = true;
|
|
4365
5239
|
return true;
|
|
4366
5240
|
}
|
|
5241
|
+
/**
|
|
5242
|
+
* The literal a signal always holds: `const [get] = createSignal(literal)`,
|
|
5243
|
+
* or `const [get, set] = ...` with nothing referencing `set`. Options can
|
|
5244
|
+
* change what a read does, such as `unobserved`, so a signal with options
|
|
5245
|
+
* stays.
|
|
5246
|
+
*/
|
|
5247
|
+
signalConstant(declarator, binding) {
|
|
5248
|
+
const pattern = declarator.id;
|
|
5249
|
+
const init = declarator.init ? unwrap(declarator.init) : void 0;
|
|
5250
|
+
if (pattern.type !== "ArrayPattern" || pattern.elements.at(0) !== binding.identifier || pattern.elements.length > 2 || init?.type !== "CallExpression" || init.callee.type !== "Identifier" || init.arguments.length !== 1) return;
|
|
5251
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
5252
|
+
if (!callee || solidExport(callee, this.context.options) !== "createSignal") return;
|
|
5253
|
+
const value = init.arguments.at(0);
|
|
5254
|
+
if (!value || value.type === "SpreadElement") return;
|
|
5255
|
+
const statement = this.context.parents.get(declarator);
|
|
5256
|
+
if (statement && this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
|
|
5257
|
+
const setter = pattern.elements.at(1);
|
|
5258
|
+
if (setter) {
|
|
5259
|
+
const setterBinding = setter.type === "Identifier" ? binding.scope.bindings.get(setter.name) : void 0;
|
|
5260
|
+
if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) return;
|
|
5261
|
+
}
|
|
5262
|
+
return constantText(this.context, value);
|
|
5263
|
+
}
|
|
5264
|
+
tryInlineSignal(declarator, binding) {
|
|
5265
|
+
const text = this.signalConstant(declarator, binding);
|
|
5266
|
+
if (text === void 0) return;
|
|
5267
|
+
this.reason = "";
|
|
5268
|
+
if (this.inlineConstant(declarator, binding, text, "signal") === void 0) recordSkip(this.context, "memos", this.reason, binding.name);
|
|
5269
|
+
}
|
|
5270
|
+
/**
|
|
5271
|
+
* Replaces every call of `const name = () => expression` with the
|
|
5272
|
+
* expression. Each reference has to be a call with no arguments after the
|
|
5273
|
+
* declaration, and every name in the body has to mean the same at the call.
|
|
5274
|
+
*/
|
|
5275
|
+
tryInlineArrow(declarator, binding) {
|
|
5276
|
+
const fn = declarator.init ? unwrap(declarator.init) : void 0;
|
|
5277
|
+
if (fn?.type !== "ArrowFunctionExpression" || fn.async || fn.params.length > 0) return;
|
|
5278
|
+
const body = returnedExpression(fn);
|
|
5279
|
+
const statement = this.context.parents.get(declarator);
|
|
5280
|
+
if (!body || statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
|
|
5281
|
+
const calls = [];
|
|
5282
|
+
for (const reference of binding.references) {
|
|
5283
|
+
const call = this.context.parents.get(reference);
|
|
5284
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.optional || call.arguments.length > 0 || call.start < declarator.end) return;
|
|
5285
|
+
const parent = this.context.parents.get(call);
|
|
5286
|
+
if (parent?.type === "CallExpression" && parent.callee === call || parent?.type === "TaggedTemplateExpression" && parent.tag === call || parent?.type === "UnaryExpression" && parent.operator === "delete") return;
|
|
5287
|
+
calls.push(call);
|
|
5288
|
+
}
|
|
5289
|
+
if (calls.length === 0) return;
|
|
5290
|
+
for (const [start, end] of this.edited) if (start < statement.end && end > statement.start) return;
|
|
5291
|
+
const text = getText(this.context, body);
|
|
5292
|
+
const length = compactLength(text.code);
|
|
5293
|
+
if ((calls.length - 1) * length > calls.length * 3 + 13) return;
|
|
5294
|
+
if (usesFunctionContext(this.context, fn)) return;
|
|
5295
|
+
for (const call of calls) if (!this.resolvesSameAt(body, call, binding)) return;
|
|
5296
|
+
const bare = isPrimary(body);
|
|
5297
|
+
for (const call of calls) {
|
|
5298
|
+
const inContainer = this.context.parents.get(call)?.type === "JSXExpressionContainer" && body.type !== "SequenceExpression";
|
|
5299
|
+
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : joinText`(${text})`);
|
|
5300
|
+
this.edited.push([call.start, call.end]);
|
|
5301
|
+
}
|
|
5302
|
+
this.context.s.remove(statement.start, statement.end);
|
|
5303
|
+
this.edited.push([statement.start, statement.end]);
|
|
5304
|
+
this.changed = true;
|
|
5305
|
+
}
|
|
4367
5306
|
run() {
|
|
4368
|
-
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
5307
|
+
for (const scope of new Set(this.context.scopes.scopes.values())) for (const binding of scope.bindings.values()) {
|
|
5308
|
+
const { declaration } = binding;
|
|
5309
|
+
if (binding.kind !== "const" || binding.mutated || declaration.type !== "VariableDeclarator") continue;
|
|
5310
|
+
if (declaration.id.type === "ArrayPattern") this.tryInlineSignal(declaration, binding);
|
|
5311
|
+
else if (declaration.id === binding.identifier) {
|
|
5312
|
+
this.tryInlineArrow(declaration, binding);
|
|
5313
|
+
if (scope.parent !== void 0) this.tryInline(declaration, binding);
|
|
4373
5314
|
}
|
|
4374
5315
|
}
|
|
4375
5316
|
}
|
|
@@ -4383,6 +5324,581 @@ function inlineMemos(context) {
|
|
|
4383
5324
|
return inliner.changed;
|
|
4384
5325
|
}
|
|
4385
5326
|
//#endregion
|
|
5327
|
+
//#region src/nesting.ts
|
|
5328
|
+
const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
5329
|
+
"area",
|
|
5330
|
+
"base",
|
|
5331
|
+
"br",
|
|
5332
|
+
"col",
|
|
5333
|
+
"embed",
|
|
5334
|
+
"hr",
|
|
5335
|
+
"img",
|
|
5336
|
+
"input",
|
|
5337
|
+
"keygen",
|
|
5338
|
+
"link",
|
|
5339
|
+
"meta",
|
|
5340
|
+
"param",
|
|
5341
|
+
"source",
|
|
5342
|
+
"track",
|
|
5343
|
+
"wbr"
|
|
5344
|
+
]);
|
|
5345
|
+
const SVG_ELEMENTS = /* @__PURE__ */ new Set([
|
|
5346
|
+
"altGlyph",
|
|
5347
|
+
"altGlyphDef",
|
|
5348
|
+
"altGlyphItem",
|
|
5349
|
+
"animate",
|
|
5350
|
+
"animateColor",
|
|
5351
|
+
"animateMotion",
|
|
5352
|
+
"animateTransform",
|
|
5353
|
+
"circle",
|
|
5354
|
+
"clipPath",
|
|
5355
|
+
"cursor",
|
|
5356
|
+
"defs",
|
|
5357
|
+
"desc",
|
|
5358
|
+
"ellipse",
|
|
5359
|
+
"feBlend",
|
|
5360
|
+
"feColorMatrix",
|
|
5361
|
+
"feComponentTransfer",
|
|
5362
|
+
"feComposite",
|
|
5363
|
+
"feConvolveMatrix",
|
|
5364
|
+
"feDiffuseLighting",
|
|
5365
|
+
"feDisplacementMap",
|
|
5366
|
+
"feDistantLight",
|
|
5367
|
+
"feDropShadow",
|
|
5368
|
+
"feFlood",
|
|
5369
|
+
"feFuncA",
|
|
5370
|
+
"feFuncB",
|
|
5371
|
+
"feFuncG",
|
|
5372
|
+
"feFuncR",
|
|
5373
|
+
"feGaussianBlur",
|
|
5374
|
+
"feImage",
|
|
5375
|
+
"feMerge",
|
|
5376
|
+
"feMergeNode",
|
|
5377
|
+
"feMorphology",
|
|
5378
|
+
"feOffset",
|
|
5379
|
+
"fePointLight",
|
|
5380
|
+
"feSpecularLighting",
|
|
5381
|
+
"feSpotLight",
|
|
5382
|
+
"feTile",
|
|
5383
|
+
"feTurbulence",
|
|
5384
|
+
"filter",
|
|
5385
|
+
"font",
|
|
5386
|
+
"foreignObject",
|
|
5387
|
+
"g",
|
|
5388
|
+
"glyph",
|
|
5389
|
+
"glyphRef",
|
|
5390
|
+
"hkern",
|
|
5391
|
+
"image",
|
|
5392
|
+
"line",
|
|
5393
|
+
"linearGradient",
|
|
5394
|
+
"marker",
|
|
5395
|
+
"mask",
|
|
5396
|
+
"metadata",
|
|
5397
|
+
"mpath",
|
|
5398
|
+
"path",
|
|
5399
|
+
"pattern",
|
|
5400
|
+
"polygon",
|
|
5401
|
+
"polyline",
|
|
5402
|
+
"radialGradient",
|
|
5403
|
+
"rect",
|
|
5404
|
+
"set",
|
|
5405
|
+
"stop",
|
|
5406
|
+
"switch",
|
|
5407
|
+
"symbol",
|
|
5408
|
+
"text",
|
|
5409
|
+
"textPath",
|
|
5410
|
+
"tref",
|
|
5411
|
+
"tspan",
|
|
5412
|
+
"use",
|
|
5413
|
+
"view",
|
|
5414
|
+
"vkern"
|
|
5415
|
+
]);
|
|
5416
|
+
function findElement(parent, name) {
|
|
5417
|
+
for (const child of parent.childNodes) if (child.nodeName === name && "childNodes" in child) return child;
|
|
5418
|
+
throw new Error(`[solid-optimizer] no <${name}> in an empty document`);
|
|
5419
|
+
}
|
|
5420
|
+
let body;
|
|
5421
|
+
/** What a browser makes of `html` set as the body's `innerHTML`. */
|
|
5422
|
+
function parseAsBody(html) {
|
|
5423
|
+
body ??= findElement(findElement(parse("<!DOCTYPE html><html><body></body></html>"), "html"), "body");
|
|
5424
|
+
return serialize(parseFragment(body, html, {}));
|
|
5425
|
+
}
|
|
5426
|
+
/** The tag of an intrinsic element, or `undefined` for a component or a namespaced tag. */
|
|
5427
|
+
function readIntrinsicTag(element) {
|
|
5428
|
+
const name = element.openingElement.name;
|
|
5429
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name) ? name.name : void 0;
|
|
5430
|
+
}
|
|
5431
|
+
/**
|
|
5432
|
+
* The template rooted at `element`. Elements inside fragments join it.
|
|
5433
|
+
* Elements in expression containers or under a component start their own.
|
|
5434
|
+
*/
|
|
5435
|
+
function collectTemplate(element, tag) {
|
|
5436
|
+
const node = {
|
|
5437
|
+
element,
|
|
5438
|
+
tag,
|
|
5439
|
+
children: []
|
|
5440
|
+
};
|
|
5441
|
+
addTemplateChildren(node, element.children);
|
|
5442
|
+
return node;
|
|
5443
|
+
}
|
|
5444
|
+
function addTemplateChildren(node, children) {
|
|
5445
|
+
for (const child of children) if (child.type === "JSXFragment") addTemplateChildren(node, child.children);
|
|
5446
|
+
else if (child.type === "JSXElement") {
|
|
5447
|
+
const tag = readIntrinsicTag(child);
|
|
5448
|
+
if (tag !== void 0) node.children.push(collectTemplate(child, tag));
|
|
5449
|
+
}
|
|
5450
|
+
}
|
|
5451
|
+
/**
|
|
5452
|
+
* The tags of a template, without text or attributes. Elements not in
|
|
5453
|
+
* `included` are left out with what they hold.
|
|
5454
|
+
*/
|
|
5455
|
+
function writeTags(node, included) {
|
|
5456
|
+
if (VOID_ELEMENTS.has(node.tag)) return `<${node.tag}>`;
|
|
5457
|
+
let inner = "";
|
|
5458
|
+
for (const child of node.children) if (included.has(child)) inner += writeTags(child, included);
|
|
5459
|
+
return `<${node.tag}>${inner}</${node.tag}>`;
|
|
5460
|
+
}
|
|
5461
|
+
/**
|
|
5462
|
+
* Table parts parse only inside a table, and SVG parts only inside an
|
|
5463
|
+
* `<svg>`, so a template rooted at one is checked inside one, as Solid
|
|
5464
|
+
* creates it.
|
|
5465
|
+
*/
|
|
5466
|
+
function placeInContext(tag, html) {
|
|
5467
|
+
if (SVG_ELEMENTS.has(tag)) return `<svg>${html}</svg>`;
|
|
5468
|
+
switch (tag) {
|
|
5469
|
+
case "td":
|
|
5470
|
+
case "th": return `<table><tbody><tr>${html}</tr></tbody></table>`;
|
|
5471
|
+
case "tr": return `<table><tbody>${html}</tbody></table>`;
|
|
5472
|
+
case "col": return `<table><colgroup>${html}</colgroup></table>`;
|
|
5473
|
+
case "thead":
|
|
5474
|
+
case "tbody":
|
|
5475
|
+
case "tfoot":
|
|
5476
|
+
case "colgroup":
|
|
5477
|
+
case "caption": return `<table>${html}</table>`;
|
|
5478
|
+
default: return html;
|
|
5479
|
+
}
|
|
5480
|
+
}
|
|
5481
|
+
/** Whether a `<template>` in the template holds an element, which Solid rejects. */
|
|
5482
|
+
function hasTemplateContent(node, included) {
|
|
5483
|
+
for (const child of node.children) if (included.has(child) && (node.tag === "template" || hasTemplateContent(child, included))) return true;
|
|
5484
|
+
return false;
|
|
5485
|
+
}
|
|
5486
|
+
function isValidTemplate(root, included) {
|
|
5487
|
+
if (hasTemplateContent(root, included)) return false;
|
|
5488
|
+
const html = placeInContext(root.tag, writeTags(root, included));
|
|
5489
|
+
return parseAsBody(html).toLowerCase() === html.toLowerCase();
|
|
5490
|
+
}
|
|
5491
|
+
function collectAll(node, into) {
|
|
5492
|
+
into.add(node);
|
|
5493
|
+
for (const child of node.children) collectAll(child, into);
|
|
5494
|
+
return into;
|
|
5495
|
+
}
|
|
5496
|
+
/**
|
|
5497
|
+
* Wraps the elements that make `root`'s template parse differently, and
|
|
5498
|
+
* returns the wrapped elements, each the root of a template of its own.
|
|
5499
|
+
*/
|
|
5500
|
+
function wrapInvalidElements(context, root) {
|
|
5501
|
+
if (isValidTemplate(root, collectAll(root, /* @__PURE__ */ new Set()))) return [];
|
|
5502
|
+
const included = /* @__PURE__ */ new Set([root]);
|
|
5503
|
+
if (!isValidTemplate(root, included)) return [];
|
|
5504
|
+
const wrapped = [];
|
|
5505
|
+
const pending = [];
|
|
5506
|
+
pushChildren(pending, root);
|
|
5507
|
+
for (let node = pending.pop(); node; node = pending.pop()) {
|
|
5508
|
+
included.add(node);
|
|
5509
|
+
if (!isValidTemplate(root, included)) {
|
|
5510
|
+
included.delete(node);
|
|
5511
|
+
context.s.prependRight(node.element.start, "{");
|
|
5512
|
+
context.s.appendLeft(node.element.end, "}");
|
|
5513
|
+
wrapped.push(node);
|
|
5514
|
+
continue;
|
|
5515
|
+
}
|
|
5516
|
+
pushChildren(pending, node);
|
|
5517
|
+
}
|
|
5518
|
+
return wrapped;
|
|
5519
|
+
}
|
|
5520
|
+
/** Pushes the children of `node` so that they pop in source order. */
|
|
5521
|
+
function pushChildren(pending, node) {
|
|
5522
|
+
for (let index = node.children.length - 1; index >= 0; index -= 1) pending.push(node.children[index]);
|
|
5523
|
+
}
|
|
5524
|
+
function repairNesting(context) {
|
|
5525
|
+
const roots = [];
|
|
5526
|
+
const visit = (node, inTemplate) => {
|
|
5527
|
+
if (node.type === "JSXElement") {
|
|
5528
|
+
const tag = readIntrinsicTag(node);
|
|
5529
|
+
if (tag !== void 0 && !inTemplate) roots.push(collectTemplate(node, tag));
|
|
5530
|
+
for (const child of node.children) visit(child, tag !== void 0);
|
|
5531
|
+
for (const attribute of node.openingElement.attributes) visit(attribute, false);
|
|
5532
|
+
return;
|
|
5533
|
+
}
|
|
5534
|
+
if (node.type === "JSXFragment") {
|
|
5535
|
+
for (const child of node.children) visit(child, inTemplate);
|
|
5536
|
+
return;
|
|
5537
|
+
}
|
|
5538
|
+
forEachChild(node, (child) => {
|
|
5539
|
+
visit(child, false);
|
|
5540
|
+
});
|
|
5541
|
+
};
|
|
5542
|
+
visit(context.program, false);
|
|
5543
|
+
let changed = false;
|
|
5544
|
+
for (let root = roots.pop(); root; root = roots.pop()) {
|
|
5545
|
+
const wrapped = wrapInvalidElements(context, root);
|
|
5546
|
+
if (wrapped.length > 0) {
|
|
5547
|
+
changed = true;
|
|
5548
|
+
roots.push(...wrapped);
|
|
5549
|
+
}
|
|
5550
|
+
}
|
|
5551
|
+
return changed;
|
|
5552
|
+
}
|
|
5553
|
+
//#endregion
|
|
5554
|
+
//#region src/conditional.ts
|
|
5555
|
+
/**
|
|
5556
|
+
* Rewrites each `<Show>` and `<Switch>` that can be lowered. One inside
|
|
5557
|
+
* another that is lowered stays for the next run, since both would edit the
|
|
5558
|
+
* same text.
|
|
5559
|
+
*/
|
|
5560
|
+
function lowerConditionals(context) {
|
|
5561
|
+
let changed = false;
|
|
5562
|
+
visit(context.program);
|
|
5563
|
+
return changed;
|
|
5564
|
+
function visit(node) {
|
|
5565
|
+
if (node.type === "JSXElement") {
|
|
5566
|
+
const ternary = ternaryOf(context, node);
|
|
5567
|
+
const form = ternary === void 0 ? void 0 : formAt(context, node);
|
|
5568
|
+
if (ternary !== void 0 && form !== void 0) {
|
|
5569
|
+
context.s.overwrite(node.start, node.end, form === "container" ? joinText`{${ternary}}` : joinText`<>{${ternary}}</>`);
|
|
5570
|
+
changed = true;
|
|
5571
|
+
return;
|
|
5572
|
+
}
|
|
5573
|
+
}
|
|
5574
|
+
forEachChild(node, visit);
|
|
5575
|
+
}
|
|
5576
|
+
}
|
|
5577
|
+
/**
|
|
5578
|
+
* The Solid built-in an element's tag resolves to, by the name Solid exports it under.
|
|
5579
|
+
*/
|
|
5580
|
+
function builtInOf(context, element) {
|
|
5581
|
+
const { name } = element.openingElement;
|
|
5582
|
+
if (name.type !== "JSXIdentifier") return;
|
|
5583
|
+
const { references } = context.scopes;
|
|
5584
|
+
if (!references.has(name)) return;
|
|
5585
|
+
const binding = references.get(name);
|
|
5586
|
+
const identity = binding ? solidExport(binding, context.options) : name.name;
|
|
5587
|
+
return identity !== void 0 && context.options.builtIns.has(identity) ? identity : void 0;
|
|
5588
|
+
}
|
|
5589
|
+
/**
|
|
5590
|
+
* The ternary a `<Show>` or `<Switch>` lowers to, or `undefined` when its
|
|
5591
|
+
* props or children need the component.
|
|
5592
|
+
*/
|
|
5593
|
+
function ternaryOf(context, element) {
|
|
5594
|
+
switch (builtInOf(context, element)) {
|
|
5595
|
+
case "Show": {
|
|
5596
|
+
const branch = branchOf(context, element, ["when", "fallback"]);
|
|
5597
|
+
if (branch === void 0) return;
|
|
5598
|
+
return joinText`${branch.when} ? ${branch.children} : ${branch.fallback ?? "undefined"}`;
|
|
5599
|
+
}
|
|
5600
|
+
case "Switch": return switchTernary(context, element);
|
|
5601
|
+
default: return;
|
|
5602
|
+
}
|
|
5603
|
+
}
|
|
5604
|
+
/**
|
|
5605
|
+
* A `<Switch>` whose children are all `<Match>`es that lower, as a chain of ternaries.
|
|
5606
|
+
*/
|
|
5607
|
+
function switchTernary(context, element) {
|
|
5608
|
+
const props = propsOf(context, element, ["fallback"]);
|
|
5609
|
+
if (props === void 0) return;
|
|
5610
|
+
const chain = [];
|
|
5611
|
+
let count = 0;
|
|
5612
|
+
for (const child of significantChildren(element.children)) {
|
|
5613
|
+
if (child.type !== "JSXElement" || builtInOf(context, child) !== "Match") return;
|
|
5614
|
+
const branch = branchOf(context, child, ["when"]);
|
|
5615
|
+
if (branch === void 0) return;
|
|
5616
|
+
chain.push(branch.when, " ? ", branch.children, " : ");
|
|
5617
|
+
count += 1;
|
|
5618
|
+
}
|
|
5619
|
+
if (count === 0) return;
|
|
5620
|
+
return join([...chain, props.get("fallback") ?? "undefined"]);
|
|
5621
|
+
}
|
|
5622
|
+
/**
|
|
5623
|
+
* How a `<Show>` or `<Switch>` at this position is written once lowered, or `undefined`
|
|
5624
|
+
* when the position is not supported.
|
|
5625
|
+
*/
|
|
5626
|
+
function formAt(context, element) {
|
|
5627
|
+
const parent = context.parents.get(element);
|
|
5628
|
+
if (parent === void 0) return "fragment";
|
|
5629
|
+
switch (parent.type) {
|
|
5630
|
+
case "JSXFragment": return "container";
|
|
5631
|
+
case "JSXElement": {
|
|
5632
|
+
const { name } = parent.openingElement;
|
|
5633
|
+
if (name.type === "JSXNamespacedName" || name.type === "JSXIdentifier" && isIntrinsicTag(name.name)) return "container";
|
|
5634
|
+
return isSoleChild(parent.children, element) ? "fragment" : void 0;
|
|
5635
|
+
}
|
|
5636
|
+
case "JSXExpressionContainer":
|
|
5637
|
+
case "JSXAttribute": return;
|
|
5638
|
+
default: return isInJSXExpression(context, parent) ? void 0 : "fragment";
|
|
5639
|
+
}
|
|
5640
|
+
}
|
|
5641
|
+
/**
|
|
5642
|
+
* Whether a node is part of an expression in a JSX container, such as
|
|
5643
|
+
* `{ok && <Show />}`, where a function boundary does not separate them.
|
|
5644
|
+
*/
|
|
5645
|
+
function isInJSXExpression(context, node) {
|
|
5646
|
+
let current = node;
|
|
5647
|
+
while (current !== void 0 && !isFunctionNode(current)) {
|
|
5648
|
+
if (current.type === "JSXExpressionContainer") return true;
|
|
5649
|
+
current = context.parents.get(current);
|
|
5650
|
+
}
|
|
5651
|
+
return false;
|
|
5652
|
+
}
|
|
5653
|
+
function isSoleChild(children, element) {
|
|
5654
|
+
for (const child of children) if (child !== element && !isInsignificant(child)) return false;
|
|
5655
|
+
return true;
|
|
5656
|
+
}
|
|
5657
|
+
/**
|
|
5658
|
+
* The text of each prop of an element, or `undefined` when it has a prop
|
|
5659
|
+
* other than `names`, a spread, or a prop more than once. A repeated prop
|
|
5660
|
+
* takes its last value, and a spread could hold any prop, such as `keyed`.
|
|
5661
|
+
*/
|
|
5662
|
+
function propsOf(context, element, names) {
|
|
5663
|
+
const props = /* @__PURE__ */ new Map();
|
|
5664
|
+
for (const attribute of element.openingElement.attributes) {
|
|
5665
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || !names.includes(attribute.name.name) || props.has(attribute.name.name)) return;
|
|
5666
|
+
const text = expressionText(context, attribute);
|
|
5667
|
+
if (text === void 0) return;
|
|
5668
|
+
props.set(attribute.name.name, text);
|
|
5669
|
+
}
|
|
5670
|
+
return props;
|
|
5671
|
+
}
|
|
5672
|
+
/**
|
|
5673
|
+
* The condition and children of a `<Show>` or `<Match>`, or `undefined` when
|
|
5674
|
+
* the children could need the narrowed value.
|
|
5675
|
+
*/
|
|
5676
|
+
function branchOf(context, element, names) {
|
|
5677
|
+
const props = propsOf(context, element, names);
|
|
5678
|
+
const when = props?.get("when");
|
|
5679
|
+
if (props === void 0 || when === void 0) return;
|
|
5680
|
+
const significant = significantChildren(element.children);
|
|
5681
|
+
const sole = significant.length === 1 ? significant[0] : void 0;
|
|
5682
|
+
const callback = sole?.type === "JSXExpressionContainer" && sole.expression.type !== "JSXEmptyExpression" ? unwrap(sole.expression) : void 0;
|
|
5683
|
+
if (callback?.type === "ArrowFunctionExpression") {
|
|
5684
|
+
const body = callbackBody(context, element, callback);
|
|
5685
|
+
return body === void 0 ? void 0 : {
|
|
5686
|
+
when,
|
|
5687
|
+
children: body,
|
|
5688
|
+
fallback: props.get("fallback")
|
|
5689
|
+
};
|
|
5690
|
+
}
|
|
5691
|
+
if (sole?.type === "JSXExpressionContainer" && sole.expression.type !== "JSXEmptyExpression" && !isNeverFunction(sole.expression)) return;
|
|
5692
|
+
for (const child of significant) if (child.type === "JSXExpressionContainer" && child.expression.type !== "JSXEmptyExpression" && isFunctionNode(unwrap(child.expression))) return;
|
|
5693
|
+
const { closingElement } = element;
|
|
5694
|
+
const inner = closingElement ? context.s.slice(element.openingElement.end, closingElement.start) : "";
|
|
5695
|
+
return {
|
|
5696
|
+
when,
|
|
5697
|
+
children: significant.length > 0 ? joinText`<>${inner}</>` : "undefined",
|
|
5698
|
+
fallback: props.get("fallback")
|
|
5699
|
+
};
|
|
5700
|
+
}
|
|
5701
|
+
/**
|
|
5702
|
+
* The body of a callback child with each read of its parameter written as
|
|
5703
|
+
* `when`, or `undefined` when that could change what the body renders.
|
|
5704
|
+
*/
|
|
5705
|
+
function callbackBody(context, element, callback) {
|
|
5706
|
+
const param = callback.params.at(0);
|
|
5707
|
+
if (callback.async || callback.body.type === "BlockStatement" || callback.params.length !== 1 || param?.type !== "Identifier") return;
|
|
5708
|
+
const body = unwrap(callback.body);
|
|
5709
|
+
if (body.type !== "JSXElement" && body.type !== "JSXFragment") return;
|
|
5710
|
+
const value = findAttribute(element, "when")?.value;
|
|
5711
|
+
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression" || !isPlainRead(context, value.expression)) return;
|
|
5712
|
+
const when = value.expression;
|
|
5713
|
+
const whenText = joinText`(${context.s.slice(when.start, when.end)})`;
|
|
5714
|
+
const names = [];
|
|
5715
|
+
collectReferences(context, when, names);
|
|
5716
|
+
const binding = lookup(scopeAt(context.scopes, context.parents, param), param.name);
|
|
5717
|
+
if (binding?.identifier !== param || binding.mutated || namesInTypes(body, param.name)) return;
|
|
5718
|
+
const edits = [];
|
|
5719
|
+
for (const reference of binding.references) {
|
|
5720
|
+
if (reference.type !== "Identifier") return;
|
|
5721
|
+
const scope = scopeAt(context.scopes, context.parents, reference);
|
|
5722
|
+
for (const name of names) if (name.type !== "Identifier" || lookup(scope, name.name) !== context.scopes.references.get(name)) return;
|
|
5723
|
+
const parent = context.parents.get(reference);
|
|
5724
|
+
if (parent?.type === "CallExpression" && parent.callee === reference && parent.arguments.length === 0 && !parent.optional) edits.push({
|
|
5725
|
+
start: parent.start,
|
|
5726
|
+
end: parent.end,
|
|
5727
|
+
text: whenText
|
|
5728
|
+
});
|
|
5729
|
+
else if (parent?.type === "Property" && parent.shorthand) return;
|
|
5730
|
+
else edits.push({
|
|
5731
|
+
start: reference.start,
|
|
5732
|
+
end: reference.end,
|
|
5733
|
+
text: joinText`(() => ${whenText})`
|
|
5734
|
+
});
|
|
5735
|
+
}
|
|
5736
|
+
edits.sort((a, b) => a.start - b.start);
|
|
5737
|
+
const parts = [];
|
|
5738
|
+
let at = body.start;
|
|
5739
|
+
for (const edit of edits) {
|
|
5740
|
+
parts.push(context.s.slice(at, edit.start), edit.text);
|
|
5741
|
+
at = edit.end;
|
|
5742
|
+
}
|
|
5743
|
+
parts.push(context.s.slice(at, body.end));
|
|
5744
|
+
return join(parts);
|
|
5745
|
+
}
|
|
5746
|
+
/**
|
|
5747
|
+
* Whether reading an expression again gives the same value, with nothing run
|
|
5748
|
+
* between the reads: names, members, literals, and operators, and calls of
|
|
5749
|
+
* a signal or a memo. Another call could change something or return
|
|
5750
|
+
* something new, and `this` changes inside a `function` in the body.
|
|
5751
|
+
*/
|
|
5752
|
+
function isPlainRead(context, node) {
|
|
5753
|
+
switch (node.type) {
|
|
5754
|
+
case "Identifier":
|
|
5755
|
+
case "Literal": return true;
|
|
5756
|
+
case "ParenthesizedExpression":
|
|
5757
|
+
case "TSAsExpression":
|
|
5758
|
+
case "TSSatisfiesExpression":
|
|
5759
|
+
case "TSNonNullExpression":
|
|
5760
|
+
case "ChainExpression": return isPlainRead(context, node.expression);
|
|
5761
|
+
case "MemberExpression": return isPlainRead(context, node.object) && (!node.computed || isPlainRead(context, node.property));
|
|
5762
|
+
case "CallExpression": return node.arguments.length === 0 && isAccessor(context, node.callee);
|
|
5763
|
+
case "UnaryExpression": return node.operator !== "delete" && isPlainRead(context, node.argument);
|
|
5764
|
+
case "BinaryExpression":
|
|
5765
|
+
case "LogicalExpression": return isPlainRead(context, node.left) && isPlainRead(context, node.right);
|
|
5766
|
+
case "ConditionalExpression": return isPlainRead(context, node.test) && isPlainRead(context, node.consequent) && isPlainRead(context, node.alternate);
|
|
5767
|
+
default: return false;
|
|
5768
|
+
}
|
|
5769
|
+
}
|
|
5770
|
+
/**
|
|
5771
|
+
* Whether a callee is the getter of a `createSignal` or a `createMemo` from
|
|
5772
|
+
* Solid, declared with `const` and never written, which returns its current
|
|
5773
|
+
* value without running anything else.
|
|
5774
|
+
*/
|
|
5775
|
+
function isAccessor(context, callee) {
|
|
5776
|
+
if (callee.type !== "Identifier") return false;
|
|
5777
|
+
const binding = context.scopes.references.get(callee);
|
|
5778
|
+
if (!binding || binding.mutated || binding.kind !== "const") return false;
|
|
5779
|
+
const { declaration } = binding;
|
|
5780
|
+
if (declaration.type !== "VariableDeclarator" || !declaration.init) return false;
|
|
5781
|
+
const init = unwrap(declaration.init);
|
|
5782
|
+
if (init.type !== "CallExpression" || init.callee.type !== "Identifier") return false;
|
|
5783
|
+
const factory = context.scopes.references.get(init.callee);
|
|
5784
|
+
const name = factory ? solidExport(factory, context.options) : void 0;
|
|
5785
|
+
if (name === "createMemo") return declaration.id === binding.identifier;
|
|
5786
|
+
return name === "createSignal" && declaration.id.type === "ArrayPattern" && declaration.id.elements.at(0) === binding.identifier;
|
|
5787
|
+
}
|
|
5788
|
+
/** Whether a TypeScript type in `node`, like `typeof v`, names `name`. */
|
|
5789
|
+
function namesInTypes(node, name) {
|
|
5790
|
+
if (typeof node !== "object" || node === null) return false;
|
|
5791
|
+
if (Array.isArray(node)) {
|
|
5792
|
+
for (const item of node) if (namesInTypes(item, name)) return true;
|
|
5793
|
+
return false;
|
|
5794
|
+
}
|
|
5795
|
+
if (Reflect.get(node, "type") === "TSTypeQuery") {
|
|
5796
|
+
const exprName = Reflect.get(node, "exprName");
|
|
5797
|
+
return typeof exprName === "object" && exprName !== null && Reflect.get(exprName, "name") === name;
|
|
5798
|
+
}
|
|
5799
|
+
for (const key of Object.keys(node)) if (key !== "parent" && namesInTypes(Reflect.get(node, key), name)) return true;
|
|
5800
|
+
return false;
|
|
5801
|
+
}
|
|
5802
|
+
/** The identifiers in an expression that refer to a binding or a global. */
|
|
5803
|
+
function collectReferences(context, node, out) {
|
|
5804
|
+
if (node.type === "Identifier" && context.scopes.references.has(node)) out.push(node);
|
|
5805
|
+
forEachChild(node, (child) => {
|
|
5806
|
+
collectReferences(context, child, out);
|
|
5807
|
+
});
|
|
5808
|
+
}
|
|
5809
|
+
/**
|
|
5810
|
+
* Whether an expression's value can never be a function: a literal, JSX, or
|
|
5811
|
+
* an operator that returns a string, number, or boolean.
|
|
5812
|
+
*/
|
|
5813
|
+
function isNeverFunction(node) {
|
|
5814
|
+
switch (node.type) {
|
|
5815
|
+
case "Literal":
|
|
5816
|
+
case "TemplateLiteral":
|
|
5817
|
+
case "JSXElement":
|
|
5818
|
+
case "JSXFragment":
|
|
5819
|
+
case "BinaryExpression":
|
|
5820
|
+
case "UnaryExpression":
|
|
5821
|
+
case "UpdateExpression": return true;
|
|
5822
|
+
case "ParenthesizedExpression": return isNeverFunction(node.expression);
|
|
5823
|
+
case "ConditionalExpression": return isNeverFunction(node.consequent) && isNeverFunction(node.alternate);
|
|
5824
|
+
case "LogicalExpression": return isNeverFunction(node.left) && isNeverFunction(node.right);
|
|
5825
|
+
default: return false;
|
|
5826
|
+
}
|
|
5827
|
+
}
|
|
5828
|
+
function significantChildren(children) {
|
|
5829
|
+
const result = [];
|
|
5830
|
+
for (const child of children) if (!isInsignificant(child)) result.push(child);
|
|
5831
|
+
return result;
|
|
5832
|
+
}
|
|
5833
|
+
/**
|
|
5834
|
+
* The text of an attribute's value as an expression, or `undefined` when the
|
|
5835
|
+
* value is not supported.
|
|
5836
|
+
*/
|
|
5837
|
+
function expressionText(context, attribute) {
|
|
5838
|
+
const { value } = attribute;
|
|
5839
|
+
if (value === null) return;
|
|
5840
|
+
switch (value.type) {
|
|
5841
|
+
case "JSXExpressionContainer":
|
|
5842
|
+
if (value.expression.type === "JSXEmptyExpression") return;
|
|
5843
|
+
return joinText`(${context.s.slice(value.expression.start, value.expression.end)})`;
|
|
5844
|
+
case "JSXElement":
|
|
5845
|
+
case "JSXFragment": return context.s.slice(value.start, value.end);
|
|
5846
|
+
default: return;
|
|
5847
|
+
}
|
|
5848
|
+
}
|
|
5849
|
+
//#endregion
|
|
5850
|
+
//#region src/terminate.ts
|
|
5851
|
+
const TERMINATED = /* @__PURE__ */ new Set([
|
|
5852
|
+
"BreakStatement",
|
|
5853
|
+
"ContinueStatement",
|
|
5854
|
+
"DebuggerStatement",
|
|
5855
|
+
"DoWhileStatement",
|
|
5856
|
+
"ExportAllDeclaration",
|
|
5857
|
+
"ExpressionStatement",
|
|
5858
|
+
"ImportDeclaration",
|
|
5859
|
+
"ReturnStatement",
|
|
5860
|
+
"ThrowStatement",
|
|
5861
|
+
"TSTypeAliasDeclaration",
|
|
5862
|
+
"VariableDeclaration"
|
|
5863
|
+
]);
|
|
5864
|
+
const LOOP_HEADS = /* @__PURE__ */ new Set([
|
|
5865
|
+
"ForStatement",
|
|
5866
|
+
"ForInStatement",
|
|
5867
|
+
"ForOfStatement"
|
|
5868
|
+
]);
|
|
5869
|
+
function isTerminated(node, context) {
|
|
5870
|
+
if (node.type === "VariableDeclaration") {
|
|
5871
|
+
const parent = context.parents.get(node);
|
|
5872
|
+
return !(parent && LOOP_HEADS.has(parent.type) && Reflect.get(parent, "body") !== node);
|
|
5873
|
+
}
|
|
5874
|
+
if (node.type === "ExportNamedDeclaration") return !node.declaration;
|
|
5875
|
+
if (node.type === "ExportDefaultDeclaration") {
|
|
5876
|
+
const { type } = node.declaration;
|
|
5877
|
+
return type !== "FunctionDeclaration" && type !== "ClassDeclaration";
|
|
5878
|
+
}
|
|
5879
|
+
return TERMINATED.has(node.type);
|
|
5880
|
+
}
|
|
5881
|
+
const TRIVIA = /(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/y;
|
|
5882
|
+
/** Whether code other than the closing brace of a block follows `position`. */
|
|
5883
|
+
function hasCodeAfter(code, position) {
|
|
5884
|
+
TRIVIA.lastIndex = position;
|
|
5885
|
+
TRIVIA.exec(code);
|
|
5886
|
+
return TRIVIA.lastIndex < code.length && code[TRIVIA.lastIndex] !== "}";
|
|
5887
|
+
}
|
|
5888
|
+
function terminateStatements(context) {
|
|
5889
|
+
const { code, s } = context;
|
|
5890
|
+
let changed = false;
|
|
5891
|
+
const visit = (node) => {
|
|
5892
|
+
if (isTerminated(node, context) && code[node.end - 1] !== ";" && hasCodeAfter(code, node.end)) {
|
|
5893
|
+
s.appendLeft(node.end, ";");
|
|
5894
|
+
changed = true;
|
|
5895
|
+
}
|
|
5896
|
+
forEachChild(node, visit);
|
|
5897
|
+
};
|
|
5898
|
+
visit(context.program);
|
|
5899
|
+
return changed;
|
|
5900
|
+
}
|
|
5901
|
+
//#endregion
|
|
4386
5902
|
//#region src/compile.ts
|
|
4387
5903
|
const DEFAULT_BUILT_INS = [
|
|
4388
5904
|
"For",
|
|
@@ -4398,6 +5914,8 @@ const DEFAULT_BUILT_INS = [
|
|
|
4398
5914
|
];
|
|
4399
5915
|
const DEFAULT_MODULE_SOURCES = ["solid-js", "@solidjs/web"];
|
|
4400
5916
|
function resolveOptions(options) {
|
|
5917
|
+
const importedContexts = /* @__PURE__ */ new Map();
|
|
5918
|
+
for (const [source, names] of Object.entries(options.importedContexts ?? {})) importedContexts.set(source, new Set(names));
|
|
4401
5919
|
return {
|
|
4402
5920
|
builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
|
|
4403
5921
|
moduleSources: options.moduleSources ?? DEFAULT_MODULE_SOURCES,
|
|
@@ -4405,13 +5923,14 @@ function resolveOptions(options) {
|
|
|
4405
5923
|
constantVars: options.constantVars ?? false,
|
|
4406
5924
|
importedConstants: resolveImportedConstants(options.importedConstants),
|
|
4407
5925
|
globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
|
|
4408
|
-
importedContexts
|
|
5926
|
+
importedContexts,
|
|
4409
5927
|
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
4410
|
-
alwaysInline: options.alwaysInline ?? false
|
|
5928
|
+
alwaysInline: options.alwaysInline ?? false,
|
|
5929
|
+
alwaysInlineComponents: new Set(options.alwaysInlineComponents ?? [])
|
|
4411
5930
|
};
|
|
4412
5931
|
}
|
|
4413
5932
|
function parseCode(code, filename) {
|
|
4414
|
-
const program = parse(filename, code);
|
|
5933
|
+
const program = parse$1(filename, code);
|
|
4415
5934
|
return {
|
|
4416
5935
|
code,
|
|
4417
5936
|
program,
|
|
@@ -4424,7 +5943,7 @@ function runPass(parsed, filename, options, sourceMap, pass, skips) {
|
|
|
4424
5943
|
const context = {
|
|
4425
5944
|
code,
|
|
4426
5945
|
filename,
|
|
4427
|
-
s: new
|
|
5946
|
+
s: new Editor(code, sourceMap),
|
|
4428
5947
|
program: parsed.program,
|
|
4429
5948
|
parents: parsed.parents,
|
|
4430
5949
|
scopes: {
|
|
@@ -4437,11 +5956,7 @@ function runPass(parsed, filename, options, sourceMap, pass, skips) {
|
|
|
4437
5956
|
if (!pass(context)) return;
|
|
4438
5957
|
return {
|
|
4439
5958
|
code: context.s.toString(),
|
|
4440
|
-
map: sourceMap ? context.s.generateMap(
|
|
4441
|
-
source: filename,
|
|
4442
|
-
hires: true,
|
|
4443
|
-
includeContent: true
|
|
4444
|
-
}).toString() : void 0
|
|
5959
|
+
map: sourceMap ? context.s.generateMap(filename) : void 0
|
|
4445
5960
|
};
|
|
4446
5961
|
}
|
|
4447
5962
|
/**
|
|
@@ -4471,6 +5986,12 @@ function compile(code, options = {}) {
|
|
|
4471
5986
|
if (parsed?.code !== current) parsed = parseCode(current, filename);
|
|
4472
5987
|
return parsed;
|
|
4473
5988
|
};
|
|
5989
|
+
const terminated = runPass(parsedCurrent(), filename, resolved, sourceMap, terminateStatements, void 0);
|
|
5990
|
+
if (terminated) {
|
|
5991
|
+
current = terminated.code;
|
|
5992
|
+
if (terminated.map !== void 0) maps.push(terminated.map);
|
|
5993
|
+
}
|
|
5994
|
+
const start = current;
|
|
4474
5995
|
const maxPasses = options.maxPasses ?? 10;
|
|
4475
5996
|
let skipped;
|
|
4476
5997
|
for (let round = 0; round < maxPasses; round += 1) {
|
|
@@ -4486,7 +6007,25 @@ function compile(code, options = {}) {
|
|
|
4486
6007
|
}
|
|
4487
6008
|
if (!changed) break;
|
|
4488
6009
|
}
|
|
6010
|
+
if (options.conditionals ?? true) for (let round = 0; round < maxPasses; round += 1) {
|
|
6011
|
+
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, lowerConditionals, void 0);
|
|
6012
|
+
if (!result) break;
|
|
6013
|
+
current = result.code;
|
|
6014
|
+
if (result.map !== void 0) maps.push(result.map);
|
|
6015
|
+
}
|
|
6016
|
+
if (current !== start) {
|
|
6017
|
+
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, repairNesting, void 0);
|
|
6018
|
+
if (result) {
|
|
6019
|
+
current = result.code;
|
|
6020
|
+
if (result.map !== void 0) maps.push(result.map);
|
|
6021
|
+
}
|
|
6022
|
+
}
|
|
4489
6023
|
const report = skipped ? { skipped } : {};
|
|
6024
|
+
if (current === start) return {
|
|
6025
|
+
code,
|
|
6026
|
+
map: null,
|
|
6027
|
+
...report
|
|
6028
|
+
};
|
|
4490
6029
|
if (maps.length === 0) return {
|
|
4491
6030
|
code: current,
|
|
4492
6031
|
map: null,
|
|
@@ -4505,13 +6044,14 @@ function compile(code, options = {}) {
|
|
|
4505
6044
|
* Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
|
|
4506
6045
|
*/
|
|
4507
6046
|
function readModuleConstants(code, options = {}) {
|
|
4508
|
-
const program = parse(options.filename ?? "input.jsx", code);
|
|
6047
|
+
const program = parse$1(options.filename ?? "input.jsx", code);
|
|
6048
|
+
const scopes = analyzeScopes(program);
|
|
4509
6049
|
return {
|
|
4510
|
-
imports: namedImportSources(program),
|
|
4511
|
-
exports: constantExports(program, resolveOptions(options))
|
|
6050
|
+
imports: namedImportSources(program, scopes),
|
|
6051
|
+
exports: constantExports(program, resolveOptions(options), scopes)
|
|
4512
6052
|
};
|
|
4513
6053
|
}
|
|
4514
6054
|
//#endregion
|
|
4515
|
-
export {
|
|
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 };
|
|
4516
6056
|
|
|
4517
|
-
//# sourceMappingURL=compile-
|
|
6057
|
+
//# sourceMappingURL=compile-Cvjy2e11.mjs.map
|