solid-optimizer 1.0.0-next.1 → 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-BpoiBa6b.mjs → compile-Cvjy2e11.mjs} +1910 -387
- 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 +5 -2
- package/src/ast.ts +4 -3
- package/src/compile.ts +93 -14
- package/src/conditional.ts +547 -0
- package/src/constants.ts +31 -5
- 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-BpoiBa6b.mjs.map +0 -1
package/src/fold.ts
CHANGED
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
* - Statements a constant condition makes unreachable: `if`/`else` branches,
|
|
19
19
|
* `while (false)` loops, and anything after a `return`, `throw`, `break`,
|
|
20
20
|
* or `continue`.
|
|
21
|
+
* - A `const`, or a `let` nothing writes to, declared inside a function,
|
|
22
|
+
* that nothing references and whose initializer has no side effects. A
|
|
23
|
+
* component left with such a declaration has a statement, which stops it
|
|
24
|
+
* from inlining where a statement has no place to go.
|
|
21
25
|
* - A call of an arrow function whose body is JSX, with constant arguments,
|
|
22
26
|
* as an element's child. The call becomes the JSX with the arguments in
|
|
23
27
|
* place of the parameters, so it joins the template around it.
|
|
@@ -49,9 +53,9 @@ import type {
|
|
|
49
53
|
LogicalExpression,
|
|
50
54
|
Node,
|
|
51
55
|
Statement,
|
|
56
|
+
VariableDeclaration,
|
|
52
57
|
WhileStatement,
|
|
53
58
|
} from 'oxc-parser';
|
|
54
|
-
import MagicString from 'magic-string';
|
|
55
59
|
import type { Parents } from './ast';
|
|
56
60
|
import {
|
|
57
61
|
forEachChild,
|
|
@@ -62,10 +66,11 @@ import {
|
|
|
62
66
|
needsParensAt,
|
|
63
67
|
parse,
|
|
64
68
|
startsLikeStatement,
|
|
69
|
+
unwrap,
|
|
65
70
|
} from './ast';
|
|
66
71
|
import type { PassContext } from './context';
|
|
67
72
|
import { ConstantTable } from './constants';
|
|
68
|
-
import { isConstantDeclaration, solidExport
|
|
73
|
+
import { getText, isConstantDeclaration, solidExport } from './context';
|
|
69
74
|
import type { Piece } from './jsx';
|
|
70
75
|
import {
|
|
71
76
|
findAttribute,
|
|
@@ -80,7 +85,7 @@ import {
|
|
|
80
85
|
import type { Binding } from './scope';
|
|
81
86
|
import { lookup, scopeAt } from './scope';
|
|
82
87
|
import { SizeEstimator, compactLength } from './size';
|
|
83
|
-
import type { ConstantLookup } from './value';
|
|
88
|
+
import type { Const, ConstantLookup } from './value';
|
|
84
89
|
import {
|
|
85
90
|
arrayLiteralLength,
|
|
86
91
|
effectfulParts,
|
|
@@ -90,6 +95,8 @@ import {
|
|
|
90
95
|
literalText,
|
|
91
96
|
truthiness,
|
|
92
97
|
} from './value';
|
|
98
|
+
import type { Content, Editor, Text } from './text';
|
|
99
|
+
import { getCode, join, joinText } from './text';
|
|
93
100
|
|
|
94
101
|
/**
|
|
95
102
|
* The control-flow components this pass resolves.
|
|
@@ -104,11 +111,11 @@ type Fold =
|
|
|
104
111
|
/** JSX children, spliced in. */
|
|
105
112
|
| { readonly kind: 'children'; readonly children: readonly JSXChild[] }
|
|
106
113
|
/** An element or fragment, as source text. */
|
|
107
|
-
| { readonly kind: 'jsx'; readonly text:
|
|
114
|
+
| { readonly kind: 'jsx'; readonly text: Content }
|
|
108
115
|
/** An expression, such as a `fallback` prop's value. */
|
|
109
116
|
| {
|
|
110
117
|
readonly kind: 'expression';
|
|
111
|
-
readonly text:
|
|
118
|
+
readonly text: Content;
|
|
112
119
|
readonly primary: boolean;
|
|
113
120
|
}
|
|
114
121
|
| { readonly kind: 'empty' };
|
|
@@ -118,6 +125,15 @@ const EMPTY: Fold = { kind: 'empty' };
|
|
|
118
125
|
/** Solid's calls that only create a value, so the bundler can drop an unused one. */
|
|
119
126
|
const PURE_CALLS = new Set(['lazy', 'createContext']);
|
|
120
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Solid's calls that only create a value when their first argument is not a
|
|
130
|
+
* function. Given a function, they create a computation, which runs it.
|
|
131
|
+
*/
|
|
132
|
+
const PURE_VALUE_CALLS = new Set(['createSignal', 'createStore']);
|
|
133
|
+
|
|
134
|
+
/** Expression types that are never a function. */
|
|
135
|
+
const NOT_FUNCTION = new Set(['Literal', 'TemplateLiteral', 'ObjectExpression', 'ArrayExpression']);
|
|
136
|
+
|
|
121
137
|
/**
|
|
122
138
|
* The globals worth folding. They only apply to a reference that binds to
|
|
123
139
|
* nothing, which is when the global is what runs.
|
|
@@ -202,6 +218,54 @@ function containsHoistedDeclaration(statement: Node): boolean {
|
|
|
202
218
|
return found;
|
|
203
219
|
}
|
|
204
220
|
|
|
221
|
+
/** The source text of each node. */
|
|
222
|
+
function getTexts(context: PassContext, nodes: readonly Node[]): Text[] {
|
|
223
|
+
const texts: Text[] = [];
|
|
224
|
+
for (const node of nodes) {
|
|
225
|
+
texts.push(getText(context, node));
|
|
226
|
+
}
|
|
227
|
+
return texts;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The children that are not whitespace-only text or an empty expression. */
|
|
231
|
+
function getSignificant(children: readonly JSXChild[]): JSXChild[] {
|
|
232
|
+
const significant: JSXChild[] = [];
|
|
233
|
+
for (const child of children) {
|
|
234
|
+
if (!isInsignificant(child)) {
|
|
235
|
+
significant.push(child);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return significant;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Whether the element has a `keyed` attribute that is not `{false}`. */
|
|
242
|
+
function hasEnabledKeyed(element: JSXElement): boolean {
|
|
243
|
+
for (const attribute of element.openingElement.attributes) {
|
|
244
|
+
if (
|
|
245
|
+
attribute.type === 'JSXAttribute' &&
|
|
246
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
247
|
+
attribute.name.name === 'keyed' &&
|
|
248
|
+
!(
|
|
249
|
+
attribute.value?.type === 'JSXExpressionContainer' &&
|
|
250
|
+
attribute.value.expression.type === 'Literal' &&
|
|
251
|
+
attribute.value.expression.value === false
|
|
252
|
+
)
|
|
253
|
+
) {
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** The summed size estimate of the children of a fragment spliced into a template. */
|
|
261
|
+
function estimateChildren(estimator: SizeEstimator, children: readonly JSXChild[]): number {
|
|
262
|
+
let size = 0;
|
|
263
|
+
for (const child of children) {
|
|
264
|
+
size += child.type === 'JSXElement' ? estimator.jsx(child, true) : estimator.node(child);
|
|
265
|
+
}
|
|
266
|
+
return size;
|
|
267
|
+
}
|
|
268
|
+
|
|
205
269
|
/**
|
|
206
270
|
* Whether a statement starting with `text` could join the statement before
|
|
207
271
|
* it when that one has no semicolon.
|
|
@@ -218,30 +282,39 @@ function keyName(node: Node): string | undefined {
|
|
|
218
282
|
return node.type === 'Literal' && typeof node.value === 'string' ? node.value : undefined;
|
|
219
283
|
}
|
|
220
284
|
|
|
285
|
+
/** Whether a call has no first argument, or one that is never a function. */
|
|
286
|
+
function isValueArgument(call: CallExpression): boolean {
|
|
287
|
+
if (call.arguments.length === 0) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
const first = call.arguments[0];
|
|
291
|
+
return first.type !== 'SpreadElement' && NOT_FUNCTION.has(unwrap(first).type);
|
|
292
|
+
}
|
|
293
|
+
|
|
221
294
|
type ConstantItem =
|
|
222
|
-
| { readonly kind: 'primitive'; readonly text:
|
|
223
|
-
| { readonly kind: 'object'; readonly keys: ReadonlyMap<string,
|
|
295
|
+
| { readonly kind: 'primitive'; readonly text: Text }
|
|
296
|
+
| { readonly kind: 'object'; readonly keys: ReadonlyMap<string, Text> };
|
|
224
297
|
|
|
225
298
|
/**
|
|
226
299
|
* The text of an expression that always gives the same primitive: a literal
|
|
227
300
|
* other than a regular expression, a template literal without expressions,
|
|
228
301
|
* or a negated number.
|
|
229
302
|
*/
|
|
230
|
-
function primitiveText(context: PassContext, node: Expression):
|
|
303
|
+
function primitiveText(context: PassContext, node: Expression): Text | undefined {
|
|
231
304
|
let expression: Expression = node;
|
|
232
305
|
while (expression.type === 'ParenthesizedExpression') {
|
|
233
306
|
expression = expression.expression;
|
|
234
307
|
}
|
|
235
308
|
switch (expression.type) {
|
|
236
309
|
case 'Literal':
|
|
237
|
-
return 'regex' in expression ? undefined :
|
|
310
|
+
return 'regex' in expression ? undefined : getText(context, expression);
|
|
238
311
|
case 'TemplateLiteral':
|
|
239
|
-
return expression.expressions.length === 0 ?
|
|
312
|
+
return expression.expressions.length === 0 ? getText(context, expression) : undefined;
|
|
240
313
|
case 'UnaryExpression':
|
|
241
314
|
return expression.operator === '-' &&
|
|
242
315
|
expression.argument.type === 'Literal' &&
|
|
243
316
|
typeof expression.argument.value === 'number'
|
|
244
|
-
?
|
|
317
|
+
? getText(context, expression)
|
|
245
318
|
: undefined;
|
|
246
319
|
default:
|
|
247
320
|
return undefined;
|
|
@@ -287,6 +360,9 @@ class Folder {
|
|
|
287
360
|
|
|
288
361
|
private readonly lookup: ConstantLookup;
|
|
289
362
|
|
|
363
|
+
/** Declarations removed before the walk, which it does not enter. */
|
|
364
|
+
private readonly removed = new Set<Node>();
|
|
365
|
+
|
|
290
366
|
constructor(private readonly context: PassContext) {
|
|
291
367
|
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
292
368
|
}
|
|
@@ -296,6 +372,9 @@ class Folder {
|
|
|
296
372
|
// ---------------------------------------------------------------------------
|
|
297
373
|
|
|
298
374
|
walk(node: Node): void {
|
|
375
|
+
if (this.removed.has(node)) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
299
378
|
forEachChild(node, (child) => {
|
|
300
379
|
this.walk(child);
|
|
301
380
|
});
|
|
@@ -351,6 +430,7 @@ class Folder {
|
|
|
351
430
|
break;
|
|
352
431
|
case 'CallExpression':
|
|
353
432
|
this.annotatePure(node);
|
|
433
|
+
this.foldUntrack(node);
|
|
354
434
|
break;
|
|
355
435
|
default:
|
|
356
436
|
break;
|
|
@@ -364,8 +444,8 @@ class Folder {
|
|
|
364
444
|
);
|
|
365
445
|
}
|
|
366
446
|
|
|
367
|
-
private replace(node: Node, text:
|
|
368
|
-
if (
|
|
447
|
+
private replace(node: Node, text: Content): void {
|
|
448
|
+
if (getText(this.context, node).code === getCode(text)) {
|
|
369
449
|
return;
|
|
370
450
|
}
|
|
371
451
|
this.context.s.overwrite(node.start, node.end, text);
|
|
@@ -382,6 +462,10 @@ class Folder {
|
|
|
382
462
|
* branch can leave it unused, and the annotation lets the bundler drop it
|
|
383
463
|
* along with its dynamic import. `createContext` only creates the context,
|
|
384
464
|
* which inlining can leave unused.
|
|
465
|
+
*
|
|
466
|
+
* `createSignal` and `createStore` with a first argument that is not a
|
|
467
|
+
* function only create a node. It takes no owner and no hydration id, so
|
|
468
|
+
* dropping an unused one changes nothing else.
|
|
385
469
|
*/
|
|
386
470
|
private annotatePure(call: CallExpression): void {
|
|
387
471
|
if (call.callee.type !== 'Identifier') {
|
|
@@ -389,7 +473,10 @@ class Folder {
|
|
|
389
473
|
}
|
|
390
474
|
const binding = this.context.scopes.references.get(call.callee);
|
|
391
475
|
const identity = binding ? solidExport(binding, this.context.options) : undefined;
|
|
392
|
-
if (identity === undefined
|
|
476
|
+
if (identity === undefined) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) {
|
|
393
480
|
return;
|
|
394
481
|
}
|
|
395
482
|
const before = this.context.code.slice(0, call.start).trimEnd();
|
|
@@ -400,11 +487,39 @@ class Folder {
|
|
|
400
487
|
this.changed = true;
|
|
401
488
|
}
|
|
402
489
|
|
|
403
|
-
|
|
490
|
+
/**
|
|
491
|
+
* `untrack(() => value)` is the value, when the value is a constant.
|
|
492
|
+
* Nothing is read, so there is nothing to untrack. A component inlined
|
|
493
|
+
* with a literal prop leaves this, like Kobalte's `untrack(() => props.as)`.
|
|
494
|
+
*/
|
|
495
|
+
private foldUntrack(call: CallExpression): void {
|
|
496
|
+
if (call.callee.type !== 'Identifier' || call.arguments.length !== 1) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const binding = this.context.scopes.references.get(call.callee);
|
|
500
|
+
const identity = binding ? solidExport(binding, this.context.options) : undefined;
|
|
501
|
+
const callback = call.arguments.at(0);
|
|
502
|
+
if (
|
|
503
|
+
identity !== 'untrack' ||
|
|
504
|
+
callback?.type !== 'ArrowFunctionExpression' ||
|
|
505
|
+
callback.async ||
|
|
506
|
+
callback.params.length > 0 ||
|
|
507
|
+
!callback.expression ||
|
|
508
|
+
callback.body.type === 'BlockStatement'
|
|
509
|
+
) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const value = evaluate(callback.body, this.lookup);
|
|
513
|
+
if (value) {
|
|
514
|
+
this.foldConstant(call, value);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private foldConstant(node: Node, known?: Const): void {
|
|
404
519
|
if (isLiteralForm(node)) {
|
|
405
520
|
return;
|
|
406
521
|
}
|
|
407
|
-
const value = evaluate(node, this.lookup);
|
|
522
|
+
const value = known ?? evaluate(node, this.lookup);
|
|
408
523
|
if (!value) {
|
|
409
524
|
return;
|
|
410
525
|
}
|
|
@@ -444,15 +559,15 @@ class Folder {
|
|
|
444
559
|
* Keeps `value`, evaluating `discarded` first when it has side effects.
|
|
445
560
|
* `(discarded, value)` runs it in the same order and yields the same result.
|
|
446
561
|
*/
|
|
447
|
-
private keepAfter(target: Node, discarded: Expression | undefined, value: Expression):
|
|
562
|
+
private keepAfter(target: Node, discarded: Expression | undefined, value: Expression): Text {
|
|
448
563
|
const parts = discarded ? effectfulParts(discarded) : [];
|
|
449
564
|
if (parts.length === 0) {
|
|
450
|
-
const text =
|
|
451
|
-
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
565
|
+
const text = getText(this.context, value);
|
|
566
|
+
return needsParensAt(target, value, this.context.parents) ? joinText`(${text})` : text;
|
|
452
567
|
}
|
|
453
|
-
const texts =
|
|
454
|
-
texts.push(
|
|
455
|
-
return `(${
|
|
568
|
+
const texts = getTexts(this.context, parts);
|
|
569
|
+
texts.push(getText(this.context, value));
|
|
570
|
+
return joinText`(${join(texts, ', ')})`;
|
|
456
571
|
}
|
|
457
572
|
|
|
458
573
|
private foldLogical(node: LogicalExpression): void {
|
|
@@ -645,28 +760,41 @@ class Folder {
|
|
|
645
760
|
}
|
|
646
761
|
return {
|
|
647
762
|
kind: 'expression',
|
|
648
|
-
text: JSON.stringify(value.value),
|
|
763
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
649
764
|
primary: true,
|
|
650
765
|
};
|
|
651
766
|
case 'JSXElement':
|
|
652
767
|
case 'JSXFragment':
|
|
653
|
-
return
|
|
768
|
+
return this.foldedJSX(value);
|
|
654
769
|
default:
|
|
655
770
|
if (value.expression.type === 'JSXEmptyExpression') {
|
|
656
771
|
return EMPTY;
|
|
657
772
|
}
|
|
658
773
|
// JSX in a container splices like JSX, so it can join the surrounding template.
|
|
659
774
|
if (value.expression.type === 'JSXElement' || value.expression.type === 'JSXFragment') {
|
|
660
|
-
return
|
|
775
|
+
return this.foldedJSX(value.expression);
|
|
661
776
|
}
|
|
662
777
|
return {
|
|
663
778
|
kind: 'expression',
|
|
664
|
-
text:
|
|
779
|
+
text: getText(this.context, value.expression),
|
|
665
780
|
primary: isPrimary(value.expression),
|
|
666
781
|
};
|
|
667
782
|
}
|
|
668
783
|
}
|
|
669
784
|
|
|
785
|
+
/**
|
|
786
|
+
* What a JSX value renders, read after the folds of this round. A fold
|
|
787
|
+
* inside it can have replaced it with an expression already, such as an
|
|
788
|
+
* inner `<Show>` that became its string `fallback`.
|
|
789
|
+
*/
|
|
790
|
+
private foldedJSX(node: JSXElement | JSXFragment): Fold {
|
|
791
|
+
const text = getText(this.context, node);
|
|
792
|
+
if (text.code.startsWith('<')) {
|
|
793
|
+
return { kind: 'jsx', text };
|
|
794
|
+
}
|
|
795
|
+
return { kind: 'expression', text, primary: false };
|
|
796
|
+
}
|
|
797
|
+
|
|
670
798
|
private foldShow(element: JSXElement): Fold | undefined {
|
|
671
799
|
const when = this.attributeTruthiness(element, 'when');
|
|
672
800
|
if (when === undefined) {
|
|
@@ -717,19 +845,7 @@ class Folder {
|
|
|
717
845
|
* would run while the copy is made, so either one keeps the loop.
|
|
718
846
|
*/
|
|
719
847
|
private unrollFor(element: JSXElement, expression: Expression): Fold | undefined {
|
|
720
|
-
if (
|
|
721
|
-
element.openingElement.attributes.some(
|
|
722
|
-
(attribute) =>
|
|
723
|
-
attribute.type === 'JSXAttribute' &&
|
|
724
|
-
attribute.name.type === 'JSXIdentifier' &&
|
|
725
|
-
attribute.name.name === 'keyed' &&
|
|
726
|
-
!(
|
|
727
|
-
attribute.value?.type === 'JSXExpressionContainer' &&
|
|
728
|
-
attribute.value.expression.type === 'Literal' &&
|
|
729
|
-
attribute.value.expression.value === false
|
|
730
|
-
),
|
|
731
|
-
)
|
|
732
|
-
) {
|
|
848
|
+
if (hasEnabledKeyed(element)) {
|
|
733
849
|
return undefined;
|
|
734
850
|
}
|
|
735
851
|
const byIndex = findAttribute(element, 'keyed') !== undefined;
|
|
@@ -737,7 +853,7 @@ class Folder {
|
|
|
737
853
|
if (!list) {
|
|
738
854
|
return undefined;
|
|
739
855
|
}
|
|
740
|
-
const significant = element.children
|
|
856
|
+
const significant = getSignificant(element.children);
|
|
741
857
|
const only = significant.at(0);
|
|
742
858
|
if (significant.length !== 1 || only?.type !== 'JSXExpressionContainer') {
|
|
743
859
|
return undefined;
|
|
@@ -763,7 +879,8 @@ class Folder {
|
|
|
763
879
|
}
|
|
764
880
|
// A fold inside the row in this pass would be lost, so it waits for the next pass.
|
|
765
881
|
if (
|
|
766
|
-
this.context.s.slice(body.start, body.end)
|
|
882
|
+
this.context.s.slice(body.start, body.end).code !==
|
|
883
|
+
this.context.code.slice(body.start, body.end)
|
|
767
884
|
) {
|
|
768
885
|
return undefined;
|
|
769
886
|
}
|
|
@@ -781,9 +898,9 @@ class Folder {
|
|
|
781
898
|
}
|
|
782
899
|
const itemParam = params.at(0);
|
|
783
900
|
const indexParam = params.at(1);
|
|
784
|
-
const rows:
|
|
901
|
+
const rows: Text[] = [];
|
|
785
902
|
for (const [index, item] of list.items.entries()) {
|
|
786
|
-
const copy =
|
|
903
|
+
const copy = this.context.s.fresh();
|
|
787
904
|
// With `keyed={false}` the item is an accessor and the index a number, and the other way around otherwise.
|
|
788
905
|
if (
|
|
789
906
|
(itemParam && !this.substituteItem(copy, itemParam, item, byIndex)) ||
|
|
@@ -793,7 +910,7 @@ class Folder {
|
|
|
793
910
|
}
|
|
794
911
|
rows.push(copy.slice(body.start, body.end));
|
|
795
912
|
}
|
|
796
|
-
const text = `<>${
|
|
913
|
+
const text = joinText`<>${join(rows)}</>`;
|
|
797
914
|
if (!this.unrollingPays(element, text, list.declaration)) {
|
|
798
915
|
return undefined;
|
|
799
916
|
}
|
|
@@ -807,10 +924,8 @@ class Folder {
|
|
|
807
924
|
private constantList(
|
|
808
925
|
expression: Expression,
|
|
809
926
|
): { items: ConstantItem[]; declaration: Node | undefined } | undefined {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
node = node.expression;
|
|
813
|
-
}
|
|
927
|
+
// `as const` and `satisfies` change nothing at runtime.
|
|
928
|
+
let node: Expression = unwrap(expression);
|
|
814
929
|
let declaration: Node | undefined;
|
|
815
930
|
if (node.type === 'Identifier') {
|
|
816
931
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -825,16 +940,17 @@ class Folder {
|
|
|
825
940
|
// Any other use could change the list, and an export can be changed by another module.
|
|
826
941
|
const statement = this.context.parents.get(binding.declaration);
|
|
827
942
|
const owner = statement ? this.context.parents.get(statement) : undefined;
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
!
|
|
831
|
-
|
|
832
|
-
|
|
943
|
+
let onlyForEach = true;
|
|
944
|
+
for (const reference of binding.references) {
|
|
945
|
+
if (!this.isForEach(reference)) {
|
|
946
|
+
onlyForEach = false;
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
833
949
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
node = node.expression;
|
|
950
|
+
if (owner?.type === 'ExportNamedDeclaration' || !onlyForEach) {
|
|
951
|
+
return undefined;
|
|
837
952
|
}
|
|
953
|
+
node = unwrap(binding.declaration.init);
|
|
838
954
|
declaration =
|
|
839
955
|
binding.references.length === 1 &&
|
|
840
956
|
statement?.type === 'VariableDeclaration' &&
|
|
@@ -882,7 +998,7 @@ class Folder {
|
|
|
882
998
|
if (node.type !== 'ObjectExpression') {
|
|
883
999
|
return undefined;
|
|
884
1000
|
}
|
|
885
|
-
const keys = new Map<string,
|
|
1001
|
+
const keys = new Map<string, Text>();
|
|
886
1002
|
for (const property of node.properties) {
|
|
887
1003
|
if (
|
|
888
1004
|
property.type !== 'Property' ||
|
|
@@ -907,7 +1023,7 @@ class Folder {
|
|
|
907
1023
|
* `false` when a read is not one the value can take the place of.
|
|
908
1024
|
*/
|
|
909
1025
|
private substituteItem(
|
|
910
|
-
copy:
|
|
1026
|
+
copy: Editor,
|
|
911
1027
|
param: Binding,
|
|
912
1028
|
item: ConstantItem,
|
|
913
1029
|
accessor: boolean,
|
|
@@ -963,17 +1079,14 @@ class Folder {
|
|
|
963
1079
|
}
|
|
964
1080
|
|
|
965
1081
|
/** A value's text in place of `node`, with parentheses unless it fills a JSX container. */
|
|
966
|
-
private valueAt(node: Node, text:
|
|
967
|
-
return this.context.parents.get(node)?.type === 'JSXExpressionContainer'
|
|
1082
|
+
private valueAt(node: Node, text: Content): Content {
|
|
1083
|
+
return this.context.parents.get(node)?.type === 'JSXExpressionContainer'
|
|
1084
|
+
? text
|
|
1085
|
+
: joinText`(${text})`;
|
|
968
1086
|
}
|
|
969
1087
|
|
|
970
1088
|
/** Writes an item's index over each read of the row's index parameter. */
|
|
971
|
-
private substituteIndex(
|
|
972
|
-
copy: MagicString,
|
|
973
|
-
param: Binding,
|
|
974
|
-
index: number,
|
|
975
|
-
accessor: boolean,
|
|
976
|
-
): boolean {
|
|
1089
|
+
private substituteIndex(copy: Editor, param: Binding, index: number, accessor: boolean): boolean {
|
|
977
1090
|
for (const reference of param.references) {
|
|
978
1091
|
let read: Node = reference;
|
|
979
1092
|
if (accessor) {
|
|
@@ -996,14 +1109,14 @@ class Folder {
|
|
|
996
1109
|
* Whether the copies are estimated to be no larger, once lowered, than
|
|
997
1110
|
* the loop and the list it no longer needs.
|
|
998
1111
|
*/
|
|
999
|
-
private unrollingPays(element: JSXElement, text:
|
|
1112
|
+
private unrollingPays(element: JSXElement, text: Text, declaration: Node | undefined): boolean {
|
|
1000
1113
|
const inTemplate = this.joinsParentTemplate(element);
|
|
1001
1114
|
const loop =
|
|
1002
1115
|
new SizeEstimator(this.context.code).jsx(element, inTemplate) +
|
|
1003
1116
|
(declaration
|
|
1004
1117
|
? compactLength(this.context.code.slice(declaration.start, declaration.end))
|
|
1005
1118
|
: 0);
|
|
1006
|
-
const code = `const __so_rows__ = ${text};`;
|
|
1119
|
+
const code = `const __so_rows__ = ${text.code};`;
|
|
1007
1120
|
const program = parse(this.context.filename, code);
|
|
1008
1121
|
const statement = program.body.at(0);
|
|
1009
1122
|
const init =
|
|
@@ -1014,12 +1127,7 @@ class Folder {
|
|
|
1014
1127
|
// In an element, the rows splice into its template. Elsewhere they are a fragment of templates.
|
|
1015
1128
|
const estimator = new SizeEstimator(code);
|
|
1016
1129
|
const unrolled = inTemplate
|
|
1017
|
-
? init.children
|
|
1018
|
-
(size, child) =>
|
|
1019
|
-
size +
|
|
1020
|
-
(child.type === 'JSXElement' ? estimator.jsx(child, true) : estimator.node(child)),
|
|
1021
|
-
0,
|
|
1022
|
-
)
|
|
1130
|
+
? estimateChildren(estimator, init.children)
|
|
1023
1131
|
: estimator.jsx(init, false);
|
|
1024
1132
|
return unrolled <= loop;
|
|
1025
1133
|
}
|
|
@@ -1078,8 +1186,19 @@ class Folder {
|
|
|
1078
1186
|
return undefined;
|
|
1079
1187
|
}
|
|
1080
1188
|
|
|
1081
|
-
|
|
1082
|
-
|
|
1189
|
+
// Find the first true match, and whether every match before it is false.
|
|
1190
|
+
let winner = -1;
|
|
1191
|
+
let allFalse = true;
|
|
1192
|
+
for (const [index, item] of matches.entries()) {
|
|
1193
|
+
if (item.when === true) {
|
|
1194
|
+
winner = index;
|
|
1195
|
+
break;
|
|
1196
|
+
}
|
|
1197
|
+
if (item.when !== false) {
|
|
1198
|
+
allFalse = false;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
if (winner !== -1 && allFalse) {
|
|
1083
1202
|
const match = matches.at(winner);
|
|
1084
1203
|
if (!match || hasCallbackChild(match.element.children)) {
|
|
1085
1204
|
return undefined;
|
|
@@ -1087,7 +1206,7 @@ class Folder {
|
|
|
1087
1206
|
return { kind: 'children', children: match.element.children };
|
|
1088
1207
|
}
|
|
1089
1208
|
|
|
1090
|
-
if (
|
|
1209
|
+
if (winner === -1 && allFalse) {
|
|
1091
1210
|
return this.fallback(element);
|
|
1092
1211
|
}
|
|
1093
1212
|
|
|
@@ -1142,13 +1261,16 @@ class Folder {
|
|
|
1142
1261
|
if (tag === undefined) {
|
|
1143
1262
|
return undefined;
|
|
1144
1263
|
}
|
|
1145
|
-
const attributes =
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1264
|
+
const attributes: Content[] = [tag];
|
|
1265
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1266
|
+
if (attribute !== component) {
|
|
1267
|
+
attributes.push(getText(this.context, attribute));
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
const opening = join(attributes, ' ');
|
|
1149
1271
|
const text = element.closingElement
|
|
1150
|
-
? `<${opening}>${innerText(this.context.s, element)}</${tag}>`
|
|
1151
|
-
: `<${opening} />`;
|
|
1272
|
+
? joinText`<${opening}>${innerText(this.context.s, element)}</${tag}>`
|
|
1273
|
+
: joinText`<${opening} />`;
|
|
1152
1274
|
return { kind: 'jsx', text };
|
|
1153
1275
|
}
|
|
1154
1276
|
|
|
@@ -1163,16 +1285,16 @@ class Folder {
|
|
|
1163
1285
|
this.replace(element, this.expressionText(resolved));
|
|
1164
1286
|
}
|
|
1165
1287
|
|
|
1166
|
-
private expressionText(resolved: Fold):
|
|
1288
|
+
private expressionText(resolved: Fold): Content {
|
|
1167
1289
|
switch (resolved.kind) {
|
|
1168
1290
|
case 'empty':
|
|
1169
1291
|
return 'null';
|
|
1170
1292
|
case 'jsx':
|
|
1171
1293
|
return resolved.text;
|
|
1172
1294
|
case 'expression':
|
|
1173
|
-
return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
1295
|
+
return resolved.primary ? resolved.text : joinText`(${resolved.text})`;
|
|
1174
1296
|
default: {
|
|
1175
|
-
const significant = resolved.children
|
|
1297
|
+
const significant = getSignificant(resolved.children);
|
|
1176
1298
|
const only = significant.at(0);
|
|
1177
1299
|
if (only === undefined) {
|
|
1178
1300
|
return 'null';
|
|
@@ -1181,14 +1303,14 @@ class Folder {
|
|
|
1181
1303
|
significant.length === 1 &&
|
|
1182
1304
|
(only.type === 'JSXElement' || only.type === 'JSXFragment')
|
|
1183
1305
|
) {
|
|
1184
|
-
return
|
|
1306
|
+
return getText(this.context, only);
|
|
1185
1307
|
}
|
|
1186
1308
|
const first = resolved.children.at(0);
|
|
1187
1309
|
const last = resolved.children.at(-1);
|
|
1188
1310
|
if (!first || !last) {
|
|
1189
1311
|
return 'null';
|
|
1190
1312
|
}
|
|
1191
|
-
return `<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
1313
|
+
return joinText`<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
1192
1314
|
}
|
|
1193
1315
|
}
|
|
1194
1316
|
}
|
|
@@ -1230,7 +1352,7 @@ class Folder {
|
|
|
1230
1352
|
pieces.push(replacedPiece(child, resolved.text));
|
|
1231
1353
|
break;
|
|
1232
1354
|
case 'expression':
|
|
1233
|
-
pieces.push(replacedPiece(child, `{${resolved.text}}`));
|
|
1355
|
+
pieces.push(replacedPiece(child, joinText`{${resolved.text}}`));
|
|
1234
1356
|
break;
|
|
1235
1357
|
default:
|
|
1236
1358
|
pieces.push(replacedPiece(child, ''));
|
|
@@ -1255,7 +1377,7 @@ class Folder {
|
|
|
1255
1377
|
* constant or a binding nothing reassigns, so reading it again where the
|
|
1256
1378
|
* parameter was gives the same value.
|
|
1257
1379
|
*/
|
|
1258
|
-
private reducedCall(expression: Expression | JSXEmptyExpression):
|
|
1380
|
+
private reducedCall(expression: Expression | JSXEmptyExpression): Text | undefined {
|
|
1259
1381
|
if (expression.type !== 'CallExpression' || expression.optional) {
|
|
1260
1382
|
return undefined;
|
|
1261
1383
|
}
|
|
@@ -1280,7 +1402,8 @@ class Folder {
|
|
|
1280
1402
|
}
|
|
1281
1403
|
// A fold inside the body in this pass would be lost, so it waits for the next pass.
|
|
1282
1404
|
if (
|
|
1283
|
-
this.context.s.slice(body.start, body.end)
|
|
1405
|
+
this.context.s.slice(body.start, body.end).code !==
|
|
1406
|
+
this.context.code.slice(body.start, body.end)
|
|
1284
1407
|
) {
|
|
1285
1408
|
return undefined;
|
|
1286
1409
|
}
|
|
@@ -1288,7 +1411,7 @@ class Folder {
|
|
|
1288
1411
|
if (!scope) {
|
|
1289
1412
|
return undefined;
|
|
1290
1413
|
}
|
|
1291
|
-
const copy =
|
|
1414
|
+
const copy = this.context.s.fresh();
|
|
1292
1415
|
for (const [index, param] of params.entries()) {
|
|
1293
1416
|
const arg = args.at(index);
|
|
1294
1417
|
if (param.type !== 'Identifier' || !arg || arg.type === 'SpreadElement') {
|
|
@@ -1311,26 +1434,30 @@ class Folder {
|
|
|
1311
1434
|
const parent = this.context.parents.get(reference);
|
|
1312
1435
|
const shorthand =
|
|
1313
1436
|
parent?.type === 'Property' && parent.shorthand && parent.value === reference;
|
|
1314
|
-
copy.overwrite(
|
|
1437
|
+
copy.overwrite(
|
|
1438
|
+
reference.start,
|
|
1439
|
+
reference.end,
|
|
1440
|
+
shorthand ? joinText`${param.name}: ${text}` : text,
|
|
1441
|
+
);
|
|
1315
1442
|
}
|
|
1316
1443
|
}
|
|
1317
1444
|
return copy.slice(body.start, body.end);
|
|
1318
1445
|
}
|
|
1319
1446
|
|
|
1320
1447
|
/** The text of an argument that reads the same wherever it is copied. */
|
|
1321
|
-
private argumentText(arg: Expression):
|
|
1448
|
+
private argumentText(arg: Expression): Content | undefined {
|
|
1322
1449
|
let node: Expression = arg;
|
|
1323
1450
|
while (node.type === 'ParenthesizedExpression') {
|
|
1324
1451
|
node = node.expression;
|
|
1325
1452
|
}
|
|
1326
1453
|
switch (node.type) {
|
|
1327
1454
|
case 'Literal':
|
|
1328
|
-
return 'regex' in node ? undefined :
|
|
1455
|
+
return 'regex' in node ? undefined : getText(this.context, node);
|
|
1329
1456
|
case 'TemplateLiteral':
|
|
1330
|
-
return node.expressions.length === 0 ?
|
|
1457
|
+
return node.expressions.length === 0 ? getText(this.context, node) : undefined;
|
|
1331
1458
|
case 'UnaryExpression':
|
|
1332
1459
|
return node.operator === '-' && node.argument.type === 'Literal'
|
|
1333
|
-
? `(${
|
|
1460
|
+
? joinText`(${getText(this.context, node)})`
|
|
1334
1461
|
: undefined;
|
|
1335
1462
|
case 'Identifier': {
|
|
1336
1463
|
const binding = this.context.scopes.references.get(node);
|
|
@@ -1370,33 +1497,132 @@ class Folder {
|
|
|
1370
1497
|
const unreachable = statements.slice(terminator + 1);
|
|
1371
1498
|
const first = unreachable.at(0);
|
|
1372
1499
|
const last = unreachable.at(-1);
|
|
1373
|
-
|
|
1500
|
+
let hoists = false;
|
|
1501
|
+
for (const statement of unreachable) {
|
|
1502
|
+
if (containsHoistedDeclaration(statement)) {
|
|
1503
|
+
hoists = true;
|
|
1504
|
+
break;
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
if (first && last && !hoists) {
|
|
1374
1508
|
this.context.s.remove(first.start, last.end);
|
|
1375
1509
|
this.changed = true;
|
|
1376
1510
|
}
|
|
1377
1511
|
}
|
|
1378
1512
|
|
|
1379
|
-
|
|
1380
|
-
|
|
1513
|
+
/**
|
|
1514
|
+
* Removes each declaration inside a function that nothing references. The
|
|
1515
|
+
* references are those of the code this pass parsed, so a binding whose last
|
|
1516
|
+
* read this pass folds is removed in the next round.
|
|
1517
|
+
*/
|
|
1518
|
+
removeUnusedDeclarations(): void {
|
|
1519
|
+
for (const scope of this.context.scopes.scopes.values()) {
|
|
1520
|
+
if (scope === this.context.scopes.root) {
|
|
1521
|
+
continue;
|
|
1522
|
+
}
|
|
1523
|
+
for (const binding of scope.bindings.values()) {
|
|
1524
|
+
const declaration = this.unusedDeclaration(binding);
|
|
1525
|
+
if (declaration && !this.removed.has(declaration)) {
|
|
1526
|
+
this.removed.add(declaration);
|
|
1527
|
+
this.context.s.remove(declaration.start, declaration.end);
|
|
1528
|
+
this.changed = true;
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* The declaration statement of `binding` when every binding it declares is
|
|
1536
|
+
* unused and every initializer can be skipped.
|
|
1537
|
+
*/
|
|
1538
|
+
private unusedDeclaration(binding: Binding): VariableDeclaration | undefined {
|
|
1539
|
+
if (binding.declaration.type !== 'VariableDeclarator') {
|
|
1540
|
+
return undefined;
|
|
1541
|
+
}
|
|
1542
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
1543
|
+
if (
|
|
1544
|
+
statement?.type !== 'VariableDeclaration' ||
|
|
1545
|
+
(statement.kind !== 'const' && statement.kind !== 'let') ||
|
|
1546
|
+
statement.declare
|
|
1547
|
+
) {
|
|
1548
|
+
return undefined;
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
// Only a statement in a list can go. A loop head or an export needs it.
|
|
1552
|
+
const container = this.context.parents.get(statement)?.type;
|
|
1553
|
+
if (
|
|
1554
|
+
container !== 'BlockStatement' &&
|
|
1555
|
+
container !== 'StaticBlock' &&
|
|
1556
|
+
container !== 'SwitchCase'
|
|
1557
|
+
) {
|
|
1558
|
+
return undefined;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
// Every declarator has to go, or the statement stays whole.
|
|
1562
|
+
for (const declarator of statement.declarations) {
|
|
1563
|
+
if (declarator.id.type !== 'Identifier') {
|
|
1564
|
+
return undefined;
|
|
1565
|
+
}
|
|
1566
|
+
const declared = binding.scope.bindings.get(declarator.id.name);
|
|
1567
|
+
if (!declared || declared.mutated || declared.references.length > 0) {
|
|
1568
|
+
return undefined;
|
|
1569
|
+
}
|
|
1570
|
+
if (declarator.init && !this.isSkippableInit(declarator.init)) {
|
|
1571
|
+
return undefined;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
return statement;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
/**
|
|
1578
|
+
* Whether an unused initializer can be dropped: an expression with no side
|
|
1579
|
+
* effects, or a Solid call that only creates a value from such arguments.
|
|
1580
|
+
*/
|
|
1581
|
+
private isSkippableInit(init: Expression): boolean {
|
|
1582
|
+
if (isSideEffectFree(init)) {
|
|
1583
|
+
return true;
|
|
1584
|
+
}
|
|
1585
|
+
const call = unwrap(init);
|
|
1586
|
+
if (call.type !== 'CallExpression' || call.callee.type !== 'Identifier') {
|
|
1587
|
+
return false;
|
|
1588
|
+
}
|
|
1589
|
+
const callee = this.context.scopes.references.get(call.callee);
|
|
1590
|
+
const identity = callee ? solidExport(callee, this.context.options) : undefined;
|
|
1591
|
+
if (
|
|
1592
|
+
identity === undefined ||
|
|
1593
|
+
(!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call)))
|
|
1594
|
+
) {
|
|
1595
|
+
return false;
|
|
1596
|
+
}
|
|
1597
|
+
for (const argument of call.arguments) {
|
|
1598
|
+
if (argument.type === 'SpreadElement' || !isSideEffectFree(argument)) {
|
|
1599
|
+
return false;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
return true;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
private replaceStatement(statement: Node, text: Content): void {
|
|
1606
|
+
if (getCode(text) === '') {
|
|
1381
1607
|
this.context.s.remove(statement.start, statement.end);
|
|
1382
1608
|
this.changed = true;
|
|
1383
1609
|
return;
|
|
1384
1610
|
}
|
|
1385
|
-
this.replace(statement, continuesPreviousStatement(text) ? `;${text}` : text);
|
|
1611
|
+
this.replace(statement, continuesPreviousStatement(getCode(text)) ? joinText`;${text}` : text);
|
|
1386
1612
|
}
|
|
1387
1613
|
|
|
1388
1614
|
/**
|
|
1389
1615
|
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
1390
1616
|
* when skipping the test changes nothing.
|
|
1391
1617
|
*/
|
|
1392
|
-
private discardedTest(test: Expression):
|
|
1618
|
+
private discardedTest(test: Expression): Text | undefined {
|
|
1393
1619
|
const parts = effectfulParts(test);
|
|
1394
1620
|
const first = parts.at(0);
|
|
1395
1621
|
if (first === undefined) {
|
|
1396
1622
|
return undefined;
|
|
1397
1623
|
}
|
|
1398
|
-
const text =
|
|
1399
|
-
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
1624
|
+
const text = join(getTexts(this.context, parts), ', ');
|
|
1625
|
+
return startsLikeStatement(first) ? joinText`(${text});` : joinText`${text};`;
|
|
1400
1626
|
}
|
|
1401
1627
|
|
|
1402
1628
|
/**
|
|
@@ -1418,15 +1644,15 @@ class Folder {
|
|
|
1418
1644
|
if (kept?.type === 'FunctionDeclaration') {
|
|
1419
1645
|
return undefined;
|
|
1420
1646
|
}
|
|
1421
|
-
const parts:
|
|
1647
|
+
const parts: Text[] = [];
|
|
1422
1648
|
const effect = this.discardedTest(statement.test);
|
|
1423
1649
|
if (effect !== undefined) {
|
|
1424
1650
|
parts.push(effect);
|
|
1425
1651
|
}
|
|
1426
1652
|
if (kept) {
|
|
1427
|
-
parts.push(
|
|
1653
|
+
parts.push(getText(this.context, kept));
|
|
1428
1654
|
}
|
|
1429
|
-
this.replaceStatement(statement,
|
|
1655
|
+
this.replaceStatement(statement, join(parts, ' '));
|
|
1430
1656
|
return kept;
|
|
1431
1657
|
}
|
|
1432
1658
|
|
|
@@ -1447,6 +1673,7 @@ class Folder {
|
|
|
1447
1673
|
*/
|
|
1448
1674
|
export function fold(context: PassContext): boolean {
|
|
1449
1675
|
const folder = new Folder(context);
|
|
1676
|
+
folder.removeUnusedDeclarations();
|
|
1450
1677
|
folder.walk(context.program);
|
|
1451
1678
|
return folder.changed;
|
|
1452
1679
|
}
|