Skip to content

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

refactor(sdk): Add type-checking for SDK Bundle exports (#62202)

Timofey Kachalovf7e06aac9d3f
diff --git a/enterprise/frontend/src/embedding-sdk/bundle.ts b/enterprise/frontend/src/embedding-sdk/bundle.ts
index 3aee72bf270..ee96269e6ef 100644
--- a/enterprise/frontend/src/embedding-sdk/bundle.ts
+++ b/enterprise/frontend/src/embedding-sdk/bundle.ts
@@ -1,16 +1,13 @@
+/* eslint-disable import/order */
+
import { EMBEDDING_SDK_CONFIG } from "metabase/embedding-sdk/config";
// Enable SDK mode as we are in the SDK bundle
// This applies to SDK derivatives such as new iframe embedding.
EMBEDDING_SDK_CONFIG.isEmbeddingSdk = true;
-import "@mantine/core/styles.css";
-import "@mantine/dates/styles.css";
-
-import "metabase/css/vendor.css";
-import "metabase/css/index.module.css";
-
-import "metabase/lib/dayjs";
+// Import the embedding SDK vendors side-effects
+import "metabase/embedding-sdk/vendors-side-effects";
// Import the EE plugins required by the embedding sdk.
import "sdk-ee-plugins";
@@ -18,17 +15,58 @@ import "sdk-ee-plugins";
// Imports which are only applicable to the embedding sdk, and not the new iframe embedding.
import "sdk-specific-imports";
-// Components
-export * from "./components/public";
+import type { MetabaseEmbeddingSdkBundleExports } from "./types/sdk-bundle";
-// Init data hook
-export { useInitData } from "./hooks/private/use-init-data";
+// Components
+import { CollectionBrowser } from "./components/public/CollectionBrowser";
+import { CreateDashboardModal } from "./components/public/CreateDashboardModal";
+import { CreateQuestion } from "./components/public/CreateQuestion";
+import { InteractiveQuestion } from "./components/public/InteractiveQuestion";
+import { ComponentProvider } from "./components/public/ComponentProvider";
+import { MetabotQuestion } from "./components/public/MetabotQuestion";
+import { StaticQuestion } from "./components/public/StaticQuestion";
+import {
+ EditableDashboard,
+ InteractiveDashboard,
+ StaticDashboard,
+} from "./components/public/dashboard";
+import { SdkDebugInfo } from "./components/public/debug/SdkDebugInfo";
// Exports needed for public Hooks that use sdk redux store
-export { getSdkStore } from "./store/index";
-export { getApplicationName } from "metabase/selectors/whitelabel";
-export { getSetting } from "metabase/selectors/settings";
-export { getUser } from "metabase/selectors/user";
-export { getLoginStatus } from "embedding-sdk/store/selectors";
-export { createDashboard } from "metabase/api/dashboard";
-export { getCollectionNumericIdFromReference } from "embedding-sdk/store/collections";
+import { createDashboard } from "metabase/api/dashboard";
+import { getApplicationName } from "metabase/selectors/whitelabel";
+import { getCollectionNumericIdFromReference } from "embedding-sdk/store/collections";
+import { getSdkStore } from "./store/index";
+import { getLoginStatus } from "embedding-sdk/store/selectors";
+import { getSetting } from "metabase/selectors/settings";
+import { getUser } from "metabase/selectors/user";
+import { useInitData } from "./hooks/private/use-init-data";
+
+/**
+ * IMPORTANT!
+ * Any rename/removal change for object is a breaking change between the SDK Bundle and the SDK NPM package,
+ * and should be done via the deprecation of the field first.
+ */
+const sdkBundleExports: MetabaseEmbeddingSdkBundleExports = {
+ CollectionBrowser: CollectionBrowser,
+ CreateDashboardModal: CreateDashboardModal,
+ CreateQuestion: CreateQuestion,
+ EditableDashboard: EditableDashboard,
+ InteractiveDashboard: InteractiveDashboard,
+ InteractiveQuestion: InteractiveQuestion,
+ ComponentProvider: ComponentProvider,
+ MetabotQuestion: MetabotQuestion,
+ SdkDebugInfo: SdkDebugInfo,
+ StaticDashboard: StaticDashboard,
+ StaticQuestion: StaticQuestion,
+ createDashboard: createDashboard,
+ getApplicationName: getApplicationName,
+ getCollectionNumericIdFromReference: getCollectionNumericIdFromReference,
+ getLoginStatus: getLoginStatus,
+ getSdkStore: getSdkStore,
+ getSetting: getSetting,
+ getUser: getUser,
+ useInitData: useInitData,
+};
+
+window.MetabaseEmbeddingSDK = sdkBundleExports;
diff --git a/enterprise/frontend/src/embedding-sdk/sdk-package/hooks/public/use-create-dashboard-api/use-create-dashboard-api.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk/sdk-package/hooks/public/use-create-dashboard-api/use-create-dashboard-api.unit.spec.tsx
index ea86149734b..76f5b55b9fa 100644
--- a/enterprise/frontend/src/embedding-sdk/sdk-package/hooks/public/use-create-dashboard-api/use-create-dashboard-api.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk/sdk-package/hooks/public/use-create-dashboard-api/use-create-dashboard-api.unit.spec.tsx
@@ -3,10 +3,7 @@ import fetchMock from "fetch-mock";
import { setupDashboardCreateEndpoint } from "__support__/server-mocks";
import { screen } from "__support__/ui";
-import {
- createDashboard,
- getCollectionNumericIdFromReference,
-} from "embedding-sdk/bundle";
+import { getCollectionNumericIdFromReference } from "embedding-sdk/store/collections";
import { renderWithSDKProviders } from "embedding-sdk/test/__support__/ui";
import { createMockSdkConfig } from "embedding-sdk/test/mocks/config";
import { setupSdkState } from "embedding-sdk/test/server-mocks/sdk-init";
@@ -14,6 +11,7 @@ import type {
CreateDashboardValues,
MetabaseDashboard,
} from "embedding-sdk/types/dashboard";
+import { createDashboard } from "metabase/api/dashboard";
import { createMockUser } from "metabase-types/api/mocks";
import { useCreateDashboardApi } from "./use-create-dashboard-api";
diff --git a/enterprise/frontend/src/embedding-sdk/types/globalTypes.d.ts b/enterprise/frontend/src/embedding-sdk/types/globalTypes.d.ts
index 16b5d42521a..fbeb513c709 100644
--- a/enterprise/frontend/src/embedding-sdk/types/globalTypes.d.ts
+++ b/enterprise/frontend/src/embedding-sdk/types/globalTypes.d.ts
@@ -7,7 +7,7 @@ interface Window {
ReactDOMServer?: unknown;
// Metabase Embedding SDK from Hosted Bundle
- MetabaseEmbeddingSDK?: typeof import("embedding-sdk/bundle");
+ MetabaseEmbeddingSDK?: import("embedding-sdk/types/sdk-bundle").MetabaseEmbeddingSdkBundleExports;
METABASE_PROVIDER_PROPS_STORE?: typeof import("embedding-sdk/sdk-shared/lib/ensure-metabase-provider-props-store").MetabaseProviderPropsStore;
diff --git a/enterprise/frontend/src/embedding-sdk/types/sdk-bundle.ts b/enterprise/frontend/src/embedding-sdk/types/sdk-bundle.ts
new file mode 100644
index 00000000000..992a1f9984c
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk/types/sdk-bundle.ts
@@ -0,0 +1,46 @@
+import type { CollectionBrowser } from "embedding-sdk/components/public/CollectionBrowser";
+import type { ComponentProvider } from "embedding-sdk/components/public/ComponentProvider";
+import type { CreateDashboardModal } from "embedding-sdk/components/public/CreateDashboardModal";
+import type { CreateQuestion } from "embedding-sdk/components/public/CreateQuestion";
+import type { InteractiveQuestion } from "embedding-sdk/components/public/InteractiveQuestion/InteractiveQuestion";
+import type { MetabotQuestion } from "embedding-sdk/components/public/MetabotQuestion";
+import type { StaticQuestion } from "embedding-sdk/components/public/StaticQuestion/StaticQuestion";
+import type { EditableDashboard } from "embedding-sdk/components/public/dashboard/EditableDashboard";
+import type { InteractiveDashboard } from "embedding-sdk/components/public/dashboard/InteractiveDashboard";
+import type { StaticDashboard } from "embedding-sdk/components/public/dashboard/StaticDashboard";
+import type { SdkDebugInfo } from "embedding-sdk/components/public/debug/SdkDebugInfo";
+import type { useInitData } from "embedding-sdk/hooks/private/use-init-data";
+import type { getSdkStore } from "embedding-sdk/store";
+import type { getCollectionNumericIdFromReference } from "embedding-sdk/store/collections";
+import type { getLoginStatus } from "embedding-sdk/store/selectors";
+import type { createDashboard } from "metabase/api/dashboard";
+import type { getSetting } from "metabase/selectors/settings";
+import type { getUser } from "metabase/selectors/user";
+import type { getApplicationName } from "metabase/selectors/whitelabel";
+
+/**
+ * IMPORTANT!
+ * Any rename/removal change for object is a breaking change between the SDK Bundle and the SDK NPM package,
+ * and should be done via the deprecation of the field first.
+ */
+export type MetabaseEmbeddingSdkBundleExports = {
+ CollectionBrowser: typeof CollectionBrowser;
+ CreateDashboardModal: typeof CreateDashboardModal;
+ CreateQuestion: typeof CreateQuestion;
+ EditableDashboard: typeof EditableDashboard;
+ InteractiveDashboard: typeof InteractiveDashboard;
+ InteractiveQuestion: typeof InteractiveQuestion;
+ ComponentProvider: typeof ComponentProvider;
+ MetabotQuestion: typeof MetabotQuestion;
+ SdkDebugInfo: typeof SdkDebugInfo;
+ StaticDashboard: typeof StaticDashboard;
+ StaticQuestion: typeof StaticQuestion;
+ createDashboard: typeof createDashboard;
+ getApplicationName: typeof getApplicationName;
+ getCollectionNumericIdFromReference: typeof getCollectionNumericIdFromReference;
+ getLoginStatus: typeof getLoginStatus;
+ getSdkStore: typeof getSdkStore;
+ getSetting: typeof getSetting;
+ getUser: typeof getUser;
+ useInitData: typeof useInitData;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
index 6cbde01dcc9..e063865d50f 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk/components/SdkIframeEmbedRoute.tsx
@@ -1,13 +1,13 @@
import type { ReactNode } from "react";
import { P, match } from "ts-pattern";
+import { ComponentProvider } from "embedding-sdk/components/public/ComponentProvider";
+import { SdkQuestion } from "embedding-sdk/components/public/SdkQuestion";
+import { StaticQuestion } from "embedding-sdk/components/public/StaticQuestion";
import {
- ComponentProvider,
InteractiveDashboard,
StaticDashboard,
- StaticQuestion,
-} from "embedding-sdk/bundle";
-import { SdkQuestion } from "embedding-sdk/components/public/SdkQuestion";
+} from "embedding-sdk/components/public/dashboard";
import { defineMetabaseAuthConfig } from "embedding-sdk/sdk-package/lib/public/define-metabase-auth-config";
import { EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG } from "metabase/embedding-sdk/config";
import { PLUGIN_EMBEDDING_IFRAME_SDK } from "metabase/plugins";
diff --git a/enterprise/frontend/src/metabase-enterprise/sdk-iframe-embedding-plugins.js b/enterprise/frontend/src/metabase-enterprise/sdk-iframe-embedding-plugins.js
index b96ff6da7e2..e6fb04cf223 100644
--- a/enterprise/frontend/src/metabase-enterprise/sdk-iframe-embedding-plugins.js
+++ b/enterprise/frontend/src/metabase-enterprise/sdk-iframe-embedding-plugins.js
@@ -1,5 +1,5 @@
-/**
- * Plugins that are only applicable to the new iframe embedding.
- */
+// Import the EE plugins required by the embedding sdk.
+import "sdk-ee-plugins";
+// Plugins that are only applicable to the new iframe embedding.
import "./embedding_iframe_sdk";
diff --git a/frontend/src/metabase/app-embed-sdk.tsx b/frontend/src/metabase/app-embed-sdk.tsx
index eeee333dc9f..19f93d46f18 100644
--- a/frontend/src/metabase/app-embed-sdk.tsx
+++ b/frontend/src/metabase/app-embed-sdk.tsx
@@ -3,6 +3,9 @@ import { createRoot } from "react-dom/client";
// Setup CSP nonce for CodeMirror dynamic styles
import "metabase/lib/csp";
+// Import the embedding SDK vendors side-effects
+import "metabase/embedding-sdk/vendors-side-effects";
+
import {
EMBEDDING_SDK_CONFIG,
EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG,
diff --git a/frontend/src/metabase/embedding-sdk/vendors-side-effects.ts b/frontend/src/metabase/embedding-sdk/vendors-side-effects.ts
new file mode 100644
index 00000000000..ba6bc2d8b22
--- /dev/null
+++ b/frontend/src/metabase/embedding-sdk/vendors-side-effects.ts
@@ -0,0 +1,7 @@
+import "@mantine/core/styles.css";
+import "@mantine/dates/styles.css";
+
+import "metabase/css/vendor.css";
+import "metabase/css/index.module.css";
+
+import "metabase/lib/dayjs";
diff --git a/rspack.embedding-sdk-bundle.config.js b/rspack.embedding-sdk-bundle.config.js
index 2a9e9c47e91..ba76dfe8c10 100644
--- a/rspack.embedding-sdk-bundle.config.js
+++ b/rspack.embedding-sdk-bundle.config.js
@@ -77,10 +77,9 @@ const config = {
path: TMP_BUILD_PATH,
publicPath: "",
filename: SDK_BUNDLE_FILENAME,
- library: {
- type: "umd",
- name: "MetabaseEmbeddingSDK",
- },
+
+ // We assign exports from SDK bundle into window.MetabaseEmbeddingSDK manually in the SDK bundle entry point.
+ library: false,
},
devtool: IS_DEV_MODE ? mainConfig.devtool : false,