From d1e704bd40346cf3b7596d2792b97ee0a655095e Mon Sep 17 00:00:00 2001 From: Marcos Caceres Date: Mon, 15 Aug 2016 18:49:29 +1000 Subject: [PATCH] feat(lib): add AMD export support (closes #48) --- lib/webidl2.js | 24 +++++++++++++++--------- lib/writer.js | 23 ++++++++++++++--------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/webidl2.js b/lib/webidl2.js index 7e19a78e..d0a41714 100644 --- a/lib/webidl2.js +++ b/lib/webidl2.js @@ -1012,15 +1012,21 @@ return res; }; - var inNode = typeof module !== "undefined" && module.exports - , obj = { - parse: function (str, opt) { - if (!opt) opt = {}; - var tokens = tokenise(str); - return parse(tokens, opt); - } + var obj = { + parse: function(str, opt) { + if (!opt) opt = {}; + var tokens = tokenise(str); + return parse(tokens, opt); + } }; - if (inNode) module.exports = obj; - else self.WebIDL2 = obj; + if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { + module.exports = obj; + } else if (typeof define === 'function' && define.amd) { + define([], function(){ + return obj; + }); + } else { + (self || window).WebIDL2 = obj; + } }()); diff --git a/lib/writer.js b/lib/writer.js index 2be566f2..c4f2ccbd 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -268,15 +268,20 @@ }; - var inNode = typeof module !== "undefined" && module.exports - , obj = { - write: function (ast, opt) { - if (!opt) opt = {}; - return write(ast, opt); - } + var obj = { + write: function(ast, opt) { + if (!opt) opt = {}; + return write(ast, opt); + } }; - if (inNode) module.exports = obj; - else window.WebIDL2Writer = obj; - + if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { + module.exports = obj; + } else if (typeof define === 'function' && define.amd) { + define([], function() { + return obj; + }); + } else { + (self || window).WebIDL2Writer = obj; + } }());