plugin_loader.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import * as emotion from '@emotion/css';
  2. import * as d3 from 'd3';
  3. import jquery from 'jquery';
  4. import _ from 'lodash'; // eslint-disable-line lodash/import-scope
  5. import moment from 'moment'; // eslint-disable-line no-restricted-imports
  6. import prismjs from 'prismjs';
  7. import react from 'react';
  8. import reactDom from 'react-dom';
  9. import * as reactRedux from 'react-redux';
  10. import * as reactRouter from 'react-router-dom';
  11. import * as redux from 'redux';
  12. import * as rxjs from 'rxjs';
  13. import * as rxjsOperators from 'rxjs/operators';
  14. import slate from 'slate';
  15. import slatePlain from 'slate-plain-serializer';
  16. import * as grafanaData from '@grafana/data';
  17. import * as grafanaRuntime from '@grafana/runtime';
  18. import slateReact from '@grafana/slate-react';
  19. import * as grafanaUIraw from '@grafana/ui';
  20. import config from 'app/core/config';
  21. import { appEvents, contextSrv } from 'app/core/core';
  22. import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv';
  23. import impressionSrv from 'app/core/services/impression_srv';
  24. import TableModel from 'app/core/table_model';
  25. import TimeSeries from 'app/core/time_series2';
  26. import * as flatten from 'app/core/utils/flatten';
  27. import kbn from 'app/core/utils/kbn';
  28. import * as ticks from 'app/core/utils/ticks';
  29. import { GenericDataSourcePlugin } from '../datasources/settings/PluginSettings';
  30. import builtInPlugins from './built_in_plugins';
  31. import { locateWithCache, registerPluginInCache } from './pluginCacheBuster';
  32. // Help the 6.4 to 6.5 migration
  33. // The base classes were moved from @grafana/ui to @grafana/data
  34. // This exposes the same classes on both import paths
  35. const grafanaUI = grafanaUIraw as any;
  36. grafanaUI.PanelPlugin = grafanaData.PanelPlugin;
  37. grafanaUI.DataSourcePlugin = grafanaData.DataSourcePlugin;
  38. grafanaUI.AppPlugin = grafanaData.AppPlugin;
  39. grafanaUI.DataSourceApi = grafanaData.DataSourceApi;
  40. grafanaRuntime.SystemJS.registry.set('plugin-loader', grafanaRuntime.SystemJS.newModule({ locate: locateWithCache }));
  41. grafanaRuntime.SystemJS.config({
  42. baseURL: 'public',
  43. defaultExtension: 'js',
  44. packages: {
  45. plugins: {
  46. defaultExtension: 'js',
  47. },
  48. },
  49. map: {
  50. text: 'vendor/plugin-text/text.js',
  51. css: 'vendor/plugin-css/css.js',
  52. },
  53. meta: {
  54. '/*': {
  55. esModule: true,
  56. authorization: true,
  57. loader: 'plugin-loader',
  58. },
  59. },
  60. });
  61. export function exposeToPlugin(name: string, component: any) {
  62. grafanaRuntime.SystemJS.registerDynamic(name, [], true, (require: any, exports: any, module: { exports: any }) => {
  63. module.exports = component;
  64. });
  65. }
  66. exposeToPlugin('@grafana/data', grafanaData);
  67. exposeToPlugin('@grafana/ui', grafanaUI);
  68. exposeToPlugin('@grafana/runtime', grafanaRuntime);
  69. exposeToPlugin('lodash', _);
  70. exposeToPlugin('moment', moment);
  71. exposeToPlugin('jquery', jquery);
  72. exposeToPlugin('d3', d3);
  73. exposeToPlugin('rxjs', rxjs);
  74. exposeToPlugin('rxjs/operators', rxjsOperators);
  75. exposeToPlugin('react-router-dom', reactRouter);
  76. // Experimental modules
  77. exposeToPlugin('prismjs', prismjs);
  78. exposeToPlugin('slate', slate);
  79. exposeToPlugin('@grafana/slate-react', slateReact);
  80. exposeToPlugin('slate-plain-serializer', slatePlain);
  81. exposeToPlugin('react', react);
  82. exposeToPlugin('react-dom', reactDom);
  83. exposeToPlugin('react-redux', reactRedux);
  84. exposeToPlugin('redux', redux);
  85. exposeToPlugin('emotion', emotion);
  86. exposeToPlugin('@emotion/css', emotion);
  87. exposeToPlugin('app/features/dashboard/impression_store', {
  88. impressions: impressionSrv,
  89. __esModule: true,
  90. });
  91. /**
  92. * NOTE: this is added temporarily while we explore a long term solution
  93. * If you use this export, only use the:
  94. * get/delete/post/patch/request methods
  95. */
  96. exposeToPlugin('app/core/services/backend_srv', {
  97. BackendSrv,
  98. getBackendSrv,
  99. });
  100. exposeToPlugin('app/core/utils/datemath', grafanaData.dateMath);
  101. exposeToPlugin('app/core/utils/flatten', flatten);
  102. exposeToPlugin('app/core/utils/kbn', kbn);
  103. exposeToPlugin('app/core/utils/ticks', ticks);
  104. exposeToPlugin('app/core/config', config);
  105. exposeToPlugin('app/core/time_series', TimeSeries);
  106. exposeToPlugin('app/core/time_series2', TimeSeries);
  107. exposeToPlugin('app/core/table_model', TableModel);
  108. exposeToPlugin('app/core/app_events', appEvents);
  109. exposeToPlugin('app/core/core', {
  110. appEvents: appEvents,
  111. contextSrv: contextSrv,
  112. __esModule: true,
  113. });
  114. import 'vendor/flot/jquery.flot';
  115. import 'vendor/flot/jquery.flot.selection';
  116. import 'vendor/flot/jquery.flot.time';
  117. import 'vendor/flot/jquery.flot.stack';
  118. import 'vendor/flot/jquery.flot.stackpercent';
  119. import 'vendor/flot/jquery.flot.fillbelow';
  120. import 'vendor/flot/jquery.flot.crosshair';
  121. import 'vendor/flot/jquery.flot.dashes';
  122. import 'vendor/flot/jquery.flot.gauge';
  123. const flotDeps = [
  124. 'jquery.flot',
  125. 'jquery.flot.pie',
  126. 'jquery.flot.time',
  127. 'jquery.flot.fillbelow',
  128. 'jquery.flot.crosshair',
  129. 'jquery.flot.stack',
  130. 'jquery.flot.selection',
  131. 'jquery.flot.stackpercent',
  132. 'jquery.flot.events',
  133. 'jquery.flot.gauge',
  134. ];
  135. for (const flotDep of flotDeps) {
  136. exposeToPlugin(flotDep, { fakeDep: 1 });
  137. }
  138. export async function importPluginModule(path: string, version?: string): Promise<any> {
  139. if (version) {
  140. registerPluginInCache({ path, version });
  141. }
  142. const builtIn = builtInPlugins[path];
  143. if (builtIn) {
  144. // for handling dynamic imports
  145. if (typeof builtIn === 'function') {
  146. return await builtIn();
  147. } else {
  148. return builtIn;
  149. }
  150. }
  151. return grafanaRuntime.SystemJS.import(path);
  152. }
  153. export function importDataSourcePlugin(meta: grafanaData.DataSourcePluginMeta): Promise<GenericDataSourcePlugin> {
  154. return importPluginModule(meta.module, meta.info?.version).then((pluginExports) => {
  155. if (pluginExports.plugin) {
  156. const dsPlugin = pluginExports.plugin as GenericDataSourcePlugin;
  157. dsPlugin.meta = meta;
  158. return dsPlugin;
  159. }
  160. if (pluginExports.Datasource) {
  161. const dsPlugin = new grafanaData.DataSourcePlugin<
  162. grafanaData.DataSourceApi<grafanaData.DataQuery, grafanaData.DataSourceJsonData>,
  163. grafanaData.DataQuery,
  164. grafanaData.DataSourceJsonData
  165. >(pluginExports.Datasource);
  166. dsPlugin.setComponentsFromLegacyExports(pluginExports);
  167. dsPlugin.meta = meta;
  168. return dsPlugin;
  169. }
  170. throw new Error('Plugin module is missing DataSourcePlugin or Datasource constructor export');
  171. });
  172. }
  173. export function importAppPlugin(meta: grafanaData.PluginMeta): Promise<grafanaData.AppPlugin> {
  174. return importPluginModule(meta.module, meta.info?.version).then((pluginExports) => {
  175. const plugin = pluginExports.plugin ? (pluginExports.plugin as grafanaData.AppPlugin) : new grafanaData.AppPlugin();
  176. plugin.init(meta);
  177. plugin.meta = meta;
  178. plugin.setComponentsFromLegacyExports(pluginExports);
  179. return plugin;
  180. });
  181. }