Skip to content
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 9 commits into from
Nov 20, 2024

Conversation

danpeen
Copy link
Contributor

@danpeen danpeen commented Nov 15, 2024

Description

feat: mount bridge api to module instance

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Copy link

changeset-bot bot commented Nov 15, 2024

🦋 Changeset detected

Latest commit: 3f767c5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@module-federation/bridge-react Patch
@module-federation/runtime Patch
@module-federation/enhanced Patch
@module-federation/rspack Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/sdk Patch
@module-federation/runtime-tools Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/modern-js Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/rsbuild-plugin Patch
@module-federation/error-codes Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/storybook-addon Patch
@module-federation/modernjsapp Patch
@module-federation/esbuild Patch
@module-federation/utilities Patch
website-new Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Nov 15, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 3f767c5
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/673d584d8a2d2f0008a95d2c
😎 Deploy Preview https://deploy-preview-3234--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This pull request introduces a new feature to the Bridge React Plugin that allows for better integration and management of the bridge API within the module instance. The key changes include:

  • A new createLazyRemoteComponent function that creates a React.lazy component to dynamically import remote modules and handle loading and error states.
  • The createRemoteComponent function has been updated to use the LazyRemoteComponentInfo type, simplifying the function signature.
  • The introduction of a new BridgeReactPlugin that stores the FederationHost instance in a global federationRuntime object, making it accessible to other parts of the application.
  • The BridgeReactPlugin now includes a federationRuntime property to access the federation instance, enabling better integration with the bridge API.
  • The remote/index.tsx file has been updated to mount the bridge API to the module instance, including lifecycle hooks for beforeBridgeRender, afterBridgeRender, beforeBridgeDestroy, and afterBridgeDestroy to handle the rendering and destruction of the remote component.

These changes aim to improve the overall functionality and integration of the bridge API within the module instance, providing a more seamless and efficient development experience.

File Summaries
File Summary
packages/bridge/bridge-react/src/create.tsx The code changes introduce a new function createLazyRemoteComponent that takes in an object with properties loader, loading, fallback, and an optional export. This function creates a React.lazy component that dynamically imports the remote module and handles loading and error states. Additionally, the createRemoteComponent function is updated to use the LazyRemoteComponentInfo type, simplifying the function signature.
packages/bridge/bridge-react/src/plugin.ts This code introduces a new plugin called BridgeReactPlugin that is designed to work with the @module-federation/runtime library. The plugin's primary purpose is to store the FederationHost instance in a global federationRuntime object, which can be accessed by other parts of the application.
packages/bridge/bridge-react/src/provider.tsx The code changes introduce a new feature to the BridgeReactPlugin by adding a federationRuntime property to access the federation instance. This allows for better integration and management of the bridge API within the module instance.
packages/bridge/bridge-react/src/remote/index.tsx The code changes introduce a new feature to mount the bridge API to the module instance. The key modifications include replacing the getInstance() function with the federationRuntime.instance to access the federation instance, and adding lifecycle hooks for beforeBridgeRender, afterBridgeRender, beforeBridgeDestroy, and afterBridgeDestroy to handle the rendering and destruction of the remote component.

@danpeen danpeen changed the title feat: mount bridge api to module instance feat: mount bridge api to host instance Nov 15, 2024
Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incremental Review

Comments posted: 8

Configuration

Squadron Mode: essential

Commits Reviewed

9bff805fe24953c5368a4eecce08fcea3f0bd9c0...9b011959e29a4afbdb1ac4d6b8429ab870ae9364

Files Reviewed
  • packages/bridge/bridge-react/src/create.tsx
  • packages/bridge/bridge-react/src/plugin.ts
  • packages/bridge/bridge-react/src/provider.tsx
  • packages/bridge/bridge-react/src/remote/index.tsx
  • packages/bridge/bridge-react/src/index.ts
Files Ignored

These files were ignored due to the filter in the squadron.yaml file.

  • .changeset/small-bats-leave.md
  • apps/router-demo/router-host-2000/src/App.tsx
  • apps/router-demo/router-remote1-2001/rsbuild.config.ts
  • packages/bridge/bridge-react/package.json
  • packages/bridge/bridge-react/vite.config.ts
  • pnpm-lock.yaml

