solid-optimizer 1.0.0-next.9 → 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-DptOANJi.mjs → compile-Cvjy2e11.mjs} +311 -159
- 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 +57 -53
- package/src/inline.ts +67 -59
- package/src/jsx.ts +16 -14
- package/src/memo.ts +12 -6
- package/src/text.ts +297 -0
- package/src/vite/index.ts +66 -12
- package/dist/compile-DptOANJi.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
|
|
|
@@ -1164,7 +1165,7 @@ class Inliner {
|
|
|
1164
1165
|
if (inner.type !== 'ObjectExpression') {
|
|
1165
1166
|
return undefined;
|
|
1166
1167
|
}
|
|
1167
|
-
const defaults = new Map<string,
|
|
1168
|
+
const defaults = new Map<string, Text>();
|
|
1168
1169
|
for (const property of inner.properties) {
|
|
1169
1170
|
if (property.type !== 'Property' || property.kind !== 'init' || property.computed) {
|
|
1170
1171
|
return undefined;
|
|
@@ -1182,7 +1183,7 @@ class Inliner {
|
|
|
1182
1183
|
if (text === undefined || isReservedPropName(key)) {
|
|
1183
1184
|
return undefined;
|
|
1184
1185
|
}
|
|
1185
|
-
defaults.set(key, text);
|
|
1186
|
+
defaults.set(key, this.context.s.written(text, property.value.start));
|
|
1186
1187
|
}
|
|
1187
1188
|
sources.push({ defaults, hidden: new Set() });
|
|
1188
1189
|
}
|
|
@@ -1334,7 +1335,7 @@ class Inliner {
|
|
|
1334
1335
|
element: JSXElement,
|
|
1335
1336
|
reads: readonly ReadCount[],
|
|
1336
1337
|
fresh: (base: string) => string,
|
|
1337
|
-
snapshots:
|
|
1338
|
+
snapshots: Content[],
|
|
1338
1339
|
): PropValue | undefined {
|
|
1339
1340
|
const { children } = element;
|
|
1340
1341
|
if (hasSpreadChild(children)) {
|
|
@@ -1345,10 +1346,10 @@ class Inliner {
|
|
|
1345
1346
|
if (only === undefined) {
|
|
1346
1347
|
return { kind: 'children', children: [], text: 'void 0', primary: false };
|
|
1347
1348
|
}
|
|
1348
|
-
let text:
|
|
1349
|
+
let text: Content;
|
|
1349
1350
|
let primary = true;
|
|
1350
1351
|
if (significant.length > 1 || only.type === 'JSXText') {
|
|
1351
|
-
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)}</>`;
|
|
1352
1353
|
} else if (only.type === 'JSXExpressionContainer') {
|
|
1353
1354
|
if (only.expression.type === 'JSXEmptyExpression') {
|
|
1354
1355
|
return undefined;
|
|
@@ -1356,7 +1357,10 @@ class Inliner {
|
|
|
1356
1357
|
// A single expression is passed like an attribute value, such as a
|
|
1357
1358
|
// render function that has to stay one function.
|
|
1358
1359
|
const value = this.expressionValue(only.expression, 'children', reads, fresh, snapshots);
|
|
1359
|
-
if (
|
|
1360
|
+
if (
|
|
1361
|
+
value.kind !== 'expression' ||
|
|
1362
|
+
getCode(value.text) !== getText(this.context, only.expression).code
|
|
1363
|
+
) {
|
|
1360
1364
|
return value;
|
|
1361
1365
|
}
|
|
1362
1366
|
text = value.text;
|
|
@@ -1412,7 +1416,7 @@ class Inliner {
|
|
|
1412
1416
|
attribute: JSXAttribute | undefined,
|
|
1413
1417
|
reads: readonly ReadCount[],
|
|
1414
1418
|
fresh: (base: string) => string,
|
|
1415
|
-
snapshots:
|
|
1419
|
+
snapshots: Content[],
|
|
1416
1420
|
): PropValue | undefined {
|
|
1417
1421
|
if (!attribute) {
|
|
1418
1422
|
return UNDEFINED;
|
|
@@ -1427,7 +1431,11 @@ class Inliner {
|
|
|
1427
1431
|
if (value.value.includes('&')) {
|
|
1428
1432
|
return undefined;
|
|
1429
1433
|
}
|
|
1430
|
-
return {
|
|
1434
|
+
return {
|
|
1435
|
+
kind: 'expression',
|
|
1436
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
1437
|
+
primary: true,
|
|
1438
|
+
};
|
|
1431
1439
|
case 'JSXElement':
|
|
1432
1440
|
case 'JSXFragment':
|
|
1433
1441
|
return { kind: 'jsx', text: getText(this.context, value), primary: true };
|
|
@@ -1450,7 +1458,7 @@ class Inliner {
|
|
|
1450
1458
|
name: string,
|
|
1451
1459
|
reads: readonly ReadCount[],
|
|
1452
1460
|
fresh: (base: string) => string,
|
|
1453
|
-
snapshots:
|
|
1461
|
+
snapshots: Content[],
|
|
1454
1462
|
): PropValue {
|
|
1455
1463
|
const text = getText(this.context, expression);
|
|
1456
1464
|
const inner = unwrap(expression);
|
|
@@ -1467,7 +1475,7 @@ class Inliner {
|
|
|
1467
1475
|
return { kind: 'expression', text, primary };
|
|
1468
1476
|
}
|
|
1469
1477
|
const local = fresh(name);
|
|
1470
|
-
snapshots.push(`const ${local} = ${text};`);
|
|
1478
|
+
snapshots.push(joinText`const ${local} = ${text};`);
|
|
1471
1479
|
return { kind: 'expression', text: local, primary: true };
|
|
1472
1480
|
}
|
|
1473
1481
|
|
|
@@ -1558,7 +1566,7 @@ class Inliner {
|
|
|
1558
1566
|
}
|
|
1559
1567
|
|
|
1560
1568
|
// What each prop reads as.
|
|
1561
|
-
const snapshots:
|
|
1569
|
+
const snapshots: Content[] = [];
|
|
1562
1570
|
const fresh = (base: string): string => this.freshName(base);
|
|
1563
1571
|
const readsByName = new Map<string, ReadCount[]>();
|
|
1564
1572
|
const count = (name: string, read: ReadCount): void => {
|
|
@@ -1617,7 +1625,7 @@ class Inliner {
|
|
|
1617
1625
|
}
|
|
1618
1626
|
|
|
1619
1627
|
// Build the copy on the original code, so this pass's other edits stay out of it.
|
|
1620
|
-
const copy =
|
|
1628
|
+
const copy = this.context.s.fresh();
|
|
1621
1629
|
this.renameLocals(copy, component, fresh);
|
|
1622
1630
|
this.substituteProps(copy, views.propReads, values);
|
|
1623
1631
|
this.substituteViews(copy, component, element, views, values);
|
|
@@ -1631,7 +1639,7 @@ class Inliner {
|
|
|
1631
1639
|
if (first && last) {
|
|
1632
1640
|
// The last statement can rely on a line break that it loses here.
|
|
1633
1641
|
const text = copy.slice(first.start, last.end);
|
|
1634
|
-
statements.push(text.endsWith(';') ? text : `${text};`);
|
|
1642
|
+
statements.push(text.code.endsWith(';') ? text : joinText`${text};`);
|
|
1635
1643
|
}
|
|
1636
1644
|
return {
|
|
1637
1645
|
element,
|
|
@@ -1642,7 +1650,7 @@ class Inliner {
|
|
|
1642
1650
|
}
|
|
1643
1651
|
|
|
1644
1652
|
/** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
|
|
1645
|
-
private addToHost(found: { fn: Node; host: Host }, texts: readonly
|
|
1653
|
+
private addToHost(found: { fn: Node; host: Host }, texts: readonly Content[]): void {
|
|
1646
1654
|
const key = found.host.kind === 'return' ? found.host.statement : found.fn;
|
|
1647
1655
|
const entry = this.hosts.get(key) ?? { host: found.host, texts: [] };
|
|
1648
1656
|
entry.texts.push(...texts);
|
|
@@ -1664,8 +1672,8 @@ class Inliner {
|
|
|
1664
1672
|
* provider above it answers, with the text each becomes. `undefined` when
|
|
1665
1673
|
* a read cannot take the value there.
|
|
1666
1674
|
*/
|
|
1667
|
-
private contextTexts(component: Component, element: JSXElement): Map<Node,
|
|
1668
|
-
const texts = new Map<Node,
|
|
1675
|
+
private contextTexts(component: Component, element: JSXElement): Map<Node, Content> | undefined {
|
|
1676
|
+
const texts = new Map<Node, Content>();
|
|
1669
1677
|
for (const { provider, text } of this.answeringProviders(element)) {
|
|
1670
1678
|
for (const read of contextReads(this.context, component.fn, provider.context)) {
|
|
1671
1679
|
const value = text ?? stableValueText(this.context, provider, element);
|
|
@@ -1682,8 +1690,8 @@ class Inliner {
|
|
|
1682
1690
|
* The closed providers that answer context reads at `element`. The nearest
|
|
1683
1691
|
* provider of each context answers its reads, closed or not.
|
|
1684
1692
|
*/
|
|
1685
|
-
private answeringProviders(element: JSXElement): { provider: Provider; text?:
|
|
1686
|
-
const answering: { provider: Provider; text?:
|
|
1693
|
+
private answeringProviders(element: JSXElement): { provider: Provider; text?: Content }[] {
|
|
1694
|
+
const answering: { provider: Provider; text?: Content }[] = [];
|
|
1687
1695
|
const seen = new Set<Binding>();
|
|
1688
1696
|
for (
|
|
1689
1697
|
let node = this.context.parents.get(element);
|
|
@@ -1778,7 +1786,7 @@ class Inliner {
|
|
|
1778
1786
|
const name = this.freshName(
|
|
1779
1787
|
`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
|
|
1780
1788
|
);
|
|
1781
|
-
this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
1789
|
+
this.addToHost(host, [joinText`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
1782
1790
|
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
1783
1791
|
this.transparent.set(element, { provider, text: name });
|
|
1784
1792
|
this.changed = true;
|
|
@@ -1806,7 +1814,7 @@ class Inliner {
|
|
|
1806
1814
|
const has = (key: string): boolean =>
|
|
1807
1815
|
attributes.has(key) || (key === 'children' && hasChildren);
|
|
1808
1816
|
// The last source that has the key answers, even when its value is `undefined`.
|
|
1809
|
-
const resolve = (view: View, key: string):
|
|
1817
|
+
const resolve = (view: View, key: string): Content | undefined => {
|
|
1810
1818
|
for (let index = view.sources.length - 1; index >= 0; index -= 1) {
|
|
1811
1819
|
const source = view.sources.at(index);
|
|
1812
1820
|
if (!source || source.hidden.has(key)) {
|
|
@@ -1824,7 +1832,7 @@ class Inliner {
|
|
|
1824
1832
|
};
|
|
1825
1833
|
|
|
1826
1834
|
const propReads: PropRead[] = [...component.props];
|
|
1827
|
-
const texts = new Map<Node,
|
|
1835
|
+
const texts = new Map<Node, Content>();
|
|
1828
1836
|
for (const read of component.viewReads) {
|
|
1829
1837
|
const text = resolve(read.view, read.name);
|
|
1830
1838
|
if (text === undefined) {
|
|
@@ -1864,13 +1872,13 @@ class Inliner {
|
|
|
1864
1872
|
names.add(
|
|
1865
1873
|
attribute.name.type === 'JSXIdentifier'
|
|
1866
1874
|
? attribute.name.name
|
|
1867
|
-
: getText(this.context, attribute.name),
|
|
1875
|
+
: getText(this.context, attribute.name).code,
|
|
1868
1876
|
);
|
|
1869
1877
|
}
|
|
1870
1878
|
}
|
|
1871
1879
|
taken.set(spread.element, names);
|
|
1872
1880
|
}
|
|
1873
|
-
const keys = new Map<string,
|
|
1881
|
+
const keys = new Map<string, Content | undefined>();
|
|
1874
1882
|
for (const key of order) {
|
|
1875
1883
|
// Children become the element's children, so it must have none of its own.
|
|
1876
1884
|
const fits =
|
|
@@ -1893,39 +1901,39 @@ class Inliner {
|
|
|
1893
1901
|
* view declarations.
|
|
1894
1902
|
*/
|
|
1895
1903
|
private substituteViews(
|
|
1896
|
-
copy:
|
|
1904
|
+
copy: Editor,
|
|
1897
1905
|
component: Component,
|
|
1898
1906
|
element: JSXElement,
|
|
1899
1907
|
views: ResolvedViews,
|
|
1900
1908
|
values: ReadonlyMap<string, PropValue>,
|
|
1901
1909
|
): void {
|
|
1902
1910
|
for (const [member, text] of views.texts) {
|
|
1903
|
-
const bare = text !== 'void 0' || acceptsAnyExpression(this.context, member);
|
|
1904
|
-
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})`);
|
|
1905
1913
|
}
|
|
1906
1914
|
for (const { spread, keys } of views.spreads) {
|
|
1907
|
-
const parts:
|
|
1908
|
-
let children:
|
|
1915
|
+
const parts: Text[] = [];
|
|
1916
|
+
let children: Content | undefined;
|
|
1909
1917
|
for (const [key, text] of keys) {
|
|
1910
1918
|
const value = values.get(key);
|
|
1911
1919
|
if (key === 'children') {
|
|
1912
1920
|
children = this.spreadChildren(element, text, value);
|
|
1913
1921
|
} else {
|
|
1914
|
-
parts.push(`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1922
|
+
parts.push(joinText`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1915
1923
|
}
|
|
1916
1924
|
}
|
|
1917
1925
|
const { attribute } = spread;
|
|
1918
1926
|
if (parts.length === 0) {
|
|
1919
1927
|
copy.remove(attribute.start, attribute.end);
|
|
1920
1928
|
} else {
|
|
1921
|
-
copy.overwrite(attribute.start, attribute.end,
|
|
1929
|
+
copy.overwrite(attribute.start, attribute.end, join(parts, ' '));
|
|
1922
1930
|
}
|
|
1923
1931
|
if (children !== undefined) {
|
|
1924
1932
|
const opening = spread.element.openingElement;
|
|
1925
1933
|
// Read the tag from the copy, which holds the renamed locals.
|
|
1926
1934
|
const tag = copy.slice(opening.name.start, opening.name.end);
|
|
1927
1935
|
const close = this.context.code.lastIndexOf('/>', opening.end);
|
|
1928
|
-
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
1936
|
+
copy.overwrite(close, opening.end, joinText`>${children}</${tag}>`);
|
|
1929
1937
|
}
|
|
1930
1938
|
}
|
|
1931
1939
|
for (const declaration of component.viewDeclarations) {
|
|
@@ -1939,11 +1947,11 @@ class Inliner {
|
|
|
1939
1947
|
*/
|
|
1940
1948
|
private spreadChildren(
|
|
1941
1949
|
element: JSXElement,
|
|
1942
|
-
text:
|
|
1950
|
+
text: Content | undefined,
|
|
1943
1951
|
value: PropValue | undefined,
|
|
1944
|
-
):
|
|
1952
|
+
): Content | undefined {
|
|
1945
1953
|
if (text !== undefined) {
|
|
1946
|
-
return text === 'void 0' ? undefined : `{${text}}`;
|
|
1954
|
+
return getCode(text) === 'void 0' ? undefined : joinText`{${text}}`;
|
|
1947
1955
|
}
|
|
1948
1956
|
if (!value) {
|
|
1949
1957
|
return undefined;
|
|
@@ -1951,14 +1959,10 @@ class Inliner {
|
|
|
1951
1959
|
if (value.kind === 'children' && element.closingElement) {
|
|
1952
1960
|
return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
1953
1961
|
}
|
|
1954
|
-
return `{${value.text}}`;
|
|
1962
|
+
return joinText`{${value.text}}`;
|
|
1955
1963
|
}
|
|
1956
1964
|
|
|
1957
|
-
private renameLocals(
|
|
1958
|
-
copy: MagicString,
|
|
1959
|
-
component: Component,
|
|
1960
|
-
fresh: (base: string) => string,
|
|
1961
|
-
): void {
|
|
1965
|
+
private renameLocals(copy: Editor, component: Component, fresh: (base: string) => string): void {
|
|
1962
1966
|
for (const local of component.locals) {
|
|
1963
1967
|
const name = fresh(local.name);
|
|
1964
1968
|
for (const node of [local.identifier, ...local.references]) {
|
|
@@ -1981,7 +1985,7 @@ class Inliner {
|
|
|
1981
1985
|
}
|
|
1982
1986
|
|
|
1983
1987
|
private substituteProps(
|
|
1984
|
-
copy:
|
|
1988
|
+
copy: Editor,
|
|
1985
1989
|
reads: readonly PropRead[],
|
|
1986
1990
|
values: ReadonlyMap<string, PropValue>,
|
|
1987
1991
|
): void {
|
|
@@ -2008,7 +2012,11 @@ class Inliner {
|
|
|
2008
2012
|
continue;
|
|
2009
2013
|
}
|
|
2010
2014
|
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
2011
|
-
copy.overwrite(
|
|
2015
|
+
copy.overwrite(
|
|
2016
|
+
read.member.start,
|
|
2017
|
+
read.member.end,
|
|
2018
|
+
bare ? value.text : joinText`(${value.text})`,
|
|
2019
|
+
);
|
|
2012
2020
|
}
|
|
2013
2021
|
for (const [parent, pieces] of spliced) {
|
|
2014
2022
|
const children: Piece[] = [];
|
|
@@ -2245,7 +2253,7 @@ class Inliner {
|
|
|
2245
2253
|
for (const component of components) {
|
|
2246
2254
|
const statement = this.topLevelStatement(component.binding.declaration);
|
|
2247
2255
|
if (statement) {
|
|
2248
|
-
texts.push(getText(this.context, statement));
|
|
2256
|
+
texts.push(getText(this.context, statement).code);
|
|
2249
2257
|
}
|
|
2250
2258
|
}
|
|
2251
2259
|
return texts.join('\n');
|
|
@@ -2728,7 +2736,7 @@ class Inliner {
|
|
|
2728
2736
|
): { reason: string; name: string } | undefined {
|
|
2729
2737
|
const tag = element.openingElement.name;
|
|
2730
2738
|
if (tag.type === 'JSXMemberExpression') {
|
|
2731
|
-
return { reason: 'member-tag', name: getText(this.context, tag) };
|
|
2739
|
+
return { reason: 'member-tag', name: getText(this.context, tag).code };
|
|
2732
2740
|
}
|
|
2733
2741
|
if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
|
|
2734
2742
|
return undefined;
|
|
@@ -2770,13 +2778,13 @@ class Inliner {
|
|
|
2770
2778
|
for (const { host, texts } of this.hosts.values()) {
|
|
2771
2779
|
if (host.kind === 'return') {
|
|
2772
2780
|
const indent = this.indentationAt(host.statement.start);
|
|
2773
|
-
|
|
2781
|
+
const written: Content[] = [];
|
|
2774
2782
|
for (const text of texts) {
|
|
2775
|
-
written
|
|
2783
|
+
written.push(text, `\n${indent}`);
|
|
2776
2784
|
}
|
|
2777
|
-
this.context.s.prependRight(host.statement.start, written);
|
|
2785
|
+
this.context.s.prependRight(host.statement.start, join(written));
|
|
2778
2786
|
} else {
|
|
2779
|
-
this.context.s.prependRight(host.body.start, `{ ${
|
|
2787
|
+
this.context.s.prependRight(host.body.start, joinText`{ ${join(texts, ' ')} return `);
|
|
2780
2788
|
this.context.s.appendLeft(host.body.end, '; }');
|
|
2781
2789
|
}
|
|
2782
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);
|