solid-optimizer 1.0.0-next.1 → 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-BpoiBa6b.mjs → compile-sCZdwEKG.mjs} +283 -41
- 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 +1 -1
- package/src/compile.ts +7 -0
- 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-BpoiBa6b.mjs.map +0 -1
package/package.json
CHANGED
package/src/compile.ts
CHANGED
|
@@ -111,6 +111,12 @@ export interface CompileOptions {
|
|
|
111
111
|
* @default false
|
|
112
112
|
*/
|
|
113
113
|
alwaysInline?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Top-level components to inline wherever they can be, like
|
|
116
|
+
* `alwaysInline` does for every component. The Vite plugin lists the
|
|
117
|
+
* copies of components from `optimizer.packages` here.
|
|
118
|
+
*/
|
|
119
|
+
alwaysInlineComponents?: readonly string[];
|
|
114
120
|
/**
|
|
115
121
|
* The most passes to run. One pass can expose work for the next, such as
|
|
116
122
|
* an inlined component whose props now fold. Compilation stops early once
|
|
@@ -176,6 +182,7 @@ function resolveOptions(options: CompileOptions): ResolvedOptions {
|
|
|
176
182
|
),
|
|
177
183
|
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
178
184
|
alwaysInline: options.alwaysInline ?? false,
|
|
185
|
+
alwaysInlineComponents: new Set(options.alwaysInlineComponents ?? []),
|
|
179
186
|
};
|
|
180
187
|
}
|
|
181
188
|
|
package/src/context.ts
CHANGED
|
@@ -24,6 +24,8 @@ export interface ResolvedOptions {
|
|
|
24
24
|
readonly sharedComponents: ReadonlyMap<string, number>;
|
|
25
25
|
/** Whether to inline components even where the copies are larger. */
|
|
26
26
|
readonly alwaysInline: boolean;
|
|
27
|
+
/** Components to inline wherever they can be, whatever their size. */
|
|
28
|
+
readonly alwaysInlineComponents: ReadonlySet<string>;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/**
|
package/src/copy-size.ts
CHANGED
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Measures a component's copy at a call site: folds it
|
|
3
|
-
*
|
|
2
|
+
* Measures a component's copy at a call site: folds it, and inlines the
|
|
3
|
+
* components it calls, as the passes after inlining would. Then it estimates
|
|
4
|
+
* its size with `SizeEstimator`.
|
|
4
5
|
*/
|
|
5
6
|
import MagicString from 'magic-string';
|
|
6
7
|
import { collectParents, parse } from './ast';
|
|
7
|
-
import type { ResolvedOptions } from './context';
|
|
8
|
+
import type { PassContext, ResolvedOptions } from './context';
|
|
8
9
|
import { fold } from './fold';
|
|
9
10
|
import { analyzeScopes } from './scope';
|
|
10
11
|
import { SizeEstimator } from './size';
|
|
11
12
|
|
|
12
|
-
/**
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The name of the function a copy is wrapped in to be folded and measured.
|
|
15
|
+
* It is capitalized, so the inline pass can move statements into it.
|
|
16
|
+
*/
|
|
17
|
+
const WRAPPER = 'SoSize$$';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How many copies are being measured inside each other. Components that
|
|
21
|
+
* call each other would otherwise measure each other without end.
|
|
22
|
+
*/
|
|
23
|
+
let depth = 0;
|
|
24
|
+
const MAX_DEPTH = 3;
|
|
25
|
+
|
|
26
|
+
/** The module's components a copy calls, which can inline into it. */
|
|
27
|
+
export interface CalledComponents {
|
|
28
|
+
/** Their declarations. */
|
|
29
|
+
readonly declarations: string;
|
|
30
|
+
/** The inline pass. It is passed in, since it measures copies with this module. */
|
|
31
|
+
readonly inline: (context: PassContext) => boolean;
|
|
32
|
+
}
|
|
14
33
|
|
|
15
34
|
export interface Copy {
|
|
16
35
|
/** The statements the copy hoists out of the JSX. */
|
|
@@ -22,8 +41,10 @@ export interface Copy {
|
|
|
22
41
|
/**
|
|
23
42
|
* The size of a component's copy at one call site, once its props have
|
|
24
43
|
* folded. `header` declares the names the fold pass needs to know, like
|
|
25
|
-
* imports of Solid. `
|
|
26
|
-
*
|
|
44
|
+
* imports of Solid. `called` holds the module's components the copy calls,
|
|
45
|
+
* which inline into it when its props resolve their call sites.
|
|
46
|
+
* `inTemplate` is whether the call site sits directly in an element.
|
|
47
|
+
* `measured` receives the code that was measured.
|
|
27
48
|
*/
|
|
28
49
|
export function copySize(
|
|
29
50
|
filename: string,
|
|
@@ -31,27 +52,46 @@ export function copySize(
|
|
|
31
52
|
copy: Copy,
|
|
32
53
|
inTemplate: boolean,
|
|
33
54
|
options: ResolvedOptions,
|
|
55
|
+
called?: CalledComponents,
|
|
56
|
+
measured?: { code: string },
|
|
34
57
|
): number {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
filename,
|
|
43
|
-
s
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
const calls = depth < MAX_DEPTH ? called : undefined;
|
|
59
|
+
let code = `${header}\n${calls?.declarations ?? ''}\nfunction ${WRAPPER}() {\n${copy.statements.join('\n')}\nreturn (${copy.root});\n}\n`;
|
|
60
|
+
// Constant props fold the copy's conditions, and can let the calls in it
|
|
61
|
+
// inline, as later passes would.
|
|
62
|
+
for (let round = 0; round < 4; round += 1) {
|
|
63
|
+
let changed = false;
|
|
64
|
+
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
65
|
+
const program = parse(filename, code);
|
|
66
|
+
const s = new MagicString(code);
|
|
67
|
+
depth += 1;
|
|
68
|
+
let passChanged: boolean;
|
|
69
|
+
try {
|
|
70
|
+
passChanged = pass({
|
|
71
|
+
code,
|
|
72
|
+
filename,
|
|
73
|
+
s,
|
|
74
|
+
program,
|
|
75
|
+
parents: collectParents(program),
|
|
76
|
+
scopes: analyzeScopes(program),
|
|
77
|
+
options,
|
|
78
|
+
// Only the copy is measured, so nothing it skips is reported.
|
|
79
|
+
skips: undefined,
|
|
80
|
+
});
|
|
81
|
+
} finally {
|
|
82
|
+
depth -= 1;
|
|
83
|
+
}
|
|
84
|
+
if (passChanged) {
|
|
85
|
+
code = s.toString();
|
|
86
|
+
changed = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
51
89
|
if (!changed) {
|
|
52
90
|
break;
|
|
53
91
|
}
|
|
54
|
-
|
|
92
|
+
}
|
|
93
|
+
if (measured) {
|
|
94
|
+
measured.code = code;
|
|
55
95
|
}
|
|
56
96
|
const program = parse(filename, code);
|
|
57
97
|
const wrapper = program.body.at(-1);
|
package/src/fold.ts
CHANGED
|
@@ -80,7 +80,7 @@ import {
|
|
|
80
80
|
import type { Binding } from './scope';
|
|
81
81
|
import { lookup, scopeAt } from './scope';
|
|
82
82
|
import { SizeEstimator, compactLength } from './size';
|
|
83
|
-
import type { ConstantLookup } from './value';
|
|
83
|
+
import type { Const, ConstantLookup } from './value';
|
|
84
84
|
import {
|
|
85
85
|
arrayLiteralLength,
|
|
86
86
|
effectfulParts,
|
|
@@ -351,6 +351,7 @@ class Folder {
|
|
|
351
351
|
break;
|
|
352
352
|
case 'CallExpression':
|
|
353
353
|
this.annotatePure(node);
|
|
354
|
+
this.foldUntrack(node);
|
|
354
355
|
break;
|
|
355
356
|
default:
|
|
356
357
|
break;
|
|
@@ -400,11 +401,39 @@ class Folder {
|
|
|
400
401
|
this.changed = true;
|
|
401
402
|
}
|
|
402
403
|
|
|
403
|
-
|
|
404
|
+
/**
|
|
405
|
+
* `untrack(() => value)` is the value, when the value is a constant.
|
|
406
|
+
* Nothing is read, so there is nothing to untrack. A component inlined
|
|
407
|
+
* with a literal prop leaves this, like Kobalte's `untrack(() => props.as)`.
|
|
408
|
+
*/
|
|
409
|
+
private foldUntrack(call: CallExpression): void {
|
|
410
|
+
if (call.callee.type !== 'Identifier' || call.arguments.length !== 1) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const binding = this.context.scopes.references.get(call.callee);
|
|
414
|
+
const identity = binding ? solidExport(binding, this.context.options) : undefined;
|
|
415
|
+
const callback = call.arguments.at(0);
|
|
416
|
+
if (
|
|
417
|
+
identity !== 'untrack' ||
|
|
418
|
+
callback?.type !== 'ArrowFunctionExpression' ||
|
|
419
|
+
callback.async ||
|
|
420
|
+
callback.params.length > 0 ||
|
|
421
|
+
!callback.expression ||
|
|
422
|
+
callback.body.type === 'BlockStatement'
|
|
423
|
+
) {
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const value = evaluate(callback.body, this.lookup);
|
|
427
|
+
if (value) {
|
|
428
|
+
this.foldConstant(call, value);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private foldConstant(node: Node, known?: Const): void {
|
|
404
433
|
if (isLiteralForm(node)) {
|
|
405
434
|
return;
|
|
406
435
|
}
|
|
407
|
-
const value = evaluate(node, this.lookup);
|
|
436
|
+
const value = known ?? evaluate(node, this.lookup);
|
|
408
437
|
if (!value) {
|
|
409
438
|
return;
|
|
410
439
|
}
|