solid-optimizer 0.4.1 → 1.0.0-next.1

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,7 +1,7 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "0.4.1",
4
- "description": "Experimental compile-time optimizer for SolidJS 1.x",
3
+ "version": "1.0.0-next.1",
4
+ "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
7
7
  "jsx",
@@ -39,29 +39,28 @@
39
39
  "oxc-parser": "^0.151.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@babel/core": "^7.29.7",
43
42
  "@jridgewell/trace-mapping": "^0.3.31",
44
- "@types/babel__core": "^7.20.5",
43
+ "@solidjs/compiler": "2.0.0-rc.21",
44
+ "@solidjs/vite-plugin": "3.0.0-next.50",
45
+ "@solidjs/web": "2.0.0-rc.21",
45
46
  "@types/node": "^26.6.3",
46
- "babel-preset-solid": "^1.9.15",
47
47
  "happy-dom": "^20.14.5",
48
48
  "publint": "^0.3.24",
49
- "solid-js": "^1.9.15",
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",
53
- "vite-plugin-solid": "^2.11.14",
54
53
  "vitest": "^5.0.2"
55
54
  },
56
55
  "peerDependencies": {
57
- "vite": "^8.0.0 || ^9.0.0",
58
- "vite-plugin-solid": "^2.11.0"
56
+ "@solidjs/vite-plugin": "^3.0.0-next.46",
57
+ "vite": "^8.0.0 || ^9.0.0"
59
58
  },
