Skip to content

Commit

Permalink
[7.x] [kbn/ui-shared-deps] track asset sizes (#78718) (#78899)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: spalger <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2020
1 parent b7259e6 commit a5692c1
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions packages/kbn-ui-shared-deps/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
*/

const Path = require('path');
const Fs = require('fs');

const { run, createFailError } = require('@kbn/dev-utils');
const { run, createFailError, CiStatsReporter } = require('@kbn/dev-utils');
const webpack = require('webpack');
const Stats = require('webpack/lib/Stats');
const del = require('del');

const { getWebpackConfig } = require('../webpack.config');

const DIST_DIR = Path.resolve(__dirname, '../target');

run(
async ({ log, flags }) => {
log.info('cleaning previous build output');
await del(Path.resolve(__dirname, '../target'));
await del(DIST_DIR);

const compiler = webpack(
getWebpackConfig({
Expand All @@ -38,10 +41,38 @@ run(
);

/** @param {webpack.Stats} stats */
const onCompilationComplete = (stats) => {
const onCompilationComplete = async (stats) => {
const took = Math.round((stats.endTime - stats.startTime) / 1000);

if (!stats.hasErrors() && !stats.hasWarnings()) {
if (!flags.dev) {
const reporter = CiStatsReporter.fromEnv(log);

const metrics = [
{
group: '@kbn/ui-shared-deps asset size',
id: 'kbn-ui-shared-deps.js',
value: Fs.statSync(Path.resolve(DIST_DIR, 'kbn-ui-shared-deps.js')).size,
},
{
group: '@kbn/ui-shared-deps asset size',
id: '[email protected]',
value: Fs.statSync(Path.resolve(DIST_DIR, '[email protected]')).size,
},
{
group: '@kbn/ui-shared-deps asset size',
id: 'css',
value:
Fs.statSync(Path.resolve(DIST_DIR, 'kbn-ui-shared-deps.css')).size +
Fs.statSync(Path.resolve(DIST_DIR, 'kbn-ui-shared-deps.v7.light.css')).size,
},
];

log.debug('metrics:', metrics);

await reporter.metrics(metrics);
}

log.success(`webpack completed in about ${took} seconds`);
return;
}
Expand All @@ -56,11 +87,9 @@ run(

if (flags.watch) {
compiler.hooks.done.tap('report on stats', (stats) => {
try {
onCompilationComplete(stats);
} catch (error) {
onCompilationComplete(stats).catch((error) => {
log.error(error.message);
}
});
});

compiler.hooks.watchRun.tap('report on start', () => {
Expand All @@ -83,7 +112,7 @@ run(
return;
}

onCompilationComplete(
await onCompilationComplete(
await new Promise((resolve, reject) => {
compiler.run((error, stats) => {
if (error) {
Expand Down

0 comments on commit a5692c1

Please sign in to comment.