Skip to content

Commit 734df18

Browse files
authored
feat(vue3-bridge): add support for passing parameters to defineAsyncComponent (#3268)
1 parent d5c783b commit 734df18

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

.changeset/rude-berries-look.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@module-federation/bridge-vue3': minor
3+
---
4+
5+
Added the ability to pass parameters to `defineAsyncComponent`, which solves the following issues:
6+
- Enables control over fallback components and their display parameters;
7+
- Improves compatibility with Nuxt by allowing configuration of the `suspensible` parameter;
8+
- Eliminates the package requirement to `Configure your bundler to alias "vue" to "vue/dist/vue.esm-bundler.js"`.

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ import { PackageManagerTabs } from '@theme';
1919

2020
```tsx
2121
function createRemoteComponent<T, E extends keyof T>(
22-
// Function to load remote application, e.g., loadRemote('remote1/export-app') or import('remote1/export-app')
23-
lazyComponent: () => Promise<T>,
24-
info?:
25-
{
22+
options: {
23+
// Function to load remote application, e.g., loadRemote('remote1/export-app') or import('remote1/export-app')
24+
loader: () => Promise<T>,
2625
// Default is 'default', used to specify module export
27-
export?: E;
26+
export?: E,
27+
// Parameters that will be passed to defineAsyncComponent
28+
asyncComponentOptions?: Omit<AsyncComponentOptions, 'loader'>;
2829
}
2930
): (props: {
3031
basename?: string;
3132
memoryRoute?: { entryPath: string };
3233
}) => DefineComponent;
3334

35+
3436
function createBridgeComponent(bridgeInfo: {
3537
rootComponent: VueComponent;
3638
}): () => {
@@ -105,9 +107,7 @@ export default defineConfig({
105107
// ./src/router.ts
106108
import * as bridge from '@module-federation/bridge-vue3';
107109

108-
const Remote2 = bridge.createRemoteComponent(() =>
109-
loadRemote('remote1/export-app'),
110-
);
110+
const Remote2 = bridge.createRemoteComponent({ loader: () => loadRemote('remote1/export-app') });
111111

112112
const router = createRouter({
113113
history: createWebHistory(),
@@ -132,6 +132,8 @@ function createRemoteComponent<T, E extends keyof T>(
132132
loader: () => Promise<T>,
133133
// Default is 'default', used to specify module export
134134
export?: E;
135+
// Parameters that will be passed to defineAsyncComponent
136+
asyncComponentOptions?: Omit<AsyncComponentOptions, 'loader'>;
135137
}
136138
): (props: {
137139
basename?: string;
@@ -141,7 +143,7 @@ function createRemoteComponent<T, E extends keyof T>(
141143

142144

143145
```tsx
144-
const Remote1App = createRemoteComponent(() => loadRemote('remote1/export-app'));
146+
const Remote1App = createRemoteComponent({ loader: () => loadRemote('remote1/export-app') });
145147
```
146148

147149
* `options`
@@ -151,14 +153,18 @@ const Remote1App = createRemoteComponent(() => loadRemote('remote1/export-app'))
151153
* `export`
152154
* type: `string`
153155
* Purpose: Used to specify module export
156+
* `asyncComponentOptions`
157+
* type: `Omit<AsyncComponentOptions, 'loader'>`
158+
* Purpose: Parameters that will be passed to defineAsyncComponent, except for the loader parameter
154159
```tsx
155160
// remote
156161
export const provider = createBridgeComponent({
157162
rootComponent: App
158163
});
159164

160165
// host
161-
const Remote1App = createRemoteComponent(() => loadRemote('remote1/export-app'), {
166+
const Remote1App = createRemoteComponent({
167+
loader: () => loadRemote('remote1/export-app'),
162168
export: 'provider'
163169
});
164170
```
@@ -170,9 +176,7 @@ const Remote1App = createRemoteComponent(() => loadRemote('remote1/export-app'),
170176
```tsx
171177
import * as bridge from '@module-federation/bridge-vue3';
172178

173-
const Remote2 = bridge.createRemoteComponent(() =>
174-
loadRemote('remote1/export-app'),
175-
);
179+
const Remote2 = bridge.createRemoteComponent({ loader: () => loadRemote('remote1/export-app') });
176180

177181
const router = createRouter({
178182
history: createWebHistory(),

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineAsyncComponent, h } from 'vue';
1+
import { type AsyncComponentOptions, defineAsyncComponent, h } from 'vue';
22
import { useRoute } from 'vue-router';
33
import RemoteApp from './remoteApp.jsx';
44
import { LoggerInstance } from './utils.js';
@@ -8,9 +8,11 @@ declare const __APP_VERSION__: string;
88
export function createRemoteComponent(info: {
99
loader: () => Promise<any>;
1010
export?: string;
11+
asyncComponentOptions?: Omit<AsyncComponentOptions, 'loader'>;
1112
}) {
1213
return defineAsyncComponent({
1314
__APP_VERSION__,
15+
...info.asyncComponentOptions,
1416
//@ts-ignore
1517
loader: async () => {
1618
const route = useRoute();
@@ -45,7 +47,6 @@ export function createRemoteComponent(info: {
4547
render() {
4648
return h(RemoteApp, {
4749
moduleName,
48-
...info,
4950
providerInfo: exportFn,
5051
basename,
5152
});
@@ -54,13 +55,5 @@ export function createRemoteComponent(info: {
5455
}
5556
throw new Error('module not found');
5657
},
57-
loadingComponent: {
58-
template: '<div>Loading...</div>',
59-
},
60-
errorComponent: {
61-
template: '<div>Error loading component</div>',
62-
},
63-
delay: 200,
64-
timeout: 3000,
6558
});
6659
}

0 commit comments

Comments
 (0)