plugin_loader.ts 7.4 KB

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