plugin_loader.ts 4.5 KB

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