Skip to content

Instantly share code, notes, and snippets.

@barbagrigia
Forked from surma/importPolyfill.js
Created May 12, 2017 21:13
Show Gist options
  • Select an option

  • Save barbagrigia/b877453c38621d070889c06d40f94162 to your computer and use it in GitHub Desktop.

Select an option

Save barbagrigia/b877453c38621d070889c06d40f94162 to your computer and use it in GitHub Desktop.
Polyfill for dynamic module loading
_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;
}
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