plugin_loader.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import config from 'app/core/config';
  9. import TimeSeries from 'app/core/time_series2';
  10. import TableModel from 'app/core/table_model';
  11. import {coreModule, appEvents, contextSrv} from 'app/core/core';
  12. import * as datemath from 'app/core/utils/datemath';
  13. import * as fileExport from 'app/core/utils/file_export';
  14. import * as flatten from 'app/core/utils/flatten';
  15. import * as ticks from 'app/core/utils/ticks';
  16. import builtInPlugins from './buit_in_plugins';
  17. import d3 from 'vendor/d3/d3';
  18. // rxjs
  19. import {Observable} from 'rxjs/Observable';
  20. import {Subject} from 'rxjs/Subject';
  21. // these imports add functions to Observable
  22. import 'rxjs/add/observable/empty';
  23. import 'rxjs/add/observable/from';
  24. import 'rxjs/add/operator/map';
  25. import 'rxjs/add/operator/combineAll';
  26. System.config({
  27. baseURL: 'public',
  28. defaultExtension: 'js',
  29. packages: {
  30. 'plugins': {
  31. defaultExtension: 'js'
  32. }
  33. },
  34. map: {
  35. text: 'vendor/plugin-text/text.js',
  36. css: 'vendor/plugin-css/css.js'
  37. },
  38. });
  39. // add cache busting
  40. var systemLocate = System.locate;
  41. System.cacheBust = '?bust=' + Date.now();
  42. System.locate = function(load) {
  43. var System = this;
  44. return Promise.resolve(systemLocate.call(this, load)).then(function(address) {
  45. return address + System.cacheBust;
  46. });
  47. };
  48. function exposeToPlugin(name: string, component: any) {
  49. System.registerDynamic(name, [], true, function(require, exports, module) {
  50. module.exports = component;
  51. });
  52. }
  53. exposeToPlugin('lodash', _);
  54. exposeToPlugin('moment', moment);
  55. exposeToPlugin('jquery', jquery);
  56. exposeToPlugin('angular', angular);
  57. exposeToPlugin('rxjs/Subject', Subject);
  58. exposeToPlugin('rxjs/Observable', Observable);
  59. exposeToPlugin('d3', d3);
  60. exposeToPlugin('app/plugins/sdk', sdk);
  61. exposeToPlugin('app/core/utils/datemath', datemath);
  62. exposeToPlugin('app/core/utils/file_export', fileExport);
  63. exposeToPlugin('app/core/utils/flatten', flatten);
  64. exposeToPlugin('app/core/utils/kbn', kbn);
  65. exposeToPlugin('app/core/utils/ticks', ticks);
  66. exposeToPlugin('app/core/config', config);
  67. exposeToPlugin('app/core/time_series', TimeSeries);
  68. exposeToPlugin('app/core/time_series2', TimeSeries);
  69. exposeToPlugin('app/core/table_model', TableModel);
  70. exposeToPlugin('app/core/app_events', appEvents);
  71. exposeToPlugin('app/core/core_module', coreModule);
  72. exposeToPlugin('app/core/core', {
  73. coreModule: coreModule,
  74. appEvents: appEvents,
  75. contextSrv: contextSrv,
  76. __esModule: true
  77. });
  78. import 'vendor/flot/jquery.flot';
  79. import 'vendor/flot/jquery.flot.selection';
  80. import 'vendor/flot/jquery.flot.time';
  81. import 'vendor/flot/jquery.flot.stack';
  82. import 'vendor/flot/jquery.flot.pie';
  83. import 'vendor/flot/jquery.flot.stackpercent';
  84. import 'vendor/flot/jquery.flot.fillbelow';
  85. import 'vendor/flot/jquery.flot.crosshair';
  86. import 'vendor/flot/jquery.flot.dashes';
  87. const flotDeps = [
  88. 'jquery.flot', 'jquery.flot.pie', 'jquery.flot.time', 'jquery.flot.fillbelow', 'jquery.flot.crosshair',
  89. 'jquery.flot.stack', 'jquery.flot.selection', 'jquery.flot.stackpercent', 'jquery.flot.events'
  90. ];
  91. for (let flotDep of flotDeps) {
  92. exposeToPlugin(flotDep, {fakeDep: 1});
  93. }
  94. export function importPluginModule(path: string): Promise<any> {
  95. let builtIn = builtInPlugins[path];
  96. if (builtIn) {
  97. return Promise.resolve(builtIn);
  98. }
  99. return System.import(path);
  100. }
  101. export function loadPluginCss(options) {
  102. if (config.bootData.user.lightTheme) {
  103. System.import(options.light + '!css');
  104. } else {
  105. System.import(options.dark + '!css');
  106. }
  107. }