solid-optimizer 1.0.0-next.0 → 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/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.10",
4
4
  "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
@@ -36,17 +36,20 @@
36
36
  "dependencies": {
37
37
  "@jridgewell/remapping": "^2.3.5",
38
38
  "magic-string": "^1.4.2",
39
- "oxc-parser": "^0.151.0"
39
+ "oxc-parser": "^0.151.0",
40
+ "parse5": "^8.1.0"
40
41
  },
41
42
  "devDependencies": {
43
+ "@fast-check/vitest": "^0.5.0",
42
44
  "@jridgewell/trace-mapping": "^0.3.31",
43
- "@solidjs/compiler": "2.0.0-rc.16",
45
+ "@solidjs/compiler": "2.0.0-rc.21",
44
46
  "@solidjs/vite-plugin": "3.0.0-next.50",
45
- "@solidjs/web": "2.0.0-rc.16",
47
+ "@solidjs/web": "2.0.0-rc.21",
46
48
  "@types/node": "^26.6.3",
49
+ "fast-check": "^4.10.2",
47
50
  "happy-dom": "^20.14.5",
48
51
  "publint": "^0.3.24",
49
- "solid-js": "2.0.0-rc.16",
52
+ "solid-js": "2.0.0-rc.21",
50
53
  "tsdown": "^0.23.0",
51
54
  "typescript": "^7.0.2",
52
55
  "vite": "^8.3.1",
package/src/ast.ts CHANGED
@@ -90,7 +90,7 @@ const RAW_TRANSFER = rawTransferSupported();
90
90
 
91
91
  export function parse(filename: string, code: string): Program {
92
92
  const options: ParserOptions & { experimentalRawTransfer?: boolean } = {
93
- lang: languageOf(filename),
93
+ lang: getLanguage(filename),
94
94
  sourceType: 'module',
95
95
  preserveParens: true,
96
96
  experimentalRawTransfer: RAW_TRANSFER,
@@ -103,7 +103,7 @@ export function parse(filename: string, code: string): Program {
103
103
  return result.program;
104
104
  }
105
105
 
106
- function languageOf(filename: string): 'jsx' | 'ts' | 'tsx' {
106
+ function getLanguage(filename: string): 'jsx' | 'ts' | 'tsx' {
107
107
  if (/\.tsx$/i.test(filename)) {
108
108
  return 'tsx';
109
109
  }
@@ -124,7 +124,8 @@ export function unwrap(node: Expression): Expression {
124
124
  current.type === 'TSAsExpression' ||
125
125
  current.type === 'TSSatisfiesExpression' ||
126
126
  current.type === 'TSNonNullExpression' ||
127
- current.type === 'TSTypeAssertion'
127
+ current.type === 'TSTypeAssertion' ||
128
+ current.type === 'TSInstantiationExpression'
128
129
  ) {
129
130
  current = current.expression;
130
131
  }
package/src/compile.ts CHANGED
@@ -1,7 +1,6 @@
1
- import type { SourceMap } from '@jridgewell/remapping';
1
+ import type { DecodedSourceMap, SourceMap } from '@jridgewell/remapping';
2
2
  import type { Program } from 'oxc-parser';
3
3
  import remapping from '@jridgewell/remapping';
4
- import MagicString from 'magic-string';
5
4
  import type { Parents } from './ast';
6
5
  import { collectParents, parse } from './ast';
7
6
  import type { ImportedConstants } from './constants';
@@ -10,9 +9,13 @@ import type { PassContext, ResolvedOptions, Skip } from './context';
10
9
  import { fold } from './fold';
11
10
  import { inline } from './inline';
12
11
  import { inlineMemos } from './memo';
12
+ import { repairNesting } from './nesting';
13
13
  import { removeProviders } from './provider';
14
+ import { lowerConditionals } from './conditional';
14
15
  import type { ScopeAnalysis } from './scope';
15
16
  import { analyzeScopes } from './scope';
17
+ import { terminateStatements } from './terminate';
18
+ import { Editor } from './text';
16
19
  import type { Primitive } from './value';
17
20
 
18
21
  export interface CompileOptions {
@@ -50,6 +53,14 @@ export interface CompileOptions {
50
53
  * @default true
51
54
  */
52
55
  memos?: boolean;
56
+ /**
57
+ * Replace a `<Show>` or `<Switch>` without callback children or `keyed`
58
+ * with a ternary, which Solid's JSX transform compiles to memos without a
59
+ * component.
60
+ *
61
+ * @default true
62
+ */
63
+ conditionals?: boolean;
53
64
  /**
54
65
  * The names of Solid's built-in components. A tag only folds when it is
55
66
  * one of these, and an empty list turns control-flow folding off.
@@ -111,6 +122,12 @@ export interface CompileOptions {
111
122
  * @default false
112
123
  */
113
124
  alwaysInline?: boolean;
125
+ /**
126
+ * Top-level components to inline wherever they can be, like
127
+ * `alwaysInline` does for every component. The Vite plugin lists the
128
+ * copies of components from `optimizer.packages` here.
129
+ */
130
+ alwaysInlineComponents?: readonly string[];
114
131
  /**
115
132
  * The most passes to run. One pass can expose work for the next, such as
116
133
  * an inlined component whose props now fold. Compilation stops early once
@@ -161,6 +178,12 @@ const DEFAULT_BUILT_INS = [
161
178
  const DEFAULT_MODULE_SOURCES = ['solid-js', '@solidjs/web'];
162
179
 
163
180
  function resolveOptions(options: CompileOptions): ResolvedOptions {
181
+ // Each module's context names become a set.
182
+ const importedContexts = new Map<string, Set<string>>();
183
+ for (const [source, names] of Object.entries(options.importedContexts ?? {})) {
184
+ importedContexts.set(source, new Set(names));
185
+ }
186
+
164
187
  return {
165
188
  builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
166
189
  moduleSources: options.moduleSources ?? DEFAULT_MODULE_SOURCES,
@@ -168,14 +191,10 @@ function resolveOptions(options: CompileOptions): ResolvedOptions {
168
191
  constantVars: options.constantVars ?? false,
169
192
  importedConstants: resolveImportedConstants(options.importedConstants),
170
193
  globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
171
- importedContexts: new Map(
172
- Object.entries(options.importedContexts ?? {}).map(([source, names]) => [
173
- source,
174
- new Set(names),
175
- ]),
176
- ),
194
+ importedContexts,
177
195
  sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
178
196
  alwaysInline: options.alwaysInline ?? false,
197
+ alwaysInlineComponents: new Set(options.alwaysInlineComponents ?? []),
179
198
  };
180
199
  }
181
200
 
@@ -183,7 +202,7 @@ type Pass = (context: PassContext) => boolean;
183
202
 
184
203
  interface PassResult {
185
204
  code: string;
186
- map: string | undefined;
205
+ map: DecodedSourceMap | undefined;
187
206
  }
188
207
 
189
208
  /** A parse of some code, which passes that change nothing can share. */
@@ -211,7 +230,7 @@ function runPass(
211
230
  const context: PassContext = {
212
231
  code,
213
232
  filename,
214
- s: new MagicString(code),
233
+ s: new Editor(code, sourceMap),
215
234
  program: parsed.program,
216
235
  parents: parsed.parents,
217
236
  // A pass adds the names it gives out, so each pass starts from the parse's own.
@@ -224,9 +243,7 @@ function runPass(
224
243
  }
225
244
  return {
226
245
  code: context.s.toString(),
227
- map: sourceMap
228
- ? context.s.generateMap({ source: filename, hires: true, includeContent: true }).toString()
229
- : undefined,
246
+ map: sourceMap ? context.s.generateMap(filename) : undefined,
230
247
  };
231
248
  }
232
249
 
@@ -259,7 +276,7 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
259
276
  passes.push(inlineMemos);
260
277
  }
261
278
 
262
- const maps: string[] = [];
279
+ const maps: DecodedSourceMap[] = [];
263
280
  let current = code;
264
281
  // A pass that changes nothing leaves the code as it was, so the next pass
265
282
  // reuses the parse instead of parsing the same code again.
@@ -270,6 +287,21 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
270
287
  }
271
288
  return parsed;
272
289
  };
290
+ const terminated = runPass(
291
+ parsedCurrent(),
292
+ filename,
293
+ resolved,
294
+ sourceMap,
295
+ terminateStatements,
296
+ undefined,
297
+ );
298
+ if (terminated) {
299
+ current = terminated.code;
300
+ if (terminated.map !== undefined) {
301
+ maps.push(terminated.map);
302
+ }
303
+ }
304
+ const start = current;
273
305
  const maxPasses = options.maxPasses ?? 10;
274
306
  let skipped: Skip[] | undefined;
275
307
  for (let round = 0; round < maxPasses; round += 1) {
@@ -291,7 +323,54 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
291
323
  }
292
324
  }
293
325
 
326
+ // `<Show>` and `<Switch>` are lowered last. The other passes read `when` of
327
+ // `<Show>` and `<Match>` as a tracked place, and a ternary's condition is
328
+ // not one. A lowered one can hold another, which the next round lowers.
329
+ if (options.conditionals ?? true) {
330
+ for (let round = 0; round < maxPasses; round += 1) {
331
+ const result = runPass(
332
+ parsedCurrent(),
333
+ filename,
334
+ resolved,
335
+ sourceMap,
336
+ lowerConditionals,
337
+ undefined,
338
+ );
339
+ if (!result) {
340
+ break;
341
+ }
342
+ current = result.code;
343
+ if (result.map !== undefined) {
344
+ maps.push(result.map);
345
+ }
346
+ }
347
+ }
348
+
349
+ // A pass can put an element straight inside a parent that cannot hold it,
350
+ // which Solid's JSX transform rejects. Only a changed module can have one
351
+ // that its source did not.
352
+ if (current !== start) {
353
+ const result = runPass(
354
+ parsedCurrent(),
355
+ filename,
356
+ resolved,
357
+ sourceMap,
358
+ repairNesting,
359
+ undefined,
360
+ );
361
+ if (result) {
362
+ current = result.code;
363
+ if (result.map !== undefined) {
364
+ maps.push(result.map);
365
+ }
366
+ }
367
+ }
368
+
294
369
  const report = skipped ? { skipped } : {};
370
+ // Semicolons alone are no reason to change a module.
371
+ if (current === start) {
372
+ return { code, map: null, ...report };
373
+ }
295
374
  if (maps.length === 0) {
296
375
  return { code: current, map: null, ...report };
297
376
  }
@@ -324,8 +403,9 @@ export function readModuleConstants(
324
403
  options: Pick<CompileOptions, 'filename' | 'importedConstants' | 'globalConstants'> = {},
325
404
  ): ModuleConstants {
326
405
  const program = parse(options.filename ?? 'input.jsx', code);
406
+ const scopes = analyzeScopes(program);
327
407
  return {
328
- imports: namedImportSources(program),
329
- exports: constantExports(program, resolveOptions(options)),
408
+ imports: namedImportSources(program, scopes),
409
+ exports: constantExports(program, resolveOptions(options), scopes),
330
410
  };
331
411
  }