@@ -19,6 +19,7 @@ import { BudgetCalculatorResult } from '../../utils/bundle-calculator';
19
19
import { Spinner } from '../../utils/spinner' ;
20
20
import { BundleStats , generateBuildStatsTable } from '../webpack/utils/stats' ;
21
21
import { BuildOutputFile , BuildOutputFileType , InitialFileRecord } from './bundler-context' ;
22
+ import { BuildOutputAsset } from './bundler-execution-result' ;
22
23
23
24
const compressAsync = promisify ( brotliCompress ) ;
24
25
@@ -179,63 +180,58 @@ const MAX_CONCURRENT_WRITES = 64;
179
180
180
181
export async function writeResultFiles (
181
182
outputFiles : BuildOutputFile [ ] ,
182
- assetFiles : { source : string ; destination : string } [ ] | undefined ,
183
+ assetFiles : BuildOutputAsset [ ] | undefined ,
183
184
outputPath : string ,
184
185
) {
185
186
const directoryExists = new Set < string > ( ) ;
186
-
187
- // Writes the output file to disk and ensures the containing directories are present
188
- const writeOutputFile = async ( file : BuildOutputFile ) => {
189
- const fullOutputPath = file . fullOutputPath ;
190
- // Ensure output subdirectories exist
191
- const basePath = path . dirname ( fullOutputPath ) ;
187
+ const ensureDirectoryExists = async ( basePath : string ) => {
192
188
if ( basePath && ! directoryExists . has ( basePath ) ) {
193
189
await fs . mkdir ( path . join ( outputPath , basePath ) , { recursive : true } ) ;
194
190
directoryExists . add ( basePath ) ;
195
191
}
196
- // Write file contents
197
- await fs . writeFile ( path . join ( outputPath , fullOutputPath ) , file . contents ) ;
198
192
} ;
199
193
200
- // Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
201
- for ( let fileIndex = 0 ; fileIndex < outputFiles . length ; ) {
202
- const groupMax = Math . min ( fileIndex + MAX_CONCURRENT_WRITES , outputFiles . length ) ;
203
-
204
- const actions = [ ] ;
205
- while ( fileIndex < groupMax ) {
206
- actions . push ( writeOutputFile ( outputFiles [ fileIndex ++ ] ) ) ;
207
- }
194
+ // Writes the output file to disk and ensures the containing directories are present
195
+ await emitFilesToDisk ( outputFiles , async ( file : BuildOutputFile ) => {
196
+ const fullOutputPath = file . fullOutputPath ;
197
+ // Ensure output subdirectories exist
198
+ const basePath = path . dirname ( fullOutputPath ) ;
199
+ await ensureDirectoryExists ( basePath ) ;
208
200
209
- await Promise . all ( actions ) ;
210
- }
201
+ // Write file contents
202
+ await fs . writeFile ( path . join ( outputPath , fullOutputPath ) , file . contents ) ;
203
+ } ) ;
211
204
212
205
if ( assetFiles ?. length ) {
213
- const copyAssetFile = async ( asset : { source : string ; destination : string } ) => {
206
+ await emitFilesToDisk ( assetFiles , async ( asset : BuildOutputAsset ) => {
214
207
// Ensure output subdirectories exist
215
208
const destPath = join ( 'browser' , asset . destination ) ;
216
- const basePath = path . dirname ( destPath ) ;
217
- if ( basePath && ! directoryExists . has ( basePath ) ) {
218
- await fs . mkdir ( path . join ( outputPath , basePath ) , { recursive : true } ) ;
219
- directoryExists . add ( basePath ) ;
220
- }
209
+ await ensureDirectoryExists ( destPath ) ;
210
+
221
211
// Copy file contents
222
212
await fs . copyFile (
223
213
asset . source ,
224
214
path . join ( outputPath , destPath ) ,
225
215
fsConstants . COPYFILE_FICLONE ,
226
216
) ;
227
- } ;
228
-
229
- for ( let fileIndex = 0 ; fileIndex < assetFiles . length ; ) {
230
- const groupMax = Math . min ( fileIndex + MAX_CONCURRENT_WRITES , assetFiles . length ) ;
217
+ } ) ;
218
+ }
219
+ }
231
220
232
- const actions = [ ] ;
233
- while ( fileIndex < groupMax ) {
234
- actions . push ( copyAssetFile ( assetFiles [ fileIndex ++ ] ) ) ;
235
- }
221
+ async function emitFilesToDisk < T = BuildOutputAsset | BuildOutputFile > (
222
+ files : T [ ] ,
223
+ writeFileCallback : ( asset : T ) => Promise < void > ,
224
+ ) : Promise < void > {
225
+ // Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
226
+ for ( let fileIndex = 0 ; fileIndex < files . length ; ) {
227
+ const groupMax = Math . min ( fileIndex + MAX_CONCURRENT_WRITES , files . length ) ;
236
228
237
- await Promise . all ( actions ) ;
229
+ const actions = [ ] ;
230
+ while ( fileIndex < groupMax ) {
231
+ actions . push ( writeFileCallback ( files [ fileIndex ++ ] ) ) ;
238
232
}
233
+
234
+ await Promise . all ( actions ) ;
239
235
}
240
236
}
241
237
0 commit comments