Skip to content

Commit

Permalink
load react component lazily to reduce entry bundle (#65267)
Browse files Browse the repository at this point in the history
* load react component lazily to reduce entry bundle

* address comments
  • Loading branch information
mshustov committed May 5, 2020
1 parent 1f44c7e commit 380a396
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
import React from 'react';
import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers';
import { TelemetryManagementSection } from './telemetry_management_section';
import TelemetryManagementSection from './telemetry_management_section';
import { TelemetryService } from '../../../telemetry/public/services';
import { coreMock } from '../../../../core/public/mocks';
import { telemetryManagementSectionWrapper } from './telemetry_management_section_wrapper';

describe('TelemetryManagementSectionComponent', () => {
const coreStart = coreMock.createStart();
Expand Down Expand Up @@ -270,10 +269,12 @@ describe('TelemetryManagementSectionComponent', () => {
notifications: coreStart.notifications,
http: coreSetup.http,
});
const Wrapper = telemetryManagementSectionWrapper(telemetryService);

expect(
shallowWithIntl(
<Wrapper
<TelemetryManagementSection
showAppliesSettingMessage={true}
telemetryService={telemetryService}
onQueryMatchChange={onQueryMatchChange}
enableSaving={true}
toasts={coreStart.notifications.toasts}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ export class TelemetryManagementSection extends Component<Props, State> {
});
};
}

// required for lazy loading
// eslint-disable-next-line import/no-default-export
export default TelemetryManagementSection;
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@
* under the License.
*/

import React from 'react';
import React, { lazy, Suspense } from 'react';
import { EuiLoadingSpinner } from '@elastic/eui';
import { TelemetryPluginSetup } from 'src/plugins/telemetry/public';
import { TelemetryManagementSection } from './telemetry_management_section';

// It should be this but the types are way too vague in the AdvancedSettings plugin `Record<string, any>`
// type Props = Omit<TelemetryManagementSection['props'], 'telemetryService'>;
type Props = any;

const TelemetryManagementSectionComponent = lazy(() => import('./telemetry_management_section'));

export function telemetryManagementSectionWrapper(
telemetryService: TelemetryPluginSetup['telemetryService']
) {
const TelemetryManagementSectionWrapper = (props: Props) => (
<TelemetryManagementSection
showAppliesSettingMessage={true}
telemetryService={telemetryService}
{...props}
/>
<Suspense fallback={<EuiLoadingSpinner />}>
<TelemetryManagementSectionComponent
showAppliesSettingMessage={true}
telemetryService={telemetryService}
{...props}
/>
</Suspense>
);

return TelemetryManagementSectionWrapper;
Expand Down

0 comments on commit 380a396

Please sign in to comment.