{"action":"created","issue":{"url":"https://api.github.com/repos/rollup/rollup/issues/429","labels_url":"https://api.github.com/repos/rollup/rollup/issues/429/labels{/name}","comments_url":"https://api.github.com/repos/rollup/rollup/issues/429/comments","events_url":"https://api.github.com/repos/rollup/rollup/issues/429/events","html_url":"https://github.com/rollup/rollup/issues/429","id":125198917,"number":429,"title":"What's Rollup's stance on side-effectful imports","user":{"login":"Victorystick","id":703602,"avatar_url":"https://avatars.githubusercontent.com/u/703602?v=3","gravatar_id":"","url":"https://api.github.com/users/Victorystick","html_url":"https://github.com/Victorystick","followers_url":"https://api.github.com/users/Victorystick/followers","following_url":"https://api.github.com/users/Victorystick/following{/other_user}","gists_url":"https://api.github.com/users/Victorystick/gists{/gist_id}","starred_url":"https://api.github.com/users/Victorystick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Victorystick/subscriptions","organizations_url":"https://api.github.com/users/Victorystick/orgs","repos_url":"https://api.github.com/users/Victorystick/repos","events_url":"https://api.github.com/users/Victorystick/events{/privacy}","received_events_url":"https://api.github.com/users/Victorystick/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rollup/rollup/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-01-06T15:31:41Z","updated_at":"2016-01-07T01:59:25Z","closed_at":null,"body":"I've encountered this issue once before, where I have an import for side-effects and then import that rely on those side-effects. How should Rollup handle things [like this](http://rollupjs.org/#%7B%22options%22%3A%7B%22format%22%3A%22cjs%22%2C%22moduleName%22%3A%22myBundle%22%2C%22globals%22%3A%7B%7D%7D%2C%22modules%22%3A%5B%7B%22name%22%3A%22main.js%22%2C%22code%22%3A%22import%20'.%2FsetImmediate-polyfill.js'%3B%5Cnimport%20asap%20from%20'.%2Fasap.js'%3B%5Cn%5Cnconsole.log(%20asap(%20()%20%3D%3E%20console.log(%20'foo'%20)%20)%20)%3B%22%7D%2C%7B%22name%22%3A%22asap.js%22%2C%22code%22%3A%22%2F%2F%20uses%20the%20best%20available%20option%5Cnexport%20default%20window.setImmediate%20%7C%7C%C2%A0window.setTimeout%3B%22%7D%2C%7B%22name%22%3A%22setImmediate-polyfill.js%22%2C%22code%22%3A%22%2F%2F%20this%20goes%20in%20too%20late%5Cnwindow.setImmediate%20%3D%20function%20()%20%7B%7D%3B%22%7D%5D%7D)? I think that many people, instinctively, would expect the first import to execute before the second; but currently that is not the case.\r\n\r\n```js\r\n// This first import should execute...\r\nimport './setImmediate-polyfill.js';\r\n// ... before this.\r\nimport asap from './asap.js';\r\n\r\nconsole.log( asap( () => console.log( 'foo' ) ) );\r\n```\r\n\r\nRollup will detect that `main` requires the other two, but that there is no dependency between the other modules. It therefore places them in some deterministic, but arbitrary order."},"comment":{"url":"https://api.github.com/repos/rollup/rollup/issues/comments/169522030","html_url":"https://github.com/rollup/rollup/issues/429#issuecomment-169522030","issue_url":"https://api.github.com/repos/rollup/rollup/issues/429","id":169522030,"user":{"login":"Rich-Harris","id":1162160,"avatar_url":"https://avatars.githubusercontent.com/u/1162160?v=3","gravatar_id":"","url":"https://api.github.com/users/Rich-Harris","html_url":"https://github.com/Rich-Harris","followers_url":"https://api.github.com/users/Rich-Harris/followers","following_url":"https://api.github.com/users/Rich-Harris/following{/other_user}","gists_url":"https://api.github.com/users/Rich-Harris/gists{/gist_id}","starred_url":"https://api.github.com/users/Rich-Harris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Rich-Harris/subscriptions","organizations_url":"https://api.github.com/users/Rich-Harris/orgs","repos_url":"https://api.github.com/users/Rich-Harris/repos","events_url":"https://api.github.com/users/Rich-Harris/events{/privacy}","received_events_url":"https://api.github.com/users/Rich-Harris/received_events","type":"User","site_admin":false},"created_at":"2016-01-07T01:59:25Z","updated_at":"2016-01-07T01:59:25Z","body":"Thanks @Swatinem, was hoping someone would dig up the relevant bit of spec! So https://tc39.github.io/ecma262/#sec-moduleevaluation is basically describing a straightforward topological sort, up until the point we encounter cyclical dependencies – at which point we're supposed to short-circuit.\r\n\r\nDoes this mean that a situation like this...\r\n\r\n```js\r\n// main.js\r\nimport A from './A.js'; // 1\r\nimport B from './B.js';\r\n\r\nnew A();\r\nnew B();\r\n\r\n// A.js\r\nimport B from './B.js'; // 2\r\n\r\nexport default class A {\r\n  b () {\r\n    return new B();\r\n  }\r\n}\r\n\r\n// B.js\r\nimport A from './A.js'; // 3 – short-circuits before A can be evaluated...\r\n\r\nexport default class B extends A { // ...meaning this can't execute\r\n  a () {\r\n    return new A();\r\n  }\r\n}\r\n```\r\n\r\n...should result in an error, because `B.js` gets evaluated before `A.js`? If the two imports in `main.js` were reversed, everything would be fine – which seems undesirable, because `main.js` shouldn't need to be concerned about the relationship between `A.js` and `B.js`.\r\n\r\nRollup shifts the order in a way that's designed to avoid that scenario. Is that incorrect?"}}