Skip to content

Commit 3c36b8f

Browse files
committed
add export
1 parent f429166 commit 3c36b8f

1 file changed

Lines changed: 28 additions & 59 deletions

File tree

src/ModuleFederation.tsx

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
loadRemote,
3-
registerRemotes,
4-
} from "@module-federation/enhanced/runtime";
1+
import { loadRemote, registerRemotes } from '@module-federation/enhanced/runtime';
52
import React, {
63
ComponentType,
74
FC,
@@ -15,17 +12,16 @@ import React, {
1512
useLayoutEffect,
1613
useMemo,
1714
useState,
18-
} from "react";
19-
import { NavigateOptions, To, useNavigate } from "react-router";
20-
import { MicroAppRuntimeConfigurationPlugin } from "./MicroAppRuntimeConfigurationPlugin";
15+
} from 'react';
16+
import { NavigateOptions, To, useNavigate } from 'react-router';
17+
import {
18+
MicroAppRuntimeConfigurationPlugin,
19+
microAppRuntimeConfigurationPlugin,
20+
} from './MicroAppRuntimeConfigurationPlugin';
2121
type Module = any;
2222

2323
const registeredApps: string[] = [];
24-
export function registerAndLoadModule(
25-
scope: string,
26-
module: string,
27-
url: string
28-
): () => Promise<Module> {
24+
export function registerAndLoadModule(scope: string, module: string, url: string): () => Promise<Module> {
2925
if (registeredApps.includes(scope)) {
3026
return loadModule(scope, module);
3127
}
@@ -41,10 +37,7 @@ export function registerAndLoadModule(
4137
return loadModule(scope, module);
4238
}
4339

44-
export function loadModule(
45-
scope: string,
46-
module: string
47-
): () => Promise<Module> {
40+
export function loadModule(scope: string, module: string): () => Promise<Module> {
4841
return async () => {
4942
const moduleAbsolutePath = module.substring(1);
5043

@@ -60,9 +53,7 @@ export const useCurrentApp = () => {
6053
const contextValue = useContext(CurrentAppContext);
6154

6255
if (contextValue === null) {
63-
throw new Error(
64-
"useCurrentApp can't be used outside of CurrentAppContext.Provider"
65-
);
56+
throw new Error("useCurrentApp can't be used outside of CurrentAppContext.Provider");
6657
}
6758

6859
return contextValue;
@@ -106,13 +97,8 @@ export function FederatedComponent({
10697

10798
return (
10899
<Suspense fallback={renderOnLoading ?? <>Loading...</>}>
109-
<ShellHooksProvider
110-
shellHooks={props.shellHooks}
111-
shellAlerts={props.shellAlerts}
112-
>
113-
<CurrentAppContext.Provider value={app}>
114-
{Component && <Component {...props} />}
115-
</CurrentAppContext.Provider>
100+
<ShellHooksProvider shellHooks={props.shellHooks} shellAlerts={props.shellAlerts}>
101+
<CurrentAppContext.Provider value={app}>{Component && <Component {...props} />}</CurrentAppContext.Provider>
116102
</ShellHooksProvider>
117103
</Suspense>
118104
);
@@ -126,19 +112,18 @@ export const lazyWithModules = <Props extends {}>(
126112
const loadedModules = await Promise.all(
127113
modules.map((mod) => {
128114
return registerAndLoadModule(mod.scope, mod.module, mod.url)();
129-
})
115+
}),
130116
);
131117
const moduleExports = loadedModules.reduce(
132118
(current, loadedModule, index) => ({
133119
...current,
134120
[modules[index].module]: loadedModule,
135121
}),
136-
{}
122+
{},
137123
);
138124
return {
139125
__esModule: true,
140-
default: (originalProps: Props) =>
141-
functionComponent({ moduleExports: moduleExports, ...originalProps }),
126+
default: (originalProps: Props) => functionComponent({ moduleExports: moduleExports, ...originalProps }),
142127
};
143128
});
144129
};
@@ -162,65 +147,50 @@ export const ComponentWithFederatedImports = <Props extends {}>({
162147
module: string;
163148
}[];
164149
}) => {
165-
const [Component, setComponent] = useState<LazyExoticComponent<any> | null>(
166-
null
167-
);
150+
const [Component, setComponent] = useState<LazyExoticComponent<any> | null>(null);
168151
useLayoutEffect(() => {
169152
const Comp = lazyWithModules(
170153
componentWithInjectedImports,
171154
...federatedImports.map((federatedImport) => ({
172155
scope: federatedImport.scope,
173156
module: federatedImport.module,
174157
url: federatedImport.remoteEntryUrl,
175-
}))
158+
})),
176159
);
177160
setComponent(() => Comp);
178161
}, [JSON.stringify(federatedImports)]);
179162

180163
return (
181-
<Suspense fallback={renderOnLoading ?? <>Loading...</>}>
182-
{Component && <Component {...componentProps} />}
183-
</Suspense>
164+
<Suspense fallback={renderOnLoading ?? <>Loading...</>}>{Component && <Component {...componentProps} />}</Suspense>
184165
);
185166
};
186167

