plugin_loader.ts 7.1 KB

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