Skip to content

Instantly share code, notes, and snippets.

@hallettj
Created May 27, 2013 23:57
Show Gist options
  • Save hallettj/5659633 to your computer and use it in GitHub Desktop.
Save hallettj/5659633 to your computer and use it in GitHub Desktop.
Defining a module in a way that will work in several different environments. This is from Acorn <https://github.com/marijnh/acorn.git>, a JavaScript parser.
(function(mod) {
if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
mod(self.acorn || (self.acorn = {})); // Plain browser env
})(function(exports) {
"use strict";
exports.version = "0.2.0";
/* ... */
});
@hallettj
Copy link
Author

The AMD line could be written a little differently to avoid relying on the "exports" dependency:

if (typeof define == "function" && define.amd) return define("acorn", function() {
    var exp = {}; mod(exp); return exp;
}); // AMD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment