plugin_loader.ts 6.9 KB

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