diff --git a/packages/grafana-runtime/src/index.ts b/packages/grafana-runtime/src/index.ts index 6e1104c92c2..d9ff32a39c0 100644 --- a/packages/grafana-runtime/src/index.ts +++ b/packages/grafana-runtime/src/index.ts @@ -6,7 +6,13 @@ export * from './services'; export * from './config'; export * from './analytics/types'; -export { loadPluginCss, SystemJS, type PluginCssOptions } from './utils/plugin'; +export { + loadPluginCss, + SystemJS, + type PluginCssOptions, + setPluginImportUtils, + getPluginImportUtils, +} from './utils/plugin'; export { reportMetaAnalytics, reportInteraction, reportPageview, reportExperimentView } from './analytics/utils'; export { featureEnabled } from './utils/licensing'; export { logInfo, logDebug, logWarning, logError } from './utils/logging'; diff --git a/packages/grafana-runtime/src/utils/plugin.ts b/packages/grafana-runtime/src/utils/plugin.ts index 9a5363e3f07..a1024229a9c 100644 --- a/packages/grafana-runtime/src/utils/plugin.ts +++ b/packages/grafana-runtime/src/utils/plugin.ts @@ -1,6 +1,8 @@ // @ts-ignore import System from 'systemjs/dist/system.js'; +import { PanelPlugin } from '@grafana/data'; + import { config } from '../config'; // @ts-ignore @@ -32,3 +34,26 @@ export function loadPluginCss(options: PluginCssOptions): Promise { const theme = config.bootData.user.lightTheme ? options.light : options.dark; return SystemJS.import(`${theme}!css`); } + +interface PluginImportUtils { + importPanelPlugin: (id: string) => Promise; + getPanelPluginFromCache: (id: string) => PanelPlugin | undefined; +} + +let pluginImportUtils: PluginImportUtils | undefined; + +export function setPluginImportUtils(utils: PluginImportUtils) { + if (pluginImportUtils) { + throw new Error('pluginImportUtils should only be set once, when Grafana is starting.'); + } + + pluginImportUtils = utils; +} + +export function getPluginImportUtils(): PluginImportUtils { + if (!pluginImportUtils) { + throw new Error('pluginImportUtils can only be used after Grafana instance has started.'); + } + + return pluginImportUtils; +} diff --git a/public/app/app.ts b/public/app/app.ts index 1463dccfad7..560a1ae2165 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -32,6 +32,7 @@ import { setLocationSrv, setQueryRunnerFactory, setRunRequest, + setPluginImportUtils, } from '@grafana/runtime'; import { setPanelDataErrorView } from '@grafana/runtime/src/components/PanelDataErrorView'; import { setPanelRenderer } from '@grafana/runtime/src/components/PanelRenderer'; @@ -68,6 +69,7 @@ import { getTimeSrv } from './features/dashboard/services/TimeSrv'; import { PanelDataErrorView } from './features/panel/components/PanelDataErrorView'; import { PanelRenderer } from './features/panel/components/PanelRenderer'; import { DatasourceSrv } from './features/plugins/datasource_srv'; +import { importPanelPlugin, syncGetPanelPlugin } from './features/plugins/importPanelPlugin'; import { preloadPlugins } from './features/plugins/pluginPreloader'; import { QueryRunner } from './features/query/state/QueryRunner'; import { runRequest } from './features/query/state/runRequest'; @@ -145,6 +147,12 @@ export class GrafanaApp { // Provide runRequest implementation to packages, @grafana/scenes in particular setRunRequest(runRequest); + // Privide plugin import utils to packages, @grafana/scenes in particular + setPluginImportUtils({ + importPanelPlugin, + getPanelPluginFromCache: syncGetPanelPlugin, + }); + locationUtil.initialize({ config, getTimeRangeForUrl: getTimeSrv().timeRangeForUrl,