Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kbn/optimizer] link to kibanaReact/kibanaUtils plugins #62720

Merged
merged 30 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5fed504
[kbn/optimizer] link to data/kibanaReact/kibanaUtils plugins
spalger Apr 6, 2020
3a4d545
depend on normalize-path package
spalger Apr 6, 2020
916cc3f
typos
spalger Apr 6, 2020
959650f
avoid loading kibanaUtils and kibanaReact from urls
spalger Apr 6, 2020
aa2c3c2
Merge branch 'master' into implement/link-data-plugin
elasticmachine Apr 7, 2020
70e72eb
update types and tests, now that whole plugin is exported to window
spalger Apr 7, 2020
7c9c51e
update snapshot, removed export of `plugins` property
spalger Apr 7, 2020
dbac5f9
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
0c94376
fix condition, ignore things NOT in data/react/utils
spalger Apr 7, 2020
3a3fbe7
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
08d881f
make es_ui_shared a "static bundle" too
spalger Apr 7, 2020
a8bac54
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
26a99ce
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
d3ae538
Merge branch 'master' into implement/link-data-plugin
elasticmachine Apr 7, 2020
d77a892
Merge remote-tracking branch 'origin/implement/link-data-plugin' into…
spalger Apr 7, 2020
312cfd2
move kibana_utils/common usage to /public
spalger Apr 7, 2020
eb84a72
convert some more /common usage to /public
spalger Apr 7, 2020
1da77f8
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
ade184e
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 7, 2020
9f0c524
use async-download/ordered-execution for bootstrap script
spalger Apr 7, 2020
5eed7a9
fix typo
spalger Apr 7, 2020
a64b2a7
remove kibanaUtils bundle
spalger Apr 7, 2020
f14e9ee
remove kibanaReact bundle
spalger Apr 7, 2020
3a2b7dc
Revert "remove kibanaReact bundle"
spalger Apr 8, 2020
7f3b819
Revert "remove kibanaUtils bundle"
spalger Apr 8, 2020
4d71565
stop linking to the data plugin
spalger Apr 8, 2020
5195a80
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 8, 2020
2761f4d
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 8, 2020
4970b54
add comment pointing to async-download info
spalger Apr 8, 2020
06f5afb
Merge branch 'master' of github.com:elastic/kibana into implement/lin…
spalger Apr 8, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/kbn-optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"json-stable-stringify": "^1.0.1",
"loader-utils": "^1.2.3",
"node-sass": "^4.13.0",
"normalize-path": "^3.0.0",
"postcss-loader": "^3.0.0",
"raw-loader": "^3.1.0",
"resolve-url-loader": "^3.1.1",
Expand Down

Large diffs are not rendered by default.

68 changes: 64 additions & 4 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import Path from 'path';

