@@ -22,11 +22,50 @@ type HmrOutput = Exclude<
22
22
undefined
23
23
>
24
24
25
+ export class MemoryFiles {
26
+ private files = new Map <
27
+ string ,
28
+ string | Uint8Array | ( ( ) => string | Uint8Array )
29
+ > ( )
30
+
31
+ get size ( ) : number {
32
+ return this . files . size
33
+ }
34
+
35
+ get ( file : string ) : string | Uint8Array | undefined {
36
+ const result = this . files . get ( file )
37
+ if ( result === undefined ) {
38
+ return undefined
39
+ }
40
+ if ( typeof result === 'function' ) {
41
+ const content = result ( )
42
+ this . files . set ( file , content )
43
+ return content
44
+ }
45
+ return result
46
+ }
47
+
48
+ set (
49
+ file : string ,
50
+ content : string | Uint8Array | ( ( ) => string | Uint8Array ) ,
51
+ ) : void {
52
+ this . files . set ( file , content )
53
+ }
54
+
55
+ has ( file : string ) : boolean {
56
+ return this . files . has ( file )
57
+ }
58
+
59
+ clear ( ) : void {
60
+ this . files . clear ( )
61
+ }
62
+ }
63
+
25
64
export class FullBundleDevEnvironment extends DevEnvironment {
26
65
private state : BundleState = { type : 'initial' }
27
66
28
67
watchFiles = new Set < string > ( )
29
- memoryFiles = new Map < string , string | Uint8Array > ( )
68
+ memoryFiles = new MemoryFiles ( )
30
69
31
70
constructor (
32
71
name : string ,
@@ -299,14 +338,13 @@ export class FullBundleDevEnvironment extends DevEnvironment {
299
338
) {
300
339
try {
301
340
const startTime = Date . now ( )
302
- const newMemoryFiles = new Map < string , string | Uint8Array > ( )
341
+ const newMemoryFiles = new Map < string , ( ) => string | Uint8Array > ( )
303
342
for ( const outputOpts of arraify ( outOpts ) ) {
304
343
const output = await bundle . generate ( outputOpts )
305
344
if ( signal . aborted ) return
306
345
307
346
for ( const outputFile of output . output ) {
308
- newMemoryFiles . set (
309
- outputFile . fileName ,
347
+ newMemoryFiles . set ( outputFile . fileName , ( ) =>
310
348
outputFile . type === 'chunk' ? outputFile . code : outputFile . source ,
311
349
)
312
350
}
0 commit comments