Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

smart multi bundles #312

Closed
wants to merge 3 commits into from

3 participants

Roman Shtylman Esa-Matti Suuronen James Halliday
Roman Shtylman

Creating this pull request so we can discuss the best approach for both API surface and feature implementation. See the added test if it is not clear from my comments about what the final bundle output benefits are.

.require(bundle) allows for the capture of all files which this bundle
might contain. Previously, just the file which you did
.require({external: true}) was the one not bundled. This is even more
efficient when using external bundles.

defunctzombie added some commits
Roman Shtylman defunctzombie smart multi bundles
.require(bundle) allows for the capture of all files which this bundle
might contain. Previously, just the file which you did
.require({external: true}) was the one not bundled. This is even more
efficient when using external bundles.
9516a94
Roman Shtylman defunctzombie record when a bundle is already ready
needed to avoid waiting for a ready event if a bundle is already ready
50fef84
Roman Shtylman defunctzombie version bump fe3cd0d
Esa-Matti Suuronen

This might be somewhat related to #361

James Halliday
Owner

This PR is pretty out of date but we got exposeAll in a different PR.

James Halliday substack closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2013
  1. Roman Shtylman

    smart multi bundles

    defunctzombie authored
    .require(bundle) allows for the capture of all files which this bundle
    might contain. Previously, just the file which you did
    .require({external: true}) was the one not bundled. This is even more
    efficient when using external bundles.
  2. Roman Shtylman

    record when a bundle is already ready

    defunctzombie authored
    needed to avoid waiting for a ready event if a bundle is already ready
  3. Roman Shtylman

    version bump

    defunctzombie authored
This page is out of date. Refresh to see the latest.
41 index.js
View
@@ -32,6 +32,13 @@ function Browserify (files) {
this._expose = {};
this._mapped = {};
this._transforms = [];
+
+ var self = this;
+ this._ready = false;
+
+ this.once('_ready', function() {
+ self._ready = true;
+ });
[].concat(files).filter(Boolean).forEach(this.add.bind(this));
}
@@ -43,6 +50,27 @@ Browserify.prototype.add = function (file) {
Browserify.prototype.require = function (id, opts) {
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 };
self._pending ++;
@@ -178,6 +206,10 @@ Browserify.prototype.deps = function (opts) {
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
if (self._external[row.id]) {
return;
@@ -211,6 +243,9 @@ Browserify.prototype.pack = function (debug) {
if (row.exposed) {
ix = row.exposed;
}
+ else if (self.expose_all) {
+ ix = hash(row.id);
+ }
else {
ix = ids[row.id] !== undefined ? ids[row.id] : idIndex++;
}
@@ -230,6 +265,12 @@ Browserify.prototype.pack = function (debug) {
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]) {
acc[key] = self._expose[file];
return acc;
3  package.json
View
@@ -1,6 +1,6 @@
{
"name": "browserify",
- "version": "2.6.0",
+ "version": "2.6.0-dz0",
"description": "browser-side require() the node way",
"main": "index.js",
"bin": {
@@ -35,7 +35,6 @@
"devDependencies": {
"tap": "~0.4.0",
"mkdirp": "~0.3.3",
-
"backbone": "~0.9.2",
"dnode": "~1.0.3",
"seq": "0.3.3",
42 test/multi_bundle.js
View
@@ -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  test/multi_bundle/a.js
View
@@ -1,2 +1,4 @@
var b = require('./b');
t.equal(b, 'foo');
+
+module.exports = 'bar';
4 test/multi_bundle/c.js
View
@@ -0,0 +1,4 @@
+var b = require('./b');
+var a = require('./a');
+t.equal(b, 'foo');
+t.equal(a, 'bar');
Something went wrong with that request. Please try again.