jsdf / browserify-incremental

incremental rebuild for browserify
175 stars 13 forks source link

end is never triggered, cache file is never saved #23

Open dantman opened 8 years ago

dantman commented 8 years ago

For some reason the cache file is never saved why I try using this.

Versions:

    "babelify": "^7.2.0",
    "browserify": "^12.0.1",
    "browserify-incremental": "^3.0.1",

Node: v0.12.0 (I think)

I'm using this inside of a cordova js hook.

Relevant parts of the code:

var browserify = require('browserify-incremental'),
    b = browserify({cacheFile: path.join(ctx.opts.projectRoot, 'browserify-cache.json')})
        .transform(require('babelify'))
        .require(filePath, {expose: 'app'});

return Q.ninvoke(b, 'bundle')
    .then(function(buf) {
    });

I dug into the code and fiddled around with some console logging.

In this code bundle gets called but end is never called.

  b.on('bundle', function(bundleStream) {
    // store on completion
    bundleStream.on('end', function() {
      storeCache(b, cacheFile);
    });
  });

In this code, if I kill the proxyEvent and pipe and just set outputStream = bundleStream the end event is emitted and the cache file is saved.

      var bundleStream = prevBundle.call(b, cb);
      proxyEvent(bundleStream, outputStream, 'file');
      proxyEvent(bundleStream, outputStream, 'package');
      proxyEvent(bundleStream, outputStream, 'transform');
      proxyEvent(bundleStream, outputStream, 'error');
      bundleStream.pipe(outputStream);

So for some reason the end event isn't being proxied/piped. I tried various things like adding a proxyEvent for it or calling .end() on outputStream in an .on handler for bundleStream. But none of them seemed to work, so I can't figure out what exactly is wrong.

Note that I'm not sure my change would be a proper fix anyways. The cache file was saved and read but I never got any notable speed-up to my compilation of babel+react so I don't know if the caching wasn't working or I need a different browserify cache module.

Macil commented 8 years ago

I can't reproduce this issue in Browserify-incremental 3.0.1 or the current version.

package.json

{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "main": "foo.js",
  "dependencies": {
    "babelify": "^7.2.0",
    "browserify": "^13.0.0",
    "browserify-incremental": "^3.1.1",
    "q": "^1.4.1"
  },
  "license": "ISC"
}

test.js

var path = require('path');
var Q = require('q');

var browserify = require('browserify-incremental'),
  b = browserify({cacheFile: path.join(__dirname, 'browserify-cache.json')})
    .transform(require('babelify'))
    .require('./foo.js');

return Q.ninvoke(b, 'bundle')
  .then(function(buf) {
    console.log('success', buf);
  })
  .catch(function(err) {
    console.error('error', err);
  });

foo.js

console.log('foo');
$ node test.js 
success <Buffer 72 65 71 75 69 72 65 3d 28 66 75 6e 63 74 69 6f 6e 20 65 28 74 2c 6e 2c 72 29 7b 66 75 6e 63 74 69 6f 6e 20 73 28 6f 2c 75 29 7b 69 66 28 21 6e 5b 6f ... >
$ cat browserify-cache.json 
{"modules":{"/private/tmp/foo/foo.js":{"file":"/private/tmp/foo/foo.js","source":"console.log('foo');\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxRQUFRLEdBQVIsQ0FBWSxLQUFaIiwiZmlsZSI6ImZvby5qcyIsInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKCdmb28nKTtcbiJdfQ==","deps":{}}},"mtimes":{"/private/tmp/foo/foo.js":1459458561000},"dependentFiles":{}}

Running test.js logs the success statement, a healthy cache file is made, and any log statements I add to browserify-cache-api's 'end' event callback get called.