Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from spalger/pr/61011
Browse files Browse the repository at this point in the history
implement `includeCoreBundle: boolean` config
joshdover authored Apr 21, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 329c044 + fb44467 commit 1fd2e06
Showing 10 changed files with 63 additions and 85 deletions.
10 changes: 9 additions & 1 deletion packages/kbn-optimizer/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
(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",
]
30 changes: 0 additions & 30 deletions packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts
Original file line number Diff line number Diff line change
@@ -83,11 +83,6 @@ it('emits "bundle cached" event when everything is updated', async () => {

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Object {
"bundle": <Bundle>,
"reason": "missing optimizer cache key",
"type": "bundle not cached",
},
Object {
"bundle": <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": <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": <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": <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": <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": <Bundle>,
"reason": "missing optimizer cache key",
"type": "bundle not cached",
},
Object {
"bundle": <Bundle>,
"diff": "- Expected
Original file line number Diff line number Diff line change
@@ -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": <absolute path>/src/core,
"entry": "./public/entry_point",
"id": "core",
"outputDir": <absolute path>/src/core/target/public,
"sourceRoot": <absolute path>,
"type": "entry",
},
Object {
"contextDir": <absolute path>/plugins/foo,
"entry": "./public/index",
Original file line number Diff line number Diff line change
@@ -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];
}
28 changes: 22 additions & 6 deletions packages/kbn-optimizer/src/optimizer/optimizer_config.test.ts
Original file line number Diff line number Diff line change
@@ -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),
],
},
],
}
24 changes: 22 additions & 2 deletions packages/kbn-optimizer/src/optimizer/optimizer_config.ts
Original file line number Diff line number Diff line change
@@ -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,
1 change: 1 addition & 0 deletions src/cli/cluster/run_kbn_optimizer.ts
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ export function runKbnOptimizer(opts: Record<string, any>, config: LegacyConfig)
const optimizerConfig = OptimizerConfig.create({
repoRoot: REPO_ROOT,
watch: true,
includeCoreBundle: true,
oss: !!opts.oss,
examples: !!opts.runExamples,
pluginPaths: config.get('plugins.paths'),
1 change: 1 addition & 0 deletions src/dev/build/tasks/build_kibana_platform_plugins.js
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ export const BuildKibanaPlatformPluginsTask = {
examples: false,
watch: false,
dist: true,
includeCoreBundle: true,
});

await runOptimizer(optimizerConfig)

0 comments on commit 1fd2e06

Please sign in to comment.