grunt-browserify
grunt-browserify copied to clipboard
Half complete outputs after using factor-bundle
Similar to #330. That was closed due to node version however I believe that was a red herring, as I've noticed this now on
grunt-browserify
: 3.3.0 & 4.0.0
browserify
: 8.x+
factor-bundle
: 2.5.0
On node 0.10.40, 0.12.7 and iojs 3.0.0
I have been getting inconsistent results with factor-bundle
and grunt-browserify
and I believe the issue to be a race condition.
To reproduce:
git clone [email protected]:jmreidy/grunt-browserify.git
cd grunt-browserify
npm install
cd examples/factor-bundle
npm install
grunt
cat ./public/x.js
Expected: A fully formed bundle
Actual: Sometimes just require=
, sometimes a full line ending with an open brace, sometimes the actual bundle
Issue: When next()
is invoked then the plugin may still be producing output. When run via the browserify CLI, everything works as expected. When run via grunt-browserify
, next
seems to immediately interrupt the browserify process, when factor-bundle
might still be streaming its output.
Adding a simple timeout of 100ms to the above seems to fix it. I will investigate further and try to submit a PR.
Note: I would like to add a test, but will need to expand the test suite to actually output files to determine if the write successfully completed. I may not have time to expand the suite to actually monitor output.
Seems to be related to https://github.com/substack/factor-bundle/issues/61
So, the solution I have is non-trivial. It involves using a slightly modified fork of factor-bundle
which emits the output stream for each bundle, and then modifying grunt-browserify
to use a simple promise to emit with or without factor bundles.
https://github.com/jmreidy/grunt-browserify/compare/master...justinjmoses:master
I don't expect this to get merged in, though I will try to get my factor-bundle fork into that repo, which is a step forward.
Hi, I encountered the same problem & I think I have an easy fix by adding a timeout function to the post bundle callback. Just add this to your browserify config:
postBundleCB: (err, src, next) => { setTimeout(()=>{ next(err, src); }, 0); }
Grunt & Browserify beginner here.
I found this to be a problem, too, while trying to create the production build of this tutorial repo: https://github.com/mitchgavan/grunt-browserify-babel/blob/master/gruntfile.js
Like @shiftie, I had to add this to the production.options
settings, just below line 40.
production: {
..., // all other kinds of settings
options: {
browserifyOptions: { debug: false },
transform: [["babelify", { "presets": ["es2015"] }]],
plugin: [ ... ],
// @shiftie's fix here.
postBundleCB: (err, src, next) => { setTimeout(()=>{ next(err, src); }, 0); }
}
}