1
1
<script setup lang="ts">
2
- import { SandpackInternalOptions , SandpackProvider , SandpackSetup } from " codesandbox-sandpack-vue3" ;
2
+ import {
3
+ SandpackFiles ,
4
+ SandpackInternalOptions ,
5
+ SandpackProvider ,
6
+ SandpackSetup ,
7
+ } from " codesandbox-sandpack-vue3" ;
3
8
import { useData } from " vitepress" ;
4
9
import { computed , Ref } from " vue" ;
5
10
6
-
7
11
import type { StarbeamFrontmatter } from " ../../config/site-data.js" ;
8
12
import {
9
13
DemoDeps ,
10
- toSandpackDeps , toSandpackFiles , type DemoFiles
14
+ toSandpackDeps ,
15
+ toSandpackFiles ,
16
+ type DemoFiles ,
11
17
} from " ./demo.js" ;
12
18
import DemoBody from " ./DemoBody.vue" ;
13
19
14
20
const { config } = defineProps <{
15
21
config: {
16
22
files: DemoFiles ;
17
23
dependencies: DemoDeps ;
18
-
24
+ jsx? : string ;
25
+ main? : string ;
19
26
};
20
27
}>();
21
28
@@ -26,22 +33,89 @@ const deps = computed(() => {
26
33
const versions = data .value [" @starbeam:versions" ];
27
34
const deps = toSandpackDeps (config .dependencies ) ?? {};
28
35
29
- return Object .fromEntries (Object .entries (deps ).map (([dep , version ]) => {
30
- if (dep in versions ) {
31
- return [dep , version === " package.json" ? versions [dep ] : version ]
32
- } else {
33
- throw Error (` Dependency ${dep } (used in ${info .page .value .relativePath }) not found in versions: ${JSON .stringify (versions )} ` );
36
+ return Object .fromEntries (
37
+ Object .entries (deps ).map (([dep , version ]) => {
38
+ if (dep in versions ) {
39
+ return [dep , version === " package.json" ? versions [dep ] : version ];
40
+ } else {
41
+ throw Error (
42
+ ` Dependency ${dep } (used in ${
43
+ info .page .value .relativePath
44
+ }) not found in versions: ${JSON .stringify (versions )} `
45
+ );
46
+ }
47
+ })
48
+ );
49
+ });
50
+
51
+ const BABEL_JSX = config .jsx
52
+ ? [
53
+ " transform-react-jsx" ,
54
+ {
55
+ runtime: " automatic" ,
56
+ importSource: config .jsx ,
57
+ },
58
+ ]
59
+ : [" transform-react-jsx" ];
60
+
61
+ const TSCONFIG_JSX = config .jsx
62
+ ? {
63
+ jsx: " preserve" ,
64
+ jsxImportSource: config .jsx ,
34
65
}
35
- }));
36
- })
37
-
38
-
39
- const files = {
40
- ... toSandpackFiles (config .files )
66
+ : { jsx: " preserve" };
67
+
68
+ const files: SandpackFiles = {
69
+ ... toSandpackFiles (config .files ),
70
+ " .babelrc" : {
71
+ hidden: true ,
72
+ code: JSON .stringify (
73
+ {
74
+ presets: [" env" ],
75
+ plugins: [" transform-runtime" , BABEL_JSX ],
76
+ parserOpts: {
77
+ plugins: [" dynamicImport" ],
78
+ },
79
+ },
80
+ null ,
81
+ 2
82
+ ),
83
+ },
84
+ " package.json" : JSON .stringify (
85
+ {
86
+ main: config .main ?? " /src/index.ts" ,
87
+ type: " module" ,
88
+ dependencies: deps .value ,
89
+ devDependencies: {
90
+ typescript: " ^4.0.0" ,
91
+ },
92
+ },
93
+ null ,
94
+ 2
95
+ ),
96
+ " tsconfig.json" : JSON .stringify (
97
+ {
98
+ compilerOptions: {
99
+ strict: true ,
100
+ module: " commonjs" ,
101
+ target: " esnext" ,
102
+ ... TSCONFIG_JSX ,
103
+ esModuleInterop: true ,
104
+ sourceMap: true ,
105
+ allowJs: true ,
106
+ lib: [" es6" , " dom" ],
107
+ rootDir: " src" ,
108
+ moduleResolution: " node16" ,
109
+ },
110
+ },
111
+ null ,
112
+ 2
113
+ ),
41
114
};
42
115
43
116
const options: SandpackInternalOptions = {
44
117
recompileMode: " delayed" ,
118
+ // bundlerURL: "https://db2aecf9.sandpack-bundler.pages.dev",
45
119
};
46
120
47
121
const registryURL = import .meta .env .STARBEAM_REGISTRY_URL ;
@@ -50,23 +124,31 @@ const customSetup = computed((): SandpackSetup => {
50
124
if (registryURL ) {
51
125
return {
52
126
dependencies: deps .value ,
53
- npmRegistries: [{
54
- enabledScopes: [" @starbeam" ],
55
- registryUrl: registryURL ,
56
- limitToScopes: true ,
57
- }],
127
+ entry: config .main ?? " /src/index.ts" ,
128
+ npmRegistries: [
129
+ {
130
+ enabledScopes: [" @starbeam" ],
131
+ registryUrl: registryURL ,
132
+ limitToScopes: true ,
133
+ },
134
+ ],
58
135
};
59
136
} else {
60
137
return {
61
138
dependencies: deps .value ,
62
- }
139
+ };
63
140
}
64
- })
141
+ });
65
142
</script >
66
143
67
144
<template >
68
145
<div class =" demo" >
69
- <SandpackProvider :files =" files" :options =" options" :custom-setup =" customSetup" template =" vanilla-ts" >
146
+ <SandpackProvider
147
+ :files =" files"
148
+ :options =" options"
149
+ :custom-setup =" customSetup"
150
+ template =" vanilla-ts"
151
+ >
70
152
<DemoBody />
71
153
</SandpackProvider >
72
154
</div >
@@ -108,7 +190,7 @@ div.demo {
108
190
padding : 0 ;
109
191
border : none ;
110
192
111
- & + h2 {
193
+ & + h2 {
112
194
border-block-start : 1rem ;
113
195
margin-block-start : 0 ;
114
196
}
@@ -133,14 +215,14 @@ button.toggle-button {
133
215
color : var (--sp-colors-clickable );
134
216
}
135
217
136
- >span .count {
218
+ > span .count {
137
219
display : none ;
138
220
}
139
221
140
222
& :not ([data-count = " 0" ]) {
141
223
padding-inline-end : 0.75rem ;
142
224
143
- >span .count {
225
+ > span .count {
144
226
display : grid ;
145
227
font-size : 0.75em ;
146
228
width : 2em ;
0 commit comments