2023-05-31 02:26:37 -05:00
|
|
|
import type { PluginExtensionConfig } from '@grafana/data';
|
2023-03-02 08:42:00 -06:00
|
|
|
import type { AppPluginConfig } from '@grafana/runtime';
|
2023-05-10 04:03:15 -05:00
|
|
|
import { startMeasure, stopMeasure } from 'app/core/utils/metrics';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2023-03-02 08:42:00 -06:00
|
|
|
import * as pluginLoader from './plugin_loader';
|
2021-11-10 12:06:55 -06:00
|
|
|
|
2023-03-02 08:42:00 -06:00
|
|
|
export type PluginPreloadResult = {
|
|
|
|
pluginId: string;
|
|
|
|
error?: unknown;
|
2023-05-31 02:26:37 -05:00
|
|
|
extensionConfigs: PluginExtensionConfig[];
|
2023-03-02 08:42:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function preloadPlugins(apps: Record<string, AppPluginConfig> = {}): Promise<PluginPreloadResult[]> {
|
2023-05-10 04:03:15 -05:00
|
|
|
startMeasure('frontend_plugins_preload');
|
2023-02-07 10:20:05 -06:00
|
|
|
const pluginsToPreload = Object.values(apps).filter((app) => app.preload);
|
2023-05-10 04:03:15 -05:00
|
|
|
const result = await Promise.all(pluginsToPreload.map(preload));
|
|
|
|
stopMeasure('frontend_plugins_preload');
|
|
|
|
return result;
|
2021-11-10 12:06:55 -06:00
|
|
|
}
|
|
|
|
|
2023-03-02 08:42:00 -06:00
|
|
|
async function preload(config: AppPluginConfig): Promise<PluginPreloadResult> {
|
|
|
|
const { path, version, id: pluginId } = config;
|
2021-11-10 12:06:55 -06:00
|
|
|
try {
|
2023-05-10 04:03:15 -05:00
|
|
|
startMeasure(`frontend_plugin_preload_${pluginId}`);
|
2023-06-05 03:51:36 -05:00
|
|
|
const { plugin } = await pluginLoader.importPluginModule({
|
|
|
|
path,
|
|
|
|
version,
|
2023-11-10 04:44:54 -06:00
|
|
|
isAngular: config.angular.detected,
|
2023-06-05 03:51:36 -05:00
|
|
|
pluginId,
|
|
|
|
});
|
2023-04-03 03:42:15 -05:00
|
|
|
const { extensionConfigs = [] } = plugin;
|
|
|
|
return { pluginId, extensionConfigs };
|
2023-03-02 08:42:00 -06:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`[Plugins] Failed to preload plugin: ${path} (version: ${version})`, error);
|
2023-04-03 03:42:15 -05:00
|
|
|
return { pluginId, extensionConfigs: [], error };
|
2023-05-10 04:03:15 -05:00
|
|
|
} finally {
|
|
|
|
stopMeasure(`frontend_plugin_preload_${pluginId}`);
|
2021-11-10 12:06:55 -06:00
|
|
|
}
|
|
|
|
}
|