solid-optimizer 1.0.0-next.0 → 1.0.0-next.10
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 +27 -13
- package/dist/{compile-Cz6hbiQv.mjs → compile-Cvjy2e11.mjs} +1925 -385
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -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 +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +8 -5
- package/src/ast.ts +4 -3
- package/src/compile.ts +96 -16
- package/src/conditional.ts +547 -0
- package/src/constants.ts +58 -13
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
package/src/jsx.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* whenever a replacement would put two texts that were apart next to each
|
|
8
8
|
* other, an empty `{}` keeps them apart.
|
|
9
9
|
*/
|
|
10
|
-
import type { JSXAttribute, JSXChild, JSXElement, JSXFragment,
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
10
|
+
import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser';
|
|
11
|
+
import type { Content, Editor, Text } from './text';
|
|
12
|
+
import { getCode, joinText } from './text';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* What a JSX child turns into.
|
|
@@ -18,48 +18,65 @@ export interface Piece {
|
|
|
18
18
|
/** The child the piece replaces. */
|
|
19
19
|
readonly node: JSXChild;
|
|
20
20
|
/** The replacement text, or `undefined` to keep the child as written. */
|
|
21
|
-
text:
|
|
22
|
-
/** The JSX text the
|
|
23
|
-
startText:
|
|
24
|
-
/** The JSX text the
|
|
25
|
-
endText:
|
|
21
|
+
text: Content | undefined;
|
|
22
|
+
/** The JSX text the piece starts with, or `undefined` when it starts with a tag or a container. */
|
|
23
|
+
startText: string | undefined;
|
|
24
|
+
/** The JSX text the piece ends with. */
|
|
25
|
+
endText: string | undefined;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function keptPiece(node: JSXChild): Piece {
|
|
29
|
-
const text = node.type === 'JSXText' ? node : undefined;
|
|
29
|
+
const text = node.type === 'JSXText' ? node.value : undefined;
|
|
30
30
|
return { node, text: undefined, startText: text, endText: text };
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* A piece that splices `children` in place of `node`.
|
|
35
35
|
*/
|
|
36
|
-
export function splicePiece(s:
|
|
36
|
+
export function splicePiece(s: Editor, node: JSXChild, children: readonly JSXChild[]): Piece {
|
|
37
37
|
const first = children.at(0);
|
|
38
38
|
const last = children.at(-1);
|
|
39
39
|
if (!first || !last) {
|
|
40
40
|
return replacedPiece(node, '');
|
|
41
41
|
}
|
|
42
|
+
return replacedPiece(node, s.slice(first.start, last.end));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A piece that writes `text`, JSX children as source, in place of `node`.
|
|
47
|
+
*
|
|
48
|
+
* The texts it starts and ends with are read from `text` itself, not from
|
|
49
|
+
* the children it came from. A fold inside them in the same round can have
|
|
50
|
+
* turned a child that was an element into text, such as an inner `<Show>`.
|
|
51
|
+
* JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
|
|
52
|
+
* mark where the text ends.
|
|
53
|
+
*/
|
|
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);
|
|
42
60
|
return {
|
|
43
61
|
node,
|
|
44
|
-
text
|
|
45
|
-
startText:
|
|
46
|
-
endText:
|
|
62
|
+
text,
|
|
63
|
+
startText: startText === '' ? undefined : startText,
|
|
64
|
+
endText: endText === '' ? undefined : endText,
|
|
47
65
|
};
|
|
48
66
|
}
|
|
49
67
|
|
|
50
|
-
|
|
51
|
-
|
|
68
|
+
/** Whether JSX drops this text: whitespace that contains a line break. */
|
|
69
|
+
function isDroppedText(text: string): boolean {
|
|
70
|
+
return /^\s*$/.test(text) && /[\r\n]/.test(text);
|
|
52
71
|
}
|
|
53
72
|
|
|
54
73
|
/**
|
|
55
74
|
* Whether two texts render differently as one text node than apart.
|
|
56
75
|
* Two texts that JSX drops, like line breaks and indentation, still drop together.
|
|
57
76
|
*/
|
|
58
|
-
function mergeChangesText(before:
|
|
77
|
+
function mergeChangesText(before: string | undefined, after: string | undefined): boolean {
|
|
59
78
|
return (
|
|
60
|
-
before !== undefined &&
|
|
61
|
-
after !== undefined &&
|
|
62
|
-
!(isFilteredText(before) && isFilteredText(after))
|
|
79
|
+
before !== undefined && after !== undefined && !(isDroppedText(before) && isDroppedText(after))
|
|
63
80
|
);
|
|
64
81
|
}
|
|
65
82
|
|
|
@@ -68,12 +85,12 @@ function mergeChangesText(before: JSXText | undefined, after: JSXText | undefine
|
|
|
68
85
|
*/
|
|
69
86
|
function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]): void {
|
|
70
87
|
if (next.text !== undefined) {
|
|
71
|
-
next.text = `{}${next.text}`;
|
|
88
|
+
next.text = joinText`{}${next.text}`;
|
|
72
89
|
next.startText = undefined;
|
|
73
90
|
return;
|
|
74
91
|
}
|
|
75
92
|
if (previous.text !== undefined) {
|
|
76
|
-
previous.text = `${previous.text}{}`;
|
|
93
|
+
previous.text = joinText`${previous.text}{}`;
|
|
77
94
|
previous.endText = undefined;
|
|
78
95
|
return;
|
|
79
96
|
}
|
|
@@ -88,13 +105,13 @@ function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]
|
|
|
88
105
|
* Writes the replaced pieces of a child list, adding `{}` wherever two texts
|
|
89
106
|
* would otherwise merge.
|
|
90
107
|
*/
|
|
91
|
-
export function writePieces(s:
|
|
108
|
+
export function writePieces(s: Editor, pieces: readonly Piece[]): boolean {
|
|
92
109
|
let changed = false;
|
|
93
110
|
// The last piece that renders anything, and the empty pieces after it.
|
|
94
111
|
let previous: Piece | undefined;
|
|
95
112
|
let emptiesSincePrevious: Piece[] = [];
|
|
96
113
|
for (const piece of pieces) {
|
|
97
|
-
if (piece.text === '') {
|
|
114
|
+
if (piece.text !== undefined && getCode(piece.text) === '') {
|
|
98
115
|
emptiesSincePrevious.push(piece);
|
|
99
116
|
continue;
|
|
100
117
|
}
|
|
@@ -109,7 +126,7 @@ export function writePieces(s: MagicString, pieces: readonly Piece[]): boolean {
|
|
|
109
126
|
continue;
|
|
110
127
|
}
|
|
111
128
|
changed = true;
|
|
112
|
-
if (piece.text === '') {
|
|
129
|
+
if (getCode(piece.text) === '') {
|
|
113
130
|
s.remove(piece.node.start, piece.node.end);
|
|
114
131
|
} else {
|
|
115
132
|
s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
@@ -132,9 +149,12 @@ export function findAttribute(element: JSXElement, name: string): JSXAttribute |
|
|
|
132
149
|
}
|
|
133
150
|
|
|
134
151
|
export function hasSpreadAttribute(element: JSXElement): boolean {
|
|
135
|
-
|
|
136
|
-
(attribute
|
|
137
|
-
|
|
152
|
+
for (const attribute of element.openingElement.attributes) {
|
|
153
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
138
158
|
}
|
|
139
159
|
|
|
140
160
|
/**
|
|
@@ -142,24 +162,25 @@ export function hasSpreadAttribute(element: JSXElement): boolean {
|
|
|
142
162
|
* narrowed value. Folding one away would drop that call.
|
|
143
163
|
*/
|
|
144
164
|
export function hasCallbackChild(children: readonly JSXChild[]): boolean {
|
|
145
|
-
|
|
165
|
+
for (const child of children) {
|
|
146
166
|
if (child.type !== 'JSXExpressionContainer') {
|
|
147
|
-
|
|
167
|
+
continue;
|
|
148
168
|
}
|
|
149
169
|
let expression: Node = child.expression;
|
|
150
170
|
while (expression.type === 'ParenthesizedExpression') {
|
|
151
171
|
expression = expression.expression;
|
|
152
172
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
173
|
+
if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
157
178
|
}
|
|
158
179
|
|
|
159
180
|
/**
|
|
160
181
|
* The text between an element's tags, or an empty string for a self-closing element.
|
|
161
182
|
*/
|
|
162
|
-
export function innerText(s:
|
|
183
|
+
export function innerText(s: Editor, node: JSXElement | JSXFragment): Text | string {
|
|
163
184
|
if (node.type === 'JSXFragment') {
|
|
164
185
|
return s.slice(node.openingFragment.end, node.closingFragment.start);
|
|
165
186
|
}
|