Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use target_web to ensure browser compatibility #130874

Merged

Conversation

afharo
Copy link
Member

@afharo afharo commented Apr 25, 2022

Summary

Because of how our bundle process works, any @kbn/* package used in the UI should build a web-optimized version target_web. Using the target_node build risks introducing compatibility issues as only target_web applies the browserlist compatibility-matrix necessary polyfills.

This PR adds the script node scripts/find_target_node_imports_in_bundles to list all packages imported in the UI with the default target_node build.

It also attempts to fix all the packages found in the script or removes the UI dependency on that package.

Related to #88678.

Highlights

@kbn/server-route-repository

@kbn/server-route-repository could leak @kbn/config-schema if imported from the UI. Plugins used a workaround to only import the method they used and enrich the typing with a 2nd import + type casting:

import type {
ClientRequestParamsOf,
formatRequest as formatRequestType,
ReturnOf,
RouteRepositoryClient,
ServerRouteRepository,
} from '@kbn/server-route-repository';
// @ts-expect-error cannot find module or correspondent type declarations
// The code and types are at separated folders on @kbn/server-route-repository
// so in order to do targeted imports they must me imported separately, and
// an error is expected here
import { formatRequest } from '@kbn/server-route-repository/target_node/format_request';

This PR creates a new browser-specific entry point to the package that only exports the utilities that are meant to be used in the UI. Thanks to these changes, the package can be imported from the UI without the risk of leaking server-side code.

Plugin cloud_security_posture

It was accidentally importing @kbn/config-schema to the UI because it imported a constant from the same file a validation schema was defined.

@kbn/optimizer and kbn-ui-shared-deps-npm

After the changes, running the script still brings 2 uses of target_node that I was not able to remove:

➜ node scripts/find_target_node_imports_in_bundles.js
 info analyzing 130 stats files
 succ found 2 @kbn/*/target_node imports in entry bundles and chunks
'@kbn/optimizer',
'kbn-ui-shared-deps-npm',
  • @kbn/optimizer is found in all the bundles because of the entry_point_creator.js injected via its webpack config.
  • kbn-ui-shared-deps-npm: I'm not entirely sure what is bringing this one in. Especially, that it's not @kbn/ui-shared-deps-npm.

@elastic/kibana-operations can you help me out with these? 🙏

Open question

  • Should we add a CI check to validate that we don't use target_node builds in our bundles?

For maintainers

@afharo afharo changed the base branch from afharo/spacetime-bundle-improvements to main April 25, 2022 11:29
@afharo afharo added performance enhancement New value added to drive a business result technical debt Improvement of the software architecture and operational architecture release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting v8.3.0 labels Apr 25, 2022
@afharo
Copy link
Member Author

afharo commented Apr 25, 2022

The page load increase triggered an investigation about how target_web builds are generated. #130904 should improve this.

@afharo afharo force-pushed the afharo/spacetime-bundle-improvements-target_web branch 2 times, most recently from 88770df to 9484183 Compare April 28, 2022 15:50
@@ -14,4 +14,4 @@ export * from './node';
export * from './limits';
export * from './cli';
export * from './report_optimizer_timings';
export * from './babel_runtime_helpers';
export * from './audit_bundle_dependencies';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename the dir because it no-longer contained only the logic for babel_runtime_helpers 😇

@@ -52,6 +52,13 @@ jsts_transpiler(
build_pkg_name = package_name(),
)

jsts_transpiler(
name = "target_web",
srcs = SRCS,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if a potential improvement could be to only include in this version the formatRequest helper to avoid the split imports in the plugins.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and added only the needed files because jest complained that it couldn't reach the dependency ./parse_endpoint because it was from outside the module (from the import import { formatRequest } from '@kbn/server-route-repository/target_web/format_request';)

@@ -6,8 +6,6 @@
*/
import { schema as rt, TypeOf } from '@kbn/config-schema';

export const cspRuleAssetSavedObjectType = 'csp_rule';
Copy link
Member Author

@afharo afharo Apr 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to constants because importing this constant from here was also importing the bundle @kbn/config-schema.

@afharo afharo force-pushed the afharo/spacetime-bundle-improvements-target_web branch 3 times, most recently from 9753ccb to f957144 Compare April 28, 2022 19:29
@afharo afharo force-pushed the afharo/spacetime-bundle-improvements-target_web branch from f957144 to 1ef7722 Compare April 29, 2022 00:34
@afharo afharo marked this pull request as ready for review April 29, 2022 07:19
@afharo afharo requested review from a team as code owners April 29, 2022 07:19
@botelastic botelastic bot added the Team:APM All issues that need APM UI Team support label Apr 29, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/apm-ui (Team:apm)

@afharo afharo changed the title Use target_web to improve bundle size and performance Use target_web to improve browser compatibility Apr 29, 2022
@afharo afharo changed the title Use target_web to improve browser compatibility Use target_web to ensure browser compatibility Apr 29, 2022
Copy link
Member

@dgieselaar dgieselaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, page load bundle improvements look pretty good!

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maps changes LGTM
code review

build_pkg_name = package_name(),
)

