Skip to content

Commit a2138f2

Browse files
authored
Nitpicks for code splitting (#4346)
1 parent 2979d0c commit a2138f2

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

beta/src/components/MDX/MDXComponents.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import Intro from './Intro';
1919
import Link from './Link';
2020
import {PackageImport} from './PackageImport';
2121
import Recap from './Recap';
22-
import dynamic from 'next/dynamic';
2322
import Sandpack from './Sandpack';
2423
import SimpleCallout from './SimpleCallout';
2524
import TerminalBlock from './TerminalBlock';
@@ -363,7 +362,6 @@ export const MDXComponents = {
363362
code: CodeBlock,
364363
// The code block renders <pre> so we just want a div here.
365364
pre: (p: JSX.IntrinsicElements['div']) => <div {...p} />,
366-
// Scary: dynamic(() => import('./Scary')),
367365
APIAnatomy,
368366
AnatomyStep,
369367
CodeDiagram,

beta/src/components/MDX/Sandpack/SandpackWrapper.tsx renamed to beta/src/components/MDX/Sandpack/SandpackRoot.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*/
44

5-
import React from 'react';
6-
import {
7-
SandpackProvider,
8-
SandpackSetup,
9-
SandpackFile,
10-
} from '@codesandbox/sandpack-react';
11-
5+
import * as React from 'react';
6+
import {SandpackProvider} from '@codesandbox/sandpack-react';
127
import {CustomPreset} from './CustomPreset';
8+
import {createFileMap} from './utils';
9+
10+
import type {SandpackSetup} from '@codesandbox/sandpack-react';
1311

1412
type SandpackProps = {
1513
children: React.ReactChildren;
1614
autorun?: boolean;
1715
setup?: SandpackSetup;
1816
showDevTools?: boolean;
1917
};
20-
import {reducedCodeSnippet} from './utils';
2118

2219
const sandboxStyle = `
2320
* {
@@ -65,13 +62,13 @@ ul {
6562
}
6663
`.trim();
6764

68-
function SandpackWrapper(props: SandpackProps) {
65+
function SandpackRoot(props: SandpackProps) {
6966
let {children, setup, autorun = true, showDevTools = false} = props;
7067
const [devToolsLoaded, setDevToolsLoaded] = React.useState(false);
7168
let codeSnippets = React.Children.toArray(children) as React.ReactElement[];
7269
let isSingleFile = true;
7370

74-
const files = reducedCodeSnippet(codeSnippets);
71+
const files = createFileMap(codeSnippets);
7572

7673
files['/styles.css'] = {
7774
code: [sandboxStyle, files['/styles.css']?.code ?? ''].join('\n\n'),
@@ -97,6 +94,6 @@ function SandpackWrapper(props: SandpackProps) {
9794
);
9895
}
9996

100-
SandpackWrapper.displayName = 'Sandpack';
97+
SandpackRoot.displayName = 'Sandpack';
10198

102-
export default SandpackWrapper;
99+
export default SandpackRoot;

beta/src/components/MDX/Sandpack/index.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
15
import * as React from 'react';
2-
import {reducedCodeSnippet} from './utils';
3-
const Sandpack = React.lazy(() => import('./SandpackWrapper'));
6+
import dynamic from 'next/dynamic';
7+
import {createFileMap} from './utils';
8+
9+
const SandpackRoot = dynamic(() => import('./SandpackRoot'), {suspense: true});
410

5-
const SandpackFallBack = ({code}: {code: string}) => (
11+
const SandpackGlimmer = ({code}: {code: string}) => (
612
<div className="sandpack-container my-8">
713
<div className="shadow-lg dark:shadow-lg-dark rounded-lg">
814
<div className="bg-wash h-10 dark:bg-card-dark flex justify-between items-center relative z-10 border-b border-border dark:border-border-dark rounded-t-lg rounded-b-none">
@@ -41,11 +47,10 @@ const SandpackFallBack = ({code}: {code: string}) => (
4147
);
4248

4349
export default React.memo(function SandpackWrapper(props: any): any {
44-
const codeSnippet = reducedCodeSnippet(
45-
React.Children.toArray(props.children)
46-
);
50+
const codeSnippet = createFileMap(React.Children.toArray(props.children));
4751

48-
// To set the active file in the fallback we have to find the active file first. If there are no active files we fallback to App.js as default
52+
// To set the active file in the fallback we have to find the active file first.
53+
// If there are no active files we fallback to App.js as default.
4954
let activeCodeSnippet = Object.keys(codeSnippet).filter(
5055
(fileName) =>
5156
codeSnippet[fileName]?.active === true &&
@@ -59,10 +64,8 @@ export default React.memo(function SandpackWrapper(props: any): any {
5964
}
6065

6166
return (
62-
<>
63-
<React.Suspense fallback={<SandpackFallBack code={activeCode} />}>
64-
<Sandpack {...props} />
65-
</React.Suspense>
66-
</>
67+
<React.Suspense fallback={<SandpackGlimmer code={activeCode} />}>
68+
<SandpackRoot {...props} />
69+
</React.Suspense>
6770
);
6871
});

beta/src/components/MDX/Sandpack/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ export const computeViewportSize = (
4848
return viewport;
4949
};
5050

51-
//TODO: revisit to reduce this (moved this to utils from sandpackWrapper since it is being used for finding active file for fallBack too)
52-
export const reducedCodeSnippet = (codeSnippets: any) => {
51+
export const createFileMap = (codeSnippets: any) => {
5352
let isSingleFile = true;
5453

5554
return codeSnippets.reduce(

0 commit comments

Comments
 (0)