plugin_loader.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import System from 'systemjs/dist/system.js';
  2. import _ from 'lodash';
  3. import * as sdk from 'app/plugins/sdk';
  4. import kbn from 'app/core/utils/kbn';
  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, ReactPanelPlugin, AngularPanelPlugin } from '@grafana/ui/src/types';
  20. import * as datemath from 'app/core/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 impressionSrv from 'app/core/services/impression_srv';
  25. import builtInPlugins from './built_in_plugins';
  26. import * as d3 from 'd3';
  27. import * as grafanaUI from '@grafana/ui';
  28. // rxjs
  29. import { Observable, Subject } from 'rxjs';
  30. // add cache busting
  31. const bust = `?_cache=${Date.now()}`;
  32. function locate(load) {
  33. return load.address + bust;
  34. }
  35. System.registry.set('plugin-loader', System.newModule({ locate: locate }));
  36. System.config({
  37. baseURL: 'public',
  38. defaultExtension: 'js',
  39. packages: {
  40. plugins: {
  41. defaultExtension: 'js',
  42. },
  43. },
  44. map: {
  45. text: 'vendor/plugin-text/text.js',
  46. css: 'vendor/plugin-css/css.js',
  47. },
  48. meta: {
  49. '/*': {
  50. esModule: true,
  51. authorization: true,
  52. loader: 'plugin-loader',
  53. },
  54. },
  55. });
  56. function exposeToPlugin(name: string, component: any) {
  57. System.registerDynamic(name, [], true, (require, exports, module) => {
  58. module.exports = component;
  59. });
  60. }
  61. exposeToPlugin('@grafana/ui', grafanaUI);
  62. exposeToPlugin('lodash', _);
  63. exposeToPlugin('moment', moment);
  64. exposeToPlugin('jquery', jquery);
  65. exposeToPlugin('angular', angular);
  66. exposeToPlugin('d3', d3);
  67. exposeToPlugin('rxjs/Subject', Subject);
  68. exposeToPlugin('rxjs/Observable', Observable);
  69. // Experimental modules
  70. exposeToPlugin('prismjs', prismjs);
  71. exposeToPlugin('slate', slate);
  72. exposeToPlugin('slate-react', slateReact);
  73. exposeToPlugin('slate-plain-serializer', slatePlain);
  74. exposeToPlugin('react', react);
  75. exposeToPlugin('react-dom', reactDom);
  76. // backward compatible path
  77. exposeToPlugin('vendor/npm/rxjs/Rx', {
  78. Subject: Subject,
  79. Observable: Observable,
  80. });
  81. exposeToPlugin('app/features/dashboard/impression_store', {
  82. impressions: impressionSrv,
  83. __esModule: true,
  84. });
  85. exposeToPlugin('app/plugins/sdk', sdk);
  86. exposeToPlugin('app/core/utils/datemath', datemath);
  87. exposeToPlugin('app/core/utils/file_export', fileExport);
  88. exposeToPlugin('app/core/utils/flatten', flatten);
  89. exposeToPlugin('app/core/utils/kbn', kbn);
  90. exposeToPlugin('app/core/utils/ticks', ticks);
  91. exposeToPlugin('app/core/config', config);
  92. exposeToPlugin('app/core/time_series', TimeSeries);
  93. exposeToPlugin('app/core/time_series2', TimeSeries);
  94. exposeToPlugin('app/core/table_model', TableModel);
  95. exposeToPlugin('app/core/app_events', appEvents);
  96. exposeToPlugin('app/core/core_module', coreModule);
  97. exposeToPlugin('app/core/core', {
  98. coreModule: coreModule,
  99. appEvents: appEvents,
  100. contextSrv: contextSrv,
  101. __esModule: true,
  102. });
  103. import 'vendor/flot/jquery.flot';
  104. import 'vendor/flot/jquery.flot.selection';
  105. import 'vendor/flot/jquery.flot.time';
  106. import 'vendor/flot/jquery.flot.stack';
  107. import 'vendor/flot/jquery.flot.pie';
  108. import 'vendor/flot/jquery.flot.stackpercent';
  109. import 'vendor/flot/jquery.flot.fillbelow';
  110. import 'vendor/flot/jquery.flot.crosshair';
  111. import 'vendor/flot/jquery.flot.dashes';
  112. import 'vendor/flot/jquery.flot.gauge';
  113. const flotDeps = [
  114. 'jquery.flot',
  115. 'jquery.flot.pie',
  116. 'jquery.flot.time',
  117. 'jquery.flot.fillbelow',
  118. 'jquery.flot.crosshair',
  119. 'jquery.flot.stack',
  120. 'jquery.flot.selection',
  121. 'jquery.flot.stackpercent',
  122. 'jquery.flot.events',
  123. 'jquery.flot.gauge',
  124. ];
  125. for (const flotDep of flotDeps) {
  126. exposeToPlugin(flotDep, { fakeDep: 1 });
  127. }
  128. export function importPluginModule(path: string): Promise<any> {
  129. const builtIn = builtInPlugins[path];
  130. if (builtIn) {
  131. return Promise.resolve(builtIn);
  132. }
  133. return System.import(path);
  134. }
  135. export function importDataSourcePlugin(path: string): Promise<DataSourcePlugin> {
  136. return importPluginModule(path).then(pluginExports => {
  137. if (pluginExports.plugin) {
  138. return pluginExports.plugin as DataSourcePlugin;
  139. }
  140. if (pluginExports.Datasource) {
  141. const dsPlugin = new DataSourcePlugin(pluginExports.Datasource);
  142. dsPlugin.setComponentsFromLegacyExports(pluginExports);
  143. return dsPlugin;
  144. }
  145. throw new Error('Plugin module is missing DataSourcePlugin or Datasource constructor export');
  146. });
  147. }
  148. export function importAppPlugin(path: string): Promise<AppPlugin> {
  149. return importPluginModule(path).then(pluginExports => {
  150. return new AppPlugin(pluginExports.ConfigCtrl);
  151. });
  152. }
  153. export function importPanelPlugin(path: string): Promise<AngularPanelPlugin | ReactPanelPlugin> {
  154. return importPluginModule(path).then(pluginExports => {
  155. if (pluginExports.reactPanel) {
  156. return pluginExports.reactPanel as ReactPanelPlugin;
  157. } else {
  158. return new AngularPanelPlugin(pluginExports.PanelCtrl);
  159. }
  160. });
  161. }
  162. export function loadPluginCss(options) {
  163. if (config.bootData.user.lightTheme) {
  164. System.import(options.light + '!css');
  165. } else {
  166. System.import(options.dark + '!css');
  167. }
  168. }