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/jsx.ts
CHANGED
|
@@ -132,9 +132,12 @@ export function findAttribute(element: JSXElement, name: string): JSXAttribute |
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
export function hasSpreadAttribute(element: JSXElement): boolean {
|
|
135
|
-
|
|
136
|
-
(attribute
|
|
137
|
-
|
|
135
|
+
for (const attribute of element.openingElement.attributes) {
|
|
136
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
138
141
|
}
|
|
139
142
|
|
|
140
143
|
/**
|
|
@@ -142,18 +145,19 @@ export function hasSpreadAttribute(element: JSXElement): boolean {
|
|
|
142
145
|
* narrowed value. Folding one away would drop that call.
|
|
143
146
|
*/
|
|
144
147
|
export function hasCallbackChild(children: readonly JSXChild[]): boolean {
|
|
145
|
-
|
|
148
|
+
for (const child of children) {
|
|
146
149
|
if (child.type !== 'JSXExpressionContainer') {
|
|
147
|
-
|
|
150
|
+
continue;
|
|
148
151
|
}
|
|
149
152
|
let expression: Node = child.expression;
|
|
150
153
|
while (expression.type === 'ParenthesizedExpression') {
|
|
151
154
|
expression = expression.expression;
|
|
152
155
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
156
|
+
if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
157
161
|
}
|
|
158
162
|
|
|
159
163
|
/**
|
package/src/memo.ts
CHANGED
|
@@ -44,6 +44,31 @@
|
|
|
44
44
|
*
|
|
45
45
|
* becomes `<div><b>x</b></div>`.
|
|
46
46
|
*
|
|
47
|
+
* A signal that nothing writes is a constant too. When its setter is left
|
|
48
|
+
* out or never referenced, and its initial value is a literal, every read
|
|
49
|
+
* becomes the literal:
|
|
50
|
+
*
|
|
51
|
+
* ```jsx
|
|
52
|
+
* const [size] = createSignal(3);
|
|
53
|
+
* return <p>{size()}</p>;
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* becomes `<p>{3}</p>`. A signal with a value that is not a function takes no
|
|
57
|
+
* owner and no hydration id, so removing it changes nothing else.
|
|
58
|
+
*
|
|
59
|
+
* A plain arrow that only returns an expression, like a derived accessor,
|
|
60
|
+
* has no cache and no owner, so each call already runs its body again. The
|
|
61
|
+
* calls can become the body:
|
|
62
|
+
*
|
|
63
|
+
* ```jsx
|
|
64
|
+
* const label = () => (props.open ? 'Close' : 'Open');
|
|
65
|
+
* return <button>{label()}</button>;
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* becomes `<button>{(props.open ? 'Close' : 'Open')}</button>`. With more than
|
|
69
|
+
* one call, the body is copied only when the copies are no larger than the
|
|
70
|
+
* arrow and its calls.
|
|
71
|
+
*
|
|
47
72
|
* # Accepted differences
|
|
48
73
|
*
|
|
49
74
|
* - The computation first runs when its reader first runs, instead of when
|
|
@@ -51,6 +76,9 @@
|
|
|
51
76
|
* - The memo no longer takes a slot in the owner tree, which shifts the
|
|
52
77
|
* hydration keys after it. A server build and its client build must
|
|
53
78
|
* compile with the same options.
|
|
79
|
+
* - A call that runs before the declaration, from a hoisted function the
|
|
80
|
+
* declaration comes before, throws on the original and returns the value
|
|
81
|
+
* on the output.
|
|
54
82
|
*/
|
|
55
83
|
import type {
|
|
56
84
|
ArrowFunctionExpression,
|
|
@@ -61,12 +89,13 @@ import type {
|
|
|
61
89
|
Node,
|
|
62
90
|
VariableDeclarator,
|
|
63
91
|
} from 'oxc-parser';
|
|
64
|
-
import { isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
92
|
+
import { isFunctionNode, isIntrinsicTag, isPrimary, unwrap } from './ast';
|
|
65
93
|
import type { PassContext } from './context';
|
|
66
|
-
import { recordSkip, solidExport
|
|
94
|
+
import { getText, recordSkip, solidExport } from './context';
|
|
67
95
|
import { usesFunctionContext } from './inline';
|
|
68
96
|
import type { Binding } from './scope';
|
|
69
97
|
import { lookup, scopeAt } from './scope';
|
|
98
|
+
import { compactLength } from './size';
|
|
70
99
|
|
|
71
100
|
/**
|
|
72
101
|
* Props of Solid's built-in components that are read inside a memo, once per update.
|
|
@@ -120,14 +149,14 @@ function constantText(context: PassContext, node: Expression): string | undefine
|
|
|
120
149
|
const expression = unwrap(node);
|
|
121
150
|
switch (expression.type) {
|
|
122
151
|
case 'Literal':
|
|
123
|
-
return 'regex' in expression ? undefined :
|
|
152
|
+
return 'regex' in expression ? undefined : getText(context, expression);
|
|
124
153
|
case 'TemplateLiteral':
|
|
125
|
-
return expression.expressions.length === 0 ?
|
|
154
|
+
return expression.expressions.length === 0 ? getText(context, expression) : undefined;
|
|
126
155
|
case 'UnaryExpression':
|
|
127
156
|
return expression.operator === '-' &&
|
|
128
157
|
expression.argument.type === 'Literal' &&
|
|
129
158
|
typeof expression.argument.value === 'number'
|
|
130
|
-
?
|
|
159
|
+
? getText(context, expression)
|
|
131
160
|
: undefined;
|
|
132
161
|
default:
|
|
133
162
|
return undefined;
|
|
@@ -152,6 +181,9 @@ function returnedExpression(fn: FunctionNode | ArrowFunctionExpression): Express
|
|
|
152
181
|
class MemoInliner {
|
|
153
182
|
changed = false;
|
|
154
183
|
|
|
184
|
+
/** Ranges of the source rewritten so far in this run. */
|
|
185
|
+
private readonly edited: [number, number][] = [];
|
|
186
|
+
|
|
155
187
|
constructor(private readonly context: PassContext) {}
|
|
156
188
|
|
|
157
189
|
/** Why the last memo was turned down, for the report. Empty for a call that is not a memo. */
|
|
@@ -225,7 +257,7 @@ class MemoInliner {
|
|
|
225
257
|
}
|
|
226
258
|
|
|
227
259
|
/**
|
|
228
|
-
* Replaces every read of a memo whose
|
|
260
|
+
* Replaces every read of a memo or signal whose value is a literal with the
|
|
229
261
|
* literal. Each reference has to be a call after the declaration, since a
|
|
230
262
|
* call before it would throw.
|
|
231
263
|
*/
|
|
@@ -233,10 +265,11 @@ class MemoInliner {
|
|
|
233
265
|
declarator: VariableDeclarator,
|
|
234
266
|
binding: Binding,
|
|
235
267
|
text: string,
|
|
268
|
+
kind: 'memo' | 'signal' = 'memo',
|
|
236
269
|
): true | undefined {
|
|
237
270
|
const statement = this.context.parents.get(declarator);
|
|
238
271
|
if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1) {
|
|
239
|
-
this.reason =
|
|
272
|
+
this.reason = `${kind}-declaration`;
|
|
240
273
|
return undefined;
|
|
241
274
|
}
|
|
242
275
|
const calls: CallExpression[] = [];
|
|
@@ -248,7 +281,7 @@ class MemoInliner {
|
|
|
248
281
|
call.arguments.length > 0 ||
|
|
249
282
|
call.start < declarator.end
|
|
250
283
|
) {
|
|
251
|
-
this.reason =
|
|
284
|
+
this.reason = `${kind}-constant`;
|
|
252
285
|
return undefined;
|
|
253
286
|
}
|
|
254
287
|
calls.push(call);
|
|
@@ -264,8 +297,10 @@ class MemoInliner {
|
|
|
264
297
|
parent?.type === 'Property' ||
|
|
265
298
|
(parent?.type === 'CallExpression' && parent.callee !== call);
|
|
266
299
|
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
300
|
+
this.edited.push([call.start, call.end]);
|
|
267
301
|
}
|
|
268
302
|
this.context.s.remove(statement.start, statement.end);
|
|
303
|
+
this.edited.push([statement.start, statement.end]);
|
|
269
304
|
this.changed = true;
|
|
270
305
|
return true;
|
|
271
306
|
}
|
|
@@ -273,7 +308,7 @@ class MemoInliner {
|
|
|
273
308
|
/**
|
|
274
309
|
* The function a node runs in.
|
|
275
310
|
*/
|
|
276
|
-
private
|
|
311
|
+
private findEnclosingFunction(node: Node): Node | undefined {
|
|
277
312
|
let current = this.context.parents.get(node);
|
|
278
313
|
while (current && !isFunctionNode(current)) {
|
|
279
314
|
current = this.context.parents.get(current);
|
|
@@ -282,7 +317,7 @@ class MemoInliner {
|
|
|
282
317
|
}
|
|
283
318
|
|
|
284
319
|
/** The JSX expression container a call is the whole of. */
|
|
285
|
-
private
|
|
320
|
+
private findContainer(call: Node): JSXExpressionContainer | undefined {
|
|
286
321
|
let parent = this.context.parents.get(call);
|
|
287
322
|
while (parent?.type === 'ParenthesizedExpression') {
|
|
288
323
|
parent = this.context.parents.get(parent);
|
|
@@ -331,7 +366,7 @@ class MemoInliner {
|
|
|
331
366
|
return false;
|
|
332
367
|
}
|
|
333
368
|
} else if (
|
|
334
|
-
this.
|
|
369
|
+
this.getBuiltIn(element.openingElement.name) !== tag ||
|
|
335
370
|
TRACKED_PROPS.get(tag) !== attribute
|
|
336
371
|
) {
|
|
337
372
|
// A component can read a prop any number of times.
|
|
@@ -343,7 +378,7 @@ class MemoInliner {
|
|
|
343
378
|
return this.isStaticJSX(element, fn);
|
|
344
379
|
}
|
|
345
380
|
|
|
346
|
-
private
|
|
381
|
+
private getBuiltIn(name: Node): string | undefined {
|
|
347
382
|
if (name.type !== 'JSXIdentifier') {
|
|
348
383
|
return undefined;
|
|
349
384
|
}
|
|
@@ -382,7 +417,7 @@ class MemoInliner {
|
|
|
382
417
|
case 'ParenthesizedExpression':
|
|
383
418
|
break;
|
|
384
419
|
case 'ReturnStatement':
|
|
385
|
-
return this.
|
|
420
|
+
return this.findEnclosingFunction(parent) === fn && !this.inLoop(parent, fn);
|
|
386
421
|
default:
|
|
387
422
|
return false;
|
|
388
423
|
}
|
|
@@ -472,7 +507,7 @@ class MemoInliner {
|
|
|
472
507
|
return undefined;
|
|
473
508
|
}
|
|
474
509
|
const statement = this.context.parents.get(declarator);
|
|
475
|
-
const fn = this.
|
|
510
|
+
const fn = this.findEnclosingFunction(declarator);
|
|
476
511
|
if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1 || !fn) {
|
|
477
512
|
this.reason = 'memo-declaration';
|
|
478
513
|
return undefined;
|
|
@@ -488,7 +523,7 @@ class MemoInliner {
|
|
|
488
523
|
return undefined;
|
|
489
524
|
}
|
|
490
525
|
// The resolved children are inserted, so the read has to be a child, not an attribute.
|
|
491
|
-
const container = this.
|
|
526
|
+
const container = this.findContainer(call);
|
|
492
527
|
const owner = container ? this.context.parents.get(container) : undefined;
|
|
493
528
|
if (
|
|
494
529
|
kind === 'children' &&
|
|
@@ -506,31 +541,179 @@ class MemoInliner {
|
|
|
506
541
|
}
|
|
507
542
|
if (kind === 'children' && container) {
|
|
508
543
|
// The JSX takes the place of its container, so it joins the template around it.
|
|
509
|
-
this.context.s.overwrite(container.start, container.end,
|
|
544
|
+
this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
|
|
545
|
+
this.edited.push([container.start, container.end]);
|
|
510
546
|
} else {
|
|
511
547
|
// The read sits in a JSX container, which takes any expression.
|
|
512
|
-
this.context.s.overwrite(call.start, call.end,
|
|
548
|
+
this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
|
|
549
|
+
this.edited.push([call.start, call.end]);
|
|
513
550
|
}
|
|
514
551
|
this.context.s.remove(statement.start, statement.end);
|
|
552
|
+
this.edited.push([statement.start, statement.end]);
|
|
515
553
|
this.changed = true;
|
|
516
554
|
return true;
|
|
517
555
|
}
|
|
518
556
|
|
|
557
|
+
/**
|
|
558
|
+
* The literal a signal always holds: `const [get] = createSignal(literal)`,
|
|
559
|
+
* or `const [get, set] = ...` with nothing referencing `set`. Options can
|
|
560
|
+
* change what a read does, such as `unobserved`, so a signal with options
|
|
561
|
+
* stays.
|
|
562
|
+
*/
|
|
563
|
+
private signalConstant(declarator: VariableDeclarator, binding: Binding): string | undefined {
|
|
564
|
+
const pattern = declarator.id;
|
|
565
|
+
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
566
|
+
if (
|
|
567
|
+
pattern.type !== 'ArrayPattern' ||
|
|
568
|
+
pattern.elements.at(0) !== binding.identifier ||
|
|
569
|
+
pattern.elements.length > 2 ||
|
|
570
|
+
init?.type !== 'CallExpression' ||
|
|
571
|
+
init.callee.type !== 'Identifier' ||
|
|
572
|
+
init.arguments.length !== 1
|
|
573
|
+
) {
|
|
574
|
+
return undefined;
|
|
575
|
+
}
|
|
576
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
577
|
+
if (!callee || solidExport(callee, this.context.options) !== 'createSignal') {
|
|
578
|
+
return undefined;
|
|
579
|
+
}
|
|
580
|
+
const value = init.arguments.at(0);
|
|
581
|
+
if (!value || value.type === 'SpreadElement') {
|
|
582
|
+
return undefined;
|
|
583
|
+
}
|
|
584
|
+
// Other modules can read an exported getter.
|
|
585
|
+
const statement = this.context.parents.get(declarator);
|
|
586
|
+
if (statement && this.context.parents.get(statement)?.type === 'ExportNamedDeclaration') {
|
|
587
|
+
return undefined;
|
|
588
|
+
}
|
|
589
|
+
const setter = pattern.elements.at(1);
|
|
590
|
+
if (setter) {
|
|
591
|
+
const setterBinding =
|
|
592
|
+
setter.type === 'Identifier' ? binding.scope.bindings.get(setter.name) : undefined;
|
|
593
|
+
if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) {
|
|
594
|
+
return undefined;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return constantText(this.context, value);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private tryInlineSignal(declarator: VariableDeclarator, binding: Binding): void {
|
|
601
|
+
const text = this.signalConstant(declarator, binding);
|
|
602
|
+
if (text === undefined) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
this.reason = '';
|
|
606
|
+
if (this.inlineConstant(declarator, binding, text, 'signal') === undefined) {
|
|
607
|
+
recordSkip(this.context, 'memos', this.reason, binding.name);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Replaces every call of `const name = () => expression` with the
|
|
613
|
+
* expression. Each reference has to be a call with no arguments after the
|
|
614
|
+
* declaration, and every name in the body has to mean the same at the call.
|
|
615
|
+
*/
|
|
616
|
+
private tryInlineArrow(declarator: VariableDeclarator, binding: Binding): void {
|
|
617
|
+
const fn = declarator.init ? unwrap(declarator.init) : undefined;
|
|
618
|
+
if (fn?.type !== 'ArrowFunctionExpression' || fn.async || fn.params.length > 0) {
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
const body = returnedExpression(fn);
|
|
622
|
+
const statement = this.context.parents.get(declarator);
|
|
623
|
+
if (
|
|
624
|
+
!body ||
|
|
625
|
+
statement?.type !== 'VariableDeclaration' ||
|
|
626
|
+
statement.declarations.length !== 1 ||
|
|
627
|
+
// Other modules can call an exported arrow.
|
|
628
|
+
this.context.parents.get(statement)?.type === 'ExportNamedDeclaration'
|
|
629
|
+
) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
const calls: CallExpression[] = [];
|
|
633
|
+
for (const reference of binding.references) {
|
|
634
|
+
const call = this.context.parents.get(reference);
|
|
635
|
+
if (
|
|
636
|
+
call?.type !== 'CallExpression' ||
|
|
637
|
+
call.callee !== reference ||
|
|
638
|
+
call.optional ||
|
|
639
|
+
call.arguments.length > 0 ||
|
|
640
|
+
call.start < declarator.end
|
|
641
|
+
) {
|
|
642
|
+
// The arrow is used as a value, like an event handler. That is not a derivation.
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
// `l()()` calls the result with no `this`, but `(a.b)()` passes `a`, and
|
|
646
|
+
// `delete (a.b)` deletes what `delete l()` leaves alone.
|
|
647
|
+
const parent = this.context.parents.get(call);
|
|
648
|
+
if (
|
|
649
|
+
(parent?.type === 'CallExpression' && parent.callee === call) ||
|
|
650
|
+
(parent?.type === 'TaggedTemplateExpression' && parent.tag === call) ||
|
|
651
|
+
(parent?.type === 'UnaryExpression' && parent.operator === 'delete')
|
|
652
|
+
) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
calls.push(call);
|
|
656
|
+
}
|
|
657
|
+
if (calls.length === 0) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
// The body is copied from the source, so it must not hold an edit from this run.
|
|
661
|
+
// The next round sees the edit.
|
|
662
|
+
for (const [start, end] of this.edited) {
|
|
663
|
+
if (start < statement.end && end > statement.start) {
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
const text = getText(this.context, body);
|
|
668
|
+
// Minified, the arrow costs `const a=()=>body;`, 13 characters besides the
|
|
669
|
+
// body, and each call `a()` costs 3. Copying adds the body to every call
|
|
670
|
+
// but one, and removes the rest.
|
|
671
|
+
const length = compactLength(text);
|
|
672
|
+
if ((calls.length - 1) * length > calls.length * 3 + 13) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (usesFunctionContext(this.context, fn)) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
for (const call of calls) {
|
|
679
|
+
if (!this.resolvesSameAt(body, call, binding)) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
const bare = isPrimary(body);
|
|
684
|
+
for (const call of calls) {
|
|
685
|
+
const parent = this.context.parents.get(call);
|
|
686
|
+
const inContainer =
|
|
687
|
+
parent?.type === 'JSXExpressionContainer' && body.type !== 'SequenceExpression';
|
|
688
|
+
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
|
|
689
|
+
this.edited.push([call.start, call.end]);
|
|
690
|
+
}
|
|
691
|
+
this.context.s.remove(statement.start, statement.end);
|
|
692
|
+
this.edited.push([statement.start, statement.end]);
|
|
693
|
+
this.changed = true;
|
|
694
|
+
}
|
|
695
|
+
|
|
519
696
|
run(): void {
|
|
520
697
|
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
521
|
-
// A memo outside any function has no owner, and disposes differently.
|
|
522
|
-
if (scope.parent === undefined) {
|
|
523
|
-
continue;
|
|
524
|
-
}
|
|
525
698
|
for (const binding of scope.bindings.values()) {
|
|
526
699
|
const { declaration } = binding;
|
|
527
700
|
if (
|
|
528
|
-
binding.kind
|
|
529
|
-
|
|
530
|
-
declaration.type
|
|
531
|
-
declaration.id === binding.identifier
|
|
701
|
+
binding.kind !== 'const' ||
|
|
702
|
+
binding.mutated ||
|
|
703
|
+
declaration.type !== 'VariableDeclarator'
|
|
532
704
|
) {
|
|
533
|
-
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
if (declaration.id.type === 'ArrayPattern') {
|
|
708
|
+
// A signal takes no owner, so one outside any function works the same.
|
|
709
|
+
this.tryInlineSignal(declaration, binding);
|
|
710
|
+
} else if (declaration.id === binding.identifier) {
|
|
711
|
+
// An arrow has no owner, so one outside any function works the same.
|
|
712
|
+
this.tryInlineArrow(declaration, binding);
|
|
713
|
+
// A memo outside any function has no owner, and disposes differently.
|
|
714
|
+
if (scope.parent !== undefined) {
|
|
715
|
+
this.tryInline(declaration, binding);
|
|
716
|
+
}
|
|
534
717
|
}
|
|
535
718
|
}
|
|
536
719
|
}
|
package/src/provider.ts
CHANGED
|
@@ -86,7 +86,7 @@ export interface Provider {
|
|
|
86
86
|
* `useContext` throws when the value and the default are both `undefined`,
|
|
87
87
|
* so a value that could be `undefined` keeps its provider.
|
|
88
88
|
*/
|
|
89
|
-
export function
|
|
89
|
+
export function findProvider(context: PassContext, element: JSXElement): Provider | undefined {
|
|
90
90
|
const binding = contextBinding(context, element.openingElement.name);
|
|
91
91
|
if (!binding || element.openingElement.selfClosing) {
|
|
92
92
|
return undefined;
|
|
@@ -278,8 +278,13 @@ function isAccessor(context: PassContext, binding: Binding): boolean {
|
|
|
278
278
|
if (id.type !== 'ArrayPattern' || callee === undefined || !ACCESSOR_PAIRS.has(callee)) {
|
|
279
279
|
return false;
|
|
280
280
|
}
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
// Only the first two slots of the pair are accessors.
|
|
282
|
+
for (const [index, element] of id.elements.entries()) {
|
|
283
|
+
if (element === binding.identifier) {
|
|
284
|
+
return index === 0 || index === 1;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return false;
|
|
283
288
|
}
|
|
284
289
|
|
|
285
290
|
export interface ClosedOptions {
|
|
@@ -457,17 +462,20 @@ function isOwnView(
|
|
|
457
462
|
if (callee !== 'merge' && callee !== 'omit') {
|
|
458
463
|
return false;
|
|
459
464
|
}
|
|
460
|
-
|
|
465
|
+
for (const argument of call.arguments) {
|
|
461
466
|
if (argument.type === 'SpreadElement') {
|
|
462
467
|
return false;
|
|
463
468
|
}
|
|
464
469
|
const inner = unwrap(argument);
|
|
465
|
-
|
|
466
|
-
inner.type
|
|
467
|
-
(inner.type === 'Literal' && typeof inner.value === 'string')
|
|
468
|
-
isOwnView(context, inner, props, depth + 1)
|
|
469
|
-
)
|
|
470
|
-
|
|
470
|
+
if (
|
|
471
|
+
inner.type !== 'ObjectExpression' &&
|
|
472
|
+
!(inner.type === 'Literal' && typeof inner.value === 'string') &&
|
|
473
|
+
!isOwnView(context, inner, props, depth + 1)
|
|
474
|
+
) {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return true;
|
|
471
479
|
}
|
|
472
480
|
|
|
473
481
|
function isVisibleElement(
|
|
@@ -533,7 +541,7 @@ function isAccessorValue(
|
|
|
533
541
|
return false;
|
|
534
542
|
}
|
|
535
543
|
if (expression.type === 'CallExpression') {
|
|
536
|
-
const value =
|
|
544
|
+
const value = findContextValue(context, expression, options);
|
|
537
545
|
return value !== undefined && isAccessorValue(context, value, options, depth + 1);
|
|
538
546
|
}
|
|
539
547
|
if (expression.type !== 'Identifier') {
|
|
@@ -569,7 +577,7 @@ function constantInit(context: PassContext, binding: Binding): Expression | unde
|
|
|
569
577
|
/**
|
|
570
578
|
* The value `useContext(Ctx)` returns, when the provider is known.
|
|
571
579
|
*/
|
|
572
|
-
function
|
|
580
|
+
function findContextValue(
|
|
573
581
|
context: PassContext,
|
|
574
582
|
call: CallExpression,
|
|
575
583
|
options: ClosedOptions,
|
|
@@ -600,18 +608,20 @@ function isVisibleMethod(
|
|
|
600
608
|
if (member.computed || member.property.type !== 'Identifier' || member.optional) {
|
|
601
609
|
return false;
|
|
602
610
|
}
|
|
603
|
-
const object =
|
|
604
|
-
if (!object
|
|
611
|
+
const object = findObjectLiteral(context, member.object, options, 0);
|
|
612
|
+
if (!object) {
|
|
605
613
|
return false;
|
|
606
614
|
}
|
|
607
615
|
const { name } = member.property;
|
|
608
|
-
const matches
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
616
|
+
const matches: typeof object.properties = [];
|
|
617
|
+
for (const property of object.properties) {
|
|
618
|
+
if (property.type !== 'Property') {
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
if (!property.computed && property.key.type === 'Identifier' && property.key.name === name) {
|
|
622
|
+
matches.push(property);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
615
625
|
const property = matches.at(0);
|
|
616
626
|
if (matches.length !== 1 || property?.type !== 'Property' || property.kind !== 'init') {
|
|
617
627
|
return false;
|
|
@@ -637,7 +647,7 @@ function isVisibleMethod(
|
|
|
637
647
|
* The object literal an expression evaluates to, through bindings that
|
|
638
648
|
* never change and reads of a context whose value is known.
|
|
639
649
|
*/
|
|
640
|
-
function
|
|
650
|
+
function findObjectLiteral(
|
|
641
651
|
context: PassContext,
|
|
642
652
|
node: Expression,
|
|
643
653
|
options: ClosedOptions,
|
|
@@ -651,15 +661,15 @@ function objectLiteralOf(
|
|
|
651
661
|
return expression;
|
|
652
662
|
}
|
|
653
663
|
if (expression.type === 'CallExpression') {
|
|
654
|
-
const value =
|
|
655
|
-
return value ?
|
|
664
|
+
const value = findContextValue(context, expression, options);
|
|
665
|
+
return value ? findObjectLiteral(context, value, options, depth + 1) : undefined;
|
|
656
666
|
}
|
|
657
667
|
if (expression.type !== 'Identifier') {
|
|
658
668
|
return undefined;
|
|
659
669
|
}
|
|
660
670
|
const binding = context.scopes.references.get(expression);
|
|
661
671
|
const init = binding ? constantInit(context, binding) : undefined;
|
|
662
|
-
return init ?
|
|
672
|
+
return init ? findObjectLiteral(context, init, options, depth + 1) : undefined;
|
|
663
673
|
}
|
|
664
674
|
|
|
665
675
|
/**
|
|
@@ -731,7 +741,7 @@ export function findProviders(context: PassContext): Provider[] {
|
|
|
731
741
|
const providers: Provider[] = [];
|
|
732
742
|
const visit = (node: Node): void => {
|
|
733
743
|
if (node.type === 'JSXElement') {
|
|
734
|
-
const provider =
|
|
744
|
+
const provider = findProvider(context, node);
|
|
735
745
|
if (provider) {
|
|
736
746
|
providers.push(provider);
|
|
737
747
|
}
|
|
@@ -754,9 +764,13 @@ class ProviderRemover {
|
|
|
754
764
|
// A provider inside one removed in this pass waits for the next pass,
|
|
755
765
|
// so their edits do not overlap.
|
|
756
766
|
const { element } = provider;
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
767
|
+
let overlaps = false;
|
|
768
|
+
for (const other of this.removed) {
|
|
769
|
+
if (element.start < other.end && other.start < element.end) {
|
|
770
|
+
overlaps = true;
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
760
774
|
if (overlaps) {
|
|
761
775
|
continue;
|
|
762
776
|
}
|
|
@@ -783,7 +797,7 @@ class ProviderRemover {
|
|
|
783
797
|
const visit = (node: Node): void => {
|
|
784
798
|
if (node.type === 'JSXElement') {
|
|
785
799
|
const binding = contextBinding(this.context, node.openingElement.name);
|
|
786
|
-
if (binding && !
|
|
800
|
+
if (binding && !findProvider(this.context, node)) {
|
|
787
801
|
// A lone `value` is turned down because it could be `undefined`.
|
|
788
802
|
const { attributes } = node.openingElement;
|
|
789
803
|
const attribute = attributes.at(0);
|
|
@@ -809,20 +823,28 @@ class ProviderRemover {
|
|
|
809
823
|
const { element } = provider;
|
|
810
824
|
const children = element.children;
|
|
811
825
|
let reason = 'open-other';
|
|
812
|
-
const
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
)
|
|
826
|
+
const closure: ClosedOptions = {
|
|
827
|
+
component: () => false,
|
|
828
|
+
contextValue: (target) => (target === provider.context ? provider.value : undefined),
|
|
829
|
+
onOpen: (open) => {
|
|
830
|
+
reason = open;
|
|
831
|
+
},
|
|
832
|
+
};
|
|
833
|
+
let closed = true;
|
|
834
|
+
for (const child of children) {
|
|
835
|
+
if (!isClosed(this.context, child, closure)) {
|
|
836
|
+
closed = false;
|
|
837
|
+
break;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
821
840
|
if (!closed) {
|
|
822
841
|
this.skip(provider, reason);
|
|
823
842
|
return false;
|
|
824
843
|
}
|
|
825
|
-
const reads
|
|
844
|
+
const reads: ReturnType<typeof contextReads> = [];
|
|
845
|
+
for (const child of children) {
|
|
846
|
+
reads.push(...contextReads(this.context, child, provider.context));
|
|
847
|
+
}
|
|
826
848
|
const texts: string[] = [];
|
|
827
849
|
for (const read of reads) {
|
|
828
850
|
const text = stableValueText(this.context, provider, read);
|
package/src/size.ts
CHANGED
|
@@ -248,7 +248,12 @@ export class SizeEstimator {
|
|
|
248
248
|
size += name.length + GETTER + this.jsx(value, false);
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
|
-
const significant =
|
|
251
|
+
const significant: JSXChild[] = [];
|
|
252
|
+
for (const child of element.children) {
|
|
253
|
+
if (!isInsignificant(child)) {
|
|
254
|
+
significant.push(child);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
252
257
|
if (significant.length > 0) {
|
|
253
258
|
size += GETTER + 'children'.length + this.children(significant);
|
|
254
259
|
}
|