plugin_loader.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import _ from 'lodash';
  2. import * as sdk from 'app/plugins/sdk';
  3. import kbn from 'app/core/utils/kbn';
  4. // tslint:disable:import-blacklist
  5. import moment from 'moment';
  6. import angular from 'angular';
  7. import jquery from 'jquery';
  8. // Experimental module exports
  9. import prismjs from 'prismjs';
  10. import slate from 'slate';
  11. // @ts-ignore
  12. import slateReact from 'slate-react';
  13. // @ts-ignore
  14. import slatePlain from 'slate-plain-serializer';
  15. import react from 'react';
  16. import reactDom from 'react-dom';
  17. import reactRedux from 'react-redux';
  18. import redux from 'redux';
  19. import config from 'app/core/config';
  20. import TimeSeries from 'app/core/time_series2';
  21. import TableModel from 'app/core/table_model';
  22. import { coreModule, appEvents, contextSrv } from 'app/core/core';
  23. import { DataSourcePlugin, AppPlugin, PanelPlugin, PluginMeta, DataSourcePluginMeta } from '@grafana/ui';
  24. import { dateMath } from '@grafana/data';
  25. import * as fileExport from 'app/core/utils/file_export';
  26. import * as flatten from 'app/core/utils/flatten';
  27. import * as ticks from 'app/core/utils/ticks';
  28. import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv';
  29. import impressionSrv from 'app/core/services/impression_srv';
  30. import builtInPlugins from './built_in_plugins';
  31. import * as d3 from 'd3';
  32. import * as emotion from 'emotion';
  33. import * as grafanaData from '@grafana/data';
  34. import * as grafanaUI from '@grafana/ui';
  35. import * as grafanaRuntime from '@grafana/runtime';
  36. // rxjs
  37. import * as rxjs from 'rxjs';
  38. import * as rxjsOperators from 'rxjs/operators';
  39. // add cache busting
  40. const bust = `?_cache=${Date.now()}`;
  41. function locate(load: { address: string }) {
  42. return load.address + bust;
  43. }
  44. grafanaRuntime.SystemJS.registry.set('plugin-loader', grafanaRuntime.SystemJS.newModule({ locate: locate }));
  45. grafanaRuntime.SystemJS.config({
  46. baseURL: 'public',
  47. defaultExtension: 'js',
  48. packages: {
  49. plugins: {
  50. defaultExtension: 'js',
  51. },
  52. },
  53. map: {
  54. text: 'vendor/plugin-text/text.js',
  55. css: 'vendor/plugin-css/css.js',
  56. },
  57. meta: {
  58. '/*': {
  59. esModule: true,
  60. authorization: true,
  61. loader: 'plugin-loader',
  62. },
  63. },
  64. });
  65. function exposeToPlugin(name: string, component: any) {
  66. grafanaRuntime.SystemJS.registerDynamic(name, [], true, (require: any, exports: any, module: { exports: any }) => {
  67. module.exports = component;
  68. });
  69. }
  70. exposeToPlugin('@grafana/data', grafanaData);
  71. exposeToPlugin('@grafana/ui', grafanaUI);
  72. exposeToPlugin('@grafana/runtime', grafanaRuntime);
  73. exposeToPlugin('lodash', _);
  74. exposeToPlugin('moment', moment);
  75. exposeToPlugin('jquery', jquery);
  76. exposeToPlugin('angular', angular);
  77. exposeToPlugin('d3', d3);
  78. exposeToPlugin('rxjs', rxjs);
  79. exposeToPlugin('rxjs/operators', rxjsOperators);
  80. // Experimental modules
  81. exposeToPlugin('prismjs', prismjs);
  82. exposeToPlugin('slate', slate);
  83. exposeToPlugin('slate-react', slateReact);
  84. exposeToPlugin('slate-plain-serializer', slatePlain);
  85. exposeToPlugin('react', react);
  86. exposeToPlugin('react-dom', reactDom);
  87. exposeToPlugin('react-redux', reactRedux);
  88. exposeToPlugin('redux', redux);
  89. exposeToPlugin('emotion', emotion);
  90. exposeToPlugin('app/features/dashboard/impression_store', {
  91. impressions: impressionSrv,
  92. __esModule: true,
  93. });
  94. /**
  95. * NOTE: this is added temporarily while we explore a long term solution
  96. * If you use this export, only use the:
  97. * get/delete/post/patch/request methods
  98. */
  99. exposeToPlugin('app/core/services/backend_srv', {
  100. BackendSrv,
  101. getBackendSrv,
  102. });
  103. exposeToPlugin('app/plugins/sdk', sdk);
  104. exposeToPlugin('app/core/utils/datemath', dateMath);
  105. exposeToPlugin('app/core/utils/file_export', fileExport);
  106. exposeToPlugin('app/core/utils/flatten', flatten);
  107. exposeToPlugin('app/core/utils/kbn', kbn);
  108. exposeToPlugin('app/core/utils/ticks', ticks);
  109. exposeToPlugin('app/core/config', config);
  110. exposeToPlugin('app/core/time_series', TimeSeries);
  111. exposeToPlugin('app/core/time_series2', TimeSeries);
  112. exposeToPlugin('app/core/table_model', TableModel);
  113. exposeToPlugin('app/core/app_events', appEvents);
  114. exposeToPlugin('app/core/core_module', coreModule);
  115. exposeToPlugin('app/core/core', {
  116. coreModule: coreModule,
  117. appEvents: appEvents,
  118. contextSrv: contextSrv,
  119. __esModule: true,
  120. });
  121. import 'vendor/flot/jquery.flot';
  122. import 'vendor/flot/jquery.flot.selection';
  123. import 'vendor/flot/jquery.flot.time';
  124. import 'vendor/flot/jquery.flot.stack';
  125. import 'vendor/flot/jquery.flot.pie';
  126. import 'vendor/flot/jquery.flot.stackpercent';
  127. import 'vendor/flot/jquery.flot.fillbelow';
  128. import 'vendor/flot/jquery.flot.crosshair';
  129. import 'vendor/flot/jquery.flot.dashes';
  130. import 'vendor/flot/jquery.flot.gauge';
  131. const flotDeps = [
  132. 'jquery.flot',
  133. 'jquery.flot.pie',
  134. 'jquery.flot.time',
  135. 'jquery.flot.fillbelow',
  136. 'jquery.flot.crosshair',
  137. 'jquery.flot.stack',
  138. 'jquery.flot.selection',
  139. 'jquery.flot.stackpercent',
  140. 'jquery.flot.events',
  141. 'jquery.flot.gauge',
  142. ];
  143. for (const flotDep of flotDeps) {
  144. exposeToPlugin(flotDep, { fakeDep: 1 });
  145. }
  146. export async function importPluginModule(path: string): Promise<any> {
  147. const builtIn = builtInPlugins[path];
  148. if (builtIn) {
  149. // for handling dynamic imports
  150. if (typeof builtIn === 'function') {
  151. return await builtIn();
  152. } else {
  153. return Promise.resolve(builtIn);
  154. }
  155. }
  156. return grafanaRuntime.SystemJS.import(path);
  157. }
  158. export function importDataSourcePlugin(meta: DataSourcePluginMeta): Promise<DataSourcePlugin<any>> {
  159. return importPluginModule(meta.module).then(pluginExports => {
  160. if (pluginExports.plugin) {
  161. const dsPlugin = pluginExports.plugin as DataSourcePlugin<any>;
  162. dsPlugin.meta = meta;
  163. return dsPlugin;
  164. }
  165. if (pluginExports.Datasource) {
  166. const dsPlugin = new DataSourcePlugin(pluginExports.Datasource);
  167. dsPlugin.setComponentsFromLegacyExports(pluginExports);
  168. dsPlugin.meta = meta;
  169. return dsPlugin;
  170. }
  171. throw new Error('Plugin module is missing DataSourcePlugin or Datasource constructor export');
  172. });
  173. }
  174. export function importAppPlugin(meta: PluginMeta): Promise<AppPlugin> {
  175. return importPluginModule(meta.module).then(pluginExports => {
  176. const plugin = pluginExports.plugin ? (pluginExports.plugin as AppPlugin) : new AppPlugin();
  177. plugin.init(meta);
  178. plugin.meta = meta;
  179. plugin.setComponentsFromLegacyExports(pluginExports);
  180. return plugin;
  181. });
  182. }
  183. import { getPanelPluginNotFound, getPanelPluginLoadError } from '../dashboard/dashgrid/PanelPluginError';
  184. interface PanelCache {
  185. [key: string]: PanelPlugin;
  186. }
  187. const panelCache: PanelCache = {};
  188. export function importPanelPlugin(id: string): Promise<PanelPlugin> {
  189. const loaded = panelCache[id];
  190. if (loaded) {
  191. return Promise.resolve(loaded);
  192. }
  193. const meta = config.panels[id];
  194. if (!meta) {
  195. return Promise.resolve(getPanelPluginNotFound(id));
  196. }
  197. return importPluginModule(meta.module)
  198. .then(pluginExports => {
  199. if (pluginExports.plugin) {
  200. return pluginExports.plugin as PanelPlugin;
  201. } else if (pluginExports.PanelCtrl) {
  202. const plugin = new PanelPlugin(null);
  203. plugin.angularPanelCtrl = pluginExports.PanelCtrl;
  204. return plugin;
  205. }
  206. throw new Error('missing export: plugin or PanelCtrl');
  207. })
  208. .then(plugin => {
  209. plugin.meta = meta;
  210. return (panelCache[meta.id] = plugin);
  211. })
  212. .catch(err => {
  213. // TODO, maybe a different error plugin
  214. console.warn('Error loading panel plugin: ' + id, err);
  215. return getPanelPluginLoadError(meta, err);
  216. });
  217. }