@module-federation/webpack-bundler-runtime
Advanced tools
Comparing version 0.0.0-next-20240102023857 to 0.0.0-next-20240102063242
'use strict'; | ||
var runtime = require('@module-federation/runtime'); | ||
var sdk = require('@module-federation/sdk'); | ||
var constant = require('./constant.cjs.js'); | ||
@@ -25,4 +27,13 @@ function _interopNamespace(e) { | ||
function attachShareScopeMap(webpackRequire) { | ||
if (!webpackRequire.S || webpackRequire.federation.hasAttachShareScopeMap || !webpackRequire.federation.instance || !webpackRequire.federation.instance.shareScopeMap) { | ||
return; | ||
} | ||
webpackRequire.S = webpackRequire.federation.instance.shareScopeMap; | ||
webpackRequire.federation.hasAttachShareScopeMap = true; | ||
} | ||
function remotes(options) { | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire; | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap; | ||
attachShareScopeMap(webpackRequire); | ||
if (webpackRequire.o(chunkMapping, chunkId)) { | ||
@@ -35,2 +46,3 @@ chunkMapping[chunkId].forEach(function(id) { | ||
var data = idToExternalAndNameMapping[id]; | ||
var remoteInfos = idToRemoteMap[id]; | ||
// @ts-ignore seems not work | ||
@@ -83,5 +95,2 @@ if (getScope.indexOf(data) >= 0) { | ||
}; | ||
var useRuntimeLoad = [ | ||
"script" | ||
].includes(data[3]) && data[4]; | ||
// eslint-disable-next-line no-var | ||
@@ -96,5 +105,7 @@ var onFactory = function(factory) { | ||
try { | ||
var remoteModuleName = data[4] + data[1].slice(1); | ||
var remoteName = sdk.decodeName(remoteInfos[0].name, constant.ENCODE_NAME_PREFIX); | ||
var remoteModuleName = remoteName + data[1].slice(1); | ||
return webpackRequire.federation.instance.loadRemote(remoteModuleName, { | ||
loadFactory: false | ||
loadFactory: false, | ||
from: "build" | ||
}); | ||
@@ -105,2 +116,5 @@ } catch (error) { | ||
}; | ||
var useRuntimeLoad = remoteInfos.length === 1 && [ | ||
"script" | ||
].includes(remoteInfos[0].externalType) && remoteInfos[0].name; | ||
if (useRuntimeLoad) { | ||
@@ -115,21 +129,5 @@ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1); | ||
function proxyShareScopeMap(__webpack_require__) { | ||
if (!__webpack_require__.S) { | ||
return; | ||
} | ||
// @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error | ||
__webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, { | ||
get: function get(target, prop, receiver) { | ||
return globalThis.__VMOK__.__SHARE__[prop]; | ||
}, | ||
set: function set(target, prop, value) { | ||
globalThis.__VMOK__.__SHARE__[prop] = value; | ||
return true; | ||
} | ||
}); | ||
} | ||
function consumes(options) { | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire; | ||
proxyShareScopeMap(webpackRequire); | ||
attachShareScopeMap(webpackRequire); | ||
if (webpackRequire.o(chunkMapping, chunkId)) { | ||
@@ -179,4 +177,57 @@ chunkMapping[chunkId].forEach(function(id) { | ||
function initializeSharing(shareScopeName, webpackRequire) { | ||
return webpackRequire.federation.instance.initializeSharing(shareScopeName); | ||
function initializeSharing(param) { | ||
var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope; | ||
if (!initScope) initScope = []; | ||
// handling circular init calls | ||
var initToken = initTokens[shareScopeName]; | ||
if (!initToken) initToken = initTokens[shareScopeName] = {}; | ||
if (initScope.indexOf(initToken) >= 0) return; | ||
initScope.push(initToken); | ||
var promise = initPromises[shareScopeName]; | ||
if (promise) return promise; | ||
var warn = function(msg) { | ||
return typeof console !== "undefined" && console.warn && console.warn(msg); | ||
}; | ||
var initExternal = function(id) { | ||
var handleError = function(err) { | ||
return warn("Initialization of sharing external failed: " + err); | ||
}; | ||
try { | ||
var module = webpackRequire(id); | ||
if (!module) return; | ||
var initFn = function(module) { | ||
return module && module.init && module.init(webpackRequire.S[shareScopeName], initScope); | ||
}; | ||
if (module.then) return promises.push(module.then(initFn, handleError)); | ||
var initResult = initFn(module); | ||
// @ts-ignore | ||
if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore | ||
return promises.push(initResult["catch"](handleError)); | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
}; | ||
var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName); | ||
attachShareScopeMap(webpackRequire); | ||
var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes; | ||
if (bundlerRuntimeRemotesOptions) { | ||
Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) { | ||
var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId]; | ||
var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2]; | ||
if (info.length > 1) { | ||
initExternal(externalModuleId); | ||
} else if (info.length === 1) { | ||
var remoteInfo = info[0]; | ||
if (!constant.FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) { | ||
initExternal(externalModuleId); | ||
} | ||
} | ||
}); | ||
} | ||
if (!promises.length) { | ||
return initPromises[shareScopeName] = true; | ||
} | ||
return initPromises[shareScopeName] = Promise.all(promises).then(function() { | ||
return initPromises[shareScopeName] = true; | ||
}); | ||
} | ||
@@ -216,2 +267,36 @@ | ||
function isLegacyHost(shareScope) { | ||
if ("version" in shareScope && typeof shareScope["version"] !== "object") { | ||
return true; | ||
} | ||
if ("region" in shareScope && typeof shareScope["region"] !== "object") { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function initContainerEntry(options) { | ||
var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey; | ||
if (!webpackRequire.S) return; | ||
if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return; | ||
var name = shareScopeKey || "default"; | ||
webpackRequire.federation.instance.initOptions({ | ||
name: webpackRequire.federation.initOptions.name, | ||
remotes: [] | ||
}); | ||
if (isLegacyHost(shareScope)) { | ||
var prevShareScope = globalThis.__FEDERATION__.__SHARE__["default"]; | ||
if (prevShareScope) { | ||
webpackRequire.federation.instance.initShareScopeMap(name, prevShareScope); | ||
} | ||
} else { | ||
webpackRequire.federation.instance.initShareScopeMap(name, shareScope); | ||
} | ||
webpackRequire.S[name] = shareScope; | ||
if (webpackRequire.federation.attachShareScopeMap) { | ||
webpackRequire.federation.attachShareScopeMap(webpackRequire); | ||
} | ||
// @ts-ignore | ||
return webpackRequire.I(name, initScope); | ||
} | ||
var federation = { | ||
@@ -226,6 +311,9 @@ runtime: runtime__namespace, | ||
S: {}, | ||
installInitialConsumes: installInitialConsumes | ||
} | ||
installInitialConsumes: installInitialConsumes, | ||
initContainerEntry: initContainerEntry | ||
}, | ||
attachShareScopeMap: attachShareScopeMap, | ||
bundlerRuntimeOptions: {} | ||
}; | ||
module.exports = federation; |
import * as runtime from '@module-federation/runtime'; | ||
import { decodeName } from '@module-federation/sdk'; | ||
import { ENCODE_NAME_PREFIX, FEDERATION_SUPPORTED_TYPES } from './constant.esm.js'; | ||
function attachShareScopeMap(webpackRequire) { | ||
if (!webpackRequire.S || webpackRequire.federation.hasAttachShareScopeMap || !webpackRequire.federation.instance || !webpackRequire.federation.instance.shareScopeMap) { | ||
return; | ||
} | ||
webpackRequire.S = webpackRequire.federation.instance.shareScopeMap; | ||
webpackRequire.federation.hasAttachShareScopeMap = true; | ||
} | ||
function remotes(options) { | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire; | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire, idToRemoteMap = options.idToRemoteMap; | ||
attachShareScopeMap(webpackRequire); | ||
if (webpackRequire.o(chunkMapping, chunkId)) { | ||
@@ -12,2 +23,3 @@ chunkMapping[chunkId].forEach(function(id) { | ||
var data = idToExternalAndNameMapping[id]; | ||
var remoteInfos = idToRemoteMap[id]; | ||
// @ts-ignore seems not work | ||
@@ -60,5 +72,2 @@ if (getScope.indexOf(data) >= 0) { | ||
}; | ||
var useRuntimeLoad = [ | ||
"script" | ||
].includes(data[3]) && data[4]; | ||
// eslint-disable-next-line no-var | ||
@@ -73,5 +82,7 @@ var onFactory = function(factory) { | ||
try { | ||
var remoteModuleName = data[4] + data[1].slice(1); | ||
var remoteName = decodeName(remoteInfos[0].name, ENCODE_NAME_PREFIX); | ||
var remoteModuleName = remoteName + data[1].slice(1); | ||
return webpackRequire.federation.instance.loadRemote(remoteModuleName, { | ||
loadFactory: false | ||
loadFactory: false, | ||
from: "build" | ||
}); | ||
@@ -82,2 +93,5 @@ } catch (error) { | ||
}; | ||
var useRuntimeLoad = remoteInfos.length === 1 && [ | ||
"script" | ||
].includes(remoteInfos[0].externalType) && remoteInfos[0].name; | ||
if (useRuntimeLoad) { | ||
@@ -92,21 +106,5 @@ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1); | ||
function proxyShareScopeMap(__webpack_require__) { | ||
if (!__webpack_require__.S) { | ||
return; | ||
} | ||
// @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error | ||
__webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, { | ||
get: function get(target, prop, receiver) { | ||
return globalThis.__VMOK__.__SHARE__[prop]; | ||
}, | ||
set: function set(target, prop, value) { | ||
globalThis.__VMOK__.__SHARE__[prop] = value; | ||
return true; | ||
} | ||
}); | ||
} | ||
function consumes(options) { | ||
var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire; | ||
proxyShareScopeMap(webpackRequire); | ||
attachShareScopeMap(webpackRequire); | ||
if (webpackRequire.o(chunkMapping, chunkId)) { | ||
@@ -156,4 +154,57 @@ chunkMapping[chunkId].forEach(function(id) { | ||
function initializeSharing(shareScopeName, webpackRequire) { | ||
return webpackRequire.federation.instance.initializeSharing(shareScopeName); | ||
function initializeSharing(param) { | ||
var shareScopeName = param.shareScopeName, webpackRequire = param.webpackRequire, initPromises = param.initPromises, initTokens = param.initTokens, initScope = param.initScope; | ||
if (!initScope) initScope = []; | ||
// handling circular init calls | ||
var initToken = initTokens[shareScopeName]; | ||
if (!initToken) initToken = initTokens[shareScopeName] = {}; | ||
if (initScope.indexOf(initToken) >= 0) return; | ||
initScope.push(initToken); | ||
var promise = initPromises[shareScopeName]; | ||
if (promise) return promise; | ||
var warn = function(msg) { | ||
return typeof console !== "undefined" && console.warn && console.warn(msg); | ||
}; | ||
var initExternal = function(id) { | ||
var handleError = function(err) { | ||
return warn("Initialization of sharing external failed: " + err); | ||
}; | ||
try { | ||
var module = webpackRequire(id); | ||
if (!module) return; | ||
var initFn = function(module) { | ||
return module && module.init && module.init(webpackRequire.S[shareScopeName], initScope); | ||
}; | ||
if (module.then) return promises.push(module.then(initFn, handleError)); | ||
var initResult = initFn(module); | ||
// @ts-ignore | ||
if (initResult && typeof initResult !== "boolean" && initResult.then) // @ts-ignore | ||
return promises.push(initResult["catch"](handleError)); | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
}; | ||
var promises = webpackRequire.federation.instance.initializeSharing(shareScopeName); | ||
attachShareScopeMap(webpackRequire); | ||
var bundlerRuntimeRemotesOptions = webpackRequire.federation.bundlerRuntimeOptions.remotes; | ||
if (bundlerRuntimeRemotesOptions) { | ||
Object.keys(bundlerRuntimeRemotesOptions.idToRemoteMap).forEach(function(moduleId) { | ||
var info = bundlerRuntimeRemotesOptions.idToRemoteMap[moduleId]; | ||
var externalModuleId = bundlerRuntimeRemotesOptions.idToExternalAndNameMapping[moduleId][2]; | ||
if (info.length > 1) { | ||
initExternal(externalModuleId); | ||
} else if (info.length === 1) { | ||
var remoteInfo = info[0]; | ||
if (!FEDERATION_SUPPORTED_TYPES.includes(remoteInfo.externalType)) { | ||
initExternal(externalModuleId); | ||
} | ||
} | ||
}); | ||
} | ||
if (!promises.length) { | ||
return initPromises[shareScopeName] = true; | ||
} | ||
return initPromises[shareScopeName] = Promise.all(promises).then(function() { | ||
return initPromises[shareScopeName] = true; | ||
}); | ||
} | ||
@@ -193,2 +244,36 @@ | ||
function isLegacyHost(shareScope) { | ||
if ("version" in shareScope && typeof shareScope["version"] !== "object") { | ||
return true; | ||
} | ||
if ("region" in shareScope && typeof shareScope["region"] !== "object") { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function initContainerEntry(options) { | ||
var webpackRequire = options.webpackRequire, shareScope = options.shareScope, initScope = options.initScope, shareScopeKey = options.shareScopeKey; | ||
if (!webpackRequire.S) return; | ||
if (!webpackRequire.federation || !webpackRequire.federation.instance || !webpackRequire.federation.initOptions) return; | ||
var name = shareScopeKey || "default"; | ||
webpackRequire.federation.instance.initOptions({ | ||
name: webpackRequire.federation.initOptions.name, | ||
remotes: [] | ||
}); | ||
if (isLegacyHost(shareScope)) { | ||
var prevShareScope = globalThis.__FEDERATION__.__SHARE__["default"]; | ||
if (prevShareScope) { | ||
webpackRequire.federation.instance.initShareScopeMap(name, prevShareScope); | ||
} | ||
} else { | ||
webpackRequire.federation.instance.initShareScopeMap(name, shareScope); | ||
} | ||
webpackRequire.S[name] = shareScope; | ||
if (webpackRequire.federation.attachShareScopeMap) { | ||
webpackRequire.federation.attachShareScopeMap(webpackRequire); | ||
} | ||
// @ts-ignore | ||
return webpackRequire.I(name, initScope); | ||
} | ||
var federation = { | ||
@@ -203,6 +288,9 @@ runtime: runtime, | ||
S: {}, | ||
installInitialConsumes: installInitialConsumes | ||
} | ||
installInitialConsumes: installInitialConsumes, | ||
initContainerEntry: initContainerEntry | ||
}, | ||
attachShareScopeMap: attachShareScopeMap, | ||
bundlerRuntimeOptions: {} | ||
}; | ||
export { federation as default }; |
@@ -21,6 +21,28 @@ { | ||
"module": "./index.esm.js", | ||
"types": "./index.cjs.d.ts", | ||
"types": "./dist/index.cjs.d.ts", | ||
"dependencies": { | ||
"@module-federation/runtime": "workspace:*" | ||
"@module-federation/runtime": "workspace:*", | ||
"@module-federation/sdk": "workspace:*" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.cjs.js" | ||
}, | ||
"./constant": { | ||
"import": "./dist/constant.esm.js", | ||
"require": "./dist/constant.cjs.js" | ||
}, | ||
"./*": "./*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
".": [ | ||
"./dist/index.cjs.d.ts" | ||
], | ||
"constant": [ | ||
"./dist/constant.cjs.d.ts" | ||
] | ||
} | ||
} | ||
} |
import { Federation } from './types'; | ||
export * from './types'; | ||
declare const federation: Federation; | ||
export default federation; |
@@ -1,2 +0,2 @@ | ||
import { WebpackRequire } from './types'; | ||
export declare function initializeSharing(shareScopeName: string, webpackRequire: WebpackRequire): Promise<boolean> | boolean; | ||
import { InitializeSharingOptions } from './types'; | ||
export declare function initializeSharing({ shareScopeName, webpackRequire, initPromises, initTokens, initScope, }: InitializeSharingOptions): Promise<boolean> | boolean | void; |
import * as runtime from '@module-federation/runtime'; | ||
import { initializeSharing } from './initializeSharing'; | ||
import { attachShareScopeMap } from './attachShareScopeMap'; | ||
import { initContainerEntry } from './initContainerEntry'; | ||
type ExcludeUndefined<T> = T extends undefined ? never : T; | ||
@@ -11,2 +13,11 @@ type NonUndefined<T = Shared> = ExcludeUndefined<T>; | ||
type InferredModule = InferModule<ModuleCache>; | ||
export type ShareScopeMap = runtime.FederationHost['shareScopeMap']; | ||
type InitToken = Record<string, Record<string, any>>; | ||
export interface InitializeSharingOptions { | ||
shareScopeName: string; | ||
webpackRequire: WebpackRequire; | ||
initPromises: Record<string, Promise<boolean> | boolean>; | ||
initTokens: InitToken; | ||
initScope: InitToken[]; | ||
} | ||
export type RemoteEntryExports = NonUndefined<InferredModule['remoteEntryExports']>; | ||
@@ -20,9 +31,3 @@ type ExtractInitParameters<T> = T extends { | ||
}; | ||
type IdToExternalAndNameMappingItem = [ | ||
string, | ||
string, | ||
string | number, | ||
string, | ||
string | ||
]; | ||
type IdToExternalAndNameMappingItem = [string, string, string | number]; | ||
interface IdToExternalAndNameMappingItemWithPromise extends IdToExternalAndNameMappingItem { | ||
@@ -37,3 +42,3 @@ p?: Promise<any> | number; | ||
c: Record<string, any>; | ||
I: typeof initializeSharing; | ||
I: (scopeName: string, initScope?: InitializeSharingOptions['initScope']) => ReturnType<typeof initializeSharing>; | ||
S?: InferredGlobalShareScope; | ||
@@ -51,2 +56,7 @@ federation: Federation; | ||
} | ||
interface IdToRemoteMapItem { | ||
externalType: string; | ||
name: string; | ||
externalModuleId?: string | number; | ||
} | ||
export interface RemotesOptions { | ||
@@ -57,2 +67,3 @@ chunkId: string | number; | ||
idToExternalAndNameMapping: Record<string, IdToExternalAndNameMappingItemWithPromise>; | ||
idToRemoteMap: Record<string, IdToRemoteMapItem[]>; | ||
webpackRequire: WebpackRequire; | ||
@@ -79,2 +90,8 @@ } | ||
} | ||
export interface InitContainerEntryOptions { | ||
shareScope: ShareScopeMap[string]; | ||
shareScopeKey: string; | ||
webpackRequire: WebpackRequire; | ||
initScope?: InitializeSharingOptions['initScope']; | ||
} | ||
export interface Federation { | ||
@@ -88,7 +105,13 @@ runtime?: typeof runtime; | ||
consumes: (options: ConsumesOptions) => void; | ||
I: (name: string, webpackRequire: WebpackRequire) => Promise<boolean> | boolean; | ||
I: typeof initializeSharing; | ||
S: InferredGlobalShareScope; | ||
installInitialConsumes: (options: InstallInitialConsumesOptions) => any; | ||
initContainerEntry: typeof initContainerEntry; | ||
}; | ||
bundlerRuntimeOptions: { | ||
remotes?: Exclude<RemotesOptions, 'chunkId' | 'promises'>; | ||
}; | ||
attachShareScopeMap?: typeof attachShareScopeMap; | ||
hasAttachShareScopeMap?: boolean; | ||
} | ||
export {}; |
{ | ||
"public": true, | ||
"name": "@module-federation/webpack-bundler-runtime", | ||
"version": "0.0.0-next-20240102023857", | ||
"version": "0.0.0-next-20240102063242", | ||
"license": "MIT", | ||
@@ -19,8 +19,30 @@ "description": "Module Federation Runtime for webpack", | ||
"author": "zhanghang <hanric.zhang@gmail.com>", | ||
"main": "./index.cjs.js", | ||
"module": "./index.esm.js", | ||
"types": "./index.cjs.d.ts", | ||
"main": "./dist/index.cjs.js", | ||
"module": "./dist/index.esm.js", | ||
"types": "./dist/index.cjs.d.ts", | ||
"dependencies": { | ||
"@module-federation/runtime": "0.0.0-next-20240102023857" | ||
"@module-federation/runtime": "0.0.0-next-20240102063242", | ||
"@module-federation/sdk": "0.0.0-next-20240102063242" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.cjs.js" | ||
}, | ||
"./constant": { | ||
"import": "./dist/constant.esm.js", | ||
"require": "./dist/constant.cjs.js" | ||
}, | ||
"./*": "./*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
".": [ | ||
"./dist/index.cjs.d.ts" | ||
], | ||
"constant": [ | ||
"./dist/constant.cjs.d.ts" | ||
] | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36309
21
724
2
+ Added@module-federation/runtime@0.0.0-next-20240102063242(transitive)
+ Added@module-federation/sdk@0.0.0-next-20240102063242(transitive)
- Removed@module-federation/runtime@0.0.0-next-20240102023857(transitive)
- Removed@module-federation/sdk@0.0.0-next-20240102023857(transitive)