1
1
import { load } from '@webcontainer/api' ;
2
-
3
2
import base64 from 'base64-js' ;
4
3
import { ready } from '../common/index.js' ;
5
4
@@ -13,8 +12,12 @@ export async function create(stubs) {
13
12
const tree = convert_stubs_to_tree ( stubs ) ;
14
13
15
14
const common = await ready ;
16
- tree [ 'common.zip' ] = file ( new Uint8Array ( common . zipped ) ) ;
17
- tree [ 'unzip.cjs' ] = file ( common . unzip ) ;
15
+ tree [ 'common.zip' ] = {
16
+ file : { contents : new Uint8Array ( common . zipped ) }
17
+ } ;
18
+ tree [ 'unzip.cjs' ] = {
19
+ file : { contents : common . unzip }
20
+ } ;
18
21
19
22
const vm = await WebContainer . boot ( ) ;
20
23
await vm . loadFiles ( tree ) ;
@@ -113,7 +116,7 @@ export async function create(stubs) {
113
116
tree = /** @type {import('@webcontainer/api').DirectoryEntry } */ ( tree [ part ] ) . directory ;
114
117
}
115
118
116
- tree [ basename ] = file ( stub . text ? stub . contents : base64 . toByteArray ( stub . contents ) ) ;
119
+ tree [ basename ] = to_file ( stub ) ;
117
120
}
118
121
119
122
await vm . loadFiles ( root ) ;
@@ -144,16 +147,30 @@ function convert_stubs_to_tree(stubs, depth = 1) {
144
147
directory : convert_stubs_to_tree ( children , depth + 1 )
145
148
} ;
146
149
} else {
147
- tree [ stub . basename ] = file ( stub . text ? stub . contents : base64 . toByteArray ( stub . contents ) ) ;
150
+ tree [ stub . basename ] = to_file ( stub ) ;
148
151
}
149
152
}
150
153
}
151
154
152
155
return tree ;
153
156
}
154
157
155
- /** @param {string | Uint8Array } contents */
156
- function file ( contents ) {
158
+ /** @param {import('$lib/types').FileStub } stub */
159
+ function to_file ( stub ) {
160
+ // special case
161
+ if ( stub . name === '/src/app.html' ) {
162
+ const contents = stub . contents . replace (
163
+ '</head>' ,
164
+ '<script type="module" src="/src/__client.js"></script></head>'
165
+ ) ;
166
+
167
+ return {
168
+ file : { contents }
169
+ } ;
170
+ }
171
+
172
+ const contents = stub . text ? stub . contents : base64 . toByteArray ( stub . contents ) ;
173
+
157
174
return {
158
175
file : { contents }
159
176
} ;
0 commit comments