@@ -49,6 +49,11 @@ interface OutputFileRecord {
49
49
type : BuildOutputFileType ;
50
50
}
51
51
52
+ interface OutputAssetRecord {
53
+ source : string ;
54
+ updated : boolean ;
55
+ }
56
+
52
57
interface DevServerExternalResultMetadata extends Omit < ExternalResultMetadata , 'explicit' > {
53
58
explicitBrowser : string [ ] ;
54
59
explicitServer : string [ ] ;
@@ -168,7 +173,7 @@ export async function* serveWithVite(
168
173
let serverUrl : URL | undefined ;
169
174
let hadError = false ;
170
175
const generatedFiles = new Map < string , OutputFileRecord > ( ) ;
171
- const assetFiles = new Map < string , string > ( ) ;
176
+ const assetFiles = new Map < string , OutputAssetRecord > ( ) ;
172
177
const externalMetadata : DevServerExternalResultMetadata = {
173
178
implicitBrowser : [ ] ,
174
179
implicitServer : [ ] ,
@@ -229,19 +234,15 @@ export async function* serveWithVite(
229
234
assetFiles . clear ( ) ;
230
235
componentStyles . clear ( ) ;
231
236
generatedFiles . clear ( ) ;
232
- for ( const entry of Object . entries ( result . files ) ) {
233
- const [ outputPath , file ] = entry ;
234
- if ( file . origin === 'disk' ) {
235
- assetFiles . set ( '/' + normalizePath ( outputPath ) , normalizePath ( file . inputPath ) ) ;
236
- continue ;
237
- }
238
237
238
+ for ( const [ outputPath , file ] of Object . entries ( result . files ) ) {
239
239
updateResultRecord (
240
240
outputPath ,
241
241
file ,
242
242
normalizePath ,
243
243
htmlIndexPath ,
244
244
generatedFiles ,
245
+ assetFiles ,
245
246
componentStyles ,
246
247
// The initial build will not yet have a server setup
247
248
! server ,
@@ -265,23 +266,27 @@ export async function* serveWithVite(
265
266
generatedFiles . delete ( filePath ) ;
266
267
assetFiles . delete ( filePath ) ;
267
268
}
269
+
268
270
for ( const modified of result . modified ) {
269
271
updateResultRecord (
270
272
modified ,
271
273
result . files [ modified ] ,
272
274
normalizePath ,
273
275
htmlIndexPath ,
274
276
generatedFiles ,
277
+ assetFiles ,
275
278
componentStyles ,
276
279
) ;
277
280
}
281
+
278
282
for ( const added of result . added ) {
279
283
updateResultRecord (
280
284
added ,
281
285
result . files [ added ] ,
282
286
normalizePath ,
283
287
htmlIndexPath ,
284
288
generatedFiles ,
289
+ assetFiles ,
285
290
componentStyles ,
286
291
) ;
287
292
}
@@ -352,12 +357,16 @@ export async function* serveWithVite(
352
357
if ( server ) {
353
358
// Update fs allow list to include any new assets from the build option.
354
359
server . config . server . fs . allow = [
355
- ...new Set ( [ ...server . config . server . fs . allow , ...assetFiles . values ( ) ] ) ,
360
+ ...new Set ( [
361
+ ...server . config . server . fs . allow ,
362
+ ...[ ...assetFiles . values ( ) ] . map ( ( { source } ) => source ) ,
363
+ ] ) ,
356
364
] ;
357
365
358
366
await handleUpdate (
359
367
normalizePath ,
360
368
generatedFiles ,
369
+ assetFiles ,
361
370
server ,
362
371
serverOptions ,
363
372
context . logger ,
@@ -471,15 +480,26 @@ export async function* serveWithVite(
471
480
async function handleUpdate (
472
481
normalizePath : ( id : string ) => string ,
473
482
generatedFiles : Map < string , OutputFileRecord > ,
483
+ assetFiles : Map < string , OutputAssetRecord > ,
474
484
server : ViteDevServer ,
475
485
serverOptions : NormalizedDevServerOptions ,
476
486
logger : BuilderContext [ 'logger' ] ,
477
487
componentStyles : Map < string , ComponentStyleRecord > ,
478
488
) : Promise < void > {
479
489
const updatedFiles : string [ ] = [ ] ;
480
- let destroyAngularServerAppCalled = false ;
490
+
491
+ // Invalidate any updated asset
492
+ for ( const [ file , record ] of assetFiles ) {
493
+ if ( ! record . updated ) {
494
+ continue ;
495
+ }
496
+
497
+ record . updated = false ;
498
+ updatedFiles . push ( file ) ;
499
+ }
481
500
482
501
// Invalidate any updated files
502
+ let destroyAngularServerAppCalled = false ;
483
503
for ( const [ file , record ] of generatedFiles ) {
484
504
if ( ! record . updated ) {
485
505
continue ;
@@ -584,10 +604,16 @@ function updateResultRecord(
584
604
normalizePath : ( id : string ) => string ,
585
605
htmlIndexPath : string ,
586
606
generatedFiles : Map < string , OutputFileRecord > ,
607
+ assetFiles : Map < string , OutputAssetRecord > ,
587
608
componentStyles : Map < string , ComponentStyleRecord > ,
588
609
initial = false ,
589
610
) : void {
590
611
if ( file . origin === 'disk' ) {
612
+ assetFiles . set ( '/' + normalizePath ( outputPath ) , {
613
+ source : normalizePath ( file . inputPath ) ,
614
+ updated : ! initial ,
615
+ } ) ;
616
+
591
617
return ;
592
618
}
593
619
@@ -644,7 +670,7 @@ function updateResultRecord(
644
670
export async function setupServer (
645
671
serverOptions : NormalizedDevServerOptions ,
646
672
outputFiles : Map < string , OutputFileRecord > ,
647
- assets : Map < string , string > ,
673
+ assets : Map < string , OutputAssetRecord > ,
648
674
preserveSymlinks : boolean | undefined ,
649
675
externalMetadata : DevServerExternalResultMetadata ,
650
676
ssrMode : ServerSsrMode ,
@@ -743,7 +769,11 @@ export async function setupServer(
743
769
// The first two are required for Vite to function in prebundling mode (the default) and to load
744
770
// the Vite client-side code for browser reloading. These would be available by default but when
745
771
// the `allow` option is explicitly configured, they must be included manually.
746
- allow : [ cacheDir , join ( serverOptions . workspaceRoot , 'node_modules' ) , ...assets . values ( ) ] ,
772
+ allow : [
773
+ cacheDir ,
774
+ join ( serverOptions . workspaceRoot , 'node_modules' ) ,
775
+ ...[ ...assets . values ( ) ] . map ( ( { source } ) => source ) ,
776
+ ] ,
747
777
} ,
748
778
// This is needed when `externalDependencies` is used to prevent Vite load errors.
749
779
// NOTE: If Vite adds direct support for externals, this can be removed.
0 commit comments