solid-optimizer 0.3.0 → 0.4.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": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Experimental compile-time optimizer for SolidJS 1.x",
5
5
  "keywords": [
6
6
  "compiler",
package/src/ast.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Expression, JSXChild, Node, Program } from 'oxc-parser';
2
- import { parseSync, visitorKeys } from 'oxc-parser';
1
+ import type { Expression, JSXChild, Node, ParserOptions, Program } from 'oxc-parser';
2
+ import { parseSync, rawTransferSupported, visitorKeys } from 'oxc-parser';
3
3
 
4
4
  /**
5
5
  * Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
@@ -82,12 +82,20 @@ export function collectParents(program: Program): Parents {
82
82
  return parents;
83
83
  }
84
84
 
85
+ /**
86
+ * Raw transfer hands the AST over in shared memory instead of as JSON, which
87
+ * is several times faster to read. It is not in the options' type yet.
88
+ */
89
+ const RAW_TRANSFER = rawTransferSupported();
90
+
85
91
  export function parse(filename: string, code: string): Program {
86
- const result = parseSync(filename, code, {
92
+ const options: ParserOptions & { experimentalRawTransfer?: boolean } = {
87
93
  lang: languageOf(filename),
88
94
  sourceType: 'module',
89
95
  preserveParens: true,
90
- });
96
+ experimentalRawTransfer: RAW_TRANSFER,
97
+ };
98
+ const result = parseSync(filename, code, options);
91
99
  const error = result.errors.at(0);
92
100
  if (error) {
93
101
  throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
package/src/compile.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import type { SourceMap } from '@jridgewell/remapping';
2
+ import type { Program } from 'oxc-parser';
2
3
  import remapping from '@jridgewell/remapping';
3
4
  import MagicString from 'magic-string';
5
+ import type { Parents } from './ast';
4
6
  import { collectParents, parse } from './ast';
5
7
  import type { ImportedConstants } from './constants';
6
8
  import { constantExports, namedImportSources, resolveImportedConstants } from './constants';
7
- import type { PassContext, ResolvedOptions } from './context';
9
+ import type { PassContext, ResolvedOptions, Skip } from './context';
8
10
  import { fold } from './fold';
9
11
  import { inline } from './inline';
10
12
  import { inlineMemos } from './memo';
11
13
  import { simplifyServer } from './server';
12
14
  import { removeProviders } from './provider';
15
+ import type { ScopeAnalysis } from './scope';
13
16
  import { analyzeScopes } from './scope';
14
17
  import type { Primitive } from './value';
15
18
 
@@ -130,12 +133,24 @@ export interface CompileOptions {
130
133
  * @default true
131
134
  */
132
135
  sourceMap?: boolean;
136
+ /**
137
+ * Record what the inline, provider, and memo passes leave alone, and why.
138
+ * The result lists them in `skipped`.
139
+ *
140
+ * @default false
141
+ */
142
+ report?: boolean;
133
143
  }
134
144
 
135
145
  export interface CompileResult {
136
146
  code: string;
137
147
  /** The map from the output back to the input, or `null` when nothing changed or maps are off. */
138
148
  map: SourceMap | null;
149
+ /**
150
+ * What the last round left alone, and why, when `report` is set. The last
151
+ * round changes nothing, unless `maxPasses` stopped the rounds first.
152
+ */
153
+ skipped?: Skip[];
139
154
  }
140
155
 
141
156
  const DEFAULT_BUILT_INS = [
@@ -178,22 +193,38 @@ interface PassResult {
178
193
  map: string | undefined;
179
194
  }
180
195
 
196
+ /** A parse of some code, which passes that change nothing can share. */
197
+ interface Parsed {
198
+ readonly code: string;
199
+ readonly program: Program;
200
+ readonly parents: Parents;
201
+ readonly scopes: ScopeAnalysis;
202
+ }
203
+
204
+ function parseCode(code: string, filename: string): Parsed {
205
+ const program = parse(filename, code);
206
+ return { code, program, parents: collectParents(program), scopes: analyzeScopes(program) };
207
+ }
208
+
181
209
  function runPass(
182
- code: string,
210
+ parsed: Parsed,
183
211
  filename: string,
184
212
  options: ResolvedOptions,
185
213
  sourceMap: boolean,
186
214
  pass: Pass,
215
+ skips: Skip[] | undefined,
187
216
  ): PassResult | undefined {
188
- const program = parse(filename, code);
217
+ const { code } = parsed;
189
218
  const context: PassContext = {
190
219
  code,
191
220
  filename,
192
221
  s: new MagicString(code),
193
- program,
194
- parents: collectParents(program),
195
- scopes: analyzeScopes(program),
222
+ program: parsed.program,
223
+ parents: parsed.parents,
224
+ // A pass adds the names it gives out, so each pass starts from the parse's own.
225
+ scopes: { ...parsed.scopes, names: new Set(parsed.scopes.names) },
196
226
  options,
227
+ skips,
197
228
  };
198
229
  if (!pass(context)) {
199
230
  return undefined;
@@ -237,11 +268,23 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
237
268
 
238
269
  const maps: string[] = [];
239
270
  let current = code;
271
+ // A pass that changes nothing leaves the code as it was, so the next pass
272
+ // reuses the parse instead of parsing the same code again.
273
+ let parsed: Parsed | undefined;
274
+ const parsedCurrent = (): Parsed => {
275
+ if (parsed?.code !== current) {
276
+ parsed = parseCode(current, filename);
277
+ }
278
+ return parsed;
279
+ };
240
280
  const maxPasses = options.maxPasses ?? 10;
281
+ let skipped: Skip[] | undefined;
241
282
  for (let round = 0; round < maxPasses; round += 1) {
242
283
  let changed = false;
284
+ // Only the last round's skips are kept. An earlier round can skip what a later one takes.
285
+ skipped = options.report === true ? [] : undefined;
243
286
  for (const pass of passes) {
244
- const result = runPass(current, filename, resolved, sourceMap, pass);
287
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, pass, skipped);
245
288
  if (result) {
246
289
  changed = true;
247
290
  current = result.code;
@@ -256,7 +299,14 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
256
299
  }
257
300
 
258
301
  if (options.server ?? false) {
259
- const result = runPass(current, filename, resolved, sourceMap, simplifyServer);
302
+ const result = runPass(
303
+ parsedCurrent(),
304
+ filename,
305
+ resolved,
306
+ sourceMap,
307
+ simplifyServer,
308
+ undefined,
309
+ );
260
310
  if (result) {
261
311
  current = result.code;
262
312
  if (result.map !== undefined) {
@@ -265,11 +315,12 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
265
315
  }
266
316
  }
267
317
 
318
+ const report = skipped ? { skipped } : {};
268
319
  if (maps.length === 0) {
269
- return { code: current, map: null };
320
+ return { code: current, map: null, ...report };
270
321
  }
271
322
  // Each map points at the output of the pass before it, so they chain from the last one.
272
- return { code: current, map: remapping(maps.reverse(), () => null) };
323
+ return { code: current, map: remapping(maps.reverse(), () => null), ...report };
273
324
  }
274
325
 
275
326
  export interface ModuleConstants {
package/src/context.ts CHANGED
@@ -62,6 +62,21 @@ export function solidExport(binding: Binding, options: ResolvedOptions): string
62
62
  return undefined;
63
63
  }
64
64
 
65
+ /** The passes that record what they leave alone. */
66
+ export type SkipPass = 'inline' | 'contexts' | 'memos';
67
+
68
+ /**
69
+ * Something a pass left alone, and why. The `compile` option `report`
70
+ * collects them.
71
+ */
72
+ export interface Skip {
73
+ readonly pass: SkipPass;
74
+ /** A short code for the reason, like `call-spread`. */
75
+ readonly reason: string;
76
+ /** The component, context, or memo it is about. */
77
+ readonly name: string;
78
+ }
79
+
65
80
  /**
66
81
  * Everything a pass needs: the parsed code and the string it edits.
67
82
  *
@@ -77,6 +92,20 @@ export interface PassContext {
77
92
  readonly parents: Parents;
78
93
  readonly scopes: ScopeAnalysis;
79
94
  readonly options: ResolvedOptions;
95
+ /** Where a pass records what it leaves alone, when the caller asked for a report. */
96
+ readonly skips: Skip[] | undefined;
97
+ }
98
+
99
+ /**
100
+ * Records that a pass left something alone, when the caller asked for a report.
101
+ */
102
+ export function recordSkip(
103
+ context: PassContext,
104
+ pass: SkipPass,
105
+ reason: string,
106
+ name: string,
107
+ ): void {
108
+ context.skips?.push({ pass, reason, name });
80
109
  }
81
110
 
82
111
  /**
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export type { CompileOptions, CompileResult, ModuleConstants } from './compile';
2
2
  export { compile, readModuleConstants } from './compile';
3
3
  export type { ImportedConstants } from './constants';
4
+ export type { Skip, SkipPass } from './context';
4
5
  export type { Primitive } from './value';
package/src/inline.ts CHANGED
@@ -9,7 +9,8 @@
9
9
  *
10
10
  * - It is declared once at the top level of the module, with a capitalized
11
11
  * name, as a function declaration or a `const` bound to a function.
12
- * - It takes no parameter, or one identifier that is only read as `props.name`.
12
+ * - It takes no parameter, or one identifier that is only read as `props.name`,
13
+ * or spread onto an element as `{...props}`.
13
14
  * - Its body is statements followed by one final `return` of JSX, or an arrow
14
15
  * function whose body is JSX.
15
16
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
@@ -86,7 +87,7 @@ import {
86
87
  unwrap,
87
88
  } from './ast';
88
89
  import type { PassContext, ResolvedOptions } from './context';
89
- import { isConstantDeclaration, textOf } from './context';
90
+ import { isConstantDeclaration, recordSkip, solidExport, textOf } from './context';
90
91
  import type { Piece } from './jsx';
91
92
  import type { ClosedOptions, Provider } from './provider';
92
93
  import {
@@ -473,6 +474,29 @@ function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefine
473
474
  return attributes;
474
475
  }
475
476
 
477
+ /**
478
+ * Why `attributesOf` turns down a call site, for the report.
479
+ */
480
+ function callSiteReason(element: JSXElement): string {
481
+ const names = new Set<string>();
482
+ for (const attribute of element.openingElement.attributes) {
483
+ if (attribute.type !== 'JSXAttribute') {
484
+ return 'call-spread';
485
+ }
486
+ if (attribute.name.type !== 'JSXIdentifier') {
487
+ return 'call-namespace';
488
+ }
489
+ if (attribute.name.name === 'ref') {
490
+ return 'call-ref';
491
+ }
492
+ if (names.has(attribute.name.name)) {
493
+ break;
494
+ }
495
+ names.add(attribute.name.name);
496
+ }
497
+ return 'call-duplicate';
498
+ }
499
+
476
500
  function isAssignmentTarget(context: PassContext, node: Node): boolean {
477
501
  const parent = context.parents.get(node);
478
502
  if (!parent) {
@@ -517,6 +541,44 @@ class Inliner {
517
541
 
518
542
  constructor(private readonly context: PassContext) {}
519
543
 
544
+ /** Why the last component or call site was turned down, for the report. */
545
+ private reason = '';
546
+
547
+ /**
548
+ * Whether a binding holds a component made by Solid's `lazy`, for the
549
+ * report. In a chunk, `lazy` can be a local declaration under another
550
+ * name, so a call given a function with a dynamic import counts too.
551
+ */
552
+ private isLazy(binding: Binding): boolean {
553
+ const { declaration } = binding;
554
+ const init =
555
+ declaration.type === 'VariableDeclarator' && declaration.init
556
+ ? unwrap(declaration.init)
557
+ : undefined;
558
+ if (init?.type !== 'CallExpression' || init.callee.type !== 'Identifier') {
559
+ return false;
560
+ }
561
+ const callee = this.context.scopes.references.get(init.callee);
562
+ if (callee && solidExport(callee, this.context.options) === 'lazy') {
563
+ return true;
564
+ }
565
+ const loader = init.arguments.at(0);
566
+ if (!loader || !isFunctionNode(loader)) {
567
+ return false;
568
+ }
569
+ let imports = false;
570
+ const visit = (node: Node): void => {
571
+ if (node.type === 'ImportExpression') {
572
+ imports = true;
573
+ }
574
+ if (!imports) {
575
+ forEachChild(node, visit);
576
+ }
577
+ };
578
+ visit(loader);
579
+ return imports;
580
+ }
581
+
520
582
  // ---------------------------------------------------------------------------
521
583
  // Components
522
584
  // ---------------------------------------------------------------------------
@@ -546,15 +608,29 @@ class Inliner {
546
608
  }
547
609
 
548
610
  private analyzeComponent(binding: Binding): Component | undefined {
549
- if (!isCapitalized(binding.name) || binding.mutated) {
611
+ if (!isCapitalized(binding.name)) {
612
+ return undefined;
613
+ }
614
+ if (binding.mutated) {
615
+ this.reason = 'component-mutated';
550
616
  return undefined;
551
617
  }
552
618
  const fn = functionOf(binding, this.context.options);
553
- if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) {
619
+ if (!fn?.body) {
620
+ this.reason = this.isLazy(binding) ? 'lazy' : 'not-a-function';
621
+ return undefined;
622
+ }
623
+ if (fn.async || fn.generator) {
624
+ this.reason = 'component-async';
625
+ return undefined;
626
+ }
627
+ if (fn.params.length > 1) {
628
+ this.reason = 'component-params';
554
629
  return undefined;
555
630
  }
556
631
  const scope = this.scopeOf(fn);
557
632
  if (!scope) {
633
+ this.reason = 'not-a-function';
558
634
  return undefined;
559
635
  }
560
636
 
@@ -565,6 +641,7 @@ class Inliner {
565
641
  const body = fn.body.body;
566
642
  const last = body.at(-1);
567
643
  if (last?.type !== 'ReturnStatement' || !last.argument) {
644
+ this.reason = 'component-body';
568
645
  return undefined;
569
646
  }
570
647
  const rest: Statement[] = [];
@@ -573,7 +650,12 @@ class Inliner {
573
650
  // TypeScript's AST gives every expression statement a `directive`
574
651
  // key, which is `null` unless the statement is a directive.
575
652
  const directive = 'directive' in statement ? statement.directive : null;
576
- if (typeof directive === 'string' || containsReturn(statement)) {
653
+ if (typeof directive === 'string') {
654
+ this.reason = 'component-directive';
655
+ return undefined;
656
+ }
657
+ if (containsReturn(statement)) {
658
+ this.reason = 'component-return';
577
659
  return undefined;
578
660
  }
579
661
  rest.push(statement);
@@ -584,9 +666,11 @@ class Inliner {
584
666
  root = unwrap(fn.body);
585
667
  }
586
668
  if (root.type !== 'JSXElement' && root.type !== 'JSXFragment') {
669
+ this.reason = 'component-body';
587
670
  return undefined;
588
671
  }
589
672
  if (usesFunctionContext(this.context, fn.body)) {
673
+ this.reason = 'component-this';
590
674
  return undefined;
591
675
  }
592
676
 
@@ -595,15 +679,18 @@ class Inliner {
595
679
  let paramBinding: Binding | undefined;
596
680
  if (param) {
597
681
  if (param.type !== 'Identifier') {
682
+ this.reason = 'component-params';
598
683
  return undefined;
599
684
  }
600
685
  paramBinding = scope.bindings.get(param.name);
601
686
  if (!paramBinding || paramBinding.mutated) {
687
+ this.reason = 'props-use';
602
688
  return undefined;
603
689
  }
604
690
  }
605
691
  const views = this.analyzeViews(fn, scope, statements, paramBinding);
606
692
  if (!views) {
693
+ this.reason = 'view-use';
607
694
  return undefined;
608
695
  }
609
696
  const props: PropRead[] = [];
@@ -613,6 +700,7 @@ class Inliner {
613
700
  }
614
701
  const read = this.propRead(fn, reference);
615
702
  if (!read) {
703
+ this.reason = 'props-use';
616
704
  return undefined;
617
705
  }
618
706
  props.push(read);
@@ -641,6 +729,7 @@ class Inliner {
641
729
  }
642
730
  // A component that renders itself cannot be inlined into itself.
643
731
  if (target === binding) {
732
+ this.reason = 'component-recursive';
644
733
  return undefined;
645
734
  }
646
735
  if (target && isWithinScope(target.scope, scope)) {
@@ -780,24 +869,32 @@ class Inliner {
780
869
 
781
870
  const reads: ViewRead[] = [];
782
871
  const spreads: ViewSpread[] = [];
872
+ // `{...props}` spreads every prop, like a view of the props with nothing hidden.
873
+ const whole: View = { sources: [{ defaults: undefined, hidden: new Set(), only: undefined }] };
874
+ for (const reference of param?.references ?? []) {
875
+ const spread = this.spreadOnElement(reference);
876
+ if (spread) {
877
+ args.add(reference);
878
+ spreads.push({
879
+ attribute: spread.attribute,
880
+ element: spread.element,
881
+ view: whole,
882
+ repeated: this.isRepeated(fn, spread.attribute),
883
+ });
884
+ }
885
+ }
783
886
  for (const [binding, view] of views) {
784
887
  for (const reference of binding.references) {
785
888
  if (args.has(reference)) {
786
889
  continue;
787
890
  }
788
- const parent = this.context.parents.get(reference);
789
- const opening = parent ? this.context.parents.get(parent) : undefined;
790
- const element = opening ? this.context.parents.get(opening) : undefined;
791
- if (
792
- parent?.type === 'JSXSpreadAttribute' &&
793
- opening?.type === 'JSXOpeningElement' &&
794
- element?.type === 'JSXElement'
795
- ) {
891
+ const spread = this.spreadOnElement(reference);
892
+ if (spread) {
796
893
  spreads.push({
797
- attribute: parent,
798
- element,
894
+ attribute: spread.attribute,
895
+ element: spread.element,
799
896
  view,
800
- repeated: this.isRepeated(fn, parent),
897
+ repeated: this.isRepeated(fn, spread.attribute),
801
898
  });
802
899
  continue;
803
900
  }
@@ -811,6 +908,20 @@ class Inliner {
811
908
  return { bindings: new Set(views.keys()), arguments: args, declarations, reads, spreads };
812
909
  }
813
910
 
911
+ /** The spread a reference is the whole argument of, and the element it is on. */
912
+ private spreadOnElement(
913
+ reference: Node,
914
+ ): { attribute: JSXSpreadAttribute; element: JSXElement } | undefined {
915
+ const parent = this.context.parents.get(reference);
916
+ const opening = parent ? this.context.parents.get(parent) : undefined;
917
+ const element = opening ? this.context.parents.get(opening) : undefined;
918
+ return parent?.type === 'JSXSpreadAttribute' &&
919
+ opening?.type === 'JSXOpeningElement' &&
920
+ element?.type === 'JSXElement'
921
+ ? { attribute: parent, element }
922
+ : undefined;
923
+ }
924
+
814
925
  /**
815
926
  * The sources a view argument stands for: the props, a view, or a nested
816
927
  * `mergeProps()` call.
@@ -1195,19 +1306,23 @@ class Inliner {
1195
1306
  private prepare(component: Component, element: JSXElement): PreparedSite | undefined {
1196
1307
  const attributes = attributesOf(element);
1197
1308
  if (!attributes) {
1309
+ this.reason = callSiteReason(element);
1198
1310
  return undefined;
1199
1311
  }
1200
1312
  const hasChildren = element.children.some((child) => !isInsignificant(child));
1201
1313
  if (hasChildren && attributes.has('children')) {
1314
+ this.reason = 'call-children';
1202
1315
  return undefined;
1203
1316
  }
1204
1317
  for (const attribute of attributes.values()) {
1205
1318
  if (attribute.value && usesFunctionContext(this.context, attribute.value)) {
1319
+ this.reason = 'call-this';
1206
1320
  return undefined;
1207
1321
  }
1208
1322
  }
1209
1323
  for (const child of element.children) {
1210
1324
  if (usesFunctionContext(this.context, child)) {
1325
+ this.reason = 'call-this';
1211
1326
  return undefined;
1212
1327
  }
1213
1328
  }
@@ -1215,6 +1330,7 @@ class Inliner {
1215
1330
  // What each view read and spread resolves to at this call site.
1216
1331
  const views = this.resolveViews(component, element, attributes, hasChildren);
1217
1332
  if (!views) {
1333
+ this.reason = 'view-conflict';
1218
1334
  return undefined;
1219
1335
  }
1220
1336
 
@@ -1249,6 +1365,7 @@ class Inliner {
1249
1365
  value = this.propValue(attributes.get(name), reads, fresh, snapshots);
1250
1366
  }
1251
1367
  if (!value) {
1368
+ this.reason = 'prop-value';
1252
1369
  return undefined;
1253
1370
  }
1254
1371
  values.set(name, value);
@@ -1258,6 +1375,7 @@ class Inliner {
1258
1375
  const scope = scopeAt(this.context.scopes, this.context.parents, element);
1259
1376
  for (const reference of component.outer) {
1260
1377
  if (lookup(scope, reference.name) !== reference.binding) {
1378
+ this.reason = 'shadowed';
1261
1379
  return undefined;
1262
1380
  }
1263
1381
  }
@@ -1265,11 +1383,13 @@ class Inliner {
1265
1383
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
1266
1384
  const host = hoisted ? this.hostOf(element) : undefined;
1267
1385
  if (hoisted && !host) {
1386
+ this.reason = 'no-host';
1268
1387
  return undefined;
1269
1388
  }
1270
1389
 
1271
1390
  const contextTexts = this.contextTexts(component, element);
1272
1391
  if (!contextTexts) {
1392
+ this.reason = 'context-value';
1273
1393
  return undefined;
1274
1394
  }
1275
1395
 
@@ -1909,13 +2029,20 @@ class Inliner {
1909
2029
 
1910
2030
  run(): void {
1911
2031
  const components: Component[] = [];
2032
+ const rejected = new Map<Binding, string>();
1912
2033
  for (const binding of this.context.scopes.root.bindings.values()) {
2034
+ this.reason = 'not-a-function';
1913
2035
  const component = this.analyzeComponent(binding);
1914
2036
  if (component) {
1915
2037
  components.push(component);
2038
+ } else {
2039
+ rejected.set(binding, this.reason);
1916
2040
  }
1917
2041
  }
2042
+ // Why each call site that stays a call was not inlined, for the report.
2043
+ const siteReasons = new Map<JSXElement, string>();
1918
2044
  if (components.length === 0) {
2045
+ this.report(rejected, siteReasons);
1919
2046
  return;
1920
2047
  }
1921
2048
 
@@ -1962,6 +2089,9 @@ class Inliner {
1962
2089
  inlined.set(site.component, elements);
1963
2090
  } else {
1964
2091
  otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
2092
+ if (this.context.skips && !postponed) {
2093
+ siteReasons.set(site.element, this.siteReason(site, worthwhile));
2094
+ }
1965
2095
  }
1966
2096
  // A component holding a call that was inlined now, or postponed to the
1967
2097
  // next pass, waits too. A copy made now would copy the call as it is.
@@ -1975,6 +2105,7 @@ class Inliner {
1975
2105
  }
1976
2106
 
1977
2107
  this.writeHosts();
2108
+ this.report(rejected, siteReasons);
1978
2109
 
1979
2110
  // A component nothing uses anymore is removed. A copy made in this pass
1980
2111
  // comes from the original code, so a component used inside a copied
@@ -1993,6 +2124,84 @@ class Inliner {
1993
2124
  }
1994
2125
  }
1995
2126
 
2127
+ /**
2128
+ * Why a call site of a component was not inlined. A component that is
2129
+ * not worth inlining is only turned down for its size when the call site
2130
+ * could be inlined.
2131
+ */
2132
+ private siteReason(site: CallSite, worthwhile: ReadonlySet<Component>): string {
2133
+ if (worthwhile.has(site.component)) {
2134
+ return this.reason;
2135
+ }
2136
+ this.scratchNames = new Set(this.context.scopes.names);
2137
+ try {
2138
+ return this.prepare(site.component, site.element) ? 'size' : this.reason;
2139
+ } finally {
2140
+ this.scratchNames = undefined;
2141
+ }
2142
+ }
2143
+
2144
+ /**
2145
+ * Records each component call that stays a call, and why. Solid's
2146
+ * built-ins and context providers are left out. The provider pass
2147
+ * reports providers.
2148
+ */
2149
+ private report(
2150
+ rejected: ReadonlyMap<Binding, string>,
2151
+ siteReasons: ReadonlyMap<JSXElement, string>,
2152
+ ): void {
2153
+ if (!this.context.skips) {
2154
+ return;
2155
+ }
2156
+ const visit = (node: Node): void => {
2157
+ if (node.type === 'JSXElement') {
2158
+ const reason = this.callReason(node, rejected, siteReasons);
2159
+ if (reason) {
2160
+ recordSkip(this.context, 'inline', reason.reason, reason.name);
2161
+ }
2162
+ }
2163
+ forEachChild(node, visit);
2164
+ };
2165
+ visit(this.context.program);
2166
+ }
2167
+
2168
+ private callReason(
2169
+ element: JSXElement,
2170
+ rejected: ReadonlyMap<Binding, string>,
2171
+ siteReasons: ReadonlyMap<JSXElement, string>,
2172
+ ): { reason: string; name: string } | undefined {
2173
+ const tag = element.openingElement.name;
2174
+ // The provider pass reports providers.
2175
+ if (providerContext(this.context, tag)) {
2176
+ return undefined;
2177
+ }
2178
+ if (tag.type === 'JSXMemberExpression') {
2179
+ return { reason: 'member-tag', name: textOf(this.context, tag) };
2180
+ }
2181
+ if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
2182
+ return undefined;
2183
+ }
2184
+ const { name } = tag;
2185
+ const binding = this.context.scopes.references.get(tag);
2186
+ const identity = binding ? solidExport(binding, this.context.options) : name;
2187
+ if (identity !== undefined && this.context.options.builtIns.has(identity)) {
2188
+ return undefined;
2189
+ }
2190
+ if (!binding) {
2191
+ return { reason: 'global', name };
2192
+ }
2193
+ if (binding.kind === 'import') {
2194
+ // A chunk imports other chunks through placeholders the bundler fills in later.
2195
+ const source = (binding.source ?? '').replaceAll(/!~\{\w+\}~/g, '*');
2196
+ return { reason: 'imported', name: `${name} (${source})` };
2197
+ }
2198
+ if (binding.scope.parent !== undefined) {
2199
+ return { reason: 'nested', name };
2200
+ }
2201
+ const reason = rejected.get(binding) ?? siteReasons.get(element);
2202
+ return reason === undefined ? undefined : { reason, name };
2203
+ }
2204
+
1996
2205
  /**
1997
2206
  * The whitespace before `position` on its line.
1998
2207
  */