import normalizePath from 'normalize-path';
import { stringifyRequest } from 'loader-utils';
import webpack from 'webpack';
// @ts-ignore
Expand All @@ -36,6 +37,59 @@ const ISTANBUL_PRESET_PATH = require.resolve('@kbn/babel-preset/istanbul_preset'
const PUBLIC_PATH_PLACEHOLDER = '__REPLACE_WITH_PUBLIC_PATH__';
const BABEL_PRESET_PATH = require.resolve('@kbn/babel-preset/webpack_preset');

const STATIC_BUNDLE_PLUGINS = [
{ id: 'data', dirname: 'data' },
{ id: 'kibanaReact', dirname: 'kibana_react' },
{ id: 'kibanaUtils', dirname: 'kibana_utils' },
{ id: 'esUiShared', dirname: 'es_ui_shared' },
];

/**
* Determine externals statements for require/import statements by looking
* for requests resolving to the primary public export of the data, kibanaReact,
* amd kibanaUtils plugins. If this module is being imported then rewrite
* the import to access the global `__kbnBundles__` variables and access
* the relavent properties from that global object.
*
* @param bundle
* @param context the directory containing the module which made `request`
* @param request the request for a module from a commonjs require() call or import statement
*/
function dynamicExternals(bundle: Bundle, context: string, request: string) {
// ignore imports that have loaders defined
if (request.includes('!')) {
return;
}

// don't allow any static bundle to rely on other static bundles
if (STATIC_BUNDLE_PLUGINS.some(p => bundle.id === p.id)) {
return;
}

// ignore requests that don't include a /data/public, /kibana_react/public, or
// /kibana_utils/public segment as a cheap way to avoid doing path resolution
// for paths that couldn't possibly resolve to what we're looking for
const reqToStaticBundle = STATIC_BUNDLE_PLUGINS.some(p =>
request.includes(`/${p.dirname}/public`)
);
if (!reqToStaticBundle) {
return;
}

// determine the most acurate resolution string we can without running full resolution
const rootRelative = normalizePath(
Path.relative(bundle.sourceRoot, Path.resolve(context, request))
);
for (const { id, dirname } of STATIC_BUNDLE_PLUGINS) {
if (rootRelative === `src/plugins/${dirname}/public`) {
return `__kbnBundles__['plugin/${id}']`;
}
}

// import doesn't match a root public import
return undefined;
}

export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
const commonConfig: webpack.Configuration = {
node: { fs: 'empty' },
Expand Down Expand Up @@ -63,7 +117,6 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
// When the entry point is loaded, assign it's exported `plugin`
// value to a key on the global `__kbnBundles__` object.
library: ['__kbnBundles__', `plugin/${bundle.id}`],
libraryExport: 'plugin',
}
: {}),
},
Expand All @@ -72,9 +125,16 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
noEmitOnErrors: true,
},

externals: {
...UiSharedDeps.externals,
},
externals: [
UiSharedDeps.externals,
function(context, request, cb) {
try {
cb(undefined, dynamicExternals(bundle, context, request));
} catch (error) {
cb(error, undefined);
}
},
],

plugins: [new CleanWebpackPlugin(), new DisallowedSyntaxPlugin()],

