solid-optimizer 1.0.0-next.3 → 1.0.0-next.5
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 +2 -1
- package/dist/{compile-sCZdwEKG.mjs → compile-By9Fecbx.mjs} +612 -148
- package/dist/compile-By9Fecbx.mjs.map +1 -0
- package/dist/{compile-CwH9mvHD.d.mts → compile-X_SMDFr2.d.mts} +9 -1
- package/dist/compile-X_SMDFr2.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +2 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +195 -74
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +4 -3
- package/src/compile.ts +39 -6
- package/src/conditional.ts +540 -0
- package/src/constants.ts +9 -5
- package/src/context.ts +1 -1
- package/src/fold.ts +109 -57
- package/src/inline.ts +276 -113
- package/src/jsx.ts +13 -9
- package/src/memo.ts +13 -13
- package/src/provider.ts +62 -40
- package/src/size.ts +6 -1
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +169 -66
- package/src/vite/inject.ts +27 -15
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-CwH9mvHD.d.mts.map +0 -1
- package/dist/compile-sCZdwEKG.mjs.map +0 -1
package/src/context.ts
CHANGED
|
@@ -116,6 +116,6 @@ export function recordSkip(
|
|
|
116
116
|
/**
|
|
117
117
|
* The current text of `node`, including the edits made inside it so far.
|
|
118
118
|
*/
|
|
119
|
-
export function
|
|
119
|
+
export function getText(context: PassContext, node: Node): string {
|
|
120
120
|
return context.s.slice(node.start, node.end);
|
|
121
121
|
}
|
package/src/fold.ts
CHANGED
|
@@ -62,10 +62,11 @@ import {
|
|
|
62
62
|
needsParensAt,
|
|
63
63
|
parse,
|
|
64
64
|
startsLikeStatement,
|
|
65
|
+
unwrap,
|
|
65
66
|
} from './ast';
|
|
66
67
|
import type { PassContext } from './context';
|
|
67
68
|
import { ConstantTable } from './constants';
|
|
68
|
-
import { isConstantDeclaration, solidExport
|
|
69
|
+
import { getText, isConstantDeclaration, solidExport } from './context';
|
|
69
70
|
import type { Piece } from './jsx';
|
|
70
71
|
import {
|
|
71
72
|
findAttribute,
|
|
@@ -202,6 +203,54 @@ function containsHoistedDeclaration(statement: Node): boolean {
|
|
|
202
203
|
return found;
|
|
203
204
|
}
|
|
204
205
|
|
|
206
|
+
/** The source text of each node. */
|
|
207
|
+
function getTexts(context: PassContext, nodes: readonly Node[]): string[] {
|
|
208
|
+
const texts: string[] = [];
|
|
209
|
+
for (const node of nodes) {
|
|
210
|
+
texts.push(getText(context, node));
|
|
211
|
+
}
|
|
212
|
+
return texts;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The children that are not whitespace-only text or an empty expression. */
|
|
216
|
+
function getSignificant(children: readonly JSXChild[]): JSXChild[] {
|
|
217
|
+
const significant: JSXChild[] = [];
|
|
218
|
+
for (const child of children) {
|
|
219
|
+
if (!isInsignificant(child)) {
|
|
220
|
+
significant.push(child);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return significant;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Whether the element has a `keyed` attribute that is not `{false}`. */
|
|
227
|
+
function hasEnabledKeyed(element: JSXElement): boolean {
|
|
228
|
+
for (const attribute of element.openingElement.attributes) {
|
|
229
|
+
if (
|
|
230
|
+
attribute.type === 'JSXAttribute' &&
|
|
231
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
232
|
+
attribute.name.name === 'keyed' &&
|
|
233
|
+
!(
|
|
234
|
+
attribute.value?.type === 'JSXExpressionContainer' &&
|
|
235
|
+
attribute.value.expression.type === 'Literal' &&
|
|
236
|
+
attribute.value.expression.value === false
|
|
237
|
+
)
|
|
238
|
+
) {
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The summed size estimate of the children of a fragment spliced into a template. */
|
|
246
|
+
function estimateChildren(estimator: SizeEstimator, children: readonly JSXChild[]): number {
|
|
247
|
+
let size = 0;
|
|
248
|
+
for (const child of children) {
|
|
249
|
+
size += child.type === 'JSXElement' ? estimator.jsx(child, true) : estimator.node(child);
|
|
250
|
+
}
|
|
251
|
+
return size;
|
|
252
|
+
}
|
|
253
|
+
|
|
205
254
|
/**
|
|
206
255
|
* Whether a statement starting with `text` could join the statement before
|
|
207
256
|
* it when that one has no semicolon.
|
|
@@ -234,14 +283,14 @@ function primitiveText(context: PassContext, node: Expression): string | undefin
|
|
|
234
283
|
}
|
|
235
284
|
switch (expression.type) {
|
|
236
285
|
case 'Literal':
|
|
237
|
-
return 'regex' in expression ? undefined :
|
|
286
|
+
return 'regex' in expression ? undefined : getText(context, expression);
|
|
238
287
|
case 'TemplateLiteral':
|
|
239
|
-
return expression.expressions.length === 0 ?
|
|
288
|
+
return expression.expressions.length === 0 ? getText(context, expression) : undefined;
|
|
240
289
|
case 'UnaryExpression':
|
|
241
290
|
return expression.operator === '-' &&
|
|
242
291
|
expression.argument.type === 'Literal' &&
|
|
243
292
|
typeof expression.argument.value === 'number'
|
|
244
|
-
?
|
|
293
|
+
? getText(context, expression)
|
|
245
294
|
: undefined;
|
|
246
295
|
default:
|
|
247
296
|
return undefined;
|
|
@@ -366,7 +415,7 @@ class Folder {
|
|
|
366
415
|
}
|
|
367
416
|
|
|
368
417
|
private replace(node: Node, text: string): void {
|
|
369
|
-
if (
|
|
418
|
+
if (getText(this.context, node) === text) {
|
|
370
419
|
return;
|
|
371
420
|
}
|
|
372
421
|
this.context.s.overwrite(node.start, node.end, text);
|
|
@@ -476,11 +525,11 @@ class Folder {
|
|
|
476
525
|
private keepAfter(target: Node, discarded: Expression | undefined, value: Expression): string {
|
|
477
526
|
const parts = discarded ? effectfulParts(discarded) : [];
|
|
478
527
|
if (parts.length === 0) {
|
|
479
|
-
const text =
|
|
528
|
+
const text = getText(this.context, value);
|
|
480
529
|
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
481
530
|
}
|
|
482
|
-
const texts =
|
|
483
|
-
texts.push(
|
|
531
|
+
const texts = getTexts(this.context, parts);
|
|
532
|
+
texts.push(getText(this.context, value));
|
|
484
533
|
return `(${texts.join(', ')})`;
|
|
485
534
|
}
|
|
486
535
|
|
|
@@ -679,18 +728,18 @@ class Folder {
|
|
|
679
728
|
};
|
|
680
729
|
case 'JSXElement':
|
|
681
730
|
case 'JSXFragment':
|
|
682
|
-
return { kind: 'jsx', text:
|
|
731
|
+
return { kind: 'jsx', text: getText(this.context, value) };
|
|
683
732
|
default:
|
|
684
733
|
if (value.expression.type === 'JSXEmptyExpression') {
|
|
685
734
|
return EMPTY;
|
|
686
735
|
}
|
|
687
736
|
// JSX in a container splices like JSX, so it can join the surrounding template.
|
|
688
737
|
if (value.expression.type === 'JSXElement' || value.expression.type === 'JSXFragment') {
|
|
689
|
-
return { kind: 'jsx', text:
|
|
738
|
+
return { kind: 'jsx', text: getText(this.context, value.expression) };
|
|
690
739
|
}
|
|
691
740
|
return {
|
|
692
741
|
kind: 'expression',
|
|
693
|
-
text:
|
|
742
|
+
text: getText(this.context, value.expression),
|
|
694
743
|
primary: isPrimary(value.expression),
|
|
695
744
|
};
|
|
696
745
|
}
|
|
@@ -746,19 +795,7 @@ class Folder {
|
|
|
746
795
|
* would run while the copy is made, so either one keeps the loop.
|
|
747
796
|
*/
|
|
748
797
|
private unrollFor(element: JSXElement, expression: Expression): Fold | undefined {
|
|
749
|
-
if (
|
|
750
|
-
element.openingElement.attributes.some(
|
|
751
|
-
(attribute) =>
|
|
752
|
-
attribute.type === 'JSXAttribute' &&
|
|
753
|
-
attribute.name.type === 'JSXIdentifier' &&
|
|
754
|
-
attribute.name.name === 'keyed' &&
|
|
755
|
-
!(
|
|
756
|
-
attribute.value?.type === 'JSXExpressionContainer' &&
|
|
757
|
-
attribute.value.expression.type === 'Literal' &&
|
|
758
|
-
attribute.value.expression.value === false
|
|
759
|
-
),
|
|
760
|
-
)
|
|
761
|
-
) {
|
|
798
|
+
if (hasEnabledKeyed(element)) {
|
|
762
799
|
return undefined;
|
|
763
800
|
}
|
|
764
801
|
const byIndex = findAttribute(element, 'keyed') !== undefined;
|
|
@@ -766,7 +803,7 @@ class Folder {
|
|
|
766
803
|
if (!list) {
|
|
767
804
|
return undefined;
|
|
768
805
|
}
|
|
769
|
-
const significant = element.children
|
|
806
|
+
const significant = getSignificant(element.children);
|
|
770
807
|
const only = significant.at(0);
|
|
771
808
|
if (significant.length !== 1 || only?.type !== 'JSXExpressionContainer') {
|
|
772
809
|
return undefined;
|
|
@@ -836,10 +873,8 @@ class Folder {
|
|
|
836
873
|
private constantList(
|
|
837
874
|
expression: Expression,
|
|
838
875
|
): { items: ConstantItem[]; declaration: Node | undefined } | undefined {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
node = node.expression;
|
|
842
|
-
}
|
|
876
|
+
// `as const` and `satisfies` change nothing at runtime.
|
|
877
|
+
let node: Expression = unwrap(expression);
|
|
843
878
|
let declaration: Node | undefined;
|
|
844
879
|
if (node.type === 'Identifier') {
|
|
845
880
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -854,16 +889,17 @@ class Folder {
|
|
|
854
889
|
// Any other use could change the list, and an export can be changed by another module.
|
|
855
890
|
const statement = this.context.parents.get(binding.declaration);
|
|
856
891
|
const owner = statement ? this.context.parents.get(statement) : undefined;
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
!
|
|
860
|
-
|
|
861
|
-
|
|
892
|
+
let onlyForEach = true;
|
|
893
|
+
for (const reference of binding.references) {
|
|
894
|
+
if (!this.isForEach(reference)) {
|
|
895
|
+
onlyForEach = false;
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
862
898
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
node = node.expression;
|
|
899
|
+
if (owner?.type === 'ExportNamedDeclaration' || !onlyForEach) {
|
|
900
|
+
return undefined;
|
|
866
901
|
}
|
|
902
|
+
node = unwrap(binding.declaration.init);
|
|
867
903
|
declaration =
|
|
868
904
|
binding.references.length === 1 &&
|
|
869
905
|
statement?.type === 'VariableDeclaration' &&
|
|
@@ -1043,12 +1079,7 @@ class Folder {
|
|
|
1043
1079
|
// In an element, the rows splice into its template. Elsewhere they are a fragment of templates.
|
|
1044
1080
|
const estimator = new SizeEstimator(code);
|
|
1045
1081
|
const unrolled = inTemplate
|
|
1046
|
-
? init.children
|
|
1047
|
-
(size, child) =>
|
|
1048
|
-
size +
|
|
1049
|
-
(child.type === 'JSXElement' ? estimator.jsx(child, true) : estimator.node(child)),
|
|
1050
|
-
0,
|
|
1051
|
-
)
|
|
1082
|
+
? estimateChildren(estimator, init.children)
|
|
1052
1083
|
: estimator.jsx(init, false);
|
|
1053
1084
|
return unrolled <= loop;
|
|
1054
1085
|
}
|
|
@@ -1107,8 +1138,19 @@ class Folder {
|
|
|
1107
1138
|
return undefined;
|
|
1108
1139
|
}
|
|
1109
1140
|
|
|
1110
|
-
|
|
1111
|
-
|
|
1141
|
+
// Find the first true match, and whether every match before it is false.
|
|
1142
|
+
let winner = -1;
|
|
1143
|
+
let allFalse = true;
|
|
1144
|
+
for (const [index, item] of matches.entries()) {
|
|
1145
|
+
if (item.when === true) {
|
|
1146
|
+
winner = index;
|
|
1147
|
+
break;
|
|
1148
|
+
}
|
|
1149
|
+
if (item.when !== false) {
|
|
1150
|
+
allFalse = false;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
if (winner !== -1 && allFalse) {
|
|
1112
1154
|
const match = matches.at(winner);
|
|
1113
1155
|
if (!match || hasCallbackChild(match.element.children)) {
|
|
1114
1156
|
return undefined;
|
|
@@ -1116,7 +1158,7 @@ class Folder {
|
|
|
1116
1158
|
return { kind: 'children', children: match.element.children };
|
|
1117
1159
|
}
|
|
1118
1160
|
|
|
1119
|
-
if (
|
|
1161
|
+
if (winner === -1 && allFalse) {
|
|
1120
1162
|
return this.fallback(element);
|
|
1121
1163
|
}
|
|
1122
1164
|
|
|
@@ -1171,9 +1213,12 @@ class Folder {
|
|
|
1171
1213
|
if (tag === undefined) {
|
|
1172
1214
|
return undefined;
|
|
1173
1215
|
}
|
|
1174
|
-
const attributes =
|
|
1175
|
-
|
|
1176
|
-
|
|
1216
|
+
const attributes: string[] = [];
|
|
1217
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1218
|
+
if (attribute !== component) {
|
|
1219
|
+
attributes.push(getText(this.context, attribute));
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1177
1222
|
const opening = [tag, ...attributes].join(' ');
|
|
1178
1223
|
const text = element.closingElement
|
|
1179
1224
|
? `<${opening}>${innerText(this.context.s, element)}</${tag}>`
|
|
@@ -1201,7 +1246,7 @@ class Folder {
|
|
|
1201
1246
|
case 'expression':
|
|
1202
1247
|
return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
1203
1248
|
default: {
|
|
1204
|
-
const significant = resolved.children
|
|
1249
|
+
const significant = getSignificant(resolved.children);
|
|
1205
1250
|
const only = significant.at(0);
|
|
1206
1251
|
if (only === undefined) {
|
|
1207
1252
|
return 'null';
|
|
@@ -1210,7 +1255,7 @@ class Folder {
|
|
|
1210
1255
|
significant.length === 1 &&
|
|
1211
1256
|
(only.type === 'JSXElement' || only.type === 'JSXFragment')
|
|
1212
1257
|
) {
|
|
1213
|
-
return
|
|
1258
|
+
return getText(this.context, only);
|
|
1214
1259
|
}
|
|
1215
1260
|
const first = resolved.children.at(0);
|
|
1216
1261
|
const last = resolved.children.at(-1);
|
|
@@ -1354,12 +1399,12 @@ class Folder {
|
|
|
1354
1399
|
}
|
|
1355
1400
|
switch (node.type) {
|
|
1356
1401
|
case 'Literal':
|
|
1357
|
-
return 'regex' in node ? undefined :
|
|
1402
|
+
return 'regex' in node ? undefined : getText(this.context, node);
|
|
1358
1403
|
case 'TemplateLiteral':
|
|
1359
|
-
return node.expressions.length === 0 ?
|
|
1404
|
+
return node.expressions.length === 0 ? getText(this.context, node) : undefined;
|
|
1360
1405
|
case 'UnaryExpression':
|
|
1361
1406
|
return node.operator === '-' && node.argument.type === 'Literal'
|
|
1362
|
-
? `(${
|
|
1407
|
+
? `(${getText(this.context, node)})`
|
|
1363
1408
|
: undefined;
|
|
1364
1409
|
case 'Identifier': {
|
|
1365
1410
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -1399,7 +1444,14 @@ class Folder {
|
|
|
1399
1444
|
const unreachable = statements.slice(terminator + 1);
|
|
1400
1445
|
const first = unreachable.at(0);
|
|
1401
1446
|
const last = unreachable.at(-1);
|
|
1402
|
-
|
|
1447
|
+
let hoists = false;
|
|
1448
|
+
for (const statement of unreachable) {
|
|
1449
|
+
if (containsHoistedDeclaration(statement)) {
|
|
1450
|
+
hoists = true;
|
|
1451
|
+
break;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
if (first && last && !hoists) {
|
|
1403
1455
|
this.context.s.remove(first.start, last.end);
|
|
1404
1456
|
this.changed = true;
|
|
1405
1457
|
}
|
|
@@ -1424,7 +1476,7 @@ class Folder {
|
|
|
1424
1476
|
if (first === undefined) {
|
|
1425
1477
|
return undefined;
|
|
1426
1478
|
}
|
|
1427
|
-
const text =
|
|
1479
|
+
const text = getTexts(this.context, parts).join(', ');
|
|
1428
1480
|
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
1429
1481
|
}
|
|
1430
1482
|
|
|
@@ -1453,7 +1505,7 @@ class Folder {
|
|
|
1453
1505
|
parts.push(effect);
|
|
1454
1506
|
}
|
|
1455
1507
|
if (kept) {
|
|
1456
|
-
parts.push(
|
|
1508
|
+
parts.push(getText(this.context, kept));
|
|
1457
1509
|
}
|
|
1458
1510
|
this.replaceStatement(statement, parts.join(' '));
|
|
1459
1511
|
return kept;
|