@esmx/rspack
Version:
A high-performance Rspack integration for Esmx microfrontend framework, providing module federation and SSR capabilities.
45 lines (44 loc) • 1.51 kB
JavaScript
(() => {
const HMR_JSONP = "__esmx_rspack_hmr_jsonp__";
const HMR_JSONP_LIST = "__esmx_rspack_hmr_jsonp_list__";
const list = window[HMR_JSONP_LIST] = window[HMR_JSONP_LIST] || [];
Object.defineProperty(window, HMR_JSONP, {
get() {
return (...args) => {
const hotUrl = getStackUrl(new Error().stack || "", 1);
if (hotUrl) {
const item = list.find(
(item2) => isSameModule(hotUrl, item2.url)
);
if (item) {
return item.jsonp(...args);
}
}
console.log("%chot update not found", "color: red", args);
};
},
set(jsonp) {
const url = getStackUrl(new Error().stack || "", 1);
if (url) {
list.push({ url, jsonp });
}
}
});
function isSameModule(hotUrl, originalUrl) {
const normalizedHotUrl = hotUrl.replace(/\/__hot__\//, "/exports/").replace(/\.\w+\.hot-update\.mjs$/, ".mjs");
const normalizedOriginalUrl = originalUrl;
return normalizedHotUrl === normalizedOriginalUrl;
}
function getStackUrl(stack, index = 0) {
const lines = stack.split("\n");
const stackLines = lines.filter((line2) => line2.includes("at "));
if (index < 0 || index >= stackLines.length) {
return null;
}
const line = stackLines[index];
const withoutAt = line.replace(/^\s*at\s+/, "");
const urlMatch = withoutAt.match(/\((.*?)\)/);
const url = urlMatch ? urlMatch[1] : withoutAt;
return url.replace(/:\d+:\d+$/, "");
}
})();