solid-optimizer 1.0.0-next.4 → 1.0.0-next.6
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 +8 -3
- package/dist/{compile-Dx577-sp.mjs → compile-DnfP-Quf.mjs} +720 -157
- package/dist/compile-DnfP-Quf.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 +135 -58
- package/src/inline.ts +274 -112
- package/src/jsx.ts +13 -9
- package/src/memo.ts +209 -26
- 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-Dx577-sp.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,
|
|
@@ -118,6 +119,15 @@ const EMPTY: Fold = { kind: 'empty' };
|
|
|
118
119
|
/** Solid's calls that only create a value, so the bundler can drop an unused one. */
|
|
119
120
|
const PURE_CALLS = new Set(['lazy', 'createContext']);
|
|
120
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Solid's calls that only create a value when their first argument is not a
|
|
124
|
+
* function. Given a function, they create a computation, which runs it.
|
|
125
|
+
*/
|
|
126
|
+
const PURE_VALUE_CALLS = new Set(['createSignal', 'createStore']);
|
|
127
|
+
|
|
128
|
+
/** Expression types that are never a function. */
|
|
129
|
+
const NOT_FUNCTION = new Set(['Literal', 'TemplateLiteral', 'ObjectExpression', 'ArrayExpression']);
|
|
130
|
+
|
|
121
131
|
/**
|
|
122
132
|
* The globals worth folding. They only apply to a reference that binds to
|
|
123
133
|
* nothing, which is when the global is what runs.
|
|
@@ -202,6 +212,54 @@ function containsHoistedDeclaration(statement: Node): boolean {
|
|
|
202
212
|
return found;
|
|
203
213
|
}
|
|
204
214
|
|
|
215
|
+
/** The source text of each node. */
|
|
216
|
+
function getTexts(context: PassContext, nodes: readonly Node[]): string[] {
|
|
217
|
+
const texts: string[] = [];
|
|
218
|
+
for (const node of nodes) {
|
|
219
|
+
texts.push(getText(context, node));
|
|
220
|
+
}
|
|
221
|
+
return texts;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The children that are not whitespace-only text or an empty expression. */
|
|
225
|
+
function getSignificant(children: readonly JSXChild[]): JSXChild[] {
|
|
226
|
+
const significant: JSXChild[] = [];
|
|
227
|
+
for (const child of children) {
|
|
228
|
+
if (!isInsignificant(child)) {
|
|
229
|
+
significant.push(child);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return significant;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Whether the element has a `keyed` attribute that is not `{false}`. */
|
|
236
|
+
function hasEnabledKeyed(element: JSXElement): boolean {
|
|
237
|
+
for (const attribute of element.openingElement.attributes) {
|
|
238
|
+
if (
|
|
239
|
+
attribute.type === 'JSXAttribute' &&
|
|
240
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
241
|
+
attribute.name.name === 'keyed' &&
|
|
242
|
+
!(
|
|
243
|
+
attribute.value?.type === 'JSXExpressionContainer' &&
|
|
244
|
+
attribute.value.expression.type === 'Literal' &&
|
|
245
|
+
attribute.value.expression.value === false
|
|
246
|
+
)
|
|
247
|
+
) {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The summed size estimate of the children of a fragment spliced into a template. */
|
|
255
|
+
function estimateChildren(estimator: SizeEstimator, children: readonly JSXChild[]): number {
|
|
256
|
+
let size = 0;
|
|
257
|
+
for (const child of children) {
|
|
258
|
+
size += child.type === 'JSXElement' ? estimator.jsx(child, true) : estimator.node(child);
|
|
259
|
+
}
|
|
260
|
+
return size;
|
|
261
|
+
}
|
|
262
|
+
|
|
205
263
|
/**
|
|
206
264
|
* Whether a statement starting with `text` could join the statement before
|
|
207
265
|
* it when that one has no semicolon.
|
|
@@ -218,6 +276,15 @@ function keyName(node: Node): string | undefined {
|
|
|
218
276
|
return node.type === 'Literal' && typeof node.value === 'string' ? node.value : undefined;
|
|
219
277
|
}
|
|
220
278
|
|
|
279
|
+
/** Whether a call has no first argument, or one that is never a function. */
|
|
280
|
+
function isValueArgument(call: CallExpression): boolean {
|
|
281
|
+
if (call.arguments.length === 0) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
const first = call.arguments[0];
|
|
285
|
+
return first.type !== 'SpreadElement' && NOT_FUNCTION.has(unwrap(first).type);
|
|
286
|
+
}
|
|
287
|
+
|
|
221
288
|
type ConstantItem =
|
|
222
289
|
| { readonly kind: 'primitive'; readonly text: string }
|
|
223
290
|
| { readonly kind: 'object'; readonly keys: ReadonlyMap<string, string> };
|
|
@@ -234,14 +301,14 @@ function primitiveText(context: PassContext, node: Expression): string | undefin
|
|
|
234
301
|
}
|
|
235
302
|
switch (expression.type) {
|
|
236
303
|
case 'Literal':
|
|
237
|
-
return 'regex' in expression ? undefined :
|
|
304
|
+
return 'regex' in expression ? undefined : getText(context, expression);
|
|
238
305
|
case 'TemplateLiteral':
|
|
239
|
-
return expression.expressions.length === 0 ?
|
|
306
|
+
return expression.expressions.length === 0 ? getText(context, expression) : undefined;
|
|
240
307
|
case 'UnaryExpression':
|
|
241
308
|
return expression.operator === '-' &&
|
|
242
309
|
expression.argument.type === 'Literal' &&
|
|
243
310
|
typeof expression.argument.value === 'number'
|
|
244
|
-
?
|
|
311
|
+
? getText(context, expression)
|
|
245
312
|
: undefined;
|
|
246
313
|
default:
|
|
247
314
|
return undefined;
|
|
@@ -366,7 +433,7 @@ class Folder {
|
|
|
366
433
|
}
|
|
367
434
|
|
|
368
435
|
private replace(node: Node, text: string): void {
|
|
369
|
-
if (
|
|
436
|
+
if (getText(this.context, node) === text) {
|
|
370
437
|
return;
|
|
371
438
|
}
|
|
372
439
|
this.context.s.overwrite(node.start, node.end, text);
|
|
@@ -383,6 +450,10 @@ class Folder {
|
|
|
383
450
|
* branch can leave it unused, and the annotation lets the bundler drop it
|
|
384
451
|
* along with its dynamic import. `createContext` only creates the context,
|
|
385
452
|
* which inlining can leave unused.
|
|
453
|
+
*
|
|
454
|
+
* `createSignal` and `createStore` with a first argument that is not a
|
|
455
|
+
* function only create a node. It takes no owner and no hydration id, so
|
|
456
|
+
* dropping an unused one changes nothing else.
|
|
386
457
|
*/
|
|
387
458
|
private annotatePure(call: CallExpression): void {
|
|
388
459
|
if (call.callee.type !== 'Identifier') {
|
|
@@ -390,7 +461,10 @@ class Folder {
|
|
|
390
461
|
}
|
|
391
462
|
const binding = this.context.scopes.references.get(call.callee);
|
|
392
463
|
const identity = binding ? solidExport(binding, this.context.options) : undefined;
|
|
393
|
-
if (identity === undefined
|
|
464
|
+
if (identity === undefined) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) {
|
|
394
468
|
return;
|
|
395
469
|
}
|
|
396
470
|
const before = this.context.code.slice(0, call.start).trimEnd();
|
|
@@ -476,11 +550,11 @@ class Folder {
|
|
|
476
550
|
private keepAfter(target: Node, discarded: Expression | undefined, value: Expression): string {
|
|
477
551
|
const parts = discarded ? effectfulParts(discarded) : [];
|
|
478
552
|
if (parts.length === 0) {
|
|
479
|
-
const text =
|
|
553
|
+
const text = getText(this.context, value);
|
|
480
554
|
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
481
555
|
}
|
|
482
|
-
const texts =
|
|
483
|
-
texts.push(
|
|
556
|
+
const texts = getTexts(this.context, parts);
|
|
557
|
+
texts.push(getText(this.context, value));
|
|
484
558
|
return `(${texts.join(', ')})`;
|
|
485
559
|
}
|
|
486
560
|
|
|
@@ -679,18 +753,18 @@ class Folder {
|
|
|
679
753
|
};
|
|
680
754
|
case 'JSXElement':
|
|
681
755
|
case 'JSXFragment':
|
|
682
|
-
return { kind: 'jsx', text:
|
|
756
|
+
return { kind: 'jsx', text: getText(this.context, value) };
|
|
683
757
|
default:
|
|
684
758
|
if (value.expression.type === 'JSXEmptyExpression') {
|
|
685
759
|
return EMPTY;
|
|
686
760
|
}
|
|
687
761
|
// JSX in a container splices like JSX, so it can join the surrounding template.
|
|
688
762
|
if (value.expression.type === 'JSXElement' || value.expression.type === 'JSXFragment') {
|
|
689
|
-
return { kind: 'jsx', text:
|
|
763
|
+
return { kind: 'jsx', text: getText(this.context, value.expression) };
|
|
690
764
|
}
|
|
691
765
|
return {
|
|
692
766
|
kind: 'expression',
|
|
693
|
-
text:
|
|
767
|
+
text: getText(this.context, value.expression),
|
|
694
768
|
primary: isPrimary(value.expression),
|
|
695
769
|
};
|
|
696
770
|
}
|
|
@@ -746,19 +820,7 @@ class Folder {
|
|
|
746
820
|
* would run while the copy is made, so either one keeps the loop.
|
|
747
821
|
*/
|
|
748
822
|
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
|
-
) {
|
|
823
|
+
if (hasEnabledKeyed(element)) {
|
|
762
824
|
return undefined;
|
|
763
825
|
}
|
|
764
826
|
const byIndex = findAttribute(element, 'keyed') !== undefined;
|
|
@@ -766,7 +828,7 @@ class Folder {
|
|
|
766
828
|
if (!list) {
|
|
767
829
|
return undefined;
|
|
768
830
|
}
|
|
769
|
-
const significant = element.children
|
|
831
|
+
const significant = getSignificant(element.children);
|
|
770
832
|
const only = significant.at(0);
|
|
771
833
|
if (significant.length !== 1 || only?.type !== 'JSXExpressionContainer') {
|
|
772
834
|
return undefined;
|
|
@@ -836,10 +898,8 @@ class Folder {
|
|
|
836
898
|
private constantList(
|
|
837
899
|
expression: Expression,
|
|
838
900
|
): { items: ConstantItem[]; declaration: Node | undefined } | undefined {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
node = node.expression;
|
|
842
|
-
}
|
|
901
|
+
// `as const` and `satisfies` change nothing at runtime.
|
|
902
|
+
let node: Expression = unwrap(expression);
|
|
843
903
|
let declaration: Node | undefined;
|
|
844
904
|
if (node.type === 'Identifier') {
|
|
845
905
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -854,16 +914,17 @@ class Folder {
|
|
|
854
914
|
// Any other use could change the list, and an export can be changed by another module.
|
|
855
915
|
const statement = this.context.parents.get(binding.declaration);
|
|
856
916
|
const owner = statement ? this.context.parents.get(statement) : undefined;
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
!
|
|
860
|
-
|
|
861
|
-
|
|
917
|
+
let onlyForEach = true;
|
|
918
|
+
for (const reference of binding.references) {
|
|
919
|
+
if (!this.isForEach(reference)) {
|
|
920
|
+
onlyForEach = false;
|
|
921
|
+
break;
|
|
922
|
+
}
|
|
862
923
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
node = node.expression;
|
|
924
|
+
if (owner?.type === 'ExportNamedDeclaration' || !onlyForEach) {
|
|
925
|
+
return undefined;
|
|
866
926
|
}
|
|
927
|
+
node = unwrap(binding.declaration.init);
|
|
867
928
|
declaration =
|
|
868
929
|
binding.references.length === 1 &&
|
|
869
930
|
statement?.type === 'VariableDeclaration' &&
|
|
@@ -1043,12 +1104,7 @@ class Folder {
|
|
|
1043
1104
|
// In an element, the rows splice into its template. Elsewhere they are a fragment of templates.
|
|
1044
1105
|
const estimator = new SizeEstimator(code);
|
|
1045
1106
|
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
|
-
)
|
|
1107
|
+
? estimateChildren(estimator, init.children)
|
|
1052
1108
|
: estimator.jsx(init, false);
|
|
1053
1109
|
return unrolled <= loop;
|
|
1054
1110
|
}
|
|
@@ -1107,8 +1163,19 @@ class Folder {
|
|
|
1107
1163
|
return undefined;
|
|
1108
1164
|
}
|
|
1109
1165
|
|
|
1110
|
-
|
|
1111
|
-
|
|
1166
|
+
// Find the first true match, and whether every match before it is false.
|
|
1167
|
+
let winner = -1;
|
|
1168
|
+
let allFalse = true;
|
|
1169
|
+
for (const [index, item] of matches.entries()) {
|
|
1170
|
+
if (item.when === true) {
|
|
1171
|
+
winner = index;
|
|
1172
|
+
break;
|
|
1173
|
+
}
|
|
1174
|
+
if (item.when !== false) {
|
|
1175
|
+
allFalse = false;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
if (winner !== -1 && allFalse) {
|
|
1112
1179
|
const match = matches.at(winner);
|
|
1113
1180
|
if (!match || hasCallbackChild(match.element.children)) {
|
|
1114
1181
|
return undefined;
|
|
@@ -1116,7 +1183,7 @@ class Folder {
|
|
|
1116
1183
|
return { kind: 'children', children: match.element.children };
|
|
1117
1184
|
}
|
|
1118
1185
|
|
|
1119
|
-
if (
|
|
1186
|
+
if (winner === -1 && allFalse) {
|
|
1120
1187
|
return this.fallback(element);
|
|
1121
1188
|
}
|
|
1122
1189
|
|
|
@@ -1171,9 +1238,12 @@ class Folder {
|
|
|
1171
1238
|
if (tag === undefined) {
|
|
1172
1239
|
return undefined;
|
|
1173
1240
|
}
|
|
1174
|
-
const attributes =
|
|
1175
|
-
|
|
1176
|
-
|
|
1241
|
+
const attributes: string[] = [];
|
|
1242
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1243
|
+
if (attribute !== component) {
|
|
1244
|
+
attributes.push(getText(this.context, attribute));
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1177
1247
|
const opening = [tag, ...attributes].join(' ');
|
|
1178
1248
|
const text = element.closingElement
|
|
1179
1249
|
? `<${opening}>${innerText(this.context.s, element)}</${tag}>`
|
|
@@ -1201,7 +1271,7 @@ class Folder {
|
|
|
1201
1271
|
case 'expression':
|
|
1202
1272
|
return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
1203
1273
|
default: {
|
|
1204
|
-
const significant = resolved.children
|
|
1274
|
+
const significant = getSignificant(resolved.children);
|
|
1205
1275
|
const only = significant.at(0);
|
|
1206
1276
|
if (only === undefined) {
|
|
1207
1277
|
return 'null';
|
|
@@ -1210,7 +1280,7 @@ class Folder {
|
|
|
1210
1280
|
significant.length === 1 &&
|
|
1211
1281
|
(only.type === 'JSXElement' || only.type === 'JSXFragment')
|
|
1212
1282
|
) {
|
|
1213
|
-
return
|
|
1283
|
+
return getText(this.context, only);
|
|
1214
1284
|
}
|
|
1215
1285
|
const first = resolved.children.at(0);
|
|
1216
1286
|
const last = resolved.children.at(-1);
|
|
@@ -1354,12 +1424,12 @@ class Folder {
|
|
|
1354
1424
|
}
|
|
1355
1425
|
switch (node.type) {
|
|
1356
1426
|
case 'Literal':
|
|
1357
|
-
return 'regex' in node ? undefined :
|
|
1427
|
+
return 'regex' in node ? undefined : getText(this.context, node);
|
|
1358
1428
|
case 'TemplateLiteral':
|
|
1359
|
-
return node.expressions.length === 0 ?
|
|
1429
|
+
return node.expressions.length === 0 ? getText(this.context, node) : undefined;
|
|
1360
1430
|
case 'UnaryExpression':
|
|
1361
1431
|
return node.operator === '-' && node.argument.type === 'Literal'
|
|
1362
|
-
? `(${
|
|
1432
|
+
? `(${getText(this.context, node)})`
|
|
1363
1433
|
: undefined;
|
|
1364
1434
|
case 'Identifier': {
|
|
1365
1435
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -1399,7 +1469,14 @@ class Folder {
|
|
|
1399
1469
|
const unreachable = statements.slice(terminator + 1);
|
|
1400
1470
|
const first = unreachable.at(0);
|
|
1401
1471
|
const last = unreachable.at(-1);
|
|
1402
|
-
|
|
1472
|
+
let hoists = false;
|
|
1473
|
+
for (const statement of unreachable) {
|
|
1474
|
+
if (containsHoistedDeclaration(statement)) {
|
|
1475
|
+
hoists = true;
|
|
1476
|
+
break;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
if (first && last && !hoists) {
|
|
1403
1480
|
this.context.s.remove(first.start, last.end);
|
|
1404
1481
|
this.changed = true;
|
|
1405
1482
|
}
|
|
@@ -1424,7 +1501,7 @@ class Folder {
|
|
|
1424
1501
|
if (first === undefined) {
|
|
1425
1502
|
return undefined;
|
|
1426
1503
|
}
|
|
1427
|
-
const text =
|
|
1504
|
+
const text = getTexts(this.context, parts).join(', ');
|
|
1428
1505
|
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
1429
1506
|
}
|
|
1430
1507
|
|
|
@@ -1453,7 +1530,7 @@ class Folder {
|
|
|
1453
1530
|
parts.push(effect);
|
|
1454
1531
|
}
|
|
1455
1532
|
if (kept) {
|
|
1456
|
-
parts.push(
|
|
1533
|
+
parts.push(getText(this.context, kept));
|
|
1457
1534
|
}
|
|
1458
1535
|
this.replaceStatement(statement, parts.join(' '));
|
|
1459
1536
|
return kept;
|