File size: 1,794 Bytes
b3d14e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { ComponentType } from "react";

import Plotly from "plotly.js-dist-min";
import * as factoryModule from "react-plotly.js/factory";
import type { PlotParams } from "react-plotly.js";

/**
 * Local re-export of `react-plotly.js` bound to the slim
 * `plotly.js-dist-min` build so we don't ship the full ~5 MB
 * Plotly bundle to the browser.
 *
 * `react-plotly.js/factory` is a Babel-compiled CJS module:
 *   exports.__esModule = true;
 *   exports.default = plotComponentFactory;
 *
 * Depending on which interop layer touches it (esbuild dev-server,
 * Rolldown prod, or Vite's `import * as` namespace shim), the same
 * import statement can land on the function itself, on
 * `{ default: fn }`, or even on `{ default: { default: fn } }`
 * when two CJS-interop layers run in series. `unwrapDefault` walks
 * those wrappers until it finds the callable.
 */
type PlotlyFactory = (plotly: unknown) => ComponentType<PlotParams>;

function unwrapDefault(value: unknown, depth = 5): unknown {
  let current = value;
  for (let i = 0; i < depth; i++) {
    if (typeof current === "function") return current;
    if (current && typeof current === "object" && "default" in current) {
      current = (current as { default: unknown }).default;
    } else {
      return current;
    }
  }
  return current;
}

const createPlotlyComponent = unwrapDefault(factoryModule) as PlotlyFactory;

if (typeof createPlotlyComponent !== "function") {
  console.error("react-plotly.js/factory module:", factoryModule);
  throw new Error(
    "react-plotly.js/factory did not resolve to a callable factory; " +
      "got: " +
      typeof createPlotlyComponent +
      " — see console.error above for the raw module shape.",
  );
}

const Plot = createPlotlyComponent(Plotly);

export default Plot;