factor-bundle
Advanced tools
Comparing version 2.2.4 to 2.3.0
79
index.js
@@ -15,2 +15,3 @@ var Transform = require('stream').Transform; | ||
var defined = require('defined'); | ||
var splicer = require('labeled-stream-splicer'); | ||
@@ -28,2 +29,3 @@ module.exports = function f (b, opts) { | ||
opts.outputs = defined(opts.outputs, opts.o, {}); | ||
opts.objectMode = true; | ||
@@ -33,40 +35,57 @@ opts.raw = true; | ||
var cwd = defined(opts.basedir, b._options.basedir, process.cwd()); | ||
b.pipeline.get('record').push(through.obj(function(row, enc, next) { | ||
if (needRecords) { | ||
files.push(row.file); | ||
} | ||
next(null, row); | ||
}, function(next) { | ||
var fileMap = files.reduce(function (acc, x, ix) { | ||
acc[path.resolve(cwd, x)] = opts.o[ix]; | ||
return acc; | ||
}, {}); | ||
// Force browser-pack to wrap the common bundle | ||
b._bpack.hasExports = true; | ||
var packOpts = xtend(b._options, { | ||
raw: true, | ||
hasExports: true | ||
var cwd = defined(opts.basedir, b._options.basedir, process.cwd()), | ||
packOpts = xtend(b._options, { | ||
raw: true, | ||
hasExports: true | ||
}); | ||
var s = createStream(files, opts); | ||
s.on('stream', function (bundle) { | ||
var output = fileMap[bundle.file]; | ||
var ws = isStream(output) ? output : fs.createWriteStream(output); | ||
b.on('reset', addHooks); | ||
addHooks(); | ||
bundle.pipe(pack(packOpts)).pipe(ws); | ||
}); | ||
function addHooks () { | ||
b.pipeline.get('record').push(through.obj(function(row, enc, next) { | ||
if (needRecords) { | ||
files.push(row.file); | ||
} | ||
next(null, row); | ||
}, function(next) { | ||
var pipelines = files.reduce(function (acc, x, ix) { | ||
var pipeline = splicer.obj([ | ||
'pack', [ pack(packOpts) ], | ||
'wrap', [] | ||
]); | ||
var output = opts.outputs[ix]; | ||
if (output) { | ||
var ws = isStream(output) ? output : fs.createWriteStream(output); | ||
pipeline.push(ws); | ||
} | ||
acc[path.resolve(cwd, x)] = pipeline; | ||
return acc; | ||
}, {}); | ||
b.pipeline.get('pack').unshift(s); | ||
// Force browser-pack to wrap the common bundle | ||
b._bpack.hasExports = true; | ||
next(); | ||
})); | ||
Object.keys(pipelines).forEach(function (id) { | ||
b.emit('factor.pipeline', id, pipelines[id]); | ||
}); | ||
var s = createStream(files, opts); | ||
s.on('stream', function (bundle) { | ||
bundle.pipe(pipelines[bundle.file]); | ||
}); | ||
b.pipeline.get('label').push(through.obj(function(row, enc, next) { | ||
opts.rmap[row.id] = path.resolve(cwd, row.file); | ||
next(null, row); | ||
})); | ||
b.pipeline.get('pack').unshift(s); | ||
if (needRecords) files = []; | ||
next(); | ||
})); | ||
b.pipeline.get('label').push(through.obj(function(row, enc, next) { | ||
opts.rmap[row.id] = path.resolve(cwd, row.file); | ||
next(null, row); | ||
})); | ||
} | ||
return b; | ||
@@ -73,0 +92,0 @@ |
{ | ||
"name": "factor-bundle", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "factor browser-pack bundles into common shared bundles", | ||
@@ -13,2 +13,3 @@ "main": "index.js", | ||
"inherits": "^2.0.1", | ||
"labeled-stream-splicer": "^1.0.0", | ||
"minimist": "~0.2.0", | ||
@@ -15,0 +16,0 @@ "nub": "0.0.0", |
@@ -69,1 +69,35 @@ var test = require('tape'); | ||
}); | ||
test('browserify plugin multiple bundle calls', function(t) { | ||
t.plan(4); | ||
var b = browserify(files); | ||
var sources = {}; | ||
b.on('factor.pipeline', function(id, pipeline) { | ||
pipeline.pipe(concat(function(data) { | ||
if (/x\.js$/.test(id)) sources.x = data; | ||
else sources.y = data; | ||
})); | ||
}); | ||
b.plugin(factor); | ||
b.bundle().pipe(concat(function(data) { | ||
checkBundle(data); | ||
b.bundle().pipe(concat(checkBundle)); | ||
})); | ||
function checkBundle(data) { | ||
var common = data.toString('utf8'); | ||
var x = sources.x.toString('utf8'); | ||
var y = sources.y.toString('utf8'); | ||
vm.runInNewContext(common + x, { console: { log: function (msg) { | ||
t.equal(msg, 55500); | ||
} } }); | ||
vm.runInNewContext(common + y, { console: { log: function (msg) { | ||
t.equal(msg, 333); | ||
} } }); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
38676
959
204
12
+ Addedindexof@0.0.1(transitive)
+ Addedlabeled-stream-splicer@1.0.2(transitive)
+ Addedreadable-wrap@1.0.0(transitive)
+ Addedstream-splicer@1.3.2(transitive)
+ Addedthrough2@1.1.1(transitive)