-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert env var remote to v2 runtime
- Loading branch information
1 parent
7c94c58
commit b4d4ae6
Showing
6 changed files
with
249 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
advanced-api/dynamic-remotes-runtime-environment-variables/host/src/components/Main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
...nced-api/dynamic-remotes-runtime-environment-variables/host/src/hooks/useDynamicScript.js
This file was deleted.
Oops, something went wrong.
31 changes: 17 additions & 14 deletions
31
...api/dynamic-remotes-runtime-environment-variables/host/src/hooks/useFederatedComponent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
import React, { lazy, useEffect, useState } from 'react'; | ||
|
||
import useDynamicScript from './useDynamicScript'; | ||
import {registerRemotes, loadRemote} from "@module-federation/runtime"; | ||
|
||
function loadComponent(scope, module) { | ||
return async () => { | ||
// Initializes the share scope. This fills it with known provided modules from this build and all remotes | ||
await __webpack_init_sharing__('default'); | ||
const container = window[scope]; // or get the container somewhere else | ||
// Initialize the container, it may provide shared modules | ||
await container.init(__webpack_share_scopes__.default); | ||
const factory = await window[scope].get(module); | ||
const Module = factory(); | ||
return Module; | ||
if(!scope && !module) { | ||
return {default: ()=>null} | ||
} | ||
return await loadRemote(scope + module.replace('.','')); | ||
}; | ||
} | ||
|
||
const componentCache = new Map(); | ||
export const useFederatedComponent = (remoteUrl, scope, module) => { | ||
console.log(remoteUrl,scope, module); | ||
if(scope && remoteUrl) { | ||
registerRemotes([ | ||
{ | ||
name: scope, | ||
entry: remoteUrl, | ||
}, | ||
]); | ||
} | ||
const key = `${remoteUrl}-${scope}-${module}`; | ||
const [Component, setComponent] = useState(null); | ||
|
||
const { ready, errorLoading } = useDynamicScript(remoteUrl); | ||
useEffect(() => { | ||
if (Component) setComponent(null); | ||
// Only recalculate when key changes | ||
}, [key]); | ||
|
||
useEffect(() => { | ||
if (ready && !Component) { | ||
if (!Component) { | ||
const Comp = lazy(loadComponent(scope, module)); | ||
componentCache.set(key, Comp); | ||
setComponent(Comp); | ||
} | ||
// key includes all dependencies (scope/module) | ||
}, [Component, ready, key]); | ||
}, [Component, key]); | ||
|
||
return { errorLoading, Component }; | ||
return { Component }; | ||
}; | ||
|
||
export default useFederatedComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,5 +215,6 @@ | |
"report:generate": "allure generate ./cypress/results/allure-results --clean -o ./cypress/report", | ||
"report:open": "allure open ./cypress/report", | ||
"preinstall": "npx only-allow pnpm" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a" | ||
} |
Oops, something went wrong.