From 4dfacfff2ac0025e05ce48f35b2209999d0c9754 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Wed, 22 Jul 2015 16:44:04 -0400 Subject: [PATCH] Wait until there some data before opening write stream This waits until browser-pack has something to be read before opening the write stream. This prevents issues with bundles that have a long processing time (mostly in module-deps), where you'd have 0 byte output file for a while before anything actually gets written to it. Fixes #239 and #272 --- bin/cmd.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 0a5c3f2..942ac6c 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -2,6 +2,7 @@ var path = require('path'); var outpipe = require('outpipe'); +var through = require('through2'); var fromArgs = require('./args.js'); var w = fromArgs(process.argv.slice(2)); @@ -33,24 +34,31 @@ bundle(); function bundle () { var didError = false; - var outStream = outpipe(outfile); - + var writer = through(); var wb = w.bundle(); + + w.pipeline.get('pack').once('readable', function() { + wb.pipe(writer); + }); + wb.on('error', function (err) { console.error(String(err)); didError = true; - outStream.end('console.error('+JSON.stringify(String(err))+');'); - }); - wb.pipe(outStream); - - outStream.on('error', function (err) { - console.error(err); + writer.end('console.error(' + JSON.stringify(String(err)) + ');'); }); - outStream.on('exit', function () { - if (verbose && !didError) { - console.error(bytes + ' bytes written to ' + outfile - + ' (' + (time / 1000).toFixed(2) + ' seconds)' - ); - } + + writer.once('readable', function() { + var outStream = outpipe(outfile); + outStream.on('error', function (err) { + console.error(err); + }); + outStream.on('exit', function () { + if (verbose && !didError) { + console.error(bytes + ' bytes written to ' + outfile + + ' (' + (time / 1000).toFixed(2) + ' seconds)' + ); + } + }); + writer.pipe(outStream); }); }