Skip to content

Commit

Permalink
refactor(vue3-bridge): added type annotations for the argument in the…
Browse files Browse the repository at this point in the history
… createBridgeComponent
  • Loading branch information
Dell-it committed Nov 30, 2024
1 parent 9b43c60 commit 394cbcd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 36 deletions.
9 changes: 9 additions & 0 deletions .changeset/heavy-flowers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@module-federation/bridge-vue3': patch
---

1. Added type annotations for the argument in the `createBridgeComponent` function.
2. Added passing the instance of the created Vue-application to `appOptions` so that necessary plugins can be registered in this callback.
3. Made `router` returned from `appOptions` optional.
4. Fixed issues reported by TypeScript and ESLint, including allowing `beforeBridgeRenderRes` to be a promise.
5. Updated the documentation.
41 changes: 29 additions & 12 deletions apps/website-new/docs/en/practice/bridge/vue-bridge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function createRemoteComponent<T, E extends keyof T>(

function createBridgeComponent(bridgeInfo: {
rootComponent: VueComponent;
appOptions?: (params: {
app: Vue.App<VueComponent>;
basename?: string;
memoryRoute?: { entryPath: string };
[key: string]: any;
}) => { router?: Router };
}): () => {
render(info: {
name?: string;
Expand All @@ -58,13 +64,17 @@ function createBridgeComponent(bridgeInfo: {
// ./src/export-app.ts
import App from './App.vue';
import router from './router';
import customPlugin from './plugins/custom-vue-plugin';
import { createBridgeComponent } from '@module-federation/bridge-vue3';

export default createBridgeComponent({
rootComponent: App,
appOptions: () => ({
router,
}),
appOptions: ({app}) => {
// Optional: adding a plugin to the new Vue instance on the host application side
app.use(customPlugin)

return {router}
},
});
```

Expand Down Expand Up @@ -193,11 +203,14 @@ export default router;
#### createBridgeComponent

```tsx
function createBridgeComponent<T>(bridgeInfo: {
function createBridgeComponent(bridgeInfo: {
rootComponent: VueComponent;
appOptions?: () => ({
router: Router;
});
appOptions?: (params: {
app: Vue.App<VueComponent>;
basename?: string;
memoryRoute?: { entryPath: string };
[key: string]: any;
}) => { router?: Router };
}): () => {
render(info: {
name?: string;
Expand All @@ -212,21 +225,25 @@ function createBridgeComponent<T>(bridgeInfo: {
```

* `bridgeInfo`
* type: `{ rootComponent: VueComponent; appOptions?: () => ({ router: Router }) }`
* type: `{ rootComponent: VueComponent; appOptions?: (params: AddOptionsFnParams) => ({ router?: Router }) }`
* Purpose: Used to pass the root component
* ReturnType
* type: `() => { render: (info: RenderFnParams) => void; destroy: (info: { dom: HTMLElement}) => void; }`

```tsx
// ./src/export-app.ts
import { createBridgeComponent } from '@module-federation/bridge-vue3';
import { createBridgeComponent } from '@module-federation/bridge-vue3';
import App from './App.vue';
import customPlugin from './plugins/custom-vue-plugin';
import router from './router';

export default createBridgeComponent({
rootComponent: App,
appOptions: () => ({
router,
}),
appOptions: ({app}) => {
// Optional: adding a plugin to the new Vue instance on the host application side
app.use(customPlugin)

return {router}
},
});
```
63 changes: 39 additions & 24 deletions packages/bridge/vue3-bridge/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,34 @@ import { getInstance } from '@module-federation/runtime';

declare const __APP_VERSION__: string;

export function createBridgeComponent(bridgeInfo: any) {
type AddOptionsFnParams = {
rootComponent: Vue.Component;
appOptions: (params: {
app: Vue.App<Vue.Component>;
basename: RenderFnParams['basename'];
memoryRoute: RenderFnParams['memoryRoute'];
[key: string]: any;
}) => { router?: VueRouter.Router };
};

export type ProviderFnParams = {
rootComponent: Vue.Component;
appOptions: (params: AddOptionsFnParams) => { router?: VueRouter.Router };
};

export function createBridgeComponent(bridgeInfo: ProviderFnParams) {
const rootMap = new Map();
const instance = getInstance();
return () => {
return {
__APP_VERSION__,
render(info: RenderFnParams) {
async render(info: RenderFnParams) {
LoggerInstance.log(`createBridgeComponent render Info`, info);
const app = Vue.createApp(bridgeInfo.rootComponent);
rootMap.set(info.dom, app);

const beforeBridgeRenderRes =
instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info) || {};
await instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info);

const extraProps =
beforeBridgeRenderRes &&
Expand All @@ -27,38 +42,38 @@ export function createBridgeComponent(bridgeInfo: any) {
? beforeBridgeRenderRes?.extraProps
: {};

const appOptions = bridgeInfo.appOptions({
const { router: remoteRouter } = bridgeInfo.appOptions({
app,
basename: info.basename,
memoryRoute: info.memoryRoute,
...extraProps,
});

const history = info.memoryRoute
? VueRouter.createMemoryHistory(info.basename)
: VueRouter.createWebHistory(info.basename);
if (remoteRouter) {
const history = info.memoryRoute
? VueRouter.createMemoryHistory(info.basename)
: VueRouter.createWebHistory(info.basename);

const router = VueRouter.createRouter({
...appOptions.router.options,
history,
routes: appOptions.router.getRoutes(),
});
const router = VueRouter.createRouter({
...remoteRouter.options,
history,
routes: remoteRouter.getRoutes(),
});

LoggerInstance.log(`createBridgeComponent render router info>>>`, {
name: info.moduleName,
router,
});
// memory route Initializes the route
if (info.memoryRoute) {
router.push(info.memoryRoute.entryPath).then(() => {
app.use(router);
app.mount(info.dom);
LoggerInstance.log(`createBridgeComponent render router info>>>`, {
name: info.moduleName,
router,
});
} else {
// memory route Initializes the route
if (info.memoryRoute) {
await router.push(info.memoryRoute.entryPath);
}

app.use(router);
app.mount(info.dom);
}

instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
app.mount(info.dom);
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info);
},
destroy(info: { dom: HTMLElement }) {
LoggerInstance.log(`createBridgeComponent destroy Info`, info);
Expand Down

0 comments on commit 394cbcd

Please sign in to comment.