{"action":"opened","issue":{"url":"https://api.github.com/repos/systemjs/systemjs/issues/982","labels_url":"https://api.github.com/repos/systemjs/systemjs/issues/982/labels{/name}","comments_url":"https://api.github.com/repos/systemjs/systemjs/issues/982/comments","events_url":"https://api.github.com/repos/systemjs/systemjs/issues/982/events","html_url":"https://github.com/systemjs/systemjs/issues/982","id":123365860,"number":982,"title":"Separate instances, shared resources (à la requirejs context)","user":{"login":"sberney","id":7673075,"avatar_url":"https://avatars.githubusercontent.com/u/7673075?v=3","gravatar_id":"","url":"https://api.github.com/users/sberney","html_url":"https://github.com/sberney","followers_url":"https://api.github.com/users/sberney/followers","following_url":"https://api.github.com/users/sberney/following{/other_user}","gists_url":"https://api.github.com/users/sberney/gists{/gist_id}","starred_url":"https://api.github.com/users/sberney/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sberney/subscriptions","organizations_url":"https://api.github.com/users/sberney/orgs","repos_url":"https://api.github.com/users/sberney/repos","events_url":"https://api.github.com/users/sberney/events{/privacy}","received_events_url":"https://api.github.com/users/sberney/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-21T21:49:13Z","updated_at":"2015-12-21T21:49:13Z","closed_at":null,"body":"In requirejs, after you make a new context, you can do this:\r\n\r\n```javascript\r\nrequire.s.contexts['context-no-1'].defined['some-package'] = require.s.contexts._.defined['some-package']\r\n```\r\n\r\nAnd now, bam, you can load `some-package` in `context-no-1` even though it lives in the master (or some other) context.\r\n\r\nI've created a plugin+normalization override that allow me to do much the same thing, but not quite perfectly. SystemJS has `loaderObj._loader.modules` which can be swapped around. However, because names are resolved to absolute URLs, it's not so simple if a package's dependencies aren't fully loaded at the time you move a module from one instance to another.\r\n\r\nI would rather be doing this more fully through the plugin system, instead of performing customizations on a new System instance. However, the plugin API (definitely correct me if I'm wrong) seems to only support locate/fetch/translate/instantiate overrides, whereas I would actually like to provide a completely separate System instance as a loader.\r\n\r\nHere is what I ultimately want (SystemOne and SystemTwo have different `baseURL`s):\r\n\r\n```javascript\r\nPromise.resolve()\r\n.then(() => { SystemTwo.import('my-application'); })\r\n.then(() => { {SystemTwo.import('my-library!one'); })\r\n.then(myLibraryTwo => {\r\n  SystemOne.import('my-library')\r\n  .then(myLibraryOne => {\r\n    myLibraryTwo === myLibraryOne // true\r\n  });\r\n});\r\n```\r\n\r\nIt's not actually required that `===` hold true, but just that the two libraries are identical, and the library code preferably not be held in memory in more than one place. There are several issues with the way I have accomplished this so far (and, I have inelegant work arounds, but they are icky).\r\n\r\nSystemJS loads itself to the window in [`preExec`](https://github.com/systemjs/systemjs/blob/b4fd67410768d85641f706ab0f86f542a74a7655/lib/global-eval.js#L14) and restores itself to the window in `postExec` defined immediately afterwards. However, that restoration occurs only after the callCounter is at zero, which to the best of my knowledge indicates that all requested dependencies and top level packages have been loaded. I need to use another System instance part way through. Sometimes, this causes somewhat mysterious behavior (but I have thus far avoided the need to delve deeply into that). Perhaps the source of mystery is *actually* some place else, but that will have to wait its turn for inspection.\r\n\r\nSystemJS overwrites `window.define` all over the place. Unless I explicitly reset `define`, I will be defining things into the wrong instance. Typically I am resetting the window define to `() => { System.amdDefine.apply(System, arguments); }`, which seems to work. I really don't think this **should** be necessary...\r\n\r\nWhat do you think of the behavior I am trying to introduce? Did you envision accomplishing the same thing, in a particular way? Is there a way to just stick another System instance into the chain as a `._loader` and move on with my life? Any wisdom is welcome, as I'm getting a lot closer to being done with this."}}