-
-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add BridgeReactPlugin to get federation instance #3234
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7dbc4b
feat: mount bridge api to module instance
danpeen 0a52f84
chore: merge main
danpeen 9bff805
chore: update example to get instance from init api
danpeen 153b377
Merge branch 'main' into feat/connect-bridge-with-runtime
ScriptedAlchemy d8dc59f
feat: update mf/enhance pkg to mf/runtime pkg
danpeen fbbdcbd
chore: megre main
danpeen 9b01195
feat: adjust bridge runtime plugin
danpeen a3674d3
feat: get federation runtime instance
danpeen 3f767c5
chore: update
danpeen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@module-federation/bridge-react': patch | ||
--- | ||
|
||
feat: mount bridge api to module instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; | ||
import { createRemoteComponent } from './create'; | ||
import { createBridgeComponent } from './provider'; | ||
|
||
function BridgeReactPlugin(): FederationRuntimePlugin { | ||
return { | ||
name: 'bridge-react-plugin', | ||
beforeInit(args) { | ||
const originalCreateRemoteComponent = createRemoteComponent; | ||
// @ts-ignore | ||
args.origin.createRemoteComponent = function (info) { | ||
return originalCreateRemoteComponent({ | ||
...info, | ||
instance: args.origin, | ||
}); | ||
}; | ||
|
||
const originalCreateBridgeComponentt = createBridgeComponent; // 保存原始函数 | ||
// @ts-ignore | ||
args.origin.createBridgeComponent = function (info) { | ||
return originalCreateBridgeComponentt({ | ||
...info, | ||
instance: args.origin, | ||
}); | ||
}; | ||
return args; | ||
}, | ||
}; | ||
} | ||
|
||
export default BridgeReactPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ import type { | |||||||||||
import { ErrorBoundary } from 'react-error-boundary'; | ||||||||||||
import { RouterContext } from './context'; | ||||||||||||
import { LoggerInstance, atLeastReact18 } from './utils'; | ||||||||||||
import { getInstance } from '@module-federation/runtime'; | ||||||||||||
import type { FederationHost } from '@module-federation/enhanced/runtime'; | ||||||||||||
|
||||||||||||
type RenderParams = RenderFnParams & { | ||||||||||||
[key: string]: unknown; | ||||||||||||
Comment on lines
14
to
15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The RenderParams type extends RenderFnParams but allows arbitrary additional properties through an index signature. This could lead to type safety issues. Consider explicitly defining expected properties or using a more specific type:
Suggested change
For the second code block (lines 134-144): |
||||||||||||
|
@@ -26,13 +26,17 @@ type ProviderFnParams<T> = { | |||||||||||
App: React.ReactElement, | ||||||||||||
id?: HTMLElement | string, | ||||||||||||
) => RootType | Promise<RootType>; | ||||||||||||
instance?: FederationHost; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) { | ||||||||||||
return () => { | ||||||||||||
const rootMap = new Map<any, RootType>(); | ||||||||||||
const instance = getInstance(); | ||||||||||||
LoggerInstance.log(`createBridgeComponent remote instance`, instance); | ||||||||||||
const { instance } = bridgeInfo; | ||||||||||||
LoggerInstance.log( | ||||||||||||
`createBridgeComponent instance from props >>>`, | ||||||||||||
instance, | ||||||||||||
); | ||||||||||||
|
||||||||||||
const RawComponent = (info: { propsInfo: T; appInfo: ProviderParams }) => { | ||||||||||||
const { appInfo, propsInfo, ...restProps } = info; | ||||||||||||
|
@@ -95,15 +99,13 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) { | |||||||||||
const renderFn = bridgeInfo?.render || ReactDOM.render; | ||||||||||||
renderFn?.(rootComponentWithErrorBoundary, info.dom); | ||||||||||||
} | ||||||||||||
|
||||||||||||
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {}; | ||||||||||||
}, | ||||||||||||
|
||||||||||||
async destroy(info: DestroyParams) { | ||||||||||||
LoggerInstance.log(`createBridgeComponent destroy Info`, { | ||||||||||||
dom: info.dom, | ||||||||||||
}); | ||||||||||||
|
||||||||||||
instance?.bridgeHook?.lifecycle?.beforeBridgeDestroy?.emit(info); | ||||||||||||
|
||||||||||||
// call destroy function | ||||||||||||
|
@@ -115,7 +117,9 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) { | |||||||||||
ReactDOM.unmountComponentAtNode(info.dom); | ||||||||||||
} | ||||||||||||
|
||||||||||||
instance?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit(info); | ||||||||||||
(instance as any)?.bridgeHook?.lifecycle?.afterBridgeDestroy?.emit( | ||||||||||||
info, | ||||||||||||
); | ||||||||||||
}, | ||||||||||||
rawComponent: bridgeInfo.rootComponent, | ||||||||||||
__BRIDGE_FN__: (_args: T) => {}, | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type exports appear to be incomplete, ending with a comma suggesting there are more types to export. Ensure all necessary types are properly exported for API completeness and documentation.