[refactor] browser compile function to replace with `require('./error')' with 'require('./browser/error')'

This commit is contained in:
Jake Luer 2012-03-18 17:42:03 -04:00
parent 30e59bcb99
commit 2e8a50a90f

View file

@ -5,16 +5,43 @@ var args = process.argv.slice(2)
, pending = args.length , pending = args.length
, files = {}; , files = {};
var ignores = [
'error.js'
]
args.forEach(function(file){ args.forEach(function(file){
var mod = file.replace('lib/', ''); var mod = file.replace('lib/', '');
if (~ignores.indexOf(mod)) return --pending;
fs.readFile(file, 'utf8', function(err, js){ fs.readFile(file, 'utf8', function(err, js){
if (err) throw err; if (err) throw err;
console.log(' \033[90mcompile : \033[0m\033[36m%s\033[0m', file); console.log(' \033[90mcompile : \033[0m\033[36m%s\033[0m', file);
files[file] = js; files[file] = parse(js);
--pending || compile(); --pending || compile();
}); });
}); });
function parse (js) {
return parseInheritance(parseRequires(js));
}
/**
* Parse __proto__.
* @attribute https://github.com/visionmedia/mocha/blob/master/support/compile.js
*/
function parseInheritance (js) {
return js
.replace(/^ *(\w+)\.prototype\.__proto__ * = *(\w+)\.prototype *;?/gm, function(_, child, parent){
return child + '.prototype = new ' + parent + ';\n'
+ child + '.prototype.constructor = '+ child + ';\n';
});
}
function parseRequires (js) {
return js
.replace(/require\('.\/error'\)/g, "require('./browser/error')");
}
function compile() { function compile() {
var buf = ''; var buf = '';
@ -27,6 +54,7 @@ function compile() {
args.forEach(function(file){ args.forEach(function(file){
var js = files[file]; var js = files[file];
if (!js) return;
file = file.replace('lib/', ''); file = file.replace('lib/', '');
buf += '\nrequire.register("' + file + '", function(module, exports, require){\n'; buf += '\nrequire.register("' + file + '", function(module, exports, require){\n';
buf += js; buf += js;
@ -109,4 +137,4 @@ var browser = {
register: function(path, fn){ register: function(path, fn){
require.modules[path] = fn; require.modules[path] = fn;
} }
}; };