plugin_loader.ts 3.8 KB

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