From 76be1f8eee184c104d4d328ca93c4002e42b6d72 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Sun, 17 Mar 2013 22:56:49 +0100 Subject: [PATCH] Make Acorn work with AMD modules (Hopefully) --- acorn.js | 8 ++++++-- acorn_loose.js | 10 +++++++--- index.html | 8 ++++++-- util/walk.js | 8 ++++++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/acorn.js b/acorn.js index b2a513d52a..fb7b734aa0 100644 --- a/acorn.js +++ b/acorn.js @@ -20,7 +20,11 @@ // [dammit]: acorn_loose.js // [walk]: util/walk.js -(function(exports) { +(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.1.01"; @@ -1708,4 +1712,4 @@ return finishNode(node, "Identifier"); } -})(typeof exports === "undefined" ? (self.acorn = {}) : exports); +}); diff --git a/acorn_loose.js b/acorn_loose.js index 4c97323a74..87802f3b13 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -29,10 +29,14 @@ // invasive changes and simplifications without creating a complicated // tangle. -(function(exports) { +(function(mod) { + if (typeof exports == "object" && typeof module == "object") return mod(exports, require("./acorn")); // CommonJS + if (typeof define == "function" && define.amd) return define(["exports", "./acorn"], mod); // AMD + mod(self.acorn || (self.acorn = {}), self.acorn); // Plain browser env +})(function(exports, acorn) { "use strict"; - var acorn = exports.parse ? exports : require("./acorn"), tt = acorn.tokTypes; + var tt = acorn.tokTypes; var options, input, fetchToken, context; @@ -740,4 +744,4 @@ eat(close); return elts; } -})(typeof exports === "undefined" ? self.acorn : exports); +}); diff --git a/index.html b/index.html index e005f759d4..257958c239 100644 --- a/index.html +++ b/index.html @@ -14,7 +14,11 @@ https://github.com/marijnh/acorn.git

This file defines the main parser interface. The library also comes with a error-tolerant parser and an -abstract syntax tree walker, defined in other files.

(function(exports) {
+abstract syntax tree walker, defined in other files.

(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.1.01";

The main exported interface (under self.acorn when in the @@ -1284,6 +1288,6 @@ identifiers.

< return finishNode(node, "Identifier"); } -})(typeof exports === "undefined" ? (self.acorn = {}) : exports); +});
\ No newline at end of file diff --git a/util/walk.js b/util/walk.js index afed78c0f3..054c289ef7 100644 --- a/util/walk.js +++ b/util/walk.js @@ -1,6 +1,10 @@ // AST walker module for Mozilla Parser API compatible trees -(function(exports) { +(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.walk = {}); // Plain browser env +})(function(exports) { "use strict"; // A simple walk is one where you simply specify callbacks to be @@ -302,4 +306,4 @@ } }); -})(typeof exports == "undefined" ? acorn.walk = {} : exports); +});