Skip to content

Commit 2a69e2c

Browse files
committed
[kbn/optimizer] report sizes of assets produced by optimizer
1 parent cac0717 commit 2a69e2c

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

packages/kbn-optimizer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"css-loader": "^3.4.2",
3030
"del": "^5.1.0",
3131
"execa": "^4.0.2",
32+
"fast-glob": "^3.2.4",
3233
"file-loader": "^4.2.0",
3334
"istanbul-instrumenter-loader": "^3.0.1",
3435
"jest-diff": "^25.5.0",

packages/kbn-optimizer/src/report_optimizer_stats.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
* under the License.
1818
*/
1919

20+
import { Stats } from 'fs';
21+
2022
import { materialize, mergeMap, dematerialize } from 'rxjs/operators';
2123
import { CiStatsReporter } from '@kbn/dev-utils';
24+
import fastGlob, { Entry } from 'fast-glob';
2225

2326
import { OptimizerUpdate$ } from './run_optimizer';
2427
import { OptimizerState, OptimizerConfig } from './optimizer';
2528
import { pipeClosure } from './common';
2629

30+
const flatten = <T>(arr: Array<T | T[]>): T[] =>
31+
arr.reduce((acc: T[], item) => acc.concat(item), []);
32+
2733
export function reportOptimizerStats(reporter: CiStatsReporter, config: OptimizerConfig) {
2834
return pipeClosure((update$: OptimizerUpdate$) => {
2935
let lastState: OptimizerState | undefined;
@@ -36,16 +42,60 @@ export function reportOptimizerStats(reporter: CiStatsReporter, config: Optimize
3642

3743
if (n.kind === 'C' && lastState) {
3844
await reporter.metrics(
39-
config.bundles.map((bundle) => {
40-
// make the cache read from the cache file since it was likely updated by the worker
41-
bundle.cache.refresh();
42-
43-
return {
44-
group: `@kbn/optimizer bundle module count`,
45-
id: bundle.id,
46-
value: bundle.cache.getModuleCount() || 0,
47-
};
48-
})
45+
flatten(
46+
config.bundles.map((bundle) => {
47+
// make the cache read from the cache file since it was likely updated by the worker
48+
bundle.cache.refresh();
49+
50+
const outputFiles = fastGlob.sync('**/*', {
51+
cwd: bundle.outputDir,
52+
onlyFiles: true,
53+
unique: true,
54+
stats: true,
55+
absolute: false,
56+
ignore: ['!**/*.map'],
57+
});
58+
59+
const entryName = `${bundle.id}.${bundle.type}.js`;
60+
const entry = outputFiles.find((f) => f.path === entryName);
61+
if (!entry) {
62+
throw new Error(
63+
`Unable to find bundle entry named [${entryName}] in [${bundle.outputDir}]`
64+
);
65+
}
66+
67+
const chunkPrefix = `${bundle.id}.chunk.`;
68+
const asyncChunks = outputFiles.filter((f) => f.path.startsWith(chunkPrefix));
69+
const miscFiles = outputFiles.filter(
70+
(f) => f !== entry && !asyncChunks.includes(f)
71+
);
72+
const sumSize = (files: Entry[]) =>
73+
files.reduce((acc: number, f) => acc + f.stats!.size, 0);
74+
75+
return [
76+
{
77+
group: `@kbn/optimizer bundle module count`,
78+
id: bundle.id,
79+
value: bundle.cache.getModuleCount() || 0,
80+
},
81+
{
82+
group: `page load bundle size`,
83+
id: bundle.id,
84+
value: entry.stats!.size,
85+
},
86+
{
87+
group: `async chunks size`,
88+
id: bundle.id,
89+
value: sumSize(asyncChunks),
90+
},
91+
{
92+
group: `miscellaneous assets size`,
93+
id: bundle.id,
94+
value: sumSize(miscFiles),
95+
},
96+
];
97+
})
98+
)
4999
);
50100
}
51101

packages/kbn-optimizer/src/worker/webpack.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
5252

5353
output: {
5454
path: bundle.outputDir,
55-
filename: `[name].${bundle.type}.js`,
55+
filename: `${bundle.id}.${bundle.type}.js`,
56+
chunkFilename: `${bundle.id}.chunk.[id].js`,
5657
devtoolModuleFilenameTemplate: (info) =>
5758
`/${bundle.type}:${bundle.id}/${Path.relative(
5859
bundle.sourceRoot,

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14403,6 +14403,18 @@ fast-glob@^3.0.3, fast-glob@^3.1.1, fast-glob@^3.2.2:
1440314403
micromatch "^4.0.2"
1440414404
picomatch "^2.2.1"
1440514405

14406+
fast-glob@^3.2.4:
14407+
version "3.2.4"
14408+
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.4.tgz#d20aefbf99579383e7f3cc66529158c9b98554d3"
14409+
integrity sha512-kr/Oo6PX51265qeuCYsyGypiO5uJFgBS0jksyG7FUeCyQzNwYnzrNIMR1NXfkZXsMYXYLRAHgISHBz8gQcxKHQ==
14410+
dependencies:
14411+
"@nodelib/fs.stat" "^2.0.2"
14412+
"@nodelib/fs.walk" "^1.2.3"
14413+
glob-parent "^5.1.0"
14414+
merge2 "^1.3.0"
14415+
micromatch "^4.0.2"
14416+
picomatch "^2.2.1"
14417+
1440614418
fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@~2.1.0:
1440714419
version "2.1.0"
1440814420
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
@@ -27324,6 +27336,13 @@ rimraf@^2.5.4, rimraf@^2.7.1:
2732427336
dependencies:
2732527337
glob "^7.1.3"
2732627338

27339+
rimraf@^3.0.2:
27340+
version "3.0.2"
27341+
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
27342+
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
27343+
dependencies:
27344+
glob "^7.1.3"
27345+
2732727346
rimraf@~2.0.2:
2732827347
version "2.0.3"
2732927348
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.0.3.tgz#f50a2965e7144e9afd998982f15df706730f56a9"

0 commit comments

Comments
 (0)