8
8
9
9
import type { BuilderContext } from '@angular-devkit/architect' ;
10
10
import type { json } from '@angular-devkit/core' ;
11
+ import type { OutputFile } from 'esbuild' ;
11
12
import { lookup as lookupMimeType } from 'mrmime' ;
12
13
import assert from 'node:assert' ;
13
14
import { BinaryLike , createHash } from 'node:crypto' ;
@@ -55,71 +56,25 @@ export async function* serveWithVite(
55
56
56
57
let server : ViteDevServer | undefined ;
57
58
let listeningAddress : AddressInfo | undefined ;
58
- const outputFiles = new Map < string , OutputFileRecord > ( ) ;
59
- const assets = new Map < string , string > ( ) ;
59
+ const generatedFiles = new Map < string , OutputFileRecord > ( ) ;
60
+ const assetFiles = new Map < string , string > ( ) ;
60
61
// TODO: Switch this to an architect schedule call when infrastructure settings are supported
61
62
for await ( const result of buildEsbuildBrowser ( browserOptions , context , { write : false } ) ) {
62
63
assert ( result . outputFiles , 'Builder did not provide result files.' ) ;
63
64
64
65
// Analyze result files for changes
65
- const seen = new Set < string > ( [ '/index.html' ] ) ;
66
- for ( const file of result . outputFiles ) {
67
- const filePath = '/' + normalizePath ( file . path ) ;
68
- seen . add ( filePath ) ;
69
-
70
- // Skip analysis of sourcemaps
71
- if ( filePath . endsWith ( '.map' ) ) {
72
- outputFiles . set ( filePath , {
73
- contents : file . contents ,
74
- size : file . contents . byteLength ,
75
- updated : false ,
76
- } ) ;
77
-
78
- continue ;
79
- }
80
-
81
- let fileHash : Buffer | undefined ;
82
- const existingRecord = outputFiles . get ( filePath ) ;
83
- if ( existingRecord && existingRecord . size === file . contents . byteLength ) {
84
- // Only hash existing file when needed
85
- if ( existingRecord . hash === undefined ) {
86
- existingRecord . hash = hashContent ( existingRecord . contents ) ;
87
- }
66
+ analyzeResultFiles ( result . outputFiles , generatedFiles ) ;
88
67
89
- // Compare against latest result output
90
- fileHash = hashContent ( file . contents ) ;
91
- if ( fileHash . equals ( existingRecord . hash ) ) {
92
- // Same file
93
- existingRecord . updated = false ;
94
- continue ;
95
- }
96
- }
97
-
98
- outputFiles . set ( filePath , {
99
- contents : file . contents ,
100
- size : file . contents . byteLength ,
101
- hash : fileHash ,
102
- updated : true ,
103
- } ) ;
104
- }
105
-
106
- // Clear stale output files
107
- for ( const file of outputFiles . keys ( ) ) {
108
- if ( ! seen . has ( file ) ) {
109
- outputFiles . delete ( file ) ;
110
- }
111
- }
112
-
113
- assets . clear ( ) ;
68
+ assetFiles . clear ( ) ;
114
69
if ( result . assetFiles ) {
115
70
for ( const asset of result . assetFiles ) {
116
- assets . set ( '/' + normalizePath ( asset . destination ) , asset . source ) ;
71
+ assetFiles . set ( '/' + normalizePath ( asset . destination ) , asset . source ) ;
117
72
}
118
73
}
119
74
120
75
if ( server ) {
121
76
// Invalidate any updated files
122
- for ( const [ file , record ] of outputFiles ) {
77
+ for ( const [ file , record ] of generatedFiles ) {
123
78
if ( record . updated ) {
124
79
const updatedModules = server . moduleGraph . getModulesByFile ( file ) ;
125
80
updatedModules ?. forEach ( ( m ) => server ?. moduleGraph . invalidateModule ( m ) ) ;
@@ -137,7 +92,8 @@ export async function* serveWithVite(
137
92
}
138
93
} else {
139
94
// Setup server and start listening
140
- server = await setupServer ( serverOptions , outputFiles , assets ) ;
95
+ const serverConfiguration = await setupServer ( serverOptions , generatedFiles , assetFiles ) ;
96
+ server = await createServer ( serverConfiguration ) ;
141
97
142
98
await server . listen ( ) ;
143
99
listeningAddress = server . httpServer ?. address ( ) as AddressInfo ;
@@ -160,11 +116,64 @@ export async function* serveWithVite(
160
116
}
161
117
}
162
118
163
- async function setupServer (
119
+ function analyzeResultFiles (
120
+ resultFiles : OutputFile [ ] ,
121
+ generatedFiles : Map < string , OutputFileRecord > ,
122
+ ) {
123
+ const seen = new Set < string > ( [ '/index.html' ] ) ;
124
+ for ( const file of resultFiles ) {
125
+ const filePath = '/' + normalizePath ( file . path ) ;
126
+ seen . add ( filePath ) ;
127
+
128
+ // Skip analysis of sourcemaps
129
+ if ( filePath . endsWith ( '.map' ) ) {
130
+ generatedFiles . set ( filePath , {
131
+ contents : file . contents ,
132
+ size : file . contents . byteLength ,
133
+ updated : false ,
134
+ } ) ;
135
+
136
+ continue ;
137
+ }
138
+
139
+ let fileHash : Buffer | undefined ;
140
+ const existingRecord = generatedFiles . get ( filePath ) ;
141
+ if ( existingRecord && existingRecord . size === file . contents . byteLength ) {
142
+ // Only hash existing file when needed
143
+ if ( existingRecord . hash === undefined ) {
144
+ existingRecord . hash = hashContent ( existingRecord . contents ) ;
145
+ }
146
+
147
+ // Compare against latest result output
148
+ fileHash = hashContent ( file . contents ) ;
149
+ if ( fileHash . equals ( existingRecord . hash ) ) {
150
+ // Same file
151
+ existingRecord . updated = false ;
152
+ continue ;
153
+ }
154
+ }
155
+
156
+ generatedFiles . set ( filePath , {
157
+ contents : file . contents ,
158
+ size : file . contents . byteLength ,
159
+ hash : fileHash ,
160
+ updated : true ,
161
+ } ) ;
162
+ }
163
+
164
+ // Clear stale output files
165
+ for ( const file of generatedFiles . keys ( ) ) {
166
+ if ( ! seen . has ( file ) ) {
167
+ generatedFiles . delete ( file ) ;
168
+ }
169
+ }
170
+ }
171
+
172
+ export async function setupServer (
164
173
serverOptions : NormalizedDevServerOptions ,
165
174
outputFiles : Map < string , OutputFileRecord > ,
166
175
assets : Map < string , string > ,
167
- ) : Promise < ViteDevServer > {
176
+ ) : Promise < InlineConfig > {
168
177
const proxy = await loadProxyConfiguration (
169
178
serverOptions . workspaceRoot ,
170
179
serverOptions . proxyConfig ,
@@ -330,7 +339,5 @@ async function setupServer(
330
339
}
331
340
}
332
341
333
- const server = await createServer ( configuration ) ;
334
-
335
- return server ;
342
+ return configuration ;
336
343
}
0 commit comments