Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

define -> require.define. removed race condition. now ignores comment…

…ed require's in dependencies
  • Loading branch information...
commit 789dce99896620e35e910d2ec27ae09a1e29ac35 1 parent 76b5797
weepy authored
8 README.md
Source Rendered
@@ -103,16 +103,16 @@ Notes
103 103 -----
104 104
105 105
106   -* require.async uses a regex to determine dependencies, which is solid with two caveats:
107   - * require's inside comments
108   - * dynamic calls to require (which will not be picked up)
  106 +* require.async uses a regex to determine dependencies, which is solid with one caveats:
  107 + * dynamic calls to require will not be picked up, e.g. <code>require(myvar)</code>
  108 +
  109 +* NB require's inside comments are not picked up
109 110
110 111 * require.async works seemlessly with require, so if a dependency is already available, require.async will return immediately with no xhr
111 112
112 113 * CoffeeScript lovers:
113 114 * if you want to be able to do require.async("./my.coffee") then just add the following line after coffeescript.js
114 115 * require.registerExtension('.coffee', CoffeeScript.compile)
115   - * note best to load coffeescript before require
116 116
117 117
118 118
107 browser/require.async.js
... ... @@ -1,4 +1,4 @@
1   -+function(global) {
  1 +!function(global) {
2 2 var require_sync = require
3 3 global.require = function(path) {
4 4 return arguments.length > 1
@@ -10,40 +10,48 @@
10 10 require.sync = require_sync
11 11
12 12 require.async = function() {
13   - var paths = Array.prototype.slice.call(arguments),
14   - callback = typeof paths[paths.length-1] == "function" && paths.pop(),
  13 + var modules = Array.prototype.slice.call(arguments),
  14 + callback = typeof modules[modules.length-1] == "function" && modules.pop(),
15 15 deps = []
16 16
17   - for(var i=0; i<paths.length; i++)
18   - deps.push(paths[i])
  17 + for(var i=0; i<modules.length; i++)
  18 + deps.push(modules[i])
19 19
20   - function run(path) {
21   - load(path, function(mod, new_deps) {
  20 + function run(module) {
22 21
23   - deps.splice(deps.indexOf(path), 1)
  22 + load(module, function(mod, new_deps) {
  23 + deps.splice(deps.indexOf(module), 1)
  24 +
24 25 var dep
25   -
  26 +
26 27 if(!deps.length && !new_deps.length) {
27 28 var args = []
28   - for(var i=0; i<paths.length; i++) args.push(require(paths[i]))
  29 + for(var i=0; i<modules.length; i++)
  30 + args.push(require(modules[i]))
  31 +
29 32 return callback && callback.apply(global, args)
  33 + // && setTimeout(function() { callback.apply(global, args) }, 0)
30 34 }
31 35
  36 + var to_run = []
32 37 while(dep = new_deps.shift()) {
33   - var p = require.relative(dep, path)
  38 + var p = require.relative(dep, module)
34 39 deps.unshift(p)
35   - run(p)
  40 + to_run.unshift(p)
36 41 }
  42 +
  43 + // run them after adding to avoid race condition
  44 + while(dep = to_run.shift()) run(dep)
37 45 })
38 46 }
39   - for(var i=0; i<paths.length; i++)
40   - run(paths[i])
  47 + for(var i=0; i<modules.length; i++)
  48 + run(modules[i])
41 49 }
42 50
43 51 function wrap(name, ext, text, deps) {
44 52 var deps2 = []
45 53 for(var i =0; i < deps.length; deps++) deps2[i] = "'" + deps[i] + "'"
46   - return "define('" + name + "', [" + deps2.join(", ") + "], function(module, exports, require) {\n" + text + "\n});"
  54 + return "require.define('" + name + "', [" + deps2.join(", ") + "], function(module, exports, require) {\n" + text + "\n});"
47 55 }
48 56
49 57 var compilers = {
@@ -54,9 +62,16 @@
54 62 compilers[ext] = fn
55 63 }
56 64
  65 + require.sysPath = function(path) {
  66 + return "./" + path
  67 + }
  68 +
57 69 function load(module_name, callback) {
58 70 var mod, l
59 71 var ext = module_name.match(/\.[a-zA-Z0-9_]*$/)
  72 +
  73 + // if(module_name.match(/^[^.]/)) module_name = require.sysPath(module_name)
  74 +
60 75 if(ext) {
61 76 var path = module_name
62 77 ext = ext[0]
@@ -65,32 +80,39 @@
65 80 ext = ".js"
66 81 }
67 82
68   - if(mod = require.resolve(path)) { return callback(mod, []) }
  83 + if(!path.match(/^\./)) path = require.sysPath(path)
  84 +
  85 +
69 86
  87 + if(mod = require.resolve(path)) { return callback(mod, []) }
70 88 if(l = load.loaders[path]) {
71 89 l.callbacks.push(callback)
72 90 return l
73 91 }
74 92
75 93 var loader = { callbacks: [callback] }
76   -
77   - console.log("loading", path)
78 94
79   - xhr(path, function(u, text) {
80   -
81   - text = compilers[ext](text)
  95 + XHR(path, function(u, text) {
  96 + try {
  97 + text = compilers[ext](text)
  98 + }
  99 + catch(e) {
  100 + console.error("Error during compile: ", e)
  101 + throw e
  102 + }
82 103
83 104 var deps = extract_dependencies(text)
  105 + text = wrap(module_name, ext, text, deps)
84 106
85   - text = wrap(path, ext, text, deps)
86   -
87   - var mod = require.globalEval(text + "//@ sourceURL=" + u)
88   - define(path, deps, mod)
  107 + try {
  108 + var mod = require.globalEval(text + "//@ sourceURL=" + u)
  109 + } catch(e) {
  110 + throw "Syntax Error in " + path + ": " + e.toString()
  111 + }
89 112
90   - for(var i=0; i<loader.callbacks.length; i++) {
  113 + require.define(path, deps, mod)
  114 + for(var i=0; i<loader.callbacks.length; i++)
91 115 loader.callbacks[i](mod, deps)
92   - }
93   -
94 116 })
95 117
96 118 load.loaders[path] = loader
@@ -98,24 +120,39 @@
98 120 }
99 121 load.loaders = {}
100 122
101   - function extract_dependencies(text) {
102   - var requires = text.match(/require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g) || []
  123 + var regex = {
  124 + all: /require\s*\('\s*([^'])*'\s*\)|require\s*\("\s*([^"])*"\s*\)/g,
  125 + start: /require\s*\(\s*["']/,
  126 + end: /["']\s*\)$/,
  127 + comments: /\/\*.+?\*\/|\/\/.*(?=[\n\r])/g // or /(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm
  128 + }
103 129
  130 + function extract_dependencies(text) {
  131 + text = text.replace(regex.comments, '')
  132 + var requires = text.match(regex.all) || []
104 133 for(var i=0; i< requires.length; i++) {
105   - requires[i] = requires[i].replace(/^require\s*\(\s*["']/, "").replace(/["']\s*\)$/,"")
  134 + requires[i] = requires[i].replace(regex.start, "").replace(regex.end, "")
106 135 }
107 136 return requires
108 137 }
109 138
110   - function xhr(url, callback) {
111   - // console.info("loading " + url)
  139 + function XHR(url, callback) {
  140 + console.info("loading " + url)
112 141 var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
113 142 xhr.open('GET', url, true);
114 143 if ('overrideMimeType' in xhr) xhr.overrideMimeType('text/plain');
115 144 xhr.onreadystatechange = function() {
116 145 if (xhr.readyState == 4) {
117   - if(xhr.responseText.length == 0) console.error(url + " is zero length")
118   - callback(url, xhr.responseText)
  146 + if(xhr.responseText.length == 0) {
  147 + if(url.match(/\.js$/)) {
  148 + if(!url.match(/index\.js$/))
  149 + XHR(url.replace(/\.js$/, "/index.js"), callback)
  150 + }
  151 + console.error(url + " is zero length")
  152 + } else {
  153 + callback(url, xhr.responseText)
  154 + }
  155 +
119 156 }
120 157 }
121 158 try {
9 browser/require.js
@@ -12,9 +12,10 @@ function require(p) {
12 12
13 13 require.modules = {}
14 14
  15 +
15 16 require.resolve = function(path) {
16 17 if(require.modules[path]) return path
17   -
  18 +
18 19 if(!path.match(/\.js$/)) {
19 20 if(require.modules[path+".js"]) return path + ".js"
20 21 if(require.modules[path+"/index.js"]) return path + "/index.js"
@@ -23,6 +24,8 @@ require.resolve = function(path) {
23 24 }
24 25
25 26 require.relative = function(file, file2) {
  27 + if(!file.match(/^\./)) return file
  28 +
26 29 function dir(file) {
27 30 var parts = file.split('/');
28 31 parts.pop();
@@ -51,11 +54,11 @@ require.bind = function(path) {
51 54 }
52 55 }
53 56
54   -function define(path, deps, mod) {
  57 +require.define = function(path, deps, mod) {
55 58 mod.dependencies = deps
56 59 return require.modules[path] = mod
57 60 }
58   -define.amd = {}
  61 +
59 62
60 63 require.script = function() {
61 64 for(var i=0; i<arguments.length; i++) {
2  lib/brequire.js
@@ -158,7 +158,7 @@ function mkDirTree(_path) {
158 158
159 159
160 160 function wrap(file, text) {
161   - return "define('" + file + "', [], function(module, exports, require) {\n" + text + "\n}); // end module: "+ file +"\n"
  161 + return "require.define('" + file + "', [], function(module, exports, require) {\n" + text + "\n}); // end module: "+ file +"\n"
162 162 }
163 163
164 164 module.exports = compile

0 comments on commit 789dce9

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