187-
type ShellHooks<T extends { shellHooks: any }> = T["shellHooks"];
188-
type ShellAlerts<T extends { shellAlerts: any }> = T["shellAlerts"];
168+
type ShellHooks<T extends { shellHooks: any }> = T['shellHooks'];
169+
type ShellAlerts<T extends { shellAlerts: any }> = T['shellAlerts'];
189170

190171
const shellHooksContext = createContext<ShellHooks<any> | null>(null);
191172
const shellAlertsContext = createContext<ShellAlerts<any> | null>(null);
192173

193-
export const useShellHooks = <
194-
T extends { shellHooks: any }
195-
>(): ShellHooks<T> => {
174+
export const useShellHooks = <T extends { shellHooks: any }>(): ShellHooks<T> => {
196175
const hooks = useContext(shellHooksContext);
197176
if (!hooks) {
198-
throw new Error(
199-
"useShellHooks must be used within a ShellHooksProvider and initialized with valid hooks."
200-
);
177+
throw new Error('useShellHooks must be used within a ShellHooksProvider and initialized with valid hooks.');
201178
}
202179

203180
return hooks;
204181
};
205182

206-
export const useShellAlerts = <
207-
T extends { shellAlerts: any }
208-
>(): ShellAlerts<T> => {
183+
export const useShellAlerts = <T extends { shellAlerts: any }>(): ShellAlerts<T> => {
209184
const alerts = useContext(shellAlertsContext);
210185

211186
if (!alerts) {
212-
throw new Error(
213-
"useShellAlerts must be used within a ShellHooksProvider and initialized with valid alerts."
214-
);
187+
throw new Error('useShellAlerts must be used within a ShellHooksProvider and initialized with valid alerts.');
215188
}
216189

217190
return alerts;
218191
};
219192

220-
export const ShellHooksProvider = <
221-
T extends { shellHooks: any },
222-
K extends { shellAlerts: any }
223-
>({
193+
export const ShellHooksProvider = <T extends { shellHooks: any }, K extends { shellAlerts: any }>({
224194
shellHooks,
225195
shellAlerts,
226196
children,
@@ -231,9 +201,7 @@ export const ShellHooksProvider = <
231201
}) => {
232202
return (
233203
<shellHooksContext.Provider value={shellHooks}>
234-
<shellAlertsContext.Provider value={shellAlerts}>
235-
{children}
236-
</shellAlertsContext.Provider>
204+
<shellAlertsContext.Provider value={shellAlerts}>{children}</shellAlertsContext.Provider>
237205
</shellHooksContext.Provider>
238206
);
239207
};
@@ -249,4 +217,5 @@ export const useBasenameRelativeNavigate = () => {
249217
return navigate;
250218
};
251219

252-
export { MicroAppRuntimeConfigurationPlugin };
220+
export { MicroAppRuntimeConfigurationPlugin };
221+
export { microAppRuntimeConfigurationPlugin };

0 commit comments

Comments
 (0)