plugin_loader.ts 6.9 KB

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