Expand Down
2 changes: 1 addition & 1 deletion src/core/public/plugins/plugin_loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test('`loadPluginBundles` creates a script tag and loads initializer', async ()

// Setup a fake initializer as if a plugin bundle had actually been loaded.
const fakeInitializer = jest.fn();
coreWindow.__kbnBundles__['plugin/plugin-a'] = fakeInitializer;
coreWindow.__kbnBundles__['plugin/plugin-a'] = { plugin: fakeInitializer };
// Call the onload callback
fakeScriptTag.onload();
await expect(loadPromise).resolves.toEqual(fakeInitializer);
Expand Down
33 changes: 22 additions & 11 deletions src/core/public/plugins/plugin_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type UnknownPluginInitializer = PluginInitializer<unknown, Record<string,
*/
export interface CoreWindow {
__kbnBundles__: {
[pluginBundleName: string]: UnknownPluginInitializer | undefined;
[pluginBundleName: string]: { plugin: UnknownPluginInitializer } | undefined;
};
}

Expand Down Expand Up @@ -70,9 +70,28 @@ export const loadPluginBundle: LoadPluginBundle = <
) =>
new Promise<PluginInitializer<TSetup, TStart, TPluginsSetup, TPluginsStart>>(
(resolve, reject) => {
const script = document.createElement('script');
const coreWindow = (window as unknown) as CoreWindow;
const exportId = `plugin/${pluginName}`;

const readPluginExport = () => {
const PluginExport: any = coreWindow.__kbnBundles__[exportId];
if (typeof PluginExport?.plugin !== 'function') {
reject(
new Error(`Definition of plugin "${pluginName}" should be a function (${bundlePath}).`)
);
} else {
resolve(
PluginExport.plugin as PluginInitializer<TSetup, TStart, TPluginsSetup, TPluginsStart>
);
}
};

if (coreWindow.__kbnBundles__[exportId]) {
readPluginExport();
return;
}

const script = document.createElement('script');
// Assumes that all plugin bundles get put into the bundles/plugins subdirectory
const bundlePath = addBasePath(`/bundles/plugin/${pluginName}/${pluginName}.plugin.js`);
script.setAttribute('src', bundlePath);
Expand All @@ -89,15 +108,7 @@ export const loadPluginBundle: LoadPluginBundle = <
// Wire up resolve and reject
script.onload = () => {
cleanupTag();

const initializer = coreWindow.__kbnBundles__[`plugin/${pluginName}`];
if (!initializer || typeof initializer !== 'function') {
reject(
new Error(`Definition of plugin "${pluginName}" should be a function (${bundlePath}).`)
);
} else {
resolve(initializer as PluginInitializer<TSetup, TStart, TPluginsSetup, TPluginsStart>);
}
readPluginExport();
};

script.onerror = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createInputControlVisController } from './vis_controller';
import { getControlsTab } from './components/editor/controls_tab';
import { OptionsTab } from './components/editor/options_tab';
import { InputControlVisDependencies } from './plugin';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/public';

export function createInputControlVisTypeDefinition(deps: InputControlVisDependencies) {
const InputControlVisController = createInputControlVisController(deps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/
import { DiscoverServices } from './build_services';
import { createGetterSetter } from '../../../../../plugins/kibana_utils/public';
import { search } from '../../../../../plugins/data/public';

let angularModule: any = null;
let services: DiscoverServices | null = null;
Expand Down Expand Up @@ -52,8 +54,6 @@ export const [getUrlTracker, setUrlTracker] = createGetterSetter<{

// EXPORT legacy static dependencies, should be migrated when available in a new version;
export { wrapInI18nContext } from 'ui/i18n';
import { search } from '../../../../../plugins/data/public';
import { createGetterSetter } from '../../../../../plugins/kibana_utils/common';
export const { getRequestInspectorStats, getResponseInspectorStats, tabifyAggResponse } = search;
export {
unhashUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_metric/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { createGetterSetter } from '../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getFormatService, setFormatService] = createGetterSetter<
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_table/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { createGetterSetter } from '../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getFormatService, setFormatService] = createGetterSetter<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { createGetterSetter } from '../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getFormatService, setFormatService] = createGetterSetter<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { metricsRequestHandler } from './request_handler';
import { EditorController } from './editor_controller';
// @ts-ignore
import { PANEL_TYPES } from '../../../../plugins/vis_type_timeseries/common/panel_types';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/public';

export const metricsVisDefinition = {
name: 'metrics',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../../plugins/kibana_utils/common';
import { createGetterSetter } from '../../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../../plugins/data/public';
import { IUiSettingsClient, NotificationsStart, SavedObjectsStart } from 'kibana/public';
import { dataPluginMock } from '../../../../../plugins/data/public/mocks';
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_vega/public/vega_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { i18n } from '@kbn/i18n';
import { DefaultEditorSize } from '../../vis_default_editor/public';
import { VegaVisualizationDependencies } from './plugin';
import { VegaVisEditor } from './components';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/common';
import { defaultFeedbackMessage } from '../../../../plugins/kibana_utils/public';

import { createVegaRequestHandler } from './vega_request_handler';
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/vis_type_vislib/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../../../plugins/kibana_utils/common';
import { createGetterSetter } from '../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getDataActions, setDataActions] = createGetterSetter<
Expand Down
18 changes: 14 additions & 4 deletions src/legacy/ui/ui_render/bootstrap/template.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,20 @@ if (window.__kbnStrictCsp__ && window.__kbnCspNotEnforced__) {
'{{regularBundlePath}}/commons.bundle.js',
],
urls: [
'{{regularBundlePath}}/{{appId}}.bundle.js',
{{#each styleSheetPaths}}
'{{this}}',
{{/each}}
{
deps: [
'{{regularBundlePath}}/plugin/data/data.plugin.js',
'{{regularBundlePath}}/plugin/kibanaUtils/kibanaUtils.plugin.js',
'{{regularBundlePath}}/plugin/esUiShared/esUiShared.plugin.js',
'{{regularBundlePath}}/plugin/kibanaReact/kibanaReact.plugin.js',
],
urls: [
'{{regularBundlePath}}/{{appId}}.bundle.js',
{{#each styleSheetPaths}}
'{{this}}',
{{/each}}
]
}
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { createGetterSetter } from '../../kibana_utils/common';
import { createGetterSetter } from '../../kibana_utils/public';
import { DocViewsRegistry } from './doc_views/doc_views_registry';

export const [getDocViewsRegistry, setDocViewsRegistry] = createGetterSetter<DocViewsRegistry>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Embeddable } from './embeddable';
import { EmbeddableInput } from './i_embeddable';
import { ViewMode } from '../types';
import { EmbeddableActionStorage, SerializedEvent } from './embeddable_action_storage';
import { of } from '../../../../kibana_utils/common';
import { of } from '../../../../kibana_utils/public';

class TestEmbeddable extends Embeddable<EmbeddableInput> {
public readonly type = 'test';
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/es_ui_shared/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "esUiShared",
"version": "kibana",
"ui": true
}
8 changes: 8 additions & 0 deletions src/plugins/es_ui_shared/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ export {
export { indices } from './indices';

export { useUIAceKeyboardMode } from './use_ui_ace_keyboard_mode';

/** dummy plugin, we just want kibanaReact to have its own bundle */
spalger marked this conversation as resolved.
Show resolved Hide resolved
export function plugin() {
return new (class KibanaReactPlugin {
spalger marked this conversation as resolved.
Show resolved Hide resolved
setup() {}
start() {}
})();
}
5 changes: 5 additions & 0 deletions src/plugins/kibana_react/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "kibanaReact",
"version": "kibana",
"ui": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { ComponentType, createElement as h } from 'react';
import { render as renderReact, unmountComponentAtNode } from 'react-dom';
import { UiComponent, UiComponentInstance } from '../../../kibana_utils/common';
import { UiComponent, UiComponentInstance } from '../../../kibana_utils/public';

/**
* Transform a React component into a `UiComponent`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { UiComponent } from '../../../kibana_utils/common';
import { UiComponent } from '../../../kibana_utils/public';
import { uiToReactComponent } from './ui_to_react_component';
import { reactToUiComponent } from './react_to_ui_component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { FC, createElement as h, useRef, useLayoutEffect, useMemo } from 'react';
import { UiComponent, UiComponentInstance } from '../../../kibana_utils/common';
import { UiComponent, UiComponentInstance } from '../../../kibana_utils/public';

/**
* Transforms `UiComponent` into a React component.
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/kibana_react/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export { Markdown, MarkdownSimple } from './markdown';
export { reactToUiComponent, uiToReactComponent } from './adapters';
export { useUrlTracker } from './use_url_tracker';
export { toMountPoint } from './util';

/** dummy plugin, we just want kibanaReact to have its own bundle */
export function plugin() {
return new (class KibanaReactPlugin {
spalger marked this conversation as resolved.
Show resolved Hide resolved
setup() {}
start() {}
})();
}
5 changes: 5 additions & 0 deletions src/plugins/kibana_utils/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "kibanaUtils",
"version": "kibana",
"ui": true
}
11 changes: 10 additions & 1 deletion src/plugins/kibana_utils/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

export {
calculateObjectHash,
createGetterSetter,
defer,
Defer,
Get,
Expand All @@ -31,6 +30,8 @@ export {
UiComponent,
UiComponentInstance,
url,
createGetterSetter,
defaultFeedbackMessage,
} from '../common';
export * from './core';
export * from './errors';
Expand Down Expand Up @@ -75,3 +76,11 @@ export {
} from './state_sync';
export { removeQueryParam, redirectWhenMissing, ensureDefaultIndexPattern } from './history';
export { applyDiff } from './state_management/utils/diff_object';

/** dummy plugin, we just want kibanaUtils to have its own bundle */
export function plugin() {
return new (class KibanaUtilsPlugin {
setup() {}
start() {}
})();
}