Permalink
Browse files

bundling works in lib/wrap

  • Loading branch information...
1 parent a0e7ccd commit 07c23c1982479ea60f5eb299656918c3cfbfa64e @substack committed Jun 19, 2011
Showing with 40 additions and 24 deletions.
  1. +29 −17 lib/wrap.js
  2. +8 −5 test/bundle.js
  3. +2 −2 test/wrap.js
  4. +1 −0 wrappers/prelude.js
View
@@ -16,10 +16,11 @@ function Wrap (startFiles) {
this.hashes = {};
this.aliases = {};
- this.prepends = [];
+ this.prepends = [ wrappers.prelude ];
this.appends = []
this.require(startFiles || []);
+ this.require('path');
}
Wrap.prototype.prepend = function (src) {
@@ -30,27 +31,31 @@ Wrap.prototype.append = function (src) {
this.appends.push(src);
};
-Wrap.prototype.bundle = function (root) {
+Wrap.prototype.bundle = function () {
var files = this.files;
- var coredir = path.resolve(__dirname, '../builtins');
return []
.concat(this.prepends)
- .concat(Object.keys(files).map(function (file) {
- if (file.slice(0, root.length + 1) === root + '/') {
- var mfile = './' + file.slice(root.length + 1);
+ .concat(Object.keys(files).map(function (key) {
+ var file = files[key];
+ var root = file.root;
+ var name = key;
+ var body = file.body;
+
+ if (name.slice(0, root.length + 1) === root + '/') {
+ var mfile = './' + name.slice(root.length + 1);
}
- else if (file.match(/\/node_modules\/.+/)) {
+ else if (name.match(/\/node_modules\/.+/)) {
var mfile = './node_modules/'
- + file.match(/\/node_modules\/(.+)/)[1]
+ + name.match(/\/node_modules\/(.+)/)[1]
;
}
- else if (path.dirname(file) === coredir) {
- var mfile = path.basename(file);
+ else if (resolve.isCore(name)) {
+ var mfile = name;
}
else {
throw new Error('Can only load non-root modules'
- + ' in a node_modules for file: ' + file
+ + ' in a node_modules for file: ' + name
);
}
@@ -62,7 +67,7 @@ Wrap.prototype.bundle = function (root) {
return JSON.stringify(path.dirname(mfile))
})
.replace(/\$body/, function () {
- return files[file].toString().replace(/^#![^\n]*\n/, '');
+ return body.toString().replace(/^#![^\n]*\n/, '');
})
;
}))
@@ -77,21 +82,28 @@ Wrap.prototype.require = function (startFiles) {
if (!Array.isArray(startFiles)) startFiles = [ startFiles ];
startFiles.forEach(function (file) {
- walker(path.resolve(__dirname, file), __dirname);
+ var res = resolve.isCore(file)
+ ? file
+ : path.resolve(process.cwd(), file)
+ ;
+ var dir = path.dirname(res);
+ walker(res, dir, dir);
});
return this;
- function walker (mfile, basedir) {
+ function walker (mfile, basedir, root) {
if (resolve.isCore(mfile)) {
var file = path.resolve(__dirname, '../builtins/' + mfile + '.js');
+ var name = mfile;
if (!path.existsSync(file)) {
throw new Error('No wrapper for core module ' + mfile);
}
}
else {
try {
var file = resolve.sync(mfile, { basedir : basedir });
+ var name = file;
basedir = path.dirname(file);
}
catch (err) {
@@ -101,10 +113,10 @@ Wrap.prototype.require = function (startFiles) {
}
}
- if (files[file]) return;
+ if (files[name]) return;
var body = fs.readFileSync(file);
- files[file] = body;
+ files[name] = { root : root, body : body };
var required = detective.find(body);
@@ -122,6 +134,6 @@ Wrap.prototype.require = function (startFiles) {
}, {})
);
- keys.forEach(function (f) { walker(f, basedir) });
+ keys.forEach(function (f) { walker(f, basedir, root) });
}
};
View
@@ -1,20 +1,23 @@
var assert = require('assert');
-var browserify = require('browserify');
+var browserify = require('../lib/wrap');
var vm = require('vm');
exports.bundle = function () {
- var src = browserify.bundle({
- require : 'seq'
- });
+ var src = browserify(require.resolve('seq')).bundle();
+
assert.ok(typeof src === 'string');
assert.ok(src.length > 0);
var to = setTimeout(function () {
assert.fail('never ran');
}, 10000);
- var c = { setTimeout : setTimeout };
+ var c = {
+ setTimeout : setTimeout,
+ console : console
+ };
vm.runInNewContext(src, c);
+
c.require('seq')([1,2,3])
.parMap_(function (next, x) {
setTimeout(function () {
View
@@ -6,11 +6,11 @@ exports.wrap = function () {
var files = wrapper(__dirname + '/wrap/a.js').files;
assert.deepEqual(Object.keys(files).sort(), [
+ 'path', 'vm',
__dirname + '/wrap/a.js',
__dirname + '/wrap/node_modules/b/main.js',
__dirname + '/wrap/c.js',
__dirname + '/wrap/x.js',
- path.resolve(__dirname, '../builtins/vm.js'),
].sort());
};
@@ -21,12 +21,12 @@ exports.wrapArray = function () {
]).files;
assert.deepEqual(Object.keys(files).sort(), [
+ 'path', 'vm',
__dirname + '/wrap/a.js',
__dirname + '/wrap/node_modules/b/main.js',
__dirname + '/wrap/c.js',
__dirname + '/wrap/x.js',
__dirname + '/wrap/skipme.js',
__dirname + '/wrap/node_modules/skipmetoo/index.js',
- path.resolve(__dirname, '../builtins/vm.js'),
].sort());
};
View
@@ -10,6 +10,7 @@ require.paths = [];
require.modules = {};
require.resolve = function (file, cwd) {
+console.dir(require.modules);
var path = require.modules['path']();
var resolve = function (p) {
if (p === '.' || p === './') return '.';

0 comments on commit 07c23c1

Please sign in to comment.