60
59
  "peerDependenciesMeta": {
61
- "vite": {
60
+ "@solidjs/vite-plugin": {
62
61
  "optional": true
63
62
  },
64
- "vite-plugin-solid": {
63
+ "vite": {
65
64
  "optional": true
66
65
  }
67
66
  },
package/src/compile.ts CHANGED
@@ -10,7 +10,6 @@ import type { PassContext, ResolvedOptions, Skip } from './context';
10
10
  import { fold } from './fold';
11
11
  import { inline } from './inline';
12
12
  import { inlineMemos } from './memo';
13
- import { simplifyServer } from './server';
14
13
  import { removeProviders } from './provider';
15
14
  import type { ScopeAnalysis } from './scope';
16
15
  import { analyzeScopes } from './scope';
@@ -37,19 +36,6 @@ export interface CompileOptions {
37
36
  * @default true
38
37
  */
39
38
  inline?: boolean;
40
- /**
41
- * Compile for the server: remove `createEffect` and `onMount`, and reduce
42
- * `untrack`, `batch`, `startTransition`, `createDeferred`, `getListener`,
43
- * `createMemo`, `createRenderEffect`, and `createComputed` to what they do
44
- * on the server.
45
- *
46
- * This runs once, after the other passes. The server and client build
47
- * make the same folding and inlining decisions from the same code, so the
48
- * server renders the tree the client hydrates.
49
- *
50
- * @default false
51
- */
52
- server?: boolean;
53
39
  /**
54
40
  * Remove context providers whose reads are all visible, and give each
55
41
  * read the provider's value.
@@ -68,14 +54,14 @@ export interface CompileOptions {
68
54
  * The names of Solid's built-in components. A tag only folds when it is
69
55
  * one of these, and an empty list turns control-flow folding off.
70
56
  *
71
- * @default ['For', 'Show', 'Switch', 'Match', 'Suspense', 'SuspenseList', 'Portal', 'Index', 'Dynamic', 'ErrorBoundary']
57
+ * @default ['For', 'Show', 'Switch', 'Match', 'Loading', 'Reveal', 'Portal', 'Repeat', 'Dynamic', 'Errored']
72
58
  */
73
59
  builtIns?: readonly string[];
74
60
  /**
75
61
  * The modules that export Solid's built-in components. A tag imported from
76
62
  * anywhere else is a different component.
77
63
  *
78
- * @default ['solid-js', 'solid-js/web']
64
+ * @default ['solid-js', '@solidjs/web']
79
65
  */
80
66
  moduleSources?: readonly string[];
81
67
  /**
@@ -97,6 +83,12 @@ export interface CompileOptions {
97
83
  * like a local `const`. `constantExports` reads them from a module.
98
84
  */
99
85
  importedConstants?: ImportedConstants;
86
+ /**
87
+ * Values of global names and paths that fold like constants, such as the
88
+ * `define` entries of a bundler: `{ __DEV__: false, 'import.meta.env.MODE': 'production' }`.
89
+ * A path starts at `import.meta` or at a name the module does not declare.
90
+ */
91
+ globalConstants?: Readonly<Record<string, Primitive>>;
100
92
  /**
101
93
  * The exports of imported modules that are contexts made by Solid's
102
94
  * `createContext`, keyed by the import specifier as the code writes it.
@@ -158,15 +150,15 @@ const DEFAULT_BUILT_INS = [
158
150
  'Show',
159
151
  'Switch',
160
152
  'Match',
161
- 'Suspense',
162
- 'SuspenseList',
153
+ 'Loading',
154
+ 'Reveal',
163
155
  'Portal',
164
- 'Index',
156
+ 'Repeat',
165
157
  'Dynamic',
166
- 'ErrorBoundary',
158
+ 'Errored',
167
159
  ];
168
160
 
169
- const DEFAULT_MODULE_SOURCES = ['solid-js', 'solid-js/web'];
161
+ const DEFAULT_MODULE_SOURCES = ['solid-js', '@solidjs/web'];
170
162
 
171
163
  function resolveOptions(options: CompileOptions): ResolvedOptions {
172
164
  return {
@@ -175,6 +167,7 @@ function resolveOptions(options: CompileOptions): ResolvedOptions {
175
167
  builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
176
168
  constantVars: options.constantVars ?? false,
177
169
  importedConstants: resolveImportedConstants(options.importedConstants),
170
+ globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
178
171
  importedContexts: new Map(
179
172
  Object.entries(options.importedContexts ?? {}).map(([source, names]) => [
180
173
  source,
@@ -298,23 +291,6 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
298
291
  }
299
292
  }
300
293
 
301
- if (options.server ?? false) {
302
- const result = runPass(
303
- parsedCurrent(),
304
- filename,
305
- resolved,
306
- sourceMap,
307
- simplifyServer,
308
- undefined,
309
- );
310
- if (result) {
311
- current = result.code;
312
- if (result.map !== undefined) {
313
- maps.push(result.map);
314
- }
315
- }
316
- }
317
-
318
294
  const report = skipped ? { skipped } : {};
319
295
  if (maps.length === 0) {
320
296
  return { code: current, map: null, ...report };
@@ -341,11 +317,11 @@ export interface ModuleConstants {
341
317
  * Reads what a module imports and the constants it exports, so that
342
318
  * `compile` can fold them in the modules that import it.
343
319
  *
344
- * Only `filename` and `importedConstants` of the options are used.
320
+ * Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
345
321
  */
346
322
  export function readModuleConstants(
347
323
  code: string,
348
- options: Pick<CompileOptions, 'filename' | 'importedConstants'> = {},
324
+ options: Pick<CompileOptions, 'filename' | 'importedConstants' | 'globalConstants'> = {},
349
325
  ): ModuleConstants {
350
326
  const program = parse(options.filename ?? 'input.jsx', code);
351
327
  const scopes = analyzeScopes(program);
package/src/constants.ts CHANGED
@@ -60,16 +60,56 @@ export class ConstantTable {
60
60
  ) {
61
61
  this.lookup = (reference) => {
62
62
  if (reference.type === 'MemberExpression') {
63
- return this.namespaceMember(scopes, reference);
63
+ return this.namespaceMember(scopes, reference) ?? this.globalMember(scopes, reference);
64
64
  }
65
65
  if (!scopes.references.has(reference) || reference.type !== 'Identifier') {
66
66
  return undefined;
67
67
  }
68
68
  const binding = scopes.references.get(reference);
69
- return binding ? this.constantOf(binding) : globalValue(reference.name);
69
+ if (binding) {
70
+ return this.constantOf(binding);
71
+ }
72
+ return this.globalConstant(reference.name) ?? globalValue(reference.name);
70
73
  };
71
74
  }
72
75
 
76
+ private globalConstant(path: string): Const | undefined {
77
+ const { globalConstants } = this.options;
78
+ return globalConstants.has(path) ? { value: globalConstants.get(path) } : undefined;
79
+ }
80
+
81
+ /**
82
+ * The value of a path like `import.meta.env.MODE` or `process.env.X` that
83
+ * `globalConstants` holds. The path starts at `import.meta` or at a name
84
+ * nothing declares, and goes through plain property reads.
85
+ */
86
+ private globalMember(scopes: ScopeAnalysis, node: MemberExpression): Const | undefined {
87
+ if (this.options.globalConstants.size === 0) {
88
+ return undefined;
89
+ }
90
+ const names: string[] = [];
91
+ let current: Node = node;
92
+ while (current.type === 'MemberExpression') {
93
+ if (current.computed || current.optional || current.property.type !== 'Identifier') {
94
+ return undefined;
95
+ }
96
+ names.unshift(current.property.name);
97
+ current = current.object;
98
+ }
99
+ if (current.type === 'MetaProperty') {
100
+ names.unshift(`${current.meta.name}.${current.property.name}`);
101
+ } else if (
102
+ current.type === 'Identifier' &&
103
+ scopes.references.has(current) &&
104
+ scopes.references.get(current) === undefined
105
+ ) {
106
+ names.unshift(current.name);
107
+ } else {
108
+ return undefined;
109
+ }
110
+ return this.globalConstant(names.join('.'));
111
+ }
112
+
73
113
  /**
74
114
  * The value of `ns.NAME`, where `ns` is `import * as ns` from a module
75
115
  * whose constants are known.
package/src/context.ts CHANGED
@@ -3,6 +3,7 @@ import type { Node, Program } from 'oxc-parser';
3
3
  import type { Parents } from './ast';
4
4
  import type { ResolvedImportedConstants } from './constants';
5
5
  import type { Binding, ScopeAnalysis } from './scope';
6
+ import type { Primitive } from './value';
6
7
 
7
8
  export interface ResolvedOptions {
8
9
  /** The control-flow components a tag may resolve to. */
@@ -15,6 +16,8 @@ export interface ResolvedOptions {
15
16
  readonly constantVars: boolean;
16
17
  /** The constants imported modules export, by import specifier and export name. */
17
18
  readonly importedConstants: ResolvedImportedConstants;
19
+ /** Values of global names and paths, like `__DEV__` or `import.meta.env.MODE`. */
20
+ readonly globalConstants: ReadonlyMap<string, Primitive>;
18
21
  /** The exports of imported modules that are contexts, by import specifier. */
19
22
  readonly importedContexts: ReadonlyMap<string, ReadonlySet<string>>;
20
23
  /** Components other modules use too, by the number of modules that use them. */
@@ -0,0 +1,73 @@
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`.
4
+ */
5
+ import MagicString from 'magic-string';
6
+ import { collectParents, parse } from './ast';
7
+ import type { ResolvedOptions } from './context';
8
+ import { fold } from './fold';
9
+ import { analyzeScopes } from './scope';
10
+ import { SizeEstimator } from './size';
11
+
12
+ /** The name of the function a copy is wrapped in to be folded and measured. */
13
+ const WRAPPER = '__so_size__';
14
+
15
+ export interface Copy {
16
+ /** The statements the copy hoists out of the JSX. */
17
+ readonly statements: readonly string[];
18
+ /** The JSX that replaces the call site. */
19
+ readonly root: string;
20
+ }
21
+
22
+ /**
23
+ * The size of a component's copy at one call site, once its props have
24
+ * 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.
27
+ */
28
+ export function copySize(
29
+ filename: string,
30
+ header: string,
31
+ copy: Copy,
32
+ inTemplate: boolean,
33
+ options: ResolvedOptions,
34
+ ): 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
+ });
51
+ if (!changed) {
52
+ break;
53
+ }
54
+ code = s.toString();
55
+ }
56
+ const program = parse(filename, code);
57
+ const wrapper = program.body.at(-1);
58
+ if (wrapper?.type !== 'FunctionDeclaration' || !wrapper.body) {
59
+ return Number.POSITIVE_INFINITY;
60
+ }
61
+ const estimator = new SizeEstimator(code);
62
+ let size = 0;
63
+ for (const statement of wrapper.body.body) {
64
+ if (statement.type !== 'ReturnStatement') {
65
+ size += estimator.node(statement);
66
+ continue;
67
+ }
68
+ if (statement.argument) {
69
+ size += estimator.root(statement.argument, inTemplate);
70
+ }
71
+ }
72
+ return size;
73
+ }