11import { load } from '@webcontainer/api' ;
2-
32import base64 from 'base64-js' ;
43import { ready } from '../common/index.js' ;
54
@@ -13,8 +12,12 @@ export async function create(stubs) {
1312 const tree = convert_stubs_to_tree ( stubs ) ;
1413
1514 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+ } ;
1821
1922 const vm = await WebContainer . boot ( ) ;
2023 await vm . loadFiles ( tree ) ;
@@ -113,7 +116,7 @@ export async function create(stubs) {
113116 tree = /** @type {import('@webcontainer/api').DirectoryEntry } */ ( tree [ part ] ) . directory ;
114117 }
115118
116- tree [ basename ] = file ( stub . text ? stub . contents : base64 . toByteArray ( stub . contents ) ) ;
119+ tree [ basename ] = to_file ( stub ) ;
117120 }
118121
119122 await vm . loadFiles ( root ) ;
@@ -144,16 +147,30 @@ function convert_stubs_to_tree(stubs, depth = 1) {
144147 directory : convert_stubs_to_tree ( children , depth + 1 )
145148 } ;
146149 } else {
147- tree [ stub . basename ] = file ( stub . text ? stub . contents : base64 . toByteArray ( stub . contents ) ) ;
150+ tree [ stub . basename ] = to_file ( stub ) ;
148151 }
149152 }
150153 }
151154
152155 return tree ;
153156}
154157
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+
157174 return {
158175 file : { contents }
159176 } ;
0 commit comments