From e4c8c3b7e4b2f0b9ecdc639c95e353241800a94b Mon Sep 17 00:00:00 2001 From: eggyal Date: Fri, 29 May 2015 10:23:14 +0100 Subject: [PATCH 1/2] Ensures factor bundle correctly matches entries to output streams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The documentation for [`_.values(object)`](https://lodash.com/docs#values) gives an example: > _.values(new Foo); > // → [1, 2] (iteration order is not guaranteed) Since "iteration order is not guaranteed", the array of streams passed to factor-bundle may not be in the same order as the corresponding files in `entries`. Factor bundle [depends on this correlation](https://github.com/substack/factor-bundle/blob/master/index.js#L73) when building its pipeline. This commit ensures that there is never a mismatch. --- index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6c54b53..fa2bcd5 100644 --- a/index.js +++ b/index.js @@ -208,20 +208,20 @@ ctor = module.exports = function atomifyJs (opts, cb) { } // for each entry, we're going to create a stream-able buffer to put the factored bundle into - outputs = {} + outputs = [] opts.entries.forEach(function createOutputs (entry) { - outputs[path.basename(entry).replace(path.extname(entry), '')] = new streamBuffer.WritableStreamBuffer({ + outputs.push(new streamBuffer.WritableStreamBuffer({ // these values are arbitrary, but we don't want to require huge buffers // start as 1 kilobytes. initialSize: 1 * 1024 // grow by 1 kilobytes each time buffer overflows. , incrementAmount: 1 * 1024 - }) + })) }) // setup factor bundle, pass in our streamable-buffers as the output source b.plugin(factorBundle, { - o: _.values(outputs) + o: outputs }) // we need to wrap the callback to output an object with all the bundles @@ -232,15 +232,17 @@ ctor = module.exports = function atomifyJs (opts, cb) { if (err && hasCallback) return void cb(err) // turn the stream-able buffers into plain buffers - out = _.mapValues(outputs, function convertStreamToBuffer (stream, entryName) { + out = opts.entries.reduce(function(acc, entryName, ix) { + var stream = outputs[ix] var entryBuffer = stream.getContents() // for those using the streaming interface, emit an event with the entry // do this here so that we don't have to itterate through the outputs twice emitter.emit('entry', entryBuffer, entryName) - return entryBuffer - }) + acc[path.basename(entryName).replace(path.extname(entryName), '')] = entryBuffer + return acc + }, {}) // add in the common bundle out.common = common From 6fb8ed84f1672afe2f901182159ec6027d3dc57f Mon Sep 17 00:00:00 2001 From: eggyal Date: Fri, 29 May 2015 10:26:16 +0100 Subject: [PATCH 2/2] Emit abbreviated entry name --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fa2bcd5..7025043 100644 --- a/index.js +++ b/index.js @@ -232,15 +232,16 @@ ctor = module.exports = function atomifyJs (opts, cb) { if (err && hasCallback) return void cb(err) // turn the stream-able buffers into plain buffers - out = opts.entries.reduce(function(acc, entryName, ix) { + out = opts.entries.reduce(function(acc, entry, ix) { var stream = outputs[ix] var entryBuffer = stream.getContents() + var entryName = path.basename(entry).replace(path.extname(entry), '') // for those using the streaming interface, emit an event with the entry // do this here so that we don't have to itterate through the outputs twice emitter.emit('entry', entryBuffer, entryName) - acc[path.basename(entryName).replace(path.extname(entryName), '')] = entryBuffer + acc[entryName] = entryBuffer return acc }, {})