{"action":"created","issue":{"url":"https://api.github.com/repos/systemjs/systemjs/issues/595","labels_url":"https://api.github.com/repos/systemjs/systemjs/issues/595/labels{/name}","comments_url":"https://api.github.com/repos/systemjs/systemjs/issues/595/comments","events_url":"https://api.github.com/repos/systemjs/systemjs/issues/595/events","html_url":"https://github.com/systemjs/systemjs/issues/595","id":95523954,"number":595,"title":"Module is loaded incorrectly if the very first statement of the module is not a `System.register` call","user":{"login":"daniel-a-melo","id":6760808,"avatar_url":"https://avatars.githubusercontent.com/u/6760808?v=3","gravatar_id":"","url":"https://api.github.com/users/daniel-a-melo","html_url":"https://github.com/daniel-a-melo","followers_url":"https://api.github.com/users/daniel-a-melo/followers","following_url":"https://api.github.com/users/daniel-a-melo/following{/other_user}","gists_url":"https://api.github.com/users/daniel-a-melo/gists{/gist_id}","starred_url":"https://api.github.com/users/daniel-a-melo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daniel-a-melo/subscriptions","organizations_url":"https://api.github.com/users/daniel-a-melo/orgs","repos_url":"https://api.github.com/users/daniel-a-melo/repos","events_url":"https://api.github.com/users/daniel-a-melo/events{/privacy}","received_events_url":"https://api.github.com/users/daniel-a-melo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-16T20:44:19Z","updated_at":"2015-07-17T12:20:23Z","closed_at":null,"body":"The modules which first statement is not `System.register` are not loaded correctly. They are loaded with the value evaluated from this statement instead of function registered. \r\n\r\n**Steps to reproduce**\r\n\r\n1 - `npm init`\r\n2 - `npm install systemjs --save-dev` . Currently version v0.18.4 is installed\r\n\r\nResult `package.json`\r\n\r\n``` javascript\r\n{\r\n  \"name\": \"systemjs-test\",\r\n  \"version\": \"1.0.0\",\r\n  \"description\": \"\",\r\n  \"main\": \"main.js\",\r\n  \"scripts\": {\r\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\r\n  },\r\n  \"author\": \"\",\r\n  \"license\": \"MIT\",\r\n  \"devDependencies\": {\r\n    \"systemjs\": \"^0.18.4\"\r\n  }\r\n}\r\n```\r\n3 - Create a module `main.js`\r\n\r\n``` javascript\r\nvar x = 1;\r\nSystem.register([], function(exports) {\r\n    console.log('Registered main module');\r\n    var ConsoleWriter;\r\n    return {\r\n        setters:[],\r\n        execute: function() {\r\n            ConsoleWriter = (function () {\r\n                function ConsoleWriter() {\r\n                }\r\n                ConsoleWriter.prototype.sayHello = function () {\r\n                  console.log('Hello!');\r\n                };\r\n                return ConsoleWriter;\r\n            })();\r\n            exports(\"ConsoleWriter\", ConsoleWriter);\r\n        }\r\n    }\r\n});\r\n```\r\n4 - Create an `index.html`\r\n\r\n```html\r\n <!doctype html>\r\n  <html>\r\n  \t<head>\r\n       <script src=\"node_modules/systemjs/dist/system.js\"></script>\r\n       <title>System JS</title>\r\n    </head>\r\n    <body>\r\n        <script>\r\n          System.config({\r\n            \"baseURL\": \"./\",\r\n            \"defaultJSExtensions\": true\r\n          });\r\n\r\n          System.import('main').then(function(main) {\r\n            console.log('Imported main');\r\n            console.log(main); //Outputs 1\r\n            console.log(typeof main); //Outputs number\r\n            if (main.ConsoleWriter) {\r\n              var writer = new main.ConsoleWriter();\r\n              writer.sayHello();\r\n            } else {\r\n              console.log('Module was loaded incorrecly');\r\n            }\r\n\r\n          });\r\n        </script>\r\n    </body>\r\n  </html>\r\n```\r\nList of files:\r\n\r\n```\r\npackage.json\r\nmain.js\r\nindex.html\r\nnode_modules/systemjs/dist/system.js\r\n```\r\nThe function registered on `main.js` is never executed. `System.import` completes the module loading providing the `number` 1 evaluated as the result of the first statement of `main.js`\r\n\r\nIf I remove the statement `var x = 1;` from `main.js` the module is loaded correctly. \r\n\r\nTested under FireFox 39 and Chorme 34 on Ubuntu 12.10\r\n\r\nThis issue affects TypeScript classes that extends some class and transpilation module is set to `system`. TypeScript transpiler inserts the definition of function `__extends` before `System.register` and it prevents the module from being loaded correctly as shown in this bug report.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/systemjs/systemjs/issues/comments/122259045","html_url":"https://github.com/systemjs/systemjs/issues/595#issuecomment-122259045","issue_url":"https://api.github.com/repos/systemjs/systemjs/issues/595","id":122259045,"user":{"login":"guybedford","id":598730,"avatar_url":"https://avatars.githubusercontent.com/u/598730?v=3","gravatar_id":"","url":"https://api.github.com/users/guybedford","html_url":"https://github.com/guybedford","followers_url":"https://api.github.com/users/guybedford/followers","following_url":"https://api.github.com/users/guybedford/following{/other_user}","gists_url":"https://api.github.com/users/guybedford/gists{/gist_id}","starred_url":"https://api.github.com/users/guybedford/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guybedford/subscriptions","organizations_url":"https://api.github.com/users/guybedford/orgs","repos_url":"https://api.github.com/users/guybedford/repos","events_url":"https://api.github.com/users/guybedford/events{/privacy}","received_events_url":"https://api.github.com/users/guybedford/received_events","type":"User","site_admin":false},"created_at":"2015-07-17T12:20:23Z","updated_at":"2015-07-17T12:20:23Z","body":"Thanks, setting variables outside of the `System.register` function is actually not supported by the format. You can make it work via metadata to inform that the module format is register:\r\n\r\n```javascript\r\nSystem.config({\r\n  meta: { main: { format: 'register' } }\r\n});\r\n```\r\n\r\nBut that will break down in other places too and is just a hotfix for the problem.\r\n\r\nThe problem is that TypeScript shouldn't be including code outside of the `System.register` function. Let me know if you'd like me to create an issue."}}