Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

2.5 KB
Raw Blame
import fs from "fs";
import path from "path";

import { getPublicComponents } from "embedding-sdk-package/bin/get-public-components";

const destinationDir = path.resolve(
  __dirname,
  "../../../../../resources/embedding-sdk/dist",
);

const writeToFile = async (filePath: string, content: string) => {
  const fullPath = path.resolve(destinationDir, filePath);

  fs.mkdirSync(destinationDir, { recursive: true });
  fs.writeFileSync(fullPath, content);

  console.log(`wrote ${fullPath}`);
};

const nextjs_cjs = `
"use client";

const MetabaseEmbeddingSDK = require("./main.bundle");

module.exports = MetabaseEmbeddingSDK;
`.trim();

const nextjs_js = `
"use client";

export * from "./main.bundle.js";
`.trim();

writeToFile("nextjs.cjs", nextjs_cjs);
writeToFile("nextjs.js", nextjs_js);

// Data-app specific imports
const dataApp_cjs = `
const DataApp = require("./data-app.bundle");

module.exports = DataApp;
`.trim();

const dataApp_js = `
export * from "./data-app.esm.js";
`.trim();

writeToFile("data-app.cjs", dataApp_cjs);
writeToFile("data-app.js", dataApp_js);

const testing_cjs = `
const Testing = require("./testing.bundle");

module.exports = Testing;
`.trim();

const testing_js = `
export * from "./testing.esm.js";
`.trim();

writeToFile("testing.cjs", testing_cjs);
writeToFile("testing.js", testing_js);

// The data-app dev preset (`data-app-dev.js`, ESM-only) and the dev entry
// (`data-app-dev-entry.js`) are emitted directly by the rspack builds, so they
// need no wrappers here.

// Development mode entry point.
// When the host app bundler resolves the "development" exports condition,
// this file sets a window global so the SDK bundle can detect dev mode.
const devMode_js = `
try {
  if (typeof window !== "undefined") {
    window.METABASE_EMBEDDING_SDK_IS_HOST_APP_IN_DEV_MODE = true;
  }
} catch (e) {
  console.warn("Metabase SDK: Failed to set dev mode flag", e);
}

export * from "./main.esm.js";
`.trim();

writeToFile("main.development.js", devMode_js);

writeToFile(
  "nextjs.d.ts",
  `
import {
${getPublicComponents()
  .map(({ mainComponent }) => {
    return `${mainComponent} as _${mainComponent},`;
  })
  .join("\n")}
} from './index.d.ts';

export * from "./index.d.ts";

${getPublicComponents()
  .map(({ mainComponent }) => {
    return `
/**
  * @deprecated Next.js compat layer is deprecated and will be removed in a future version.
  * Use the '@metabase/embedding-sdk-react' main entry point instead.
  */
export declare const ${mainComponent} = _${mainComponent};
    `;
  })
  .join("")}
`,
);