@module-federation/runtime
Advanced tools
Comparing version 0.0.0-next-20241106063644 to 0.0.0-next-20241106104434
@@ -91,2 +91,5 @@ 'use strict'; | ||
} | ||
get bridgeHook() { | ||
return this._getInstance().bridgeHook; | ||
} | ||
initOptions(...args) { | ||
@@ -93,0 +96,0 @@ return this._getInstance().initOptions(...args); |
'use strict'; | ||
var share = require('./share.cjs.js'); | ||
require('@module-federation/sdk'); | ||
require('@module-federation/error-codes'); | ||
var pluginHelper = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
AsyncHook: share.AsyncHook, | ||
AsyncWaterfallHook: share.AsyncWaterfallHook, | ||
PluginSystem: share.PluginSystem, | ||
SyncHook: share.SyncHook, | ||
SyncWaterfallHook: share.SyncWaterfallHook | ||
}); | ||
const ShareUtils = { | ||
@@ -27,3 +38,5 @@ getRegisteredShare: share.getRegisteredShare, | ||
getPreloaded: share.getPreloaded, | ||
setPreloaded: share.setPreloaded | ||
setPreloaded: share.setPreloaded, | ||
registerPlugins: share.registerPlugins, | ||
pluginHelper | ||
}; | ||
@@ -30,0 +43,0 @@ var helpers = { |
'use strict'; | ||
var sdk = require('@module-federation/sdk'); | ||
require('@module-federation/error-codes'); | ||
var polyfills = require('./polyfills.cjs.js'); | ||
var sdk = require('@module-federation/sdk'); | ||
@@ -92,3 +93,18 @@ function getBuilderId() { | ||
} | ||
const processModuleAlias = (name, subPath)=>{ | ||
// @host/ ./button -> @host/button | ||
let moduleName; | ||
if (name.endsWith('/')) { | ||
moduleName = name.slice(0, -1); | ||
} else { | ||
moduleName = name; | ||
} | ||
if (subPath.startsWith('.')) { | ||
subPath = subPath.slice(1); | ||
} | ||
moduleName = moduleName + subPath; | ||
return moduleName; | ||
}; | ||
const CurrentGlobal = typeof globalThis === 'object' ? globalThis : window; | ||
const nativeGlobal = (()=>{ | ||
@@ -100,3 +116,3 @@ try { | ||
// node env | ||
return globalThis; | ||
return CurrentGlobal; | ||
} | ||
@@ -118,6 +134,6 @@ })(); | ||
// If there is no loading content on the global object, this section defines the loading object. | ||
if (!includeOwnProperty(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__')) { | ||
definePropertyGlobalVal(globalThis, '__GLOBAL_LOADING_REMOTE_ENTRY__', {}); | ||
if (!includeOwnProperty(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__')) { | ||
definePropertyGlobalVal(CurrentGlobal, '__GLOBAL_LOADING_REMOTE_ENTRY__', {}); | ||
} | ||
const globalLoading = globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__; | ||
const globalLoading = CurrentGlobal.__GLOBAL_LOADING_REMOTE_ENTRY__; | ||
function setGlobalDefaultVal(target) { | ||
@@ -152,14 +168,14 @@ var _target___FEDERATION__, _target___FEDERATION__1, _target___FEDERATION__2, _target___FEDERATION__3, _target___FEDERATION__4, _target___FEDERATION__5; | ||
} | ||
setGlobalDefaultVal(globalThis); | ||
setGlobalDefaultVal(CurrentGlobal); | ||
setGlobalDefaultVal(nativeGlobal); | ||
function resetFederationGlobalInfo() { | ||
globalThis.__FEDERATION__.__GLOBAL_PLUGIN__ = []; | ||
globalThis.__FEDERATION__.__INSTANCES__ = []; | ||
globalThis.__FEDERATION__.moduleInfo = {}; | ||
globalThis.__FEDERATION__.__SHARE__ = {}; | ||
globalThis.__FEDERATION__.__MANIFEST_LOADING__ = {}; | ||
CurrentGlobal.__FEDERATION__.__GLOBAL_PLUGIN__ = []; | ||
CurrentGlobal.__FEDERATION__.__INSTANCES__ = []; | ||
CurrentGlobal.__FEDERATION__.moduleInfo = {}; | ||
CurrentGlobal.__FEDERATION__.__SHARE__ = {}; | ||
CurrentGlobal.__FEDERATION__.__MANIFEST_LOADING__ = {}; | ||
} | ||
function getGlobalFederationInstance(name, version) { | ||
const buildId = getBuilderId(); | ||
return globalThis.__FEDERATION__.__INSTANCES__.find((GMInstance)=>{ | ||
return CurrentGlobal.__FEDERATION__.__INSTANCES__.find((GMInstance)=>{ | ||
if (buildId && GMInstance.options.id === getBuilderId()) { | ||
@@ -178,11 +194,11 @@ return true; | ||
function setGlobalFederationInstance(FederationInstance) { | ||
globalThis.__FEDERATION__.__INSTANCES__.push(FederationInstance); | ||
CurrentGlobal.__FEDERATION__.__INSTANCES__.push(FederationInstance); | ||
} | ||
function getGlobalFederationConstructor() { | ||
return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__; | ||
return CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__; | ||
} | ||
function setGlobalFederationConstructor(FederationConstructor, isDebug = sdk.isDebugMode()) { | ||
if (isDebug) { | ||
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor; | ||
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.7.0"; | ||
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor; | ||
CurrentGlobal.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.7.0"; | ||
} | ||
@@ -263,3 +279,3 @@ } | ||
const remoteEntryKey = globalName || `__FEDERATION_${name}:custom__`; | ||
const entryExports = globalThis[remoteEntryKey]; | ||
const entryExports = CurrentGlobal[remoteEntryKey]; | ||
return { | ||
@@ -285,8 +301,213 @@ remoteEntryKey, | ||
const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__; | ||
const getPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id); | ||
const setPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true); | ||
const getPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.get(id); | ||
const setPreloaded = (id)=>CurrentGlobal.__FEDERATION__.__PRELOADED_MAP__.set(id, true); | ||
function registerPlugins(plugins, hookInstances) { | ||
const globalPlugins = getGlobalHostPlugins(); | ||
// Incorporate global plugins | ||
if (globalPlugins.length > 0) { | ||
globalPlugins.forEach((plugin)=>{ | ||
if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) { | ||
plugins.push(plugin); | ||
} | ||
}); | ||
} | ||
if (plugins && plugins.length > 0) { | ||
plugins.forEach((plugin)=>{ | ||
hookInstances.forEach((hookInstance)=>{ | ||
hookInstance.applyPlugin(plugin); | ||
}); | ||
}); | ||
} | ||
return plugins; | ||
} | ||
const DEFAULT_SCOPE = 'default'; | ||
const DEFAULT_REMOTE_TYPE = 'global'; | ||
class SyncHook { | ||
on(fn) { | ||
if (typeof fn === 'function') { | ||
this.listeners.add(fn); | ||
} | ||
} | ||
once(fn) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const self = this; | ||
this.on(function wrapper(...args) { | ||
self.remove(wrapper); | ||
// eslint-disable-next-line prefer-spread | ||
return fn.apply(null, args); | ||
}); | ||
} | ||
emit(...data) { | ||
let result; | ||
if (this.listeners.size > 0) { | ||
// eslint-disable-next-line prefer-spread | ||
this.listeners.forEach((fn)=>{ | ||
result = fn(...data); | ||
}); | ||
} | ||
return result; | ||
} | ||
remove(fn) { | ||
this.listeners.delete(fn); | ||
} | ||
removeAll() { | ||
this.listeners.clear(); | ||
} | ||
constructor(type){ | ||
this.type = ''; | ||
this.listeners = new Set(); | ||
if (type) { | ||
this.type = type; | ||
} | ||
} | ||
} | ||
class AsyncHook extends SyncHook { | ||
emit(...data) { | ||
let result; | ||
const ls = Array.from(this.listeners); | ||
if (ls.length > 0) { | ||
let i = 0; | ||
const call = (prev)=>{ | ||
if (prev === false) { | ||
return false; // Abort process | ||
} else if (i < ls.length) { | ||
return Promise.resolve(ls[i++].apply(null, data)).then(call); | ||
} else { | ||
return prev; | ||
} | ||
}; | ||
result = call(); | ||
} | ||
return Promise.resolve(result); | ||
} | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function checkReturnData(originalData, returnedData) { | ||
if (!isObject(returnedData)) { | ||
return false; | ||
} | ||
if (originalData !== returnedData) { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for(const key in originalData){ | ||
if (!(key in returnedData)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
class SyncWaterfallHook extends SyncHook { | ||
emit(data) { | ||
if (!isObject(data)) { | ||
error(`The data for the "${this.type}" hook should be an object.`); | ||
} | ||
for (const fn of this.listeners){ | ||
try { | ||
const tempData = fn(data); | ||
if (checkReturnData(data, tempData)) { | ||
data = tempData; | ||
} else { | ||
this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`); | ||
break; | ||
} | ||
} catch (e) { | ||
warn(e); | ||
this.onerror(e); | ||
} | ||
} | ||
return data; | ||
} | ||
constructor(type){ | ||
super(), this.onerror = error; | ||
this.type = type; | ||
} | ||
} | ||
class AsyncWaterfallHook extends SyncHook { | ||
emit(data) { | ||
if (!isObject(data)) { | ||
error(`The response data for the "${this.type}" hook must be an object.`); | ||
} | ||
const ls = Array.from(this.listeners); | ||
if (ls.length > 0) { | ||
let i = 0; | ||
const processError = (e)=>{ | ||
warn(e); | ||
this.onerror(e); | ||
return data; | ||
}; | ||
const call = (prevData)=>{ | ||
if (checkReturnData(data, prevData)) { | ||
data = prevData; | ||
if (i < ls.length) { | ||
try { | ||
return Promise.resolve(ls[i++](data)).then(call, processError); | ||
} catch (e) { | ||
return processError(e); | ||
} | ||
} | ||
} else { | ||
this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`); | ||
} | ||
return data; | ||
}; | ||
return Promise.resolve(call(data)); | ||
} | ||
return Promise.resolve(data); | ||
} | ||
constructor(type){ | ||
super(), this.onerror = error; | ||
this.type = type; | ||
} | ||
} | ||
class PluginSystem { | ||
applyPlugin(plugin) { | ||
assert(isPlainObject(plugin), 'Plugin configuration is invalid.'); | ||
// The plugin's name is mandatory and must be unique | ||
const pluginName = plugin.name; | ||
assert(pluginName, 'A name must be provided by the plugin.'); | ||
if (!this.registerPlugins[pluginName]) { | ||
this.registerPlugins[pluginName] = plugin; | ||
Object.keys(this.lifecycle).forEach((key)=>{ | ||
const pluginLife = plugin[key]; | ||
if (pluginLife) { | ||
this.lifecycle[key].on(pluginLife); | ||
} | ||
}); | ||
} | ||
} | ||
removePlugin(pluginName) { | ||
assert(pluginName, 'A name is required.'); | ||
const plugin = this.registerPlugins[pluginName]; | ||
assert(plugin, `The plugin "${pluginName}" is not registered.`); | ||
Object.keys(plugin).forEach((key)=>{ | ||
if (key !== 'name') { | ||
this.lifecycle[key].remove(plugin[key]); | ||
} | ||
}); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
inherit({ lifecycle, registerPlugins }) { | ||
Object.keys(lifecycle).forEach((hookName)=>{ | ||
assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`); | ||
this.lifecycle[hookName] = lifecycle[hookName]; | ||
}); | ||
Object.keys(registerPlugins).forEach((pluginName)=>{ | ||
assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`); | ||
this.applyPlugin(registerPlugins[pluginName]); | ||
}); | ||
} | ||
constructor(lifecycle){ | ||
this.registerPlugins = {}; | ||
this.lifecycle = lifecycle; | ||
this.lifecycleKeys = Object.keys(lifecycle); | ||
} | ||
} | ||
// fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts | ||
@@ -887,5 +1108,11 @@ // those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant | ||
exports.AsyncHook = AsyncHook; | ||
exports.AsyncWaterfallHook = AsyncWaterfallHook; | ||
exports.CurrentGlobal = CurrentGlobal; | ||
exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE; | ||
exports.DEFAULT_SCOPE = DEFAULT_SCOPE; | ||
exports.Global = Global; | ||
exports.PluginSystem = PluginSystem; | ||
exports.SyncHook = SyncHook; | ||
exports.SyncWaterfallHook = SyncWaterfallHook; | ||
exports.addGlobalSnapshot = addGlobalSnapshot; | ||
@@ -913,4 +1140,2 @@ exports.addUniqueItem = addUniqueItem; | ||
exports.globalLoading = globalLoading; | ||
exports.isObject = isObject; | ||
exports.isPlainObject = isPlainObject; | ||
exports.isPureRemoteEntry = isPureRemoteEntry; | ||
@@ -920,3 +1145,5 @@ exports.isRemoteInfoWithEntry = isRemoteInfoWithEntry; | ||
exports.nativeGlobal = nativeGlobal; | ||
exports.processModuleAlias = processModuleAlias; | ||
exports.registerGlobalPlugins = registerGlobalPlugins; | ||
exports.registerPlugins = registerPlugins; | ||
exports.resetFederationGlobalInfo = resetFederationGlobalInfo; | ||
@@ -927,2 +1154,1 @@ exports.setGlobalFederationConstructor = setGlobalFederationConstructor; | ||
exports.setPreloaded = setPreloaded; | ||
exports.warn = warn; |
@@ -69,2 +69,8 @@ import type { CreateScriptHookReturn, ModuleInfo } from '@module-federation/sdk'; | ||
}>; | ||
bridgeHook: PluginSystem<{ | ||
beforeBridgeRender: SyncHook<[Record<string, any>], any>; | ||
afterBridgeRender: SyncHook<[Record<string, any>], any>; | ||
beforeBridgeDestroy: SyncHook<[Record<string, any>], any>; | ||
afterBridgeDestroy: SyncHook<[Record<string, any>], any>; | ||
}>; | ||
constructor(userOptions: UserOptions); | ||
@@ -71,0 +77,0 @@ initOptions(userOptions: UserOptions): Options; |
@@ -81,2 +81,8 @@ import type * as IndexModule from './index'; | ||
}>; | ||
get bridgeHook(): import("./utils/hooks").PluginSystem<{ | ||
beforeBridgeRender: import("./utils/hooks").SyncHook<[Record<string, any>], any>; | ||
afterBridgeRender: import("./utils/hooks").SyncHook<[Record<string, any>], any>; | ||
beforeBridgeDestroy: import("./utils/hooks").SyncHook<[Record<string, any>], any>; | ||
afterBridgeDestroy: import("./utils/hooks").SyncHook<[Record<string, any>], any>; | ||
}>; | ||
initOptions(...args: Parameters<IndexModule.FederationHost['initOptions']>): import("./type").Options; | ||
@@ -83,0 +89,0 @@ loadShare<T>(...args: Parameters<IndexModule.FederationHost['loadShare']>): Promise<false | (() => T | undefined)>; |
@@ -15,2 +15,3 @@ import { FederationHost } from './core'; | ||
} | ||
export declare const CurrentGlobal: typeof globalThis; | ||
export declare const nativeGlobal: typeof global; | ||
@@ -17,0 +18,0 @@ export declare const Global: typeof globalThis; |
import { resetFederationGlobalInfo, getGlobalFederationInstance, setGlobalFederationInstance, getGlobalFederationConstructor, setGlobalFederationConstructor, getInfoWithoutType, getGlobalSnapshot, getTargetSnapshotInfoByModuleInfo, getGlobalSnapshotInfoByModuleInfo, setGlobalSnapshotInfoByModuleInfo, addGlobalSnapshot, getRemoteEntryExports, registerGlobalPlugins, getGlobalHostPlugins, getPreloaded, setPreloaded, Global } from './global'; | ||
import { getRegisteredShare, getGlobalShareScope } from './utils/share'; | ||
import * as pluginHelper from './utils/hooks'; | ||
import { registerPlugins } from './utils'; | ||
interface IShareUtils { | ||
@@ -26,2 +28,4 @@ getRegisteredShare: typeof getRegisteredShare; | ||
setPreloaded: typeof setPreloaded; | ||
registerPlugins: typeof registerPlugins; | ||
pluginHelper: typeof pluginHelper; | ||
} | ||
@@ -28,0 +32,0 @@ declare const _default: { |
@@ -35,2 +35,7 @@ import { GlobalModuleInfo, Manifest, ModuleInfo } from '@module-federation/sdk'; | ||
}>; | ||
afterLoadSnapshot: AsyncWaterfallHook<{ | ||
options: Options; | ||
moduleInfo: Remote; | ||
remoteSnapshot: ModuleInfo; | ||
}>; | ||
}>; | ||
@@ -37,0 +42,0 @@ loaderHook: FederationHost['loaderHook']; |
@@ -7,3 +7,3 @@ import type { RemoteWithEntry, RemoteWithVersion, Module, RemoteEntryType } from '@module-federation/sdk'; | ||
}; | ||
interface RemoteInfoCommon { | ||
export interface RemoteInfoCommon { | ||
alias?: string; | ||
@@ -10,0 +10,0 @@ shareScope?: string; |
@@ -26,6 +26,7 @@ import { FederationHost } from '../core'; | ||
}>; | ||
export type FederationRuntimePlugin = CoreLifeCyclePartial & SnapshotLifeCycleCyclePartial & SharedLifeCycleCyclePartial & RemoteLifeCycleCyclePartial & ModuleLifeCycleCyclePartial & { | ||
type LifeCycle = CoreLifeCyclePartial & SnapshotLifeCycleCyclePartial & SharedLifeCycleCyclePartial & RemoteLifeCycleCyclePartial & ModuleLifeCycleCyclePartial & { | ||
name: string; | ||
version?: string; | ||
}; | ||
export type FederationRuntimePlugin<T = LifeCycle> = LifeCycle & T; | ||
export {}; |
import { FederationHost } from '../core'; | ||
import { UserOptions } from '../type'; | ||
import { Module } from '../module'; | ||
export declare function registerPlugins(plugins: UserOptions['plugins'], hookInstances: Array<FederationHost['hooks'] | FederationHost['snapshotHandler']['hooks'] | FederationHost['sharedHandler']['hooks'] | FederationHost['remoteHandler']['hooks'] | Module['host']['loaderHook']>): import("../type").FederationRuntimePlugin[] | undefined; | ||
import { PluginSystem } from './hooks'; | ||
export declare function registerPlugins<Y extends Record<string, any>, T extends PluginSystem<Y>>(plugins: UserOptions['plugins'], hookInstances: Array<T | FederationHost['hooks'] | FederationHost['snapshotHandler']['hooks'] | FederationHost['sharedHandler']['hooks'] | FederationHost['remoteHandler']['hooks'] | Module['host']['loaderHook'] | Module['host']['bridgeHook']>): import("../type").FederationRuntimePlugin[] | undefined; |
@@ -18,1 +18,2 @@ import { RemoteWithEntry, ModuleInfo, RemoteEntryType } from '@module-federation/sdk'; | ||
}; | ||
export declare const processModuleAlias: (name: string, subPath: string) => string; |
{ | ||
"name": "@module-federation/runtime", | ||
"version": "0.0.0-next-20241106063644", | ||
"version": "0.0.0-next-20241106104434", | ||
"author": "zhouxiao <codingzx@gmail.com>", | ||
@@ -53,5 +53,5 @@ "main": "./dist/index.cjs.js", | ||
"dependencies": { | ||
"@module-federation/sdk": "0.0.0-next-20241106063644", | ||
"@module-federation/error-codes": "0.0.0-next-20241106063644" | ||
"@module-federation/sdk": "0.0.0-next-20241106104434", | ||
"@module-federation/error-codes": "0.0.0-next-20241106104434" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
328922
7675
+ Added@module-federation/error-codes@0.0.0-next-20241106104434(transitive)
+ Added@module-federation/sdk@0.0.0-next-20241106104434(transitive)
- Removed@module-federation/error-codes@0.0.0-next-20241106063644(transitive)
- Removed@module-federation/sdk@0.0.0-next-20241106063644(transitive)
Updated@module-federation/error-codes@0.0.0-next-20241106104434