plugin_loader.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 grafanaData from '@grafana/data';
  30. import * as grafanaUI from '@grafana/ui';
  31. import * as grafanaRuntime from '@grafana/runtime';
  32. export { loadPluginCss } from '@grafana/runtime';
  33. // rxjs
  34. import { Observable, Subject } from 'rxjs';
  35. // add cache busting
  36. const bust = `?_cache=${Date.now()}`;
  37. function locate(load) {
  38. return load.address + bust;
  39. }
  40. System.registry.set('plugin-loader', System.newModule({ locate: locate }));
  41. System.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. function exposeToPlugin(name: string, component: any) {
  62. System.registerDynamic(name, [], true, (require, exports, module) => {
  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('angular', angular);
  73. exposeToPlugin('d3', d3);
  74. exposeToPlugin('rxjs/Subject', Subject);
  75. exposeToPlugin('rxjs/Observable', Observable);
  76. exposeToPlugin('rxjs', {
  77. Subject: Subject,
  78. Observable: Observable,
  79. });
  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('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/plugins/sdk', sdk);
  101. exposeToPlugin('app/core/utils/datemath', datemath);
  102. exposeToPlugin('app/core/utils/file_export', fileExport);
  103. exposeToPlugin('app/core/utils/flatten', flatten);
  104. exposeToPlugin('app/core/utils/kbn', kbn);
  105. exposeToPlugin('app/core/utils/ticks', ticks);
  106. exposeToPlugin('app/core/config', config);
  107. exposeToPlugin('app/core/time_series', TimeSeries);
  108. exposeToPlugin('app/core/time_series2', TimeSeries);
  109. exposeToPlugin('app/core/table_model', TableModel);
  110. exposeToPlugin('app/core/app_events', appEvents);
  111. exposeToPlugin('app/core/core_module', coreModule);
  112. exposeToPlugin('app/core/core', {
  113. coreModule: coreModule,
  114. appEvents: appEvents,
  115. contextSrv: contextSrv,
  116. __esModule: true,
  117. });
  118. import 'vendor/flot/jquery.flot';
  119. import 'vendor/flot/jquery.flot.selection';
  120. import 'vendor/flot/jquery.flot.time';
  121. import 'vendor/flot/jquery.flot.stack';
  122. import 'vendor/flot/jquery.flot.pie';
  123. import 'vendor/flot/jquery.flot.stackpercent';
  124. import 'vendor/flot/jquery.flot.fillbelow';
  125. import 'vendor/flot/jquery.flot.crosshair';
  126. import 'vendor/flot/jquery.flot.dashes';
  127. import 'vendor/flot/jquery.flot.gauge';
  128. const flotDeps = [
  129. 'jquery.flot',
  130. 'jquery.flot.pie',
  131. 'jquery.flot.time',
  132. 'jquery.flot.fillbelow',
  133. 'jquery.flot.crosshair',
  134. 'jquery.flot.stack',
  135. 'jquery.flot.selection',
  136. 'jquery.flot.stackpercent',
  137. 'jquery.flot.events',
  138. 'jquery.flot.gauge',
  139. ];
  140. for (const flotDep of flotDeps) {
  141. exposeToPlugin(flotDep, { fakeDep: 1 });
  142. }
  143. export function importPluginModule(path: string): Promise<any> {
  144. const builtIn = builtInPlugins[path];
  145. if (builtIn) {
  146. return Promise.resolve(builtIn);
  147. }
  148. return System.import(path);
  149. }
  150. export function importDataSourcePlugin(meta: DataSourcePluginMeta): Promise<DataSourcePlugin<any>> {
  151. return importPluginModule(meta.module).then(pluginExports => {
  152. if (pluginExports.plugin) {
  153. const dsPlugin = pluginExports.plugin as DataSourcePlugin<any>;
  154. dsPlugin.meta = meta;
  155. return dsPlugin;
  156. }
  157. if (pluginExports.Datasource) {
  158. const dsPlugin = new DataSourcePlugin(pluginExports.Datasource);
  159. dsPlugin.setComponentsFromLegacyExports(pluginExports);
  160. dsPlugin.meta = meta;
  161. return dsPlugin;
  162. }
  163. throw new Error('Plugin module is missing DataSourcePlugin or Datasource constructor export');
  164. });
  165. }
  166. export function importAppPlugin(meta: PluginMeta): Promise<AppPlugin> {
  167. return importPluginModule(meta.module).then(pluginExports => {
  168. const plugin = pluginExports.plugin ? (pluginExports.plugin as AppPlugin) : new AppPlugin();
  169. plugin.init(meta);
  170. plugin.meta = meta;
  171. plugin.setComponentsFromLegacyExports(pluginExports);
  172. return plugin;
  173. });
  174. }
  175. import { getPanelPluginNotFound } from '../dashboard/dashgrid/PanelPluginNotFound';
  176. interface PanelCache {
  177. [key: string]: PanelPlugin;
  178. }
  179. const panelCache: PanelCache = {};
  180. export function importPanelPlugin(id: string): Promise<PanelPlugin> {
  181. const loaded = panelCache[id];
  182. if (loaded) {
  183. return Promise.resolve(loaded);
  184. }
  185. const meta = config.panels[id];
  186. if (!meta) {
  187. return Promise.resolve(getPanelPluginNotFound(id));
  188. }
  189. return importPluginModule(meta.module)
  190. .then(pluginExports => {
  191. if (pluginExports.plugin) {
  192. return pluginExports.plugin as PanelPlugin;
  193. } else if (pluginExports.PanelCtrl) {
  194. const plugin = new PanelPlugin(null);
  195. plugin.angularPanelCtrl = pluginExports.PanelCtrl;
  196. return plugin;
  197. }
  198. throw new Error('missing export: plugin or PanelCtrl');
  199. })
  200. .then(plugin => {
  201. plugin.meta = meta;
  202. return (panelCache[meta.id] = plugin);
  203. })
  204. .catch(err => {
  205. // TODO, maybe a different error plugin
  206. console.log('Error loading panel plugin', err);
  207. return getPanelPluginNotFound(id);
  208. });
  209. }