solid-optimizer 0.3.0 → 0.4.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.
@@ -1,6 +1,6 @@
1
1
  import remapping from "@jridgewell/remapping";
2
2
  import MagicString from "magic-string";
3
- import { parseSync, visitorKeys } from "oxc-parser";
3
+ import { parseSync, rawTransferSupported, visitorKeys } from "oxc-parser";
4
4
  //#region src/ast.ts
5
5
  /**
6
6
  * Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
@@ -60,12 +60,19 @@ function collectParents(program) {
60
60
  visit(program);
61
61
  return parents;
62
62
  }
63
+ /**
64
+ * Raw transfer hands the AST over in shared memory instead of as JSON, which
65
+ * is several times faster to read. It is not in the options' type yet.
66
+ */
67
+ const RAW_TRANSFER = rawTransferSupported();
63
68
  function parse(filename, code) {
64
- const result = parseSync(filename, code, {
69
+ const options = {
65
70
  lang: languageOf(filename),
66
71
  sourceType: "module",
67
- preserveParens: true
68
- });
72
+ preserveParens: true,
73
+ experimentalRawTransfer: RAW_TRANSFER
74
+ };
75
+ const result = parseSync(filename, code, options);
69
76
  const error = result.errors.at(0);
70
77
  if (error) throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
71
78
  return result.program;
@@ -202,6 +209,16 @@ function solidExport(binding, options) {
202
209
  if (binding.kind === "import" && binding.source !== void 0 && options.moduleSources.includes(binding.source)) return binding.imported;
203
210
  }
204
211
  /**
212
+ * Records that a pass left something alone, when the caller asked for a report.
213
+ */
214
+ function recordSkip(context, pass, reason, name) {
215
+ context.skips?.push({
216
+ pass,
217
+ reason,
218
+ name
219
+ });
220
+ }
221
+ /**
205
222
  * The current text of `node`, including the edits made inside it so far.
206
223
  */
207
224
  function textOf(context, node) {
@@ -851,16 +868,32 @@ var ConstantTable = class {
851
868
  /**
852
869
  * The modules `program` imports or re-exports bindings from. Their
853
870
  * constant exports are what `importedConstants` can hold.
871
+ *
872
+ * An import counts only when one of its bindings is read as a value. A
873
+ * binding read only in types has no constant to fold, and TypeScript drops its
874
+ * import, so the module it names may not even build for this target.
854
875
  */
855
- function namedImportSources(program) {
876
+ function namedImportSources(program, scopes = analyzeScopes(program)) {
856
877
  const sources = /* @__PURE__ */ new Set();
857
878
  for (const statement of program.body) if (statement.type === "ImportDeclaration") {
858
- if (statement.importKind !== "type" && statement.specifiers.some((specifier) => !(specifier.type === "ImportSpecifier" && specifier.importKind === "type"))) sources.add(statement.source.value);
879
+ if (statement.importKind !== "type" && readsAsValue(statement.specifiers, scopes)) sources.add(statement.source.value);
859
880
  } else if (statement.type === "ExportNamedDeclaration" && statement.source || statement.type === "ExportAllDeclaration" && !statement.exported) {
860
881
  if (statement.exportKind !== "type" && statement.source) sources.add(statement.source.value);
861
882
  }
862
883
  return [...sources];
863
884
  }
885
+ /**
886
+ * Whether the module reads one of these import bindings as a value. Scope
887
+ * analysis skips type positions, so a binding used only in types has no
888
+ * references.
889
+ */
890
+ function readsAsValue(specifiers, scopes) {
891
+ for (const specifier of specifiers) {
892
+ if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") continue;
893
+ if (scopes.root.bindings.get(specifier.local.name)?.references.length) return true;
894
+ }
895
+ return false;
896
+ }
864
897
  function exportName(node) {
865
898
  if (node.type === "Identifier") return node.name;
866
899
  if (node.type === "Literal" && typeof node.value === "string") return node.value;
@@ -1683,6 +1716,10 @@ function isAccessor(context, binding) {
1683
1716
  */
1684
1717
  function isClosed(context, root, options) {
1685
1718
  let closed = true;
1719
+ const open = (reason) => {
1720
+ closed = false;
1721
+ options.onOpen?.(reason);
1722
+ };
1686
1723
  const visit = (node) => {
1687
1724
  if (!closed) return;
1688
1725
  switch (node.type) {
@@ -1691,32 +1728,34 @@ function isClosed(context, root, options) {
1691
1728
  break;
1692
1729
  case "JSXSpreadAttribute":
1693
1730
  if (!isOwnView(context, node.argument, options.props, 0)) {
1694
- closed = false;
1731
+ open("open-spread");
1695
1732
  return;
1696
1733
  }
1697
1734
  break;
1698
- case "JSXSpreadChild":
1699
1735
  case "NewExpression":
1736
+ open("open-new");
1737
+ return;
1738
+ case "JSXSpreadChild":
1700
1739
  case "TaggedTemplateExpression":
1701
1740
  case "ImportExpression":
1702
- closed = false;
1741
+ open("open-other");
1703
1742
  return;
1704
1743
  case "JSXElement":
1705
1744
  if (!isVisibleElement(context, node, options)) {
1706
- closed = false;
1745
+ open("open-component");
1707
1746
  return;
1708
1747
  }
1709
1748
  break;
1710
1749
  case "CallExpression":
1711
1750
  if (!isVisibleCall(context, node, options)) {
1712
- closed = false;
1751
+ open("open-call");
1713
1752
  return;
1714
1753
  }
1715
1754
  break;
1716
1755
  case "Identifier": {
1717
1756
  const binding = context.scopes.references.get(node);
1718
1757
  if (binding && runsOutsideCode(context, binding, root, options, 0)) {
1719
- closed = false;
1758
+ open("open-outer");
1720
1759
  return;
1721
1760
  }
1722
1761
  break;
@@ -1948,25 +1987,63 @@ var ProviderRemover = class {
1948
1987
  run() {
1949
1988
  for (const provider of findProviders(this.context)) {
1950
1989
  const { element } = provider;
1951
- if (!this.removed.some((other) => element.start < other.end && other.start < element.end) && !hasNestedProvider(this.context, provider) && this.tryRemove(provider)) this.removed.push(element);
1990
+ if (this.removed.some((other) => element.start < other.end && other.start < element.end)) continue;
1991
+ if (hasNestedProvider(this.context, provider)) this.skip(provider, "provider-nested");
1992
+ else if (this.tryRemove(provider)) this.removed.push(element);
1952
1993
  }
1994
+ if (this.context.skips) this.reportShapes();
1995
+ }
1996
+ skip(provider, reason) {
1997
+ recordSkip(this.context, "contexts", reason, provider.context.name);
1998
+ }
1999
+ /**
2000
+ * Records each context tag that is not a provider this pass understands,
2001
+ * such as one with more attributes than `value`.
2002
+ */
2003
+ reportShapes() {
2004
+ const visit = (node) => {
2005
+ if (node.type === "JSXElement") {
2006
+ const binding = providerContext(this.context, node.openingElement.name);
2007
+ if (binding && !providerOf(this.context, node)) {
2008
+ const { attributes } = node.openingElement;
2009
+ const attribute = attributes.at(0);
2010
+ const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
2011
+ recordSkip(this.context, "contexts", onlyValue && !node.openingElement.selfClosing ? "provider-undefined" : "provider-shape", binding.name);
2012
+ }
2013
+ }
2014
+ forEachChild(node, visit);
2015
+ };
2016
+ visit(this.context.program);
1953
2017
  }
1954
2018
  tryRemove(provider) {
1955
2019
  const { element } = provider;
1956
2020
  const children = element.children;
2021
+ let reason = "open-other";
1957
2022
  if (!children.every((child) => isClosed(this.context, child, {
1958
2023
  component: () => false,
1959
- contextValue: (target) => target === provider.context ? provider.value : void 0
1960
- }))) return false;
2024
+ contextValue: (target) => target === provider.context ? provider.value : void 0,
2025
+ onOpen: (open) => {
2026
+ reason = open;
2027
+ }
2028
+ }))) {
2029
+ this.skip(provider, reason);
2030
+ return false;
2031
+ }
1961
2032
  const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
1962
2033
  const texts = [];
1963
2034
  for (const read of reads) {
1964
2035
  const text = stableValueText(this.context, provider, read);
1965
- if (text === void 0) return false;
2036
+ if (text === void 0) {
2037
+ this.skip(provider, "provider-value");
2038
+ return false;
2039
+ }
1966
2040
  texts.push(text);
1967
2041
  }
1968
2042
  if (reads.length === 0 && !isSideEffectFree(provider.value)) {
1969
- if (stableValueText(this.context, provider, element) === void 0) return false;
2043
+ if (stableValueText(this.context, provider, element) === void 0) {
2044
+ this.skip(provider, "provider-value");
2045
+ return false;
2046
+ }
1970
2047
  }
1971
2048
  const { s } = this.context;
1972
2049
  for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
@@ -2170,7 +2247,8 @@ function copySize(filename, header, copy, inTemplate, options) {
2170
2247
  program,
2171
2248
  parents: collectParents(program),
2172
2249
  scopes: analyzeScopes(program),
2173
- options
2250
+ options,
2251
+ skips: void 0
2174
2252
  })) break;
2175
2253
  code = s.toString();
2176
2254
  }
@@ -2202,7 +2280,8 @@ function copySize(filename, header, copy, inTemplate, options) {
2202
2280
  *
2203
2281
  * - It is declared once at the top level of the module, with a capitalized
2204
2282
  * name, as a function declaration or a `const` bound to a function.
2205
- * - It takes no parameter, or one identifier that is only read as `props.name`.
2283
+ * - It takes no parameter, or one identifier that is only read as `props.name`,
2284
+ * or spread onto an element as `{...props}`.
2206
2285
  * - Its body is statements followed by one final `return` of JSX, or an arrow
2207
2286
  * function whose body is JSX.
2208
2287
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
@@ -2431,6 +2510,20 @@ function attributesOf(element) {
2431
2510
  }
2432
2511
  return attributes;
2433
2512
  }
2513
+ /**
2514
+ * Why `attributesOf` turns down a call site, for the report.
2515
+ */
2516
+ function callSiteReason(element) {
2517
+ const names = /* @__PURE__ */ new Set();
2518
+ for (const attribute of element.openingElement.attributes) {
2519
+ if (attribute.type !== "JSXAttribute") return "call-spread";
2520
+ if (attribute.name.type !== "JSXIdentifier") return "call-namespace";
2521
+ if (attribute.name.name === "ref") return "call-ref";
2522
+ if (names.has(attribute.name.name)) break;
2523
+ names.add(attribute.name.name);
2524
+ }
2525
+ return "call-duplicate";
2526
+ }
2434
2527
  function isAssignmentTarget(context, node) {
2435
2528
  const parent = context.parents.get(node);
2436
2529
  if (!parent) return false;
@@ -2455,6 +2548,28 @@ var Inliner = class {
2455
2548
  this.changed = false;
2456
2549
  this.hosts = /* @__PURE__ */ new Map();
2457
2550
  this.transparent = /* @__PURE__ */ new Map();
2551
+ this.reason = "";
2552
+ }
2553
+ /**
2554
+ * Whether a binding holds a component made by Solid's `lazy`, for the
2555
+ * report. In a chunk, `lazy` can be a local declaration under another
2556
+ * name, so a call given a function with a dynamic import counts too.
2557
+ */
2558
+ isLazy(binding) {
2559
+ const { declaration } = binding;
2560
+ const init = declaration.type === "VariableDeclarator" && declaration.init ? unwrap(declaration.init) : void 0;
2561
+ if (init?.type !== "CallExpression" || init.callee.type !== "Identifier") return false;
2562
+ const callee = this.context.scopes.references.get(init.callee);
2563
+ if (callee && solidExport(callee, this.context.options) === "lazy") return true;
2564
+ const loader = init.arguments.at(0);
2565
+ if (!loader || !isFunctionNode(loader)) return false;
2566
+ let imports = false;
2567
+ const visit = (node) => {
2568
+ if (node.type === "ImportExpression") imports = true;
2569
+ if (!imports) forEachChild(node, visit);
2570
+ };
2571
+ visit(loader);
2572
+ return imports;
2458
2573
  }
2459
2574
  scopeOf(fn) {
2460
2575
  return this.context.scopes.scopes.get(fn);
@@ -2470,41 +2585,87 @@ var Inliner = class {
2470
2585
  if (statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program") return statement;
2471
2586
  }
2472
2587
  analyzeComponent(binding) {
2473
- if (!isCapitalized(binding.name) || binding.mutated) return;
2588
+ if (!isCapitalized(binding.name)) return;
2589
+ if (binding.mutated) {
2590
+ this.reason = "component-mutated";
2591
+ return;
2592
+ }
2474
2593
  const fn = functionOf(binding, this.context.options);
2475
- if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) return;
2594
+ if (!fn?.body) {
2595
+ this.reason = this.isLazy(binding) ? "lazy" : "not-a-function";
2596
+ return;
2597
+ }
2598
+ if (fn.async || fn.generator) {
2599
+ this.reason = "component-async";
2600
+ return;
2601
+ }
2602
+ if (fn.params.length > 1) {
2603
+ this.reason = "component-params";
2604
+ return;
2605
+ }
2476
2606
  const scope = this.scopeOf(fn);
2477
- if (!scope) return;
2607
+ if (!scope) {
2608
+ this.reason = "not-a-function";
2609
+ return;
2610
+ }
2478
2611
  let statements = [];
2479
2612
  let root;
2480
2613
  if (fn.body.type === "BlockStatement") {
2481
2614
  const body = fn.body.body;
2482
2615
  const last = body.at(-1);
2483
- if (last?.type !== "ReturnStatement" || !last.argument) return;
2616
+ if (last?.type !== "ReturnStatement" || !last.argument) {
2617
+ this.reason = "component-body";
2618
+ return;
2619
+ }
2484
2620
  const rest = [];
2485
2621
  for (const statement of body.slice(0, -1)) {
2486
- if (typeof ("directive" in statement ? statement.directive : null) === "string" || containsReturn(statement)) return;
2622
+ if (typeof ("directive" in statement ? statement.directive : null) === "string") {
2623
+ this.reason = "component-directive";
2624
+ return;
2625
+ }
2626
+ if (containsReturn(statement)) {
2627
+ this.reason = "component-return";
2628
+ return;
2629
+ }
2487
2630
  rest.push(statement);
2488
2631
  }
2489
2632
  statements = rest;
2490
2633
  root = unwrap(last.argument);
2491
2634
  } else root = unwrap(fn.body);
2492
- if (root.type !== "JSXElement" && root.type !== "JSXFragment") return;
2493
- if (usesFunctionContext(this.context, fn.body)) return;
2635
+ if (root.type !== "JSXElement" && root.type !== "JSXFragment") {
2636
+ this.reason = "component-body";
2637
+ return;
2638
+ }
2639
+ if (usesFunctionContext(this.context, fn.body)) {
2640
+ this.reason = "component-this";
2641
+ return;
2642
+ }
2494
2643
  const param = fn.params.at(0);
2495
2644
  let paramBinding;
2496
2645
  if (param) {
2497
- if (param.type !== "Identifier") return;
2646
+ if (param.type !== "Identifier") {
2647
+ this.reason = "component-params";
2648
+ return;
2649
+ }
2498
2650
  paramBinding = scope.bindings.get(param.name);
2499
- if (!paramBinding || paramBinding.mutated) return;
2651
+ if (!paramBinding || paramBinding.mutated) {
2652
+ this.reason = "props-use";
2653
+ return;
2654
+ }
2500
2655
  }
2501
2656
  const views = this.analyzeViews(fn, scope, statements, paramBinding);
2502
- if (!views) return;
2657
+ if (!views) {
2658
+ this.reason = "view-use";
2659
+ return;
2660
+ }
2503
2661
  const props = [];
2504
2662
  for (const reference of paramBinding?.references ?? []) {
2505
2663
  if (views.arguments.has(reference)) continue;
2506
2664
  const read = this.propRead(fn, reference);
2507
- if (!read) return;
2665
+ if (!read) {
2666
+ this.reason = "props-use";
2667
+ return;
2668
+ }
2508
2669
  props.push(read);
2509
2670
  }
2510
2671
  statements = statements.filter((statement) => !views.declarations.includes(statement));
@@ -2518,7 +2679,10 @@ var Inliner = class {
2518
2679
  const outer = [];
2519
2680
  for (const [reference, target] of this.context.scopes.references) {
2520
2681
  if (reference.start < fn.start || reference.end > fn.end) continue;
2521
- if (target === binding) return;
2682
+ if (target === binding) {
2683
+ this.reason = "component-recursive";
2684
+ return;
2685
+ }
2522
2686
  if (target && isWithinScope(target.scope, scope)) continue;
2523
2687
  if (reference.type === "Identifier" || reference.type === "JSXIdentifier") outer.push({
2524
2688
  name: reference.name,
@@ -2599,17 +2763,32 @@ var Inliner = class {
2599
2763
  }
2600
2764
  const reads = [];
2601
2765
  const spreads = [];
2766
+ const whole = { sources: [{
2767
+ defaults: void 0,
2768
+ hidden: /* @__PURE__ */ new Set(),
2769
+ only: void 0
2770
+ }] };
2771
+ for (const reference of param?.references ?? []) {
2772
+ const spread = this.spreadOnElement(reference);
2773
+ if (spread) {
2774
+ args.add(reference);
2775
+ spreads.push({
2776
+ attribute: spread.attribute,
2777
+ element: spread.element,
2778
+ view: whole,
2779
+ repeated: this.isRepeated(fn, spread.attribute)
2780
+ });
2781
+ }
2782
+ }
2602
2783
  for (const [binding, view] of views) for (const reference of binding.references) {
2603
2784
  if (args.has(reference)) continue;
2604
- const parent = this.context.parents.get(reference);
2605
- const opening = parent ? this.context.parents.get(parent) : void 0;
2606
- const element = opening ? this.context.parents.get(opening) : void 0;
2607
- if (parent?.type === "JSXSpreadAttribute" && opening?.type === "JSXOpeningElement" && element?.type === "JSXElement") {
2785
+ const spread = this.spreadOnElement(reference);
2786
+ if (spread) {
2608
2787
  spreads.push({
2609
- attribute: parent,
2610
- element,
2788
+ attribute: spread.attribute,
2789
+ element: spread.element,
2611
2790
  view,
2612
- repeated: this.isRepeated(fn, parent)
2791
+ repeated: this.isRepeated(fn, spread.attribute)
2613
2792
  });
2614
2793
  continue;
2615
2794
  }
@@ -2628,6 +2807,16 @@ var Inliner = class {
2628
2807
  spreads
2629
2808
  };
2630
2809
  }
2810
+ /** The spread a reference is the whole argument of, and the element it is on. */
2811
+ spreadOnElement(reference) {
2812
+ const parent = this.context.parents.get(reference);
2813
+ const opening = parent ? this.context.parents.get(parent) : void 0;
2814
+ const element = opening ? this.context.parents.get(opening) : void 0;
2815
+ return parent?.type === "JSXSpreadAttribute" && opening?.type === "JSXOpeningElement" && element?.type === "JSXElement" ? {
2816
+ attribute: parent,
2817
+ element
2818
+ } : void 0;
2819
+ }
2631
2820
  /**
2632
2821
  * The sources a view argument stands for: the props, a view, or a nested
2633
2822
  * `mergeProps()` call.
@@ -2902,13 +3091,28 @@ var Inliner = class {
2902
3091
  */
2903
3092
  prepare(component, element) {
2904
3093
  const attributes = attributesOf(element);
2905
- if (!attributes) return;
3094
+ if (!attributes) {
3095
+ this.reason = callSiteReason(element);
3096
+ return;
3097
+ }
2906
3098
  const hasChildren = element.children.some((child) => !isInsignificant(child));
2907
- if (hasChildren && attributes.has("children")) return;
2908
- for (const attribute of attributes.values()) if (attribute.value && usesFunctionContext(this.context, attribute.value)) return;
2909
- for (const child of element.children) if (usesFunctionContext(this.context, child)) return;
3099
+ if (hasChildren && attributes.has("children")) {
3100
+ this.reason = "call-children";
3101
+ return;
3102
+ }
3103
+ for (const attribute of attributes.values()) if (attribute.value && usesFunctionContext(this.context, attribute.value)) {
3104
+ this.reason = "call-this";
3105
+ return;
3106
+ }
3107
+ for (const child of element.children) if (usesFunctionContext(this.context, child)) {
3108
+ this.reason = "call-this";
3109
+ return;
3110
+ }
2910
3111
  const views = this.resolveViews(component, element, attributes, hasChildren);
2911
- if (!views) return;
3112
+ if (!views) {
3113
+ this.reason = "view-conflict";
3114
+ return;
3115
+ }
2912
3116
  const snapshots = [];
2913
3117
  const fresh = (base) => this.freshName(base);
2914
3118
  const readsByName = /* @__PURE__ */ new Map();
@@ -2925,16 +3129,28 @@ var Inliner = class {
2925
3129
  let value;
2926
3130
  if (name === "children" && !attributes.has("children")) value = this.childrenValue(element, reads, fresh, snapshots);
2927
3131
  else value = this.propValue(attributes.get(name), reads, fresh, snapshots);
2928
- if (!value) return;
3132
+ if (!value) {
3133
+ this.reason = "prop-value";
3134
+ return;
3135
+ }
2929
3136
  values.set(name, value);
2930
3137
  }
2931
3138
  const scope = scopeAt(this.context.scopes, this.context.parents, element);
2932
- for (const reference of component.outer) if (lookup(scope, reference.name) !== reference.binding) return;
3139
+ for (const reference of component.outer) if (lookup(scope, reference.name) !== reference.binding) {
3140
+ this.reason = "shadowed";
3141
+ return;
3142
+ }
2933
3143
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
2934
3144
  const host = hoisted ? this.hostOf(element) : void 0;
2935
- if (hoisted && !host) return;
3145
+ if (hoisted && !host) {
3146
+ this.reason = "no-host";
3147
+ return;
3148
+ }
2936
3149
  const contextTexts = this.contextTexts(component, element);
2937
- if (!contextTexts) return;
3150
+ if (!contextTexts) {
3151
+ this.reason = "context-value";
3152
+ return;
3153
+ }
2938
3154
  const copy = new MagicString(this.context.code);
2939
3155
  this.renameLocals(copy, component, fresh);
2940
3156
  this.substituteProps(copy, views.propReads, values);
@@ -3356,11 +3572,18 @@ var Inliner = class {
3356
3572
  }
3357
3573
  run() {
3358
3574
  const components = [];
3575
+ const rejected = /* @__PURE__ */ new Map();
3359
3576
  for (const binding of this.context.scopes.root.bindings.values()) {
3577
+ this.reason = "not-a-function";
3360
3578
  const component = this.analyzeComponent(binding);
3361
3579
  if (component) components.push(component);
3580
+ else rejected.set(binding, this.reason);
3581
+ }
3582
+ const siteReasons = /* @__PURE__ */ new Map();
3583
+ if (components.length === 0) {
3584
+ this.report(rejected, siteReasons);
3585
+ return;
3362
3586
  }
3363
- if (components.length === 0) return;
3364
3587
  const sites = [];
3365
3588
  const otherUses = /* @__PURE__ */ new Map();
3366
3589
  for (const component of components) {
@@ -3391,12 +3614,16 @@ var Inliner = class {
3391
3614
  const elements = inlined.get(site.component) ?? [];
3392
3615
  elements.push(site.element);
3393
3616
  inlined.set(site.component, elements);
3394
- } else otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
3617
+ } else {
3618
+ otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
3619
+ if (this.context.skips && !postponed) siteReasons.set(site.element, this.siteReason(site, worthwhile));
3620
+ }
3395
3621
  if (done || postponed) {
3396
3622
  for (const component of components) if (isWithin(site.element, component.fn)) changedBodies.add(component);
3397
3623
  }
3398
3624
  }
3399
3625
  this.writeHosts();
3626
+ this.report(rejected, siteReasons);
3400
3627
  const copied = [...inlined.keys()];
3401
3628
  for (const [component, elements] of inlined) {
3402
3629
  const insideCopy = elements.some((element) => copied.some((other) => other !== component && element.start >= other.fn.start && element.end <= other.fn.end));
@@ -3404,6 +3631,66 @@ var Inliner = class {
3404
3631
  }
3405
3632
  }
3406
3633
  /**
3634
+ * Why a call site of a component was not inlined. A component that is
3635
+ * not worth inlining is only turned down for its size when the call site
3636
+ * could be inlined.
3637
+ */
3638
+ siteReason(site, worthwhile) {
3639
+ if (worthwhile.has(site.component)) return this.reason;
3640
+ this.scratchNames = new Set(this.context.scopes.names);
3641
+ try {
3642
+ return this.prepare(site.component, site.element) ? "size" : this.reason;
3643
+ } finally {
3644
+ this.scratchNames = void 0;
3645
+ }
3646
+ }
3647
+ /**
3648
+ * Records each component call that stays a call, and why. Solid's
3649
+ * built-ins and context providers are left out. The provider pass
3650
+ * reports providers.
3651
+ */
3652
+ report(rejected, siteReasons) {
3653
+ if (!this.context.skips) return;
3654
+ const visit = (node) => {
3655
+ if (node.type === "JSXElement") {
3656
+ const reason = this.callReason(node, rejected, siteReasons);
3657
+ if (reason) recordSkip(this.context, "inline", reason.reason, reason.name);
3658
+ }
3659
+ forEachChild(node, visit);
3660
+ };
3661
+ visit(this.context.program);
3662
+ }
3663
+ callReason(element, rejected, siteReasons) {
3664
+ const tag = element.openingElement.name;
3665
+ if (providerContext(this.context, tag)) return;
3666
+ if (tag.type === "JSXMemberExpression") return {
3667
+ reason: "member-tag",
3668
+ name: textOf(this.context, tag)
3669
+ };
3670
+ if (tag.type !== "JSXIdentifier" || !isCapitalized(tag.name)) return;
3671
+ const { name } = tag;
3672
+ const binding = this.context.scopes.references.get(tag);
3673
+ const identity = binding ? solidExport(binding, this.context.options) : name;
3674
+ if (identity !== void 0 && this.context.options.builtIns.has(identity)) return;
3675
+ if (!binding) return {
3676
+ reason: "global",
3677
+ name
3678
+ };
3679
+ if (binding.kind === "import") return {
3680
+ reason: "imported",
3681
+ name: `${name} (${(binding.source ?? "").replaceAll(/!~\{\w+\}~/g, "*")})`
3682
+ };
3683
+ if (binding.scope.parent !== void 0) return {
3684
+ reason: "nested",
3685
+ name
3686
+ };
3687
+ const reason = rejected.get(binding) ?? siteReasons.get(element);
3688
+ return reason === void 0 ? void 0 : {
3689
+ reason,
3690
+ name
3691
+ };
3692
+ }
3693
+ /**
3407
3694
  * The whitespace before `position` on its line.
3408
3695
  */
3409
3696
  indentationAt(position) {
@@ -3478,6 +3765,7 @@ var MemoInliner = class {
3478
3765
  constructor(context) {
3479
3766
  this.context = context;
3480
3767
  this.changed = false;
3768
+ this.reason = "";
3481
3769
  }
3482
3770
  isCreateMemo(callee) {
3483
3771
  if (callee.type !== "Identifier") return false;
@@ -3494,19 +3782,35 @@ var MemoInliner = class {
3494
3782
  const compute = args.at(0);
3495
3783
  const initialValue = args.at(1);
3496
3784
  const options = args.at(2);
3497
- if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) return;
3785
+ if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) {
3786
+ this.reason = "memo-computation";
3787
+ return;
3788
+ }
3498
3789
  const fn = unwrap(compute);
3499
- if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return;
3790
+ if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") {
3791
+ this.reason = "memo-computation";
3792
+ return;
3793
+ }
3500
3794
  const expression = returnedExpression(fn);
3501
- if (!expression) return;
3795
+ if (!expression) {
3796
+ this.reason = "memo-computation";
3797
+ return;
3798
+ }
3502
3799
  let alwaysNotifies = false;
3503
3800
  if (options !== void 0) {
3504
- if (options.type !== "ObjectExpression" || options.properties.length !== 1) return;
3801
+ if (options.type !== "ObjectExpression" || options.properties.length !== 1) {
3802
+ this.reason = "memo-options";
3803
+ return;
3804
+ }
3505
3805
  const property = options.properties.at(0);
3506
- if (property?.type !== "Property" || property.computed || property.key.type !== "Identifier" || property.key.name !== "equals" || property.value.type !== "Literal" || property.value.value !== false) return;
3806
+ if (property?.type !== "Property" || property.computed || property.key.type !== "Identifier" || property.key.name !== "equals" || property.value.type !== "Literal" || property.value.value !== false) {
3807
+ this.reason = "memo-options";
3808
+ return;
3809
+ }
3507
3810
  alwaysNotifies = true;
3508
3811
  }
3509
- return alwaysNotifies || isFresh(expression) ? expression : void 0;
3812
+ if (alwaysNotifies || isFresh(expression)) return expression;
3813
+ if (unwrap(expression).type === "Literal") this.reason = "memo-constant";
3510
3814
  }
3511
3815
  /**
3512
3816
  * The function a node runs in.
@@ -3601,17 +3905,40 @@ var MemoInliner = class {
3601
3905
  return true;
3602
3906
  }
3603
3907
  tryInline(declarator, binding) {
3908
+ this.reason = "";
3909
+ if (this.inline(declarator, binding) === void 0 && this.reason !== "") recordSkip(this.context, "memos", this.reason, binding.name);
3910
+ }
3911
+ inline(declarator, binding) {
3604
3912
  const expression = this.unstableComputation(declarator);
3605
- if (!expression || binding.references.length !== 1) return;
3913
+ if (!expression) return;
3914
+ if (binding.references.length !== 1) {
3915
+ this.reason = "memo-reads";
3916
+ return;
3917
+ }
3606
3918
  const statement = this.context.parents.get(declarator);
3607
3919
  const fn = this.functionOf(declarator);
3608
- if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) return;
3920
+ if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) {
3921
+ this.reason = "memo-declaration";
3922
+ return;
3923
+ }
3609
3924
  const reference = binding.references.at(0);
3610
3925
  const call = reference ? this.context.parents.get(reference) : void 0;
3611
- if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || !this.isSingleTrackedRead(call, fn) || !this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) return;
3926
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0) {
3927
+ this.reason = "memo-read";
3928
+ return;
3929
+ }
3930
+ if (!this.isSingleTrackedRead(call, fn)) {
3931
+ this.reason = "memo-read-place";
3932
+ return;
3933
+ }
3934
+ if (!this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) {
3935
+ this.reason = "memo-scope";
3936
+ return;
3937
+ }
3612
3938
  this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
3613
3939
  this.context.s.remove(statement.start, statement.end);
3614
3940
  this.changed = true;
3941
+ return true;
3615
3942
  }
3616
3943
  run() {
3617
3944
  for (const scope of new Set(this.context.scopes.scopes.values())) {
@@ -3819,16 +4146,29 @@ function resolveOptions(options) {
3819
4146
  alwaysInline: options.alwaysInline ?? false
3820
4147
  };
3821
4148
  }
3822
- function runPass(code, filename, options, sourceMap, pass) {
4149
+ function parseCode(code, filename) {
3823
4150
  const program = parse(filename, code);
4151
+ return {
4152
+ code,
4153
+ program,
4154
+ parents: collectParents(program),
4155
+ scopes: analyzeScopes(program)
4156
+ };
4157
+ }
4158
+ function runPass(parsed, filename, options, sourceMap, pass, skips) {
4159
+ const { code } = parsed;
3824
4160
  const context = {
3825
4161
  code,
3826
4162
  filename,
3827
4163
  s: new MagicString(code),
3828
- program,
3829
- parents: collectParents(program),
3830
- scopes: analyzeScopes(program),
3831
- options
4164
+ program: parsed.program,
4165
+ parents: parsed.parents,
4166
+ scopes: {
4167
+ ...parsed.scopes,
4168
+ names: new Set(parsed.scopes.names)
4169
+ },
4170
+ options,
4171
+ skips
3832
4172
  };
3833
4173
  if (!pass(context)) return;
3834
4174
  return {
@@ -3862,11 +4202,18 @@ function compile(code, options = {}) {
3862
4202
  if (options.memos ?? true) passes.push(inlineMemos);
3863
4203
  const maps = [];
3864
4204
  let current = code;
4205
+ let parsed;
4206
+ const parsedCurrent = () => {
4207
+ if (parsed?.code !== current) parsed = parseCode(current, filename);
4208
+ return parsed;
4209
+ };
3865
4210
  const maxPasses = options.maxPasses ?? 10;
4211
+ let skipped;
3866
4212
  for (let round = 0; round < maxPasses; round += 1) {
3867
4213
  let changed = false;
4214
+ skipped = options.report === true ? [] : void 0;
3868
4215
  for (const pass of passes) {
3869
- const result = runPass(current, filename, resolved, sourceMap, pass);
4216
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, pass, skipped);
3870
4217
  if (result) {
3871
4218
  changed = true;
3872
4219
  current = result.code;
@@ -3876,19 +4223,22 @@ function compile(code, options = {}) {
3876
4223
  if (!changed) break;
3877
4224
  }
3878
4225
  if (options.server ?? false) {
3879
- const result = runPass(current, filename, resolved, sourceMap, simplifyServer);
4226
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, simplifyServer, void 0);
3880
4227
  if (result) {
3881
4228
  current = result.code;
3882
4229
  if (result.map !== void 0) maps.push(result.map);
3883
4230
  }
3884
4231
  }
4232
+ const report = skipped ? { skipped } : {};
3885
4233
  if (maps.length === 0) return {
3886
4234
  code: current,
3887
- map: null
4235
+ map: null,
4236
+ ...report
3888
4237
  };
3889
4238
  return {
3890
4239
  code: current,
3891
- map: remapping(maps.reverse(), () => null)
4240
+ map: remapping(maps.reverse(), () => null),
4241
+ ...report
3892
4242
  };
3893
4243
  }
3894
4244
  /**
@@ -3899,12 +4249,13 @@ function compile(code, options = {}) {
3899
4249
  */
3900
4250
  function readModuleConstants(code, options = {}) {
3901
4251
  const program = parse(options.filename ?? "input.jsx", code);
4252
+ const scopes = analyzeScopes(program);
3902
4253
  return {
3903
- imports: namedImportSources(program),
3904
- exports: constantExports(program, resolveOptions(options))
4254
+ imports: namedImportSources(program, scopes),
4255
+ exports: constantExports(program, resolveOptions(options), scopes)
3905
4256
  };
3906
4257
  }
3907
4258
  //#endregion
3908
- export { lookup as a, forEachChild as c, analyzeScopes as i, parse as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, unwrap as u };
4259
+ export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
3909
4260
 
3910
- //# sourceMappingURL=compile-ocDYvJUW.mjs.map
4261
+ //# sourceMappingURL=compile-CqhOfBvG.mjs.map