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

Splitting sandpack from main bundle #4256

Merged
merged 30 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions beta/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
experimental: {
plugins: true,
// TODO: this doesn't work because https://github.com/vercel/next.js/issues/30714
// concurrentFeatures: true,
concurrentFeatures: false,
scrollRestoration: true,
},
async redirects() {
Expand All @@ -27,7 +27,7 @@ module.exports = {
},
webpack: (config, {dev, isServer, ...options}) => {
if (process.env.ANALYZE) {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
Expand Down
4 changes: 2 additions & 2 deletions beta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ci-check": "npm-run-all prettier:diff --parallel lint tsc lint-heading-ids",
"tsc": "tsc --noEmit",
"start": "next start",
"postinstall": "is-ci || (cd .. && husky install beta/.husky)",
"postinstall": "patch-package && (is-ci || (cd .. && husky install beta/.husky))",
"check-all": "npm-run-all prettier lint:fix tsc"
},
"dependencies": {
Expand All @@ -32,7 +32,7 @@
"date-fns": "^2.16.1",
"debounce": "^1.2.1",
"github-slugger": "^1.3.0",
"next": "^12.0.9",
"next": "^12.0.10",
"parse-numeric-range": "^1.2.0",
"react": "experimental",
"react-collapsed": "3.1.0",
Expand Down
6 changes: 5 additions & 1 deletion beta/src/components/MDX/APIAnatomy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ export function APIAnatomy({children}: APIAnatomyProps) {
break;
case 'pre':
acc.code = (
<CodeBlock {...child.props.children.props} noMargin={true} />
<CodeBlock
{...child.props.children.props}
noMargin={true}
isFromAPIAnatomy
/>
);
break;
}
Expand Down
7 changes: 0 additions & 7 deletions beta/src/components/MDX/CodeBlock/CodeBlock.module.css

This file was deleted.

17 changes: 5 additions & 12 deletions beta/src/components/MDX/CodeBlock/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

import cn from 'classnames';
import {
ClasserProvider,
SandpackCodeViewer,
SandpackProvider,
SandpackThemeProvider,
} from '@codesandbox/sandpack-react';
import rangeParser from 'parse-numeric-range';
import {CustomTheme} from '../Sandpack/Themes';
import styles from './CodeBlock.module.css';

interface InlineHiglight {
step: number;
Expand Down Expand Up @@ -86,16 +84,11 @@ const CodeBlock = function CodeBlock({
},
}}>
<SandpackThemeProvider theme={CustomTheme}>
<ClasserProvider
classes={{
'sp-cm': styles.codeViewer,
}}>
<SandpackCodeViewer
key={children.trimEnd()}
showLineNumbers={false}
decorators={decorators}
/>
</ClasserProvider>
<SandpackCodeViewer
key={children.trimEnd()}
showLineNumbers={false}
decorators={decorators}
/>
</SandpackThemeProvider>
</SandpackProvider>
</div>
Expand Down
33 changes: 30 additions & 3 deletions beta/src/components/MDX/CodeBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import cn from 'classnames';
import * as React from 'react';
const CodeBlock = React.lazy(() => import('./CodeBlock'));

import CodeBlock from './CodeBlock';

export default CodeBlock;
export default React.memo(function CodeBlockWrapper(props: {
isFromAPIAnatomy: boolean;
isFromPackageImport: boolean;
children: string;
className?: string;
metastring: string;
noMargin?: boolean;
noMarkers?: boolean;
}): any {
const {children, isFromAPIAnatomy, isFromPackageImport} = props;
return (
<React.Suspense
fallback={
<pre
className={cn(
'rounded-lg leading-6 h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg text-[13.6px] overflow-hidden',
!isFromPackageImport && !isFromAPIAnatomy && 'my-8'
)}>
<div className="py-[18px] pl-5 font-normal ">
<p className="sp-pre-placeholder overflow-hidden">{children}</p>
</div>
</pre>
}>
<CodeBlock {...props} />
</React.Suspense>
);
});
1 change: 1 addition & 0 deletions beta/src/components/MDX/MDXComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Intro from './Intro';
import Link from './Link';
import {PackageImport} from './PackageImport';
import Recap from './Recap';
import dynamic from 'next/dynamic';
import Sandpack from './Sandpack';
import SimpleCallout from './SimpleCallout';
import TerminalBlock from './TerminalBlock';
Expand Down
1 change: 1 addition & 0 deletions beta/src/components/MDX/PackageImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function PackageImport({children}: PackageImportProps) {
return (
<CodeBlock
{...child.props.children.props}
isFromPackageImport
key={i}
noMargin={true}
noMarkers={true}
Expand Down
102 changes: 102 additions & 0 deletions beta/src/components/MDX/Sandpack/SandpackWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import React from 'react';
import {
SandpackProvider,
SandpackSetup,
SandpackFile,
} from '@codesandbox/sandpack-react';

import {CustomPreset} from './CustomPreset';

type SandpackProps = {
children: React.ReactChildren;
autorun?: boolean;
setup?: SandpackSetup;
showDevTools?: boolean;
};
import {reducedCodeSnippet} from './utils';

const sandboxStyle = `
* {
box-sizing: border-box;
}

body {
font-family: sans-serif;
margin: 20px;
padding: 0;
}

h1 {
margin-top: 0;
font-size: 22px;
}

h2 {
margin-top: 0;
font-size: 20px;
}

h3 {
margin-top: 0;
font-size: 18px;
}

h4 {
margin-top: 0;
font-size: 16px;
}

h5 {
margin-top: 0;
font-size: 14px;
}

h6 {
margin-top: 0;
font-size: 12px;
}

ul {
padding-left: 20px;
}
`.trim();

function SandpackWrapper(props: SandpackProps) {
let {children, setup, autorun = true, showDevTools = false} = props;
const [devToolsLoaded, setDevToolsLoaded] = React.useState(false);
let codeSnippets = React.Children.toArray(children) as React.ReactElement[];
let isSingleFile = true;

const files = reducedCodeSnippet(codeSnippets);

files['/styles.css'] = {
code: [sandboxStyle, files['/styles.css']?.code ?? ''].join('\n\n'),
hidden: true,
};

return (
<div className="sandpack-container my-8" translate="no">
<SandpackProvider
template="react"
customSetup={{...setup, files: files}}
autorun={autorun}
initMode="user-visible"
initModeObserverOptions={{rootMargin: '1400px 0px'}}>
<CustomPreset
isSingleFile={isSingleFile}
showDevTools={showDevTools}
onDevToolsLoad={() => setDevToolsLoaded(true)}
devToolsLoaded={devToolsLoaded}
/>
</SandpackProvider>
</div>
);
}

SandpackWrapper.displayName = 'Sandpack';

export default SandpackWrapper;