@@ -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
@@ -175,67 +176,61 @@ export function getFeatureSupport(target: string[]): BuildOptions['supported'] {
175
176
return supported ;
176
177
}
177
178
178
- const MAX_CONCURRENT_WRITES = 64 ;
179
-
180
179
export async function writeResultFiles (
181
180
outputFiles : BuildOutputFile [ ] ,
182
- assetFiles : { source : string ; destination : string } [ ] | undefined ,
181
+ assetFiles : BuildOutputAsset [ ] | undefined ,
183
182
outputPath : string ,
184
183
) {
185
184
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 ) ;
185
+ const ensureDirectoryExists = async ( basePath : string ) => {
192
186
if ( basePath && ! directoryExists . has ( basePath ) ) {
193
187
await fs . mkdir ( path . join ( outputPath , basePath ) , { recursive : true } ) ;
194
188
directoryExists . add ( basePath ) ;
195
189
}
196
- // Write file contents
197
- await fs . writeFile ( path . join ( outputPath , fullOutputPath ) , file . contents ) ;
198
190
} ;
199
191
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
- }
192
+ // Writes the output file to disk and ensures the containing directories are present
193
+ await emitFilesToDisk ( outputFiles , async ( file : BuildOutputFile ) => {
194
+ const fullOutputPath = file . fullOutputPath ;
195
+ // Ensure output subdirectories exist
196
+ const basePath = path . dirname ( fullOutputPath ) ;
197
+ await ensureDirectoryExists ( basePath ) ;
208
198
209
- await Promise . all ( actions ) ;
210
- }
199
+ // Write file contents
200
+ await fs . writeFile ( path . join ( outputPath , fullOutputPath ) , file . contents ) ;
201
+ } ) ;
211
202
212
203
if ( assetFiles ?. length ) {
213
- const copyAssetFile = async ( asset : { source : string ; destination : string } ) => {
204
+ await emitFilesToDisk ( assetFiles , async ( asset : BuildOutputAsset ) => {
214
205
// Ensure output subdirectories exist
215
206
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
- }
207
+ await ensureDirectoryExists ( destPath ) ;
208
+
221
209
// Copy file contents
222
210
await fs . copyFile (
223
211
asset . source ,
224
212
path . join ( outputPath , destPath ) ,
225
213
fsConstants . COPYFILE_FICLONE ,
226
214
) ;
227
- } ;
228
-
229
- for ( let fileIndex = 0 ; fileIndex < assetFiles . length ; ) {
230
- const groupMax = Math . min ( fileIndex + MAX_CONCURRENT_WRITES , assetFiles . length ) ;
215
+ } ) ;
216
+ }
217
+ }
231
218
232
- const actions = [ ] ;
233
- while ( fileIndex < groupMax ) {
234
- actions . push ( copyAssetFile ( assetFiles [ fileIndex ++ ] ) ) ;
235
- }
219
+ const MAX_CONCURRENT_WRITES = 64 ;
220
+ async function emitFilesToDisk < T = BuildOutputAsset | BuildOutputFile > (
221
+ files : T [ ] ,
222
+ writeFileCallback : ( asset : T ) => Promise < void > ,
223
+ ) : Promise < void > {
224
+ // Write files in groups of MAX_CONCURRENT_WRITES to avoid too many open files
225
+ for ( let fileIndex = 0 ; fileIndex < files . length ; ) {
226
+ const groupMax = Math . min ( fileIndex + MAX_CONCURRENT_WRITES , files . length ) ;
236
227
237
- await Promise . all ( actions ) ;
228
+ const actions = [ ] ;
229
+ while ( fileIndex < groupMax ) {
230
+ actions . push ( writeFileCallback ( files [ fileIndex ++ ] ) ) ;
238
231
}
232
+
233
+ await Promise . all ( actions ) ;
239
234
}
240
235
}
241
236
0 commit comments