solid-optimizer 1.0.0-next.5 → 1.0.0-next.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"compile-X_SMDFr2.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCD/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;;EAQA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAqGI,QAAQ,cAAc,UAAS,iBAAsB;UAiFpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
1
+ {"version":3,"file":"compile-X_SMDFr2.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCA/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;;EAQA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAqGI,QAAQ,cAAc,UAAS,iBAAsB;UAqGpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { n as readModuleConstants, t as compile } from "./compile-By9Fecbx.mjs";
1
+ import { n as readModuleConstants, t as compile } from "./compile-BuPR3pVT.mjs";
2
2
  export { compile, readModuleConstants };
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-By9Fecbx.mjs";
1
+ import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-BuPR3pVT.mjs";
2
2
  import { createRequire } from "node:module";
3
3
  import remapping from "@jridgewell/remapping";
4
4
  import MagicString from "magic-string";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "1.0.0-next.5",
3
+ "version": "1.0.0-next.7",
4
4
  "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
@@ -36,14 +36,17 @@
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
45
  "@solidjs/compiler": "2.0.0-rc.21",
44
46
  "@solidjs/vite-plugin": "3.0.0-next.50",
45
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
52
  "solid-js": "2.0.0-rc.21",
package/src/compile.ts CHANGED
@@ -10,6 +10,7 @@ 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 { repairNesting } from './nesting';
13
14
  import { removeProviders } from './provider';
14
15
  import { lowerConditionals } from './conditional';
15
16
  import type { ScopeAnalysis } from './scope';
@@ -331,6 +332,26 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
331
332
  }
332
333
  }
333
334
 
335
+ // A pass can put an element straight inside a parent that cannot hold it,
336
+ // which Solid's JSX transform rejects. Only a changed module can have one
337
+ // that its source did not.
338
+ if (current !== code) {
339
+ const result = runPass(
340
+ parsedCurrent(),
341
+ filename,
342
+ resolved,
343
+ sourceMap,
344
+ repairNesting,
345
+ undefined,
346
+ );
347
+ if (result) {
348
+ current = result.code;
349
+ if (result.map !== undefined) {
350
+ maps.push(result.map);
351
+ }
352
+ }
353
+ }
354
+
334
355
  const report = skipped ? { skipped } : {};
