solid-optimizer 1.0.0-next.8 → 1.0.0

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.8",
3
+ "version": "1.0.0",
4
4
  "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
@@ -42,21 +42,21 @@
42
42
  "devDependencies": {
43
43
  "@fast-check/vitest": "^0.5.0",
44
44
  "@jridgewell/trace-mapping": "^0.3.31",
45
- "@solidjs/compiler": "2.0.0-rc.21",
46
- "@solidjs/vite-plugin": "3.0.0-next.50",
47
- "@solidjs/web": "2.0.0-rc.21",
45
+ "@solidjs/compiler": "2.0.0",
46
+ "@solidjs/vite-plugin": "3.0.0",
47
+ "@solidjs/web": "2.0.0",
48
48
  "@types/node": "^26.6.3",
49
49
  "fast-check": "^4.10.2",
50
50
  "happy-dom": "^20.14.5",
51
51
  "publint": "^0.3.24",
52
- "solid-js": "2.0.0-rc.21",
52
+ "solid-js": "2.0.0",
53
53
  "tsdown": "^0.23.0",
54
54
  "typescript": "^7.0.2",
55
55
  "vite": "^8.3.1",
56
56
  "vitest": "^5.0.2"
57
57
  },
58
58
  "peerDependencies": {
59
- "@solidjs/vite-plugin": "^3.0.0-next.46",
59
+ "@solidjs/vite-plugin": "^3.0.0",
60
60
  "vite": "^8.0.0 || ^9.0.0"
61
61
  },
