plugin_loader.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 * as d3 from '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. '*': {
  41. esModule: true,
  42. authorization: true,
  43. }
  44. }
  45. });
  46. // add cache busting
  47. var systemLocate = System.locate;
  48. System.cacheBust = '?bust=' + Date.now();
  49. System.locate = function(load) {
  50. var System = this;
  51. return Promise.resolve(systemLocate.call(this, load)).then(function(address) {
  52. return address + System.cacheBust;
  53. });
  54. };
  55. function exposeToPlugin(name: string, component: any) {
  56. System.registerDynamic(name, [], true, function(require, exports, module) {
  57. module.exports = component;
  58. });
  59. }
  60. exposeToPlugin('lodash', _);
  61. exposeToPlugin('moment', moment);
  62. exposeToPlugin('jquery', jquery);
  63. exposeToPlugin('angular', angular);
  64. exposeToPlugin('d3', d3);
  65. exposeToPlugin('rxjs/Subject', Subject);
  66. exposeToPlugin('rxjs/Observable', Observable);
  67. // backward compatible path
  68. exposeToPlugin('vendor/npm/rxjs/Rx', {
  69. Subject: Subject,
  70. Observable: Observable
  71. });
  72. exposeToPlugin('app/features/dashboard/impression_store', {
  73. impressions: impressions,
  74. __esModule: true
  75. });
  76. exposeToPlugin('app/plugins/sdk', sdk);
  77. exposeToPlugin('app/core/utils/datemath', datemath);
  78. exposeToPlugin('app/core/utils/file_export', fileExport);
  79. exposeToPlugin('app/core/utils/flatten', flatten);
  80. exposeToPlugin('app/core/utils/kbn', kbn);
  81. exposeToPlugin('app/core/utils/ticks', ticks);
  82. exposeToPlugin('app/core/config', config);
  83. exposeToPlugin('app/core/time_series', TimeSeries);
  84. exposeToPlugin('app/core/time_series2', TimeSeries);
  85. exposeToPlugin('app/core/table_model', TableModel);
  86. exposeToPlugin('app/core/app_events', appEvents);
  87. exposeToPlugin('app/core/core_module', coreModule);
  88. exposeToPlugin('app/core/core', {
  89. coreModule: coreModule,
  90. appEvents: appEvents,
  91. contextSrv: contextSrv,
  92. __esModule: true
  93. });
  94. import 'vendor/flot/jquery.flot';
  95. import 'vendor/flot/jquery.flot.selection';
  96. import 'vendor/flot/jquery.flot.time';
  97. import 'vendor/flot/jquery.flot.stack';
  98. import 'vendor/flot/jquery.flot.pie';
  99. import 'vendor/flot/jquery.flot.stackpercent';
  100. import 'vendor/flot/jquery.flot.fillbelow';
  101. import 'vendor/flot/jquery.flot.crosshair';
  102. import 'vendor/flot/jquery.flot.dashes';
  103. const flotDeps = [
  104. 'jquery.flot', 'jquery.flot.pie', 'jquery.flot.time', 'jquery.flot.fillbelow', 'jquery.flot.crosshair',
  105. 'jquery.flot.stack', 'jquery.flot.selection', 'jquery.flot.stackpercent', 'jquery.flot.events'
  106. ];
  107. for (let flotDep of flotDeps) {
  108. exposeToPlugin(flotDep, {fakeDep: 1});
  109. }
  110. export function importPluginModule(path: string): Promise<any> {
  111. let builtIn = builtInPlugins[path];
  112. if (builtIn) {
  113. return Promise.resolve(builtIn);
  114. }
  115. return System.import(path);
  116. }
  117. export function loadPluginCss(options) {
  118. if (config.bootData.user.lightTheme) {
  119. System.import(options.light + '!css');
  120. } else {
  121. System.import(options.dark + '!css');
  122. }
  123. }