share core bundle with plugins (#67892)

Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
Spencer 2020-06-02 14:18:37 -07:00 committed by GitHub
parent 48e3bcdaec
commit 728a4b23e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 96 additions and 65 deletions

View file

@ -152,7 +152,7 @@ export class OptimizerConfig {
new Bundle({
type: 'entry',
id: 'core',
entry: './public/entry_point',
entry: './public/index',
sourceRoot: options.repoRoot,
contextDir: Path.resolve(options.repoRoot, 'src/core'),
outputDir: Path.resolve(options.repoRoot, 'src/core/target/public'),

View file

@ -38,11 +38,32 @@ const IS_CODE_COVERAGE = !!process.env.CODE_COVERAGE;
const ISTANBUL_PRESET_PATH = require.resolve('@kbn/babel-preset/istanbul_preset');
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' },
const SHARED_BUNDLES = [
{
type: 'entry',
id: 'core',
rootRelativeDir: 'src/core/public',
},
{
type: 'plugin',
id: 'data',
rootRelativeDir: 'src/plugins/data/public',
},
{
type: 'plugin',
id: 'kibanaReact',
rootRelativeDir: 'src/plugins/kibana_react/public',
},
{
type: 'plugin',
id: 'kibanaUtils',
rootRelativeDir: 'src/plugins/kibana_utils/public',
},
{
type: 'plugin',
id: 'esUiShared',
rootRelativeDir: 'src/plugins/es_ui_shared/public',
},
];
/**
@ -57,18 +78,8 @@ const STATIC_BUNDLE_PLUGINS = [
* @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;
}
// ignore requests that don't include a /{dirname}/public for one of our
// "static" bundles 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) {
// ignore imports that have loaders defined or are not relative seeming
if (request.includes('!') || !request.startsWith('.')) {
return;
}
@ -76,10 +87,15 @@ function dynamicExternals(bundle: Bundle, context: string, request: string) {
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}']`;
for (const sharedBundle of SHARED_BUNDLES) {
if (
rootRelative !== sharedBundle.rootRelativeDir ||
`${bundle.type}/${bundle.id}` === `${sharedBundle.type}/${sharedBundle.id}`
) {
continue;
}
return `__kbnBundles__['${sharedBundle.type}/${sharedBundle.id}']`;
}
// import doesn't match a root public import
@ -112,13 +128,9 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
info.absoluteResourcePath
)}${info.query}`,
jsonpFunction: `${bundle.id}_bundle_jsonpfunction`,
...(bundle.type === 'plugin'
? {
// 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}`],
}
: {}),
// When the entry point is loaded, assign it's default export
// to a key on the global `__kbnBundles__` object.
library: ['__kbnBundles__', `${bundle.type}/${bundle.id}`],
},
optimization: {

View file

@ -35,6 +35,8 @@
* @packageDocumentation
*/
import './index.scss';
import {
ChromeBadge,
ChromeBrand,
@ -363,3 +365,5 @@ export {
UiSettingsState,
NavType,
};
export { __kbnBootstrap__ } from './kbn_bootstrap';

View file

@ -25,39 +25,41 @@
* src/legacy/ui/ui_bundles/app_entry_template.js
*/
import './index.scss';
import { i18n } from '@kbn/i18n';
import { CoreSystem } from './core_system';
const injectedMetadata = JSON.parse(
document.querySelector('kbn-injected-metadata')!.getAttribute('data')!
);
/** @internal */
export function __kbnBootstrap__() {
const injectedMetadata = JSON.parse(
document.querySelector('kbn-injected-metadata')!.getAttribute('data')!
);
/**
* `apmConfig` would be populated with relavant APM RUM agent
* configuration if server is started with `ELASTIC_APM_ACTIVE=true`
*/
if (process.env.IS_KIBANA_DISTRIBUTABLE !== 'true' && injectedMetadata.vars.apmConfig != null) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { init } = require('@elastic/apm-rum');
init(injectedMetadata.vars.apmConfig);
}
/**
* `apmConfig` would be populated with relavant APM RUM agent
* configuration if server is started with `ELASTIC_APM_ACTIVE=true`
*/
if (process.env.IS_KIBANA_DISTRIBUTABLE !== 'true' && injectedMetadata.vars.apmConfig != null) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { init } = require('@elastic/apm-rum');
init(injectedMetadata.vars.apmConfig);
}
i18n
.load(injectedMetadata.i18n.translationsUrl)
.catch((e) => e)
.then(async (i18nError) => {
const coreSystem = new CoreSystem({
injectedMetadata,
rootDomElement: document.body,
browserSupportsCsp: !(window as any).__kbnCspNotEnforced__,
i18n
.load(injectedMetadata.i18n.translationsUrl)
.catch((e) => e)
.then(async (i18nError) => {
const coreSystem = new CoreSystem({
injectedMetadata,
rootDomElement: document.body,
browserSupportsCsp: !(window as any).__kbnCspNotEnforced__,
});
const setup = await coreSystem.setup();
if (i18nError && setup) {
setup.fatalErrors.add(i18nError);
}
await coreSystem.start();
});
const setup = await coreSystem.setup();
if (i18nError && setup) {
setup.fatalErrors.add(i18nError);
}
await coreSystem.start();
});
}

View file

@ -25,6 +25,9 @@ import { Type } from '@kbn/config-schema';
import { UnregisterCallback } from 'history';
import { UserProvidedValues as UserProvidedValues_2 } from 'src/core/server/types';
// @internal (undocumented)
export function __kbnBootstrap__(): void;
// @public
export interface App<HistoryLocationState = unknown> extends AppBase {
appRoute?: string;

View file

@ -73,12 +73,23 @@ if (window.__kbnStrictCsp__ && window.__kbnCspNotEnforced__) {
}
load([
{{#each jsDependencyPaths}}
'{{this}}',
{{/each}}
{{#each jsDependencyPaths}}
'{{this}}',
{{/each}}
], function () {
{{#unless legacyBundlePath}}
if (!__kbnBundles__ || !__kbnBundles__['entry/core'] || typeof __kbnBundles__['entry/core'].__kbnBootstrap__ !== 'function') {
console.error('entry/core bundle did not load correctly');
failure();
} else {
__kbnBundles__['entry/core'].__kbnBootstrap__()
}
{{/unless}}
load([
'{{entryBundlePath}}',
{{#if legacyBundlePath}}
'{{legacyBundlePath}}',
{{/if}}
{{#each styleSheetPaths}}
'{{this}}',
{{/each}}

View file

@ -173,6 +173,7 @@ export function uiRenderMixin(kbnServer, server, config) {
`${regularBundlePath}/commons.bundle.js`,
]),
`${regularBundlePath}/core/core.entry.js`,
...kpPluginIds.map(
(pluginId) => `${regularBundlePath}/plugin/${pluginId}/${pluginId}.plugin.js`
),
@ -199,9 +200,7 @@ export function uiRenderMixin(kbnServer, server, config) {
jsDependencyPaths,
styleSheetPaths,
publicPathMap,
entryBundlePath: isCore
? `${regularBundlePath}/core/core.entry.js`
: `${regularBundlePath}/${app.getId()}.bundle.js`,
legacyBundlePath: isCore ? undefined : `${regularBundlePath}/${app.getId()}.bundle.js`,
},
});