[kbn/optimizer] dll @babel/runtime modules used by entry bundles (#113453)

Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
Spencer 2021-10-29 18:08:20 -05:00 committed by GitHub
parent 107661129d
commit 2797fab000
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 182 additions and 4 deletions

View file

@ -32,6 +32,7 @@ NPM_MODULE_EXTRA_FILES = [
RUNTIME_DEPS = [
"//packages/kbn-config",
"//packages/kbn-config-schema",
"//packages/kbn-dev-utils",
"//packages/kbn-std",
"//packages/kbn-ui-shared-deps-npm",
@ -62,6 +63,7 @@ RUNTIME_DEPS = [
TYPES_DEPS = [
"//packages/kbn-config",
"//packages/kbn-config-schema",
"//packages/kbn-dev-utils",
"//packages/kbn-std",
"//packages/kbn-ui-shared-deps-npm",

View file

@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import Path from 'path';
import { run, REPO_ROOT } from '@kbn/dev-utils';
import { OptimizerConfig } from '../optimizer';
import { parseStats, inAnyEntryChunk } from './parse_stats';
export async function runFindBabelHelpersInEntryBundlesCli() {
run(async ({ log }) => {
const config = OptimizerConfig.create({
includeCoreBundle: true,
repoRoot: REPO_ROOT,
});
const paths = config.bundles.map((b) => Path.resolve(b.outputDir, 'stats.json'));
log.info('analyzing', paths.length, 'stats files');
log.verbose(paths);
const imports = new Set();
for (const path of paths) {
const stats = parseStats(path);
for (const module of stats.modules) {
if (!inAnyEntryChunk(stats, module)) {
continue;
}
for (const { userRequest } of module.reasons) {
if (userRequest.startsWith('@babel/runtime/')) {
imports.add(userRequest);
}
}
}
}
log.success('found', imports.size, '@babel/register imports in entry bundles');
log.write(
Array.from(imports, (i) => `'${i}',`)
.sort()
.join('\n')
);
});
}

View file

@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export * from './find_babel_runtime_helpers_in_entry_bundles';

View file

@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import Fs from 'fs';
import dedent from 'dedent';
import { schema, Props, TypeOf } from '@kbn/config-schema';
const partialObject = <P extends Props>(props: P) => {
return schema.object(props, {
unknowns: 'ignore',
});
};
export type Module = TypeOf<typeof moduleSchema>;
const moduleSchema = partialObject({
identifier: schema.string(),
chunks: schema.arrayOf(schema.oneOf([schema.string(), schema.number()])),
reasons: schema.arrayOf(
partialObject({
userRequest: schema.string(),
})
),
});
export type Chunk = TypeOf<typeof chunkSchema>;
const chunkSchema = partialObject({
id: schema.oneOf([schema.string(), schema.number()]),
entry: schema.boolean(),
initial: schema.boolean(),
});
const statsSchema = partialObject({
chunks: schema.arrayOf(chunkSchema),
modules: schema.arrayOf(moduleSchema),
});
export interface Stats {
path: string;
modules: Module[];
chunks: Chunk[];
}
export function parseStats(path: string): Stats {
try {
return {
path,
...statsSchema.validate(JSON.parse(Fs.readFileSync(path, 'utf-8'))),
};
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(dedent`
unable to find stats file at [${path}]. Make sure you run the following
before running this script:
node scripts/build_kibana_platform_plugins --dist --profile
`);
}
throw error;
}
}
export function inAnyEntryChunk(stats: Stats, module: Module): boolean {
return module.chunks.some((id) => {
const chunk = stats.chunks.find((c) => c.id === id);
if (!chunk) {
throw new Error(
`unable to find chunk ${id} for module ${module.identifier} in ${stats.path}`
);
}
return chunk.entry || chunk.initial;
});
}

View file

@ -14,3 +14,4 @@ export * from './node';
export * from './limits';
export * from './cli';
export * from './report_optimizer_timings';
export * from './babel_runtime_helpers';

View file

@ -73,6 +73,10 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
new BundleRefsPlugin(bundle, bundleRefs),
new PopulateBundleCachePlugin(worker, bundle),
new BundleMetricsPlugin(bundle),
new webpack.DllReferencePlugin({
context: worker.repoRoot,
manifest: require(UiSharedDepsNpm.dllManifestPath),
}),
...(worker.profileWebpack ? [new EmitStatsPlugin(bundle)] : []),
...(bundle.banner ? [new webpack.BannerPlugin({ banner: bundle.banner, raw: true })] : []),
],
@ -261,10 +265,6 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
test: /\.(js|css)$/,
cache: false,
}),
new webpack.DllReferencePlugin({
context: worker.repoRoot,
manifest: require(UiSharedDepsNpm.dllManifestPath),
}),
],
optimization: {

View file

@ -38,6 +38,31 @@ module.exports = (_, argv) => {
'whatwg-fetch',
'symbol-observable',
/**
* babel runtime helpers referenced from entry chunks
* determined by running:
*
* node scripts/build_kibana_platform_plugins --dist --profile
* node scripts/find_babel_runtime_helpers_in_use.js
*/
'@babel/runtime/helpers/assertThisInitialized',
'@babel/runtime/helpers/classCallCheck',
'@babel/runtime/helpers/classPrivateFieldGet',
'@babel/runtime/helpers/classPrivateFieldSet',
'@babel/runtime/helpers/createSuper',
'@babel/runtime/helpers/defineProperty',
'@babel/runtime/helpers/extends',
'@babel/runtime/helpers/inherits',
'@babel/runtime/helpers/interopRequireDefault',
'@babel/runtime/helpers/interopRequireWildcard',
'@babel/runtime/helpers/objectSpread2',
'@babel/runtime/helpers/objectWithoutPropertiesLoose',
'@babel/runtime/helpers/slicedToArray',
'@babel/runtime/helpers/toArray',
'@babel/runtime/helpers/toConsumableArray',
'@babel/runtime/helpers/typeof',
'@babel/runtime/helpers/wrapNativeSuper',
// modules from npm
'@elastic/charts',
'@elastic/eui',

View file

@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
require('../src/setup_node_env/no_transpilation');
require('@kbn/optimizer').runFindBabelHelpersInEntryBundlesCli();