kibana/src/optimize/lazy/lazy_optimizer.js
Spencer c65da14d7c
[optimizer] extract plugin discovery (#14745)
* [plugins] extract plugin discover from the kibana server

* integrate plugin discovery module with server

* [pluginDiscovery] fully extend config before checking enabled status

* [pluginDiscovery] limit arbitrary defaults in PluginSpec

* [ui/navLink] fix tests

* [ui/injectedVars] fix tests

* [ui/app] fix tests

* [server/plugins] convert init to callPluginHook tests

* [build/verifyTranslations] update verify logic

* [pluginDiscovery] remove rx utils

* fix i18n transaltion key name

* [pluginDiscovery] do kibana version checks as a part of discovery

* [pluginDiscovery/createPacksInDirectory$] clarify error handling

* [eslint] fix lint errors

* [uiApp/modules] ensure load order matches master

* [uiBundle] use known uiExport type for providers

* [uiExports] use the `home` export type

* [uiExports] validate that all uiExport types are known

* [timelion] remove archaic/broken bwc check

* revert some stragler changes

* [pluginSpecs] reformat comments

* [uiBundle] rebel and use more fcb 😬

* correct comment

* [server/waitForPluginsInit] describe queues var

* [server/plugins] prevent multiple calls to next() by using single then()

* [uiApp] remove archaic deprecation warning

* [uiApp] tighten up tests

* [pluginDiscovery/errors] remove $ from symbol var

* [pluginDiscovery/reduceExportSpecs] update docs

* [pluginDiscovery/findPluginSpecs] rightVersion -> isRightVersion

* [pluginDiscovery/findPluginSpecs] fix typos

* [uiApps/getById] use Map() rather than memoize

* save

* [savedObjects/mappings] use uiExports.savedObjectMappings

* [server/mapping/indexMapping] update tests, addRootProperties method removed

* [uiExports] "embeddableHandlers" -> "embeddableFactories"

* [pluginDiscovery] fix pluralization of invalidVersionSpec$

* [pluginDiscover] add README

* [pluginDiscovery/reduceExportSpecs] don't ignore fasly spec values, just undefined

* [ui/exportTypes] use better reducer names

* [ui/uiExports] add README

* fix links

* [pluginDiscovery/readme] expand examples

* [pluginDiscovery/readme] clean up reduceExportSpecs() doc

* [ui/uiExports/readme] cleanup example

* [pluginDiscovery] remove needless use of lodash

* [pluginDiscovery/waitForComplete] use better name

* [pluginDiscovery/findPluginSpecs] use fixtures rather than core_plugins

* [pluginDiscovery/stubSchema] use deafult: false

* [plguinDiscovery/pluginConfig] add tests

* typo

* [uiExports/readme] fix link

* [pluginDiscovery/packAtPath] fail with InvalidPackError if path is not a string

* [pluginDiscovery/packAtPath] rely on error.code to detect missing package.json file

* [pluginDiscovery/packAtPath] only attempt to get pack when observable is subscribed

* [pluginDiscovery/packAtPath] add tests

* [pluginDiscovery/pluginPack] move absolute path checks into fs lib

* [pluginDiscovery/packsInDirectory] fix error type check

* [pluginDiscovery/pluginPack/tests] share some utils

* [pluginDiscovery/packsInDirectory] add tests

* [pluginDiscovery/pluginPack] only cast undefined to array

* [pluginDiscovery/pluginPack] add tests

* [pluginDiscovery/pluginSpec/isVersionCompatible] add tests

* [pluginDiscovery/InvalidPluginError] be less redundant

* [pluginDiscovery/pluginSpec] verify config service is passed to isEnabled()

* [pluginDiscovery/pluginSpec] add tests

* fix "existent" spelling
2017-12-05 18:11:50 -07:00

119 lines
3.1 KiB
JavaScript

import BaseOptimizer from '../base_optimizer';
import WeirdControlFlow from './weird_control_flow';
import { once } from 'lodash';
import { join } from 'path';
import { createBundlesRoute } from '../bundles_route';
export default class LazyOptimizer extends BaseOptimizer {
constructor(opts) {
super(opts);
this.log = opts.log || (() => null);
this.prebuild = opts.prebuild || false;
this.timer = {
ms: null,
start: () => this.timer.ms = Date.now(),
end: () => this.timer.ms = ((Date.now() - this.timer.ms) / 1000).toFixed(2)
};
this.build = new WeirdControlFlow();
}
async init() {
this.initializing = true;
await this.uiBundles.writeEntryFiles();
await this.initCompiler();
this.compiler.plugin('watch-run', (w, webpackCb) => {
this.build.work(once(() => {
this.timer.start();
this.logRunStart();
webpackCb();
}));
});
this.compiler.plugin('done', stats => {
if (!stats.hasErrors() && !stats.hasWarnings()) {
this.logRunSuccess();
this.build.success();
return;
}
const err = this.failedStatsToError(stats);
this.logRunFailure(err);
this.build.failure(err);
this.watching.invalidate();
});
this.watching = this.compiler.watch({ aggregateTimeout: 200 }, err => {
if (err) {
this.log('fatal', err);
process.exit(1);
}
});
const buildPromise = this.build.get();
if (this.prebuild) await buildPromise;
this.initializing = false;
this.log(['info', 'optimize'], {
tmpl: `Lazy optimization of ${this.uiBundles.getDescription()} ready`,
bundles: this.uiBundles.getIds()
});
}
async getPath(relativePath) {
await this.build.get();
return join(this.compiler.outputPath, relativePath);
}
bindToServer(server, basePath) {
// calling `build.get()` resolves when the build is
// "stable" (the compiler is not running) so this pauses
// all requests received while the compiler is running
// and lets the continue once it is done.
server.ext('onRequest', (request, reply) => {
this.build.get()
.then(() => reply.continue())
.catch(reply);
});
server.route(createBundlesRoute({
bundlesPath: this.compiler.outputPath,
basePublicPath: basePath
}));
}
logRunStart() {
this.log(['info', 'optimize'], {
tmpl: `Lazy optimization started`,
bundles: this.uiBundles.getIds()
});
}
logRunSuccess() {
this.log(['info', 'optimize'], {
tmpl: 'Lazy optimization <%= status %> in <%= seconds %> seconds',
bundles: this.uiBundles.getIds(),
status: 'success',
seconds: this.timer.end()
});
}
logRunFailure(err) {
// errors during initialization to the server, unlike the rest of the
// errors produced here. Lets not muddy the console with extra errors
if (this.initializing) return;
this.log(['fatal', 'optimize'], {
tmpl: 'Lazy optimization <%= status %> in <%= seconds %> seconds<%= err %>',
bundles: this.uiBundles.getIds(),
status: 'failed',
seconds: this.timer.end(),
err: err
});
}
}