Created
May 12, 2011 20:49
-
-
Save unscriptable/969416 to your computer and use it in GitHub Desktop.
AMD-compatible CommonJS Module/1.1 that can also be loaded in a script tag
This file contains hidden or 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
// This boilerplate would allow a module to run in node.js or an | |
// AMD-enabled browser environment. On node.js, the syntax is: | |
// var Color = require("./Color").Color, | |
// color = new Color("#BADA55"); | |
// In the browser, the syntax is: | |
// var color = new Color("#BADA55"); // window.Color is implied | |
(function (define) { | |
define(function () { | |
function Color(){} | |
return Color; | |
}); | |
}( | |
// if define is not available, assume we're running in node.js | |
this.define || (function (exports, moduleId) { | |
return function (defFunc) { | |
exports[moduleId] = defFunc(); | |
} | |
})(this, 'Color') | |
)); |
This file contains hidden or 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
// This boilerplate would allow a module to run in node.js or an | |
// AMD-enabled browser environment. On node.js, the syntax is: | |
// var Color = require("./Color").Color, | |
// color = new Color("#BADA55"); | |
// In the browser, the syntax is: | |
// var color = new Color("#BADA55"); // window.Color is implied | |
(function (define) { | |
define(["exports"], function (exports) { | |
exports.Color = function () {}; | |
}); | |
}( | |
// provide define if it's not available | |
this.define || (function (exports, moduleId) { | |
return function (ignoreMe, defFunc) { | |
defFunc(exports); | |
} | |
})(exports, 'myGlobalModule') | |
)); |
This file contains hidden or 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
// This boilerplate would allow a module to run in node.js or an | |
// AMD-enabled browser environment. On node.js, the syntax is: | |
// var Color = new require("./Color")("#BADA55"); | |
// In the browser, the syntax is: | |
// var color = new Color("#BADA55"); // window.Color is implied | |
(function (define) { | |
define(function () { | |
function Color(){} | |
return Color; | |
}); | |
}( | |
// if define is not available, assume we're running in node.js | |
this.define || (function (module, moduleId) { | |
return function (defFunc) { | |
module.exports = defFunc(); | |
} | |
})(module, 'Color') | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All of the above examples had some issue for me. Either I had to settle for the differing server/browser construction or I got
ReferenceError
s (forexports
) or something else.However, I found https://github.com/kof/expose.js, included it at the top of my IIFE before my module definition and just added
expose('ModuleName', ModuleDefinition);
and everything works as I wanted.I cannot verify that it is AMD compliant but as a developer who had a working "module" and wanted it to work the same way in the server and the browser, ideally without dictating any API design choices, this is ideal.
I've updated https://gist.github.com/969908 to include
expose.js
.