plugin_loader.ts 7.0 KB

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