Skip to content

Commit

Permalink
[7.8] [kbn/optimizer] restore x-pack bundle banner (elastic#73767) (e…
Browse files Browse the repository at this point in the history
…lastic#73855)

Co-authored-by: spalger <spalger@users.noreply.github.com>
  • Loading branch information
Spencer and spalger authored Jul 30, 2020
1 parent f6f3f37 commit c037a6a
Show file tree
Hide file tree
Showing 10 changed files with 121 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",
"entry": "entry",
"id": "bar",
Expand Down Expand Up @@ -77,6 +78,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 @@ -37,6 +37,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;
}

export class Bundle {
Expand All @@ -56,6 +58,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'];

public readonly cache: BundleCache;

Expand All @@ -66,6 +70,7 @@ export class Bundle {
this.contextDir = spec.contextDir;
this.sourceRoot = spec.sourceRoot;
this.outputDir = spec.outputDir;
this.banner = spec.banner;

this.cache = new BundleCache(Path.resolve(this.outputDir, '.kbn-optimizer-cache'));
}
Expand Down Expand Up @@ -98,6 +103,7 @@ export class Bundle {
contextDir: this.contextDir,
sourceRoot: this.sourceRoot,
outputDir: this.outputDir,
banner: this.banner,
};
}
}
Expand Down Expand Up @@ -154,13 +160,21 @@ export function parseBundles(json: string) {
throw new Error('`bundles[]` must have an absolute path `outputDir` property');
}

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,
entry,
contextDir,
sourceRoot,
outputDir,
banner,
});
}
);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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: true,
});
Expand Down Expand Up @@ -99,7 +99,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 @@ -109,17 +109,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 All @@ -138,6 +138,7 @@ it('builds expected bundles, saves bundle counts to metadata', 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');

const foo = config.bundles.find((b) => b.id === 'foo')!;
expect(foo).toBeTruthy();
Expand Down Expand Up @@ -177,12 +178,24 @@ 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(2);

expect(baz.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/x-pack/baz/public/index.ts,
<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: true,
});
Expand All @@ -204,6 +217,7 @@ it('uses cache on second run and exist cleanly', async () => {
"initializing",
"initializing",
"initializing",
"initializing",
"initialized",
"success",
]
Expand Down
18 changes: 18 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 @@ -42,12 +42,18 @@ it('returns a bundle for core and each plugin', () => {
id: 'baz',
isUiPlugin: true,
},
{
directory: '/repo/x-pack/plugins/box',
id: 'box',
isUiPlugin: true,
},
],
'/repo'
).map((b) => b.toSpec())
).toMatchInlineSnapshot(`
Array [
Object {
"banner": undefined,
"contextDir": <absolute path>/plugins/foo,
"entry": "./public/index",
"id": "foo",
Expand All @@ -56,13 +62,25 @@ it('returns a bundle for core and each plugin', () => {
"type": "plugin",
},
Object {
"banner": undefined,
"contextDir": "/outside/of/repo/plugins/baz",
"entry": "./public/index",
"id": "baz",
"outputDir": "/outside/of/repo/plugins/baz/target/public",
"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,
"entry": "./public/index",
"id": "box",
"outputDir": <absolute path>/x-pack/plugins/box/target/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 @@ -35,6 +37,10 @@ export function getPluginBundles(plugins: KibanaPlatformPlugin[], repoRoot: stri
sourceRoot: repoRoot,
contextDir: p.directory,
outputDir: Path.resolve(p.directory, 'target/public'),
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,
})
);
}
6 changes: 5 additions & 1 deletion packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ export function getWebpackConfig(bundle: Bundle, worker: WorkerConfig) {
},
],

plugins: [new CleanWebpackPlugin(), new DisallowedSyntaxPlugin()],
plugins: [
new CleanWebpackPlugin(),
new DisallowedSyntaxPlugin(),
...(bundle.banner ? [new webpack.BannerPlugin({ banner: bundle.banner, raw: true })] : []),
],

module: {
// no parse rules for a few known large packages which have no require() statements
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 @@ -99,6 +99,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 c037a6a

Please sign in to comment.