Created
May 24, 2011 14:45
-
-
Save pmuellr/988840 to your computer and use it in GitHub Desktop.
a module for node which installs an AMD-compliant define() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//---------------------------------------------------------------------------- | |
// Copyright (c) 2011 Patrick Mueller | |
// | |
// The MIT License - see: http://www.opensource.org/licenses/mit-license.php | |
//---------------------------------------------------------------------------- | |
//---------------------------------------------------------------------------- | |
// adds an AMD-compliant define() function to the global object | |
// | |
// specifically for node, dependent on: | |
// module.constructor.prototype._compile() | |
// module.exports == the exports object | |
//---------------------------------------------------------------------------- | |
//---------------------------------------------------------------------------- | |
// module-scoped variables | |
//---------------------------------------------------------------------------- | |
var thisModule = module | |
var moduleCompile = module.constructor.prototype._compile | |
//---------------------------------------------------------------------------- | |
// replacement for module compiler, just sets currentModule for define() | |
//---------------------------------------------------------------------------- | |
module.constructor.prototype._compile = function compile4amd(content, filename){ | |
var line = "var define = require('" + thisModule.id + "').getDefine(require, exports, module); " | |
content = line + content | |
return moduleCompile.call(this, content, filename) | |
} | |
//---------------------------------------------------------------------------- | |
// the define function | |
//---------------------------------------------------------------------------- | |
exports.getDefine = function getDefine(require, exports, module) { | |
function defineImpl(/* moduleId, deps, factory */) { | |
var moduleId | |
var deps | |
var factory | |
if (arguments.length == 1) { | |
moduleId = null | |
deps = null | |
factory = arguments[0] | |
} | |
else if (arguments.length == 2) { | |
if (typeof arguments[0] == "string") { | |
moduleId = arguments[0] | |
deps = null | |
factory = arguments[1] | |
} | |
else { | |
moduleId = null | |
deps = arguments[0] | |
factory = arguments[1] | |
} | |
} | |
else if (arguments.length == 3) { | |
moduleId = arguments[0] | |
deps = arguments[1] | |
factory = arguments[2] | |
} | |
else { | |
throw new Error("invalid arguments") | |
} | |
if (!deps) deps = [] | |
if (deps.length == 0) deps = ["require", "exports", "module"] | |
var depModules = [] | |
for (var i=0; i<deps.length; i++) { | |
var dep = deps[i] | |
if (dep == "require") depModules.push(require) | |
else if (dep == "exports") depModules.push(exports) | |
else if (dep == "module") depModules.push(module) | |
else depModules.push(require(dep)) | |
} | |
if (typeof factory != "function") { | |
module.exports = factory | |
} | |
else { | |
var result = factory.apply(null, depModules) | |
if (result) { | |
module.exports = result | |
} | |
} | |
} | |
defineImpl.amd = true | |
return defineImpl | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment