Last active
August 3, 2019 11:11
-
-
Save MrTelanie/3e03d10362ff5daa7169ce84e85e848c to your computer and use it in GitHub Desktop.
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
| (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