plugin_loader.ts 4.0 KB

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