Skip to content

Revert commit that breaks sandboxes that provide an index.html file #6494

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/MDX/Sandpack/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import {useSyncExternalStore} from 'react';
import {useSandpack} from '@codesandbox/sandpack-react/unstyled';
import {IconDownload} from '../../Icon/IconDownload';
import {AppJSPath, StylesCSSPath, SUPPORTED_FILES} from './createFileMap';
export interface DownloadButtonProps {}

let supportsImportMap = false;
Expand Down Expand Up @@ -33,6 +32,8 @@ function useSupportsImportMap() {
return useSyncExternalStore(subscribe, getCurrentValue, getServerSnapshot);
}

const SUPPORTED_FILES = ['/App.js', '/styles.css'];

export function DownloadButton({
providedFiles,
}: {
Expand All @@ -48,8 +49,8 @@ export function DownloadButton({
}

const downloadHTML = () => {
const css = sandpack.files[StylesCSSPath]?.code ?? '';
const code = sandpack.files[AppJSPath]?.code ?? '';
const css = sandpack.files['/styles.css']?.code ?? '';
const code = sandpack.files['/App.js']?.code ?? '';
const blob = new Blob([
`<!DOCTYPE html>
<html>
Expand Down
13 changes: 5 additions & 8 deletions src/components/MDX/Sandpack/SandpackRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {SandpackLogLevel} from '@codesandbox/sandpack-client';
import {CustomPreset} from './CustomPreset';
import {createFileMap} from './createFileMap';
import {CustomTheme} from './Themes';
import {template} from './template';

type SandpackProps = {
children: React.ReactNode;
Expand Down Expand Up @@ -71,19 +70,17 @@ function SandpackRoot(props: SandpackProps) {
const codeSnippets = Children.toArray(children) as React.ReactElement[];
const files = createFileMap(codeSnippets);

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

return (
<div className="sandpack sandpack--playground w-full my-8" dir="ltr">
<SandpackProvider
files={{...template, ...files}}
template="react"
files={files}
theme={CustomTheme}
customSetup={{
environment: 'create-react-app',
}}
options={{
autorun,
initMode: 'user-visible',
Expand Down
17 changes: 3 additions & 14 deletions src/components/MDX/Sandpack/createFileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

import type {SandpackFile} from '@codesandbox/sandpack-react/unstyled';

/**
* Ideally, we should update all markdown files and all the sandboxes
* to use the same folder structure to include `src`. However, we can
* do the same by prepending the root folder on this function.
*/
const rootFolder = '/src/';
export const AppJSPath = `/src/App.js`;
export const StylesCSSPath = `/src/styles.css`;
export const SUPPORTED_FILES = [AppJSPath, StylesCSSPath];

export const createFileMap = (codeSnippets: any) => {
return codeSnippets.reduce(
(result: Record<string, SandpackFile>, codeSnippet: React.ReactElement) => {
Expand All @@ -27,7 +17,7 @@ export const createFileMap = (codeSnippets: any) => {

if (props.meta) {
const [name, ...params] = props.meta.split(' ');
filePath = rootFolder + name;
filePath = '/' + name;
if (params.includes('hidden')) {
fileHidden = true;
}
Expand All @@ -36,16 +26,15 @@ export const createFileMap = (codeSnippets: any) => {
}
} else {
if (props.className === 'language-js') {
filePath = AppJSPath;
filePath = '/App.js';
} else if (props.className === 'language-css') {
filePath = StylesCSSPath;
filePath = '/styles.css';
} else {
throw new Error(
`Code block is missing a filename: ${props.children}`
);
}
}

if (result[filePath]) {
throw new Error(
`File ${filePath} was defined multiple times. Each file snippet should have a unique path name`
Expand Down
4 changes: 2 additions & 2 deletions src/components/MDX/Sandpack/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import {lazy, memo, Children, Suspense} from 'react';
import {AppJSPath, createFileMap} from './createFileMap';
import {createFileMap} from './createFileMap';

const SandpackRoot = lazy(() => import('./SandpackRoot'));

Expand Down Expand Up @@ -57,7 +57,7 @@ export default memo(function SandpackWrapper(props: any): any {
);
let activeCode;
if (!activeCodeSnippet.length) {
activeCode = codeSnippet[AppJSPath].code;
activeCode = codeSnippet['/App.js'].code;
} else {
activeCode = codeSnippet[activeCodeSnippet[0]].code;
}
Expand Down
54 changes: 0 additions & 54 deletions src/components/MDX/Sandpack/template.ts

This file was deleted.