Fix wrap_commonjs to include code first
authorMihai Bazon <mihai.bazon@gmail.com>
Thu, 24 Sep 2015 14:55:37 +0000 (17:55 +0300)
committerMihai Bazon <mihai.bazon@gmail.com>
Thu, 24 Sep 2015 14:58:51 +0000 (17:58 +0300)
(code could have directives, i.e. "use strict")

lib/ast.js

index 2e539cf..c317670 100644 (file)
@@ -329,12 +329,11 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
                 }
             }));
         }
-        var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
+        var wrapped_tl = "(function(exports, global){ '$ORIG'; '$EXPORTS'; global['" + name + "'] = exports; }({}, (function(){return this}())))";
         wrapped_tl = parse(wrapped_tl);
         wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
-            if (node instanceof AST_SimpleStatement) {
-                node = node.body;
-                if (node instanceof AST_String) switch (node.getValue()) {
+            if (node instanceof AST_Directive) {
+                switch (node.value) {
                   case "$ORIG":
                     return MAP.splice(self.body);
                   case "$EXPORTS":