solid-optimizer 1.0.0-next.0 → 1.0.0-next.2
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 +15 -8
- package/dist/{compile-BRCj4UN7.d.mts → compile-CwH9mvHD.d.mts} +7 -1
- package/dist/compile-CwH9mvHD.d.mts.map +1 -0
- package/dist/{compile-Cz6hbiQv.mjs → compile-sCZdwEKG.mjs} +304 -45
- package/dist/compile-sCZdwEKG.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +11 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +151 -27
- package/dist/vite.mjs.map +1 -1
- package/package.json +4 -4
- package/src/compile.ts +10 -2
- package/src/constants.ts +27 -8
- package/src/context.ts +2 -0
- package/src/copy-size.ts +64 -24
- package/src/fold.ts +32 -3
- package/src/inline.ts +343 -11
- package/src/vite/index.ts +174 -32
- package/src/vite/inject.ts +64 -1
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
|
@@ -896,16 +896,32 @@ var ConstantTable = class {
|
|
|
896
896
|
/**
|
|
897
897
|
* The modules `program` imports or re-exports bindings from. Their
|
|
898
898
|
* constant exports are what `importedConstants` can hold.
|
|
899
|
+
*
|
|
900
|
+
* An import counts only when one of its bindings is read as a value. A
|
|
901
|
+
* binding read only in types has no constant to fold, and TypeScript drops its
|
|
902
|
+
* import, so the module it names may not even build for this target.
|
|
899
903
|
*/
|
|
900
|
-
function namedImportSources(program) {
|
|
904
|
+
function namedImportSources(program, scopes = analyzeScopes(program)) {
|
|
901
905
|
const sources = /* @__PURE__ */ new Set();
|
|
902
906
|
for (const statement of program.body) if (statement.type === "ImportDeclaration") {
|
|
903
|
-
if (statement.importKind !== "type" && statement.specifiers
|
|
907
|
+
if (statement.importKind !== "type" && readsAsValue(statement.specifiers, scopes)) sources.add(statement.source.value);
|
|
904
908
|
} else if (statement.type === "ExportNamedDeclaration" && statement.source || statement.type === "ExportAllDeclaration" && !statement.exported) {
|
|
905
909
|
if (statement.exportKind !== "type" && statement.source) sources.add(statement.source.value);
|
|
906
910
|
}
|
|
907
911
|
return [...sources];
|
|
908
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* Whether the module reads one of these import bindings as a value. Scope
|
|
915
|
+
* analysis skips type positions, so a binding used only in types has no
|
|
916
|
+
* references.
|
|
917
|
+
*/
|
|
918
|
+
function readsAsValue(specifiers, scopes) {
|
|
919
|
+
for (const specifier of specifiers) {
|
|
920
|
+
if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") continue;
|
|
921
|
+
if (scopes.root.bindings.get(specifier.local.name)?.references.length) return true;
|
|
922
|
+
}
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
909
925
|
function exportName(node) {
|
|
910
926
|
if (node.type === "Identifier") return node.name;
|
|
911
927
|
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
@@ -1417,7 +1433,9 @@ var Folder = class {
|
|
|
1417
1433
|
case "MemberExpression":
|
|
1418
1434
|
if (!isWriteTarget(node, this.context.parents)) this.foldConstant(node);
|
|
1419
1435
|
break;
|
|
1420
|
-
case "CallExpression":
|
|
1436
|
+
case "CallExpression":
|
|
1437
|
+
this.annotatePure(node);
|
|
1438
|
+
this.foldUntrack(node);
|
|
1421
1439
|
}
|
|
1422
1440
|
}
|
|
1423
1441
|
isJSXChildOrAttribute(node) {
|
|
@@ -1445,9 +1463,23 @@ var Folder = class {
|
|
|
1445
1463
|
this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
|
|
1446
1464
|
this.changed = true;
|
|
1447
1465
|
}
|
|
1448
|
-
|
|
1466
|
+
/**
|
|
1467
|
+
* `untrack(() => value)` is the value, when the value is a constant.
|
|
1468
|
+
* Nothing is read, so there is nothing to untrack. A component inlined
|
|
1469
|
+
* with a literal prop leaves this, like Kobalte's `untrack(() => props.as)`.
|
|
1470
|
+
*/
|
|
1471
|
+
foldUntrack(call) {
|
|
1472
|
+
if (call.callee.type !== "Identifier" || call.arguments.length !== 1) return;
|
|
1473
|
+
const binding = this.context.scopes.references.get(call.callee);
|
|
1474
|
+
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
1475
|
+
const callback = call.arguments.at(0);
|
|
1476
|
+
if (identity !== "untrack" || callback?.type !== "ArrowFunctionExpression" || callback.async || callback.params.length > 0 || !callback.expression || callback.body.type === "BlockStatement") return;
|
|
1477
|
+
const value = evaluate(callback.body, this.lookup);
|
|
1478
|
+
if (value) this.foldConstant(call, value);
|
|
1479
|
+
}
|
|
1480
|
+
foldConstant(node, known) {
|
|
1449
1481
|
if (isLiteralForm(node)) return;
|
|
1450
|
-
const value = evaluate(node, this.lookup);
|
|
1482
|
+
const value = known ?? evaluate(node, this.lookup);
|
|
1451
1483
|
if (!value) return;
|
|
1452
1484
|
let text = literalText(value.value);
|
|
1453
1485
|
if (text === void 0) return;
|
|
@@ -2565,34 +2597,61 @@ function removeProviders(context) {
|
|
|
2565
2597
|
//#endregion
|
|
2566
2598
|
//#region src/copy-size.ts
|
|
2567
2599
|
/**
|
|
2568
|
-
* Measures a component's copy at a call site: folds it
|
|
2569
|
-
*
|
|
2600
|
+
* Measures a component's copy at a call site: folds it, and inlines the
|
|
2601
|
+
* components it calls, as the passes after inlining would. Then it estimates
|
|
2602
|
+
* its size with `SizeEstimator`.
|
|
2603
|
+
*/
|
|
2604
|
+
/**
|
|
2605
|
+
* The name of the function a copy is wrapped in to be folded and measured.
|
|
2606
|
+
* It is capitalized, so the inline pass can move statements into it.
|
|
2570
2607
|
*/
|
|
2571
|
-
|
|
2572
|
-
|
|
2608
|
+
const WRAPPER = "SoSize$$";
|
|
2609
|
+
/**
|
|
2610
|
+
* How many copies are being measured inside each other. Components that
|
|
2611
|
+
* call each other would otherwise measure each other without end.
|
|
2612
|
+
*/
|
|
2613
|
+
let depth = 0;
|
|
2614
|
+
const MAX_DEPTH = 3;
|
|
2573
2615
|
/**
|
|
2574
2616
|
* The size of a component's copy at one call site, once its props have
|
|
2575
2617
|
* folded. `header` declares the names the fold pass needs to know, like
|
|
2576
|
-
* imports of Solid. `
|
|
2577
|
-
*
|
|
2618
|
+
* imports of Solid. `called` holds the module's components the copy calls,
|
|
2619
|
+
* which inline into it when its props resolve their call sites.
|
|
2620
|
+
* `inTemplate` is whether the call site sits directly in an element.
|
|
2621
|
+
* `measured` receives the code that was measured.
|
|
2578
2622
|
*/
|
|
2579
|
-
function copySize(filename, header, copy, inTemplate, options) {
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
code
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2623
|
+
function copySize(filename, header, copy, inTemplate, options, called, measured) {
|
|
2624
|
+
const calls = depth < MAX_DEPTH ? called : void 0;
|
|
2625
|
+
let code = `${header}\n${calls?.declarations ?? ""}\nfunction ${WRAPPER}() {\n${copy.statements.join("\n")}\nreturn (${copy.root});\n}\n`;
|
|
2626
|
+
for (let round = 0; round < 4; round += 1) {
|
|
2627
|
+
let changed = false;
|
|
2628
|
+
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
2629
|
+
const program = parse(filename, code);
|
|
2630
|
+
const s = new MagicString(code);
|
|
2631
|
+
depth += 1;
|
|
2632
|
+
let passChanged;
|
|
2633
|
+
try {
|
|
2634
|
+
passChanged = pass({
|
|
2635
|
+
code,
|
|
2636
|
+
filename,
|
|
2637
|
+
s,
|
|
2638
|
+
program,
|
|
2639
|
+
parents: collectParents(program),
|
|
2640
|
+
scopes: analyzeScopes(program),
|
|
2641
|
+
options,
|
|
2642
|
+
skips: void 0
|
|
2643
|
+
});
|
|
2644
|
+
} finally {
|
|
2645
|
+
depth -= 1;
|
|
2646
|
+
}
|
|
2647
|
+
if (passChanged) {
|
|
2648
|
+
code = s.toString();
|
|
2649
|
+
changed = true;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
if (!changed) break;
|
|
2595
2653
|
}
|
|
2654
|
+
if (measured) measured.code = code;
|
|
2596
2655
|
const wrapper = parse(filename, code).body.at(-1);
|
|
2597
2656
|
if (wrapper?.type !== "FunctionDeclaration" || !wrapper.body) return Number.POSITIVE_INFINITY;
|
|
2598
2657
|
const estimator = new SizeEstimator(code);
|
|
@@ -2618,7 +2677,9 @@ function copySize(filename, header, copy, inTemplate, options) {
|
|
|
2618
2677
|
* A component inlines when its shape keeps the result equivalent:
|
|
2619
2678
|
*
|
|
2620
2679
|
* - It is declared once at the top level of the module, with a capitalized
|
|
2621
|
-
* name, as a function declaration or a `const` bound to a function.
|
|
2680
|
+
* name, as a function declaration or a `const` bound to a function. A
|
|
2681
|
+
* tag of an alias, like `const Separator = SeparatorRoot`, is renamed to
|
|
2682
|
+
* the component first. See `renameAliasTags`.
|
|
2622
2683
|
* - It takes no parameter, or one identifier that is only read as `props.name`,
|
|
2623
2684
|
* or spread onto an element as `{...props}`.
|
|
2624
2685
|
* - Its body is statements followed by one final `return` of JSX, or an arrow
|
|
@@ -2706,6 +2767,12 @@ const UNDEFINED = {
|
|
|
2706
2767
|
text: "void 0",
|
|
2707
2768
|
primary: false
|
|
2708
2769
|
};
|
|
2770
|
+
/** The function a copy is wrapped in when it is measured. See `copy-size.ts`. */
|
|
2771
|
+
const COPY_WRAPPER = "function SoSize$$(";
|
|
2772
|
+
/** Whether code has a JSX tag named `name`. */
|
|
2773
|
+
function hasTag(code, name) {
|
|
2774
|
+
return new RegExp(`<${name.replaceAll("$", String.raw`\$`)}[\\s/>]`).test(code);
|
|
2775
|
+
}
|
|
2709
2776
|
function isCapitalized(name) {
|
|
2710
2777
|
return /^[A-Z]/.test(name);
|
|
2711
2778
|
}
|
|
@@ -2761,6 +2828,38 @@ function containsReturn(node) {
|
|
|
2761
2828
|
return found;
|
|
2762
2829
|
}
|
|
2763
2830
|
/**
|
|
2831
|
+
* Collects the nodes an assignment target writes through: an identifier, or
|
|
2832
|
+
* the root of a member chain, like `cache` in `cache.items[0] = x`. A root
|
|
2833
|
+
* that is not a name, like a call, is collected as it is.
|
|
2834
|
+
*/
|
|
2835
|
+
function collectWriteRoots(target, roots) {
|
|
2836
|
+
switch (target.type) {
|
|
2837
|
+
case "MemberExpression":
|
|
2838
|
+
collectWriteRoots(target.object, roots);
|
|
2839
|
+
break;
|
|
2840
|
+
case "ParenthesizedExpression":
|
|
2841
|
+
case "TSAsExpression":
|
|
2842
|
+
case "TSSatisfiesExpression":
|
|
2843
|
+
case "TSNonNullExpression":
|
|
2844
|
+
case "TSTypeAssertion":
|
|
2845
|
+
collectWriteRoots(target.expression, roots);
|
|
2846
|
+
break;
|
|
2847
|
+
case "ObjectPattern":
|
|
2848
|
+
for (const property of target.properties) collectWriteRoots(property.type === "RestElement" ? property.argument : property.value, roots);
|
|
2849
|
+
break;
|
|
2850
|
+
case "ArrayPattern":
|
|
2851
|
+
for (const element of target.elements) if (element) collectWriteRoots(element, roots);
|
|
2852
|
+
break;
|
|
2853
|
+
case "AssignmentPattern":
|
|
2854
|
+
collectWriteRoots(target.left, roots);
|
|
2855
|
+
break;
|
|
2856
|
+
case "RestElement":
|
|
2857
|
+
collectWriteRoots(target.argument, roots);
|
|
2858
|
+
break;
|
|
2859
|
+
default: roots.push(target);
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2862
|
+
/**
|
|
2764
2863
|
* Whether Solid would pass an attribute expression as a getter. The JSX
|
|
2765
2864
|
* transform wraps any expression that reads a member or calls a function.
|
|
2766
2865
|
*/
|
|
@@ -2822,10 +2921,10 @@ function functionOf(binding, options) {
|
|
|
2822
2921
|
/**
|
|
2823
2922
|
* The attributes of a call site by name, or `undefined` when one cannot be inlined.
|
|
2824
2923
|
*/
|
|
2825
|
-
function attributesOf(element) {
|
|
2924
|
+
function attributesOf(context, element) {
|
|
2826
2925
|
const attributes = /* @__PURE__ */ new Map();
|
|
2827
2926
|
for (const attribute of element.openingElement.attributes) {
|
|
2828
|
-
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(attribute) || attributes.has(attribute.name.name)) return;
|
|
2927
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(context, attribute) || attributes.has(attribute.name.name)) return;
|
|
2829
2928
|
attributes.set(attribute.name.name, attribute);
|
|
2830
2929
|
}
|
|
2831
2930
|
return attributes;
|
|
@@ -2862,16 +2961,30 @@ function isMemberRead(read) {
|
|
|
2862
2961
|
* assigned the element, so it has to stay the name itself. Anything else
|
|
2863
2962
|
* would be evaluated when the element is made, outside the `untrack` the
|
|
2864
2963
|
* component's `ref` runs it in.
|
|
2964
|
+
*
|
|
2965
|
+
* Inside an array, `void 0` and `untrack(() => target)` count too. Solid
|
|
2966
|
+
* skips an empty entry of a ref array, and evaluates the whole `ref`
|
|
2967
|
+
* untracked. They come from a component that forwards `props.ref` in an
|
|
2968
|
+
* array, once a call site that passes no `ref` is inlined.
|
|
2865
2969
|
*/
|
|
2866
|
-
function isForwardableRef(attribute) {
|
|
2970
|
+
function isForwardableRef(context, attribute) {
|
|
2867
2971
|
const { value } = attribute;
|
|
2868
2972
|
if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
|
|
2869
|
-
const isTarget = (node) => {
|
|
2973
|
+
const isTarget = (node, inArray) => {
|
|
2870
2974
|
const inner = unwrap(node);
|
|
2871
|
-
if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element));
|
|
2975
|
+
if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element, true));
|
|
2976
|
+
if (inArray && isVoidZero(inner)) return true;
|
|
2977
|
+
if (inArray && inner.type === "CallExpression" && solidCallee(context, inner) === "untrack") {
|
|
2978
|
+
const callback = inner.arguments.at(0);
|
|
2979
|
+
return inner.arguments.length === 1 && callback?.type === "ArrowFunctionExpression" && callback.expression && callback.params.length === 0 && callback.body.type !== "BlockStatement" && isTarget(callback.body, true);
|
|
2980
|
+
}
|
|
2872
2981
|
return inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2873
2982
|
};
|
|
2874
|
-
return isTarget(value.expression);
|
|
2983
|
+
return isTarget(value.expression, false);
|
|
2984
|
+
}
|
|
2985
|
+
/** Whether an expression is `void 0`. */
|
|
2986
|
+
function isVoidZero(node) {
|
|
2987
|
+
return node.type === "UnaryExpression" && node.operator === "void" && node.argument.type === "Literal" && node.argument.value === 0;
|
|
2875
2988
|
}
|
|
2876
2989
|
/**
|
|
2877
2990
|
* Why `attributesOf` turns down a call site, for the report.
|
|
@@ -2912,6 +3025,9 @@ var Inliner = class {
|
|
|
2912
3025
|
this.hosts = /* @__PURE__ */ new Map();
|
|
2913
3026
|
this.transparent = /* @__PURE__ */ new Map();
|
|
2914
3027
|
this.reason = "";
|
|
3028
|
+
this.components = [];
|
|
3029
|
+
this.sites = [];
|
|
3030
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
2915
3031
|
}
|
|
2916
3032
|
/**
|
|
2917
3033
|
* Whether a binding holds a component made by Solid's `lazy`, for the
|
|
@@ -2947,6 +3063,27 @@ var Inliner = class {
|
|
|
2947
3063
|
const statement = this.context.parents.get(declaration);
|
|
2948
3064
|
if (statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program") return statement;
|
|
2949
3065
|
}
|
|
3066
|
+
/**
|
|
3067
|
+
* Whether statements assign, update, or delete through a binding declared
|
|
3068
|
+
* outside `scope`, or through a global. Writes inside nested functions
|
|
3069
|
+
* count too, since a function can run right away, like a memo's.
|
|
3070
|
+
*/
|
|
3071
|
+
writesOutside(statements, scope) {
|
|
3072
|
+
const roots = [];
|
|
3073
|
+
const visit = (node) => {
|
|
3074
|
+
if (node.type === "AssignmentExpression") collectWriteRoots(node.left, roots);
|
|
3075
|
+
else if (node.type === "UpdateExpression") collectWriteRoots(node.argument, roots);
|
|
3076
|
+
else if (node.type === "UnaryExpression" && node.operator === "delete") collectWriteRoots(node.argument, roots);
|
|
3077
|
+
forEachChild(node, visit);
|
|
3078
|
+
};
|
|
3079
|
+
for (const statement of statements) visit(statement);
|
|
3080
|
+
for (const root of roots) {
|
|
3081
|
+
if (root.type !== "Identifier") return true;
|
|
3082
|
+
const target = this.context.scopes.references.get(root);
|
|
3083
|
+
if (!target || !isWithinScope(target.scope, scope)) return true;
|
|
3084
|
+
}
|
|
3085
|
+
return false;
|
|
3086
|
+
}
|
|
2950
3087
|
analyzeComponent(binding) {
|
|
2951
3088
|
if (!isCapitalized(binding.name)) return;
|
|
2952
3089
|
if (binding.mutated) {
|
|
@@ -3003,6 +3140,10 @@ var Inliner = class {
|
|
|
3003
3140
|
this.reason = "component-this";
|
|
3004
3141
|
return;
|
|
3005
3142
|
}
|
|
3143
|
+
if (this.writesOutside(statements, scope)) {
|
|
3144
|
+
this.reason = "component-write";
|
|
3145
|
+
return;
|
|
3146
|
+
}
|
|
3006
3147
|
const param = fn.params.at(0);
|
|
3007
3148
|
let paramBinding;
|
|
3008
3149
|
if (param) {
|
|
@@ -3476,7 +3617,7 @@ var Inliner = class {
|
|
|
3476
3617
|
* when it cannot be inlined there. Nothing is written until `commit`.
|
|
3477
3618
|
*/
|
|
3478
3619
|
prepare(component, element) {
|
|
3479
|
-
const attributes = attributesOf(element);
|
|
3620
|
+
const attributes = attributesOf(this.context, element);
|
|
3480
3621
|
if (!attributes) {
|
|
3481
3622
|
this.reason = callSiteReason(element);
|
|
3482
3623
|
return;
|
|
@@ -3816,14 +3957,27 @@ var Inliner = class {
|
|
|
3816
3957
|
byComponent.set(site.component, elements);
|
|
3817
3958
|
}
|
|
3818
3959
|
const result = /* @__PURE__ */ new Set();
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3960
|
+
const { alwaysInline, alwaysInlineComponents } = this.context.options;
|
|
3961
|
+
for (const component of byComponent.keys()) if (alwaysInline || alwaysInlineComponents.has(component.binding.name)) {
|
|
3962
|
+
result.add(component);
|
|
3963
|
+
byComponent.delete(component);
|
|
3822
3964
|
}
|
|
3965
|
+
if (byComponent.size === 0) return result;
|
|
3823
3966
|
this.scratchNames = new Set(this.context.scopes.names);
|
|
3824
3967
|
try {
|
|
3825
3968
|
const copiesOf = /* @__PURE__ */ new Map();
|
|
3826
3969
|
for (const [component, elements] of byComponent) copiesOf.set(component, new Map(elements.map((element) => [element, this.prepare(component, element)])));
|
|
3970
|
+
this.sites = sites;
|
|
3971
|
+
this.otherUses = otherUses;
|
|
3972
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
3973
|
+
for (const [inner, byElement] of copiesOf) for (const [element, copy] of byElement) {
|
|
3974
|
+
const outer = copy ? void 0 : this.components.find((item) => item !== inner && isWithin(element, item.fn));
|
|
3975
|
+
if (outer) {
|
|
3976
|
+
const list = this.blocked.get(outer) ?? [];
|
|
3977
|
+
list.push(inner);
|
|
3978
|
+
this.blocked.set(outer, list);
|
|
3979
|
+
}
|
|
3980
|
+
}
|
|
3827
3981
|
for (const [component, byElement] of copiesOf) {
|
|
3828
3982
|
const copies = [...byElement.values()].filter((copy) => copy !== void 0);
|
|
3829
3983
|
if (copies.length === 0) continue;
|
|
@@ -3831,6 +3985,8 @@ var Inliner = class {
|
|
|
3831
3985
|
}
|
|
3832
3986
|
} finally {
|
|
3833
3987
|
this.scratchNames = void 0;
|
|
3988
|
+
this.otherUses = void 0;
|
|
3989
|
+
this.blocked = /* @__PURE__ */ new Map();
|
|
3834
3990
|
}
|
|
3835
3991
|
return result;
|
|
3836
3992
|
}
|
|
@@ -3885,14 +4041,52 @@ var Inliner = class {
|
|
|
3885
4041
|
const estimator = this.estimator();
|
|
3886
4042
|
let calls = share > 0 && component.declaration ? share * estimator.node(component.declaration) : 0;
|
|
3887
4043
|
let inlined = 0;
|
|
4044
|
+
const blocked = this.blockedWithin(component);
|
|
4045
|
+
const kept = /* @__PURE__ */ new Set();
|
|
3888
4046
|
for (const copy of copies) {
|
|
3889
4047
|
const inTemplate = this.sitsInTemplate(copy.element);
|
|
3890
4048
|
calls += estimator.jsx(copy.element, inTemplate);
|
|
3891
|
-
|
|
4049
|
+
const measured = { code: "" };
|
|
4050
|
+
inlined += copySize(this.context.filename, this.sizeHeader(), copy, inTemplate, this.context.options, blocked.length > 0 ? {
|
|
4051
|
+
declarations: this.declarationsOf(blocked),
|
|
4052
|
+
inline
|
|
4053
|
+
} : void 0, measured);
|
|
4054
|
+
const body = measured.code.slice(measured.code.lastIndexOf(COPY_WRAPPER));
|
|
4055
|
+
for (const inner of blocked) if (hasTag(body, inner.binding.name)) kept.add(inner);
|
|
4056
|
+
}
|
|
4057
|
+
if (share > 0) {
|
|
4058
|
+
for (const inner of blocked) if (!kept.has(inner) && inner.declaration && this.onlyUsedWithin(inner, component)) calls += share * estimator.node(inner.declaration);
|
|
3892
4059
|
}
|
|
3893
4060
|
return calls - inlined;
|
|
3894
4061
|
}
|
|
3895
4062
|
/**
|
|
4063
|
+
* The components whose calls in a component's body cannot be inlined
|
|
4064
|
+
* there, but may be in a copy, once the call site resolves the props
|
|
4065
|
+
* they are given. Those they call in turn count too.
|
|
4066
|
+
*/
|
|
4067
|
+
blockedWithin(component) {
|
|
4068
|
+
const found = [];
|
|
4069
|
+
const pending = [component];
|
|
4070
|
+
for (let outer = pending.pop(); outer; outer = pending.pop()) for (const inner of this.blocked.get(outer) ?? []) if (inner !== component && !found.includes(inner)) {
|
|
4071
|
+
found.push(inner);
|
|
4072
|
+
pending.push(inner);
|
|
4073
|
+
}
|
|
4074
|
+
return found;
|
|
4075
|
+
}
|
|
4076
|
+
/** The declarations of components, for a copy measured on its own. */
|
|
4077
|
+
declarationsOf(components) {
|
|
4078
|
+
const texts = [];
|
|
4079
|
+
for (const component of components) {
|
|
4080
|
+
const statement = this.topLevelStatement(component.binding.declaration);
|
|
4081
|
+
if (statement) texts.push(textOf(this.context, statement));
|
|
4082
|
+
}
|
|
4083
|
+
return texts.join("\n");
|
|
4084
|
+
}
|
|
4085
|
+
/** Whether every call of `inner` is in `outer`, and nothing else uses it. */
|
|
4086
|
+
onlyUsedWithin(inner, outer) {
|
|
4087
|
+
return this.otherUses?.get(inner) === 0 && this.sites.every((site) => site.component !== inner || isWithin(site.element, outer.fn));
|
|
4088
|
+
}
|
|
4089
|
+
/**
|
|
3896
4090
|
* Whether an element sits directly in an intrinsic element, whose
|
|
3897
4091
|
* template its copy joins. A provider this pass makes transparent is
|
|
3898
4092
|
* removed once its consumers are inlined, so the element above it counts.
|
|
@@ -3907,6 +4101,15 @@ var Inliner = class {
|
|
|
3907
4101
|
this.sizeEstimator ??= new SizeEstimator(this.context.code);
|
|
3908
4102
|
return this.sizeEstimator;
|
|
3909
4103
|
}
|
|
4104
|
+
/** The top-level statement a node is part of, without an `export`. */
|
|
4105
|
+
topLevelStatement(node) {
|
|
4106
|
+
let current = node;
|
|
4107
|
+
for (let parent = this.context.parents.get(current); parent;) {
|
|
4108
|
+
if (parent.type === "Program") return current.type === "ExportNamedDeclaration" || current.type === "ExportDefaultDeclaration" ? current.declaration ?? void 0 : current;
|
|
4109
|
+
current = parent;
|
|
4110
|
+
parent = this.context.parents.get(current);
|
|
4111
|
+
}
|
|
4112
|
+
}
|
|
3910
4113
|
/**
|
|
3911
4114
|
* What a copy measured on its own needs to know: the module's imports,
|
|
3912
4115
|
* and the names that alias Solid's exports.
|
|
@@ -3920,8 +4123,56 @@ var Inliner = class {
|
|
|
3920
4123
|
}
|
|
3921
4124
|
return this.header;
|
|
3922
4125
|
}
|
|
4126
|
+
/**
|
|
4127
|
+
* The component a top-level alias like `const Button = ButtonRoot` names,
|
|
4128
|
+
* or `undefined` when the binding is not one.
|
|
4129
|
+
*/
|
|
4130
|
+
aliasTarget(binding) {
|
|
4131
|
+
const { declaration } = binding;
|
|
4132
|
+
if (!isCapitalized(binding.name) || !isConstantDeclaration(binding, this.context.options) || declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init) return;
|
|
4133
|
+
const init = unwrap(declaration.init);
|
|
4134
|
+
if (init.type !== "Identifier") return;
|
|
4135
|
+
const target = this.context.scopes.references.get(init);
|
|
4136
|
+
return target && target !== binding && target.scope.parent === void 0 && !target.mutated && isCapitalized(target.name) ? target : void 0;
|
|
4137
|
+
}
|
|
4138
|
+
/**
|
|
4139
|
+
* The statement of an alias nothing uses that `reference` is the value
|
|
4140
|
+
* of, like `const Separator = SeparatorRoot` once its tags are renamed.
|
|
4141
|
+
* It does not keep the component, and goes away with it.
|
|
4142
|
+
*/
|
|
4143
|
+
unusedAlias(reference) {
|
|
4144
|
+
const declarator = this.context.parents.get(reference);
|
|
4145
|
+
if (declarator?.type !== "VariableDeclarator" || declarator.id.type !== "Identifier") return;
|
|
4146
|
+
const alias = this.context.scopes.root.bindings.get(declarator.id.name);
|
|
4147
|
+
const statement = this.context.parents.get(declarator);
|
|
4148
|
+
return alias?.declaration === declarator && this.aliasTarget(alias) !== void 0 && alias.references.length === 0 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program" ? statement : void 0;
|
|
4149
|
+
}
|
|
4150
|
+
/**
|
|
4151
|
+
* Renames each tag of an alias to the component it names, where that name
|
|
4152
|
+
* means the same. Libraries often export a component under a second name,
|
|
4153
|
+
* like `const Separator = SeparatorRoot`. The next pass inlines the calls.
|
|
4154
|
+
*/
|
|
4155
|
+
renameAliasTags() {
|
|
4156
|
+
let renamed = false;
|
|
4157
|
+
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
4158
|
+
const target = this.aliasTarget(binding);
|
|
4159
|
+
if (!target) continue;
|
|
4160
|
+
for (const reference of binding.references) {
|
|
4161
|
+
const parent = this.context.parents.get(reference);
|
|
4162
|
+
if (parent?.type !== "JSXOpeningElement" && parent?.type !== "JSXClosingElement" || parent.name !== reference) continue;
|
|
4163
|
+
if (lookup(scopeAt(this.context.scopes, this.context.parents, reference), target.name) !== target) continue;
|
|
4164
|
+
this.context.s.overwrite(reference.start, reference.end, target.name);
|
|
4165
|
+
renamed = true;
|
|
4166
|
+
}
|
|
4167
|
+
}
|
|
4168
|
+
return renamed;
|
|
4169
|
+
}
|
|
3923
4170
|
run() {
|
|
3924
|
-
|
|
4171
|
+
if (this.renameAliasTags()) {
|
|
4172
|
+
this.changed = true;
|
|
4173
|
+
return;
|
|
4174
|
+
}
|
|
4175
|
+
const { components } = this;
|
|
3925
4176
|
const rejected = /* @__PURE__ */ new Map();
|
|
3926
4177
|
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
3927
4178
|
this.reason = "not-a-function";
|
|
@@ -3936,16 +4187,19 @@ var Inliner = class {
|
|
|
3936
4187
|
}
|
|
3937
4188
|
const sites = [];
|
|
3938
4189
|
const otherUses = /* @__PURE__ */ new Map();
|
|
4190
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
3939
4191
|
for (const component of components) {
|
|
3940
4192
|
let uses = 0;
|
|
3941
4193
|
for (const reference of component.binding.references) {
|
|
3942
4194
|
const parent = this.context.parents.get(reference);
|
|
3943
4195
|
if (parent?.type === "JSXClosingElement") continue;
|
|
3944
4196
|
const element = parent ? this.context.parents.get(parent) : void 0;
|
|
4197
|
+
const alias = this.unusedAlias(reference);
|
|
3945
4198
|
if (parent?.type === "JSXOpeningElement" && element?.type === "JSXElement") sites.push({
|
|
3946
4199
|
component,
|
|
3947
4200
|
element
|
|
3948
4201
|
});
|
|
4202
|
+
else if (alias) aliases.set(component, [...aliases.get(component) ?? [], alias]);
|
|
3949
4203
|
else uses += 1;
|
|
3950
4204
|
}
|
|
3951
4205
|
otherUses.set(component, uses);
|
|
@@ -3977,7 +4231,10 @@ var Inliner = class {
|
|
|
3977
4231
|
const copied = [...inlined.keys()];
|
|
3978
4232
|
for (const [component, elements] of inlined) {
|
|
3979
4233
|
const insideCopy = elements.some((element) => copied.some((other) => other !== component && element.start >= other.fn.start && element.end <= other.fn.end));
|
|
3980
|
-
if (component.declaration && otherUses.get(component) === 0 && !insideCopy)
|
|
4234
|
+
if (component.declaration && otherUses.get(component) === 0 && !insideCopy) {
|
|
4235
|
+
this.context.s.remove(component.declaration.start, component.declaration.end);
|
|
4236
|
+
for (const alias of aliases.get(component) ?? []) this.context.s.remove(alias.start, alias.end);
|
|
4237
|
+
}
|
|
3981
4238
|
}
|
|
3982
4239
|
}
|
|
3983
4240
|
/**
|
|
@@ -4407,7 +4664,8 @@ function resolveOptions(options) {
|
|
|
4407
4664
|
globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
|
|
4408
4665
|
importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
|
|
4409
4666
|
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
4410
|
-
alwaysInline: options.alwaysInline ?? false
|
|
4667
|
+
alwaysInline: options.alwaysInline ?? false,
|
|
4668
|
+
alwaysInlineComponents: new Set(options.alwaysInlineComponents ?? [])
|
|
4411
4669
|
};
|
|
4412
4670
|
}
|
|
4413
4671
|
function parseCode(code, filename) {
|
|
@@ -4506,12 +4764,13 @@ function compile(code, options = {}) {
|
|
|
4506
4764
|
*/
|
|
4507
4765
|
function readModuleConstants(code, options = {}) {
|
|
4508
4766
|
const program = parse(options.filename ?? "input.jsx", code);
|
|
4767
|
+
const scopes = analyzeScopes(program);
|
|
4509
4768
|
return {
|
|
4510
|
-
imports: namedImportSources(program),
|
|
4511
|
-
exports: constantExports(program, resolveOptions(options))
|
|
4769
|
+
imports: namedImportSources(program, scopes),
|
|
4770
|
+
exports: constantExports(program, resolveOptions(options), scopes)
|
|
4512
4771
|
};
|
|
4513
4772
|
}
|
|
4514
4773
|
//#endregion
|
|
4515
4774
|
export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
|
|
4516
4775
|
|
|
4517
|
-
//# sourceMappingURL=compile-
|
|
4776
|
+
//# sourceMappingURL=compile-sCZdwEKG.mjs.map
|