Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

split into a sync module and an async addin

  • Loading branch information...
commit e4e8fa5205cdb12ce35670d66495bbc52e4b7c51 1 parent 20408b9
weepy authored December 02, 2011
BIN  .DS_Store
Binary file not shown
198  browser/brequire.js
... ...
@@ -0,0 +1,198 @@
  1
+// Brequire - CommonJS support for the browser
  2
+
  3
++function(global) {
  4
+  // Sync Require  
  5
+  
  6
+  // setup global key in window
  7
+  global.global = global 
  8
+    
  9
+  global.require = function(p) {
  10
+    
  11
+    var path = require.resolve(p)
  12
+    var module = require.modules[path]
  13
+    if(!module) return undefined
  14
+    if(!module.exports) {
  15
+      module.exports = {}
  16
+      module.call(module.exports, module, module.exports, require.bind(path))
  17
+    }
  18
+    return module.exports
  19
+  }
  20
+
  21
+  require.modules = {}
  22
+
  23
+  require.resolve = function(path) {
  24
+    if(require.modules[path]) return path
  25
+    
  26
+    if(!path.match(/\.js$/)) {
  27
+      if(require.modules[path+".js"]) return path + ".js"
  28
+      if(require.modules[path+"/index.js"]) return path + "/index.js"
  29
+      if(require.modules[path+"/index"]) return path + "/index"    
  30
+    }
  31
+  }
  32
+
  33
+  require.compile = function(name, ext, text) {
  34
+    text = require.compilers[ext](text)
  35
+    return "define('" + name + "', [], function(module, exports, require) {\n" + text + "\n});"
  36
+  }
  37
+  require.compilers = {}
  38
+  require.registerExtension = function(ext, fn) {
  39
+    require.compilers[ext] = fn    
  40
+  }
  41
+  require.registerExtension(".js", function(text) { return text } )
  42
+
  43
+  function dir(file) {
  44
+    var parts = file.split('/');
  45
+    parts.pop();
  46
+    return parts.join("/")
  47
+  }
  48
+
  49
+  require.relative = function(file, file2) {
  50
+    var parts = (dir(file2) + "/" + file).split('/');
  51
+    var ret = []
  52
+
  53
+    for (var i=0; i < parts.length; i++) {        
  54
+      var part = parts[i];
  55
+      if (part == '..') ret.pop();
  56
+      else if (i == 0 || part != '.') ret.push(part);
  57
+    }
  58
+    return ret.join("/")
  59
+  }
  60
+
  61
+  require.bind = function(path) {
  62
+    return function() {
  63
+      var args = Array.prototype.slice.call(arguments)
  64
+      if(args[0].match(/^\./)) args[0] = require.relative(args[0], path)
  65
+      return require.apply(this, args)
  66
+    }
  67
+  }
  68
+
  69
+
  70
+  global.define = function(path, deps, mod) {
  71
+    mod.deps = deps
  72
+    return require.modules[path] = mod;
  73
+  }
  74
+
  75
+  require.script = function() {
  76
+    for(var i=0; i<arguments.length; i++) {
  77
+      var path = arguments[i]
  78
+      if(!path.match(/\.js$/)) path += ".js"
  79
+      document.write("<script src='" + path + "' type='text/javascript'></scr" + "ipt>")
  80
+    }
  81
+  }
  82
+
  83
+}(this)
  84
+
  85
