solid-optimizer 1.0.0-next.8 → 1.0.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 +5 -5
- package/dist/{compile-O1Z60VsW.mjs → compile-Cvjy2e11.mjs} +430 -172
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-X_SMDFr2.d.mts → compile-ioIkBKzR.d.mts} +1 -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 +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +55 -14
- package/dist/vite.mjs.map +1 -1
- package/package.json +6 -6
- package/src/compile.ts +6 -8
- package/src/conditional.ts +29 -22
- package/src/context.ts +3 -3
- package/src/copy-size.ts +6 -5
- package/src/fold.ts +161 -53
- package/src/inline.ts +70 -60
- package/src/jsx.ts +16 -14
- package/src/memo.ts +12 -6
- package/src/scope.ts +115 -9
- package/src/text.ts +297 -0
- package/src/vite/index.ts +66 -12
- package/dist/compile-O1Z60VsW.mjs.map +0 -1
- package/dist/compile-X_SMDFr2.d.mts.map +0 -1
package/src/inline.ts
CHANGED
|
@@ -71,7 +71,6 @@
|
|
|
71
71
|
* instead of while it is created. The order of `onSettled` and effect
|
|
72
72
|
* registration across siblings can change.
|
|
73
73
|
*/
|
|
74
|
-
import MagicString from 'magic-string';
|
|
75
74
|
import type {
|
|
76
75
|
ArrayExpressionElement,
|
|
77
76
|
ArrowFunctionExpression,
|
|
@@ -118,6 +117,8 @@ import { isWithinScope, lookup, scopeAt } from './scope';
|
|
|
118
117
|
import { copySize } from './copy-size';
|
|
119
118
|
import { SizeEstimator } from './size';
|
|
120
119
|
import { evaluate, literalText } from './value';
|
|
120
|
+
import type { Content, Editor, Text } from './text';
|
|
121
|
+
import { getCode, join, joinText } from './text';
|
|
121
122
|
|
|
122
123
|
type ComponentFunction = FunctionNode | ArrowFunctionExpression;
|
|
123
124
|
|
|
@@ -137,7 +138,7 @@ interface PropRead extends ReadCount {
|
|
|
137
138
|
*/
|
|
138
139
|
interface ViewSource {
|
|
139
140
|
/** The default of each key, as literal text, or `undefined` for the props. */
|
|
140
|
-
readonly defaults: ReadonlyMap<string,
|
|
141
|
+
readonly defaults: ReadonlyMap<string, Text> | undefined;
|
|
141
142
|
readonly hidden: ReadonlySet<string>;
|
|
142
143
|
}
|
|
143
144
|
|
|
@@ -153,14 +154,14 @@ interface ViewRead extends PropRead {
|
|
|
153
154
|
interface ResolvedSpread {
|
|
154
155
|
readonly spread: ViewSpread;
|
|
155
156
|
/** Each key and its default text, or `undefined` when the props hold it. */
|
|
156
|
-
readonly keys: ReadonlyMap<string,
|
|
157
|
+
readonly keys: ReadonlyMap<string, Content | undefined>;
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
interface ResolvedViews {
|
|
160
161
|
/** Reads of the props, including view reads the props answer. */
|
|
161
162
|
readonly propReads: readonly PropRead[];
|
|
162
163
|
/** View reads a default or nothing answers, and their text. */
|
|
163
|
-
readonly texts: ReadonlyMap<Node,
|
|
164
|
+
readonly texts: ReadonlyMap<Node, Content>;
|
|
164
165
|
readonly spreads: readonly ResolvedSpread[];
|
|
165
166
|
}
|
|
166
167
|
|
|
@@ -207,8 +208,8 @@ interface PreparedSite {
|
|
|
207
208
|
readonly element: JSXElement;
|
|
208
209
|
/** Where the statements go, when there are any. */
|
|
209
210
|
readonly host: { fn: Node; host: Host } | undefined;
|
|
210
|
-
readonly statements:
|
|
211
|
-
readonly root:
|
|
211
|
+
readonly statements: Content[];
|
|
212
|
+
readonly root: Text;
|
|
212
213
|
}
|
|
213
214
|
|
|
214
215
|
function isWithin(node: Node, container: Node): boolean {
|
|
@@ -254,14 +255,14 @@ type Host =
|
|
|
254
255
|
*/
|
|
255
256
|
type PropValue =
|
|
256
257
|
/** Replaced with an expression at every read. */
|
|
257
|
-
| { readonly kind: 'expression'; readonly text:
|
|
258
|
+
| { readonly kind: 'expression'; readonly text: Content; readonly primary: boolean }
|
|
258
259
|
/** JSX, spliced in place when read as a JSX child. */
|
|
259
|
-
| { readonly kind: 'jsx'; readonly text:
|
|
260
|
+
| { readonly kind: 'jsx'; readonly text: Content; readonly primary: boolean }
|
|
260
261
|
/** JSX children, spliced in place when read as a JSX child. */
|
|
261
262
|
| {
|
|
262
263
|
readonly kind: 'children';
|
|
263
264
|
readonly children: readonly JSXChild[];
|
|
264
|
-
readonly text:
|
|
265
|
+
readonly text: Content;
|
|
265
266
|
readonly primary: boolean;
|
|
266
267
|
};
|
|
267
268
|
|
|
@@ -657,14 +658,14 @@ function isAssignmentTarget(context: PassContext, node: Node): boolean {
|
|
|
657
658
|
class Inliner {
|
|
658
659
|
changed = false;
|
|
659
660
|
|
|
660
|
-
private readonly hosts = new Map<Node, { host: Host; texts:
|
|
661
|
+
private readonly hosts = new Map<Node, { host: Host; texts: Content[] }>();
|
|
661
662
|
|
|
662
663
|
/**
|
|
663
664
|
* Providers whose subtree is closed, so statements can move out through
|
|
664
665
|
* them. Each maps to the text its reads become, or `undefined` when a
|
|
665
666
|
* binding that never changes is read at each site.
|
|
666
667
|
*/
|
|
667
|
-
private readonly transparent = new Map<JSXElement, { provider: Provider; text?:
|
|
668
|
+
private readonly transparent = new Map<JSXElement, { provider: Provider; text?: Content }>();
|
|
668
669
|
|
|
669
670
|
constructor(private readonly context: PassContext) {}
|
|
670
671
|
|
|
@@ -1015,7 +1016,9 @@ class Inliner {
|
|
|
1015
1016
|
if (view === null) {
|
|
1016
1017
|
continue;
|
|
1017
1018
|
}
|
|
1018
|
-
|
|
1019
|
+
// A body with its own scope (a parameter has a default) holds the declaration.
|
|
1020
|
+
const body = (fn.body && this.context.scopes.scopes.get(fn.body)) ?? scope;
|
|
1021
|
+
const binding = body.bindings.get(declarator.id.name);
|
|
1019
1022
|
if (!view || !binding || binding.mutated) {
|
|
1020
1023
|
return undefined;
|
|
1021
1024
|
}
|
|
@@ -1162,7 +1165,7 @@ class Inliner {
|
|
|
1162
1165
|
if (inner.type !== 'ObjectExpression') {
|
|
1163
1166
|
return undefined;
|
|
1164
1167
|
}
|
|
1165
|
-
const defaults = new Map<string,
|
|
1168
|
+
const defaults = new Map<string, Text>();
|
|
1166
1169
|
for (const property of inner.properties) {
|
|
1167
1170
|
if (property.type !== 'Property' || property.kind !== 'init' || property.computed) {
|
|
1168
1171
|
return undefined;
|
|
@@ -1180,7 +1183,7 @@ class Inliner {
|
|
|
1180
1183
|
if (text === undefined || isReservedPropName(key)) {
|
|
1181
1184
|
return undefined;
|
|
1182
1185
|
}
|
|
1183
|
-
defaults.set(key, text);
|
|
1186
|
+
defaults.set(key, this.context.s.written(text, property.value.start));
|
|
1184
1187
|
}
|
|
1185
1188
|
sources.push({ defaults, hidden: new Set() });
|
|
1186
1189
|
}
|
|
@@ -1332,7 +1335,7 @@ class Inliner {
|
|
|
1332
1335
|
element: JSXElement,
|
|
1333
1336
|
reads: readonly ReadCount[],
|
|
1334
1337
|
fresh: (base: string) => string,
|
|
1335
|
-
snapshots:
|
|
1338
|
+
snapshots: Content[],
|
|
1336
1339
|
): PropValue | undefined {
|
|
1337
1340
|
const { children } = element;
|
|
1338
1341
|
if (hasSpreadChild(children)) {
|
|
@@ -1343,10 +1346,10 @@ class Inliner {
|
|
|
1343
1346
|
if (only === undefined) {
|
|
1344
1347
|
return { kind: 'children', children: [], text: 'void 0', primary: false };
|
|
1345
1348
|
}
|
|
1346
|
-
let text:
|
|
1349
|
+
let text: Content;
|
|
1347
1350
|
let primary = true;
|
|
1348
1351
|
if (significant.length > 1 || only.type === 'JSXText') {
|
|
1349
|
-
text = `<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
1352
|
+
text = joinText`<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
1350
1353
|
} else if (only.type === 'JSXExpressionContainer') {
|
|
1351
1354
|
if (only.expression.type === 'JSXEmptyExpression') {
|
|
1352
1355
|
return undefined;
|
|
@@ -1354,7 +1357,10 @@ class Inliner {
|
|
|
1354
1357
|
// A single expression is passed like an attribute value, such as a
|
|
1355
1358
|
// render function that has to stay one function.
|
|
1356
1359
|
const value = this.expressionValue(only.expression, 'children', reads, fresh, snapshots);
|
|
1357
|
-
if (
|
|
1360
|
+
if (
|
|
1361
|
+
value.kind !== 'expression' ||
|
|
1362
|
+
getCode(value.text) !== getText(this.context, only.expression).code
|
|
1363
|
+
) {
|
|
1358
1364
|
return value;
|
|
1359
1365
|
}
|
|
1360
1366
|
text = value.text;
|
|
@@ -1410,7 +1416,7 @@ class Inliner {
|
|
|
1410
1416
|
attribute: JSXAttribute | undefined,
|
|
1411
1417
|
reads: readonly ReadCount[],
|
|
1412
1418
|
fresh: (base: string) => string,
|
|
1413
|
-
snapshots:
|
|
1419
|
+
snapshots: Content[],
|
|
1414
1420
|
): PropValue | undefined {
|
|
1415
1421
|
if (!attribute) {
|
|
1416
1422
|
return UNDEFINED;
|
|
@@ -1425,7 +1431,11 @@ class Inliner {
|
|
|
1425
1431
|
if (value.value.includes('&')) {
|
|
1426
1432
|
return undefined;
|
|
1427
1433
|
}
|
|
1428
|
-
return {
|
|
1434
|
+
return {
|
|
1435
|
+
kind: 'expression',
|
|
1436
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
1437
|
+
primary: true,
|
|
1438
|
+
};
|
|
1429
1439
|
case 'JSXElement':
|
|
1430
1440
|
case 'JSXFragment':
|
|
1431
1441
|
return { kind: 'jsx', text: getText(this.context, value), primary: true };
|
|
@@ -1448,7 +1458,7 @@ class Inliner {
|
|
|
1448
1458
|
name: string,
|
|
1449
1459
|
reads: readonly ReadCount[],
|
|
1450
1460
|
fresh: (base: string) => string,
|
|
1451
|
-
snapshots:
|
|
1461
|
+
snapshots: Content[],
|
|
1452
1462
|
): PropValue {
|
|
1453
1463
|
const text = getText(this.context, expression);
|
|
1454
1464
|
const inner = unwrap(expression);
|
|
@@ -1465,7 +1475,7 @@ class Inliner {
|
|
|
1465
1475
|
return { kind: 'expression', text, primary };
|
|
1466
1476
|
}
|
|
1467
1477
|
const local = fresh(name);
|
|
1468
|
-
snapshots.push(`const ${local} = ${text};`);
|
|
1478
|
+
snapshots.push(joinText`const ${local} = ${text};`);
|
|
1469
1479
|
return { kind: 'expression', text: local, primary: true };
|
|
1470
1480
|
}
|
|
1471
1481
|
|
|
@@ -1556,7 +1566,7 @@ class Inliner {
|
|
|
1556
1566
|
}
|
|
1557
1567
|
|
|
1558
1568
|
// What each prop reads as.
|
|
1559
|
-
const snapshots:
|
|
1569
|
+
const snapshots: Content[] = [];
|
|
1560
1570
|
const fresh = (base: string): string => this.freshName(base);
|
|
1561
1571
|
const readsByName = new Map<string, ReadCount[]>();
|
|
1562
1572
|
const count = (name: string, read: ReadCount): void => {
|
|
@@ -1615,7 +1625,7 @@ class Inliner {
|
|
|
1615
1625
|
}
|
|
1616
1626
|
|
|
1617
1627
|
// Build the copy on the original code, so this pass's other edits stay out of it.
|
|
1618
|
-
const copy =
|
|
1628
|
+
const copy = this.context.s.fresh();
|
|
1619
1629
|
this.renameLocals(copy, component, fresh);
|
|
1620
1630
|
this.substituteProps(copy, views.propReads, values);
|
|
1621
1631
|
this.substituteViews(copy, component, element, views, values);
|
|
@@ -1629,7 +1639,7 @@ class Inliner {
|
|
|
1629
1639
|
if (first && last) {
|
|
1630
1640
|
// The last statement can rely on a line break that it loses here.
|
|
1631
1641
|
const text = copy.slice(first.start, last.end);
|
|
1632
|
-
statements.push(text.endsWith(';') ? text : `${text};`);
|
|
1642
|
+
statements.push(text.code.endsWith(';') ? text : joinText`${text};`);
|
|
1633
1643
|
}
|
|
1634
1644
|
return {
|
|
1635
1645
|
element,
|
|
@@ -1640,7 +1650,7 @@ class Inliner {
|
|
|
1640
1650
|
}
|
|
1641
1651
|
|
|
1642
1652
|
/** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
|
|
1643
|
-
private addToHost(found: { fn: Node; host: Host }, texts: readonly
|
|
1653
|
+
private addToHost(found: { fn: Node; host: Host }, texts: readonly Content[]): void {
|
|
1644
1654
|
const key = found.host.kind === 'return' ? found.host.statement : found.fn;
|
|
1645
1655
|
const entry = this.hosts.get(key) ?? { host: found.host, texts: [] };
|
|
1646
1656
|
entry.texts.push(...texts);
|
|
@@ -1662,8 +1672,8 @@ class Inliner {
|
|
|
1662
1672
|
* provider above it answers, with the text each becomes. `undefined` when
|
|
1663
1673
|
* a read cannot take the value there.
|
|
1664
1674
|
*/
|
|
1665
|
-
private contextTexts(component: Component, element: JSXElement): Map<Node,
|
|
1666
|
-
const texts = new Map<Node,
|
|
1675
|
+
private contextTexts(component: Component, element: JSXElement): Map<Node, Content> | undefined {
|
|
1676
|
+
const texts = new Map<Node, Content>();
|
|
1667
1677
|
for (const { provider, text } of this.answeringProviders(element)) {
|
|
1668
1678
|
for (const read of contextReads(this.context, component.fn, provider.context)) {
|
|
1669
1679
|
const value = text ?? stableValueText(this.context, provider, element);
|
|
@@ -1680,8 +1690,8 @@ class Inliner {
|
|
|
1680
1690
|
* The closed providers that answer context reads at `element`. The nearest
|
|
1681
1691
|
* provider of each context answers its reads, closed or not.
|
|
1682
1692
|
*/
|
|
1683
|
-
private answeringProviders(element: JSXElement): { provider: Provider; text?:
|
|
1684
|
-
const answering: { provider: Provider; text?:
|
|
1693
|
+
private answeringProviders(element: JSXElement): { provider: Provider; text?: Content }[] {
|
|
1694
|
+
const answering: { provider: Provider; text?: Content }[] = [];
|
|
1685
1695
|
const seen = new Set<Binding>();
|
|
1686
1696
|
for (
|
|
1687
1697
|
let node = this.context.parents.get(element);
|
|
@@ -1776,7 +1786,7 @@ class Inliner {
|
|
|
1776
1786
|
const name = this.freshName(
|
|
1777
1787
|
`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
|
|
1778
1788
|
);
|
|
1779
|
-
this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
1789
|
+
this.addToHost(host, [joinText`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
1780
1790
|
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
1781
1791
|
this.transparent.set(element, { provider, text: name });
|
|
1782
1792
|
this.changed = true;
|
|
@@ -1804,7 +1814,7 @@ class Inliner {
|
|
|
1804
1814
|
const has = (key: string): boolean =>
|
|
1805
1815
|
attributes.has(key) || (key === 'children' && hasChildren);
|
|
1806
1816
|
// The last source that has the key answers, even when its value is `undefined`.
|
|
1807
|
-
const resolve = (view: View, key: string):
|
|
1817
|
+
const resolve = (view: View, key: string): Content | undefined => {
|
|
1808
1818
|
for (let index = view.sources.length - 1; index >= 0; index -= 1) {
|
|
1809
1819
|
const source = view.sources.at(index);
|
|
1810
1820
|
if (!source || source.hidden.has(key)) {
|
|
@@ -1822,7 +1832,7 @@ class Inliner {
|
|
|
1822
1832
|
};
|
|
1823
1833
|
|
|
1824
1834
|
const propReads: PropRead[] = [...component.props];
|
|
1825
|
-
const texts = new Map<Node,
|
|
1835
|
+
const texts = new Map<Node, Content>();
|
|
1826
1836
|
for (const read of component.viewReads) {
|
|
1827
1837
|
const text = resolve(read.view, read.name);
|
|
1828
1838
|
if (text === undefined) {
|
|
@@ -1862,13 +1872,13 @@ class Inliner {
|
|
|
1862
1872
|
names.add(
|
|
1863
1873
|
attribute.name.type === 'JSXIdentifier'
|
|
1864
1874
|
? attribute.name.name
|
|
1865
|
-
: getText(this.context, attribute.name),
|
|
1875
|
+
: getText(this.context, attribute.name).code,
|
|
1866
1876
|
);
|
|
1867
1877
|
}
|
|
1868
1878
|
}
|
|
1869
1879
|
taken.set(spread.element, names);
|
|
1870
1880
|
}
|
|
1871
|
-
const keys = new Map<string,
|
|
1881
|
+
const keys = new Map<string, Content | undefined>();
|
|
1872
1882
|
for (const key of order) {
|
|
1873
1883
|
// Children become the element's children, so it must have none of its own.
|
|
1874
1884
|
const fits =
|
|
@@ -1891,39 +1901,39 @@ class Inliner {
|
|
|
1891
1901
|
* view declarations.
|
|
1892
1902
|
*/
|
|
1893
1903
|
private substituteViews(
|
|
1894
|
-
copy:
|
|
1904
|
+
copy: Editor,
|
|
1895
1905
|
component: Component,
|
|
1896
1906
|
element: JSXElement,
|
|
1897
1907
|
views: ResolvedViews,
|
|
1898
1908
|
values: ReadonlyMap<string, PropValue>,
|
|
1899
1909
|
): void {
|
|
1900
1910
|
for (const [member, text] of views.texts) {
|
|
1901
|
-
const bare = text !== 'void 0' || acceptsAnyExpression(this.context, member);
|
|
1902
|
-
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
1911
|
+
const bare = getCode(text) !== 'void 0' || acceptsAnyExpression(this.context, member);
|
|
1912
|
+
copy.overwrite(member.start, member.end, bare ? text : joinText`(${text})`);
|
|
1903
1913
|
}
|
|
1904
1914
|
for (const { spread, keys } of views.spreads) {
|
|
1905
|
-
const parts:
|
|
1906
|
-
let children:
|
|
1915
|
+
const parts: Text[] = [];
|
|
1916
|
+
let children: Content | undefined;
|
|
1907
1917
|
for (const [key, text] of keys) {
|
|
1908
1918
|
const value = values.get(key);
|
|
1909
1919
|
if (key === 'children') {
|
|
1910
1920
|
children = this.spreadChildren(element, text, value);
|
|
1911
1921
|
} else {
|
|
1912
|
-
parts.push(`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1922
|
+
parts.push(joinText`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1913
1923
|
}
|
|
1914
1924
|
}
|
|
1915
1925
|
const { attribute } = spread;
|
|
1916
1926
|
if (parts.length === 0) {
|
|
1917
1927
|
copy.remove(attribute.start, attribute.end);
|
|
1918
1928
|
} else {
|
|
1919
|
-
copy.overwrite(attribute.start, attribute.end,
|
|
1929
|
+
copy.overwrite(attribute.start, attribute.end, join(parts, ' '));
|
|
1920
1930
|
}
|
|
1921
1931
|
if (children !== undefined) {
|
|
1922
1932
|
const opening = spread.element.openingElement;
|
|
1923
1933
|
// Read the tag from the copy, which holds the renamed locals.
|
|
1924
1934
|
const tag = copy.slice(opening.name.start, opening.name.end);
|
|
1925
1935
|
const close = this.context.code.lastIndexOf('/>', opening.end);
|
|
1926
|
-
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
1936
|
+
copy.overwrite(close, opening.end, joinText`>${children}</${tag}>`);
|
|
1927
1937
|
}
|
|
1928
1938
|
}
|
|
1929
1939
|
for (const declaration of component.viewDeclarations) {
|
|
@@ -1937,11 +1947,11 @@ class Inliner {
|
|
|
1937
1947
|
*/
|
|
1938
1948
|
private spreadChildren(
|
|
1939
1949
|
element: JSXElement,
|
|
1940
|
-
text:
|
|
1950
|
+
text: Content | undefined,
|
|
1941
1951
|
value: PropValue | undefined,
|
|
1942
|
-
):
|
|
1952
|
+
): Content | undefined {
|
|
1943
1953
|
if (text !== undefined) {
|
|
1944
|
-
return text === 'void 0' ? undefined : `{${text}}`;
|
|
1954
|
+
return getCode(text) === 'void 0' ? undefined : joinText`{${text}}`;
|
|
1945
1955
|
}
|
|
1946
1956
|
if (!value) {
|
|
1947
1957
|
return undefined;
|
|
@@ -1949,14 +1959,10 @@ class Inliner {
|
|
|
1949
1959
|
if (value.kind === 'children' && element.closingElement) {
|
|
1950
1960
|
return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
1951
1961
|
}
|
|
1952
|
-
return `{${value.text}}`;
|
|
1962
|
+
return joinText`{${value.text}}`;
|
|
1953
1963
|
}
|
|
1954
1964
|
|
|
1955
|
-
private renameLocals(
|
|
1956
|
-
copy: MagicString,
|
|
1957
|
-
component: Component,
|
|
1958
|
-
fresh: (base: string) => string,
|
|
1959
|
-
): void {
|
|
1965
|
+
private renameLocals(copy: Editor, component: Component, fresh: (base: string) => string): void {
|
|
1960
1966
|
for (const local of component.locals) {
|
|
1961
1967
|
const name = fresh(local.name);
|
|
1962
1968
|
for (const node of [local.identifier, ...local.references]) {
|
|
@@ -1979,7 +1985,7 @@ class Inliner {
|
|
|
1979
1985
|
}
|
|
1980
1986
|
|
|
1981
1987
|
private substituteProps(
|
|
1982
|
-
copy:
|
|
1988
|
+
copy: Editor,
|
|
1983
1989
|
reads: readonly PropRead[],
|
|
1984
1990
|
values: ReadonlyMap<string, PropValue>,
|
|
1985
1991
|
): void {
|
|
@@ -2006,7 +2012,11 @@ class Inliner {
|
|
|
2006
2012
|
continue;
|
|
2007
2013
|
}
|
|
2008
2014
|
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
2009
|
-
copy.overwrite(
|
|
2015
|
+
copy.overwrite(
|
|
2016
|
+
read.member.start,
|
|
2017
|
+
read.member.end,
|
|
2018
|
+
bare ? value.text : joinText`(${value.text})`,
|
|
2019
|
+
);
|
|
2010
2020
|
}
|
|
2011
2021
|
for (const [parent, pieces] of spliced) {
|
|
2012
2022
|
const children: Piece[] = [];
|
|
@@ -2243,7 +2253,7 @@ class Inliner {
|
|
|
2243
2253
|
for (const component of components) {
|
|
2244
2254
|
const statement = this.topLevelStatement(component.binding.declaration);
|
|
2245
2255
|
if (statement) {
|
|
2246
|
-
texts.push(getText(this.context, statement));
|
|
2256
|
+
texts.push(getText(this.context, statement).code);
|
|
2247
2257
|
}
|
|
2248
2258
|
}
|
|
2249
2259
|
return texts.join('\n');
|
|
@@ -2726,7 +2736,7 @@ class Inliner {
|
|
|
2726
2736
|
): { reason: string; name: string } | undefined {
|
|
2727
2737
|
const tag = element.openingElement.name;
|
|
2728
2738
|
if (tag.type === 'JSXMemberExpression') {
|
|
2729
|
-
return { reason: 'member-tag', name: getText(this.context, tag) };
|
|
2739
|
+
return { reason: 'member-tag', name: getText(this.context, tag).code };
|
|
2730
2740
|
}
|
|
2731
2741
|
if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
|
|
2732
2742
|
return undefined;
|
|
@@ -2768,13 +2778,13 @@ class Inliner {
|
|
|
2768
2778
|
for (const { host, texts } of this.hosts.values()) {
|
|
2769
2779
|
if (host.kind === 'return') {
|
|
2770
2780
|
const indent = this.indentationAt(host.statement.start);
|
|
2771
|
-
|
|
2781
|
+
const written: Content[] = [];
|
|
2772
2782
|
for (const text of texts) {
|
|
2773
|
-
written
|
|
2783
|
+
written.push(text, `\n${indent}`);
|
|
2774
2784
|
}
|
|
2775
|
-
this.context.s.prependRight(host.statement.start, written);
|
|
2785
|
+
this.context.s.prependRight(host.statement.start, join(written));
|
|
2776
2786
|
} else {
|
|
2777
|
-
this.context.s.prependRight(host.body.start, `{ ${
|
|
2787
|
+
this.context.s.prependRight(host.body.start, joinText`{ ${join(texts, ' ')} return `);
|
|
2778
2788
|
this.context.s.appendLeft(host.body.end, '; }');
|
|
2779
2789
|
}
|
|
2780
2790
|
}
|
package/src/jsx.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* other, an empty `{}` keeps them apart.
|
|
9
9
|
*/
|
|
10
10
|
import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser';
|
|
11
|
-
import type
|
|
11
|
+
import type { Content, Editor, Text } from './text';
|
|
12
|
+
import { getCode, joinText } from './text';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* What a JSX child turns into.
|
|
@@ -17,7 +18,7 @@ export interface Piece {
|
|
|
17
18
|
/** The child the piece replaces. */
|
|
18
19
|
readonly node: JSXChild;
|
|
19
20
|
/** The replacement text, or `undefined` to keep the child as written. */
|
|
20
|
-
text:
|
|
21
|
+
text: Content | undefined;
|
|
21
22
|
/** The JSX text the piece starts with, or `undefined` when it starts with a tag or a container. */
|
|
22
23
|
startText: string | undefined;
|
|
23
24
|
/** The JSX text the piece ends with. */
|
|
@@ -32,7 +33,7 @@ export function keptPiece(node: JSXChild): Piece {
|
|
|
32
33
|
/**
|
|
33
34
|
* A piece that splices `children` in place of `node`.
|
|
34
35
|
*/
|
|
35
|
-
export function splicePiece(s:
|
|
36
|
+
export function splicePiece(s: Editor, node: JSXChild, children: readonly JSXChild[]): Piece {
|
|
36
37
|
const first = children.at(0);
|
|
37
38
|
const last = children.at(-1);
|
|
38
39
|
if (!first || !last) {
|
|
@@ -50,11 +51,12 @@ export function splicePiece(s: MagicString, node: JSXChild, children: readonly J
|
|
|
50
51
|
* JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
|
|
51
52
|
* mark where the text ends.
|
|
52
53
|
*/
|
|
53
|
-
export function replacedPiece(node: JSXChild, text:
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
54
|
+
export function replacedPiece(node: JSXChild, text: Content): Piece {
|
|
55
|
+
const code = getCode(text);
|
|
56
|
+
const start = code.search(/[<{]/);
|
|
57
|
+
const end = Math.max(code.lastIndexOf('>'), code.lastIndexOf('}'));
|
|
58
|
+
const startText = start === -1 ? code : code.slice(0, start);
|
|
59
|
+
const endText = code.slice(end + 1);
|
|
58
60
|
return {
|
|
59
61
|
node,
|
|
60
62
|
text,
|
|
@@ -83,12 +85,12 @@ function mergeChangesText(before: string | undefined, after: string | undefined)
|
|
|
83
85
|
*/
|
|
84
86
|
function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]): void {
|
|
85
87
|
if (next.text !== undefined) {
|
|
86
|
-
next.text = `{}${next.text}`;
|
|
88
|
+
next.text = joinText`{}${next.text}`;
|
|
87
89
|
next.startText = undefined;
|
|
88
90
|
return;
|
|
89
91
|
}
|
|
90
92
|
if (previous.text !== undefined) {
|
|
91
|
-
previous.text = `${previous.text}{}`;
|
|
93
|
+
previous.text = joinText`${previous.text}{}`;
|
|
92
94
|
previous.endText = undefined;
|
|
93
95
|
return;
|
|
94
96
|
}
|
|
@@ -103,13 +105,13 @@ function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]
|
|
|
103
105
|
* Writes the replaced pieces of a child list, adding `{}` wherever two texts
|
|
104
106
|
* would otherwise merge.
|
|
105
107
|
*/
|
|
106
|
-
export function writePieces(s:
|
|
108
|
+
export function writePieces(s: Editor, pieces: readonly Piece[]): boolean {
|
|
107
109
|
let changed = false;
|
|
108
110
|
// The last piece that renders anything, and the empty pieces after it.
|
|
109
111
|
let previous: Piece | undefined;
|
|
110
112
|
let emptiesSincePrevious: Piece[] = [];
|
|
111
113
|
for (const piece of pieces) {
|
|
112
|
-
if (piece.text === '') {
|
|
114
|
+
if (piece.text !== undefined && getCode(piece.text) === '') {
|
|
113
115
|
emptiesSincePrevious.push(piece);
|
|
114
116
|
continue;
|
|
115
117
|
}
|
|
@@ -124,7 +126,7 @@ export function writePieces(s: MagicString, pieces: readonly Piece[]): boolean {
|
|
|
124
126
|
continue;
|
|
125
127
|
}
|
|
126
128
|
changed = true;
|
|
127
|
-
if (piece.text === '') {
|
|
129
|
+
if (getCode(piece.text) === '') {
|
|
128
130
|
s.remove(piece.node.start, piece.node.end);
|
|
129
131
|
} else {
|
|
130
132
|
s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
@@ -178,7 +180,7 @@ export function hasCallbackChild(children: readonly JSXChild[]): boolean {
|
|
|
178
180
|
/**
|
|
179
181
|
* The text between an element's tags, or an empty string for a self-closing element.
|
|
180
182
|
*/
|
|
181
|
-
export function innerText(s:
|
|
183
|
+
export function innerText(s: Editor, node: JSXElement | JSXFragment): Text | string {
|
|
182
184
|
if (node.type === 'JSXFragment') {
|
|
183
185
|
return s.slice(node.openingFragment.end, node.closingFragment.start);
|
|
184
186
|
}
|
package/src/memo.ts
CHANGED
|
@@ -97,6 +97,8 @@ import { usesFunctionContext } from './inline';
|
|
|
97
97
|
import type { Binding } from './scope';
|
|
98
98
|
import { lookup, scopeAt } from './scope';
|
|
99
99
|
import { compactLength } from './size';
|
|
100
|
+
import type { Text } from './text';
|
|
101
|
+
import { joinText } from './text';
|
|
100
102
|
|
|
101
103
|
/**
|
|
102
104
|
* Props of Solid's built-in components that are read inside a memo, once per update.
|
|
@@ -219,7 +221,7 @@ type MemoKind = 'createMemo' | 'children';
|
|
|
219
221
|
* other than a regular expression, a template literal without expressions,
|
|
220
222
|
* or a negated number.
|
|
221
223
|
*/
|
|
222
|
-
function constantText(context: PassContext, node: Expression):
|
|
224
|
+
function constantText(context: PassContext, node: Expression): Text | undefined {
|
|
223
225
|
const expression = unwrap(node);
|
|
224
226
|
switch (expression.type) {
|
|
225
227
|
case 'Literal':
|
|
@@ -338,7 +340,7 @@ class MemoInliner {
|
|
|
338
340
|
private inlineConstant(
|
|
339
341
|
declarator: VariableDeclarator,
|
|
340
342
|
binding: Binding,
|
|
341
|
-
text:
|
|
343
|
+
text: Text,
|
|
342
344
|
kind: 'memo' | 'signal' = 'memo',
|
|
343
345
|
): true | undefined {
|
|
344
346
|
const statement = this.context.parents.get(declarator);
|
|
@@ -370,7 +372,7 @@ class MemoInliner {
|
|
|
370
372
|
parent?.type === 'ArrayExpression' ||
|
|
371
373
|
parent?.type === 'Property' ||
|
|
372
374
|
(parent?.type === 'CallExpression' && parent.callee !== call);
|
|
373
|
-
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
375
|
+
this.context.s.overwrite(call.start, call.end, bare ? text : joinText`(${text})`);
|
|
374
376
|
this.edited.push([call.start, call.end]);
|
|
375
377
|
}
|
|
376
378
|
this.context.s.remove(statement.start, statement.end);
|
|
@@ -642,7 +644,7 @@ class MemoInliner {
|
|
|
642
644
|
* change what a read does, such as `unobserved`, so a signal with options
|
|
643
645
|
* stays.
|
|
644
646
|
*/
|
|
645
|
-
private signalConstant(declarator: VariableDeclarator, binding: Binding):
|
|
647
|
+
private signalConstant(declarator: VariableDeclarator, binding: Binding): Text | undefined {
|
|
646
648
|
const pattern = declarator.id;
|
|
647
649
|
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
648
650
|
if (
|
|
@@ -750,7 +752,7 @@ class MemoInliner {
|
|
|
750
752
|
// Minified, the arrow costs `const a=()=>body;`, 13 characters besides the
|
|
751
753
|
// body, and each call `a()` costs 3. Copying adds the body to every call
|
|
752
754
|
// but one, and removes the rest.
|
|
753
|
-
const length = compactLength(text);
|
|
755
|
+
const length = compactLength(text.code);
|
|
754
756
|
if ((calls.length - 1) * length > calls.length * 3 + 13) {
|
|
755
757
|
return;
|
|
756
758
|
}
|
|
@@ -767,7 +769,11 @@ class MemoInliner {
|
|
|
767
769
|
const parent = this.context.parents.get(call);
|
|
768
770
|
const inContainer =
|
|
769
771
|
parent?.type === 'JSXExpressionContainer' && body.type !== 'SequenceExpression';
|
|
770
|
-
this.context.s.overwrite(
|
|
772
|
+
this.context.s.overwrite(
|
|
773
|
+
call.start,
|
|
774
|
+
call.end,
|
|
775
|
+
bare || inContainer ? text : joinText`(${text})`,
|
|
776
|
+
);
|
|
771
777
|
this.edited.push([call.start, call.end]);
|
|
772
778
|
}
|
|
773
779
|
this.context.s.remove(statement.start, statement.end);
|