chayns-api 3.6.1 → 3.7.0-beta.1
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/dist/cjs/host/module/utils/loadComponent.js +30 -13
- package/dist/cjs/plugins/ManifestShareScopePlugin.js +56 -0
- package/dist/cjs/plugins/SequentialLoadPlugin.js +2 -1
- package/dist/cjs/plugins/index.js +7 -0
- package/dist/cjs/utils/initModuleFederationSharing.js +67 -6
- package/dist/esm/host/module/utils/loadComponent.js +31 -14
- package/dist/esm/plugins/ManifestShareScopePlugin.js +48 -0
- package/dist/esm/plugins/SequentialLoadPlugin.js +2 -1
- package/dist/esm/plugins/index.js +1 -0
- package/dist/esm/utils/initModuleFederationSharing.js +67 -6
- package/dist/types/host/module/utils/loadComponent.d.ts +3 -2
- package/dist/types/plugins/ManifestShareScopePlugin.d.ts +2 -0
- package/dist/types/plugins/index.d.ts +1 -0
- package/dist/types/utils/initModuleFederationSharing.d.ts +1 -1
- package/package.json +3 -1
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.loadModule = exports.default = void 0;
|
|
7
7
|
var _react = _interopRequireDefault(require("react"));
|
|
8
8
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
-
const ERROR_CACHE_TIME = 60000;
|
|
10
9
|
const normalizeUrl = url => {
|
|
11
10
|
try {
|
|
12
11
|
return new URL(url).toString();
|
|
@@ -32,12 +31,13 @@ const resetAfterCacheTime = (scope, module, url) => {
|
|
|
32
31
|
registeredScopes[scope] = '';
|
|
33
32
|
}
|
|
34
33
|
errorResetTimeouts.delete(key);
|
|
35
|
-
},
|
|
34
|
+
}, 60000);
|
|
36
35
|
};
|
|
37
|
-
const
|
|
36
|
+
const legacyLoadModule = (scope, module, url, preventSingleton = false) => {
|
|
38
37
|
if (!globalThis.moduleFederationRuntime || !globalThis.moduleFederationScopes) {
|
|
39
38
|
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
40
39
|
}
|
|
40
|
+
console.warn('[chayns-api] Using legacy loadModule implementation. Please update the host to 3.7.0 or higher to use the new loadModule implementation.');
|
|
41
41
|
const {
|
|
42
42
|
loadRemote,
|
|
43
43
|
registerRemotes
|
|
@@ -74,8 +74,16 @@ const loadModule = (scope, module, url, preventSingleton = false) => {
|
|
|
74
74
|
}
|
|
75
75
|
return moduleMap[scope][module];
|
|
76
76
|
};
|
|
77
|
+
const loadModule = (...args) => {
|
|
78
|
+
var _globalThis$moduleFed, _globalThis$moduleFed2, _globalThis$moduleFed3;
|
|
79
|
+
if (!globalThis.moduleFederationRuntime) {
|
|
80
|
+
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
81
|
+
}
|
|
82
|
+
return (_globalThis$moduleFed = (_globalThis$moduleFed2 = (_globalThis$moduleFed3 = globalThis.moduleFederationRuntime).loadModule) === null || _globalThis$moduleFed2 === void 0 ? void 0 : _globalThis$moduleFed2.call(_globalThis$moduleFed3, ...args)) !== null && _globalThis$moduleFed !== void 0 ? _globalThis$moduleFed : legacyLoadModule(...args);
|
|
83
|
+
};
|
|
77
84
|
exports.loadModule = loadModule;
|
|
78
85
|
const loadComponent = (scope, module, url, skipCompatMode = false, preventSingleton = false) => {
|
|
86
|
+
var _globalThis$moduleFed4, _globalThis$moduleFed5;
|
|
79
87
|
if (skipCompatMode) {
|
|
80
88
|
console.warn('[chayns-api] skipCompatMode-option is deprecated and is set automatically now');
|
|
81
89
|
}
|
|
@@ -83,24 +91,32 @@ const loadComponent = (scope, module, url, skipCompatMode = false, preventSingle
|
|
|
83
91
|
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
84
92
|
}
|
|
85
93
|
const {
|
|
86
|
-
getInstance
|
|
94
|
+
getInstance,
|
|
95
|
+
loadModule: hostLoadModule
|
|
87
96
|
} = globalThis.moduleFederationRuntime;
|
|
88
97
|
const {
|
|
89
|
-
componentMap
|
|
90
|
-
registeredScopes
|
|
98
|
+
componentMap
|
|
91
99
|
} = globalThis.moduleFederationScopes;
|
|
100
|
+
const componentRegistrationKeys = (_globalThis$moduleFed5 = (_globalThis$moduleFed4 = globalThis.moduleFederationScopes).componentRegistrationKeys) !== null && _globalThis$moduleFed5 !== void 0 ? _globalThis$moduleFed5 : _globalThis$moduleFed4.componentRegistrationKeys = {};
|
|
92
101
|
if (!componentMap[scope]) {
|
|
93
102
|
componentMap[scope] = {};
|
|
94
103
|
}
|
|
95
|
-
if (!
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
if (!componentRegistrationKeys[scope]) {
|
|
105
|
+
componentRegistrationKeys[scope] = {};
|
|
106
|
+
}
|
|
107
|
+
if (!(module in componentMap[scope]) || componentRegistrationKeys[scope][module] !== url) {
|
|
108
|
+
const promise = (hostLoadModule !== null && hostLoadModule !== void 0 ? hostLoadModule : legacyLoadModule)(scope, module, url, preventSingleton).then(Module => {
|
|
98
109
|
if (typeof Module.default === 'function') {
|
|
99
110
|
return Module;
|
|
100
111
|
}
|
|
101
112
|
const shareScopes = getInstance().shareScopeMap;
|
|
102
|
-
const
|
|
103
|
-
|
|
113
|
+
const matchReactVersion = Object.values(shareScopes).map(shareScope => {
|
|
114
|
+
var _shareScope$react;
|
|
115
|
+
return (_shareScope$react = shareScope.react) === null || _shareScope$react === void 0 ? void 0 : _shareScope$react[_react.default.version];
|
|
116
|
+
}).some(reactShare => {
|
|
117
|
+
var _reactShare$lib;
|
|
118
|
+
return (reactShare === null || reactShare === void 0 ? void 0 : reactShare.useIn.includes(scope)) && ((_reactShare$lib = reactShare.lib) === null || _reactShare$lib === void 0 ? void 0 : _reactShare$lib.call(reactShare)) === _react.default;
|
|
119
|
+
});
|
|
104
120
|
if (!matchReactVersion || Module.default.environment !== 'production' || (Module.default.version || 1) < 2) {
|
|
105
121
|
const OriginalCompatComponent = (Module.default.version || 1) < 2.1 ? Module.default.CompatComponent.render({}).type.prototype : Module.default.CompatComponent.prototype;
|
|
106
122
|
class CompatComponent extends _react.default.Component {
|
|
@@ -132,10 +148,11 @@ const loadComponent = (scope, module, url, skipCompatMode = false, preventSingle
|
|
|
132
148
|
default: Module.default.Component
|
|
133
149
|
};
|
|
134
150
|
});
|
|
135
|
-
promise.catch(
|
|
136
|
-
console.error(
|
|
151
|
+
promise.catch(error => {
|
|
152
|
+
console.error('[chayns-api] Failed to load component', scope, url, error);
|
|
137
153
|
});
|
|
138
154
|
componentMap[scope][module] = _react.default.lazy(() => promise);
|
|
155
|
+
componentRegistrationKeys[scope][module] = url;
|
|
139
156
|
}
|
|
140
157
|
return componentMap[scope][module];
|
|
141
158
|
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ManifestShareScopePlugin = void 0;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
8
|
+
var _runtimeCore = require("@module-federation/runtime-core");
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
const LEGACY_SHARE_SCOPE = 'chayns-api';
|
|
11
|
+
const HOST_REACT_SHARE_SCOPE = 'chayns-react';
|
|
12
|
+
const MANIFEST_SUFFIX = 'mf-manifest.json';
|
|
13
|
+
const isManifestUrl = url => {
|
|
14
|
+
try {
|
|
15
|
+
return new URL(url).pathname.endsWith(`/${MANIFEST_SUFFIX}`);
|
|
16
|
+
} catch {
|
|
17
|
+
return url.endsWith(MANIFEST_SUFFIX);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const ManifestShareScopePlugin = () => {
|
|
21
|
+
const legacyRemotes = new WeakSet();
|
|
22
|
+
return {
|
|
23
|
+
name: 'manifest-share-scope',
|
|
24
|
+
beforeRegisterRemote(args) {
|
|
25
|
+
if (args.remote.shareScope) {
|
|
26
|
+
legacyRemotes.add(args.remote);
|
|
27
|
+
} else if ('entry' in args.remote && args.remote.entry.endsWith('v2.remoteEntry.js')) {
|
|
28
|
+
Object.assign(args.remote, {
|
|
29
|
+
shareScope: LEGACY_SHARE_SCOPE
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return args;
|
|
33
|
+
},
|
|
34
|
+
async beforeRequest(args) {
|
|
35
|
+
var _manifest$metaData, _manifest$shared$find;
|
|
36
|
+
const remote = args.options.remotes.find(candidate => {
|
|
37
|
+
const name = candidate.name;
|
|
38
|
+
const alias = candidate.alias;
|
|
39
|
+
return args.id === name || args.id.startsWith(`${name}/`) || alias && (args.id === alias || args.id.startsWith(`${alias}/`));
|
|
40
|
+
});
|
|
41
|
+
if (!remote || legacyRemotes.has(remote) || !('entry' in remote) || !isManifestUrl(remote.entry)) {
|
|
42
|
+
return args;
|
|
43
|
+
}
|
|
44
|
+
await args.origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
45
|
+
moduleInfo: remote,
|
|
46
|
+
id: args.id
|
|
47
|
+
});
|
|
48
|
+
const manifest = args.origin.snapshotHandler.manifestCache.get(remote.entry);
|
|
49
|
+
const remoteShareScope = manifest === null || manifest === void 0 || (_manifest$metaData = manifest.metaData) === null || _manifest$metaData === void 0 ? void 0 : _manifest$metaData.reactShareScope;
|
|
50
|
+
const reactRequiredVersion = manifest === null || manifest === void 0 || (_manifest$shared$find = manifest.shared.find(shared => shared.name === 'react')) === null || _manifest$shared$find === void 0 ? void 0 : _manifest$shared$find.requiredVersion;
|
|
51
|
+
remote.shareScope = !remoteShareScope ? LEGACY_SHARE_SCOPE : reactRequiredVersion && (0, _runtimeCore.satisfy)(_react.default.version, reactRequiredVersion) ? `${HOST_REACT_SHARE_SCOPE}-${_react.default.version}` : remoteShareScope;
|
|
52
|
+
return args;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.ManifestShareScopePlugin = ManifestShareScopePlugin;
|
|
@@ -8,7 +8,8 @@ const SequentialLoadPlugin = () => {
|
|
|
8
8
|
return {
|
|
9
9
|
name: 'sequential-load',
|
|
10
10
|
async beforeInitContainer(args) {
|
|
11
|
-
|
|
11
|
+
var _args$origin$snapshot;
|
|
12
|
+
const manifest = (_args$origin$snapshot = args.origin.snapshotHandler.manifestCache.get(args.remoteInfo.entry)) !== null && _args$origin$snapshot !== void 0 ? _args$origin$snapshot : args.origin.snapshotHandler.manifestCache.get(args.remoteEntryInitOptions.version);
|
|
12
13
|
const requiredShares = new Set(manifest === null || manifest === void 0 ? void 0 : manifest.shared.map(share => share.name));
|
|
13
14
|
for (const [key, pkg] of Object.entries(args.shareScope)) {
|
|
14
15
|
if (manifest && !requiredShares.has(key)) {
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
Object.defineProperty(exports, "ManifestShareScopePlugin", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _ManifestShareScopePlugin.ManifestShareScopePlugin;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
6
12
|
Object.defineProperty(exports, "SSRManifestPlugin", {
|
|
7
13
|
enumerable: true,
|
|
8
14
|
get: function () {
|
|
@@ -21,5 +27,6 @@ Object.defineProperty(exports, "isTrustedDomainsError", {
|
|
|
21
27
|
return _TrustedDomainsPlugins.isTrustedDomainsError;
|
|
22
28
|
}
|
|
23
29
|
});
|
|
30
|
+
var _ManifestShareScopePlugin = require("./ManifestShareScopePlugin");
|
|
24
31
|
var _SSRManifestPlugin = require("./SSRManifestPlugin");
|
|
25
32
|
var _TrustedDomainsPlugins = require("./TrustedDomainsPlugins");
|
|
@@ -9,8 +9,67 @@ var _jsxRuntime = _interopRequireDefault(require("react/jsx-runtime"));
|
|
|
9
9
|
var _reactDom = _interopRequireDefault(require("react-dom"));
|
|
10
10
|
var _client = _interopRequireDefault(require("react-dom/client"));
|
|
11
11
|
var _server = _interopRequireDefault(require("react-dom/server"));
|
|
12
|
+
var _injectExternalRuntimeCorePlugin = _interopRequireDefault(require("@module-federation/inject-external-runtime-core-plugin"));
|
|
13
|
+
var _ManifestShareScopePlugin = require("../plugins/ManifestShareScopePlugin");
|
|
12
14
|
var _SequentialLoadPlugin = require("../plugins/SequentialLoadPlugin");
|
|
13
15
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
+
const ERROR_CACHE_TIME = 60000;
|
|
17
|
+
const normalizeUrl = url => {
|
|
18
|
+
try {
|
|
19
|
+
return new URL(url).toString();
|
|
20
|
+
} catch {
|
|
21
|
+
return url;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const createLoadModule = () => async (scope, module, url, preventSingleton = false) => {
|
|
25
|
+
const {
|
|
26
|
+
loadRemote,
|
|
27
|
+
registerRemotes
|
|
28
|
+
} = globalThis.moduleFederationRuntime;
|
|
29
|
+
const {
|
|
30
|
+
registeredScopes,
|
|
31
|
+
moduleMap,
|
|
32
|
+
componentMap
|
|
33
|
+
} = globalThis.moduleFederationScopes;
|
|
34
|
+
const remoteUrl = normalizeUrl(url);
|
|
35
|
+
if (registeredScopes[scope] !== remoteUrl || preventSingleton) {
|
|
36
|
+
if (scope in registeredScopes) {
|
|
37
|
+
console.error(`[chayns-api] call registerRemote with force for scope ${scope}. url: ${remoteUrl}`);
|
|
38
|
+
}
|
|
39
|
+
registerRemotes([{
|
|
40
|
+
name: scope,
|
|
41
|
+
entry: url
|
|
42
|
+
}], {
|
|
43
|
+
force: scope in registeredScopes || preventSingleton
|
|
44
|
+
});
|
|
45
|
+
registeredScopes[scope] = remoteUrl;
|
|
46
|
+
moduleMap[scope] = {};
|
|
47
|
+
componentMap[scope] = {};
|
|
48
|
+
}
|
|
49
|
+
if (!(module in moduleMap[scope])) {
|
|
50
|
+
const path = `${scope}/${module.replace(/^\.\//, '')}`;
|
|
51
|
+
const promise = loadRemote(path);
|
|
52
|
+
promise.catch(error => {
|
|
53
|
+
var _globalThis$moduleFed;
|
|
54
|
+
console.error('[chayns-api] Failed to load module', scope, remoteUrl, error);
|
|
55
|
+
const key = `${scope}\n${module}\n${remoteUrl}`;
|
|
56
|
+
const errorResetTimeouts = (_globalThis$moduleFed = globalThis.moduleFederationScopes.errorResetTimeouts) !== null && _globalThis$moduleFed !== void 0 ? _globalThis$moduleFed : new Set();
|
|
57
|
+
globalThis.moduleFederationScopes.errorResetTimeouts = errorResetTimeouts;
|
|
58
|
+
if (errorResetTimeouts.has(key)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
errorResetTimeouts.add(key);
|
|
62
|
+
setTimeout(() => {
|
|
63
|
+
if (registeredScopes[scope] === remoteUrl) {
|
|
64
|
+
registeredScopes[scope] = '';
|
|
65
|
+
}
|
|
66
|
+
errorResetTimeouts.delete(key);
|
|
67
|
+
}, ERROR_CACHE_TIME);
|
|
68
|
+
});
|
|
69
|
+
moduleMap[scope][module] = promise;
|
|
70
|
+
}
|
|
71
|
+
return moduleMap[scope][module];
|
|
72
|
+
};
|
|
14
73
|
const initModuleFederationSharing = ({
|
|
15
74
|
scope,
|
|
16
75
|
name,
|
|
@@ -26,27 +85,27 @@ const initModuleFederationSharing = ({
|
|
|
26
85
|
const shared = {
|
|
27
86
|
react: {
|
|
28
87
|
version: _react.default.version,
|
|
29
|
-
scope: 'chayns-api',
|
|
88
|
+
scope: [`chayns-react-${_react.default.version}`, 'chayns-api'],
|
|
30
89
|
lib: () => _react.default
|
|
31
90
|
},
|
|
32
91
|
'react-dom': {
|
|
33
92
|
version: _react.default.version,
|
|
34
|
-
scope: 'chayns-api',
|
|
93
|
+
scope: [`chayns-react-${_react.default.version}`, 'chayns-api'],
|
|
35
94
|
lib: () => _reactDom.default
|
|
36
95
|
},
|
|
37
96
|
'react-dom/client': {
|
|
38
97
|
version: _react.default.version,
|
|
39
|
-
scope: 'chayns-api',
|
|
98
|
+
scope: [`chayns-react-${_react.default.version}`, 'chayns-api'],
|
|
40
99
|
lib: () => _client.default
|
|
41
100
|
},
|
|
42
101
|
'react-dom/server': {
|
|
43
102
|
version: _react.default.version,
|
|
44
|
-
scope: 'chayns-api',
|
|
103
|
+
scope: [`chayns-react-${_react.default.version}`, 'chayns-api'],
|
|
45
104
|
lib: () => _server.default
|
|
46
105
|
},
|
|
47
106
|
'react/jsx-runtime': {
|
|
48
107
|
version: _react.default.version,
|
|
49
|
-
scope: 'chayns-api',
|
|
108
|
+
scope: [`chayns-react-${_react.default.version}`, 'chayns-api'],
|
|
50
109
|
lib: () => _jsxRuntime.default
|
|
51
110
|
}
|
|
52
111
|
};
|
|
@@ -54,9 +113,10 @@ const initModuleFederationSharing = ({
|
|
|
54
113
|
name: (_ref = scope !== null && scope !== void 0 ? scope : name) !== null && _ref !== void 0 ? _ref : '',
|
|
55
114
|
remotes: [],
|
|
56
115
|
shared,
|
|
57
|
-
plugins: [(0, _SequentialLoadPlugin.SequentialLoadPlugin)(), ...plugins]
|
|
116
|
+
plugins: [(0, _SequentialLoadPlugin.SequentialLoadPlugin)(), (0, _ManifestShareScopePlugin.ManifestShareScopePlugin)(), (0, _injectExternalRuntimeCorePlugin.default)(), ...plugins]
|
|
58
117
|
});
|
|
59
118
|
globalThis.moduleFederationRuntime = {
|
|
119
|
+
loadModule: createLoadModule(),
|
|
60
120
|
loadRemote: instance.loadRemote.bind(instance),
|
|
61
121
|
registerRemotes: instance.registerRemotes.bind(instance),
|
|
62
122
|
loadShareSync: instance.loadShareSync.bind(instance),
|
|
@@ -66,6 +126,7 @@ const initModuleFederationSharing = ({
|
|
|
66
126
|
registeredScopes: {},
|
|
67
127
|
moduleMap: {},
|
|
68
128
|
componentMap: {},
|
|
129
|
+
componentRegistrationKeys: {},
|
|
69
130
|
errorResetTimeouts: new Set()
|
|
70
131
|
};
|
|
71
132
|
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
2
2
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
3
3
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
4
|
-
import React from
|
|
5
|
-
const ERROR_CACHE_TIME = 60000;
|
|
4
|
+
import React from 'react';
|
|
6
5
|
const normalizeUrl = url => {
|
|
7
6
|
try {
|
|
8
7
|
return new URL(url).toString();
|
|
@@ -28,12 +27,13 @@ const resetAfterCacheTime = (scope, module, url) => {
|
|
|
28
27
|
registeredScopes[scope] = '';
|
|
29
28
|
}
|
|
30
29
|
errorResetTimeouts.delete(key);
|
|
31
|
-
},
|
|
30
|
+
}, 60000);
|
|
32
31
|
};
|
|
33
|
-
|
|
32
|
+
const legacyLoadModule = (scope, module, url, preventSingleton = false) => {
|
|
34
33
|
if (!globalThis.moduleFederationRuntime || !globalThis.moduleFederationScopes) {
|
|
35
34
|
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
36
35
|
}
|
|
36
|
+
console.warn('[chayns-api] Using legacy loadModule implementation. Please update the host to 3.7.0 or higher to use the new loadModule implementation.');
|
|
37
37
|
const {
|
|
38
38
|
loadRemote,
|
|
39
39
|
registerRemotes
|
|
@@ -70,7 +70,15 @@ export const loadModule = (scope, module, url, preventSingleton = false) => {
|
|
|
70
70
|
}
|
|
71
71
|
return moduleMap[scope][module];
|
|
72
72
|
};
|
|
73
|
+
export const loadModule = (...args) => {
|
|
74
|
+
var _globalThis$moduleFed, _globalThis$moduleFed2, _globalThis$moduleFed3;
|
|
75
|
+
if (!globalThis.moduleFederationRuntime) {
|
|
76
|
+
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
77
|
+
}
|
|
78
|
+
return (_globalThis$moduleFed = (_globalThis$moduleFed2 = (_globalThis$moduleFed3 = globalThis.moduleFederationRuntime).loadModule) === null || _globalThis$moduleFed2 === void 0 ? void 0 : _globalThis$moduleFed2.call(_globalThis$moduleFed3, ...args)) !== null && _globalThis$moduleFed !== void 0 ? _globalThis$moduleFed : legacyLoadModule(...args);
|
|
79
|
+
};
|
|
73
80
|
const loadComponent = (scope, module, url, skipCompatMode = false, preventSingleton = false) => {
|
|
81
|
+
var _globalThis$moduleFed4, _globalThis$moduleFed5;
|
|
74
82
|
if (skipCompatMode) {
|
|
75
83
|
console.warn('[chayns-api] skipCompatMode-option is deprecated and is set automatically now');
|
|
76
84
|
}
|
|
@@ -78,24 +86,32 @@ const loadComponent = (scope, module, url, skipCompatMode = false, preventSingle
|
|
|
78
86
|
throw new Error('[chayns-api] moduleFederationSharing has not been initialized. Make sure to call initModuleFederationSharing.');
|
|
79
87
|
}
|
|
80
88
|
const {
|
|
81
|
-
getInstance
|
|
89
|
+
getInstance,
|
|
90
|
+
loadModule: hostLoadModule
|
|
82
91
|
} = globalThis.moduleFederationRuntime;
|
|
83
92
|
const {
|
|
84
|
-
componentMap
|
|
85
|
-
registeredScopes
|
|
93
|
+
componentMap
|
|
86
94
|
} = globalThis.moduleFederationScopes;
|
|
95
|
+
const componentRegistrationKeys = (_globalThis$moduleFed5 = (_globalThis$moduleFed4 = globalThis.moduleFederationScopes).componentRegistrationKeys) !== null && _globalThis$moduleFed5 !== void 0 ? _globalThis$moduleFed5 : _globalThis$moduleFed4.componentRegistrationKeys = {};
|
|
87
96
|
if (!componentMap[scope]) {
|
|
88
97
|
componentMap[scope] = {};
|
|
89
98
|
}
|
|
90
|
-
if (!
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
if (!componentRegistrationKeys[scope]) {
|
|
100
|
+
componentRegistrationKeys[scope] = {};
|
|
101
|
+
}
|
|
102
|
+
if (!(module in componentMap[scope]) || componentRegistrationKeys[scope][module] !== url) {
|
|
103
|
+
const promise = (hostLoadModule !== null && hostLoadModule !== void 0 ? hostLoadModule : legacyLoadModule)(scope, module, url, preventSingleton).then(Module => {
|
|
93
104
|
if (typeof Module.default === 'function') {
|
|
94
105
|
return Module;
|
|
95
106
|
}
|
|
96
107
|
const shareScopes = getInstance().shareScopeMap;
|
|
97
|
-
const
|
|
98
|
-
|
|
108
|
+
const matchReactVersion = Object.values(shareScopes).map(shareScope => {
|
|
109
|
+
var _shareScope$react;
|
|
110
|
+
return (_shareScope$react = shareScope.react) === null || _shareScope$react === void 0 ? void 0 : _shareScope$react[React.version];
|
|
111
|
+
}).some(reactShare => {
|
|
112
|
+
var _reactShare$lib;
|
|
113
|
+
return (reactShare === null || reactShare === void 0 ? void 0 : reactShare.useIn.includes(scope)) && ((_reactShare$lib = reactShare.lib) === null || _reactShare$lib === void 0 ? void 0 : _reactShare$lib.call(reactShare)) === React;
|
|
114
|
+
});
|
|
99
115
|
if (!matchReactVersion || Module.default.environment !== 'production' || (Module.default.version || 1) < 2) {
|
|
100
116
|
const OriginalCompatComponent = (Module.default.version || 1) < 2.1 ? Module.default.CompatComponent.render({}).type.prototype : Module.default.CompatComponent.prototype;
|
|
101
117
|
class CompatComponent extends React.Component {
|
|
@@ -128,10 +144,11 @@ const loadComponent = (scope, module, url, skipCompatMode = false, preventSingle
|
|
|
128
144
|
default: Module.default.Component
|
|
129
145
|
};
|
|
130
146
|
});
|
|
131
|
-
promise.catch(
|
|
132
|
-
console.error(
|
|
147
|
+
promise.catch(error => {
|
|
148
|
+
console.error('[chayns-api] Failed to load component', scope, url, error);
|
|
133
149
|
});
|
|
134
150
|
componentMap[scope][module] = React.lazy(() => promise);
|
|
151
|
+
componentRegistrationKeys[scope][module] = url;
|
|
135
152
|
}
|
|
136
153
|
return componentMap[scope][module];
|
|
137
154
|
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { satisfy } from '@module-federation/runtime-core';
|
|
3
|
+
const LEGACY_SHARE_SCOPE = 'chayns-api';
|
|
4
|
+
const HOST_REACT_SHARE_SCOPE = 'chayns-react';
|
|
5
|
+
const MANIFEST_SUFFIX = 'mf-manifest.json';
|
|
6
|
+
const isManifestUrl = url => {
|
|
7
|
+
try {
|
|
8
|
+
return new URL(url).pathname.endsWith(`/${MANIFEST_SUFFIX}`);
|
|
9
|
+
} catch {
|
|
10
|
+
return url.endsWith(MANIFEST_SUFFIX);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export const ManifestShareScopePlugin = () => {
|
|
14
|
+
const legacyRemotes = new WeakSet();
|
|
15
|
+
return {
|
|
16
|
+
name: 'manifest-share-scope',
|
|
17
|
+
beforeRegisterRemote(args) {
|
|
18
|
+
if (args.remote.shareScope) {
|
|
19
|
+
legacyRemotes.add(args.remote);
|
|
20
|
+
} else if ('entry' in args.remote && args.remote.entry.endsWith('v2.remoteEntry.js')) {
|
|
21
|
+
Object.assign(args.remote, {
|
|
22
|
+
shareScope: LEGACY_SHARE_SCOPE
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return args;
|
|
26
|
+
},
|
|
27
|
+
async beforeRequest(args) {
|
|
28
|
+
var _manifest$metaData, _manifest$shared$find;
|
|
29
|
+
const remote = args.options.remotes.find(candidate => {
|
|
30
|
+
const name = candidate.name;
|
|
31
|
+
const alias = candidate.alias;
|
|
32
|
+
return args.id === name || args.id.startsWith(`${name}/`) || alias && (args.id === alias || args.id.startsWith(`${alias}/`));
|
|
33
|
+
});
|
|
34
|
+
if (!remote || legacyRemotes.has(remote) || !('entry' in remote) || !isManifestUrl(remote.entry)) {
|
|
35
|
+
return args;
|
|
36
|
+
}
|
|
37
|
+
await args.origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
38
|
+
moduleInfo: remote,
|
|
39
|
+
id: args.id
|
|
40
|
+
});
|
|
41
|
+
const manifest = args.origin.snapshotHandler.manifestCache.get(remote.entry);
|
|
42
|
+
const remoteShareScope = manifest === null || manifest === void 0 || (_manifest$metaData = manifest.metaData) === null || _manifest$metaData === void 0 ? void 0 : _manifest$metaData.reactShareScope;
|
|
43
|
+
const reactRequiredVersion = manifest === null || manifest === void 0 || (_manifest$shared$find = manifest.shared.find(shared => shared.name === 'react')) === null || _manifest$shared$find === void 0 ? void 0 : _manifest$shared$find.requiredVersion;
|
|
44
|
+
remote.shareScope = !remoteShareScope ? LEGACY_SHARE_SCOPE : reactRequiredVersion && satisfy(React.version, reactRequiredVersion) ? `${HOST_REACT_SHARE_SCOPE}-${React.version}` : remoteShareScope;
|
|
45
|
+
return args;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
@@ -2,7 +2,8 @@ export const SequentialLoadPlugin = () => {
|
|
|
2
2
|
return {
|
|
3
3
|
name: 'sequential-load',
|
|
4
4
|
async beforeInitContainer(args) {
|
|
5
|
-
|
|
5
|
+
var _args$origin$snapshot;
|
|
6
|
+
const manifest = (_args$origin$snapshot = args.origin.snapshotHandler.manifestCache.get(args.remoteInfo.entry)) !== null && _args$origin$snapshot !== void 0 ? _args$origin$snapshot : args.origin.snapshotHandler.manifestCache.get(args.remoteEntryInitOptions.version);
|
|
6
7
|
const requiredShares = new Set(manifest === null || manifest === void 0 ? void 0 : manifest.shared.map(share => share.name));
|
|
7
8
|
for (const [key, pkg] of Object.entries(args.shareScope)) {
|
|
8
9
|
if (manifest && !requiredShares.has(key)) {
|
|
@@ -3,7 +3,66 @@ import JSXRuntime from 'react/jsx-runtime';
|
|
|
3
3
|
import ReactDOM from 'react-dom';
|
|
4
4
|
import ReactDOMClient from 'react-dom/client';
|
|
5
5
|
import ReactDOMServer from 'react-dom/server';
|
|
6
|
+
import injectExternalRuntimeCodePlugin from '@module-federation/inject-external-runtime-core-plugin';
|
|
7
|
+
import { ManifestShareScopePlugin } from '../plugins/ManifestShareScopePlugin';
|
|
6
8
|
import { SequentialLoadPlugin } from '../plugins/SequentialLoadPlugin';
|
|
9
|
+
const ERROR_CACHE_TIME = 60000;
|
|
10
|
+
const normalizeUrl = url => {
|
|
11
|
+
try {
|
|
12
|
+
return new URL(url).toString();
|
|
13
|
+
} catch {
|
|
14
|
+
return url;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const createLoadModule = () => async (scope, module, url, preventSingleton = false) => {
|
|
18
|
+
const {
|
|
19
|
+
loadRemote,
|
|
20
|
+
registerRemotes
|
|
21
|
+
} = globalThis.moduleFederationRuntime;
|
|
22
|
+
const {
|
|
23
|
+
registeredScopes,
|
|
24
|
+
moduleMap,
|
|
25
|
+
componentMap
|
|
26
|
+
} = globalThis.moduleFederationScopes;
|
|
27
|
+
const remoteUrl = normalizeUrl(url);
|
|
28
|
+
if (registeredScopes[scope] !== remoteUrl || preventSingleton) {
|
|
29
|
+
if (scope in registeredScopes) {
|
|
30
|
+
console.error(`[chayns-api] call registerRemote with force for scope ${scope}. url: ${remoteUrl}`);
|
|
31
|
+
}
|
|
32
|
+
registerRemotes([{
|
|
33
|
+
name: scope,
|
|
34
|
+
entry: url
|
|
35
|
+
}], {
|
|
36
|
+
force: scope in registeredScopes || preventSingleton
|
|
37
|
+
});
|
|
38
|
+
registeredScopes[scope] = remoteUrl;
|
|
39
|
+
moduleMap[scope] = {};
|
|
40
|
+
componentMap[scope] = {};
|
|
41
|
+
}
|
|
42
|
+
if (!(module in moduleMap[scope])) {
|
|
43
|
+
const path = `${scope}/${module.replace(/^\.\//, '')}`;
|
|
44
|
+
const promise = loadRemote(path);
|
|
45
|
+
promise.catch(error => {
|
|
46
|
+
var _globalThis$moduleFed;
|
|
47
|
+
console.error('[chayns-api] Failed to load module', scope, remoteUrl, error);
|
|
48
|
+
const key = `${scope}\n${module}\n${remoteUrl}`;
|
|
49
|
+
const errorResetTimeouts = (_globalThis$moduleFed = globalThis.moduleFederationScopes.errorResetTimeouts) !== null && _globalThis$moduleFed !== void 0 ? _globalThis$moduleFed : new Set();
|
|
50
|
+
globalThis.moduleFederationScopes.errorResetTimeouts = errorResetTimeouts;
|
|
51
|
+
if (errorResetTimeouts.has(key)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
errorResetTimeouts.add(key);
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
if (registeredScopes[scope] === remoteUrl) {
|
|
57
|
+
registeredScopes[scope] = '';
|
|
58
|
+
}
|
|
59
|
+
errorResetTimeouts.delete(key);
|
|
60
|
+
}, ERROR_CACHE_TIME);
|
|
61
|
+
});
|
|
62
|
+
moduleMap[scope][module] = promise;
|
|
63
|
+
}
|
|
64
|
+
return moduleMap[scope][module];
|
|
65
|
+
};
|
|
7
66
|
export const initModuleFederationSharing = ({
|
|
8
67
|
scope,
|
|
9
68
|
name,
|
|
@@ -19,27 +78,27 @@ export const initModuleFederationSharing = ({
|
|
|
19
78
|
const shared = {
|
|
20
79
|
react: {
|
|
21
80
|
version: React.version,
|
|
22
|
-
scope: 'chayns-api',
|
|
81
|
+
scope: [`chayns-react-${React.version}`, 'chayns-api'],
|
|
23
82
|
lib: () => React
|
|
24
83
|
},
|
|
25
84
|
'react-dom': {
|
|
26
85
|
version: React.version,
|
|
27
|
-
scope: 'chayns-api',
|
|
86
|
+
scope: [`chayns-react-${React.version}`, 'chayns-api'],
|
|
28
87
|
lib: () => ReactDOM
|
|
29
88
|
},
|
|
30
89
|
'react-dom/client': {
|
|
31
90
|
version: React.version,
|
|
32
|
-
scope: 'chayns-api',
|
|
91
|
+
scope: [`chayns-react-${React.version}`, 'chayns-api'],
|
|
33
92
|
lib: () => ReactDOMClient
|
|
34
93
|
},
|
|
35
94
|
'react-dom/server': {
|
|
36
95
|
version: React.version,
|
|
37
|
-
scope: 'chayns-api',
|
|
96
|
+
scope: [`chayns-react-${React.version}`, 'chayns-api'],
|
|
38
97
|
lib: () => ReactDOMServer
|
|
39
98
|
},
|
|
40
99
|
'react/jsx-runtime': {
|
|
41
100
|
version: React.version,
|
|
42
|
-
scope: 'chayns-api',
|
|
101
|
+
scope: [`chayns-react-${React.version}`, 'chayns-api'],
|
|
43
102
|
lib: () => JSXRuntime
|
|
44
103
|
}
|
|
45
104
|
};
|
|
@@ -47,9 +106,10 @@ export const initModuleFederationSharing = ({
|
|
|
47
106
|
name: (_ref = scope !== null && scope !== void 0 ? scope : name) !== null && _ref !== void 0 ? _ref : '',
|
|
48
107
|
remotes: [],
|
|
49
108
|
shared,
|
|
50
|
-
plugins: [SequentialLoadPlugin(), ...plugins]
|
|
109
|
+
plugins: [SequentialLoadPlugin(), ManifestShareScopePlugin(), injectExternalRuntimeCodePlugin(), ...plugins]
|
|
51
110
|
});
|
|
52
111
|
globalThis.moduleFederationRuntime = {
|
|
112
|
+
loadModule: createLoadModule(),
|
|
53
113
|
loadRemote: instance.loadRemote.bind(instance),
|
|
54
114
|
registerRemotes: instance.registerRemotes.bind(instance),
|
|
55
115
|
loadShareSync: instance.loadShareSync.bind(instance),
|
|
@@ -59,6 +119,7 @@ export const initModuleFederationSharing = ({
|
|
|
59
119
|
registeredScopes: {},
|
|
60
120
|
moduleMap: {},
|
|
61
121
|
componentMap: {},
|
|
122
|
+
componentRegistrationKeys: {},
|
|
62
123
|
errorResetTimeouts: new Set()
|
|
63
124
|
};
|
|
64
125
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type LoadModule = (scope: string, module: string, url: string, preventSingleton?: boolean) => Promise<unknown>;
|
|
3
|
+
export declare const loadModule: LoadModule;
|
|
3
4
|
declare const loadComponent: (scope: string, module: string, url: string, skipCompatMode?: boolean, preventSingleton?: boolean) => React.LazyExoticComponent<React.ComponentType<any>>;
|
|
4
5
|
export default loadComponent;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
|
|
2
|
-
export declare const initModuleFederationSharing: ({ scope, name, plugins }: {
|
|
2
|
+
export declare const initModuleFederationSharing: ({ scope, name, plugins, }: {
|
|
3
3
|
/**
|
|
4
4
|
* Module Federation scope; should be identical to the package name in package.json, formatted in snake_case.
|
|
5
5
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chayns-api",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0-beta.1",
|
|
4
4
|
"description": "new chayns api",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"@babel/preset-typescript": "^7.28.5",
|
|
64
64
|
"@chayns-toolkit/eslint-config": "^3.0.2",
|
|
65
65
|
"@module-federation/enhanced": "^2.4.0",
|
|
66
|
+
"@module-federation/inject-external-runtime-core-plugin": "^2.4.0",
|
|
66
67
|
"@rsbuild/plugin-umd": "^1.0.5",
|
|
67
68
|
"@types/htmlescape": "^1.1.3",
|
|
68
69
|
"@types/lodash.throttle": "^4.1.9",
|
|
@@ -83,6 +84,7 @@
|
|
|
83
84
|
},
|
|
84
85
|
"peerDependencies": {
|
|
85
86
|
"@module-federation/enhanced": "^2.4.0",
|
|
87
|
+
"@module-federation/inject-external-runtime-core-plugin": "^2.4.0",
|
|
86
88
|
"react": "^18.0.0 || ^19.0.0",
|
|
87
89
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
88
90
|
}
|