++function(global) {
  86
+
  87
+  var require_sync = require
  88
+  global.require = function(path) {
  89
+    return arguments.length > 1
  90
+      ? require.async.apply(global, arguments)
  91
+      : require.sync(path)
  92
+  }
  93
+  for(var i in require_sync) require[i] = require_sync[i]
  94
+
  95
+  require.sync = require_sync
  96
+  
  97
+  require.async = function() {
  98
+    var paths = Array.prototype.slice.call(arguments),
  99
+        callback = typeof paths[paths.length-1] == "function" && paths.pop(),
  100
+        deps = []
  101
+    
  102
+    for(var i=0; i<paths.length; i++) 
  103
+      deps.push(paths[i])
  104
+
  105
+    function run(path) {
  106
+      load(path, function(mod, new_deps) {
  107
+        deps.splice(deps.indexOf(path), 1)
  108
+        var dep
  109
+
  110
+        if(!deps.length && !new_deps.length) {
  111
+          var args = []
  112
+          for(var i=0; i<paths.length; i++) args.push(require(paths[i]))
  113
+          return callback && callback.apply(global, args)
  114
+        }
  115
+
  116
+        while(dep = new_deps.shift()) {
  117
+          var p = require.relative(dep, path)
  118
+          deps.unshift(p)
  119
+          run(p)
  120
+        }
  121
+        
  122
+      })
  123
+    }
  124
+
  125
+    for(var i=0; i<paths.length; i++) 
  126
+      run(paths[i])
  127
+  }
  128
+
  129
+  function load(module_name, callback) {
  130
+    var mod, l
  131
+    var ext = module_name.match(/\.[a-zA-Z0-9_]*$/)
  132
+    if(ext) {
  133
+      var path = module_name  
  134
+      ext = ext[0]
  135
+    } else {
  136
+      var path = module_name + ".js"
  137
+      ext = ".js"
  138
+    }
  139
+
  140
+    if(mod = require.resolve(path)) { return callback(mod, []) }
  141
+
  142
+    if(l = load.loaders[path]) {
  143
+      l.callbacks.push(callback)
  144
+      return l
  145
+    }
  146
+
  147
+    var loader = { callbacks: [callback] }
  148
+
  149
+    xhr(path, function(u, text) {
  150
+      var mod = require.globalEval(require.compile(path, ext, text) + "//@ sourceURL=" + u)
  151
+      var deps = extract_dependencies(text)
  152
+      define(path, deps, mod)
  153
+      for(var i=0; i<loader.callbacks.length; i++) {
  154
+        loader.callbacks[i](mod, deps)
  155
+      }
  156
+    })
  157
+
  158
+    load.loaders[path] = loader
  159
+    return loader
  160
+  }
  161
+  // require.load = load
  162
+  load.loaders = {}
  163
+
  164
+
  165
+  function extract_dependencies(text) {
  166
+    var requires = text.match(/require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g) || []
  167
+
  168
+    for(var i=0; i< requires.length; i++) {
  169
+      requires[i] = requires[i].replace(/^require\s*\(\s*["']/, "").replace(/["']\s*\)$/,"")
  170
+    }
  171
+    return requires
  172
+  }
  173
+
  174
+  function xhr(url, callback) {
  175
+    // console.info("loading " + url)
  176
+    var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
  177
+    xhr.open('GET', url, true);
  178
+    if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
  179
+    xhr.onreadystatechange = function() {
  180
+      if (xhr.readyState == 4) {
  181
+        if(xhr.responseText.length == 0) console.error(url + " is zero length")
  182
+        callback(url, xhr.responseText)
  183
+      }
  184
+    }
  185
+    try { 
  186
+      xhr.send(null)
  187
+    }
  188
+    catch(e) {
  189
+      console.error("failed loading: " + url)
  190
+    }
  191
+  }
  192
+}(this)
  193
+
  194
+require.globalEval = function(text) { 
  195
+  return (window.execScript || function(text) {
  196
+    return window["eval"].call(window, text)
  197
+  })(text)
  198
+}
217  browser/brequire.js.old
... ...
@@ -0,0 +1,217 @@
  1
+// Brequire - CommonJS support for the browser
  2
+
  3
+// SYNC MODULE
  4
++function(global) {
  5
+  global.global = global 
  6
+    
  7
+<<<<<<< HEAD
  8
+  global.require = function(p) {
  9
+=======
  10
+  var require = function(p) {
  11
+    if(arguments.length > 1) return require.async.apply(global, arguments) 
  12
+
  13
+>>>>>>> 72972b8ce35a113d0380704daa4110587685bb12
  14
+    var path = require.resolve(p)
  15
+    var module = require.modules[path]
  16
+    if(!module) return undefined
  17
+    if(!module.exports) {
  18
+      module.exports = {}
  19
+      module.call(module.exports, module, module.exports, require.bind(path))
  20
+    }
  21
+    return module.exports
  22
+  }
  23
+
  24
+  require.modules = {}
  25
+
  26
+  require.resolve = function(path) {
  27
+    if(require.modules[path]) return path
  28
+    
  29
+    if(!path.match(/\.js$/)) {
  30
+      if(require.modules[path+".js"]) return path + ".js"
  31
+      if(require.modules[path+"/index.js"]) return path + "/index.js"
  32
+      if(require.modules[path+"/index"]) return path + "/index"    
  33
+    }
  34
+  }
  35
+
  36
+  require.compile = function(name, ext, text) {
  37
+    text = require.compilers[ext](text)
  38
+    return "define('" + name + "', [], function(module, exports, require) {\n" + text + "\n});"
  39
+  }
  40
+  require.compilers = {}
  41
+  require.registerExtension = function(ext, fn) {
  42
+    require.compilers[ext] = fn    
  43
+  }
  44
+  require.registerExtension(".js", function(text) { return text } )
  45
+
  46
+  function dir(file) {
  47
+    var parts = file.split('/');
  48
+    parts.pop();
  49
+    return parts.join("/")
  50
+  }
  51
+
  52
+  require.relative = function(file, file2) {
  53
+    var parts = (dir(file2) + "/" + file).split('/');
  54
+    var ret = []
  55
+
  56
+    for (var i=0; i < parts.length; i++) {        
  57
+      var part = parts[i];
  58
+      if (part == '..') ret.pop();
  59
+      else if (i == 0 || part != '.') ret.push(part);
  60
+    }
  61
+    return ret.join("/")
  62
+  }
  63
+
  64
+  require.bind = function(path) {
  65
+    return function() {
  66
+      var args = Array.prototype.slice.call(arguments)
  67
+      if(args[0].match(/^\./)) args[0] = require.relative(args[0], path)
  68
+      return require.apply(this, args)
  69
+    }
  70
+  }
  71
+
  72
+<<<<<<< HEAD
  73
+  global.define = function(path, deps, mod) {
  74
+=======
  75
+
  76
+  function define(path, deps, mod) {
  77
+>>>>>>> 72972b8ce35a113d0380704daa4110587685bb12
  78
+    mod.deps = deps
  79
+    return require.modules[path] = mod;
  80
+  }
  81
+
  82
+  require.script = function() {
  83
+    for(var i=0; i<arguments.length; i++) {
  84
+      var path = arguments[i]
  85
+      if(!path.match(/\.js$/)) path += ".js"
  86
+      document.write("<script src='" + path + "' type='text/javascript'></scr" + "ipt>")
  87
+    }
  88
+  }
  89
+
  90
+<<<<<<< HEAD
  91
+}(this)
  92
+
  93
+
  94
+// ASYNC MODULE
  95
++function(global) {
  96
+
  97
+  var require_sync = require
  98
+  global.require = function(path) {
  99
+    return arguments.length > 1
  100
+      ? require.async.apply(global, arguments)
  101
+      : require.sync(path)
  102
+  }
  103
+  for(var i in require_sync) require[i] = require_sync[i]
  104
+
  105
+  require.sync = require_sync
  106
+=======
  107
+  // EXPORT 
  108
+  global.require = require
  109
+  global.define = require.def = define
  110
+>>>>>>> 72972b8ce35a113d0380704daa4110587685bb12
  111
+
  112
+  require.async = function() {
  113
+    var paths = Array.prototype.slice.call(arguments),
  114
+        callback = typeof paths[paths.length-1] == "function" && paths.pop(),
  115
+        deps = []
  116
+    
  117
+    for(var i=0; i<paths.length; i++) 
  118
+      deps.push(paths[i])
  119
+
  120
+    function run(path) {
  121
+      load(path, function(mod, new_deps) {
  122
+        deps.splice(deps.indexOf(path), 1)
  123
+        var dep
  124
+
  125
+        if(!deps.length && !new_deps.length) {
  126
+          var args = []
  127
+          for(var i=0; i<paths.length; i++) args.push(require.sync(paths[i]))
  128
+          return callback && callback.apply(global, args)
  129
+        }
  130
+
  131
+        while(dep = new_deps.shift()) {
  132
+          var p = require.relative(dep, path)
  133
+          deps.unshift(p)
  134
+          run(p)
  135
+        }
  136
+        
  137
+      })
  138
+    }
  139
+
  140
+    for(var i=0; i<paths.length; i++) 
  141
+      run(paths[i])
  142
+  }
  143
+
  144
+  function load(module_name, callback) {
  145
+    var mod, l
  146
+    var ext = module_name.match(/\.[a-zA-Z0-9_]*$/)
  147
+    if(ext) {
  148
+      var path = module_name  
  149
+      ext = ext[0]
  150
+    } else {
  151
+      var path = module_name + ".js"
  152
+      ext = ".js"
  153
+    }
  154
+
  155
+    if(mod = require.resolve(path)) { return callback(mod, []) }
  156
+
  157
+    if(l = load.loaders[path]) {
  158
+      l.callbacks.push(callback)
  159
+      return l
  160
+    }
  161
+
  162
+    var loader = { callbacks: [callback] }
  163
+
  164
+    xhr(path, function(u, text) {
  165
+      var mod = require.eval(require.compile(path, ext, text) + "//@ sourceURL=" + u)
  166
+      var deps = extract_dependencies(text)
  167
+      define(path, deps, mod)
  168
+      for(var i=0; i<loader.callbacks.length; i++) {
  169
+        loader.callbacks[i](mod, deps)
  170
+      }
  171
+    })
  172
+
  173
+    load.loaders[path] = loader
  174
+    return loader
  175
+  }
  176
+
  177
+  load.loaders = {}
  178
+
  179
+
  180
+  function extract_dependencies(text) {
  181
+    var requires = text.match(/require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g) || []
  182
+
  183
+    for(var i=0; i< requires.length; i++) {
  184
+      requires[i] = requires[i].replace(/^require\s*\(\s*["']/, "").replace(/["']\s*\)$/,"")
  185
+    }
  186
+    return requires
  187
+  }
  188
+
  189
+  function xhr(url, callback) {
  190
+    // console.info("loading " + url)
  191
+    var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
  192
+    xhr.open('GET', url, true);
  193
+    if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
  194
+    xhr.onreadystatechange = function() {
  195
+      if (xhr.readyState == 4) {
  196
+        if(xhr.responseText.length == 0) 
  197
+          console.error(url + " is zero length")
  198
+        callback(url, xhr.responseText)
  199
+      }
  200
+    }
  201
+    try { 
  202
+      xhr.send(null)
  203
+    }
  204
+    catch(e) {
  205
+      console.error("failed loading: " + url)
  206
+    }
  207
+  }
  208
+}(this)
  209
+
  210
+require.eval = function(text) { 
  211
+  try {
  212
+    return eval(text)
  213
+  } catch(e) {
  214
+    console.error("Failed eval: " + e.toString())
  215
+    console.info(text)    
  216
+  }
  217
+}

0 notes on commit e4e8fa5

Please sign in to comment.
Something went wrong with that request. Please try again.