Comment on lines 120 to 125
export function createRemoteComponentWithInstance<T, E extends keyof T>(
instance: FederationHost,
) {
return (info: LazyRemoteComponentInfo<T, E>) => {
return createLazyRemoteComponent({ ...info, instance });
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name createRemoteComponentWithInstance could be more specific about its purpose. Consider adding TypeScript parameter documentation to explain what T and E represent, and what the function returns. Here's a suggested improvement:

Suggested change
export function createRemoteComponentWithInstance<T, E extends keyof T>(
instance: FederationHost,
) {
return (info: LazyRemoteComponentInfo<T, E>) => {
return createLazyRemoteComponent({ ...info, instance });
};
/**
* Creates a remote component factory using a specific Federation Host instance
* @param instance - The Federation Host instance to use for component loading
* @returns A function that creates lazy-loaded remote components
* @template T - The module's exports type
* @template E - The specific export key from the module
*/
export function createRemoteComponentWithInstance<T, E extends keyof T>(
instance: FederationHost,
) {
return (info: LazyRemoteComponentInfo<T, E>) => {
return createLazyRemoteComponent({ ...info, instance });
};
}

Comment on lines 123 to 124
return (info: LazyRemoteComponentInfo<T, E>) => {
return createLazyRemoteComponent({ ...info, instance });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function could benefit from error handling for invalid input. Consider validating the instance parameter and adding error handling:

Suggested change
return (info: LazyRemoteComponentInfo<T, E>) => {
return createLazyRemoteComponent({ ...info, instance });
return (info: LazyRemoteComponentInfo<T, E>) => {
if (!instance) {
throw new Error('Federation Host instance is required');
}
return createLazyRemoteComponent({ ...info, instance });
};

Comment on lines 14 to 15
type RenderParams = RenderFnParams & {
[key: string]: unknown;
Copy link
Contributor

Choose a reason for hiding this comment

The 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
type RenderParams = RenderFnParams & {
[key: string]: unknown;
type RenderParams = RenderFnParams & {
extraProps?: Record<string, unknown>;
};

For the second code block (lines 134-144):

Comment on lines 138 to 142
export function createBridgeComponentWithInstance<T>(instance: FederationHost) {
return (bridgeInfo: ProviderFnParams<T>) => {
return createBridgeComponent({ ...bridgeInfo, instance });
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createBridgeComponentWithInstance function could benefit from memoization since it creates a new function on each call. Consider using React.useCallback or memoizing the returned function:

Suggested change
export function createBridgeComponentWithInstance<T>(instance: FederationHost) {
return (bridgeInfo: ProviderFnParams<T>) => {
return createBridgeComponent({ ...bridgeInfo, instance });
};
}
export function createBridgeComponentWithInstance<T>(instance: FederationHost) {
return React.useCallback(
(bridgeInfo: ProviderFnParams<T>) => createBridgeComponent({ ...bridgeInfo, instance }),
[instance]
);
}

These suggestions focus on improving type safety and performance while maintaining the code's functionality within their respective commit ranges.

return {
name: 'bridge-react-plugin',
beforeInit(args) {
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the @ts-ignore comment and properly type the args.origin. This improves type safety and maintainability. If origin is expected on args, consider extending the type definition of FederationRuntimePlugin's beforeInit parameters.

Comment on lines 7 to 8
let createRemoteComponent: CreateRemoteComponent;
let createBridgeComponent: CreateBridgeComponent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables are mutable globals which could lead to race conditions and make testing difficult. Consider using a class or module pattern instead. Here's a suggestion:

Suggested change
let createRemoteComponent: CreateRemoteComponent;
let createBridgeComponent: CreateBridgeComponent;
class BridgeReactAPI {
static createRemoteComponent: CreateRemoteComponent;
static createBridgeComponent: CreateBridgeComponent;
}

// @ts-ignore
createRemoteComponent = createRemoteComponentWithInstance(args.origin);
createBridgeComponent = createBridgeComponentWithInstance(args.origin);
return args;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The beforeInit function returns args without any transformation. If no modification is needed, consider documenting why this pass-through is necessary, or remove the return statement if it's not required by the plugin interface.

Comment on lines 22 to 24
export type {
ProviderParams,
RenderFnParams,
Copy link
Contributor

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.

@danpeen danpeen changed the title feat: mount bridge api to host instance feat: add BridgeReactPlugin to get federation instance Nov 20, 2024
@zhoushaw zhoushaw merged commit ae5ee1e into main Nov 20, 2024
16 checks passed
@zhoushaw zhoushaw deleted the feat/connect-bridge-with-runtime branch November 20, 2024 04:22
@2heal1 2heal1 mentioned this pull request Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants