@module-federation/utilities 3.1.83 → 3.1.84
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/Logger.js +60 -0
- package/dist/cjs/components/ErrorBoundary.js +96 -0
- package/dist/cjs/components/FederationBoundary.js +101 -0
- package/dist/cjs/index.js +170 -0
- package/dist/cjs/plugins/DelegateModulesPlugin.js +130 -0
- package/dist/cjs/types/index.js +33 -0
- package/dist/cjs/utils/common.js +173 -0
- package/dist/cjs/utils/getRuntimeRemotes.js +102 -0
- package/dist/cjs/utils/getRuntimeRemotes.test.js +98 -0
- package/dist/cjs/utils/importDelegateModule.test.js +101 -0
- package/dist/cjs/utils/importDelegatedModule.js +124 -0
- package/dist/cjs/utils/importRemote.js +174 -0
- package/dist/cjs/utils/isEmpty.js +56 -0
- package/dist/cjs/utils/pure.js +212 -0
- package/dist/cjs/utils/react.js +71 -0
- package/dist/esm/Logger.mjs +14 -0
- package/dist/esm/components/ErrorBoundary.mjs +33 -0
- package/dist/esm/components/FederationBoundary.mjs +38 -0
- package/dist/esm/index.mjs +34 -0
- package/dist/esm/plugins/DelegateModulesPlugin.mjs +84 -0
- package/dist/esm/types/index.mjs +6 -0
- package/dist/esm/utils/common.mjs +126 -0
- package/dist/esm/utils/getRuntimeRemotes.mjs +52 -0
- package/dist/esm/utils/getRuntimeRemotes.test.mjs +87 -0
- package/dist/esm/utils/importDelegateModule.test.mjs +90 -0
- package/dist/esm/utils/importDelegatedModule.mjs +74 -0
- package/dist/esm/utils/importRemote.mjs +137 -0
- package/dist/esm/utils/isEmpty.mjs +10 -0
- package/dist/esm/utils/pure.mjs +164 -0
- package/dist/esm/utils/react.mjs +8 -0
- package/package.json +2 -2
- package/dist/index.cjs.js +0 -555
- package/dist/index.d.ts +0 -1
- package/dist/index.esm.js +0 -543
- /package/dist/{src → types}/Logger.d.ts +0 -0
- /package/dist/{src → types}/components/ErrorBoundary.d.ts +0 -0
- /package/dist/{src → types}/components/FederationBoundary.d.ts +0 -0
- /package/dist/{src → types}/index.d.ts +0 -0
- /package/dist/{src → types}/plugins/DelegateModulesPlugin.d.ts +0 -0
- /package/dist/{src → types}/types/index.d.ts +0 -0
- /package/dist/{src → types}/utils/common.d.ts +0 -0
- /package/dist/{src → types}/utils/getRuntimeRemotes.d.ts +0 -0
- /package/dist/{src → types}/utils/importDelegatedModule.d.ts +0 -0
- /package/dist/{src → types}/utils/importRemote.d.ts +0 -0
- /package/dist/{src → types}/utils/isEmpty.d.ts +0 -0
- /package/dist/{src → types}/utils/pure.d.ts +0 -0
- /package/dist/{src → types}/utils/react.d.ts +0 -0
package/dist/index.cjs.js
DELETED
|
@@ -1,555 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/******************************************************************************
|
|
4
|
-
Copyright (c) Microsoft Corporation.
|
|
5
|
-
|
|
6
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
7
|
-
purpose with or without fee is hereby granted.
|
|
8
|
-
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
-
***************************************************************************** */
|
|
17
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
31
|
-
var e = new Error(message);
|
|
32
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const pure = typeof process !== 'undefined' ? process.env['REMOTES'] || {} : {};
|
|
36
|
-
const remoteVars = pure;
|
|
37
|
-
const extractUrlAndGlobal = (urlAndGlobal) => {
|
|
38
|
-
const index = urlAndGlobal.indexOf('@');
|
|
39
|
-
if (index <= 0 || index === urlAndGlobal.length - 1) {
|
|
40
|
-
throw new Error(`Invalid request "${urlAndGlobal}"`);
|
|
41
|
-
}
|
|
42
|
-
return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
|
|
43
|
-
};
|
|
44
|
-
const loadScript = (keyOrRuntimeRemoteItem) => {
|
|
45
|
-
const runtimeRemotes = getRuntimeRemotes$1();
|
|
46
|
-
// 1) Load remote container if needed
|
|
47
|
-
let asyncContainer;
|
|
48
|
-
const reference = typeof keyOrRuntimeRemoteItem === 'string'
|
|
49
|
-
? runtimeRemotes[keyOrRuntimeRemoteItem]
|
|
50
|
-
: keyOrRuntimeRemoteItem;
|
|
51
|
-
if (reference.asyncContainer) {
|
|
52
|
-
asyncContainer =
|
|
53
|
-
typeof reference.asyncContainer.then === 'function'
|
|
54
|
-
? reference.asyncContainer
|
|
55
|
-
: // @ts-ignore
|
|
56
|
-
reference.asyncContainer();
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
// This casting is just to satisfy typescript,
|
|
60
|
-
// In reality remoteGlobal will always be a string;
|
|
61
|
-
const remoteGlobal = reference.global;
|
|
62
|
-
// Check if theres an override for container key if not use remote global
|
|
63
|
-
const containerKey = reference.uniqueKey
|
|
64
|
-
? reference.uniqueKey
|
|
65
|
-
: remoteGlobal;
|
|
66
|
-
const __webpack_error__ = new Error();
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
const globalScope =
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
typeof window !== 'undefined' ? window : globalThis.__remote_scope__;
|
|
71
|
-
if (typeof window === 'undefined') {
|
|
72
|
-
//@ts-ignore
|
|
73
|
-
globalScope['_config'][containerKey] = reference.url;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// to match promise template system, can be removed once promise template is gone
|
|
77
|
-
//@ts-ignore
|
|
78
|
-
if (!globalScope['remoteLoading']) {
|
|
79
|
-
//@ts-ignore
|
|
80
|
-
globalScope['remoteLoading'] = {};
|
|
81
|
-
}
|
|
82
|
-
//@ts-ignore
|
|
83
|
-
if (globalScope['remoteLoading'][containerKey]) {
|
|
84
|
-
//@ts-ignore
|
|
85
|
-
return globalScope['remoteLoading'][containerKey];
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// @ts-ignore
|
|
89
|
-
asyncContainer = new Promise(function (resolve, reject) {
|
|
90
|
-
function resolveRemoteGlobal() {
|
|
91
|
-
//@ts-ignore
|
|
92
|
-
const asyncContainer = globalScope[remoteGlobal];
|
|
93
|
-
return resolve(asyncContainer);
|
|
94
|
-
}
|
|
95
|
-
//@ts-ignore
|
|
96
|
-
if (typeof globalScope[remoteGlobal] !== 'undefined') {
|
|
97
|
-
return resolveRemoteGlobal();
|
|
98
|
-
}
|
|
99
|
-
__webpack_require__.l(reference.url, function (event) {
|
|
100
|
-
//@ts-ignore
|
|
101
|
-
if (typeof globalScope[remoteGlobal] !== 'undefined') {
|
|
102
|
-
return resolveRemoteGlobal();
|
|
103
|
-
}
|
|
104
|
-
const errorType = event && (event.type === 'load' ? 'missing' : event.type);
|
|
105
|
-
const realSrc = event && event.target && event.target.src;
|
|
106
|
-
__webpack_error__.message =
|
|
107
|
-
'Loading script failed.\n(' +
|
|
108
|
-
errorType +
|
|
109
|
-
': ' +
|
|
110
|
-
realSrc +
|
|
111
|
-
' or global var ' +
|
|
112
|
-
remoteGlobal +
|
|
113
|
-
')';
|
|
114
|
-
__webpack_error__.name = 'ScriptExternalLoadError';
|
|
115
|
-
__webpack_error__.type = errorType;
|
|
116
|
-
__webpack_error__.request = realSrc;
|
|
117
|
-
reject(__webpack_error__);
|
|
118
|
-
}, containerKey);
|
|
119
|
-
}).catch(function (err) {
|
|
120
|
-
console.error('container is offline, returning fake remote');
|
|
121
|
-
console.error(err);
|
|
122
|
-
return {
|
|
123
|
-
fake: true,
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
get: (arg) => {
|
|
126
|
-
console.warn('faking', arg, 'module on, its offline');
|
|
127
|
-
return Promise.resolve(() => {
|
|
128
|
-
return {
|
|
129
|
-
__esModule: true,
|
|
130
|
-
default: () => {
|
|
131
|
-
return null;
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
//eslint-disable-next-line
|
|
137
|
-
init: () => { },
|
|
138
|
-
};
|
|
139
|
-
});
|
|
140
|
-
if (typeof window !== 'undefined') {
|
|
141
|
-
//@ts-ignore
|
|
142
|
-
globalScope['remoteLoading'][containerKey] = asyncContainer;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return asyncContainer;
|
|
146
|
-
};
|
|
147
|
-
const getRuntimeRemotes$1 = () => {
|
|
148
|
-
return Object.entries(remoteVars).reduce((acc, [key, value]) => {
|
|
149
|
-
if (typeof value === 'object' && typeof value.then === 'function') {
|
|
150
|
-
acc[key] = { asyncContainer: value };
|
|
151
|
-
}
|
|
152
|
-
else if (typeof value === 'function') {
|
|
153
|
-
acc[key] = { asyncContainer: Promise.resolve(value()) };
|
|
154
|
-
}
|
|
155
|
-
else if (typeof value === 'string') {
|
|
156
|
-
if (value.startsWith('internal ')) {
|
|
157
|
-
const [request, query] = value.replace('internal ', '').split('?');
|
|
158
|
-
if (query) {
|
|
159
|
-
const remoteSyntax = new URLSearchParams(query).get('remote');
|
|
160
|
-
if (remoteSyntax) {
|
|
161
|
-
const [url, global] = extractUrlAndGlobal(remoteSyntax);
|
|
162
|
-
acc[key] = { global, url };
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
const [url, global] = extractUrlAndGlobal(value);
|
|
168
|
-
acc[key] = { global, url };
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
console.warn('remotes process', process.env['REMOTES']);
|
|
173
|
-
throw new Error(`[mf] Invalid value received for runtime_remote "${key}"`);
|
|
174
|
-
}
|
|
175
|
-
return acc;
|
|
176
|
-
}, {});
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
180
|
-
const createContainerSharingScope = (asyncContainer) => {
|
|
181
|
-
// @ts-ignore
|
|
182
|
-
return asyncContainer
|
|
183
|
-
.then(function (container) {
|
|
184
|
-
if (!__webpack_share_scopes__['default']) {
|
|
185
|
-
// not always a promise, so we wrap it in a resolve
|
|
186
|
-
return Promise.resolve(__webpack_init_sharing__('default')).then(function () {
|
|
187
|
-
return container;
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
return container;
|
|
192
|
-
}
|
|
193
|
-
})
|
|
194
|
-
.then(function (container) {
|
|
195
|
-
try {
|
|
196
|
-
// WARNING: here might be a potential BUG.
|
|
197
|
-
// `container.init` does not return a Promise, and here we do not call `then` on it.
|
|
198
|
-
// But according to [docs](https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers)
|
|
199
|
-
// it must be async.
|
|
200
|
-
// The problem may be in Proxy in NextFederationPlugin.js.
|
|
201
|
-
// or maybe a bug in the webpack itself - instead of returning rejected promise it just throws an error.
|
|
202
|
-
// But now everything works properly and we keep this code as is.
|
|
203
|
-
container.init(__webpack_share_scopes__['default']);
|
|
204
|
-
}
|
|
205
|
-
catch (e) {
|
|
206
|
-
// maybe container already initialized so nothing to throw
|
|
207
|
-
}
|
|
208
|
-
return container;
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Return initialized remote container by remote's key or its runtime remote item data.
|
|
213
|
-
*
|
|
214
|
-
* `runtimeRemoteItem` might be
|
|
215
|
-
* { global, url } - values obtained from webpack remotes option `global@url`
|
|
216
|
-
* or
|
|
217
|
-
* { asyncContainer } - async container is a promise that resolves to the remote container
|
|
218
|
-
*/
|
|
219
|
-
const injectScript = (keyOrRuntimeRemoteItem) => __awaiter(void 0, void 0, void 0, function* () {
|
|
220
|
-
const asyncContainer = loadScript(keyOrRuntimeRemoteItem);
|
|
221
|
-
return createContainerSharingScope(asyncContainer);
|
|
222
|
-
});
|
|
223
|
-
/**
|
|
224
|
-
* Creates runtime variables from the provided remotes.
|
|
225
|
-
* If the value of a remote starts with 'promise ' or 'external ', it is transformed into a function that returns the promise call.
|
|
226
|
-
* Otherwise, the value is stringified.
|
|
227
|
-
* @param {Remotes} remotes - The remotes to create runtime variables from.
|
|
228
|
-
* @returns {Record<string, string>} - The created runtime variables.
|
|
229
|
-
*/
|
|
230
|
-
const createRuntimeVariables = (remotes) => {
|
|
231
|
-
if (!remotes) {
|
|
232
|
-
return {};
|
|
233
|
-
}
|
|
234
|
-
return Object.entries(remotes).reduce((acc, [key, value]) => {
|
|
235
|
-
if (value.startsWith('promise ') || value.startsWith('external ')) {
|
|
236
|
-
const promiseCall = value.split(' ')[1];
|
|
237
|
-
acc[key] = `function() {
|
|
238
|
-
return ${promiseCall}
|
|
239
|
-
}`;
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
acc[key] = JSON.stringify(value);
|
|
243
|
-
}
|
|
244
|
-
return acc;
|
|
245
|
-
}, {});
|
|
246
|
-
};
|
|
247
|
-
/**
|
|
248
|
-
* Returns initialized webpack RemoteContainer.
|
|
249
|
-
* If its' script does not loaded - then load & init it firstly.
|
|
250
|
-
*/
|
|
251
|
-
const getContainer = (remoteContainer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
252
|
-
if (!remoteContainer) {
|
|
253
|
-
throw Error(`Remote container options is empty`);
|
|
254
|
-
}
|
|
255
|
-
const containerScope = typeof window !== 'undefined'
|
|
256
|
-
? window
|
|
257
|
-
: globalThis.__remote_scope__;
|
|
258
|
-
let containerKey;
|
|
259
|
-
if (typeof remoteContainer === 'string') {
|
|
260
|
-
containerKey = remoteContainer;
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
containerKey = remoteContainer.uniqueKey;
|
|
264
|
-
if (!containerScope[containerKey]) {
|
|
265
|
-
const container = yield injectScript({
|
|
266
|
-
global: remoteContainer.global,
|
|
267
|
-
url: remoteContainer.url,
|
|
268
|
-
});
|
|
269
|
-
if (!container) {
|
|
270
|
-
throw Error(`Remote container ${remoteContainer.url} is empty`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
return containerScope[containerKey];
|
|
275
|
-
});
|
|
276
|
-
/**
|
|
277
|
-
* Return remote module from container.
|
|
278
|
-
* If you provide `exportName` it automatically return exact property value from module.
|
|
279
|
-
*
|
|
280
|
-
* @example
|
|
281
|
-
* remote.getModule('./pages/index', 'default')
|
|
282
|
-
*/
|
|
283
|
-
const getModule = (_a) => __awaiter(void 0, [_a], void 0, function* ({ remoteContainer, modulePath, exportName, }) {
|
|
284
|
-
const container = yield getContainer(remoteContainer);
|
|
285
|
-
try {
|
|
286
|
-
const modFactory = yield (container === null || container === void 0 ? void 0 : container.get(modulePath));
|
|
287
|
-
if (!modFactory) {
|
|
288
|
-
return undefined;
|
|
289
|
-
}
|
|
290
|
-
const mod = modFactory();
|
|
291
|
-
if (exportName) {
|
|
292
|
-
return mod && typeof mod === 'object' ? mod[exportName] : undefined;
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
return mod;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
catch (error) {
|
|
299
|
-
console.error(error);
|
|
300
|
-
return undefined;
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
const isObjectEmpty = (obj) => {
|
|
305
|
-
for (const x in obj) {
|
|
306
|
-
return false;
|
|
307
|
-
}
|
|
308
|
-
return true;
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Constant for remote entry file
|
|
313
|
-
* @constant {string}
|
|
314
|
-
*/
|
|
315
|
-
const REMOTE_ENTRY_FILE = 'remoteEntry.js';
|
|
316
|
-
/**
|
|
317
|
-
* Function to load remote
|
|
318
|
-
* @function
|
|
319
|
-
* @param {ImportRemoteOptions['url']} url - The url of the remote module
|
|
320
|
-
* @param {ImportRemoteOptions['scope']} scope - The scope of the remote module
|
|
321
|
-
* @param {ImportRemoteOptions['bustRemoteEntryCache']} bustRemoteEntryCache - Flag to bust the remote entry cache
|
|
322
|
-
* @returns {Promise<void>} A promise that resolves when the remote is loaded
|
|
323
|
-
*/
|
|
324
|
-
const loadRemote = (url, scope, bustRemoteEntryCache) => new Promise((resolve, reject) => {
|
|
325
|
-
const timestamp = bustRemoteEntryCache ? `?t=${new Date().getTime()}` : '';
|
|
326
|
-
const webpackRequire = __webpack_require__;
|
|
327
|
-
webpackRequire.l(`${url}${timestamp}`, (event) => {
|
|
328
|
-
var _a;
|
|
329
|
-
if ((event === null || event === void 0 ? void 0 : event.type) === 'load') {
|
|
330
|
-
// Script loaded successfully:
|
|
331
|
-
return resolve();
|
|
332
|
-
}
|
|
333
|
-
const realSrc = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.src;
|
|
334
|
-
const error = new Error();
|
|
335
|
-
error.message = 'Loading script failed.\n(missing: ' + realSrc + ')';
|
|
336
|
-
error.name = 'ScriptExternalLoadError';
|
|
337
|
-
reject(error);
|
|
338
|
-
}, scope);
|
|
339
|
-
});
|
|
340
|
-
const loadEsmRemote = (url, scope) => __awaiter(void 0, void 0, void 0, function* () {
|
|
341
|
-
const module = yield import(/* webpackIgnore: true */ url);
|
|
342
|
-
if (!module) {
|
|
343
|
-
throw new Error(`Unable to load requested remote from ${url} with scope ${scope}`);
|
|
344
|
-
}
|
|
345
|
-
window[scope] = Object.assign(Object.assign({}, module), { __initializing: false, __initialized: false });
|
|
346
|
-
});
|
|
347
|
-
/**
|
|
348
|
-
* Function to initialize sharing
|
|
349
|
-
* @async
|
|
350
|
-
* @function
|
|
351
|
-
*/
|
|
352
|
-
const initSharing = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
353
|
-
const webpackShareScopes = __webpack_share_scopes__;
|
|
354
|
-
if (!(webpackShareScopes === null || webpackShareScopes === void 0 ? void 0 : webpackShareScopes.default)) {
|
|
355
|
-
yield __webpack_init_sharing__('default');
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
/**
|
|
359
|
-
* Function to initialize container
|
|
360
|
-
* @async
|
|
361
|
-
* @function
|
|
362
|
-
* @param {WebpackRemoteContainer} containerScope - The container scope
|
|
363
|
-
*/
|
|
364
|
-
const initContainer = (containerScope) => __awaiter(void 0, void 0, void 0, function* () {
|
|
365
|
-
try {
|
|
366
|
-
const webpackShareScopes = __webpack_share_scopes__;
|
|
367
|
-
if (!containerScope.__initialized && !containerScope.__initializing) {
|
|
368
|
-
containerScope.__initializing = true;
|
|
369
|
-
yield containerScope.init(webpackShareScopes.default);
|
|
370
|
-
containerScope.__initialized = true;
|
|
371
|
-
delete containerScope.__initializing;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
catch (error) {
|
|
375
|
-
console.error(error);
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
/**
|
|
379
|
-
* Function to import remote
|
|
380
|
-
* @async
|
|
381
|
-
* @function
|
|
382
|
-
* @param {ImportRemoteOptions} options - The options for importing the remote
|
|
383
|
-
* @returns {Promise<T>} A promise that resolves with the imported module
|
|
384
|
-
*/
|
|
385
|
-
const importRemote = (_a) => __awaiter(void 0, [_a], void 0, function* ({ url, scope, module, remoteEntryFileName = REMOTE_ENTRY_FILE, bustRemoteEntryCache = true, esm = false, }) {
|
|
386
|
-
const remoteScope = scope;
|
|
387
|
-
if (!window[remoteScope]) {
|
|
388
|
-
let remoteUrl = '';
|
|
389
|
-
if (typeof url === 'string') {
|
|
390
|
-
remoteUrl = url;
|
|
391
|
-
}
|
|
392
|
-
else {
|
|
393
|
-
remoteUrl = yield url();
|
|
394
|
-
}
|
|
395
|
-
const remoteUrlWithEntryFile = `${remoteUrl}/${remoteEntryFileName}`;
|
|
396
|
-
const asyncContainer = !esm
|
|
397
|
-
? loadRemote(remoteUrlWithEntryFile, scope, bustRemoteEntryCache)
|
|
398
|
-
: loadEsmRemote(remoteUrlWithEntryFile, scope);
|
|
399
|
-
// Load the remote and initialize the share scope if it's empty
|
|
400
|
-
yield Promise.all([asyncContainer, initSharing()]);
|
|
401
|
-
if (!window[remoteScope]) {
|
|
402
|
-
throw new Error(`Remote loaded successfully but ${scope} could not be found! Verify that the name is correct in the Webpack configuration!`);
|
|
403
|
-
}
|
|
404
|
-
// Initialize the container to get shared modules and get the module factory:
|
|
405
|
-
const [, moduleFactory] = yield Promise.all([
|
|
406
|
-
initContainer(window[remoteScope]),
|
|
407
|
-
window[remoteScope].get(module === '.' || module.startsWith('./') ? module : `./${module}`),
|
|
408
|
-
]);
|
|
409
|
-
return moduleFactory();
|
|
410
|
-
}
|
|
411
|
-
else {
|
|
412
|
-
const moduleFactory = yield window[remoteScope].get(module === '.' || module.startsWith('./') ? module : `./${module}`);
|
|
413
|
-
return moduleFactory();
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
class Logger {
|
|
418
|
-
static getLogger() {
|
|
419
|
-
return this.loggerInstance;
|
|
420
|
-
}
|
|
421
|
-
static setLogger(logger) {
|
|
422
|
-
this.loggerInstance = logger || console;
|
|
423
|
-
return logger;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
Logger.loggerInstance = console;
|
|
427
|
-
|
|
428
|
-
const getRuntimeRemotes = () => {
|
|
429
|
-
try {
|
|
430
|
-
return Object.entries(remoteVars).reduce(function (acc, item) {
|
|
431
|
-
const [key, value] = item;
|
|
432
|
-
// if its an object with a thenable (eagerly executing function)
|
|
433
|
-
if (typeof value === 'object' && typeof value.then === 'function') {
|
|
434
|
-
acc[key] = { asyncContainer: value };
|
|
435
|
-
}
|
|
436
|
-
// if its a function that must be called (lazily executing function)
|
|
437
|
-
else if (typeof value === 'function') {
|
|
438
|
-
// @ts-ignore
|
|
439
|
-
acc[key] = { asyncContainer: value };
|
|
440
|
-
}
|
|
441
|
-
// if its a delegate module, skip it
|
|
442
|
-
else if (typeof value === 'string' && value.startsWith('internal ')) {
|
|
443
|
-
const [request, query] = value.replace('internal ', '').split('?');
|
|
444
|
-
if (query) {
|
|
445
|
-
const remoteSyntax = new URLSearchParams(query).get('remote');
|
|
446
|
-
if (remoteSyntax) {
|
|
447
|
-
const [url, global] = extractUrlAndGlobal(remoteSyntax);
|
|
448
|
-
acc[key] = { global, url };
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
// if its just a string (global@url)
|
|
453
|
-
else if (typeof value === 'string') {
|
|
454
|
-
const [url, global] = extractUrlAndGlobal(value);
|
|
455
|
-
acc[key] = { global, url };
|
|
456
|
-
}
|
|
457
|
-
// we dont know or currently support this type
|
|
458
|
-
else {
|
|
459
|
-
//@ts-ignore
|
|
460
|
-
console.warn('remotes process', process.env.REMOTES);
|
|
461
|
-
throw new Error(`[mf] Invalid value received for runtime_remote "${key}"`);
|
|
462
|
-
}
|
|
463
|
-
return acc;
|
|
464
|
-
}, {});
|
|
465
|
-
}
|
|
466
|
-
catch (err) {
|
|
467
|
-
console.warn('Unable to retrieve runtime remotes: ', err);
|
|
468
|
-
}
|
|
469
|
-
return {};
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
const importDelegatedModule = (keyOrRuntimeRemoteItem) => __awaiter(void 0, void 0, void 0, function* () {
|
|
473
|
-
// @ts-ignore
|
|
474
|
-
return loadScript(keyOrRuntimeRemoteItem)
|
|
475
|
-
.then((asyncContainer) => {
|
|
476
|
-
return asyncContainer;
|
|
477
|
-
})
|
|
478
|
-
.then((asyncContainer) => {
|
|
479
|
-
// most of this is only needed because of legacy promise based implementation
|
|
480
|
-
// can remove proxies once we remove promise based implementations
|
|
481
|
-
if (typeof window === 'undefined') {
|
|
482
|
-
if (!Object.hasOwnProperty.call(keyOrRuntimeRemoteItem, 'globalThis')) {
|
|
483
|
-
return asyncContainer;
|
|
484
|
-
}
|
|
485
|
-
// return asyncContainer;
|
|
486
|
-
//TODO: need to solve chunk flushing with delegated modules
|
|
487
|
-
return {
|
|
488
|
-
get: function (arg) {
|
|
489
|
-
//@ts-ignore
|
|
490
|
-
return asyncContainer.get(arg).then((f) => {
|
|
491
|
-
const m = f();
|
|
492
|
-
const result = {
|
|
493
|
-
__esModule: m.__esModule,
|
|
494
|
-
};
|
|
495
|
-
for (const prop in m) {
|
|
496
|
-
if (typeof m[prop] === 'function') {
|
|
497
|
-
Object.defineProperty(result, prop, {
|
|
498
|
-
get: function () {
|
|
499
|
-
return function () {
|
|
500
|
-
//@ts-ignore
|
|
501
|
-
if (globalThis.usedChunks) {
|
|
502
|
-
//@ts-ignore
|
|
503
|
-
globalThis.usedChunks.add(
|
|
504
|
-
//@ts-ignore
|
|
505
|
-
`${keyOrRuntimeRemoteItem.global}->${arg}`);
|
|
506
|
-
}
|
|
507
|
-
//eslint-disable-next-line prefer-rest-params
|
|
508
|
-
return m[prop](...arguments);
|
|
509
|
-
};
|
|
510
|
-
},
|
|
511
|
-
enumerable: true,
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
else {
|
|
515
|
-
Object.defineProperty(result, prop, {
|
|
516
|
-
get: () => {
|
|
517
|
-
//@ts-ignore
|
|
518
|
-
if (globalThis.usedChunks) {
|
|
519
|
-
//@ts-ignore
|
|
520
|
-
globalThis.usedChunks.add(
|
|
521
|
-
//@ts-ignore
|
|
522
|
-
`${keyOrRuntimeRemoteItem.global}->${arg}`);
|
|
523
|
-
}
|
|
524
|
-
return m[prop];
|
|
525
|
-
},
|
|
526
|
-
enumerable: true,
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
if (m.then) {
|
|
531
|
-
return Promise.resolve(() => result);
|
|
532
|
-
}
|
|
533
|
-
return () => result;
|
|
534
|
-
});
|
|
535
|
-
},
|
|
536
|
-
init: asyncContainer.init,
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
else {
|
|
540
|
-
return asyncContainer;
|
|
541
|
-
}
|
|
542
|
-
});
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
exports.Logger = Logger;
|
|
546
|
-
exports.createRuntimeVariables = createRuntimeVariables;
|
|
547
|
-
exports.extractUrlAndGlobal = extractUrlAndGlobal;
|
|
548
|
-
exports.getContainer = getContainer;
|
|
549
|
-
exports.getModule = getModule;
|
|
550
|
-
exports.getRuntimeRemotes = getRuntimeRemotes;
|
|
551
|
-
exports.importDelegatedModule = importDelegatedModule;
|
|
552
|
-
exports.importRemote = importRemote;
|
|
553
|
-
exports.injectScript = injectScript;
|
|
554
|
-
exports.isObjectEmpty = isObjectEmpty;
|
|
555
|
-
exports.loadScript = loadScript;
|
package/dist/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|