From e535ea766a257f1bb8635b833c601d64d91deb39 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 17 Oct 2021 13:07:37 +0200 Subject: [PATCH] core-components: use built-in async react-syntax-highlighter component Signed-off-by: Patrik Oldsberg --- .changeset/brave-spoons-grow.md | 5 ++ .../CodeSnippet/CodeSnippet.test.tsx | 6 ++ .../components/CodeSnippet/CodeSnippet.tsx | 86 ++++++++----------- 3 files changed, 46 insertions(+), 51 deletions(-) create mode 100644 .changeset/brave-spoons-grow.md diff --git a/.changeset/brave-spoons-grow.md b/.changeset/brave-spoons-grow.md new file mode 100644 index 0000000000..d1bd166510 --- /dev/null +++ b/.changeset/brave-spoons-grow.md @@ -0,0 +1,5 @@ +--- +'@backstage/core-components': patch +--- + +Switched to relying on the built-in support for async loading in `react-syntax-highlighter`. This should provide further improvements to async rendering and lazy loading, and avoid test flakiness that was happening because of the significant number or resources being loaded in lazily all at once. diff --git a/packages/core-components/src/components/CodeSnippet/CodeSnippet.test.tsx b/packages/core-components/src/components/CodeSnippet/CodeSnippet.test.tsx index a4ffd3462b..fec5b7ddf7 100644 --- a/packages/core-components/src/components/CodeSnippet/CodeSnippet.test.tsx +++ b/packages/core-components/src/components/CodeSnippet/CodeSnippet.test.tsx @@ -31,6 +31,12 @@ const minProps = { }; describe('', () => { + // react-syntax-highlighter is large and can cause significant slowdowns + // This test makes sure we're loading things in asynchronously and not too broadly. + it('renders quickly', async () => { + await renderInTestApp(); + }, 1000); + it('renders text without exploding', async () => { const { getByText } = await renderInTestApp(); expect(getByText(/"Hello"/)).toBeInTheDocument(); diff --git a/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx b/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx index 3aa2f42984..e1c407276b 100644 --- a/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx +++ b/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx @@ -14,56 +14,13 @@ * limitations under the License. */ -import React, { lazy, Suspense } from 'react'; +import React from 'react'; import { useTheme } from '@material-ui/core/styles'; import { BackstageTheme } from '@backstage/theme'; import { CopyTextButton } from '../CopyTextButton'; -import { Progress } from '../Progress'; - -const LazySyntaxHighlighter = lazy(async () => { - const [{ default: SyntaxHighlighter }, { docco, dark }] = await Promise.all([ - import('react-syntax-highlighter'), - import('react-syntax-highlighter/dist/cjs/styles/hljs'), - ]); - - function LazyHighlighter(props: CodeSnippetProps) { - const { - text, - language, - showLineNumbers = false, - highlightedNumbers, - customStyle, - } = props; - const theme = useTheme(); - const mode = theme.palette.type === 'dark' ? dark : docco; - const highlightColor = - theme.palette.type === 'dark' ? '#256bf3' : '#e6ffed'; - - return ( - - highlightedNumbers?.includes(lineNumber) - ? { - style: { - backgroundColor: highlightColor, - }, - } - : {} - } - > - {text} - - ); - } - - return { default: LazyHighlighter }; -}); +import { LightAsync } from 'react-syntax-highlighter'; +import dark from 'react-syntax-highlighter/dist/esm/styles/hljs/dark'; +import docco from 'react-syntax-highlighter/dist/esm/styles/hljs/docco'; /** * Properties for {@link CodeSnippet} @@ -112,12 +69,39 @@ export interface CodeSnippetProps { * providing consistent theming and copy code button */ export function CodeSnippet(props: CodeSnippetProps) { - const { text, showCopyCodeButton = false } = props; + const { + text, + language, + showLineNumbers = false, + highlightedNumbers, + customStyle, + showCopyCodeButton = false, + } = props; + const theme = useTheme(); + const mode = theme.palette.type === 'dark' ? dark : docco; + const highlightColor = theme.palette.type === 'dark' ? '#256bf3' : '#e6ffed'; + return (
- }> - - + + highlightedNumbers?.includes(lineNumber) + ? { + style: { + backgroundColor: highlightColor, + }, + } + : {} + } + > + {text} + {showCopyCodeButton && (