Skip to content

Commit a5a4ff4

Browse files
committed
Module Federation: Add approach from @ScriptedAlchemy. Rework and simplify.
1 parent 37bd14a commit a5a4ff4

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Author: ScriptedAlchemy <zackary.l.jackson@gmail.com>
2+
// Author: Johannes Raggam <thetetet@gmail.com>
3+
14
// From:
25
// https://twitter.com/ScriptedAlchemy/status/1505135006158532612
36
// https://gist.github.com/ScriptedAlchemy/3a24008ef60adc47fad1af7d3299a063
@@ -6,49 +9,44 @@
69
/**
710
* Load remote module / bundle.
811
*
9-
* Usage: load_remote("checkout", {default: {//shimShareScope}} || "default", "http://theRemote.com")
12+
* Usage: get_container("bundle-name-xyz", "default", "http://theRemote.com")
13+
*
1014
*
1115
* @param {string} remote - the remote global name
12-
* @param {object | string} share_scope - the share_scope Object OR scope key
16+
* @param {string} share_scope - the scope key
1317
* @param {string} remote_fallback_url - fallback url for remote module
1418
* @returns {Promise<object>} - Federated Module Container
1519
*/
16-
export const load_remote = (remote, share_scope, remote_fallback_url = undefined) =>
20+
export default function get_container(
21+
remote,
22+
share_scope = "default",
23+
remote_fallback_url = undefined
24+
) {
1725
new Promise((resolve, reject) => {
18-
// Check if remote exists globally.
1926
if (!window[remote]) {
20-
// Search dom to see if the remote exists as script tag.
21-
// It might still be loading (async).
22-
const existing_remote = document.querySelector(`[data-webpack="${remote}"]`);
27+
// Remote not yet globally available.
2328

24-
// When remote is loaded..
29+
// onload hook when Module Federated resource is loaded.
2530
const onload = async () => {
26-
// Check if it was initialized.
31+
// When remote is loaded, initialize it if it wasn't already.
2732
if (!window[remote].__initialized) {
28-
// If share scope doesn't exist (like in webpack 4) then
29-
// expect share_scope to be a manual object.
30-
if (typeof __webpack_share_scopes__ === "undefined") {
31-
// Use default share scope object passed in manually.
32-
await window[remote].init(share_scope.default);
33-
} else {
34-
// Otherwise, init share scope as usual.
35-
await window[remote].init(__webpack_share_scopes__[share_scope]); // eslint-disable-line no-undef
36-
}
37-
// Mark remote as initialized
33+
await window[remote].init(__webpack_share_scopes__[share_scope]); // eslint-disable-line no-undef
34+
// Mark remote as initialized.
3835
window[remote].__initialized = true;
3936
}
4037
// Resolve promise so marking remote as loaded.
41-
resolve();
38+
resolve(window[remote]);
4239
};
4340

41+
// Search dom to see if the remote exists as script tag.
42+
// It might still be loading (async).
43+
const existing_remote = document.querySelector(`[data-webpack="${remote}"]`);
44+
4445
if (existing_remote) {
4546
// If remote exists but was not loaded, hook into its onload
4647
// and wait for it to be ready.
4748
existing_remote.onload = onload;
4849
existing_remote.onerror = reject;
49-
// Check if remote fallback exists as param passed to function.
50-
// TODO: Should scan public config for a matching key if no
51-
// override exists.
5250
} else if (remote_fallback_url) {
5351
// Inject remote if a fallback exists and call the same onload
5452
// function
@@ -67,6 +65,7 @@ export const load_remote = (remote, share_scope, remote_fallback_url = undefined
6765
}
6866
} else {
6967
// Remote already instantiated, resolve
70-
resolve();
68+
resolve(window[remote]);
7169
}
7270
});
71+
}

0 commit comments

Comments
 (0)