Permalink
Browse files

AMD compatible

  • Loading branch information...
1 parent 1c39865 commit c9aba570d5f6be7d7daf04b571b1d4192fd3be17 @weepy committed Dec 10, 2011
Showing with 30 additions and 14 deletions.
  1. +23 −8 browser/require.async.js
  2. +7 −6 browser/require.js
View
@@ -19,6 +19,7 @@
function run(path) {
load(path, function(mod, new_deps) {
+
deps.splice(deps.indexOf(path), 1)
var dep
@@ -39,15 +40,19 @@
run(paths[i])
}
- require.compile = function(name, ext, text) {
- text = require.compilers[ext](text)
- return "define('" + name + "', [], function(module, exports, require) {\n" + text + "\n});"
+ function wrap(name, ext, text, deps) {
+ var deps2 = []
+ for(var i =0; i < deps.length; deps++) deps2[i] = "'" + deps[i] + "'"
+ return "define('" + name + "', [" + deps2.join(", ") + "], function(module, exports, require) {\n" + text + "\n});"
+ }
+
+ var compilers = {
+ ".js": function(text) { return text }
}
- require.compilers = {}
+
require.registerExtension = function(ext, fn) {
- require.compilers[ext] = fn
+ compilers[ext] = fn
}
- require.registerExtension(".js", function(text) { return text } )
function load(module_name, callback) {
var mod, l
@@ -69,14 +74,23 @@
var loader = { callbacks: [callback] }
+ console.log("loading", path)
+
xhr(path, function(u, text) {
- text = require.compile(path, ext, text)
- var mod = require.globalEval(text + "//@ sourceURL=" + u)
+
+ text = compilers[ext](text)
+
var deps = extract_dependencies(text)
+
+ text = wrap(path, ext, text, deps)
+
+ var mod = require.globalEval(text + "//@ sourceURL=" + u)
define(path, deps, mod)
+
for(var i=0; i<loader.callbacks.length; i++) {
loader.callbacks[i](mod, deps)
}
+
})
load.loaders[path] = loader
@@ -111,6 +125,7 @@
console.error("failed loading: " + url)
}
}
+
}(this)
require.globalEval = function(text) {
View
@@ -29,16 +29,16 @@ require.relative = function(file, file2) {
return parts.join("/")
}
- var parts = (dir(file2) + "/" + file).split('/');
+ var parts = (dir(file2) + "/" + file).split('/')
var ret = []
for (var i=0; i < parts.length; i++) {
- var part = parts[i];
+ var part = parts[i]
if (part == '..') {
- var last = ret.pop();
+ var last = ret.pop()
if(last == "." || !last) ret.push("..")
}
- else if (i == 0 || part != '.') ret.push(part);
+ else if (i == 0 || part != '.') ret.push(part)
}
return ret.join("/")
}
@@ -52,9 +52,10 @@ require.bind = function(path) {
}
function define(path, deps, mod) {
- mod.deps = deps
- return require.modules[path] = mod;
+ mod.dependencies = deps
+ return require.modules[path] = mod
}
+define.amd = {}
require.script = function() {
for(var i=0; i<arguments.length; i++) {

0 comments on commit c9aba57

Please sign in to comment.