arian / partition-bundle

A browserify plugin to partition your modules in different bundles
114 stars 18 forks source link

Can't seem to get it working with the Browserify API #1

Closed anthonyhastings closed 9 years ago

anthonyhastings commented 9 years ago

I'm having some big problems trying to get this working when using the Browserify API rather than the command-line. Do you have any documentation on how this should be approached?

There's a few problems I'm experiencing, but the main one is that I need to use vinyl-source-stream in conjunction with the bundle or else it will error and fail:

var browserify = require('browserify'),
    source = require('vinyl-source-stream');

// Creating a browserify instance / stream.
var bundleStream = browserify({ debug: false });

// NOTE: First row has to be "main" bundle which will include loadjs to load other bundles.
//       This means the main bundle must always be the starting bundle loaded onto the page.
bundleStream.plugin('partition-bundle', {
    map: {
        "bundle-main.js": ["./js/src/main"],
        "bundle-maps.js": ["./js/src/maps/index"],
        "bundle-contact.js": ["./js/src/contact/index"]
    },
    output: "./js/"
});

bundleStream
    .bundle()
    .pipe(source())
    .on('end', function() {
        // TODO: WHY DOES THIS NEVER FIRE?
        console.log('Task Completed.');
    });

If I leave out the .pipe(source()) call and the .on('end', function() { ... }) call, the process errors internally pointing at browserify:

node_modules/browserify/node_modules/labeled-stream-splicer/node_modules/stream-splicer/index.js:45
        self._streams[0].end();
                         ^
TypeError: Cannot call method 'end' of undefined

However, this error disappears if I don't use your plugin.

arian commented 9 years ago

It's probably because of partition-bundle creates a writeStream for each output file, and writes to that, so it might not really work terribly great with vinyl-source-stream... iirc it writes to the output files directly, and kind of ignores the original stream.

The ideal solution is that you would stream an object for each file that contains the filename, maybe some other meta-data and the resulting source... but I think that was rather tricky to do as browserify plugin..

arian commented 9 years ago

I don't know, but if I do:

browserify()
  .plugin(partition, {
    map: __dirname + '/fixtures/package-with-browser/bundle.json',
    output: dist
  })
  .bundle()
  .on('data', function() {})
  .on('end', function() { console.log('this does fire'); });

it works, but without the on('data', function() {}) it doesn't. I seems to be the same without the partion-bundle plugin.