mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 01:22:49 +00:00
63 lines
2.6 KiB
JavaScript
63 lines
2.6 KiB
JavaScript
const require_updateOptions = require('./updateOptions.cjs');
|
|
const require_getUsedExports = require('./getUsedExports.cjs');
|
|
|
|
//#region src/installInitialConsumes.ts
|
|
function handleInitialConsumes(options) {
|
|
const { moduleId, moduleToHandlerMapping, webpackRequire, asyncLoad } = options;
|
|
const federationInstance = webpackRequire.federation.instance;
|
|
if (!federationInstance) throw new Error("Federation instance not found!");
|
|
const { shareKey, shareInfo } = moduleToHandlerMapping[moduleId];
|
|
try {
|
|
const usedExports = require_getUsedExports.getUsedExports(webpackRequire, shareKey);
|
|
const customShareInfo = { ...shareInfo };
|
|
if (usedExports) customShareInfo.treeShaking = {
|
|
usedExports,
|
|
useIn: [federationInstance.options.name]
|
|
};
|
|
if (asyncLoad) return federationInstance.loadShare(shareKey, { customShareInfo });
|
|
return federationInstance.loadShareSync(shareKey, { customShareInfo });
|
|
} catch (err) {
|
|
console.error("loadShareSync failed! The function should not be called unless you set \"eager:true\". If you do not set it, and encounter this issue, you can check whether an async boundary is implemented.");
|
|
console.error("The original error message is as follows: ");
|
|
throw err;
|
|
}
|
|
}
|
|
function installInitialConsumes(options) {
|
|
require_updateOptions.updateConsumeOptions(options);
|
|
const { moduleToHandlerMapping, webpackRequire, installedModules, initialConsumes, asyncLoad } = options;
|
|
const factoryIdSets = [];
|
|
initialConsumes.forEach((id) => {
|
|
const factoryGetter = () => handleInitialConsumes({
|
|
moduleId: id,
|
|
moduleToHandlerMapping,
|
|
webpackRequire,
|
|
asyncLoad
|
|
});
|
|
factoryIdSets.push([id, factoryGetter]);
|
|
});
|
|
const setModule = (id, factoryGetter) => {
|
|
webpackRequire.m[id] = (module) => {
|
|
installedModules[id] = 0;
|
|
delete webpackRequire.c[id];
|
|
const factory = factoryGetter();
|
|
if (typeof factory !== "function") throw new Error(`Shared module is not available for eager consumption: ${id}`);
|
|
const result = factory();
|
|
const { shareInfo } = moduleToHandlerMapping[id];
|
|
if (shareInfo?.shareConfig?.layer && result && typeof result === "object") try {
|
|
if (!result.hasOwnProperty("layer") || result.layer === void 0) result.layer = shareInfo.shareConfig.layer;
|
|
} catch (e) {}
|
|
module.exports = result;
|
|
};
|
|
};
|
|
if (asyncLoad) return Promise.all(factoryIdSets.map(async ([id, factoryGetter]) => {
|
|
const result = await factoryGetter();
|
|
setModule(id, () => result);
|
|
}));
|
|
factoryIdSets.forEach(([id, factoryGetter]) => {
|
|
setModule(id, factoryGetter);
|
|
});
|
|
}
|
|
|
|
//#endregion
|
|
exports.installInitialConsumes = installInitialConsumes;
|
|
//# sourceMappingURL=installInitialConsumes.cjs.map
|