solid-optimizer 1.0.0-next.9 → 1.1.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 +14 -19
- package/dist/{compile-X_SMDFr2.d.mts → compile-BKA77zb8.d.mts} +10 -2
- package/dist/compile-BKA77zb8.d.mts.map +1 -0
- package/dist/{compile-DptOANJi.mjs → compile-D2f8eXFk.mjs} +548 -161
- package/dist/compile-D2f8eXFk.mjs.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 +29 -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/tidy.ts +390 -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solid-optimizer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Experimental compile-time optimizer for SolidJS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compiler",
|
|
@@ -42,21 +42,21 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@fast-check/vitest": "^0.5.0",
|
|
44
44
|
"@jridgewell/trace-mapping": "^0.3.31",
|
|
45
|
-
"@solidjs/compiler": "2.0.0
|
|
46
|
-
"@solidjs/vite-plugin": "3.0.0
|
|
47
|
-
"@solidjs/web": "2.0.0
|
|
45
|
+
"@solidjs/compiler": "2.0.0",
|
|
46
|
+
"@solidjs/vite-plugin": "3.0.0",
|
|
47
|
+
"@solidjs/web": "2.0.0",
|
|
48
48
|
"@types/node": "^26.6.3",
|
|
49
49
|
"fast-check": "^4.10.2",
|
|
50
50
|
"happy-dom": "^20.14.5",
|
|
51
51
|
"publint": "^0.3.24",
|
|
52
|
-
"solid-js": "2.0.0
|
|
52
|
+
"solid-js": "2.0.0",
|
|
53
53
|
"tsdown": "^0.23.0",
|
|
54
54
|
"typescript": "^7.0.2",
|
|
55
55
|
"vite": "^8.3.1",
|
|
56
56
|
"vitest": "^5.0.2"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@solidjs/vite-plugin": "^3.0.0
|
|
59
|
+
"@solidjs/vite-plugin": "^3.0.0",
|
|
60
60
|
"vite": "^8.0.0 || ^9.0.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependenciesMeta": {
|
package/src/compile.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { SourceMap } from '@jridgewell/remapping';
|
|
1
|
+
import type { DecodedSourceMap, SourceMap } from '@jridgewell/remapping';
|
|
2
2
|
import type { Program } from 'oxc-parser';
|
|
3
3
|
import remapping from '@jridgewell/remapping';
|
|
4
|
-
import MagicString from 'magic-string';
|
|
5
4
|
import type { Parents } from './ast';
|
|
6
5
|
import { collectParents, parse } from './ast';
|
|
7
6
|
import type { ImportedConstants } from './constants';
|
|
@@ -16,6 +15,8 @@ import { lowerConditionals } from './conditional';
|
|
|
16
15
|
import type { ScopeAnalysis } from './scope';
|
|
17
16
|
import { analyzeScopes } from './scope';
|
|
18
17
|
import { terminateStatements } from './terminate';
|
|
18
|
+
import { tidyJSX } from './tidy';
|
|
19
|
+
import { Editor } from './text';
|
|
19
20
|
import type { Primitive } from './value';
|
|
20
21
|
|
|
21
22
|
export interface CompileOptions {
|
|
@@ -136,6 +137,15 @@ export interface CompileOptions {
|
|
|
136
137
|
* @default 10
|
|
137
138
|
*/
|
|
138
139
|
maxPasses?: number;
|
|
140
|
+
/**
|
|
141
|
+
* Tidy the JSX the passes write: drop empty braces, write strings in
|
|
142
|
+
* braces as plain JSX, and unwrap fragments that hold one element. It only
|
|
143
|
+
* changes how the output reads: Solid's JSX transform lowers it to the same
|
|
144
|
+
* code. It runs only on a module the passes changed.
|
|
145
|
+
*
|
|
146
|
+
* @default true
|
|
147
|
+
*/
|
|
148
|
+
tidy?: boolean;
|
|
139
149
|
/**
|
|
140
150
|
* Generate a source map.
|
|
141
151
|
*
|
|
@@ -202,7 +212,7 @@ type Pass = (context: PassContext) => boolean;
|
|
|
202
212
|
|
|
203
213
|
interface PassResult {
|
|
204
214
|
code: string;
|
|
205
|
-
map:
|
|
215
|
+
map: DecodedSourceMap | undefined;
|
|
206
216
|
}
|
|
207
217
|
|
|
208
218
|
/** A parse of some code, which passes that change nothing can share. */
|
|
@@ -230,7 +240,7 @@ function runPass(
|
|
|
230
240
|
const context: PassContext = {
|
|
231
241
|
code,
|
|
232
242
|
filename,
|
|
233
|
-
s: new
|
|
243
|
+
s: new Editor(code, sourceMap),
|
|
234
244
|
program: parsed.program,
|
|
235
245
|
parents: parsed.parents,
|
|
236
246
|
// A pass adds the names it gives out, so each pass starts from the parse's own.
|
|
@@ -243,9 +253,7 @@ function runPass(
|
|
|
243
253
|
}
|
|
244
254
|
return {
|
|
245
255
|
code: context.s.toString(),
|
|
246
|
-
map: sourceMap
|
|
247
|
-
? context.s.generateMap({ source: filename, hires: true, includeContent: true }).toString()
|
|
248
|
-
: undefined,
|
|
256
|
+
map: sourceMap ? context.s.generateMap(filename) : undefined,
|
|
249
257
|
};
|
|
250
258
|
}
|
|
251
259
|
|
|
@@ -278,7 +286,7 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
278
286
|
passes.push(inlineMemos);
|
|
279
287
|
}
|
|
280
288
|
|
|
281
|
-
const maps:
|
|
289
|
+
const maps: DecodedSourceMap[] = [];
|
|
282
290
|
let current = code;
|
|
283
291
|
// A pass that changes nothing leaves the code as it was, so the next pass
|
|
284
292
|
// reuses the parse instead of parsing the same code again.
|
|
@@ -366,6 +374,19 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
366
374
|
maps.push(result.map);
|
|
367
375
|
}
|
|
368
376
|
}
|
|
377
|
+
// The passes leave empty braces, strings in braces and one-element
|
|
378
|
+
// fragments behind. Tidying them changes nothing Solid's JSX transform
|
|
379
|
+
// makes, but the output reads like code a person wrote.
|
|
380
|
+
const tidied =
|
|
381
|
+
(options.tidy ?? true)
|
|
382
|
+
? runPass(parsedCurrent(), filename, resolved, sourceMap, tidyJSX, undefined)
|
|
383
|
+
: undefined;
|
|
384
|
+
if (tidied) {
|
|
385
|
+
current = tidied.code;
|
|
386
|
+
if (tidied.map !== undefined) {
|
|
387
|
+
maps.push(tidied.map);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
369
390
|
}
|
|
370
391
|
|
|
371
392
|
const report = skipped ? { skipped } : {};
|
package/src/conditional.ts
CHANGED
|
@@ -24,6 +24,8 @@ import type { PassContext } from './context';
|
|
|
24
24
|
import { solidExport } from './context';
|
|
25
25
|
import { findAttribute } from './jsx';
|
|
26
26
|
import { lookup, scopeAt } from './scope';
|
|
27
|
+
import type { Content, Text } from './text';
|
|
28
|
+
import { join, joinText } from './text';
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
31
|
* How a lowered `<Show>` or `<Switch>` is written where it stands.
|
|
@@ -55,7 +57,7 @@ export function lowerConditionals(context: PassContext): boolean {
|
|
|
55
57
|
context.s.overwrite(
|
|
56
58
|
node.start,
|
|
57
59
|
node.end,
|
|
58
|
-
form === 'container' ? `{${ternary}}` : `<>{${ternary}}</>`,
|
|
60
|
+
form === 'container' ? joinText`{${ternary}}` : joinText`<>{${ternary}}</>`,
|
|
59
61
|
);
|
|
60
62
|
changed = true;
|
|
61
63
|
return;
|
|
@@ -86,14 +88,14 @@ function builtInOf(context: PassContext, element: JSXElement): string | undefine
|
|
|
86
88
|
* The ternary a `<Show>` or `<Switch>` lowers to, or `undefined` when its
|
|
87
89
|
* props or children need the component.
|
|
88
90
|
*/
|
|
89
|
-
function ternaryOf(context: PassContext, element: JSXElement):
|
|
91
|
+
function ternaryOf(context: PassContext, element: JSXElement): Text | undefined {
|
|
90
92
|
switch (builtInOf(context, element)) {
|
|
91
93
|
case 'Show': {
|
|
92
94
|
const branch = branchOf(context, element, ['when', 'fallback']);
|
|
93
95
|
if (branch === undefined) {
|
|
94
96
|
return undefined;
|
|
95
97
|
}
|
|
96
|
-
return `${branch.when} ? ${branch.children} : ${branch.fallback ?? 'undefined'}`;
|
|
98
|
+
return joinText`${branch.when} ? ${branch.children} : ${branch.fallback ?? 'undefined'}`;
|
|
97
99
|
}
|
|
98
100
|
case 'Switch':
|
|
99
101
|
return switchTernary(context, element);
|
|
@@ -105,12 +107,12 @@ function ternaryOf(context: PassContext, element: JSXElement): string | undefine
|
|
|
105
107
|
/**
|
|
106
108
|
* A `<Switch>` whose children are all `<Match>`es that lower, as a chain of ternaries.
|
|
107
109
|
*/
|
|
108
|
-
function switchTernary(context: PassContext, element: JSXElement):
|
|
110
|
+
function switchTernary(context: PassContext, element: JSXElement): Text | undefined {
|
|
109
111
|
const props = propsOf(context, element, ['fallback']);
|
|
110
112
|
if (props === undefined) {
|
|
111
113
|
return undefined;
|
|
112
114
|
}
|
|
113
|
-
|
|
115
|
+
const chain: Content[] = [];
|
|
114
116
|
let count = 0;
|
|
115
117
|
for (const child of significantChildren(element.children)) {
|
|
116
118
|
if (child.type !== 'JSXElement' || builtInOf(context, child) !== 'Match') {
|
|
@@ -120,14 +122,14 @@ function switchTernary(context: PassContext, element: JSXElement): string | unde
|
|
|
120
122
|
if (branch === undefined) {
|
|
121
123
|
return undefined;
|
|
122
124
|
}
|
|
123
|
-
chain
|
|
125
|
+
chain.push(branch.when, ' ? ', branch.children, ' : ');
|
|
124
126
|
count += 1;
|
|
125
127
|
}
|
|
126
128
|
// A `<Switch>` without a `<Match>` renders its fallback, which the fold pass resolves.
|
|
127
129
|
if (count === 0) {
|
|
128
130
|
return undefined;
|
|
129
131
|
}
|
|
130
|
-
return
|
|
132
|
+
return join([...chain, props.get('fallback') ?? 'undefined']);
|
|
131
133
|
}
|
|
132
134
|
|
|
133
135
|
/**
|
|
@@ -196,8 +198,8 @@ function propsOf(
|
|
|
196
198
|
context: PassContext,
|
|
197
199
|
element: JSXElement,
|
|
198
200
|
names: readonly string[],
|
|
199
|
-
): Map<string,
|
|
200
|
-
const props = new Map<string,
|
|
201
|
+
): Map<string, Text> | undefined {
|
|
202
|
+
const props = new Map<string, Text>();
|
|
201
203
|
for (const attribute of element.openingElement.attributes) {
|
|
202
204
|
if (
|
|
203
205
|
attribute.type !== 'JSXAttribute' ||
|
|
@@ -217,9 +219,9 @@ function propsOf(
|
|
|
217
219
|
}
|
|
218
220
|
|
|
219
221
|
interface Branch {
|
|
220
|
-
readonly when:
|
|
221
|
-
readonly children:
|
|
222
|
-
readonly fallback:
|
|
222
|
+
readonly when: Text;
|
|
223
|
+
readonly children: Content;
|
|
224
|
+
readonly fallback: Text | undefined;
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
/**
|
|
@@ -278,7 +280,7 @@ function branchOf(
|
|
|
278
280
|
: '';
|
|
279
281
|
return {
|
|
280
282
|
when,
|
|
281
|
-
children: significant.length > 0 ? `<>${inner}</>` : 'undefined',
|
|
283
|
+
children: significant.length > 0 ? joinText`<>${inner}</>` : 'undefined',
|
|
282
284
|
fallback: props.get('fallback'),
|
|
283
285
|
};
|
|
284
286
|
}
|
|
@@ -291,7 +293,7 @@ function callbackBody(
|
|
|
291
293
|
context: PassContext,
|
|
292
294
|
element: JSXElement,
|
|
293
295
|
callback: ArrowFunctionExpression,
|
|
294
|
-
):
|
|
296
|
+
): Text | undefined {
|
|
295
297
|
// `<Show>` only passes the value to a callback that takes a parameter, and
|
|
296
298
|
// calls it untracked. A body that is only JSX reads nothing as it is made.
|
|
297
299
|
const param = callback.params.at(0);
|
|
@@ -317,7 +319,7 @@ function callbackBody(
|
|
|
317
319
|
return undefined;
|
|
318
320
|
}
|
|
319
321
|
const when = value.expression;
|
|
320
|
-
const whenText = `(${context.s.slice(when.start, when.end)})`;
|
|
322
|
+
const whenText = joinText`(${context.s.slice(when.start, when.end)})`;
|
|
321
323
|
|
|
322
324
|
// Each name `when` reads has to mean the same thing where the body reads the parameter.
|
|
323
325
|
const names: Node[] = [];
|
|
@@ -327,7 +329,7 @@ function callbackBody(
|
|
|
327
329
|
if (binding?.identifier !== param || binding.mutated || namesInTypes(body, param.name)) {
|
|
328
330
|
return undefined;
|
|
329
331
|
}
|
|
330
|
-
const edits: { start: number; end: number; text:
|
|
332
|
+
const edits: { start: number; end: number; text: Text }[] = [];
|
|
331
333
|
for (const reference of binding.references) {
|
|
332
334
|
// A tag like `<C />` names a component, which an expression cannot replace.
|
|
333
335
|
if (reference.type !== 'Identifier') {
|
|
@@ -353,19 +355,24 @@ function callbackBody(
|
|
|
353
355
|
} else if (parent?.type === 'Property' && parent.shorthand) {
|
|
354
356
|
return undefined;
|
|
355
357
|
} else {
|
|
356
|
-
edits.push({
|
|
358
|
+
edits.push({
|
|
359
|
+
start: reference.start,
|
|
360
|
+
end: reference.end,
|
|
361
|
+
text: joinText`(() => ${whenText})`,
|
|
362
|
+
});
|
|
357
363
|
}
|
|
358
364
|
}
|
|
359
365
|
|
|
360
366
|
// Write the body with the edits, in order.
|
|
361
367
|
edits.sort((a, b) => a.start - b.start);
|
|
362
|
-
|
|
368
|
+
const parts: Text[] = [];
|
|
363
369
|
let at = body.start;
|
|
364
370
|
for (const edit of edits) {
|
|
365
|
-
|
|
371
|
+
parts.push(context.s.slice(at, edit.start), edit.text);
|
|
366
372
|
at = edit.end;
|
|
367
373
|
}
|
|
368
|
-
|
|
374
|
+
parts.push(context.s.slice(at, body.end));
|
|
375
|
+
return join(parts);
|
|
369
376
|
}
|
|
370
377
|
|
|
371
378
|
/**
|
|
@@ -518,7 +525,7 @@ function significantChildren(children: readonly JSXChild[]): JSXChild[] {
|
|
|
518
525
|
* The text of an attribute's value as an expression, or `undefined` when the
|
|
519
526
|
* value is not supported.
|
|
520
527
|
*/
|
|
521
|
-
function expressionText(context: PassContext, attribute: JSXAttribute):
|
|
528
|
+
function expressionText(context: PassContext, attribute: JSXAttribute): Text | undefined {
|
|
522
529
|
const { value } = attribute;
|
|
523
530
|
if (value === null) {
|
|
524
531
|
// A bare attribute is `true`. The fold pass resolves those.
|
|
@@ -529,7 +536,7 @@ function expressionText(context: PassContext, attribute: JSXAttribute): string |
|
|
|
529
536
|
if (value.expression.type === 'JSXEmptyExpression') {
|
|
530
537
|
return undefined;
|
|
531
538
|
}
|
|
532
|
-
return `(${context.s.slice(value.expression.start, value.expression.end)})`;
|
|
539
|
+
return joinText`(${context.s.slice(value.expression.start, value.expression.end)})`;
|
|
533
540
|
case 'JSXElement':
|
|
534
541
|
case 'JSXFragment':
|
|
535
542
|
return context.s.slice(value.start, value.end);
|
package/src/context.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { Editor, Text } from './text';
|
|
2
2
|
import type { Node, Program } from 'oxc-parser';
|
|
3
3
|
import type { Parents } from './ast';
|
|
4
4
|
import type { ResolvedImportedConstants } from './constants';
|
|
@@ -92,7 +92,7 @@ export interface PassContext {
|
|
|
92
92
|
readonly code: string;
|
|
93
93
|
/** The file name, which picks the parser. */
|
|
94
94
|
readonly filename: string;
|
|
95
|
-
readonly s:
|
|
95
|
+
readonly s: Editor;
|
|
96
96
|
readonly program: Program;
|
|
97
97
|
readonly parents: Parents;
|
|
98
98
|
readonly scopes: ScopeAnalysis;
|
|
@@ -116,6 +116,6 @@ export function recordSkip(
|
|
|
116
116
|
/**
|
|
117
117
|
* The current text of `node`, including the edits made inside it so far.
|
|
118
118
|
*/
|
|
119
|
-
export function getText(context: PassContext, node: Node):
|
|
119
|
+
export function getText(context: PassContext, node: Node): Text {
|
|
120
120
|
return context.s.slice(node.start, node.end);
|
|
121
121
|
}
|
package/src/copy-size.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* components it calls, as the passes after inlining would. Then it estimates
|
|
4
4
|
* its size with `SizeEstimator`.
|
|
5
5
|
*/
|
|
6
|
-
import
|
|
6
|
+
import type { Content } from './text';
|
|
7
|
+
import { Editor, getCode, join } from './text';
|
|
7
8
|
import { collectParents, parse } from './ast';
|
|
8
9
|
import type { PassContext, ResolvedOptions } from './context';
|
|
9
10
|
import { fold } from './fold';
|
|
@@ -33,9 +34,9 @@ export interface CalledComponents {
|
|
|
33
34
|
|
|
34
35
|
export interface Copy {
|
|
35
36
|
/** The statements the copy hoists out of the JSX. */
|
|
36
|
-
readonly statements: readonly
|
|
37
|
+
readonly statements: readonly Content[];
|
|
37
38
|
/** The JSX that replaces the call site. */
|
|
38
|
-
readonly root:
|
|
39
|
+
readonly root: Content;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/**
|
|
@@ -56,14 +57,14 @@ export function copySize(
|
|
|
56
57
|
measured?: { code: string },
|
|
57
58
|
): number {
|
|
58
59
|
const calls = depth < MAX_DEPTH ? called : undefined;
|
|
59
|
-
let code = `${header}\n${calls?.declarations ?? ''}\nfunction ${WRAPPER}() {\n${copy.statements
|
|
60
|
+
let code = `${header}\n${calls?.declarations ?? ''}\nfunction ${WRAPPER}() {\n${join(copy.statements, '\n').code}\nreturn (${getCode(copy.root)});\n}\n`;
|
|
60
61
|
// Constant props fold the copy's conditions, and can let the calls in it
|
|
61
62
|
// inline, as later passes would.
|
|
62
63
|
for (let round = 0; round < 4; round += 1) {
|
|
63
64
|
let changed = false;
|
|
64
65
|
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
65
66
|
const program = parse(filename, code);
|
|
66
|
-
const s = new
|
|
67
|
+
const s = new Editor(code, false);
|
|
67
68
|
depth += 1;
|
|
68
69
|
let passChanged: boolean;
|
|
69
70
|
try {
|