From bec04df0d4c04b6eddcffec38e600c396ae9b3e2 Mon Sep 17 00:00:00 2001 From: John Gozde Date: Sat, 6 Sep 2014 11:33:31 -0600 Subject: [PATCH] Expose pipeline for output bundles on event. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - browserify instance emits ‘factor.pipeline’ event when the pipeline is created - pipeline uses labeled-stream-splicer and has ‘pack’ and ‘wrap’ stages --- index.js | 34 +++++++++++++++++++++++----------- package.json | 1 + readme.markdown | 11 +++++++++++ test/plugin.js | 11 ++++++----- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 4eb4c22..4462787 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ var fs = require('fs'); var pack = require('browser-pack'); var xtend = require('xtend'); var defined = require('defined'); +var splicer = require('labeled-stream-splicer'); module.exports = function f (b, opts) { if (!opts) opts = {}; @@ -24,15 +25,20 @@ module.exports = function f (b, opts) { var needRecords = !files.length; - opts.outputs = opts.outputs || opts.o; + opts.outputs = defined(opts.outputs, opts.o, {}); opts.objectMode = true; opts.raw = true; opts.rmap = {}; + var packOpts = xtend(b._options, { + raw: true, + hasExports: true + }); + b.on('reset', addHooks); addHooks(); - function addHooks() { + function addHooks () { b.pipeline.get('record').push(through.obj(function(row, enc, next) { if (needRecords) { files.push(row.file); @@ -40,24 +46,30 @@ module.exports = function f (b, opts) { next(null, row); }, function(next) { var cwd = defined(opts.basedir, b._options.basedir, process.cwd()); - var fileMap = files.reduce(function (acc, x, ix) { - acc[path.resolve(cwd, x)] = opts.outputs[ix]; + 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; }, {}); // Force browser-pack to wrap the common bundle b._bpack.hasExports = true; - var packOpts = xtend(b._options, { - raw: true, - hasExports: true + + Object.keys(pipelines).forEach(function (id) { + b.emit('factor.pipeline', id, pipelines[id]); }); var s = createStream(files, opts); s.on('stream', function (bundle) { - var output = fileMap[bundle.file]; - var ws = isStream(output) ? output : fs.createWriteStream(output); - - bundle.pipe(pack(packOpts)).pipe(ws); + bundle.pipe(pipelines[bundle.file]); }); b.pipeline.get('pack').unshift(s); diff --git a/package.json b/package.json index 90eca35..b906101 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "defined": "0.0.0", "deps-topo-sort": "~0.2.1", "inherits": "^2.0.1", + "labeled-stream-splicer": "^1.0.0", "minimist": "~0.2.0", "nub": "0.0.0", "reversepoint": "~0.2.0", diff --git a/readme.markdown b/readme.markdown index b8cbc4a..cf3a61d 100644 --- a/readme.markdown +++ b/readme.markdown @@ -164,6 +164,17 @@ default). The entry file name is available as `stream.file`. +## b.on('factor.pipeline', function (file, pipeline) {}) + +Emits the full path to the entry file (`file`) and a [labeled-stream-splicer](https://npmjs.org/package/labeled-stream-splicer) (`pipeline`) for each entry file with these labels: + +* `'pack'` - [browser-pack](https://npmjs.org/package/browser-pack) +* `'wrap'` - apply final wrapping + +You can call `pipeline.get` with a label name to get a handle on a stream pipeline that you can `push()`, `unshift()`, or `splice()` to insert your own transform streams. + +Event handlers must be attached *before* calling `b.plugin`. + # install With [npm](https://npmjs.org) do: diff --git a/test/plugin.js b/test/plugin.js index aa37d2a..0f99edd 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -72,12 +72,13 @@ test('browserify plugin multiple bundle calls', function(t) { var b = browserify(files); var sources = {}; - b.plugin(factor, { - o: [ - function() { return concat(function(data) { sources.x = data }); }, - function() { return concat(function(data) { sources.y = data }); } - ] + 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);