Skip to content

Instantly share code, notes, and snippets.

@MrTelanie
Last active August 3, 2019 11:11
Show Gist options
  • Select an option

  • Save MrTelanie/3e03d10362ff5daa7169ce84e85e848c to your computer and use it in GitHub Desktop.

Select an option

Save MrTelanie/3e03d10362ff5daa7169ce84e85e848c to your computer and use it in GitHub Desktop.
(function () {
const packs = new Map();
let currentPack;
function dynamicImport(path) {
let pack = packs.get(path);
if (!pack) {
let load;
const prom = new Promise((resolve) => load = resolve);
pack = { path, load, prom };
packs.set(path, pack);
currentPack = pack;
//console.warn('start loading', path, pack);
import(path + '.js');
}
return pack.prom;
}
function dynamicExport(fun) {
const pack = currentPack;
currentPack = null;
const prom = fun();
//console.warn('dynamicExport', prom);
const { load } = pack;
pack.load = null;
prom.then(res => load(res));
}
window.dynamicImport = dynamicImport;
window.dynamicExport = dynamicExport
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment