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/src/copy-size.ts CHANGED
@@ -1,29 +1,51 @@
1
1
  /**
2
- * Measures a component's copy at a call site: folds it as the passes after
3
- * inlining would, then estimates its size with `SizeEstimator`.
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 MagicString from 'magic-string';
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
- /** The name of the function a copy is wrapped in to be folded and measured. */
13
- const WRAPPER = '__so_size__';
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 string[];
37
+ readonly statements: readonly Content[];
18
38
  /** The JSX that replaces the call site. */
19
- readonly root: string;
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. `inTemplate` is whether the call site sits directly in
26
- * an element.
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
- let code = `${header}\nfunction ${WRAPPER}() {\n${copy.statements.join('\n')}\nreturn (${copy.root});\n}\n`;
36
- // Constant props fold the copy's conditions, as later passes would.
37
- for (let round = 0; round < 3; round += 1) {
38
- const program = parse(filename, code);
39
- const s = new MagicString(code);
40
- const changed = fold({
41
- code,
42
- filename,
43
- s,
44
- program,
45
- parents: collectParents(program),
46
- scopes: analyzeScopes(program),
47
- options,
48
- // Only the copy is measured, so nothing it skips is reported.
49
- skips: undefined,
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
- code = s.toString();
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);