-
-
Save barbagrigia/b877453c38621d070889c06d40f94162 to your computer and use it in GitHub Desktop.
Polyfill for dynamic module loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _registry = {}; | |
| importPolyfill = path => { | |
| if(!(path in _registry)) { | |
| const entry = _registry[path] = {}; | |
| entry.promise = new Promise(resolve => entry.resolve = resolve); | |
| document.head.appendChild(Object.assign( | |
| document.createElement('script'), | |
| { | |
| type: 'module', | |
| innerText: `import * as X from '${path}'; _registry['${path}'].resolve(X);`, | |
| } | |
| )); | |
| } | |
| return _registry[path].promise; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| r={},importPolyfill=(a)=>{if(!(a in r)){const b=r[a]={};b.p=new Promise((c)=>b.r=c),document.head.appendChild(Object.assign(document.createElement('script'),{type:'module',innerText:`import * as X from '${a}'; r['${a}'].r(X);`}))}return r[a].p}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment