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

[Embeddable Rebuild] [Discover] Fix bundle size #189206

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import type { ApplicationStart } from '@kbn/core/public';
import { SEARCH_EMBEDDABLE_TYPE } from '@kbn/discover-utils';
import { ViewMode } from '@kbn/embeddable-plugin/public';
import { i18n } from '@kbn/i18n';
import {
apiCanAccessViewMode,
apiHasType,
apiIsOfType,
import type {
CanAccessViewMode,
EmbeddableApiContext,
getInheritedViewMode,
HasType,
} from '@kbn/presentation-publishing';
import type { Action } from '@kbn/ui-actions-plugin/public';
Expand All @@ -29,16 +25,19 @@ export const ACTION_VIEW_SAVED_SEARCH = 'ACTION_VIEW_SAVED_SEARCH';

type ViewSavedSearchActionApi = CanAccessViewMode & HasType & PublishesSavedSearch;

const compatibilityCheck = (
api: EmbeddableApiContext['embeddable']
): api is ViewSavedSearchActionApi => {
return (
/** Async type guards aren't supported, so need to make this an async getter instead */
const getCompatibilityCheck = async (): Promise<
(api: EmbeddableApiContext['embeddable']) => api is ViewSavedSearchActionApi
> => {
const { apiCanAccessViewMode, apiHasType, apiIsOfType, getInheritedViewMode } = await import(
'@kbn/presentation-publishing'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

One downside to this approach is that I think our async bundle will now grow with @kbn/presentation-publishing since we're importing the entire package here. I wonder if it would work to keep the imports how they were for tree shaking, but break getCompatibilityCheck out into a separate file and async import that here instead?

Copy link
Contributor Author

@Heenawter Heenawter Jul 29, 2024

Choose a reason for hiding this comment

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

Oh!! Genius 🤯 Good call on that one - did this in 5080816 and it reduced the async bundle size increase of this PR by about 50% (I think it was around +12KB before, and it's +6KB now).

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome 🤘 thank you!

return (api: EmbeddableApiContext['embeddable']): api is ViewSavedSearchActionApi =>
apiCanAccessViewMode(api) &&
getInheritedViewMode(api) === ViewMode.VIEW &&
apiHasType(api) &&
apiIsOfType(api, SEARCH_EMBEDDABLE_TYPE) &&
apiPublishesSavedSearch(api)
);
apiPublishesSavedSearch(api);
};

export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
Expand All @@ -51,6 +50,7 @@ export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
) {}

async execute({ embeddable }: EmbeddableApiContext): Promise<void> {
const compatibilityCheck = await getCompatibilityCheck();
if (!compatibilityCheck(embeddable)) {
return;
}
Expand All @@ -73,6 +73,6 @@ export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
const { capabilities } = this.application;
const hasDiscoverPermissions =
(capabilities.discover.show as boolean) || (capabilities.discover.save as boolean);
return compatibilityCheck(embeddable) && hasDiscoverPermissions;
return hasDiscoverPermissions && (await getCompatibilityCheck())(embeddable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { Filter } from '@kbn/es-query';
import { PublishesSavedObjectId, PublishesUnifiedSearch } from '@kbn/presentation-publishing';
import type { PublishesSavedObjectId, PublishesUnifiedSearch } from '@kbn/presentation-publishing';
import { DiscoverAppLocatorParams } from '../../../common';
import { PublishesSavedSearch } from '../types';

Expand Down