mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 08:32:56 +00:00
82 lines
3.2 KiB
JavaScript
82 lines
3.2 KiB
JavaScript
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
let _module_federation_runtime = require("@module-federation/runtime");
|
|
let _module_federation_runtime_helpers = require("@module-federation/runtime/helpers");
|
|
_module_federation_runtime_helpers = require_runtime.__toESM(_module_federation_runtime_helpers);
|
|
|
|
//#region src/init.ts
|
|
function init({ webpackRequire }) {
|
|
const { initOptions, runtime, sharedFallback, bundlerRuntime, libraryType } = webpackRequire.federation;
|
|
if (!initOptions) throw new Error("initOptions is required!");
|
|
const treeShakingSharePlugin = function() {
|
|
return {
|
|
name: "tree-shake-plugin",
|
|
beforeInit(args) {
|
|
const { userOptions, origin, options: registeredOptions } = args;
|
|
const version = userOptions.version || registeredOptions.version;
|
|
if (!sharedFallback) return args;
|
|
const currentShared = userOptions.shared || {};
|
|
const shared = [];
|
|
Object.keys(currentShared).forEach((sharedName) => {
|
|
(Array.isArray(currentShared[sharedName]) ? currentShared[sharedName] : [currentShared[sharedName]]).forEach((sharedArg) => {
|
|
shared.push([sharedName, sharedArg]);
|
|
if ("get" in sharedArg) {
|
|
sharedArg.treeShaking ||= {};
|
|
sharedArg.treeShaking.get = sharedArg.get;
|
|
sharedArg.get = bundlerRuntime.getSharedFallbackGetter({
|
|
shareKey: sharedName,
|
|
factory: sharedArg.get,
|
|
webpackRequire,
|
|
libraryType,
|
|
version: sharedArg.version
|
|
});
|
|
}
|
|
});
|
|
});
|
|
const hostGlobalSnapshot = _module_federation_runtime_helpers.default.global.getGlobalSnapshotInfoByModuleInfo({
|
|
name: origin.name,
|
|
version
|
|
});
|
|
if (!hostGlobalSnapshot || !("shared" in hostGlobalSnapshot)) return args;
|
|
Object.keys(registeredOptions.shared || {}).forEach((pkgName) => {
|
|
registeredOptions.shared[pkgName].forEach((sharedArg) => {
|
|
shared.push([pkgName, sharedArg]);
|
|
});
|
|
});
|
|
const patchShared = (pkgName, shared) => {
|
|
const shareSnapshot = hostGlobalSnapshot.shared.find((item) => item.sharedName === pkgName);
|
|
if (!shareSnapshot) return;
|
|
const { treeShaking } = shared;
|
|
if (!treeShaking) return;
|
|
const { secondarySharedTreeShakingName, secondarySharedTreeShakingEntry, treeShakingStatus } = shareSnapshot;
|
|
if (treeShaking.status === treeShakingStatus) return;
|
|
treeShaking.status = treeShakingStatus;
|
|
if (secondarySharedTreeShakingEntry && libraryType && secondarySharedTreeShakingName) treeShaking.get = async () => {
|
|
const shareEntry = await (0, _module_federation_runtime.getRemoteEntry)({
|
|
origin,
|
|
remoteInfo: {
|
|
name: secondarySharedTreeShakingName,
|
|
entry: secondarySharedTreeShakingEntry,
|
|
type: libraryType,
|
|
entryGlobalName: secondarySharedTreeShakingName,
|
|
shareScope: "default"
|
|
}
|
|
});
|
|
await shareEntry.init(origin, __webpack_require__.federation.bundlerRuntime);
|
|
return shareEntry.get();
|
|
};
|
|
};
|
|
shared.forEach(([pkgName, sharedArg]) => {
|
|
patchShared(pkgName, sharedArg);
|
|
});
|
|
return args;
|
|
}
|
|
};
|
|
};
|
|
initOptions.plugins ||= [];
|
|
initOptions.plugins.push(treeShakingSharePlugin());
|
|
return runtime.init(initOptions);
|
|
}
|
|
|
|
//#endregion
|
|
exports.init = init;
|
|
//# sourceMappingURL=init.cjs.map
|