62
62
  "peerDependenciesMeta": {
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';
@@ -16,6 +15,7 @@ import { lowerConditionals } from './conditional';
16
15
  import type { ScopeAnalysis } from './scope';
17
16
  import { analyzeScopes } from './scope';
18
17
  import { terminateStatements } from './terminate';
18
+ import { Editor } from './text';
19
19
  import type { Primitive } from './value';
20
20
 
21
21
  export interface CompileOptions {
@@ -202,7 +202,7 @@ type Pass = (context: PassContext) => boolean;
202
202
 
203
203
  interface PassResult {
204
204
  code: string;
205
- map: string | undefined;
205
+ map: DecodedSourceMap | undefined;
206
206
  }
207
207
 
208
208
  /** A parse of some code, which passes that change nothing can share. */
@@ -230,7 +230,7 @@ function runPass(
230
230
  const context: PassContext = {
231
231
  code,
232
232
  filename,
233
- s: new MagicString(code),
233
+ s: new Editor(code, sourceMap),
234
234
  program: parsed.program,
235
235
  parents: parsed.parents,
236
236
  // A pass adds the names it gives out, so each pass starts from the parse's own.
@@ -243,9 +243,7 @@ function runPass(
243
243
  }
244
244
  return {
245
245
  code: context.s.toString(),
246
- map: sourceMap
247
- ? context.s.generateMap({ source: filename, hires: true, includeContent: true }).toString()
248
- : undefined,
246
+ map: sourceMap ? context.s.generateMap(filename) : undefined,
249
247
  };
250
248
  }
251
249
 
@@ -278,7 +276,7 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
278
276
  passes.push(inlineMemos);
279
277
  }
280
278
 
281
- const maps: string[] = [];
279
+ const maps: DecodedSourceMap[] = [];
282
280
  let current = code;
283
281
  // A pass that changes nothing leaves the code as it was, so the next pass
284
282
  // reuses the parse instead of parsing the same code again.
@@ -24,6 +24,8 @@ import type { PassContext } from './context';
24
24
  import { solidExport } from './context';
25
25
  import { findAttribute } from './jsx';
26
26
  import { lookup, scopeAt } from './scope';
27
+ import type { Content, Text } from './text';
28
+ import { join, joinText } from './text';
27
29
 
28
30
  /**
29
31
  * How a lowered `<Show>` or `<Switch>` is written where it stands.
@@ -55,7 +57,7 @@ export function lowerConditionals(context: PassContext): boolean {
55
57
  context.s.overwrite(
56
58
  node.start,
57
59
  node.end,
58
- form === 'container' ? `{${ternary}}` : `<>{${ternary}}</>`,
60
+ form === 'container' ? joinText`{${ternary}}` : joinText`<>{${ternary}}</>`,
59
61
  );
60
62
  changed = true;
61
63
  return;
@@ -86,14 +88,14 @@ function builtInOf(context: PassContext, element: JSXElement): string | undefine
86
88
  * The ternary a `<Show>` or `<Switch>` lowers to, or `undefined` when its
87
89
  * props or children need the component.
88
90
  */
89
- function ternaryOf(context: PassContext, element: JSXElement): string | undefined {
91
+ function ternaryOf(context: PassContext, element: JSXElement): Text | undefined {
90
92
  switch (builtInOf(context, element)) {
91
93
  case 'Show': {
92
94
  const branch = branchOf(context, element, ['when', 'fallback']);
93
95
  if (branch === undefined) {
94
96
  return undefined;
95
97
  }
96
- return `${branch.when} ? ${branch.children} : ${branch.fallback ?? 'undefined'}`;
98
+ return joinText`${branch.when} ? ${branch.children} : ${branch.fallback ?? 'undefined'}`;
97
99
  }
98
100
  case 'Switch':
99
101
  return switchTernary(context, element);
@@ -105,12 +107,12 @@ function ternaryOf(context: PassContext, element: JSXElement): string | undefine
105
107
  /**
106
108
  * A `<Switch>` whose children are all `<Match>`es that lower, as a chain of ternaries.
107
109
  */
108
- function switchTernary(context: PassContext, element: JSXElement): string | undefined {
110
+ function switchTernary(context: PassContext, element: JSXElement): Text | undefined {
109
111
  const props = propsOf(context, element, ['fallback']);
110
112
  if (props === undefined) {
111
113
  return undefined;
112
114
  }
113
- let chain = '';
115
+ const chain: Content[] = [];
114
116
  let count = 0;
115
117
  for (const child of significantChildren(element.children)) {
116
118
  if (child.type !== 'JSXElement' || builtInOf(context, child) !== 'Match') {
@@ -120,14 +122,14 @@ function switchTernary(context: PassContext, element: JSXElement): string | unde
120
122
  if (branch === undefined) {
121
123
  return undefined;
122
124
  }
123
- chain += `${branch.when} ? ${branch.children} : `;
125
+ chain.push(branch.when, ' ? ', branch.children, ' : ');
124
126
  count += 1;
125
127
  }
126
128
  // A `<Switch>` without a `<Match>` renders its fallback, which the fold pass resolves.
127
129
  if (count === 0) {
128
130
  return undefined;
129
131
  }
130
- return `${chain}${props.get('fallback') ?? 'undefined'}`;
132
+ return join([...chain, props.get('fallback') ?? 'undefined']);
131
133
  }
132
134
 
133
135
  /**
@@ -196,8 +198,8 @@ function propsOf(
196
198
  context: PassContext,
197
199
  element: JSXElement,
198
200
  names: readonly string[],
199
- ): Map<string, string> | undefined {
200
- const props = new Map<string, string>();
201
+ ): Map<string, Text> | undefined {
202
+ const props = new Map<string, Text>();
201
203
  for (const attribute of element.openingElement.attributes) {
202
204
  if (
203
205
  attribute.type !== 'JSXAttribute' ||
@@ -217,9 +219,9 @@ function propsOf(
217
219
  }
218
220
 
219
221
  interface Branch {
220
- readonly when: string;
221
- readonly children: string;
222
- readonly fallback: string | undefined;
222
+ readonly when: Text;
223
+ readonly children: Content;
224
+ readonly fallback: Text | undefined;
223
225
  }
224
226
 
225
227
  /**
@@ -278,7 +280,7 @@ function branchOf(
278
280
  : '';
279
281
  return {
280
282
  when,
281
- children: significant.length > 0 ? `<>${inner}</>` : 'undefined',
283
+ children: significant.length > 0 ? joinText`<>${inner}</>` : 'undefined',
282
284
  fallback: props.get('fallback'),
283
285
  };
284
286
  }
@@ -291,7 +293,7 @@ function callbackBody(
291
293
  context: PassContext,
292
294
  element: JSXElement,
293
295
  callback: ArrowFunctionExpression,
294
- ): string | undefined {
296
+ ): Text | undefined {
295
297
  // `<Show>` only passes the value to a callback that takes a parameter, and
296
298
  // calls it untracked. A body that is only JSX reads nothing as it is made.
297
299
  const param = callback.params.at(0);
@@ -317,7 +319,7 @@ function callbackBody(
317
319
  return undefined;
318
320
  }
319
321
  const when = value.expression;
320
- const whenText = `(${context.s.slice(when.start, when.end)})`;
322
+ const whenText = joinText`(${context.s.slice(when.start, when.end)})`;
321
323
 
322
324
  // Each name `when` reads has to mean the same thing where the body reads the parameter.
323
325
  const names: Node[] = [];
@@ -327,7 +329,7 @@ function callbackBody(
327
329
  if (binding?.identifier !== param || binding.mutated || namesInTypes(body, param.name)) {
328
330
  return undefined;
329
331
  }
330
- const edits: { start: number; end: number; text: string }[] = [];
332
+ const edits: { start: number; end: number; text: Text }[] = [];
331
333
  for (const reference of binding.references) {
332
334
  // A tag like `<C />` names a component, which an expression cannot replace.
333
335
  if (reference.type !== 'Identifier') {
@@ -353,19 +355,24 @@ function callbackBody(
353
355
  } else if (parent?.type === 'Property' && parent.shorthand) {
354
356
  return undefined;
355
357
  } else {
356
- edits.push({ start: reference.start, end: reference.end, text: `(() => ${whenText})` });
358
+ edits.push({
359
+ start: reference.start,
360
+ end: reference.end,
361
+ text: joinText`(() => ${whenText})`,
362
+ });
357
363
  }
358
364
  }
359
365
 
360
366
  // Write the body with the edits, in order.
361
367
  edits.sort((a, b) => a.start - b.start);
362
- let text = '';
368
+ const parts: Text[] = [];
363
369
  let at = body.start;
364
370
  for (const edit of edits) {
365
- text += context.s.slice(at, edit.start) + edit.text;
371
+ parts.push(context.s.slice(at, edit.start), edit.text);
366
372
  at = edit.end;
367
373
  }
368
- return text + context.s.slice(at, body.end);
374
+ parts.push(context.s.slice(at, body.end));
375
+ return join(parts);
369
376
  }
370
377
 
371
378
  /**
@@ -518,7 +525,7 @@ function significantChildren(children: readonly JSXChild[]): JSXChild[] {
518
525
  * The text of an attribute's value as an expression, or `undefined` when the
519
526
  * value is not supported.
520
527
  */
521
- function expressionText(context: PassContext, attribute: JSXAttribute): string | undefined {
528
+ function expressionText(context: PassContext, attribute: JSXAttribute): Text | undefined {
522
529
  const { value } = attribute;
523
530
  if (value === null) {
524
531
  // A bare attribute is `true`. The fold pass resolves those.
@@ -529,7 +536,7 @@ function expressionText(context: PassContext, attribute: JSXAttribute): string |
529
536
  if (value.expression.type === 'JSXEmptyExpression') {
530
537
  return undefined;
531
538
  }
532
- return `(${context.s.slice(value.expression.start, value.expression.end)})`;
539
+ return joinText`(${context.s.slice(value.expression.start, value.expression.end)})`;
533
540
  case 'JSXElement':
534
541
  case 'JSXFragment':
535
542
  return context.s.slice(value.start, value.end);
package/src/context.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type MagicString from 'magic-string';
1
+ import type { Editor, Text } from './text';
2
2
  import type { Node, Program } from 'oxc-parser';
3
3
  import type { Parents } from './ast';
4
4
  import type { ResolvedImportedConstants } from './constants';
@@ -92,7 +92,7 @@ export interface PassContext {
92
92
  readonly code: string;
93
93
  /** The file name, which picks the parser. */
94
94
  readonly filename: string;
95
- readonly s: MagicString;
95
+ readonly s: Editor;
96
96
  readonly program: Program;
97
97
  readonly parents: Parents;
98
98
  readonly scopes: ScopeAnalysis;
@@ -116,6 +116,6 @@ export function recordSkip(
116
116
  /**
117
117
  * The current text of `node`, including the edits made inside it so far.
118
118
  */
119
- export function getText(context: PassContext, node: Node): string {
119
+ export function getText(context: PassContext, node: Node): Text {
120
120
  return context.s.slice(node.start, node.end);
121
121
  }
package/src/copy-size.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  * components it calls, as the passes after inlining would. Then it estimates
4
4
  * its size with `SizeEstimator`.
5
5
  */
6
- import MagicString from 'magic-string';
6
+ import type { Content } from './text';
7
+ import { Editor, getCode, join } from './text';
7
8
  import { collectParents, parse } from './ast';
8
9
  import type { PassContext, ResolvedOptions } from './context';
9
10
  import { fold } from './fold';
@@ -33,9 +34,9 @@ export interface CalledComponents {
33
34
 
34
35
  export interface Copy {
35
36
  /** The statements the copy hoists out of the JSX. */
36
- readonly statements: readonly string[];
37
+ readonly statements: readonly Content[];
37
38
  /** The JSX that replaces the call site. */
38
- readonly root: string;
39
+ readonly root: Content;
39
40
  }
40
41
 
41
42
  /**
@@ -56,14 +57,14 @@ export function copySize(
56
57
  measured?: { code: string },
57
58
  ): number {
58
59
  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
+ let code = `${header}\n${calls?.declarations ?? ''}\nfunction ${WRAPPER}() {\n${join(copy.statements, '\n').code}\nreturn (${getCode(copy.root)});\n}\n`;
60
61
  // Constant props fold the copy's conditions, and can let the calls in it
61
62
  // inline, as later passes would.
62
63
  for (let round = 0; round < 4; round += 1) {
63
64
  let changed = false;
64
65
  for (const pass of calls ? [fold, calls.inline] : [fold]) {
65
66
  const program = parse(filename, code);
66
- const s = new MagicString(code);
67
+ const s = new Editor(code, false);
67
68
  depth += 1;
68
69
  let passChanged: boolean;
69
70
  try {