-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Proposal: Remote as an Object #7
Open
schirrel
wants to merge
4
commits into
module-federation:main
Choose a base branch
from
schirrel:feat/fix-name-and-add-remote-as-object-async
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7cafb33
fix(enhanced): fixin plugin name
schirrel d08cd03
feat(remoteObject): create handle for remote as an object and default…
schirrel 56dd633
feat(remoteObject): add docs to readme
schirrel 1d35fd1
Merge remote-tracking branch 'origin/main' into feat/fix-name-and-add…
schirrel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,9 +2,7 @@ | |
|
||
## Idea | ||
|
||
Webpack Federation can do quite a lot, but orchestrating it can sometimes be a little messy or complex with its given api. The idea is to offer some additonal lifecycles directly on webpack interfaces to allow for pluggability and a more standardized ecosystem | ||
|
||
|
||
Webpack Federation can do quite a lot, but orchestrating it can sometimes be a little messy or complex with its given api. The idea is to offer some additonal lifecycles directly on webpack interfaces to allow for pluggability and a more standardized ecosystem | ||
|
||
# Remotes | ||
|
||
|
@@ -18,7 +16,7 @@ new ModuleFederationPlugin({ | |
}, | ||
(importValue) => { | ||
if(window.remoteOverrides['app1']) { | ||
// if an override exists, change the script that will be injected. | ||
// if an override exists, change the script that will be injected. | ||
return window.remoteOverrides['app1'] // --> otherRemote@http://otherUrl | ||
} else { | ||
return importValue // do nothing | ||
|
@@ -46,45 +44,82 @@ new ModuleFederationPlugin({ | |
``` | ||
|
||
## Middleware | ||
|
||
`promise new Promise` is handy, but it would be great if one could compose a series of middlewares into the promise chain in a standard manner. | ||
This would be useful for | ||
|
||
- controling versions/overrides at runtime | ||
- injecting env variables before webpack connects the containers together | ||
- doing some series of actions during the initial import() for a remote that only happens once - prior to a remote being injected | ||
- additional security or auth gates without having to write wrapper code around your exposed modules. | ||
- additional security or auth gates without having to write wrapper code around your exposed modules. | ||
|
||
## onBeforeGet | ||
The lifecycle of federation is as follows. | ||
1) inject remote (happens once) | ||
2) init() remote (happens once) | ||
3) get() exposed module (happens for every import) | ||
|
||
The lifecycle of federation is as follows. | ||
|
||
1. inject remote (happens once) | ||
2. init() remote (happens once) | ||
3. get() exposed module (happens for every import) | ||
|
||
After middleware has been run, and the remote container injected into the application, and has initialized. | ||
After middleware has been run, and the remote container injected into the application, and has initialized. | ||
|
||
Sometimes i might want to do something before calling the underlaying get() of a container. perhaps re-route the module request, or set up distributed logging automatically before import() resolves and starts executing. | ||
Sometimes i might want to do something before calling the underlaying get() of a container. perhaps re-route the module request, or set up distributed logging automatically before import() resolves and starts executing. | ||
|
||
onBeforeGet is basically middleware but for the module import itself instead of container injection | ||
|
||
### Remote as an Object | ||
|
||
Initially intended to handle the async default usage, but proves a better way of hadling remote URL based on envs or whatever. | ||
|
||
| Prop | Description | | ||
| ------- | ------------------------------------------------------------------------------------------------- | | ||
| async | if it must be wrapped aroung with `promise new Promise` | | ||
| name | the name of the remote, compiled out as the name before the `@` | | ||
| url | the url of the remoteEntry for of the remote, compiled out as the url after the `@ ` | | ||
| onError | optional function to be called on the async if the remote is offline or had any exception loading | | ||
|
||
|
||
The `async` prop, embed the Promise Based approach as in the [Webpack docs](https://webpack.js.org/concepts/module-federation/), turning the values passed as an objet to the `promise new Promise(resolve => {` that provides an outofbox handling of offline remotes. | ||
#### Usage | ||
|
||
```js | ||
remotes: { | ||
app1: "[email protected]/remoteEntry.js", | ||
app2: { | ||
name: "app2", | ||
url: isProd ? urlProd : urlDev | ||
}, | ||
app3: { | ||
async: true, | ||
name: "app3", | ||
url: "http://coolAppRunningOnCloud.com.br/remoteEntry.js", | ||
}, | ||
app2: { | ||
name: "app2", | ||
url: process.env.FINAL_REMOTE_ENTRY | ||
}, | ||
} | ||
``` | ||
|
||
### Custom Maps | ||
|
||
#### Extended to remoteEntry: | ||
| Prop | Description | | ||
| --------- | --------------------------------------------------- | | ||
| moduleMap | list of all available modules from a single remote. | | ||
| remoteMap | list of all remotes available for consumption | | ||
| remoteUrlMap | list of all remotes URL to initilize | | ||
|
||
| Prop | Description | | ||
| ------------ | --------------------------------------------------- | | ||
| moduleMap | list of all available modules from a single remote. | | ||
| remoteMap | list of all remotes available for consumption | | ||
| remoteUrlMap | list of all remotes URL to initilize | | ||
|
||
#### Usage | ||
|
||
```js | ||
const ModuleFederationEnhacedPlugin = require("@module-federation/ModuleFederationEnhacedPlugin"); | ||
const ModuleFederationEnhancedPlugin = require("@module-federation/ModuleFederationEnhancedPlugin"); | ||
|
||
module.export = { | ||
//... rest of your config | ||
plugins: [ | ||
new ModuleFederationEnhacedPlugin({ | ||
new ModuleFederationEnhancedPlugin({ | ||
name: "myApp", | ||
library: { type: "var", name: "app2" }, | ||
filename: "remoteEntry.js", | ||
|
@@ -114,10 +149,10 @@ import remoteMap from "myApp/remoteMap"; | |
import remoteUrlMap from "myApp/remoteUrlMap"; | ||
``` | ||
|
||
|
||
#### Chunk Map | ||
|
||
| Prop | Description | | ||
| --------- | --------------------------------------------------- | | ||
| Prop | Description | | ||
| ------------- | ------------------------------------------------------------- | | ||
| chunkMap.json | list of all chunkNames and create a json file on dist folder. | | ||
|
||
# Got ideas? Open a issue |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const defaultOnError = () => { | ||
const module = { | ||
get: () => () => {}, | ||
init: () => () => {}, | ||
}; | ||
resolve(module); | ||
}; | ||
|
||
const dynamicRemote = (remote) => { | ||
return `(resolve) => { | ||
const script = document.createElement("script"); | ||
script.src = "${remote.url}"; | ||
script.onload = () => { | ||
const module = { | ||
get: (request) => window["${remote.name}"].get(request), | ||
init: (arg) => { | ||
try { | ||
return window["${remote.name}"].init(arg); | ||
} catch (e) { | ||
console.log("Problem loading remote ${remote.name}", e); | ||
} | ||
}, | ||
}; | ||
resolve(module); | ||
}; | ||
script.onerror = ${ | ||
remote.onError ? remote.onError.toString() : defaultOnError.toString() | ||
} | ||
document.head.appendChild(script); | ||
}`; | ||
}; | ||
|
||
const handleAsyncRemote = (remote) => { | ||
return `promise new Promise(${dynamicRemote(remote).toString()})`; | ||
}; | ||
|
||
const mountFinalRemoteValue = (remote) => { | ||
if (remote.async) { | ||
return handleAsyncRemote(remote); | ||
} else { | ||
//Any other implementation of remote as an object goes here | ||
} | ||
|
||
return remote.name + "@" + remote.url; | ||
}; | ||
|
||
const HandleRemoteObject = (remotes) => { | ||
const _newRemotes = {}; | ||
Object.keys(remotes)?.forEach((remoteName) => { | ||
const remote = remotes[remoteName]; | ||
_newRemotes[remoteName] = | ||
typeof remote === "string" ? remote : mountFinalRemoteValue(remote); | ||
}); | ||
|
||
return _newRemotes; | ||
}; | ||
|
||
module.exports = HandleRemoteObject; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use webpack require.l for script loading here. i opened a PR adding webpack runtime requirements to promise based remotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should so this instead