Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yctercero/kibana into endpoint_bu…
Browse files Browse the repository at this point in the history
…ilder
  • Loading branch information
yctercero committed Jul 30, 2020
2 parents 923d0aa + c21474b commit c613305
Show file tree
Hide file tree
Showing 65 changed files with 1,160 additions and 383 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ module.exports = {
'x-pack/test_utils/**/*',
'x-pack/gulpfile.js',
'x-pack/plugins/apm/public/utils/testHelpers.js',
'x-pack/plugins/canvas/shareable_runtime/postcss.config.js',
],
rules: {
'import/no-extraneous-dependencies': [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@
"pixelmatch": "^5.1.0",
"pkg-up": "^2.0.0",
"pngjs": "^3.4.0",
"postcss": "^7.0.26",
"postcss": "^7.0.32",
"postcss-url": "^8.0.0",
"prettier": "^2.0.5",
"proxyquire": "1.8.0",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"loader-utils": "^1.2.3",
"node-sass": "^4.13.0",
"normalize-path": "^3.0.0",
"postcss": "^7.0.32",
"postcss-loader": "^3.0.0",
"raw-loader": "^3.1.0",
"resolve-url-loader": "^3.1.1",
Expand Down
File renamed without changes.
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
Expand Up @@ -17,6 +17,5 @@
* under the License.
*/

module.exports = {
plugins: [require('autoprefixer')()],
};
// 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 @@ -161,6 +161,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
Array [
<absolute path>/node_modules/css-loader/package.json,
<absolute path>/node_modules/style-loader/package.json,
<absolute path>/packages/kbn-optimizer/postcss.config.js,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.scss,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.ts,
Expand All @@ -171,7 +172,20 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/legacy/ui/public/styles/_globals_v7dark.scss,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/src/legacy/ui/public/styles/_globals_v7light.scss,
<absolute path>/packages/kbn-optimizer/target/worker/entry_point_creator.js,
<absolute path>/packages/kbn-optimizer/target/worker/postcss.config.js,
<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,
]
`);
Expand All @@ -180,7 +194,7 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {
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 () => {
'foo 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,
})
);
}
3 changes: 2 additions & 1 deletion packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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 Expand Up @@ -151,7 +152,7 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
options: {
sourceMap: !worker.dist,
config: {
path: require.resolve('./postcss.config'),
path: require.resolve('@kbn/optimizer/postcss.config.js'),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-storybook/lib/webpack.dll.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module.exports = {
loader: 'postcss-loader',
options: {
config: {
path: path.resolve(REPO_ROOT, 'src/optimize/postcss.config.js'),
path: require.resolve('@kbn/optimizer/postcss.config.js'),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-storybook/storybook_config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = async ({ config }) => {
loader: 'postcss-loader',
options: {
config: {
path: resolve(REPO_ROOT, 'src/optimize/'),
path: require.resolve('@kbn/optimizer/postcss.config.js'),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-ui-framework/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const sass = require('node-sass');
const postcss = require('postcss');
const postcssConfig = require('../../src/optimize/postcss.config');
const postcssConfig = require('@kbn/optimizer/postcss.config.js');
const chokidar = require('chokidar');
const { debounce } = require('lodash');

Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-ui-framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@babel/core": "^7.10.2",
"@elastic/eui": "0.0.55",
"@kbn/babel-preset": "1.0.0",
"autoprefixer": "^9.7.4",
"@kbn/optimizer": "1.0.0",
"babel-loader": "^8.0.6",
"brace": "0.11.1",
"chalk": "^2.4.2",
Expand All @@ -54,7 +54,7 @@
"keymirror": "0.1.1",
"moment": "^2.24.0",
"node-sass": "^4.13.1",
"postcss": "^7.0.26",
"postcss": "^7.0.32",
"postcss-loader": "^3.0.0",
"raw-loader": "^3.1.0",
"react-dom": "^16.12.0",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-ui-shared-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"custom-event-polyfill": "^0.3.0",
"elasticsearch-browser": "^16.7.0",
"jquery": "^3.5.0",
"mini-css-extract-plugin": "0.8.0",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",
"react": "^16.12.0",
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 @@ -105,6 +105,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
2 changes: 1 addition & 1 deletion src/optimize/base_optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { IS_KIBANA_DISTRIBUTABLE } from '../legacy/utils';
import { fromRoot } from '../core/server/utils';
import { PUBLIC_PATH_PLACEHOLDER } from './public_path_placeholder';

const POSTCSS_CONFIG_PATH = require.resolve('./postcss.config');
const POSTCSS_CONFIG_PATH = require.resolve('./postcss.config.js');
const BABEL_PRESET_PATH = require.resolve('@kbn/babel-preset/webpack_preset');
const ISTANBUL_PRESET_PATH = require.resolve('@kbn/babel-preset/istanbul_preset');
const EMPTY_MODULE_PATH = require.resolve('./intentionally_empty_module.js');
Expand Down
Loading

0 comments on commit c613305

Please sign in to comment.