solid-optimizer 1.0.0-next.1 → 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-BpoiBa6b.mjs → compile-Cvjy2e11.mjs} +1910 -387
- 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 +5 -2
- package/src/ast.ts +4 -3
- package/src/compile.ts +93 -14
- package/src/conditional.ts +547 -0
- package/src/constants.ts +31 -5
- 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-BpoiBa6b.mjs.map +0 -1
package/src/copy-size.ts
CHANGED
|
@@ -1,29 +1,51 @@
|
|
|
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
|
-
import
|
|
6
|
+
import type { Content } from './text';
|
|
7
|
+
import { Editor, getCode, join } from './text';
|
|
6
8
|
import { collectParents, parse } from './ast';
|
|
7
|
-
import type { ResolvedOptions } from './context';
|
|
9
|
+
import type { PassContext, ResolvedOptions } from './context';
|
|
8
10
|
import { fold } from './fold';
|
|
9
11
|
import { analyzeScopes } from './scope';
|
|
10
12
|
import { SizeEstimator } from './size';
|
|
11
13
|
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* The name of the function a copy is wrapped in to be folded and measured.
|
|
16
|
+
* It is capitalized, so the inline pass can move statements into it.
|
|
17
|
+
*/
|
|
18
|
+
const WRAPPER = 'SoSize$$';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* How many copies are being measured inside each other. Components that
|
|
22
|
+
* call each other would otherwise measure each other without end.
|
|
23
|
+
*/
|
|
24
|
+
let depth = 0;
|
|
25
|
+
const MAX_DEPTH = 3;
|
|
26
|
+
|
|
27
|
+
/** The module's components a copy calls, which can inline into it. */
|
|
28
|
+
export interface CalledComponents {
|
|
29
|
+
/** Their declarations. */
|
|
30
|
+
readonly declarations: string;
|
|
31
|
+
/** The inline pass. It is passed in, since it measures copies with this module. */
|
|
32
|
+
readonly inline: (context: PassContext) => boolean;
|
|
33
|
+
}
|
|
14
34
|
|
|
15
35
|
export interface Copy {
|
|
16
36
|
/** The statements the copy hoists out of the JSX. */
|
|
17
|
-
readonly statements: readonly
|
|
37
|
+
readonly statements: readonly Content[];
|
|
18
38
|
/** The JSX that replaces the call site. */
|
|
19
|
-
readonly root:
|
|
39
|
+
readonly root: Content;
|
|
20
40
|
}
|
|
21
41
|
|
|
22
42
|
/**
|
|
23
43
|
* The size of a component's copy at one call site, once its props have
|
|
24
44
|
* folded. `header` declares the names the fold pass needs to know, like
|
|
25
|
-
* imports of Solid. `
|
|
26
|
-
*
|
|
45
|
+
* imports of Solid. `called` holds the module's components the copy calls,
|
|
46
|
+
* which inline into it when its props resolve their call sites.
|
|
47
|
+
* `inTemplate` is whether the call site sits directly in an element.
|
|
48
|
+
* `measured` receives the code that was measured.
|
|
27
49
|
*/
|
|
28
50
|
export function copySize(
|
|
29
51
|
filename: string,
|
|
@@ -31,27 +53,46 @@ export function copySize(
|
|
|
31
53
|
copy: Copy,
|
|
32
54
|
inTemplate: boolean,
|
|
33
55
|
options: ResolvedOptions,
|
|
56
|
+
called?: CalledComponents,
|
|
57
|
+
measured?: { code: string },
|
|
34
58
|
): number {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
filename,
|
|
43
|
-
s,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
59
|
+
const calls = depth < MAX_DEPTH ? called : undefined;
|
|
60
|
+
let code = `${header}\n${calls?.declarations ?? ''}\nfunction ${WRAPPER}() {\n${join(copy.statements, '\n').code}\nreturn (${getCode(copy.root)});\n}\n`;
|
|
61
|
+
// Constant props fold the copy's conditions, and can let the calls in it
|
|
62
|
+
// inline, as later passes would.
|
|
63
|
+
for (let round = 0; round < 4; round += 1) {
|
|
64
|
+
let changed = false;
|
|
65
|
+
for (const pass of calls ? [fold, calls.inline] : [fold]) {
|
|
66
|
+
const program = parse(filename, code);
|
|
67
|
+
const s = new Editor(code, false);
|
|
68
|
+
depth += 1;
|
|
69
|
+
let passChanged: boolean;
|
|
70
|
+
try {
|
|
71
|
+
passChanged = pass({
|
|
72
|
+
code,
|
|
73
|
+
filename,
|
|
74
|
+
s,
|
|
75
|
+
program,
|
|
76
|
+
parents: collectParents(program),
|
|
77
|
+
scopes: analyzeScopes(program),
|
|
78
|
+
options,
|
|
79
|
+
// Only the copy is measured, so nothing it skips is reported.
|
|
80
|
+
skips: undefined,
|
|
81
|
+
});
|
|
82
|
+
} finally {
|
|
83
|
+
depth -= 1;
|
|
84
|
+
}
|
|
85
|
+
if (passChanged) {
|
|
86
|
+
code = s.toString();
|
|
87
|
+
changed = true;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
51
90
|
if (!changed) {
|
|
52
91
|
break;
|
|
53
92
|
}
|
|
54
|
-
|
|
93
|
+
}
|
|
94
|
+
if (measured) {
|
|
95
|
+
measured.code = code;
|
|
55
96
|
}
|
|
56
97
|
const program = parse(filename, code);
|
|
57
98
|
const wrapper = program.body.at(-1);
|