@@ -2,8 +2,9 @@ const path = require('path');
2
2
const packageExists = require ( './utils/package-exists' ) ;
3
3
const webpack = packageExists ( 'webpack' ) ? require ( 'webpack' ) : undefined ;
4
4
const webpackMerge = require ( 'webpack-merge' ) ;
5
- const { writeFileSync , existsSync } = require ( 'fs' ) ;
5
+ const { createWriteStream , existsSync } = require ( 'fs' ) ;
6
6
const { options : coloretteOptions , yellow } = require ( 'colorette' ) ;
7
+ const { stringifyStream : createJsonStringifyStream } = require ( '@discoveryjs/json-ext' ) ;
7
8
8
9
const logger = require ( './utils/logger' ) ;
9
10
const { cli, flags } = require ( './utils/cli-flags' ) ;
@@ -485,21 +486,23 @@ class WebpackCLI {
485
486
const foundStats = compiler . compilers
486
487
? { children : compiler . compilers . map ( getStatsOptionsFromCompiler ) }
487
488
: getStatsOptionsFromCompiler ( compiler ) ;
489
+ const handleWriteError = ( error ) => {
490
+ logger . error ( error ) ;
491
+ process . exit ( 2 ) ;
492
+ } ;
488
493
489
494
if ( args . json === true ) {
490
- process . stdout . write ( JSON . stringify ( stats . toJson ( foundStats ) ) + '\n' ) ;
495
+ createJsonStringifyStream ( stats . toJson ( foundStats ) )
496
+ . on ( 'error' , handleWriteError )
497
+ . pipe ( process . stdout )
498
+ . on ( 'error' , handleWriteError )
499
+ . on ( 'close' , ( ) => process . stdout . write ( '\n' ) ) ;
491
500
} else if ( typeof args . json === 'string' ) {
492
- const JSONStats = JSON . stringify ( stats . toJson ( foundStats ) ) ;
493
-
494
- try {
495
- writeFileSync ( args . json , JSONStats ) ;
496
-
497
- logger . success ( `stats are successfully stored as json to ${ args . json } ` ) ;
498
- } catch ( error ) {
499
- logger . error ( error ) ;
500
-
501
- process . exit ( 2 ) ;
502
- }
501
+ createJsonStringifyStream ( stats . toJson ( foundStats ) )
502
+ . on ( 'error' , handleWriteError )
503
+ . pipe ( createWriteStream ( args . json ) )
504
+ . on ( 'error' , handleWriteError )
505
+ . on ( 'close' , ( ) => logger . success ( `stats are successfully stored as json to ${ args . json } ` ) ) ;
503
506
} else {
504
507
const printedStats = stats . toString ( foundStats ) ;
505
508
0 commit comments