Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smart multi bundles #312

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function Browserify (files) {
this._expose = {}; this._expose = {};
this._mapped = {}; this._mapped = {};
this._transforms = []; this._transforms = [];

var self = this;
this._ready = false;

this.once('_ready', function() {
self._ready = true;
});


[].concat(files).filter(Boolean).forEach(this.add.bind(this)); [].concat(files).filter(Boolean).forEach(this.add.bind(this));
} }
Expand All @@ -43,6 +50,27 @@ Browserify.prototype.add = function (file) {


Browserify.prototype.require = function (id, opts) { Browserify.prototype.require = function (id, opts) {
var self = this; var self = this;

if (id instanceof Browserify) {
self._pending ++;

var go = function() {
var d = mdeps(id.files);
d.pipe(through(function(row) {
self._external[row.id] = true;
})).once('end', function() {
if (--self._pending === 0) self.emit('_ready');
});
};

// need to capture all deps so we know what is already avail
if (!id._ready) {
return id.once('_ready', go);
}

return go();
}

if (opts === undefined) opts = { expose: id }; if (opts === undefined) opts = { expose: id };


self._pending ++; self._pending ++;
Expand Down Expand Up @@ -178,6 +206,10 @@ Browserify.prototype.deps = function (opts) {


if (self.exports[row.id]) row.exposed = self.exports[row.id]; if (self.exports[row.id]) row.exposed = self.exports[row.id];


if (self.expose_all) {
row.exposed = hash(row.id);
}

// skip adding this file if it is external // skip adding this file if it is external
if (self._external[row.id]) { if (self._external[row.id]) {
return; return;
Expand Down Expand Up @@ -211,6 +243,9 @@ Browserify.prototype.pack = function (debug) {
if (row.exposed) { if (row.exposed) {
ix = row.exposed; ix = row.exposed;
} }
else if (self.expose_all) {
ix = hash(row.id);
}
else { else {
ix = ids[row.id] !== undefined ? ids[row.id] : idIndex++; ix = ids[row.id] !== undefined ? ids[row.id] : idIndex++;
} }
Expand All @@ -230,6 +265,12 @@ Browserify.prototype.pack = function (debug) {
return acc; return acc;
} }


// if we expose all, just have all hashes available
if (self.expose_all) {
acc[key] = hash(file);
return acc;
}

if (self._expose[file]) { if (self._expose[file]) {
acc[key] = self._expose[file]; acc[key] = self._expose[file];
return acc; return acc;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
{ {
"name": "browserify", "name": "browserify",
"version": "2.6.0", "version": "2.6.0-dz0",
"description": "browser-side require() the node way", "description": "browser-side require() the node way",
"main": "index.js", "main": "index.js",
"bin": { "bin": {
Expand Down Expand Up @@ -35,7 +35,6 @@
"devDependencies": { "devDependencies": {
"tap": "~0.4.0", "tap": "~0.4.0",
"mkdirp": "~0.3.3", "mkdirp": "~0.3.3",

"backbone": "~0.9.2", "backbone": "~0.9.2",
"dnode": "~1.0.3", "dnode": "~1.0.3",
"seq": "0.3.3", "seq": "0.3.3",
Expand Down
42 changes: 42 additions & 0 deletions test/multi_bundle.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,3 +42,45 @@ test('multi bundle', function (t) {
}); });
}); });
}); });

test('multi bundle', function (t) {
t.plan(8);

var core = browserify();
core.expose_all = true;
core.require(__dirname + '/multi_bundle/a.js', { expose: true });

var app = browserify([__dirname + '/multi_bundle/c.js']);
// inform this bundle that b exists in another bundle
app.require(core);

core.bundle(function (err, src) {
var c = {
console: console,
t : t,
baton: {
times: 0
}
};

// loading core will cause no require to run
vm.runInNewContext(src, c);
t.equal(c.baton.times, 0);

// loading the app will require
app.bundle(function (err, src) {
vm.runInNewContext(src, c);

// b required for the first time
t.equal(c.baton.times, 1);

// running the file again
// because it is using the same b, no reloading
vm.runInNewContext(src, c);

// b should not have been required again
// because it was part of the core bundle
t.equal(c.baton.times, 1);
});
});
});
2 changes: 2 additions & 0 deletions test/multi_bundle/a.js
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,4 @@
var b = require('./b'); var b = require('./b');
t.equal(b, 'foo'); t.equal(b, 'foo');

module.exports = 'bar';
4 changes: 4 additions & 0 deletions test/multi_bundle/c.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
var b = require('./b');
var a = require('./a');
t.equal(b, 'foo');
t.equal(a, 'bar');