Skip to content

Commit

Permalink
[autoloading] implement autoloading via modules
Browse files Browse the repository at this point in the history
Currently there is a long list of modules that is autoloaded for every app. This is because a number of angular directives/filters/services are not properly required by the code that needs them and rather than attempt to track down the relationships between every part of the app and the rest autoloading just makes sure that all of the defined modules are included. Since this was implicit it caused new apps which didn't need anything but chrome styles to be really heavy.

This change makes autoloading something you opt into by requiring a ui/autoload/* module. The options include:
 - `ui/autoload/styles` - include the base styles used by Kibana. This includes all of the styles listed in the ui/styles directory.
 - `ui/autoload/directives`
 - `ui/autoload/filters`
 - `ui/autoload/modules` - include angular and several ui modules that one might expect to be loaded by default. This list is hard coded into this file.
 - `ui/autoload/all` - includes all of the above lists

In order to support this change all plugins will likely need to update and include a `ui/autoload/*` module in their public/index.js file.
  • Loading branch information
spalger committed Jan 10, 2016
1 parent 5fa8118 commit b6a7449
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 110 deletions.
14 changes: 0 additions & 14 deletions src/plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ module.exports = function (kibana) {
'fieldFormats'
],

autoload: kibana.autoload.require.concat(
'plugins/kibana/discover',
'plugins/kibana/visualize',
'plugins/kibana/dashboard',
'plugins/kibana/settings',
'plugins/kibana/settings/sections',
'plugins/kibana/doc',
'plugins/kibana/settings/sections',
'ui/vislib',
'ui/agg_response',
'ui/agg_types',
'leaflet'
),

injectVars: function (server, options) {
let config = server.config();

Expand Down
12 changes: 10 additions & 2 deletions src/plugins/kibana/public/kibana.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// autoloading
require('ui/autoload/all');

// preloading (for faster webpack builds)
require('plugins/kibana/discover/index');
require('plugins/kibana/visualize/index');
require('plugins/kibana/dashboard/index');
require('plugins/kibana/settings/index');
require('plugins/kibana/doc/index');
require('plugins/kibana/settings/sections');
require('plugins/kibana/doc');
require('ui/vislib');
require('ui/agg_response');
require('ui/agg_types');
require('ui/timepicker');
require('leaflet');

var moment = require('moment-timezone');

var chrome = require('ui/chrome');
var routes = require('ui/routes');
var modules = require('ui/modules');
Expand Down
8 changes: 1 addition & 7 deletions src/plugins/statusPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ module.exports = function (kibana) {
title: 'Server Status',
main: 'plugins/statusPage/statusPage',
hidden: true,
url: '/status',

autoload: [].concat(
kibana.autoload.styles,
'ui/chrome',
'angular'
)
url: '/status'
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/plugins/statusPage/public/statusPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var $ = require('jquery');
var _ = require('lodash');
var notify = require('ui/notify');

require('ui/autoload/styles');
require('plugins/statusPage/statusPageMetric');
require('plugins/statusPage/statusPage.less');

Expand Down
16 changes: 15 additions & 1 deletion src/server/plugins/PluginApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ module.exports = class PluginApi {
this.config = kibana.config;
this.rootDir = kibana.rootDir;
this.package = require(join(pluginPath, 'package.json'));
this.autoload = require('../../ui/autoload');
this.Plugin = Plugin.scoped(kibana, pluginPath, this.package);
}

get uiExports() {
throw new Error('plugin.uiExports is not defined until initialize phase');
}

get autoload() {
console.warn(
`${this.package.id} accessed the autoload lists which are no longer available via the Plugin API.` +
'Use the `ui/autoload/*` modules instead.'
);

return {
directives: [],
filters: [],
styles: [],
modules: [],
require: []
};
}
};
10 changes: 7 additions & 3 deletions src/ui/UiApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var _ = require('lodash');
var { join } = require('path');
var autoload = require('./autoload');

class UiApp {
constructor(uiExports, spec) {
Expand All @@ -17,10 +16,16 @@ class UiApp {
this.description = this.spec.description;
this.icon = this.spec.icon;
this.hidden = this.spec.hidden;
this.autoloadOverrides = this.spec.autoload;
this.templateName = this.spec.templateName || 'ui_app';
this.url = `${spec.urlBasePath || ''}${this.spec.url || `/app/${this.id}`}`;

if (this.spec.autoload) {
console.warn(
`"autoload" (used by ${this.id} app) is no longer a valid app configuration directive.` +
'Use the \`ui/autoload/*\` modules instead.'
);
}

// once this resolves, no reason to run it again
this.getModules = _.once(this.getModules);

Expand All @@ -30,7 +35,6 @@ class UiApp {

getModules() {
return _.chain([
this.autoloadOverrides || autoload.require,
this.uiExports.find(_.get(this, 'spec.uses', [])),
this.uiExports.find(['chromeNavControls']),
])
Expand Down
2 changes: 1 addition & 1 deletion src/ui/appEntryTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ${pluginSlug}
*
*/
require('ui/chrome')
require('ui/chrome');
${requires}
require('ui/chrome').bootstrap(/* xoxo */);
Expand Down
82 changes: 0 additions & 82 deletions src/ui/autoload.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/ui/public/autoload/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('./modules');
require('./directives');
require('./filters');
require('./styles');
2 changes: 2 additions & 0 deletions src/ui/public/autoload/directives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const context = require.context('../directives', false, /[\/\\](?!\.|_)[^\/\\]+\.js/);
context.keys().forEach(key => context(key));
2 changes: 2 additions & 0 deletions src/ui/public/autoload/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const context = require.context('../filters', false, /[\/\\](?!\.|_)[^\/\\]+\.js/);
context.keys().forEach(key => context(key));
34 changes: 34 additions & 0 deletions src/ui/public/autoload/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require('angular');
require('ui/chrome');
require('ui/chrome/context');
require('ui/bind');
require('ui/bound_to_config_obj');
require('ui/config');
require('ui/courier');
require('ui/debounce');
require('ui/doc_title');
require('ui/elastic_textarea');
require('ui/es');
require('ui/events');
require('ui/fancy_forms');
require('ui/filter_bar');
require('ui/filter_manager');
require('ui/index_patterns');
require('ui/listen');
require('ui/notify');
require('ui/parse_query');
require('ui/persisted_log');
require('ui/private');
require('ui/promises');
require('ui/safe_confirm');
require('ui/state_management/app_state');
require('ui/state_management/global_state');
require('ui/storage');
require('ui/stringify/register');
require('ui/styleCompile');
require('ui/timefilter');
require('ui/tooltip');
require('ui/typeahead');
require('ui/url');
require('ui/validateDateInterval');
require('ui/watch_multi');
2 changes: 2 additions & 0 deletions src/ui/public/autoload/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const context = require.context('../styles', false, /[\/\\](?!mixins|variables|_|\.)[^\/\\]+\.less/);
context.keys().forEach(key => context(key));

0 comments on commit b6a7449

Please sign in to comment.