Permalink
Browse files

fixed lots of bugs

  • Loading branch information...
1 parent a6ac403 commit bb77c4b9d1a0fe353513979ba1970ea764a71367 @weepy committed Nov 29, 2011
Showing with 65 additions and 52 deletions.
  1. +62 −52 browser/brequire.js
  2. +3 −0 fix_chrome.sh
View
@@ -3,10 +3,13 @@
+function(global) {
// Sync Require
+ // setup global key in window
+ global.global = global
+
var require = function(p) {
var path = require.resolve(p)
var module = require.modules[path]
- if(!module) throw("couldn't find module for: " + p)
+ if(!module) return undefined
if(!module.exports) {
module.exports = {}
module.call(module.exports, module, module.exports, require.bind(path))
@@ -31,22 +34,28 @@
}
- function normalize(p, path) {
- var fullPath = path.split('/');
- fullPath.pop();
- var parts = p.split('/');
- for (var i=0; i < parts.length; i++) {
+ function dir(file) {
+ var parts = file.split('/');
+ parts.pop();
+ return parts.join("/")
+ }
+
+ require.relative = function(file, file2) {
+ var parts = (dir(file2) + "/" + file).split('/');
+ var ret = []
+
+ for (var i=0; i < parts.length; i++) {
var part = parts[i];
- if (part == '..') fullPath.pop();
- else if (part != '.') fullPath.push(part);
+ if (part == '..') ret.pop();
+ else if (i == 0 || part != '.') ret.push(part);
}
- return fullPath.join('/')
+ return ret.join("/")
}
require.bind = function(path) {
return function(p) {
if(!p.match(/^\./)) return require(p)
- return require(normalize(p, path));
+ return require(require.relative(p, path));
}
}
@@ -55,65 +64,64 @@
return require.modules[path] = mod;
}
+ require.script = function() {
+ for(var i=0; i<arguments.length; i++) {
+ var path = arguments[i]
+ if(!path.match(/\.js$/)) path += ".js"
+ document.write("<script src='" + path + "' type='text/javascript'></scr" + "ipt>")
+ }
+ }
+
// EXPORT
global.require = require
global.define = require.def = define
}(this)
-
+// Async support
!function(global) {
- // Adds in async support
-
+
// wrap the sync require module
var sync_require = require
- global.require = function(p, async, deps) {
- return async
- ? require.async(p, async, deps)
+ global.require = function(p) {
+ return arguments.length > 1
+ ? require.async.apply(global, arguments)
: sync_require(p)
}
for(var i in sync_require) require[i] = sync_require[i]
-
- function dir(file) {
- var parts = file.split('/');
- parts.pop();
- return parts.join("/")
- }
- function relative(file, dir) {
- var parts = (dir + "/" + file).split('/');
- var ret = []
+ require.async = function() {
+ var paths = Array.prototype.slice.call(arguments),
+ callback = typeof paths[paths.length-1] == "function" && paths.pop(),
+ deps = []
- for (var i=0; i < parts.length; i++) {
- var part = parts[i];
- if (part == '..') ret.pop();
- else if (i == 0 || part != '.') ret.push(part);
- }
-
- return ret.join("/")
- }
-
+ for(var i=0; i<paths.length; i++)
+ deps.push(paths[i])
- require.async = function(path, callback, deps) {
- deps = deps || []
- deps.unshift(path);
-
- (function run(path) {
+ function run(path) {
load(path, function(mod, new_deps) {
deps.splice(deps.indexOf(path), 1)
var dep
- while(dep = new_deps.shift()) {
- deps.unshift(relative(dep, dir(path)))
+
+ if(!deps.length && !new_deps.length) {
+ var args = []
+ for(var i=0; i<paths.length; i++) args.push(require(paths[i]))
+ return callback && callback.apply(global, args)
}
-
- if(!deps.length) return callback(require(path))
- for(var i=0; i < deps.length; i++) {
- run(deps[i])
+ while(dep = new_deps.shift()) {
+ var p = require.relative(dep, path)
+ deps.unshift(p)
+ run(p)
}
+
})
- })(path)
+ }
+
+
+ for(var i=0; i<paths.length; i++)
+ run(paths[i])
}
function load(path, callback) {
@@ -130,7 +138,7 @@
var loader = { callbacks: [callback] }
xhr(path, function(u, text) {
- var mod = require.eval(require.compile(path, text) + "//@ sourceURL=" + path)
+ var mod = require.eval(require.compile(path, text) + "//@ sourceURL=" + u)
var deps = extract_dependencies(text)
require.def(path, deps, mod)
for(var i=0; i<loader.callbacks.length; i++) {
@@ -141,27 +149,29 @@
load.loaders[path] = loader
return loader
}
-
+ // require.load = load
load.loaders = {}
function extract_dependencies(text) {
- var requires = text.match(/require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g)
+ var requires = text.match(/require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g) || []
- if(!requires) return []
for(var i=0; i< requires.length; i++) {
requires[i] = requires[i].replace(/^require\s*\(\s*["']/, "").replace(/["']\s*\)$/,"")
}
return requires
}
function xhr(url, callback) {
- console.info("loading " + url)
+ // console.info("loading " + url)
var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) callback(url, xhr.responseText)
+ if (xhr.readyState == 4) {
+ if(xhr.responseText.length == 0) console.error(url + " is zero length")
+ callback(url, xhr.responseText)
+ }
}
try {
xhr.send(null)
View
@@ -3,6 +3,9 @@
# replaces the Chrome executable with a script which calls the original executable with the -allow-file-access-from-files argument.
# pass the path to the Chrome executable as the first argument.
+# run like :
+# ./fix_chrome.sh /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
+
original_path="$1"
base="$(basename "$original_path")"
new_path="$(dirname "$original_path")/$base-bin"

0 comments on commit bb77c4b

Please sign in to comment.