Skip to content

Instantly share code, notes, and snippets.

@Lyoko-Jeremie
Last active July 7, 2023 05:09
Show Gist options
  • Save Lyoko-Jeremie/90e3c370f0fb11d1a709e5e81dd5887f to your computer and use it in GitHub Desktop.
Save Lyoko-Jeremie/90e3c370f0fb11d1a709e5e81dd5887f to your computer and use it in GitHub Desktop.
webpackJsonp simple loader
// webpackJsonp simple loader
// call this with `window.__M_resolver("0598")`
// some idea come from https://plnkr.co/edit/xFT4ib5uHqkQ8XIrPqf3?p=catalogue&preview
// all the module have 3 params, and it means :
// 1 the module default exports, will used with `newM.exports={api1,api2,api3}`
// 2 the module export table, will be used with `export val api1; export val api2;`
// 3 the webpack module resolver, module will try to get another module like `m("eff2")` , this is a DI (dependency injection) design pattern
(function () {
window.__M_readFromCache = function (id) {
return window.webpackJsonp.map(T => T[1][id]).filter(T => T)[0];
}
window.__M = {};
window.__M.TTT = {};
window.__M_resolver = function (id) {
console.log("window.__M_resolver try to load ID:", id);
if (!window.__M[id]) {
// load it
let newM = {};
newM.exports = {};
let mCache = window.__M_readFromCache(id);
if (!mCache) {
console.log("window.__M_resolver (!mCache) in ID:", id);
throw "window.__M_resolver (!mCache) in ID:" + id;
}
// will be used as (newM.exports, newM.exports.api, window.__M_resolver("id"))
mCache(newM, newM.exports, window.__M_resolver);
window.__M[id] = newM;
}
if (!window.__M[id]) {
console.log("window.__M_resolver (!window.__M[id]) in ID:", id);
throw "window.__M_resolver (!window.__M[id]) in ID:" + id;
}
return window.__M[id].exports;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment