browserify / factor-bundle

factor browser-pack bundles into common shared bundles
Other
402 stars 27 forks source link

API enhancements #28

Closed jgoz closed 9 years ago

jgoz commented 10 years ago

This improves the plugin API use case for factor-bundle.

ghost commented 10 years ago
jgoz commented 10 years ago

@substack Awesome, thanks for the feedback. I'll clean it up.

jgoz commented 10 years ago

We already have ways of exposing the underlying streams.

What did you have in mind here? It's easy to extract streams from opts.o before they get passed in, but when you have functions that return streams, there isn't really a way to collect them after the stream event.

ghost commented 10 years ago

Could we just have a new event that emits an event emitter that emits each stream? That would fit in well with the existing api.

terinjokes commented 10 years ago

I see we've been duplicating work over the past day. :)

What I was considering doing was exposing a pipeline instance for each stream, so that someone downstream could make changes to wrapping and packing. Does this help?

jgoz commented 10 years ago

@terinjokes That's a really good idea. I can give you push access to my fork if you want.

terinjokes commented 10 years ago

This works for me.

jgoz commented 10 years ago

@terinjokes I rebased & overwrote the branch to get rid of the factored() stuff. Your turn! :)

terinjokes commented 10 years ago

Gracias, getting on that.

terinjokes commented 10 years ago

@jgoz Sorry about the delay. I've been busy with my real work stuff :frowning:. I'm going to try to get to this tomorrow (Saturday).

jgoz commented 10 years ago

@terinjokes No problem at all! This isn't blocking my real work stuff - just a nice to have. If you need help let me know!

jgoz commented 10 years ago

@terinjokes Do you think it would be worth merging this as-is and splitting the pipeline-per-stream feature into a separate request? That way at least it works with watchify, even if the API isn't ideal.

terinjokes commented 10 years ago

I'm just hesitant on having four different possible settings for a single thing. Overloaded APIs are also confusing APIs. Let me think about it tonight, and I'll either do a follow up comment or a merge tomorrow.

jgoz commented 10 years ago

I'm just hesitant on having four different possible settings for a single thing.

You mean for opts.o? That's a fair point. If you removed the "function that returns a string/stream" option, it would still work with the watchify command and for direct file outputs. You just wouldn't be able to reuse stream outputs, but that could be addressed in the pipeline work.

terinjokes commented 10 years ago

Function returning a stream is what I had locally to resolve the watchify issue, I'm not sure of that usefulness in the current state of a function returning a string is (I can't think of any new information in this case).

That said, I primarily wanted to think about how the pipeline version would work, and jot it down either here or another ticket, before moving forward here. Would hate for this to shoot that in the foot.

jgoz commented 10 years ago

I'm not sure of that usefulness in the current state of a function returning a string is

It's probably not useful at all - really just a consequence of accepting stream/string/function as possible values and not restricting the function output.

That said, I primarily wanted to think about how the pipeline version would work, and jot it down either here or another ticket, before moving forward here. Would hate for this to shoot that in the foot.

Sounds good.

jgoz commented 10 years ago

What about this:

Every output stream emits a factor.pipeline event via the browserify instance with 2 parameters: stream id and pipeline. The pipeline has 2 phases: pack and wrap (not sure if anything else really applies).

This would allow you to listen for the event and hook the pipeline as required. You also wouldn't have to pass anything for o/outputs.

var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
// etc.

function wrap(id) {
  return source(path.basename(id))
    .pipe(buffer())
    .pipe(sourcemap.init({ loadMaps: true }))
    .pipe(uglify())
    .pipe(sourcemap.write())
    .pipe(gulp.dest('out'));
}

b.on('factor.pipeline', function (id, pipeline) {
  pipeline.get('wrap').push(wrap(id));
});
b.plugin('factor-bundle');
b.bundle().pipe(wrap('common.js'));

Advantages of this approach:

  1. The pipelines would get recreated with every reset, emitting the factor.pipeline event and re-hooking the streams each time, so it's watchify-compatible.
  2. Minimal pollution of browserify instance - just using it as a namespaced event relay.
  3. Could be kept compatible with opts.o - if specified, it would just hook into the pipeline internally and pipe to the stream/file.
jgoz commented 10 years ago

@terinjokes Have a look at jgoz/factor-bundle#1 and jgoz/factor-bundle#2 — both create pipelines for the output bundles but they're hooked differently (1 uses events, 2 uses a callback param). I prefer the callback myself.

Any thoughts on either of these approaches?

terinjokes commented 10 years ago

I prefer #1, trivial example aside.

jgoz commented 10 years ago

OK. Is it worth tacking that on to this PR and cleaning up as needed?

terinjokes commented 10 years ago

@jgoz Probably worth it. Might be good to squash them into two commits though.

jgoz commented 10 years ago

Will do

jgoz commented 10 years ago

@terinjokes Squashed + added docs.

sevein commented 9 years ago

@jgoz and @terinjokes, this is looking great! Any chance to merge this at some point? Thanks.

terinjokes commented 9 years ago

Thanks. I think it looks good too, but I want to take another look when I'm not sick. 😷

sevein commented 9 years ago

@jgoz, I tried both branches and they both worked great for me. I don't have strong preferences, so up to you guys! What I've noticed is that when I switched to your branches I had to start using absolute paths in my entries list because relative paths stopped working. Namely, they are completely ignored, though a error is emitted when they can't be found. Weird!

jgoz commented 9 years ago

@sevein

What I've noticed is that when I switched to your branches I had to start using absolute paths in my entries list because relative paths stopped working

That's probably because my branches haven't been rebased since 20e00a2869d was committed.

sevein commented 9 years ago

oh @jgoz, I didn't notice that!

terinjokes commented 9 years ago

@jgoz I'm going to rebase this and merge, assuming you have no other changes.

jgoz commented 9 years ago

@terinjokes No other changes from me!

sevein commented 9 years ago

Sweet!