Skip to content

Commit

Permalink
added browserify-incremental support
Browse files Browse the repository at this point in the history
  • Loading branch information
taoeffect authored and DirtyHairy committed Aug 18, 2017
1 parent afa393c commit 3d18513
Show file tree
Hide file tree
Showing 5 changed files with 3,554 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
### 5.2.0
- New: Added `cacheFile` option for [browserify-incremental](https://github.com/jsdf/browserify-incremental) support
- Update dependencies and added package-lock.json (for npm 5+)

### 5.1.0
- Update dependencies. Browserify 14.0

Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -175,6 +175,14 @@ If true, invoke [watchify](https://github.com/substack/watchify) instead of brow

For watchify to work properly, you have to keep the process running. The option `keepAlive` can help you do that, or you can use another `grunt-watch` task.

#### cacheFile

Type: String

Set to location of [browserify-incremental](https://github.com/jsdf/browserify-incremental) cache file and enable incremental builds via `browserify-incremental`. Mutually exclusive with `watchify`.

Note that unlike `watchify`, this setting is fully compatible with `grunt-contrib-watch` and should be used together with it.

#### keepAlive
Type: Boolean
If true and if `watch` above is true, keep the Grunt process alive (simulates grunt-watch functionality).
Expand Down
33 changes: 27 additions & 6 deletions lib/runner.js
Expand Up @@ -2,6 +2,7 @@ var _ = require('lodash');
var path = require('path');
var resolve = require('resolve');
var glob = require('glob');
var browserifyInc = require('browserify-incremental');

module.exports = GruntBrowserifyRunner;

Expand All @@ -13,6 +14,10 @@ function GruntBrowserifyRunner(options) {
this.firstBuild = true;
}

// persist the created browserify instances between calls from grunt-contrib-watch
// this is specifically for `options.cacheFile`, `browserify-incremental`
var destinations = {}

GruntBrowserifyRunner.prototype = _.create(GruntBrowserifyRunner.prototype, {
run: function (files, destination, options, next) {
var self = this;
Expand All @@ -24,13 +29,29 @@ GruntBrowserifyRunner.prototype = _.create(GruntBrowserifyRunner.prototype, {
// watchify options
var wOpts = options.watchifyOptions || {};

// Watchify requires specific arguments
if(options.watch) {
//determine watchify or browserify-incremental or browserify
var b
if (options.watch) {
bOpts = _.extend({ cache: {}, packageCache: {} }, bOpts);
}
b = this.watchify(this.browserify(bOpts), wOpts)
} else if (options.cacheFile) {
if (destinations[destination]) {
return destinations[destination].call(this, next)
}
bOpts = Object.assign({}, bOpts, browserifyInc.args)
b = this.browserify(bOpts)
browserifyInc(b, {cacheFile: options.cacheFile})

//determine watchify or browserify
var b = options.watch ? this.watchify(this.browserify(bOpts), wOpts) : this.browserify(bOpts);
b.on('log', function (msg) {
self.logger.log.ok(msg.cyan)
})

destinations[destination] = function (done) {
doBundle(b, options, this.onBundleComplete(destination, options, done))
}
} else {
b = this.browserify(bOpts)
}

b.on('error', function (err) {
self.logger.fail.warn(err);
Expand Down Expand Up @@ -217,4 +238,4 @@ function requireFiles(b, requiredFiles) {
}
b.require(filePath, opts);
});
}
}

0 comments on commit 3d18513

Please sign in to comment.