Skip to content

Commit

Permalink
[7.9] [kbn/optimizer] restore x-pack bundle banner (#73767) (#73852)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <[email protected]>
  • Loading branch information
Spencer and spalger authored Jul 30, 2020
1 parent d31d530 commit c55e81f
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "baz",
"ui": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// eslint-disable-next-line no-console
console.log('plugin in an x-pack dir');
2 changes: 2 additions & 0 deletions packages/kbn-optimizer/src/common/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ it('creates cache keys', () => {
"/foo/bar/c": 789,
},
"spec": Object {
"banner": undefined,
"contextDir": "/foo/bar",
"id": "bar",
"manifestPath": undefined,
Expand Down Expand Up @@ -80,6 +81,7 @@ it('parses bundles from JSON specs', () => {
expect(bundles).toMatchInlineSnapshot(`
Array [
Bundle {
"banner": undefined,
"cache": BundleCache {
"path": "/foo/bar/target/.kbn-optimizer-cache",
"state": undefined,
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-optimizer/src/common/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface BundleSpec {
readonly sourceRoot: string;
/** Absolute path to the directory where output should be written */
readonly outputDir: string;
/** Banner that should be written to all bundle JS files */
readonly banner?: string;
/** Absolute path to a kibana.json manifest file, if omitted we assume there are not dependenices */
readonly manifestPath?: string;
}
Expand All @@ -64,6 +66,8 @@ export class Bundle {
public readonly sourceRoot: BundleSpec['sourceRoot'];
/** Absolute path to the output directory for this bundle */
public readonly outputDir: BundleSpec['outputDir'];
/** Banner that should be written to all bundle JS files */
public readonly banner: BundleSpec['banner'];
/**
* Absolute path to a manifest file with "requiredBundles" which will be
* used to allow bundleRefs from this bundle to the exports of another bundle.
Expand All @@ -81,6 +85,7 @@ export class Bundle {
this.sourceRoot = spec.sourceRoot;
this.outputDir = spec.outputDir;
this.manifestPath = spec.manifestPath;
this.banner = spec.banner;

this.cache = new BundleCache(Path.resolve(this.outputDir, '.kbn-optimizer-cache'));
}
Expand Down Expand Up @@ -112,6 +117,7 @@ export class Bundle {
sourceRoot: this.sourceRoot,
outputDir: this.outputDir,
manifestPath: this.manifestPath,
banner: this.banner,
};
}

Expand Down Expand Up @@ -220,13 +226,21 @@ export function parseBundles(json: string) {
}
}

const { banner } = spec;
if (banner !== undefined) {
if (!(typeof banner === 'string')) {
throw new Error('`bundles[]` must have a string `banner` property');
}
}

return new Bundle({
type,
id,
publicDirNames,
contextDir,
sourceRoot,
outputDir,
banner,
manifestPath,
});
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ afterAll(async () => {
it('builds expected bundles, saves bundle counts to metadata', async () => {
const config = OptimizerConfig.create({
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins'), Path.resolve(MOCK_REPO_DIR, 'x-pack')],
maxWorkerCount: 1,
dist: false,
});
Expand Down Expand Up @@ -100,7 +100,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 two bundle cache events while initializing', bundleCacheStates.length === 2);
assert('produce three bundle cache events while initializing', bundleCacheStates.length === 3);

const initializedStates = msgs.filter((msg) => msg.state.phase === 'initialized');
assert('produce at least one initialized event', initializedStates.length >= 1);
Expand All @@ -110,17 +110,17 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {

const runningStates = msgs.filter((msg) => msg.state.phase === 'running');
assert(
'produce two or three "running" states',
runningStates.length === 2 || runningStates.length === 3
'produce three to five "running" states',
runningStates.length >= 3 && runningStates.length <= 5
);

const bundleNotCachedEvents = msgs.filter((msg) => msg.event?.type === 'bundle not cached');
assert('produce two "bundle not cached" events', bundleNotCachedEvents.length === 2);
assert('produce three "bundle not cached" events', bundleNotCachedEvents.length === 3);

const successStates = msgs.filter((msg) => msg.state.phase === 'success');
assert(
'produce one or two "compiler success" states',
successStates.length === 1 || successStates.length === 2
'produce one to three "compiler success" states',
successStates.length >= 1 && successStates.length <= 3
);

const otherStates = msgs.filter(
Expand Down Expand Up @@ -175,12 +175,26 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
<absolute path>/packages/kbn-ui-shared-deps/public_path_module_creator.js,
]
`);

const baz = config.bundles.find((b) => b.id === 'baz')!;
expect(baz).toBeTruthy();
baz.cache.refresh();
expect(baz.cache.getModuleCount()).toBe(3);

expect(baz.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/x-pack/baz/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/x-pack/baz/public/index.ts,
<absolute path>/packages/kbn-optimizer/target/worker/entry_point_creator.js,
<absolute path>/packages/kbn-ui-shared-deps/public_path_module_creator.js,
]
`);
});

it('uses cache on second run and exist cleanly', async () => {
const config = OptimizerConfig.create({
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins'), Path.resolve(MOCK_REPO_DIR, 'x-pack')],
maxWorkerCount: 1,
dist: false,
});
Expand All @@ -202,6 +216,7 @@ it('uses cache on second run and exist cleanly', async () => {
"initializing",
"initializing",
"initializing",
"initializing",
"initialized",
"success",
]
Expand All @@ -211,7 +226,7 @@ it('uses cache on second run and exist cleanly', async () => {
it('prepares assets for distribution', async () => {
const config = OptimizerConfig.create({
repoRoot: MOCK_REPO_DIR,
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins')],
pluginScanDirs: [Path.resolve(MOCK_REPO_DIR, 'plugins'), Path.resolve(MOCK_REPO_DIR, 'x-pack')],
maxWorkerCount: 1,
dist: true,
});
Expand All @@ -224,6 +239,7 @@ it('prepares assets for distribution', async () => {
'1 async bundle'
);
expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle');
expectFileMatchesSnapshotWithCompression('x-pack/baz/target/public/baz.plugin.js', 'baz bundle');
});

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/kbn-optimizer/src/optimizer/get_plugin_bundles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ it('returns a bundle for core and each plugin', () => {
extraPublicDirs: [],
manifestPath: '/outside/of/repo/plugins/baz/kibana.json',
},
{
directory: '/repo/x-pack/plugins/box',
id: 'box',
isUiPlugin: true,
extraPublicDirs: [],
manifestPath: '/repo/x-pack/plugins/box/kibana.json',
},
],
'/repo'
).map((b) => b.toSpec())
).toMatchInlineSnapshot(`
Array [
Object {
"banner": undefined,
"contextDir": <absolute path>/plugins/foo,
"id": "foo",
"manifestPath": <absolute path>/plugins/foo/kibana.json,
Expand All @@ -65,6 +73,7 @@ it('returns a bundle for core and each plugin', () => {
"type": "plugin",
},
Object {
"banner": undefined,
"contextDir": "/outside/of/repo/plugins/baz",
"id": "baz",
"manifestPath": "/outside/of/repo/plugins/baz/kibana.json",
Expand All @@ -75,6 +84,20 @@ it('returns a bundle for core and each plugin', () => {
"sourceRoot": <absolute path>,
"type": "plugin",
},
Object {
"banner": "/*! Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one or more contributor license agreements.
* Licensed under the Elastic License; you may not use this file except in compliance with the Elastic License. */
",
"contextDir": <absolute path>/x-pack/plugins/box,
"id": "box",
"manifestPath": <absolute path>/x-pack/plugins/box/kibana.json,
"outputDir": <absolute path>/x-pack/plugins/box/target/public,
"publicDirNames": Array [
"public",
],
"sourceRoot": <absolute path>,
"type": "plugin",
},
]
`);
});
6 changes: 6 additions & 0 deletions packages/kbn-optimizer/src/optimizer/get_plugin_bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { Bundle } from '../common';
import { KibanaPlatformPlugin } from './kibana_platform_plugins';

export function getPluginBundles(plugins: KibanaPlatformPlugin[], repoRoot: string) {
const xpackDirSlash = Path.resolve(repoRoot, 'x-pack') + Path.sep;

return plugins
.filter((p) => p.isUiPlugin)
.map(
Expand All @@ -36,6 +38,10 @@ export function getPluginBundles(plugins: KibanaPlatformPlugin[], repoRoot: stri
contextDir: p.directory,
outputDir: Path.resolve(p.directory, 'target/public'),
manifestPath: p.manifestPath,
banner: p.directory.startsWith(xpackDirSlash)
? `/*! Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one or more contributor license agreements.\n` +
` * Licensed under the Elastic License; you may not use this file except in compliance with the Elastic License. */\n`
: undefined,
})
);
}
1 change: 1 addition & 0 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
new CleanWebpackPlugin(),
new DisallowedSyntaxPlugin(),
new BundleRefsPlugin(bundle, bundleRefs),
...(bundle.banner ? [new webpack.BannerPlugin({ banner: bundle.banner, raw: true })] : []),
],

module: {
Expand Down
1 change: 1 addition & 0 deletions src/dev/precommit_hook/casing_check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const IGNORE_DIRECTORY_GLOBS = [
'test/functional/fixtures/es_archiver/visualize_source-filters',
'packages/kbn-pm/src/utils/__fixtures__/*',
'x-pack/dev-tools',
'packages/kbn-optimizer/src/__fixtures__/mock_repo/x-pack',
];

/**
Expand Down

0 comments on commit c55e81f

Please sign in to comment.