@granite-js/micro-frontend 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GraniteMicroFrontendRuntime.podspec +40 -0
- package/LICENSE +202 -0
- package/LICENSE.react-native-teleport +21 -0
- package/NOTICE +17 -0
- package/README.md +508 -0
- package/android/CMakeLists.txt +34 -0
- package/android/build.gradle +74 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/BundleEvaluator.cpp +64 -0
- package/android/src/main/cpp/BundleEvaluator.h +20 -0
- package/android/src/main/cpp/FileReader.cpp +92 -0
- package/android/src/main/cpp/FileReader.h +27 -0
- package/android/src/main/cpp/onLoad.cpp +9 -0
- package/android/src/main/java/com/teleport/extensions/Float.kt +12 -0
- package/android/src/main/java/com/teleport/extensions/View.kt +10 -0
- package/android/src/main/java/com/teleport/global/PortalRegistry.kt +117 -0
- package/android/src/main/java/com/teleport/host/PortalHostView.kt +97 -0
- package/android/src/main/java/com/teleport/host/PortalHostViewManager.kt +41 -0
- package/android/src/main/java/com/teleport/host/PortalReactRootView.kt +116 -0
- package/android/src/main/java/com/teleport/managers/TeleportViewManager.kt +27 -0
- package/android/src/main/java/com/teleport/portal/PortalLayoutStateController.kt +107 -0
- package/android/src/main/java/com/teleport/portal/PortalView.kt +309 -0
- package/android/src/main/java/com/teleport/portal/PortalViewManager.kt +51 -0
- package/android/src/main/java/run/granite/microfrontend/ActivitySessionBinding.kt +87 -0
- package/android/src/main/java/run/granite/microfrontend/BundleEvaluator.kt +46 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendEvent.kt +62 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendRuntimeEventRouter.kt +186 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendRuntimeHost.kt +42 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendRuntimeModule.kt +56 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendRuntimePackage.kt +35 -0
- package/android/src/main/java/run/granite/microfrontend/GraniteMicroFrontendSessionStore.kt +116 -0
- package/android/src/main/jni/CMakeLists.txt +64 -0
- package/android/src/test/cpp/CMakeLists.txt +35 -0
- package/android/src/test/cpp/FileReaderTest.cpp +44 -0
- package/android/src/test/java/com/teleport/host/PortalReactRootViewContractTest.kt +76 -0
- package/android/src/test/java/run/granite/microfrontend/GraniteMicroFrontendRuntimeEventRouterTest.kt +216 -0
- package/android/src/test/java/run/granite/microfrontend/GraniteMicroFrontendSessionStoreTest.kt +190 -0
- package/android/test-cpp.sh +12 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/ComponentDescriptors.h +24 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/PortalShadowRegistry.cpp +31 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/PortalShadowRegistry.h +29 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalHostViewComponentDescriptor.h +22 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalHostViewShadowNode.cpp +7 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalHostViewShadowNode.h +43 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalHostViewState.h +21 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalViewComponentDescriptor.h +54 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalViewShadowNode.cpp +7 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalViewShadowNode.h +51 -0
- package/common/cpp/react/renderer/components/GraniteMicroFrontendRuntimeSpec/RNTPortalViewState.h +43 -0
- package/dist/module/createMicroFrontendRuntime.js +18 -0
- package/dist/module/createMicroFrontendRuntime.js.map +1 -0
- package/dist/module/createRoute.js +43 -0
- package/dist/module/createRoute.js.map +1 -0
- package/dist/module/host/PendingHostComponent.js +70 -0
- package/dist/module/host/PendingHostComponent.js.map +1 -0
- package/dist/module/host/index.js +6 -0
- package/dist/module/host/index.js.map +1 -0
- package/dist/module/host/pendingHostComponentStore.js +164 -0
- package/dist/module/host/pendingHostComponentStore.js.map +1 -0
- package/dist/module/host/resolveParams.js +60 -0
- package/dist/module/host/resolveParams.js.map +1 -0
- package/dist/module/host/routeMatcher.js +92 -0
- package/dist/module/host/routeMatcher.js.map +1 -0
- package/dist/module/host/types.js +4 -0
- package/dist/module/host/types.js.map +1 -0
- package/dist/module/index.js +12 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/package.json +1 -0
- package/dist/module/plugin/index.js +4 -0
- package/dist/module/plugin/index.js.map +1 -0
- package/dist/module/plugin/intoShared.js +32 -0
- package/dist/module/plugin/intoShared.js.map +1 -0
- package/dist/module/plugin/microFrontendPlugin.js +58 -0
- package/dist/module/plugin/microFrontendPlugin.js.map +1 -0
- package/dist/module/plugin/prelude.js +23 -0
- package/dist/module/plugin/prelude.js.map +1 -0
- package/dist/module/plugin/resolver.js +25 -0
- package/dist/module/plugin/resolver.js.map +1 -0
- package/dist/module/plugin/types.js +4 -0
- package/dist/module/plugin/types.js.map +1 -0
- package/dist/module/runtime/createMicroFrontendRuntime.js +79 -0
- package/dist/module/runtime/createMicroFrontendRuntime.js.map +1 -0
- package/dist/module/runtime/errors.js +63 -0
- package/dist/module/runtime/errors.js.map +1 -0
- package/dist/module/runtime/parseAppRequest.js +16 -0
- package/dist/module/runtime/parseAppRequest.js.map +1 -0
- package/dist/module/runtime/parseNativeRuntimeEvent.js +54 -0
- package/dist/module/runtime/parseNativeRuntimeEvent.js.map +1 -0
- package/dist/module/runtime/registry.js +87 -0
- package/dist/module/runtime/registry.js.map +1 -0
- package/dist/module/session/MicroFrontendSessionContext.js +32 -0
- package/dist/module/session/MicroFrontendSessionContext.js.map +1 -0
- package/dist/module/session/useMicroFrontendSessions.js +47 -0
- package/dist/module/session/useMicroFrontendSessions.js.map +1 -0
- package/dist/module/specs/NativeGraniteMicroFrontendRuntime.js +21 -0
- package/dist/module/specs/NativeGraniteMicroFrontendRuntime.js.map +1 -0
- package/dist/module/specs/PortalHostViewNativeComponent.ts +9 -0
- package/dist/module/specs/PortalViewNativeComponent.ts +9 -0
- package/dist/module/types.js +2 -0
- package/dist/module/types.js.map +1 -0
- package/dist/typescript/createMicroFrontendRuntime.d.ts +6 -0
- package/dist/typescript/createRoute.d.ts +23 -0
- package/dist/typescript/host/PendingHostComponent.d.ts +14 -0
- package/dist/typescript/host/index.d.ts +5 -0
- package/dist/typescript/host/pendingHostComponentStore.d.ts +11 -0
- package/dist/typescript/host/resolveParams.d.ts +10 -0
- package/dist/typescript/host/routeMatcher.d.ts +7 -0
- package/dist/typescript/host/types.d.ts +32 -0
- package/dist/typescript/index.d.ts +16 -0
- package/dist/typescript/plugin/index.d.ts +2 -0
- package/dist/typescript/plugin/intoShared.d.ts +3 -0
- package/dist/typescript/plugin/microFrontendPlugin.d.ts +3 -0
- package/dist/typescript/plugin/prelude.d.ts +6 -0
- package/dist/typescript/plugin/resolver.d.ts +3 -0
- package/dist/typescript/plugin/types.d.ts +5 -0
- package/dist/typescript/runtime/createMicroFrontendRuntime.d.ts +31 -0
- package/dist/typescript/runtime/errors.d.ts +35 -0
- package/dist/typescript/runtime/parseAppRequest.d.ts +6 -0
- package/dist/typescript/runtime/parseNativeRuntimeEvent.d.ts +3 -0
- package/dist/typescript/runtime/registry.d.ts +35 -0
- package/dist/typescript/session/MicroFrontendSessionContext.d.ts +13 -0
- package/dist/typescript/session/useMicroFrontendSessions.d.ts +8 -0
- package/dist/typescript/specs/NativeGraniteMicroFrontendRuntime.d.ts +21 -0
- package/dist/typescript/specs/PortalHostViewNativeComponent.d.ts +6 -0
- package/dist/typescript/specs/PortalViewNativeComponent.d.ts +6 -0
- package/dist/typescript/types.d.ts +49 -0
- package/ios/GraniteMicroFrontendRuntimeHost+Internal.h +18 -0
- package/ios/GraniteMicroFrontendRuntimeHost.h +34 -0
- package/ios/GraniteMicroFrontendRuntimeHost.mm +485 -0
- package/ios/GraniteMicroFrontendRuntimeModule.h +6 -0
- package/ios/GraniteMicroFrontendRuntimeModule.mm +138 -0
- package/ios/PortalHostContainerView.h +56 -0
- package/ios/PortalHostContainerView.mm +133 -0
- package/ios/PortalHostView.h +39 -0
- package/ios/PortalHostView.mm +153 -0
- package/ios/PortalRegistry.h +38 -0
- package/ios/PortalRegistry.mm +228 -0
- package/ios/PortalView.h +32 -0
- package/ios/PortalView.mm +426 -0
- package/package.json +113 -0
- package/react-native.config.js +13 -0
- package/src/createMicroFrontendRuntime.ts +22 -0
- package/src/createRoute.ts +84 -0
- package/src/host/PendingHostComponent.tsx +92 -0
- package/src/host/index.ts +26 -0
- package/src/host/pendingHostComponentStore.ts +241 -0
- package/src/host/resolveParams.ts +70 -0
- package/src/host/routeMatcher.ts +117 -0
- package/src/host/types.ts +45 -0
- package/src/index.ts +57 -0
- package/src/plugin/index.ts +2 -0
- package/src/plugin/intoShared.ts +50 -0
- package/src/plugin/microFrontendPlugin.ts +57 -0
- package/src/plugin/prelude.ts +73 -0
- package/src/plugin/resolver.ts +73 -0
- package/src/plugin/types.ts +6 -0
- package/src/runtime/createMicroFrontendRuntime.ts +115 -0
- package/src/runtime/errors.ts +86 -0
- package/src/runtime/parseAppRequest.ts +22 -0
- package/src/runtime/parseNativeRuntimeEvent.ts +54 -0
- package/src/runtime/registry.ts +131 -0
- package/src/session/MicroFrontendSessionContext.tsx +45 -0
- package/src/session/useMicroFrontendSessions.ts +73 -0
- package/src/specs/NativeGraniteMicroFrontendRuntime.ts +44 -0
- package/src/specs/PortalHostViewNativeComponent.ts +9 -0
- package/src/specs/PortalViewNativeComponent.ts +9 -0
- package/src/types.ts +55 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { VisibilityChangedProvider } from '@granite-js/react-native';
|
|
2
|
+
import { createContext, useContext, useMemo, type ReactElement, type ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
export interface MicroFrontendSession {
|
|
5
|
+
readonly sessionId: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface MicroFrontendSessionProviderProps extends MicroFrontendSession {
|
|
9
|
+
readonly children: ReactNode;
|
|
10
|
+
readonly presentationVisibility: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const MicroFrontendSessionContext = createContext<MicroFrontendSession | null>(null);
|
|
14
|
+
|
|
15
|
+
export class MissingMicroFrontendSessionError extends Error {
|
|
16
|
+
constructor() {
|
|
17
|
+
super('useMicroFrontendSession must be used inside MicroFrontendSessionProvider');
|
|
18
|
+
this.name = 'MissingMicroFrontendSessionError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function MicroFrontendSessionProvider(props: MicroFrontendSessionProviderProps): ReactElement {
|
|
23
|
+
const value = useMemo<MicroFrontendSession>(
|
|
24
|
+
() => ({
|
|
25
|
+
sessionId: props.sessionId,
|
|
26
|
+
}),
|
|
27
|
+
[props.sessionId]
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<MicroFrontendSessionContext.Provider value={value}>
|
|
32
|
+
<VisibilityChangedProvider isVisible={props.presentationVisibility}>
|
|
33
|
+
{props.children}
|
|
34
|
+
</VisibilityChangedProvider>
|
|
35
|
+
</MicroFrontendSessionContext.Provider>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function useMicroFrontendSession(): MicroFrontendSession {
|
|
40
|
+
const session = useContext(MicroFrontendSessionContext);
|
|
41
|
+
if (session == null) {
|
|
42
|
+
throw new MissingMicroFrontendSessionError();
|
|
43
|
+
}
|
|
44
|
+
return session;
|
|
45
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useLayoutEffect, useReducer } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
installPendingHostComponentBridge,
|
|
4
|
+
resetPendingHostComponent,
|
|
5
|
+
} from '../host/pendingHostComponentStore';
|
|
6
|
+
import type {
|
|
7
|
+
MicroFrontendRuntimeApi,
|
|
8
|
+
MicroFrontendSessionEvent,
|
|
9
|
+
} from '../types';
|
|
10
|
+
|
|
11
|
+
export interface MicroFrontendSessionState {
|
|
12
|
+
readonly appName: string;
|
|
13
|
+
readonly sessionId: string;
|
|
14
|
+
readonly scheme: string;
|
|
15
|
+
readonly isVisible: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const INITIAL_SESSIONS: readonly MicroFrontendSessionState[] = [];
|
|
19
|
+
|
|
20
|
+
function reduceMicroFrontendSessions(
|
|
21
|
+
sessions: readonly MicroFrontendSessionState[],
|
|
22
|
+
event: MicroFrontendSessionEvent
|
|
23
|
+
): readonly MicroFrontendSessionState[] {
|
|
24
|
+
switch (event.name) {
|
|
25
|
+
case 'openApp':
|
|
26
|
+
return sessions.some(({ sessionId }) => sessionId === event.params.sessionId)
|
|
27
|
+
? sessions
|
|
28
|
+
: [
|
|
29
|
+
...sessions,
|
|
30
|
+
{
|
|
31
|
+
appName: event.params.appName,
|
|
32
|
+
sessionId: event.params.sessionId,
|
|
33
|
+
scheme: event.params.scheme,
|
|
34
|
+
isVisible: false,
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
case 'closeApp':
|
|
38
|
+
return sessions.filter(({ sessionId }) => sessionId !== event.params.sessionId);
|
|
39
|
+
case 'sessionVisibilityChanged':
|
|
40
|
+
return sessions.map((session) =>
|
|
41
|
+
session.sessionId === event.params.sessionId
|
|
42
|
+
? { ...session, isVisible: event.params.isVisible }
|
|
43
|
+
: session
|
|
44
|
+
);
|
|
45
|
+
default: {
|
|
46
|
+
const exhaustiveEvent: never = event;
|
|
47
|
+
return exhaustiveEvent;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function useMicroFrontendSessions(
|
|
53
|
+
runtime: Pick<MicroFrontendRuntimeApi, 'onEvent'>
|
|
54
|
+
): readonly MicroFrontendSessionState[] {
|
|
55
|
+
const [sessions, dispatch] = useReducer(
|
|
56
|
+
reduceMicroFrontendSessions,
|
|
57
|
+
INITIAL_SESSIONS
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
useLayoutEffect(() => {
|
|
61
|
+
installPendingHostComponentBridge();
|
|
62
|
+
const subscription = runtime.onEvent((event) => {
|
|
63
|
+
if (event.name === 'openApp') {
|
|
64
|
+
resetPendingHostComponent();
|
|
65
|
+
}
|
|
66
|
+
dispatch(event);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return () => subscription.remove();
|
|
70
|
+
}, [runtime]);
|
|
71
|
+
|
|
72
|
+
return sessions;
|
|
73
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type CodegenTypes, type TurboModule, TurboModuleRegistry } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export interface NativeMicroFrontendRuntimeEventParams {
|
|
4
|
+
readonly appName?: string;
|
|
5
|
+
readonly sessionId?: string;
|
|
6
|
+
readonly scheme?: string;
|
|
7
|
+
readonly isVisible?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface NativeMicroFrontendRuntimeEvent {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly params: NativeMicroFrontendRuntimeEventParams;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type EvaluateScriptRequest = Readonly<{
|
|
16
|
+
filePath: string;
|
|
17
|
+
}>;
|
|
18
|
+
|
|
19
|
+
export interface Spec extends TurboModule {
|
|
20
|
+
evaluateScript(request: EvaluateScriptRequest): Promise<void>;
|
|
21
|
+
startEventDelivery(): void;
|
|
22
|
+
readonly onEvent: CodegenTypes.EventEmitter<NativeMicroFrontendRuntimeEvent>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let nativeModule: Spec | null = null;
|
|
26
|
+
|
|
27
|
+
function getNativeModule() {
|
|
28
|
+
nativeModule ??= TurboModuleRegistry.getEnforcing<Spec>('GraniteMicroFrontendRuntime');
|
|
29
|
+
return nativeModule;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const NativeGraniteMicroFrontendRuntime: Spec = {
|
|
33
|
+
evaluateScript(request) {
|
|
34
|
+
return getNativeModule().evaluateScript(request);
|
|
35
|
+
},
|
|
36
|
+
startEventDelivery() {
|
|
37
|
+
getNativeModule().startEventDelivery();
|
|
38
|
+
},
|
|
39
|
+
onEvent(listener) {
|
|
40
|
+
return getNativeModule().onEvent(listener);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default NativeGraniteMicroFrontendRuntime;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type AppRequest = `${string}/${string}`;
|
|
2
|
+
|
|
3
|
+
export interface MicroFrontendBundleRequest {
|
|
4
|
+
readonly appName: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface MicroFrontendBundle {
|
|
8
|
+
readonly filePath: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface MicroFrontendAppProps {
|
|
12
|
+
readonly scheme: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface MicroFrontendAdapter {
|
|
16
|
+
readonly loadBundle: (request: MicroFrontendBundleRequest) => Promise<MicroFrontendBundle>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type MicroFrontendRuntimeEvent =
|
|
20
|
+
| {
|
|
21
|
+
readonly name: 'preloadApp';
|
|
22
|
+
readonly params: { readonly appName: string };
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
readonly name: 'openApp';
|
|
26
|
+
readonly params: {
|
|
27
|
+
readonly sessionId: string;
|
|
28
|
+
readonly appName: string;
|
|
29
|
+
readonly scheme: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
| {
|
|
33
|
+
readonly name: 'closeApp';
|
|
34
|
+
readonly params: { readonly sessionId: string };
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
readonly name: 'sessionVisibilityChanged';
|
|
38
|
+
readonly params: {
|
|
39
|
+
readonly sessionId: string;
|
|
40
|
+
readonly isVisible: boolean;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type MicroFrontendSessionEvent = Exclude<MicroFrontendRuntimeEvent, { readonly name: 'preloadApp' }>;
|
|
45
|
+
|
|
46
|
+
export interface MicroFrontendRuntimeEventSubscription {
|
|
47
|
+
readonly remove: () => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface MicroFrontendRuntimeApi {
|
|
51
|
+
readonly evaluateScript: (filePath: string) => Promise<void>;
|
|
52
|
+
readonly preloadApp: (appName: string) => Promise<void>;
|
|
53
|
+
readonly importApp: <TModule>(request: AppRequest) => Promise<TModule>;
|
|
54
|
+
readonly onEvent: (listener: (event: MicroFrontendSessionEvent) => void) => MicroFrontendRuntimeEventSubscription;
|
|
55
|
+
}
|