diff --git a/.changeset/silent-candles-remember.md b/.changeset/silent-candles-remember.md new file mode 100644 index 0000000000..99061bbc1d --- /dev/null +++ b/.changeset/silent-candles-remember.md @@ -0,0 +1,5 @@ +--- +'@backstage/core-components': patch +--- + +The syntax highlighting library used by the `CodeSnippet` component is now lazy loaded. diff --git a/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx b/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx index 3ce041a819..f8a7d2ff9e 100644 --- a/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx +++ b/packages/core-components/src/components/CodeSnippet/CodeSnippet.tsx @@ -14,12 +14,56 @@ * limitations under the License. */ -import React from 'react'; -import SyntaxHighlighter from 'react-syntax-highlighter'; -import { docco, dark } from 'react-syntax-highlighter/dist/cjs/styles/hljs'; +import React, { lazy, Suspense } from 'react'; import { useTheme } from '@material-ui/core'; 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 }; +}); /** * Properties for {@link CodeSnippet} @@ -68,38 +112,12 @@ export interface CodeSnippetProps { * providing consistent theming and copy code button */ export function CodeSnippet(props: CodeSnippetProps) { - const { - text, - language, - showLineNumbers = false, - showCopyCodeButton = false, - highlightedNumbers, - customStyle, - } = props; - const theme = useTheme(); - const mode = theme.palette.type === 'dark' ? dark : docco; - const highlightColor = theme.palette.type === 'dark' ? '#256bf3' : '#e6ffed'; + const { text, showCopyCodeButton = false } = props; return (
- - highlightedNumbers?.includes(lineNumber) - ? { - style: { - backgroundColor: highlightColor, - }, - } - : {} - } - > - {text} - + }> + + {showCopyCodeButton && (