solid-optimizer 0.3.0 → 1.0.0-next.0
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 +94 -42
- package/dist/{compile-BroGlagZ.d.mts → compile-BRCj4UN7.d.mts} +39 -19
- package/dist/compile-BRCj4UN7.d.mts.map +1 -0
- package/dist/{compile-ocDYvJUW.mjs → compile-Cz6hbiQv.mjs} +1312 -705
- package/dist/compile-Cz6hbiQv.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +21 -10
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +648 -269
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -11
- package/src/ast.ts +12 -4
- package/src/compile.ts +69 -42
- package/src/constants.ts +42 -2
- package/src/context.ts +32 -0
- package/src/copy-size.ts +73 -0
- package/src/fold.ts +619 -17
- package/src/index.ts +1 -0
- package/src/inline.ts +568 -299
- package/src/memo.ts +197 -33
- package/src/provider.ts +104 -82
- package/src/scope.ts +14 -1
- package/src/size.ts +35 -73
- package/src/vite/globals.ts +67 -0
- package/src/vite/index.ts +475 -217
- package/src/vite/inject.ts +51 -2
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +3 -3
- package/dist/compile-BroGlagZ.d.mts.map +0 -1
- package/dist/compile-ocDYvJUW.mjs.map +0 -1
- package/src/server.ts +0 -275
package/src/provider.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The provider pass: removes context providers whose reads are all visible.
|
|
3
3
|
*
|
|
4
|
-
* In Solid
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* directly, and the provider only costs a component call, a
|
|
9
|
-
*
|
|
4
|
+
* In Solid 2.0, a context is its own provider component. The provider reads
|
|
5
|
+
* `props.value` once, untracked, when it is created, and every
|
|
6
|
+
* `useContext(Ctx)` under it returns that value. When every code path that
|
|
7
|
+
* can run under a provider is visible, each read can take the value
|
|
8
|
+
* directly, and the provider only costs a component call, a root owner, and
|
|
9
|
+
* a template boundary:
|
|
10
10
|
*
|
|
11
11
|
* ```jsx
|
|
12
|
-
* <Theme
|
|
12
|
+
* <Theme value="dark">
|
|
13
13
|
* <span>{useContext(Theme)}</span>
|
|
14
|
-
* </Theme
|
|
14
|
+
* </Theme>
|
|
15
15
|
* ```
|
|
16
16
|
*
|
|
17
17
|
* becomes `<><span>{"dark"}</span></>`.
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
*
|
|
24
24
|
* # Accepted differences
|
|
25
25
|
*
|
|
26
|
-
* - The provider's owner is gone, which shifts the hydration keys
|
|
27
|
-
* it. A server build and its client build must compile with the
|
|
28
|
-
* options.
|
|
26
|
+
* - The provider's root owner is gone, which shifts the hydration keys
|
|
27
|
+
* after it. A server build and its client build must compile with the
|
|
28
|
+
* same options.
|
|
29
29
|
* - A getter defined by the app, other than on a component's props, that
|
|
30
30
|
* calls `useContext` is not seen.
|
|
31
31
|
*/
|
|
@@ -40,7 +40,7 @@ import type {
|
|
|
40
40
|
} from 'oxc-parser';
|
|
41
41
|
import { forEachChild, isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
42
42
|
import type { PassContext } from './context';
|
|
43
|
-
import { isConstantDeclaration, solidExport } from './context';
|
|
43
|
+
import { isConstantDeclaration, recordSkip, solidExport } from './context';
|
|
44
44
|
import type { Binding } from './scope';
|
|
45
45
|
import { lookup, scopeAt } from './scope';
|
|
46
46
|
import { evaluate, isSideEffectFree, literalText } from './value';
|
|
@@ -54,20 +54,22 @@ export const CONTEXT_SAFE_PRIMITIVES = [
|
|
|
54
54
|
'createMemo',
|
|
55
55
|
'createEffect',
|
|
56
56
|
'createRenderEffect',
|
|
57
|
-
'
|
|
57
|
+
'createStore',
|
|
58
|
+
'createProjection',
|
|
59
|
+
'createOptimistic',
|
|
58
60
|
'onCleanup',
|
|
59
|
-
'
|
|
61
|
+
'onSettled',
|
|
60
62
|
'untrack',
|
|
61
|
-
'batch',
|
|
62
63
|
'useContext',
|
|
64
|
+
'flush',
|
|
63
65
|
'mapArray',
|
|
64
|
-
'
|
|
65
|
-
'
|
|
66
|
-
'
|
|
66
|
+
'repeat',
|
|
67
|
+
'merge',
|
|
68
|
+
'omit',
|
|
67
69
|
];
|
|
68
70
|
|
|
69
71
|
/** Primitives that return a getter and a setter, as `[get, set]`. */
|
|
70
|
-
const ACCESSOR_PAIRS = new Set(['createSignal']);
|
|
72
|
+
const ACCESSOR_PAIRS = new Set(['createSignal', 'createStore', 'createOptimistic']);
|
|
71
73
|
|
|
72
74
|
export interface Provider {
|
|
73
75
|
readonly element: JSXElement;
|
|
@@ -77,15 +79,15 @@ export interface Provider {
|
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
/**
|
|
80
|
-
* The provider a JSX element creates:
|
|
81
|
-
*
|
|
82
|
-
*
|
|
82
|
+
* The provider a JSX element creates: a tag that refers to a top-level
|
|
83
|
+
* `createContext(...)` from Solid, nothing writes to, with one `value`
|
|
84
|
+
* attribute holding a value that is never `undefined`.
|
|
83
85
|
*
|
|
84
|
-
* `useContext`
|
|
85
|
-
* value that could be `undefined` keeps its provider.
|
|
86
|
+
* `useContext` throws when the value and the default are both `undefined`,
|
|
87
|
+
* so a value that could be `undefined` keeps its provider.
|
|
86
88
|
*/
|
|
87
89
|
export function providerOf(context: PassContext, element: JSXElement): Provider | undefined {
|
|
88
|
-
const binding =
|
|
90
|
+
const binding = contextBinding(context, element.openingElement.name);
|
|
89
91
|
if (!binding || element.openingElement.selfClosing) {
|
|
90
92
|
return undefined;
|
|
91
93
|
}
|
|
@@ -118,21 +120,7 @@ export function providerOf(context: PassContext, element: JSXElement): Provider
|
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
/**
|
|
121
|
-
* The context a
|
|
122
|
-
*/
|
|
123
|
-
export function providerContext(context: PassContext, name: Node): Binding | undefined {
|
|
124
|
-
if (
|
|
125
|
-
name.type !== 'JSXMemberExpression' ||
|
|
126
|
-
name.object.type !== 'JSXIdentifier' ||
|
|
127
|
-
name.property.name !== 'Provider'
|
|
128
|
-
) {
|
|
129
|
-
return undefined;
|
|
130
|
-
}
|
|
131
|
-
return contextBinding(context, name.object);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The top-level context binding an identifier refers to.
|
|
123
|
+
* The top-level context binding a tag or identifier refers to.
|
|
136
124
|
*/
|
|
137
125
|
export function contextBinding(context: PassContext, name: Node): Binding | undefined {
|
|
138
126
|
if (name.type !== 'JSXIdentifier' && name.type !== 'Identifier') {
|
|
@@ -301,6 +289,8 @@ export interface ClosedOptions {
|
|
|
301
289
|
readonly props?: Binding;
|
|
302
290
|
/** The value a read of a context gets, when the provider is known. */
|
|
303
291
|
readonly contextValue?: (target: Binding) => Expression | undefined;
|
|
292
|
+
/** Called with a short code for the code that is not visible, for the report. */
|
|
293
|
+
readonly onOpen?: (reason: string) => void;
|
|
304
294
|
}
|
|
305
295
|
|
|
306
296
|
/**
|
|
@@ -314,6 +304,10 @@ export interface ClosedOptions {
|
|
|
314
304
|
*/
|
|
315
305
|
export function isClosed(context: PassContext, root: Node, options: ClosedOptions): boolean {
|
|
316
306
|
let closed = true;
|
|
307
|
+
const open = (reason: string): void => {
|
|
308
|
+
closed = false;
|
|
309
|
+
options.onOpen?.(reason);
|
|
310
|
+
};
|
|
317
311
|
const visit = (node: Node): void => {
|
|
318
312
|
if (!closed) {
|
|
319
313
|
return;
|
|
@@ -327,35 +321,37 @@ export function isClosed(context: PassContext, root: Node, options: ClosedOption
|
|
|
327
321
|
case 'JSXSpreadAttribute':
|
|
328
322
|
// A view of the component's own props is expanded when it inlines.
|
|
329
323
|
if (!isOwnView(context, node.argument, options.props, 0)) {
|
|
330
|
-
|
|
324
|
+
open('open-spread');
|
|
331
325
|
return;
|
|
332
326
|
}
|
|
333
327
|
break;
|
|
334
|
-
case 'JSXSpreadChild':
|
|
335
328
|
case 'NewExpression':
|
|
329
|
+
open('open-new');
|
|
330
|
+
return;
|
|
331
|
+
case 'JSXSpreadChild':
|
|
336
332
|
case 'TaggedTemplateExpression':
|
|
337
333
|
case 'ImportExpression':
|
|
338
|
-
|
|
334
|
+
open('open-other');
|
|
339
335
|
return;
|
|
340
336
|
case 'JSXElement':
|
|
341
337
|
if (!isVisibleElement(context, node, options)) {
|
|
342
|
-
|
|
338
|
+
open('open-component');
|
|
343
339
|
return;
|
|
344
340
|
}
|
|
345
341
|
break;
|
|
346
342
|
case 'CallExpression':
|
|
347
343
|
if (!isVisibleCall(context, node, options)) {
|
|
348
|
-
|
|
344
|
+
open('open-call');
|
|
349
345
|
return;
|
|
350
346
|
}
|
|
351
347
|
break;
|
|
352
348
|
case 'Identifier': {
|
|
353
349
|
// Another component's props run its parent's code when read, and
|
|
354
|
-
// passing them on, as to `
|
|
350
|
+
// passing them on, as to `merge()`, reads them somewhere else. A
|
|
355
351
|
// binding declared under `root`, like a callback's parameter, is a value.
|
|
356
352
|
const binding = context.scopes.references.get(node);
|
|
357
353
|
if (binding && runsOutsideCode(context, binding, root, options, 0)) {
|
|
358
|
-
|
|
354
|
+
open('open-outer');
|
|
359
355
|
return;
|
|
360
356
|
}
|
|
361
357
|
break;
|
|
@@ -432,8 +428,8 @@ function runsOutsideCode(
|
|
|
432
428
|
}
|
|
433
429
|
|
|
434
430
|
/**
|
|
435
|
-
* Whether an expression is a `
|
|
436
|
-
*
|
|
431
|
+
* Whether an expression is a `merge()` or `omit()` view of a component's
|
|
432
|
+
* own props, with only literal objects and keys besides.
|
|
437
433
|
*/
|
|
438
434
|
function isOwnView(
|
|
439
435
|
context: PassContext,
|
|
@@ -451,13 +447,14 @@ function isOwnView(
|
|
|
451
447
|
if (binding === props) {
|
|
452
448
|
return true;
|
|
453
449
|
}
|
|
454
|
-
|
|
450
|
+
const init = binding ? constantInit(context, binding) : undefined;
|
|
451
|
+
call = init ? unwrap(init) : undefined;
|
|
455
452
|
}
|
|
456
453
|
if (call?.type !== 'CallExpression') {
|
|
457
454
|
return false;
|
|
458
455
|
}
|
|
459
456
|
const callee = solidCallee(context, call);
|
|
460
|
-
if (callee !== '
|
|
457
|
+
if (callee !== 'merge' && callee !== 'omit') {
|
|
461
458
|
return false;
|
|
462
459
|
}
|
|
463
460
|
return call.arguments.every((argument) => {
|
|
@@ -467,51 +464,27 @@ function isOwnView(
|
|
|
467
464
|
const inner = unwrap(argument);
|
|
468
465
|
return (
|
|
469
466
|
inner.type === 'ObjectExpression' ||
|
|
470
|
-
(inner.type === '
|
|
471
|
-
inner.elements.every((key) => key?.type === 'Literal' && typeof key.value === 'string')) ||
|
|
467
|
+
(inner.type === 'Literal' && typeof inner.value === 'string') ||
|
|
472
468
|
isOwnView(context, inner, props, depth + 1)
|
|
473
469
|
);
|
|
474
470
|
});
|
|
475
471
|
}
|
|
476
472
|
|
|
477
|
-
/**
|
|
478
|
-
* The call a view binding comes from: `const view = mergeProps(...)`, or
|
|
479
|
-
* one of the names in `const [a, b] = splitProps(...)`.
|
|
480
|
-
*/
|
|
481
|
-
function viewInit(context: PassContext, binding: Binding): Node | undefined {
|
|
482
|
-
const init = constantInit(context, binding);
|
|
483
|
-
if (init) {
|
|
484
|
-
return unwrap(init);
|
|
485
|
-
}
|
|
486
|
-
const { declaration } = binding;
|
|
487
|
-
if (
|
|
488
|
-
binding.mutated ||
|
|
489
|
-
binding.kind !== 'const' ||
|
|
490
|
-
declaration.type !== 'VariableDeclarator' ||
|
|
491
|
-
declaration.id.type !== 'ArrayPattern' ||
|
|
492
|
-
!declaration.id.elements.some((element) => element === binding.identifier) ||
|
|
493
|
-
!declaration.init
|
|
494
|
-
) {
|
|
495
|
-
return undefined;
|
|
496
|
-
}
|
|
497
|
-
return unwrap(declaration.init);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
473
|
function isVisibleElement(
|
|
501
474
|
context: PassContext,
|
|
502
475
|
element: JSXElement,
|
|
503
476
|
options: ClosedOptions,
|
|
504
477
|
): boolean {
|
|
505
478
|
const { name } = element.openingElement;
|
|
506
|
-
if (providerContext(context, name)) {
|
|
507
|
-
return true;
|
|
508
|
-
}
|
|
509
479
|
if (name.type !== 'JSXIdentifier') {
|
|
510
480
|
return false;
|
|
511
481
|
}
|
|
512
482
|
if (isIntrinsicTag(name.name)) {
|
|
513
483
|
return true;
|
|
514
484
|
}
|
|
485
|
+
if (contextBinding(context, name)) {
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
515
488
|
const binding = context.scopes.references.get(name);
|
|
516
489
|
const identity = binding ? solidExport(binding, context.options) : name.name;
|
|
517
490
|
// `<Dynamic>` renders a component it is given, which is not visible.
|
|
@@ -704,7 +677,7 @@ export function contextReads(context: PassContext, root: Node, target: Binding):
|
|
|
704
677
|
if (
|
|
705
678
|
node !== root &&
|
|
706
679
|
node.type === 'JSXElement' &&
|
|
707
|
-
|
|
680
|
+
contextBinding(context, node.openingElement.name) === target
|
|
708
681
|
) {
|
|
709
682
|
return;
|
|
710
683
|
}
|
|
@@ -738,7 +711,7 @@ export function hasNestedProvider(context: PassContext, provider: Provider): boo
|
|
|
738
711
|
if (
|
|
739
712
|
node !== provider.element &&
|
|
740
713
|
node.type === 'JSXElement' &&
|
|
741
|
-
|
|
714
|
+
contextBinding(context, node.openingElement.name) === provider.context
|
|
742
715
|
) {
|
|
743
716
|
nested = true;
|
|
744
717
|
return;
|
|
@@ -784,22 +757,69 @@ class ProviderRemover {
|
|
|
784
757
|
const overlaps = this.removed.some(
|
|
785
758
|
(other) => element.start < other.end && other.start < element.end,
|
|
786
759
|
);
|
|
787
|
-
if (
|
|
760
|
+
if (overlaps) {
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
if (hasNestedProvider(this.context, provider)) {
|
|
764
|
+
this.skip(provider, 'provider-nested');
|
|
765
|
+
} else if (this.tryRemove(provider)) {
|
|
788
766
|
this.removed.push(element);
|
|
789
767
|
}
|
|
790
768
|
}
|
|
769
|
+
if (this.context.skips) {
|
|
770
|
+
this.reportShapes();
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
private skip(provider: Provider, reason: string): void {
|
|
775
|
+
recordSkip(this.context, 'contexts', reason, provider.context.name);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Records each context tag that is not a provider this pass understands,
|
|
780
|
+
* such as one with more attributes than `value`.
|
|
781
|
+
*/
|
|
782
|
+
private reportShapes(): void {
|
|
783
|
+
const visit = (node: Node): void => {
|
|
784
|
+
if (node.type === 'JSXElement') {
|
|
785
|
+
const binding = contextBinding(this.context, node.openingElement.name);
|
|
786
|
+
if (binding && !providerOf(this.context, node)) {
|
|
787
|
+
// A lone `value` is turned down because it could be `undefined`.
|
|
788
|
+
const { attributes } = node.openingElement;
|
|
789
|
+
const attribute = attributes.at(0);
|
|
790
|
+
const onlyValue =
|
|
791
|
+
attributes.length === 1 &&
|
|
792
|
+
attribute?.type === 'JSXAttribute' &&
|
|
793
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
794
|
+
attribute.name.name === 'value';
|
|
795
|
+
recordSkip(
|
|
796
|
+
this.context,
|
|
797
|
+
'contexts',
|
|
798
|
+
onlyValue && !node.openingElement.selfClosing ? 'provider-undefined' : 'provider-shape',
|
|
799
|
+
binding.name,
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
forEachChild(node, visit);
|
|
804
|
+
};
|
|
805
|
+
visit(this.context.program);
|
|
791
806
|
}
|
|
792
807
|
|
|
793
808
|
private tryRemove(provider: Provider): boolean {
|
|
794
809
|
const { element } = provider;
|
|
795
810
|
const children = element.children;
|
|
811
|
+
let reason = 'open-other';
|
|
796
812
|
const closed = children.every((child) =>
|
|
797
813
|
isClosed(this.context, child, {
|
|
798
814
|
component: () => false,
|
|
799
815
|
contextValue: (target) => (target === provider.context ? provider.value : undefined),
|
|
816
|
+
onOpen: (open) => {
|
|
817
|
+
reason = open;
|
|
818
|
+
},
|
|
800
819
|
}),
|
|
801
820
|
);
|
|
802
821
|
if (!closed) {
|
|
822
|
+
this.skip(provider, reason);
|
|
803
823
|
return false;
|
|
804
824
|
}
|
|
805
825
|
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
@@ -807,6 +827,7 @@ class ProviderRemover {
|
|
|
807
827
|
for (const read of reads) {
|
|
808
828
|
const text = stableValueText(this.context, provider, read);
|
|
809
829
|
if (text === undefined) {
|
|
830
|
+
this.skip(provider, 'provider-value');
|
|
810
831
|
return false;
|
|
811
832
|
}
|
|
812
833
|
texts.push(text);
|
|
@@ -815,6 +836,7 @@ class ProviderRemover {
|
|
|
815
836
|
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
816
837
|
const stable = stableValueText(this.context, provider, element);
|
|
817
838
|
if (stable === undefined) {
|
|
839
|
+
this.skip(provider, 'provider-value');
|
|
818
840
|
return false;
|
|
819
841
|
}
|
|
820
842
|
}
|
package/src/scope.ts
CHANGED
|
@@ -385,6 +385,11 @@ class Analyzer {
|
|
|
385
385
|
this.reference(node, false);
|
|
386
386
|
break;
|
|
387
387
|
case 'VariableDeclaration': {
|
|
388
|
+
// `declare const` only tells TypeScript about a global. It is stripped
|
|
389
|
+
// with the types and declares nothing, so its name stays global.
|
|
390
|
+
if (node.declare) {
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
388
393
|
// `using` binds a disposable resource, so it is never a folding candidate.
|
|
389
394
|
let kind: BindingKind = 'other';
|
|
390
395
|
if (node.kind === 'var' || node.kind === 'let' || node.kind === 'const') {
|
|
@@ -399,12 +404,16 @@ class Analyzer {
|
|
|
399
404
|
break;
|
|
400
405
|
}
|
|
401
406
|
case 'FunctionDeclaration':
|
|
402
|
-
case 'TSDeclareFunction':
|
|
403
407
|
if (node.id) {
|
|
404
408
|
this.declare(node.id, 'function', node);
|
|
405
409
|
}
|
|
406
410
|
this.visitFunction(node);
|
|
407
411
|
break;
|
|
412
|
+
// An overload signature or a `declare function` has no body and is
|
|
413
|
+
// stripped with the types. The implementation declares the name, if there is one.
|
|
414
|
+
case 'TSDeclareFunction':
|
|
415
|
+
this.visitFunction(node);
|
|
416
|
+
break;
|
|
408
417
|
case 'FunctionExpression':
|
|
409
418
|
this.visitFunction(node);
|
|
410
419
|
break;
|
|
@@ -422,6 +431,10 @@ class Analyzer {
|
|
|
422
431
|
});
|
|
423
432
|
break;
|
|
424
433
|
case 'ClassDeclaration':
|
|
434
|
+
// A `declare class` is stripped with the types, like `declare const`.
|
|
435
|
+
if (node.declare) {
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
425
438
|
if (node.id) {
|
|
426
439
|
this.declare(node.id, 'class', node);
|
|
427
440
|
}
|
package/src/size.ts
CHANGED
|
@@ -17,26 +17,17 @@
|
|
|
17
17
|
* Other code counts as its text without the whitespace a minifier removes.
|
|
18
18
|
*/
|
|
19
19
|
import type {
|
|
20
|
+
Expression,
|
|
20
21
|
JSXAttribute,
|
|
21
22
|
JSXAttributeItem,
|
|
22
23
|
JSXChild,
|
|
23
24
|
JSXElement,
|
|
24
25
|
JSXElementName,
|
|
26
|
+
JSXEmptyExpression,
|
|
25
27
|
JSXFragment,
|
|
26
28
|
Node,
|
|
27
29
|
} from 'oxc-parser';
|
|
28
|
-
import
|
|
29
|
-
import {
|
|
30
|
-
collectParents,
|
|
31
|
-
forEachChild,
|
|
32
|
-
isInsignificant,
|
|
33
|
-
isIntrinsicTag,
|
|
34
|
-
parse,
|
|
35
|
-
unwrap,
|
|
36
|
-
} from './ast';
|
|
37
|
-
import type { ResolvedOptions } from './context';
|
|
38
|
-
import { fold } from './fold';
|
|
39
|
-
import { analyzeScopes } from './scope';
|
|
30
|
+
import { forEachChild, isInsignificant, isIntrinsicTag, unwrap } from './ast';
|
|
40
31
|
|
|
41
32
|
/** A template's declaration, and the function that clones and returns it. */
|
|
42
33
|
const TEMPLATE = 40;
|
|
@@ -77,6 +68,26 @@ function nameLength(name: JSXElementName): number {
|
|
|
77
68
|
}
|
|
78
69
|
}
|
|
79
70
|
|
|
71
|
+
/** Whether an expression lowers to text in the template, like a literal. */
|
|
72
|
+
function isStaticText(node: Expression | JSXEmptyExpression): boolean {
|
|
73
|
+
if (node.type === 'JSXEmptyExpression') {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const inner = unwrap(node);
|
|
77
|
+
if (inner.type === 'Literal') {
|
|
78
|
+
return typeof inner.value === 'string' || typeof inner.value === 'number';
|
|
79
|
+
}
|
|
80
|
+
if (inner.type === 'TemplateLiteral') {
|
|
81
|
+
return inner.expressions.length === 0;
|
|
82
|
+
}
|
|
83
|
+
return (
|
|
84
|
+
inner.type === 'UnaryExpression' &&
|
|
85
|
+
inner.operator === '-' &&
|
|
86
|
+
inner.argument.type === 'Literal' &&
|
|
87
|
+
typeof inner.argument.value === 'number'
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
80
91
|
function isIntrinsic(element: JSXElement): boolean {
|
|
81
92
|
const { name } = element.openingElement;
|
|
82
93
|
return name.type === 'JSXIdentifier' && isIntrinsicTag(name.name);
|
|
@@ -142,6 +153,9 @@ export class SizeEstimator {
|
|
|
142
153
|
}
|
|
143
154
|
if (child.type === 'JSXText') {
|
|
144
155
|
size += compactLength(child.value);
|
|
156
|
+
} else if (child.type === 'JSXExpressionContainer' && isStaticText(child.expression)) {
|
|
157
|
+
// A literal child is written into the template as text.
|
|
158
|
+
size += this.text(child.expression);
|
|
145
159
|
} else if (child.type === 'JSXElement') {
|
|
146
160
|
size += this.jsx(child, true);
|
|
147
161
|
} else {
|
|
@@ -240,68 +254,16 @@ export class SizeEstimator {
|
|
|
240
254
|
}
|
|
241
255
|
return size + (spread ? MERGE : 0);
|
|
242
256
|
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** The name of the function a copy is wrapped in to be folded and measured. */
|
|
246
|
-
const WRAPPER = '__so_size__';
|
|
247
|
-
|
|
248
|
-
export interface Copy {
|
|
249
|
-
/** The statements the copy hoists out of the JSX. */
|
|
250
|
-
readonly statements: readonly string[];
|
|
251
|
-
/** The JSX that replaces the call site. */
|
|
252
|
-
readonly root: string;
|
|
253
|
-
}
|
|
254
257
|
|
|
255
|
-
/**
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
header: string,
|
|
264
|
-
copy: Copy,
|
|
265
|
-
inTemplate: boolean,
|
|
266
|
-
options: ResolvedOptions,
|
|
267
|
-
): number {
|
|
268
|
-
let code = `${header}\nfunction ${WRAPPER}() {\n${copy.statements.join('\n')}\nreturn (${copy.root});\n}\n`;
|
|
269
|
-
// Constant props fold the copy's conditions, as later passes would.
|
|
270
|
-
for (let round = 0; round < 3; round += 1) {
|
|
271
|
-
const program = parse(filename, code);
|
|
272
|
-
const s = new MagicString(code);
|
|
273
|
-
const changed = fold({
|
|
274
|
-
code,
|
|
275
|
-
filename,
|
|
276
|
-
s,
|
|
277
|
-
program,
|
|
278
|
-
parents: collectParents(program),
|
|
279
|
-
scopes: analyzeScopes(program),
|
|
280
|
-
options,
|
|
281
|
-
});
|
|
282
|
-
if (!changed) {
|
|
283
|
-
break;
|
|
284
|
-
}
|
|
285
|
-
code = s.toString();
|
|
286
|
-
}
|
|
287
|
-
const program = parse(filename, code);
|
|
288
|
-
const wrapper = program.body.at(-1);
|
|
289
|
-
if (wrapper?.type !== 'FunctionDeclaration' || !wrapper.body) {
|
|
290
|
-
return Number.POSITIVE_INFINITY;
|
|
291
|
-
}
|
|
292
|
-
const estimator = new SizeEstimator(code);
|
|
293
|
-
let size = 0;
|
|
294
|
-
for (const statement of wrapper.body.body) {
|
|
295
|
-
if (statement.type !== 'ReturnStatement') {
|
|
296
|
-
size += estimator.node(statement);
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
const argument = statement.argument ? unwrap(statement.argument) : undefined;
|
|
300
|
-
if (argument?.type === 'JSXElement' || argument?.type === 'JSXFragment') {
|
|
301
|
-
size += estimator.jsx(argument, inTemplate);
|
|
302
|
-
} else if (argument) {
|
|
303
|
-
size += (inTemplate ? INSERT + WALK : 0) + estimator.node(argument);
|
|
258
|
+
/**
|
|
259
|
+
* The size of what a component returns. JSX lowers as JSX, and anything
|
|
260
|
+
* else is inserted when it sits in an element.
|
|
261
|
+
*/
|
|
262
|
+
root(node: Expression, inTemplate: boolean): number {
|
|
263
|
+
const inner = unwrap(node);
|
|
264
|
+
if (inner.type === 'JSXElement' || inner.type === 'JSXFragment') {
|
|
265
|
+
return this.jsx(inner, inTemplate);
|
|
304
266
|
}
|
|
267
|
+
return (inTemplate ? INSERT + WALK : 0) + this.node(inner);
|
|
305
268
|
}
|
|
306
|
-
return size;
|
|
307
269
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The global constants every build environment shares, from Vite's `define`
|
|
3
|
+
* and `import.meta.env`.
|
|
4
|
+
*/
|
|
5
|
+
import type { ResolvedConfig } from 'vite';
|
|
6
|
+
import type { Primitive } from '../value';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The `define` entries and `import.meta.env` values that are the same in
|
|
10
|
+
* every build environment, as global constants. A value that differs, like
|
|
11
|
+
* `import.meta.env.SSR`, could make the server and client builds fold, and
|
|
12
|
+
* then inline, differently, so their hydration keys would not match.
|
|
13
|
+
*/
|
|
14
|
+
export function sharedGlobals(
|
|
15
|
+
config: Pick<ResolvedConfig, 'env' | 'define' | 'environments'>,
|
|
16
|
+
): Record<string, Primitive> {
|
|
17
|
+
const globals: Record<string, Primitive> = {};
|
|
18
|
+
for (const [key, value] of Object.entries(config.env)) {
|
|
19
|
+
if (
|
|
20
|
+
key !== 'SSR' &&
|
|
21
|
+
(typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
22
|
+
) {
|
|
23
|
+
globals[`import.meta.env.${key}`] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const environments = Object.values(config.environments);
|
|
27
|
+
const defines =
|
|
28
|
+
environments.length > 0
|
|
29
|
+
? environments.map((environment) => environment.define ?? config.define ?? {})
|
|
30
|
+
: [config.define ?? {}];
|
|
31
|
+
const keys = new Set(defines.flatMap((define) => Object.keys(define)));
|
|
32
|
+
for (const key of keys) {
|
|
33
|
+
const values = defines.map((define) => defineValue(define[key]));
|
|
34
|
+
const first = values.at(0);
|
|
35
|
+
if (
|
|
36
|
+
first !== undefined &&
|
|
37
|
+
values.every((value) => value !== undefined && Object.is(value.value, first.value))
|
|
38
|
+
) {
|
|
39
|
+
globals[key] = first.value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return globals;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The primitive a `define` entry replaces its key with. A string is code,
|
|
47
|
+
* so only one that is a JSON literal counts.
|
|
48
|
+
*/
|
|
49
|
+
function defineValue(value: unknown): { value: Primitive } | undefined {
|
|
50
|
+
let parsed: unknown = value;
|
|
51
|
+
if (typeof value === 'string') {
|
|
52
|
+
try {
|
|
53
|
+
parsed = JSON.parse(value);
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (
|
|
59
|
+
parsed === null ||
|
|
60
|
+
typeof parsed === 'string' ||
|
|
61
|
+
typeof parsed === 'number' ||
|
|
62
|
+
typeof parsed === 'boolean'
|
|
63
|
+
) {
|
|
64
|
+
return { value: parsed };
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|