Skip to content

Commit d161cbe

Browse files
refactor(vue3-bridge): refactor of createBridgeComponent to add the ability to register plugins inside the function (#3297)
Co-authored-by: Zack Jackson <[email protected]>
1 parent 7f7bc9b commit d161cbe

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@module-federation/bridge-vue3': patch
3+
---
4+
5+
1. Added type annotations for the argument in the `createBridgeComponent` function.
6+
2. Added passing the instance of the created Vue-application to `appOptions` so that necessary plugins can be registered in this callback.
7+
3. Made `router` returned from `appOptions` optional.
8+
4. Fixed issues reported by TypeScript and ESLint, including allowing `beforeBridgeRenderRes` to be a promise.
9+
5. Updated the documentation.

apps/website-new/docs/en/practice/bridge/vue-bridge.mdx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ function createRemoteComponent<T, E extends keyof T>(
3535

3636
function createBridgeComponent(bridgeInfo: {
3737
rootComponent: VueComponent;
38+
appOptions?: (params: {
39+
app: Vue.App<VueComponent>;
40+
basename?: string;
41+
memoryRoute?: { entryPath: string };
42+
[key: string]: any;
43+
}) => { router?: Router } | void;
3844
}): () => {
3945
render(info: {
4046
name?: string;
@@ -58,13 +64,17 @@ function createBridgeComponent(bridgeInfo: {
5864
// ./src/export-app.ts
5965
import App from './App.vue';
6066
import router from './router';
67+
import customPlugin from './plugins/custom-vue-plugin';
6168
import { createBridgeComponent } from '@module-federation/bridge-vue3';
6269

6370
export default createBridgeComponent({
6471
rootComponent: App,
65-
appOptions: () => ({
66-
router,
67-
}),
72+
appOptions: ({app}) => {
73+
// Optional: adding a plugin to the new Vue instance on the host application side
74+
app.use(customPlugin)
75+
76+
return {router}
77+
},
6878
});
6979
```
7080

@@ -193,11 +203,14 @@ export default router;
193203
#### createBridgeComponent
194204

195205
```tsx
196-
function createBridgeComponent<T>(bridgeInfo: {
206+
function createBridgeComponent(bridgeInfo: {
197207
rootComponent: VueComponent;
198-
appOptions?: () => ({
199-
router: Router;
200-
});
208+
appOptions?: (params: {
209+
app: Vue.App<VueComponent>;
210+
basename?: string;
211+
memoryRoute?: { entryPath: string };
212+
[key: string]: any;
213+
}) => { router?: Router } | void;
201214
}): () => {
202215
render(info: {
203216
name?: string;
@@ -212,21 +225,25 @@ function createBridgeComponent<T>(bridgeInfo: {
212225
```
213226

214227
* `bridgeInfo`
215-
* type: `{ rootComponent: VueComponent; appOptions?: () => ({ router: Router }) }`
228+
* type: `{ rootComponent: VueComponent; appOptions?: (params: AddOptionsFnParams) => ({ router?: Router }) }`
216229
* Purpose: Used to pass the root component
217230
* ReturnType
218231
* type: `() => { render: (info: RenderFnParams) => void; destroy: (info: { dom: HTMLElement}) => void; }`
219232

220233
```tsx
221234
// ./src/export-app.ts
222-
import { createBridgeComponent } from '@module-federation/bridge-vue3';
235+
import { createBridgeComponent } from '@module-federation/bridge-vue3';
223236
import App from './App.vue';
237+
import customPlugin from './plugins/custom-vue-plugin';
224238
import router from './router';
225239

226240
export default createBridgeComponent({
227241
rootComponent: App,
228-
appOptions: () => ({
229-
router,
230-
}),
242+
appOptions: ({app}) => {
243+
// Optional: adding a plugin to the new Vue instance on the host application side
244+
app.use(customPlugin)
245+
246+
return {router}
247+
},
231248
});
232249
```

packages/bridge/vue3-bridge/src/provider.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,33 @@ import { getInstance } from '@module-federation/runtime';
66

77
declare const __APP_VERSION__: string;
88

9-
export function createBridgeComponent(bridgeInfo: any) {
9+
type AddOptionsFnParams = {
10+
app: Vue.App<Vue.Component>;
11+
basename: RenderFnParams['basename'];
12+
memoryRoute: RenderFnParams['memoryRoute'];
13+
[key: string]: any;
14+
};
15+
16+
export type ProviderFnParams = {
17+
rootComponent: Vue.Component;
18+
appOptions: (
19+
params: AddOptionsFnParams,
20+
) => { router?: VueRouter.Router } | void;
21+
};
22+
23+
export function createBridgeComponent(bridgeInfo: ProviderFnParams) {
1024
const rootMap = new Map();
1125
const instance = getInstance();
1226
return () => {
1327
return {
1428
__APP_VERSION__,
15-
render(info: RenderFnParams) {
29+
async render(info: RenderFnParams) {
1630
LoggerInstance.log(`createBridgeComponent render Info`, info);
1731
const app = Vue.createApp(bridgeInfo.rootComponent);
1832
rootMap.set(info.dom, app);
1933

2034
const beforeBridgeRenderRes =
21-
instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info) || {};
35+
await instance?.bridgeHook?.lifecycle?.beforeBridgeRender?.emit(info);
2236

2337
const extraProps =
2438
beforeBridgeRenderRes &&
@@ -27,38 +41,38 @@ export function createBridgeComponent(bridgeInfo: any) {
2741
? beforeBridgeRenderRes?.extraProps
2842
: {};
2943

30-
const appOptions = bridgeInfo.appOptions({
44+
const bridgeOptions = bridgeInfo.appOptions({
45+
app,
3146
basename: info.basename,
3247
memoryRoute: info.memoryRoute,
3348
...extraProps,
3449
});
3550

36-
const history = info.memoryRoute
37-
? VueRouter.createMemoryHistory(info.basename)
38-
: VueRouter.createWebHistory(info.basename);
51+
if (bridgeOptions?.router) {
52+
const history = info.memoryRoute
53+
? VueRouter.createMemoryHistory(info.basename)
54+
: VueRouter.createWebHistory(info.basename);
3955

40-
const router = VueRouter.createRouter({
41-
...appOptions.router.options,
42-
history,
43-
routes: appOptions.router.getRoutes(),
44-
});
56+
const router = VueRouter.createRouter({
57+
...bridgeOptions.router.options,
58+
history,
59+
routes: bridgeOptions.router.getRoutes(),
60+
});
4561

46-
LoggerInstance.log(`createBridgeComponent render router info>>>`, {
47-
name: info.moduleName,
48-
router,
49-
});
50-
// memory route Initializes the route
51-
if (info.memoryRoute) {
52-
router.push(info.memoryRoute.entryPath).then(() => {
53-
app.use(router);
54-
app.mount(info.dom);
62+
LoggerInstance.log(`createBridgeComponent render router info>>>`, {
63+
name: info.moduleName,
64+
router,
5565
});
56-
} else {
66+
// memory route Initializes the route
67+
if (info.memoryRoute) {
68+
await router.push(info.memoryRoute.entryPath);
69+
}
70+
5771
app.use(router);
58-
app.mount(info.dom);
5972
}
6073

61-
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info) || {};
74+
app.mount(info.dom);
75+
instance?.bridgeHook?.lifecycle?.afterBridgeRender?.emit(info);
6276
},
6377
destroy(info: { dom: HTMLElement }) {
6478
LoggerInstance.log(`createBridgeComponent destroy Info`, info);

0 commit comments

Comments
 (0)