{"action":"created","issue":{"url":"https://api.github.com/repos/systemjs/systemjs/issues/351","labels_url":"https://api.github.com/repos/systemjs/systemjs/issues/351/labels{/name}","comments_url":"https://api.github.com/repos/systemjs/systemjs/issues/351/comments","events_url":"https://api.github.com/repos/systemjs/systemjs/issues/351/events","html_url":"https://github.com/systemjs/systemjs/issues/351","id":57914418,"number":351,"title":"Supporting contextual AMD require","user":{"login":"markstickley","id":83684,"avatar_url":"https://avatars.githubusercontent.com/u/83684?v=3","gravatar_id":"","url":"https://api.github.com/users/markstickley","html_url":"https://github.com/markstickley","followers_url":"https://api.github.com/users/markstickley/followers","following_url":"https://api.github.com/users/markstickley/following{/other_user}","gists_url":"https://api.github.com/users/markstickley/gists{/gist_id}","starred_url":"https://api.github.com/users/markstickley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstickley/subscriptions","organizations_url":"https://api.github.com/users/markstickley/orgs","repos_url":"https://api.github.com/users/markstickley/repos","events_url":"https://api.github.com/users/markstickley/events{/privacy}","received_events_url":"https://api.github.com/users/markstickley/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-17T11:44:34Z","updated_at":"2015-02-18T13:29:55Z","closed_at":null,"body":"Hi,\r\n\r\nI'm seeing an issue when bundling with systemjs builder, although the issue comes when running the bundled code so I think I'm right in posting the issue here.\r\n\r\nI have been using require.js until recently switching to system.js, but there are other clients for this library who still rely on require.js and so radically changing the source is not an easy solution...\r\n\r\nThe issue manifests when using the AMD pattern and you are a) handling circular dependencies or b) dynamically loading modules at run time. Using `require([module], callback)` in the body of a define block where `module` has not been specified as a dependency of the define block will give an error similar to this:\r\n\r\n```\r\n Potentially unhandled rejection [2] TypeError: Error loading \"module1\" at http://spiritedaway.blinkbox.local:8080/module1.js\r\nModule module3 not declared as a dependency.\r\n    at http://spiritedaway.blinkbox.local:8080/system.js:1:6049\r\n    at http://spiritedaway.blinkbox.local:8080/build/bundle0.js:13:5\r\n    at http://spiritedaway.blinkbox.local:8080/build/bundle0.js:16:6\r\n    at f (http://spiritedaway.blinkbox.local:8080/system.js:1:5933)\r\n    at l (http://spiritedaway.blinkbox.local:8080/system.js:1:4623)\r\n    at Object.$__global.upgradeSystemLoader.e.instantiate.Promise.all.then.execute (http://spiritedaway.blinkbox.local:8080/system.js:1:8139)\r\n    at o (http://spiritedaway.blinkbox.local:8080/es6-module-loader.js:7:17774)\r\n    at p (http://spiritedaway.blinkbox.local:8080/es6-module-loader.js:7:18146)\r\n    at j (http://spiritedaway.blinkbox.local:8080/es6-module-loader.js:7:15692)\r\n    at k (http://spiritedaway.blinkbox.local:8080/es6-module-loader.js:7:16077)\r\n```\r\n\r\nThis does not happen when using require.js and when using SystemJS with the unbundled code (so omitting these lines in config.js):\r\n\r\n``` javascript\r\nSystem.depCache['module1'] = [\"module2\"];\r\nSystem.bundles['build/bundle0'] = [\"module2\",\"module1\"];\r\n```\r\n\r\nI thought I might fix it by adding 'module3' to module1's depCache but that didn't do anything.\r\n\r\nFor a working example of the problem, please download this sample project and open index-system.html: http://www.markstickley.co.uk/system-require.zip\r\n\r\nAm I doing something obviously wrong?\r\n\r\nA use case for dynamically loading modules at run time is resolving circular dependencies predictably. If two modules depend on each other then depending on the entry point and its dependencies you may not be able to tell which will be loaded first and therefore which module will have one of its dependencies set to `undefined`. By removing the circular dependency from the define block and placing it in a require block inside the definition you only have to handle it once.\r\n\r\nAnother use case is when you get data back from a web service describing an object and you have to choose which module to load and instantiate based on the data received. Not knowing the data ahead of time you would have to depend on every single module which can lead to slow loading times so it's best to handle it at run-time. In our app the modules being loaded are bundled and referenced in `config.js` also.\r\n\r\nThanks!\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/systemjs/systemjs/issues/comments/74863500","html_url":"https://github.com/systemjs/systemjs/issues/351#issuecomment-74863500","issue_url":"https://api.github.com/repos/systemjs/systemjs/issues/351","id":74863500,"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-02-18T13:29:55Z","updated_at":"2015-02-18T13:29:55Z","body":"We don't support importing the AMD-style contextual require. The way to do this would be with the `System.import` call as shown above. Perhaps have this in a conditional to support both environments, which would work fine.\r\n\r\nThis is certainly something we could support in the AMD layer, although I don't have the bandwidth myself to work on it myself right now. Feel free to make a PR, or let me know if you'd like to sponsor the feature."}}