solid-optimizer 1.0.0-next.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "1.0.0-next.0",
3
+ "version": "1.0.0-next.2",
4
4
  "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
@@ -40,13 +40,13 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@jridgewell/trace-mapping": "^0.3.31",
43
- "@solidjs/compiler": "2.0.0-rc.16",
43
+ "@solidjs/compiler": "2.0.0-rc.21",
44
44
  "@solidjs/vite-plugin": "3.0.0-next.50",
45
- "@solidjs/web": "2.0.0-rc.16",
45
+ "@solidjs/web": "2.0.0-rc.21",
46
46
  "@types/node": "^26.6.3",
47
47
  "happy-dom": "^20.14.5",
48
48
  "publint": "^0.3.24",
49
- "solid-js": "2.0.0-rc.16",
49
+ "solid-js": "2.0.0-rc.21",
50
50
  "tsdown": "^0.23.0",
51
51
  "typescript": "^7.0.2",
52
52
  "vite": "^8.3.1",
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
 
@@ -324,8 +331,9 @@ export function readModuleConstants(
324
331
  options: Pick<CompileOptions, 'filename' | 'importedConstants' | 'globalConstants'> = {},
325
332
  ): ModuleConstants {
326
333
  const program = parse(options.filename ?? 'input.jsx', code);
334
+ const scopes = analyzeScopes(program);
327
335
  return {
328
- imports: namedImportSources(program),
329
- exports: constantExports(program, resolveOptions(options)),
336
+ imports: namedImportSources(program, scopes),
337
+ exports: constantExports(program, resolveOptions(options), scopes),
330
338
  };
331
339
  }
package/src/constants.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * from `importedConstants`, which a caller fills with `constantExports` of
6
6
  * each imported module.
7
7
  */
8
- import type { MemberExpression, Node, Program } from 'oxc-parser';
8
+ import type { ImportDeclaration, MemberExpression, Node, Program } from 'oxc-parser';
9
9
  import type { ResolvedOptions } from './context';
10
10
  import { isConstantDeclaration } from './context';
11
11
  import type { Binding, ScopeAnalysis } from './scope';
@@ -171,17 +171,19 @@ export class ConstantTable {
171
171
  /**
172
172
  * The modules `program` imports or re-exports bindings from. Their
173
173
  * constant exports are what `importedConstants` can hold.
174
+ *
175
+ * An import counts only when one of its bindings is read as a value. A
176
+ * binding read only in types has no constant to fold, and TypeScript drops its
177
+ * import, so the module it names may not even build for this target.
174
178
  */
175
- export function namedImportSources(program: Program): string[] {
179
+ export function namedImportSources(
180
+ program: Program,
181
+ scopes: ScopeAnalysis = analyzeScopes(program),
182
+ ): string[] {
176
183
  const sources = new Set<string>();
177
184
  for (const statement of program.body) {
178
185
  if (statement.type === 'ImportDeclaration') {
179
- if (
180
- statement.importKind !== 'type' &&
181
- statement.specifiers.some(
182
- (specifier) => !(specifier.type === 'ImportSpecifier' && specifier.importKind === 'type'),
183
- )
184
- ) {
186
+ if (statement.importKind !== 'type' && readsAsValue(statement.specifiers, scopes)) {
185
187
  sources.add(statement.source.value);
186
188
  }
187
189
  } else if (
@@ -196,6 +198,23 @@ export function namedImportSources(program: Program): string[] {
196
198
  return [...sources];
197
199
  }
198
200
 
201
+ /**
202
+ * Whether the module reads one of these import bindings as a value. Scope
203
+ * analysis skips type positions, so a binding used only in types has no
204
+ * references.
205
+ */
206
+ function readsAsValue(specifiers: ImportDeclaration['specifiers'], scopes: ScopeAnalysis): boolean {
207
+ for (const specifier of specifiers) {
208
+ if (specifier.type === 'ImportSpecifier' && specifier.importKind === 'type') {
209
+ continue;
210
+ }
211
+ if (scopes.root.bindings.get(specifier.local.name)?.references.length) {
212
+ return true;
213
+ }
214
+ }
215
+ return false;
216
+ }
217
+
199
218
  function exportName(node: Node): string | undefined {
200
219
  if (node.type === 'Identifier') {
201
220
  return node.name;
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 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
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
- /** The name of the function a copy is wrapped in to be folded and measured. */
13
- const WRAPPER = '__so_size__';
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. `inTemplate` is whether the call site sits directly in
26
- * an element.
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
- 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
- });
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
- code = s.toString();
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
- private foldConstant(node: Node): void {
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
  }