335
356
  if (maps.length === 0) {
336
357
  return { code: current, map: null, ...report };
package/src/fold.ts CHANGED
@@ -119,6 +119,15 @@ const EMPTY: Fold = { kind: 'empty' };
119
119
  /** Solid's calls that only create a value, so the bundler can drop an unused one. */
120
120
  const PURE_CALLS = new Set(['lazy', 'createContext']);
121
121
 
122
+ /**
123
+ * Solid's calls that only create a value when their first argument is not a
124
+ * function. Given a function, they create a computation, which runs it.
125
+ */
126
+ const PURE_VALUE_CALLS = new Set(['createSignal', 'createStore']);
127
+
128
+ /** Expression types that are never a function. */
129
+ const NOT_FUNCTION = new Set(['Literal', 'TemplateLiteral', 'ObjectExpression', 'ArrayExpression']);
130
+
122
131
  /**
123
132
  * The globals worth folding. They only apply to a reference that binds to
124
133
  * nothing, which is when the global is what runs.
@@ -267,6 +276,15 @@ function keyName(node: Node): string | undefined {
267
276
  return node.type === 'Literal' && typeof node.value === 'string' ? node.value : undefined;
268
277
  }
269
278
 
279
+ /** Whether a call has no first argument, or one that is never a function. */
280
+ function isValueArgument(call: CallExpression): boolean {
281
+ if (call.arguments.length === 0) {
282
+ return true;
283
+ }
284
+ const first = call.arguments[0];
285
+ return first.type !== 'SpreadElement' && NOT_FUNCTION.has(unwrap(first).type);
286
+ }
287
+
270
288
  type ConstantItem =
271
289
  | { readonly kind: 'primitive'; readonly text: string }
272
290
  | { readonly kind: 'object'; readonly keys: ReadonlyMap<string, string> };
@@ -432,6 +450,10 @@ class Folder {
432
450
  * branch can leave it unused, and the annotation lets the bundler drop it
433
451
  * along with its dynamic import. `createContext` only creates the context,
434
452
  * which inlining can leave unused.
453
+ *
454
+ * `createSignal` and `createStore` with a first argument that is not a
455
+ * function only create a node. It takes no owner and no hydration id, so
456
+ * dropping an unused one changes nothing else.
435
457
  */
436
458
  private annotatePure(call: CallExpression): void {
437
459
  if (call.callee.type !== 'Identifier') {
@@ -439,7 +461,10 @@ class Folder {
439
461
  }
440
462
  const binding = this.context.scopes.references.get(call.callee);
441
463
  const identity = binding ? solidExport(binding, this.context.options) : undefined;
442
- if (identity === undefined || !PURE_CALLS.has(identity)) {
464
+ if (identity === undefined) {
465
+ return;
466
+ }
467
+ if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) {
443
468
  return;
444
469
  }
445
470
  const before = this.context.code.slice(0, call.start).trimEnd();
@@ -728,14 +753,14 @@ class Folder {
728
753
  };
729
754
  case 'JSXElement':
730
755
  case 'JSXFragment':
731
- return { kind: 'jsx', text: getText(this.context, value) };
756
+ return this.foldedJSX(value);
732
757
  default:
733
758
  if (value.expression.type === 'JSXEmptyExpression') {
734
759
  return EMPTY;
735
760
  }
736
761
  // JSX in a container splices like JSX, so it can join the surrounding template.
737
762
  if (value.expression.type === 'JSXElement' || value.expression.type === 'JSXFragment') {
738
- return { kind: 'jsx', text: getText(this.context, value.expression) };
763
+ return this.foldedJSX(value.expression);
739
764
  }
740
765
  return {
741
766
  kind: 'expression',
@@ -745,6 +770,19 @@ class Folder {
745
770
  }
746
771
  }
747
772
 
773
+ /**
774
+ * What a JSX value renders, read after the folds of this round. A fold
775
+ * inside it can have replaced it with an expression already, such as an
776
+ * inner `<Show>` that became its string `fallback`.
777
+ */
778
+ private foldedJSX(node: JSXElement | JSXFragment): Fold {
779
+ const text = getText(this.context, node);
780
+ if (text.startsWith('<')) {
781
+ return { kind: 'jsx', text };
782
+ }
783
+ return { kind: 'expression', text, primary: false };
784
+ }
785
+
748
786
  private foldShow(element: JSXElement): Fold | undefined {
749
787
  const when = this.attributeTruthiness(element, 'when');
750
788
  if (when === undefined) {
package/src/inline.ts CHANGED
@@ -112,7 +112,7 @@ import {
112
112
  solidCallee,
113
113
  stableValueText,
114
114
  } from './provider';
115
- import { keptPiece, replacedPiece, writePieces } from './jsx';
115
+ import { keptPiece, replacedPiece, splicePiece, writePieces } from './jsx';
116
116
  import type { Binding, Scope } from './scope';
117
117
  import { isWithinScope, lookup, scopeAt } from './scope';
118
118
  import { copySize } from './copy-size';
@@ -1627,7 +1627,9 @@ class Inliner {
1627
1627
  const first = component.statements.at(0);
1628
1628
  const last = component.statements.at(-1);
1629
1629
  if (first && last) {
1630
- statements.push(copy.slice(first.start, last.end));
1630
+ // The last statement can rely on a line break that it loses here.
1631
+ const text = copy.slice(first.start, last.end);
1632
+ statements.push(text.endsWith(';') ? text : `${text};`);
1631
1633
  }
1632
1634
  return {
1633
1635
  element,
@@ -1637,15 +1639,21 @@ class Inliner {
1637
1639
  };
1638
1640
  }
1639
1641
 
1642
+ /** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
1643
+ private addToHost(found: { fn: Node; host: Host }, texts: readonly string[]): void {
1644
+ const key = found.host.kind === 'return' ? found.host.statement : found.fn;
1645
+ const entry = this.hosts.get(key) ?? { host: found.host, texts: [] };
1646
+ entry.texts.push(...texts);
1647
+ this.hosts.set(key, entry);
1648
+ }
1649
+
1640
1650
  /**
1641
1651
  * Writes a prepared copy over its call site.
1642
1652
  */
1643
1653
  private commit(site: PreparedSite): void {
1644
1654
  this.context.s.overwrite(site.element.start, site.element.end, site.root);
1645
1655
  if (site.host) {
1646
- const entry = this.hosts.get(site.host.fn) ?? { host: site.host.host, texts: [] };
1647
- entry.texts.push(...site.statements);
1648
- this.hosts.set(site.host.fn, entry);
1656
+ this.addToHost(site.host, site.statements);
1649
1657
  }
1650
1658
  }
1651
1659
 
@@ -1768,9 +1776,7 @@ class Inliner {
1768
1776
  const name = this.freshName(
1769
1777
  `${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
1770
1778
  );
1771
- const entry = this.hosts.get(host.fn) ?? { host: host.host, texts: [] };
1772
- entry.texts.push(`const ${name} = ${getText(this.context, provider.value)};`);
1773
- this.hosts.set(host.fn, entry);
1779
+ this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
1774
1780
  this.context.s.overwrite(provider.value.start, provider.value.end, name);
1775
1781
  this.transparent.set(element, { provider, text: name });
1776
1782
  this.changed = true;
@@ -2013,17 +2019,7 @@ class Inliner {
2013
2019
 
2014
2020
  private splicedPiece(container: JSXExpressionContainer, value: PropValue): Piece {
2015
2021
  if (value.kind === 'children') {
2016
- const first = value.children.at(0);
2017
- const last = value.children.at(-1);
2018
- if (!first || !last) {
2019
- return replacedPiece(container, '');
2020
- }
2021
- return {
2022
- node: container,
2023
- text: this.context.s.slice(first.start, last.end),
2024
- startText: first.type === 'JSXText' ? first : undefined,
2025
- endText: last.type === 'JSXText' ? last : undefined,
2026
- };
2022
+ return splicePiece(this.context.s, container, value.children);
2027
2023
  }
2028
2024
  return replacedPiece(container, value.text);
2029
2025
  }
package/src/jsx.ts CHANGED
@@ -7,8 +7,7 @@
7
7
  * whenever a replacement would put two texts that were apart next to each
8
8
  * other, an empty `{}` keeps them apart.
9
9
  */
10
- import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, JSXText, Node } from 'oxc-parser';
11
- import { isFilteredText } from './ast';
10
+ import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser';
12
11
  import type MagicString from 'magic-string';
13
12
 
14
13
  /**
@@ -19,14 +18,14 @@ export interface Piece {
19
18
  readonly node: JSXChild;
20
19
  /** The replacement text, or `undefined` to keep the child as written. */
21
20
  text: string | undefined;
22
- /** The JSX text the replacement starts with. */
23
- startText: JSXText | undefined;
24
- /** The JSX text the replacement ends with. */
25
- endText: JSXText | undefined;
21
+ /** The JSX text the piece starts with, or `undefined` when it starts with a tag or a container. */
22
+ startText: string | undefined;
23
+ /** The JSX text the piece ends with. */
24
+ endText: string | undefined;
26
25
  }
27
26
 
28
27
  export function keptPiece(node: JSXChild): Piece {
29
- const text = node.type === 'JSXText' ? node : undefined;
28
+ const text = node.type === 'JSXText' ? node.value : undefined;
30
29
  return { node, text: undefined, startText: text, endText: text };
31
30
  }
32
31
 
@@ -39,27 +38,43 @@ export function splicePiece(s: MagicString, node: JSXChild, children: readonly J
39
38
  if (!first || !last) {
40
39
  return replacedPiece(node, '');
41
40
  }
41
+ return replacedPiece(node, s.slice(first.start, last.end));
42
+ }
43
+
44
+ /**
45
+ * A piece that writes `text`, JSX children as source, in place of `node`.
46
+ *
47
+ * The texts it starts and ends with are read from `text` itself, not from
48
+ * the children it came from. A fold inside them in the same round can have
49
+ * turned a child that was an element into text, such as an inner `<Show>`.
50
+ * JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
51
+ * mark where the text ends.
52
+ */
53
+ export function replacedPiece(node: JSXChild, text: string): Piece {
54
+ const start = text.search(/[<{]/);
55
+ const end = Math.max(text.lastIndexOf('>'), text.lastIndexOf('}'));
56
+ const startText = start === -1 ? text : text.slice(0, start);
57
+ const endText = text.slice(end + 1);
42
58
  return {
43
59
  node,
44
- text: s.slice(first.start, last.end),
45
- startText: first.type === 'JSXText' ? first : undefined,
46
- endText: last.type === 'JSXText' ? last : undefined,
60
+ text,
61
+ startText: startText === '' ? undefined : startText,
62
+ endText: endText === '' ? undefined : endText,
47
63
  };
48
64
  }
49
65
 
50
- export function replacedPiece(node: JSXChild, text: string): Piece {
51
- return { node, text, startText: undefined, endText: undefined };
66
+ /** Whether JSX drops this text: whitespace that contains a line break. */
67
+ function isDroppedText(text: string): boolean {
68
+ return /^\s*$/.test(text) && /[\r\n]/.test(text);
52
69
  }
53
70
 
54
71
  /**
55
72
  * Whether two texts render differently as one text node than apart.
56
73
  * Two texts that JSX drops, like line breaks and indentation, still drop together.
57
74
  */
58
- function mergeChangesText(before: JSXText | undefined, after: JSXText | undefined): boolean {
75
+ function mergeChangesText(before: string | undefined, after: string | undefined): boolean {
59
76
  return (
60
- before !== undefined &&
61
- after !== undefined &&
62
- !(isFilteredText(before) && isFilteredText(after))
77
+ before !== undefined && after !== undefined && !(isDroppedText(before) && isDroppedText(after))
63
78
  );
64
79
  }
65
80