diff --git a/docs/getting-started/authoring.mdx b/docs/getting-started/authoring.mdx
index 063dcce7..9db66684 100644
--- a/docs/getting-started/authoring.mdx
+++ b/docs/getting-started/authoring.mdx
@@ -376,6 +376,43 @@ Instead of `files`, a `folder` prop allow you to pass a folder containing all th
+It is also possible to pass some individual [file format](https://sandpack.codesandbox.io/docs/getting-started/usage#file-format) configuration:
+
+```tsx
+
+```
+
+
+ Result
+
+
+
+
+
### `Codesandbox`
```md
diff --git a/src/components/mdx/Sandpack/Sandpack.tsx b/src/components/mdx/Sandpack/Sandpack.tsx
index 69e0184a..8346acb3 100644
--- a/src/components/mdx/Sandpack/Sandpack.tsx
+++ b/src/components/mdx/Sandpack/Sandpack.tsx
@@ -1,9 +1,15 @@
import cn from '@/lib/cn'
import { crawl } from '@/utils/docs'
-import { Sandpack as SP } from '@codesandbox/sandpack-react'
+import {
+ Sandpack as SP,
+ type SandpackFile,
+ type SandpackFiles,
+ type SandpackProps,
+} from '@codesandbox/sandpack-react'
import fs from 'node:fs'
import path from 'node:path'
-import { ComponentProps } from 'react'
+
+type Files = Record>
function getSandpackDependencies(folder: string) {
const pkgPath = `${folder}/package.json`
@@ -13,9 +19,11 @@ function getSandpackDependencies(folder: string) {
return JSON.parse(str).dependencies as Record
}
-type File = { code: string }
-
-async function getSandpackFiles(folder: string, extensions = ['js', 'ts', 'jsx', 'tsx', 'css']) {
+async function getSandpackFiles(
+ folder: string,
+ files: Files = {},
+ extensions = ['js', 'ts', 'jsx', 'tsx', 'css'],
+) {
const filepaths = await crawl(
folder,
(dir) =>
@@ -23,32 +31,36 @@ async function getSandpackFiles(folder: string, extensions = ['js', 'ts', 'jsx',
)
// console.log('filepaths', filepaths)
- const files = filepaths.reduce(
- (acc, filepath) => {
- const relativeFilepath = path.relative(folder, filepath)
-
- return {
- ...acc,
- [`/${relativeFilepath}`]: {
- code: fs.readFileSync(filepath, 'utf-8'),
- },
- }
- },
- {} as Record,
- )
+ return filepaths.reduce((acc, filepath) => {
+ const relativeFilepath = path.relative(folder, filepath)
- return files
+ const key = `/${relativeFilepath}`
+ const file = files[key]
+ return {
+ ...acc,
+ [key]: {
+ ...file,
+ code: fs.readFileSync(filepath, 'utf-8'),
+ },
+ }
+ }, {} as SandpackFiles)
}
// https://sandpack.codesandbox.io/docs/getting-started/usage
export const Sandpack = async ({
className,
folder,
+ files,
...props
-}: { className: string; folder?: string } & ComponentProps) => {
+}: {
+ className: string
+ folder?: string
+ files?: Files
+} & Omit) => {
// console.log('folder', folder)
- const files = folder ? await getSandpackFiles(folder) : props.files
+ const _files = folder ? await getSandpackFiles(folder, files) : files
+ // console.log('_files', _files)
const pkgDeps = folder ? getSandpackDependencies(folder) : null
const dependencies = pkgDeps ?? props.customSetup?.dependencies
@@ -56,6 +68,7 @@ export const Sandpack = async ({
...props.customSetup,
dependencies,
}
+ // console.log('customSetup', customSetup)
const options = {
...props.options,
@@ -64,7 +77,7 @@ export const Sandpack = async ({
return (
-
+
)
}