browser-require
Advanced tools
Comparing version 0.1.2 to 0.1.4
@@ -10,2 +10,3 @@ // 1. Read the top file | ||
, path = require('path') | ||
, EventEmitter = require('events').EventEmitter | ||
, cache = {} | ||
@@ -30,3 +31,5 @@ , dependencyPromise = require('dependency-promise') | ||
} | ||
function ScriptPromise (name, location, npmBase) { | ||
EventEmitter.call(this); | ||
this.name = name; | ||
@@ -43,5 +46,11 @@ if (npmBase) { | ||
this.location = location; | ||
this.on('error', this.onError); | ||
this.addListener('error', this.onError); | ||
} | ||
ScriptPromise.prototype.__proto__ = EventEmitter.prototype; | ||
for (var k in dependencyPromise) { | ||
ScriptPromise.prototype[k] = dependencyPromise[k]; | ||
} | ||
ScriptPromise.modules = {}; | ||
@@ -144,44 +153,62 @@ | ||
ScriptPromise.prototype = { | ||
onError: function (err) { | ||
throw new Error(err); | ||
}, | ||
load: function () { | ||
var self = this; | ||
fs.readFile(this.location, 'utf8', function (err, src) { | ||
if (err) return self.trigger('error', err); | ||
self.src = src; | ||
var depNames = self.depsFor(src); | ||
if (!depNames.length) { | ||
return self.trigger('loaded', true); | ||
} | ||
var deps = []; | ||
depNames.forEach( function (name) { | ||
ScriptPromise.from(name, self, function (err, script) { | ||
if (err) return self.trigger("error", err); | ||
if (!script.isTriggered('loaded')) script.load(); | ||
deps.push(script); | ||
if (deps.length === depNames.length) { | ||
self.dependsOn('loaded', deps); | ||
} | ||
ScriptPromise.prototype.onError = function (err) { | ||
throw err; | ||
}; | ||
ScriptPromise.prototype.load = function () { | ||
var self = this; | ||
fs.readFile(this.location, 'utf8', function (err, src) { | ||
if (err) return self.trigger('error', err); | ||
self.src = src; | ||
var depNames = self.depsFor(src); | ||
if (!depNames.length) | ||
return self.trigger('loaded', true); | ||
var deps = []; | ||
depNames.forEach( function (name) { | ||
ScriptPromise.from(name, self, function (err, script) { | ||
if (err) return self.trigger("error", err); | ||
if (!script.isTriggered('loaded')) script.load(); | ||
deps.push(script); | ||
// TODO | ||
script.addListener('reloaded', function (src) { | ||
}); | ||
if (deps.length === depNames.length) { | ||
self.dependsOn('loaded', deps); | ||
} | ||
}); | ||
}); | ||
}, | ||
depsFor: function (src) { | ||
var re = this.requireRegExp | ||
, match | ||
, deps = []; | ||
while (match = re.exec(src)) { | ||
deps.push(match[1]); | ||
} | ||
return deps; | ||
}, | ||
requireRegExp: /^[^*/]*\s*require\(['"]([^'"]+)['"]\)/gm | ||
}; | ||
}); | ||
for (var k in dependencyPromise) { | ||
ScriptPromise.prototype[k] = dependencyPromise[k]; | ||
// For --watch | ||
// fs.watchFile(this.location, function (curr, prev) { | ||
// if (curr.mtime.getTime() > prev.mtime.getTime()) { | ||
// self.reload(); | ||
// } else { | ||
// throw new Error("Times are weird"); | ||
// } | ||
// }); | ||
} | ||
ScriptPromise.prototype.depsFor = function (src) { | ||
var re = /^[^(?:\*|\/)]*\s*require\(['"]([^'"]+)['"]\)/gm | ||
, match | ||
, deps = []; | ||
while (match = re.exec(src)) { | ||
deps.push(match[1]); | ||
} | ||
return deps; | ||
}; | ||
ScriptPromise.prototype.reload = function (src) { | ||
var self = this; | ||
fs.readFile(this.location, 'utf8', function (err, src) { | ||
if (err) return self.trigger('error', err); | ||
self.src = src; | ||
// Check for dependency additions or removals | ||
var depNames = self.depsFor(src); | ||
// TODO | ||
// Notify anyone who depends on me | ||
self.trigger('reloaded', src); | ||
}); | ||
}; | ||
function wrapDeps (script) { | ||
@@ -188,0 +215,0 @@ var src = {} |
function browserRequire (path) { | ||
console.log(path); | ||
var mod = browserRequire.modules[path + '.js'] || | ||
@@ -4,0 +3,0 @@ browserRequire.modules[path + '/index.js']; |
{ | ||
"name": "browser-require", | ||
"description": "Use CommonJS and NPM modules from the browser", | ||
"version": "0.1.2", | ||
"version": "0.1.4", | ||
"homepage": "https://github.com/bnoguchi/browser-require", | ||
@@ -18,4 +18,6 @@ "repository": "https://github.com/bnoguchi/browser-require.git", | ||
}, | ||
"devDependencies": { | ||
"expresso": ">= 0.7.2" | ||
}, | ||
"dependencies": { | ||
"expresso": ">= 0.7.2", | ||
"dependency-promise": ">=0.2.0" | ||
@@ -22,0 +24,0 @@ }, |
@@ -6,7 +6,7 @@ require.paths.unshift('../../lib'); | ||
var exposeRequire = require('browser-require'); | ||
var app = connect.createServer( | ||
var app = connect( | ||
exposeRequire({ | ||
base: __dirname | ||
}), | ||
connect.staticProvider(__dirname) | ||
connect.static(__dirname) | ||
); | ||
@@ -13,0 +13,0 @@ app.listen(1234); |
Sorry, the diff of this file is not supported yet
87491
1
48
2470
1
6
- Removedexpresso@>= 0.7.2
- Removedexpresso@0.9.2(transitive)