jsts_transpiler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this was repeated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattkime we are intentionally transpiling twice with different names and Babel presets:

  1. target_node - optimized for Node.js environment
  2. target_web - optimized for running in browsers (mind the additional parameter web = True). Among other things, it applies the necessary transpilation and polyfills to support ourbrowserlist compatibility matrix.

If your package only runs on the UI and you think target_node is not necessary, we can discuss with @elastic/kibana-operations if it's ok to remove it or if target_node must always exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, not duplicate!

@@ -0,0 +1,21 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a different entry point exporting the web-only APIs.

build_pkg_name = package_name(),
)

jsts_transpiler(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattkime we are intentionally transpiling twice with different names and Babel presets:

  1. target_node - optimized for Node.js environment
  2. target_web - optimized for running in browsers (mind the additional parameter web = True). Among other things, it applies the necessary transpilation and polyfills to support ourbrowserlist compatibility matrix.

If your package only runs on the UI and you think target_node is not necessary, we can discuss with @elastic/kibana-operations if it's ok to remove it or if target_node must always exist.

Copy link
Contributor

@shahzad31 shahzad31 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uptime changes LGTM !!

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
apm 1179 1180 +1
cloudSecurityPosture 245 183 -62
lists 299 300 +1
observability 393 394 +1
securitySolution 2796 2797 +1
ux 132 133 +1
total -57

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
apm 2.8MB 2.8MB -7.4KB
canvas 1.0MB 1020.1KB -4.5KB
cases 290.2KB 290.1KB -40.0B
cloud 18.6KB 18.6KB +26.0B
cloudSecurityPosture 393.8KB 180.2KB -213.6KB
console 399.5KB 396.1KB -3.4KB
core 136.1KB 136.1KB -18.0B
dataViewFieldEditor 148.0KB 147.9KB -23.0B
dataViewManagement 124.0KB 121.2KB -2.8KB
dataVisualizer 539.6KB 536.0KB -3.6KB
expressions 25.6KB 25.6KB -9.0B
infra 1002.7KB 1002.6KB -68.0B
lens 1.1MB 1.1MB -9.0KB
lists 147.7KB 147.0KB -764.0B
maps 2.5MB 2.5MB -120.0B
ml 3.3MB 3.3MB -6.4KB
observability 443.0KB 434.2KB -8.8KB
presentationUtil 136.8KB 127.3KB -9.5KB
searchprofiler 164.2KB 160.6KB -3.5KB
securitySolution 4.8MB 4.8MB -5.7KB
stackAlerts 202.8KB 199.2KB -3.6KB
synthetics 756.3KB 756.2KB -95.0B
transform 374.8KB 371.2KB -3.6KB
triggersActionsUi 727.7KB 723.4KB -4.2KB
unifiedSearch 157.2KB 151.4KB -5.8KB
ux 162.2KB 154.0KB -8.2KB
visTypeVega 1.7MB 1.7MB -211.0B
watcher 272.5KB 269.0KB -3.5KB
total -308.5KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
apm 31.1KB 26.9KB -4.2KB
cases 97.7KB 93.6KB -4.1KB
cloud 10.1KB 10.1KB -4.0B
cloudSecurityPosture 4.6KB 4.6KB -1.0B
core 288.9KB 293.1KB +4.2KB
data 416.5KB 413.7KB -2.8KB
dataViewFieldEditor 24.4KB 21.7KB -2.7KB
dataViews 42.2KB 39.7KB -2.5KB
expressions 98.8KB 96.6KB -2.2KB
fieldFormats 47.8KB 44.4KB -3.3KB
infra 88.1KB 83.9KB -4.1KB
observability 92.0KB 91.9KB -171.0B
securitySolution 249.3KB 246.5KB -2.8KB
synthetics 25.7KB 21.4KB -4.2KB
timelines 275.9KB 271.8KB -4.1KB
total -32.9KB
Unknown metric groups

API count

id before after diff
@kbn/optimizer 46 47 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@gchaps gchaps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM from docs

@afharo afharo merged commit f7a1739 into elastic:main May 5, 2022
@afharo afharo deleted the afharo/spacetime-bundle-improvements-target_web branch May 5, 2022 19:20
kertal pushed a commit to kertal/kibana that referenced this pull request May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting enhancement New value added to drive a business result performance release_note:skip Skip the PR/issue when compiling release notes Team:APM All issues that need APM UI Team support technical debt Improvement of the software architecture and operational architecture v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.