From fb444675756090f3dfb6a0ffbc2d7b5f42d8c16b Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 20 Apr 2020 16:14:50 -0700 Subject: [PATCH] implement `includeCoreBundle: boolean` config --- packages/kbn-optimizer/src/cli.ts | 10 ++++++- .../basic_optimization.test.ts.snap | 14 --------- .../basic_optimization.test.ts | 13 +++----- .../integration_tests/bundle_cache.test.ts | 30 ------------------- ...les.test.ts => get_plugin_bundles.test.ts} | 12 ++------ .../{get_bundles.ts => get_plugin_bundles.ts} | 15 ++-------- .../src/optimizer/optimizer_config.test.ts | 28 +++++++++++++---- .../src/optimizer/optimizer_config.ts | 24 +++++++++++++-- src/cli/cluster/run_kbn_optimizer.ts | 1 + .../tasks/build_kibana_platform_plugins.js | 1 + 10 files changed, 63 insertions(+), 85 deletions(-) rename packages/kbn-optimizer/src/optimizer/{get_bundles.test.ts => get_plugin_bundles.test.ts} (86%) rename packages/kbn-optimizer/src/optimizer/{get_bundles.ts => get_plugin_bundles.ts} (75%) diff --git a/packages/kbn-optimizer/src/cli.ts b/packages/kbn-optimizer/src/cli.ts index dcb4dcd35698d..c0bb408d60d8f 100644 --- a/packages/kbn-optimizer/src/cli.ts +++ b/packages/kbn-optimizer/src/cli.ts @@ -44,6 +44,11 @@ run( throw createFlagError('expected --cache to have no value'); } + const includeCoreBundle = flags.core ?? true; + if (typeof includeCoreBundle !== 'boolean') { + throw createFlagError('expected --core to have no value'); + } + const dist = flags.dist ?? false; if (typeof dist !== 'boolean') { throw createFlagError('expected --dist to have no value'); @@ -87,6 +92,7 @@ run( profileWebpack, extraPluginScanDirs, inspectWorkers, + includeCoreBundle, }); await runOptimizer(config) @@ -95,9 +101,10 @@ run( }, { flags: { - boolean: ['watch', 'oss', 'examples', 'dist', 'cache', 'profile', 'inspect-workers'], + boolean: ['core', 'watch', 'oss', 'examples', 'dist', 'cache', 'profile', 'inspect-workers'], string: ['workers', 'scan-dir'], default: { + core: true, examples: true, cache: true, 'inspect-workers': true, @@ -107,6 +114,7 @@ run( --workers max number of workers to use --oss only build oss plugins --profile profile the webpack builds and write stats.json files to build outputs + --no-core disable generating the core bundle --no-cache disable the cache --no-examples don't build the example plugins --dist create bundles that are suitable for inclusion in the Kibana distributable diff --git a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap index 4b9a6562ffeca..4b4bb1282d939 100644 --- a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap +++ b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap @@ -5,18 +5,6 @@ exports[`builds expected bundles, saves bundle counts to metadata: 1 async bundl exports[`builds expected bundles, saves bundle counts to metadata: OptimizerConfig 1`] = ` OptimizerConfig { "bundles": Array [ - Bundle { - "cache": BundleCache { - "path": /packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/target/public/.kbn-optimizer-cache, - "state": undefined, - }, - "contextDir": /packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core, - "entry": "./public/entry_point", - "id": "core", - "outputDir": /packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/core/target/public, - "sourceRoot": /packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo, - "type": "entry", - }, Bundle { "cache": BundleCache { "path": /packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/target/public/.kbn-optimizer-cache, @@ -71,6 +59,4 @@ OptimizerConfig { exports[`builds expected bundles, saves bundle counts to metadata: bar bundle 1`] = `"var __kbnBundles__=typeof __kbnBundles__===\\"object\\"?__kbnBundles__:{};__kbnBundles__[\\"plugin/bar\\"]=function(modules){function webpackJsonpCallback(data){var chunkIds=data[0];var moreModules=data[1];var moduleId,chunkId,i=0,resolves=[];for(;i { (msg.event?.type === 'bundle cached' || msg.event?.type === 'bundle not cached') && msg.state.phase === 'initializing' ); - assert('produce three bundle cache events while initializing', bundleCacheStates.length === 3); + assert('produce two bundle cache events while initializing', bundleCacheStates.length === 2); const initializedStates = msgs.filter(msg => msg.state.phase === 'initialized'); assert('produce at least one initialized event', initializedStates.length >= 1); @@ -101,12 +101,12 @@ it('builds expected bundles, saves bundle counts to metadata', async () => { const runningStates = msgs.filter(msg => msg.state.phase === 'running'); assert( - 'produce four or five "running" states', - runningStates.length === 4 || runningStates.length === 5 + 'produce two or three "running" states', + runningStates.length === 2 || runningStates.length === 3 ); const bundleNotCachedEvents = msgs.filter(msg => msg.event?.type === 'bundle not cached'); - assert('produce three "bundle not cached" events', bundleNotCachedEvents.length === 3); + assert('produce two "bundle not cached" events', bundleNotCachedEvents.length === 2); const successStates = msgs.filter(msg => msg.state.phase === 'success'); assert( @@ -124,10 +124,6 @@ it('builds expected bundles, saves bundle counts to metadata', async () => { ); assert('produce zero unexpected states', otherStates.length === 0, otherStates); - expect( - Fs.readFileSync(Path.resolve(MOCK_REPO_DIR, 'src/core/target/public/core.entry.js'), 'utf8') - ).toMatchSnapshot('core bundle'); - expect( Fs.readFileSync(Path.resolve(MOCK_REPO_DIR, 'plugins/foo/target/public/foo.plugin.js'), 'utf8') ).toMatchSnapshot('foo bundle'); @@ -202,7 +198,6 @@ it('uses cache on second run and exist cleanly', async () => { "initializing", "initializing", "initializing", - "initializing", "initialized", "success", ] diff --git a/packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts b/packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts index bfcaa04b679be..1bfd8d3fd073a 100644 --- a/packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts +++ b/packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts @@ -83,11 +83,6 @@ it('emits "bundle cached" event when everything is updated', async () => { expect(cacheEvents).toMatchInlineSnapshot(` Array [ - Object { - "bundle": , - "reason": "missing optimizer cache key", - "type": "bundle not cached", - }, Object { "bundle": , "type": "bundle cached", @@ -133,11 +128,6 @@ it('emits "bundle not cached" event when cacheKey is up to date but caching is d "reason": "cache disabled", "type": "bundle not cached", }, - Object { - "bundle": , - "reason": "cache disabled", - "type": "bundle not cached", - }, ] `); }); @@ -178,11 +168,6 @@ it('emits "bundle not cached" event when optimizerCacheKey is missing', async () "reason": "missing optimizer cache key", "type": "bundle not cached", }, - Object { - "bundle": , - "reason": "missing optimizer cache key", - "type": "bundle not cached", - }, ] `); }); @@ -228,11 +213,6 @@ it('emits "bundle not cached" event when optimizerCacheKey is outdated, includes "reason": "optimizer cache key mismatch", "type": "bundle not cached", }, - Object { - "bundle": , - "reason": "missing optimizer cache key", - "type": "bundle not cached", - }, ] `); }); @@ -271,11 +251,6 @@ it('emits "bundle not cached" event when cacheKey is missing', async () => { "reason": "missing cache key", "type": "bundle not cached", }, - Object { - "bundle": , - "reason": "missing optimizer cache key", - "type": "bundle not cached", - }, ] `); }); @@ -311,11 +286,6 @@ it('emits "bundle not cached" event when cacheKey is outdated', async () => { expect(cacheEvents).toMatchInlineSnapshot(` Array [ - Object { - "bundle": , - "reason": "missing optimizer cache key", - "type": "bundle not cached", - }, Object { "bundle": , "diff": "- Expected diff --git a/packages/kbn-optimizer/src/optimizer/get_bundles.test.ts b/packages/kbn-optimizer/src/optimizer/get_plugin_bundles.test.ts similarity index 86% rename from packages/kbn-optimizer/src/optimizer/get_bundles.test.ts rename to packages/kbn-optimizer/src/optimizer/get_plugin_bundles.test.ts index e52ec2ca664de..36dc0ca64c6ca 100644 --- a/packages/kbn-optimizer/src/optimizer/get_bundles.test.ts +++ b/packages/kbn-optimizer/src/optimizer/get_plugin_bundles.test.ts @@ -19,13 +19,13 @@ import { createAbsolutePathSerializer } from '@kbn/dev-utils'; -import { getBundles } from './get_bundles'; +import { getPluginBundles } from './get_plugin_bundles'; expect.addSnapshotSerializer(createAbsolutePathSerializer('/repo')); it('returns a bundle for core and each plugin', () => { expect( - getBundles( + getPluginBundles( [ { directory: '/repo/plugins/foo', @@ -47,14 +47,6 @@ it('returns a bundle for core and each plugin', () => { ).map(b => b.toSpec()) ).toMatchInlineSnapshot(` Array [ - Object { - "contextDir": /src/core, - "entry": "./public/entry_point", - "id": "core", - "outputDir": /src/core/target/public, - "sourceRoot": , - "type": "entry", - }, Object { "contextDir": /plugins/foo, "entry": "./public/index", diff --git a/packages/kbn-optimizer/src/optimizer/get_bundles.ts b/packages/kbn-optimizer/src/optimizer/get_plugin_bundles.ts similarity index 75% rename from packages/kbn-optimizer/src/optimizer/get_bundles.ts rename to packages/kbn-optimizer/src/optimizer/get_plugin_bundles.ts index b20ee6dcfdd53..4741cc3c30af7 100644 --- a/packages/kbn-optimizer/src/optimizer/get_bundles.ts +++ b/packages/kbn-optimizer/src/optimizer/get_plugin_bundles.ts @@ -23,17 +23,8 @@ import { Bundle } from '../common'; import { KibanaPlatformPlugin } from './kibana_platform_plugins'; -export function getBundles(plugins: KibanaPlatformPlugin[], repoRoot: string) { - const coreBundle = new Bundle({ - type: 'entry', - id: 'core', - entry: './public/entry_point', - sourceRoot: repoRoot, - contextDir: Path.resolve(repoRoot, 'src/core'), - outputDir: Path.resolve(repoRoot, 'src/core/target/public'), - }); - - const pluginBundles = plugins +export function getPluginBundles(plugins: KibanaPlatformPlugin[], repoRoot: string) { + return plugins .filter(p => p.isUiPlugin) .map( p => @@ -46,6 +37,4 @@ export function getBundles(plugins: KibanaPlatformPlugin[], repoRoot: string) { outputDir: Path.resolve(p.directory, 'target/public'), }) ); - - return [coreBundle, ...pluginBundles]; } diff --git a/packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts b/packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts index cc564dd4a8387..d4152133f289d 100644 --- a/packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts +++ b/packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts @@ -19,7 +19,7 @@ jest.mock('./assign_bundles_to_workers.ts'); jest.mock('./kibana_platform_plugins.ts'); -jest.mock('./get_bundles.ts'); +jest.mock('./get_plugin_bundles.ts'); import Path from 'path'; import Os from 'os'; @@ -90,6 +90,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 2, "pluginPaths": Array [], @@ -114,6 +115,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": false, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 2, "pluginPaths": Array [], @@ -138,6 +140,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 2, "pluginPaths": Array [], @@ -164,6 +167,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 2, "pluginPaths": Array [], @@ -187,6 +191,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 2, "pluginPaths": Array [], @@ -210,6 +215,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 100, "pluginPaths": Array [], @@ -230,6 +236,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": false, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 100, "pluginPaths": Array [], @@ -250,6 +257,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": false, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 100, "pluginPaths": Array [], @@ -271,6 +279,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": false, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 100, "pluginPaths": Array [], @@ -292,6 +301,7 @@ describe('OptimizerConfig::parseOptions()', () => { Object { "cache": true, "dist": false, + "includeCoreBundle": false, "inspectWorkers": false, "maxWorkerCount": 100, "pluginPaths": Array [], @@ -314,7 +324,7 @@ describe('OptimizerConfig::create()', () => { .assignBundlesToWorkers; const findKibanaPlatformPlugins: jest.Mock = jest.requireMock('./kibana_platform_plugins.ts') .findKibanaPlatformPlugins; - const getBundles: jest.Mock = jest.requireMock('./get_bundles.ts').getBundles; + const getPluginBundles: jest.Mock = jest.requireMock('./get_plugin_bundles.ts').getPluginBundles; beforeEach(() => { if ('mock' in OptimizerConfig.parseOptions) { @@ -326,7 +336,7 @@ describe('OptimizerConfig::create()', () => { { config: Symbol('worker config 2') }, ]); findKibanaPlatformPlugins.mockReturnValue(Symbol('new platform plugins')); - getBundles.mockReturnValue(Symbol('bundles')); + getPluginBundles.mockReturnValue([Symbol('bundle1'), Symbol('bundle2')]); jest.spyOn(OptimizerConfig, 'parseOptions').mockImplementation((): any => ({ cache: Symbol('parsed cache'), @@ -348,7 +358,10 @@ describe('OptimizerConfig::create()', () => { expect(config).toMatchInlineSnapshot(` OptimizerConfig { - "bundles": Symbol(bundles), + "bundles": Array [ + Symbol(bundle1), + Symbol(bundle2), + ], "cache": Symbol(parsed cache), "dist": Symbol(parsed dist), "inspectWorkers": Symbol(parsed inspect workers), @@ -383,7 +396,7 @@ describe('OptimizerConfig::create()', () => { } `); - expect(getBundles.mock).toMatchInlineSnapshot(` + expect(getPluginBundles.mock).toMatchInlineSnapshot(` Object { "calls": Array [ Array [ @@ -400,7 +413,10 @@ describe('OptimizerConfig::create()', () => { "results": Array [ Object { "type": "return", - "value": Symbol(bundles), + "value": Array [ + Symbol(bundle1), + Symbol(bundle2), + ], }, ], } diff --git a/packages/kbn-optimizer/src/optimizer/optimizer_config.ts b/packages/kbn-optimizer/src/optimizer/optimizer_config.ts index 7e1514058446b..d6336cf867470 100644 --- a/packages/kbn-optimizer/src/optimizer/optimizer_config.ts +++ b/packages/kbn-optimizer/src/optimizer/optimizer_config.ts @@ -23,7 +23,7 @@ import Os from 'os'; import { Bundle, WorkerConfig } from '../common'; import { findKibanaPlatformPlugins, KibanaPlatformPlugin } from './kibana_platform_plugins'; -import { getBundles } from './get_bundles'; +import { getPluginBundles } from './get_plugin_bundles'; function pickMaxWorkerCount(dist: boolean) { // don't break if cpus() returns nothing, or an empty array @@ -60,6 +60,9 @@ interface Options { pluginScanDirs?: string[]; /** absolute paths that should be added to the default scan dirs */ extraPluginScanDirs?: string[]; + + /** flag that causes the core bundle to be built along with plugins */ + includeCoreBundle?: boolean; } interface ParsedOptions { @@ -72,6 +75,7 @@ interface ParsedOptions { pluginPaths: string[]; pluginScanDirs: string[]; inspectWorkers: boolean; + includeCoreBundle: boolean; } export class OptimizerConfig { @@ -83,6 +87,7 @@ export class OptimizerConfig { const profileWebpack = !!options.profileWebpack; const inspectWorkers = !!options.inspectWorkers; const cache = options.cache !== false && !process.env.KBN_OPTIMIZER_NO_CACHE; + const includeCoreBundle = !!options.includeCoreBundle; const repoRoot = options.repoRoot; if (!Path.isAbsolute(repoRoot)) { @@ -134,13 +139,28 @@ export class OptimizerConfig { pluginScanDirs, pluginPaths, inspectWorkers, + includeCoreBundle, }; } static create(inputOptions: Options) { const options = OptimizerConfig.parseOptions(inputOptions); const plugins = findKibanaPlatformPlugins(options.pluginScanDirs, options.pluginPaths); - const bundles = getBundles(plugins, options.repoRoot); + const bundles = [ + ...(options.includeCoreBundle + ? [ + new Bundle({ + type: 'entry', + id: 'core', + entry: './public/entry_point', + sourceRoot: options.repoRoot, + contextDir: Path.resolve(options.repoRoot, 'src/core'), + outputDir: Path.resolve(options.repoRoot, 'src/core/target/public'), + }), + ] + : []), + ...getPluginBundles(plugins, options.repoRoot), + ]; return new OptimizerConfig( bundles, diff --git a/src/cli/cluster/run_kbn_optimizer.ts b/src/cli/cluster/run_kbn_optimizer.ts index 7752d4a45ab65..b811fc1f6b294 100644 --- a/src/cli/cluster/run_kbn_optimizer.ts +++ b/src/cli/cluster/run_kbn_optimizer.ts @@ -34,6 +34,7 @@ export function runKbnOptimizer(opts: Record, config: LegacyConfig) const optimizerConfig = OptimizerConfig.create({ repoRoot: REPO_ROOT, watch: true, + includeCoreBundle: true, oss: !!opts.oss, examples: !!opts.runExamples, pluginPaths: config.get('plugins.paths'), diff --git a/src/dev/build/tasks/build_kibana_platform_plugins.js b/src/dev/build/tasks/build_kibana_platform_plugins.js index 101d6bd15fc10..79cd698e4782c 100644 --- a/src/dev/build/tasks/build_kibana_platform_plugins.js +++ b/src/dev/build/tasks/build_kibana_platform_plugins.js @@ -29,6 +29,7 @@ export const BuildKibanaPlatformPluginsTask = { examples: false, watch: false, dist: true, + includeCoreBundle: true, }); await runOptimizer(optimizerConfig)