UNPKG

@macrof/loader

Version:

React MicroFrontend Loader, Typescript, Webpack 5, ModuleFederation

40 lines (39 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoadMF = exports.Bootstrap = exports.unmount = exports.load = exports.getUid = void 0; const error_1 = require("../components/error"); const getUid = (name) => `MFClient-${name}-${Math.random().toString(36).slice(2, -1)}`; exports.getUid = getUid; const load = (dynamicImport, name) => dynamicImport(name) .then((app) => { window.__MF_IMPORTED__ = true; return app; }) .catch(() => { console.error(`Failed to load MF ${name}.`); return { default: () => error_1.Error }; }); exports.load = load; const unmount = (currentTarget, name) => { var _a, _b, _c; const key = `__${name}__`; window[key] = (_a = window[key]) !== null && _a !== void 0 ? _a : {}; const insertedStyle = window[key]; (_b = insertedStyle.targets) === null || _b === void 0 ? void 0 : _b.splice((_c = insertedStyle.targets) === null || _c === void 0 ? void 0 : _c.findIndex((target) => target === currentTarget), 1); }; exports.unmount = unmount; const Bootstrap = (render, element) => { if (!window.__MF_IMPORTED__) { render(element); } }; exports.Bootstrap = Bootstrap; function LoadMF(MF) { const Window = window; if (!Window.__PROVIDERS_LOADED__) { MF.providers.forEach((func) => func()); Window.__PROVIDERS_LOADED__ = true; } return MF.apps; } exports.LoadMF = LoadMF;