diff --git a/index.js b/index.js index 81d2c17..ac9a7eb 100644 --- a/index.js +++ b/index.js @@ -20,10 +20,9 @@ var browserify = require('browserify') , _ = require('lodash') , minifyify = require('minifyify') , resrcify = require('resrcify') + , factorBundle = require('factor-bundle') , ctor -require('factor-bundle') - ctor = module.exports = function atomifyJs (opts, cb) { var outputPath , outputDir @@ -35,7 +34,6 @@ ctor = module.exports = function atomifyJs (opts, cb) { , assets , outputs , b - , w if (Array.isArray(opts)) opts = {entries: opts} if (typeof opts === 'string') opts = {entries: [opts]} @@ -105,20 +103,20 @@ ctor = module.exports = function atomifyJs (opts, cb) { emitter.emit('browserify', b) if (opts.watch) { - w = watchify(b) - emitter.emit('watchify', w) + b = watchify(b) + emitter.emit('watchify', b) } if (opts.watch) { - w.on('update', function onUpdate (ids) { + b.on('update', function onUpdate (ids) { ids.forEach(function eachId (id) { emitter.emit('changed', id) }) - w.bundle(cb) + rebundle() }) - w.on('time', function onTime (time) { + b.on('time', function onTime (time) { emitter.emit('bundle', time) }) } @@ -200,6 +198,9 @@ ctor = module.exports = function atomifyJs (opts, cb) { b.external(opts.external) } + return rebundle() + + function rebundle() { // if we've got the common option, we want to use factor bundle if (opts.common === true){ if (opts.entries.length < 2) { @@ -221,7 +222,7 @@ ctor = module.exports = function atomifyJs (opts, cb) { }) // setup factor bundle, pass in our streamable-buffers as the output source - b.plugin('factor-bundle', { + b.plugin(factorBundle, { o: _.values(outputs) }) @@ -251,6 +252,7 @@ ctor = module.exports = function atomifyJs (opts, cb) { } // if we don't need to use factor bundle, just browserify! else return b.bundle(cb) + } } ctor.emitter = emitter