@@ -3,6 +3,7 @@ import type { RolldownBuild } from 'rolldown'
3
3
import { type DevEngine , dev } from 'rolldown/experimental'
4
4
import type { Update } from 'types/hmrPayload'
5
5
import colors from 'picocolors'
6
+ import getEtag from 'etag'
6
7
import { ChunkMetadataMap , resolveRolldownOptions } from '../../build'
7
8
import { getHmrImplementation } from '../../plugins/clientInjections'
8
9
import { DevEnvironment , type DevEnvironmentContext } from '../environment'
@@ -19,17 +20,19 @@ type HmrOutput = Exclude<
19
20
undefined
20
21
>
21
22
23
+ type MemoryFile = {
24
+ source : string | Uint8Array
25
+ etag ?: string
26
+ }
27
+
22
28
export class MemoryFiles {
23
- private files = new Map <
24
- string ,
25
- string | Uint8Array | ( ( ) => string | Uint8Array )
26
- > ( )
29
+ private files = new Map < string , MemoryFile | ( ( ) => MemoryFile ) > ( )
27
30
28
31
get size ( ) : number {
29
32
return this . files . size
30
33
}
31
34
32
- get ( file : string ) : string | Uint8Array | undefined {
35
+ get ( file : string ) : MemoryFile | undefined {
33
36
const result = this . files . get ( file )
34
37
if ( result === undefined ) {
35
38
return undefined
@@ -42,10 +45,7 @@ export class MemoryFiles {
42
45
return result
43
46
}
44
47
45
- set (
46
- file : string ,
47
- content : string | Uint8Array | ( ( ) => string | Uint8Array ) ,
48
- ) : void {
48
+ set ( file : string , content : MemoryFile | ( ( ) => MemoryFile ) ) : void {
49
49
this . files . set ( file , content )
50
50
}
51
51
@@ -265,11 +265,16 @@ export class FullBundleDevEnvironment extends DevEnvironment {
265
265
handler : ( _ , bundle ) => {
266
266
// NOTE: don't clear memoryFiles here as incremental build re-uses the files
267
267
for ( const outputFile of Object . values ( bundle ) ) {
268
- this . memoryFiles . set ( outputFile . fileName , ( ) =>
269
- outputFile . type === 'chunk'
270
- ? outputFile . code
271
- : outputFile . source ,
272
- )
268
+ this . memoryFiles . set ( outputFile . fileName , ( ) => {
269
+ const source =
270
+ outputFile . type === 'chunk'
271
+ ? outputFile . code
272
+ : outputFile . source
273
+ return {
274
+ source,
275
+ etag : getEtag ( Buffer . from ( source ) , { weak : true } ) ,
276
+ }
277
+ } )
273
278
}
274
279
} ,
275
280
} ,
@@ -336,9 +341,11 @@ export class FullBundleDevEnvironment extends DevEnvironment {
336
341
code : typeof hmrOutput . code === 'string' ? '[code]' : hmrOutput . code ,
337
342
} )
338
343
339
- this . memoryFiles . set ( hmrOutput . filename , hmrOutput . code )
344
+ this . memoryFiles . set ( hmrOutput . filename , { source : hmrOutput . code } )
340
345
if ( hmrOutput . sourcemapFilename && hmrOutput . sourcemap ) {
341
- this . memoryFiles . set ( hmrOutput . sourcemapFilename , hmrOutput . sourcemap )
346
+ this . memoryFiles . set ( hmrOutput . sourcemapFilename , {
347
+ source : hmrOutput . sourcemap ,
348
+ } )
342
349
}
343
350
const updates : Update [ ] = hmrOutput . hmrBoundaries . map ( ( boundary : any ) => {
344
351
return {
0 commit comments