1
1
import cn from '@/lib/cn'
2
2
import { crawl } from '@/utils/docs'
3
- import { Sandpack as SP } from '@codesandbox/sandpack-react'
3
+ import {
4
+ Sandpack as SP ,
5
+ type SandpackFile ,
6
+ type SandpackFiles ,
7
+ type SandpackProps ,
8
+ } from '@codesandbox/sandpack-react'
4
9
import fs from 'node:fs'
5
10
import path from 'node:path'
6
- import { ComponentProps } from 'react'
11
+
12
+ type Files = Record < string , Omit < SandpackFile , 'code' > >
7
13
8
14
function getSandpackDependencies ( folder : string ) {
9
15
const pkgPath = `${ folder } /package.json`
@@ -13,49 +19,56 @@ function getSandpackDependencies(folder: string) {
13
19
return JSON . parse ( str ) . dependencies as Record < string , string >
14
20
}
15
21
16
- type File = { code : string }
17
-
18
- async function getSandpackFiles ( folder : string , extensions = [ 'js' , 'ts' , 'jsx' , 'tsx' , 'css' ] ) {
22
+ async function getSandpackFiles (
23
+ folder : string ,
24
+ files : Files = { } ,
25
+ extensions = [ 'js' , 'ts' , 'jsx' , 'tsx' , 'css' ] ,
26
+ ) {
19
27
const filepaths = await crawl (
20
28
folder ,
21
29
( dir ) =>
22
30
! dir . includes ( 'node_modules' ) && extensions . map ( ( ext ) => dir . endsWith ( ext ) ) . some ( Boolean ) ,
23
31
)
24
32
// console.log('filepaths', filepaths)
25
33
26
- const files = filepaths . reduce (
27
- ( acc , filepath ) => {
28
- const relativeFilepath = path . relative ( folder , filepath )
29
-
30
- return {
31
- ...acc ,
32
- [ `/${ relativeFilepath } ` ] : {
33
- code : fs . readFileSync ( filepath , 'utf-8' ) ,
34
- } ,
35
- }
36
- } ,
37
- { } as Record < string , File > ,
38
- )
34
+ return filepaths . reduce ( ( acc , filepath ) => {
35
+ const relativeFilepath = path . relative ( folder , filepath )
39
36
40
- return files
37
+ const key = `/${ relativeFilepath } `
38
+ const file = files [ key ]
39
+ return {
40
+ ...acc ,
41
+ [ key ] : {
42
+ ...file ,
43
+ code : fs . readFileSync ( filepath , 'utf-8' ) ,
44
+ } ,
45
+ }
46
+ } , { } as SandpackFiles )
41
47
}
42
48
43
49
// https://sandpack.codesandbox.io/docs/getting-started/usage
44
50
export const Sandpack = async ( {
45
51
className,
46
52
folder,
53
+ files,
47
54
...props
48
- } : { className : string ; folder ?: string } & ComponentProps < typeof SP > ) => {
55
+ } : {
56
+ className : string
57
+ folder ?: string
58
+ files ?: Files
59
+ } & Omit < SandpackProps , 'files' > ) => {
49
60
// console.log('folder', folder)
50
61
51
- const files = folder ? await getSandpackFiles ( folder ) : props . files
62
+ const _files = folder ? await getSandpackFiles ( folder , files ) : files
63
+ // console.log('_files', _files)
52
64
53
65
const pkgDeps = folder ? getSandpackDependencies ( folder ) : null
54
66
const dependencies = pkgDeps ?? props . customSetup ?. dependencies
55
67
const customSetup = {
56
68
...props . customSetup ,
57
69
dependencies,
58
70
}
71
+ // console.log('customSetup', customSetup)
59
72
60
73
const options = {
61
74
...props . options ,
@@ -64,7 +77,7 @@ export const Sandpack = async ({
64
77
65
78
return (
66
79
< div className = { cn ( className , 'sandpack' ) } >
67
- < SP { ...props } files = { files } customSetup = { customSetup } options = { options } />
80
+ < SP { ...props } files = { _files } customSetup = { customSetup } options = { options } />
68
81
</ div >
69
82
)
70
83
}
0 commit comments