@module-federation/sdk 0.3.0 → 0.3.2
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/README.md +99 -0
- package/dist/LICENSE +21 -0
- package/dist/index.cjs.js +749 -0
- package/dist/index.esm.js +701 -0
- package/dist/normalize-webpack-path.cjs.js +48 -0
- package/dist/normalize-webpack-path.esm.js +39 -0
- package/dist/package.json +44 -0
- package/{src → dist/src}/constant.d.ts +9 -0
- package/dist/src/dom.d.ts +30 -0
- package/{src → dist/src}/generateSnapshotFromManifest.d.ts +1 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/node.d.ts +5 -0
- package/{src → dist/src}/normalize-webpack-path.d.ts +3 -1
- package/dist/src/normalizeOptions.d.ts +1 -0
- package/{src → dist/src}/types/index.d.ts +1 -0
- package/dist/src/types/plugins/ContainerPlugin.d.ts +159 -0
- package/dist/src/types/plugins/ContainerReferencePlugin.d.ts +52 -0
- package/dist/src/types/plugins/ModuleFederationPlugin.d.ts +334 -0
- package/dist/src/types/plugins/SharePlugin.d.ts +71 -0
- package/dist/src/types/plugins/index.d.ts +4 -0
- package/{src → dist/src}/types/snapshot.d.ts +7 -0
- package/{src → dist/src}/types/stats.d.ts +15 -5
- package/{src → dist/src}/utils.d.ts +1 -1
- package/package.json +14 -11
- package/index.cjs.default.js +0 -1
- package/index.cjs.js +0 -793
- package/index.cjs.mjs +0 -2
- package/index.esm.js +0 -761
- package/normalize-webpack-path.cjs.default.js +0 -1
- package/normalize-webpack-path.cjs.js +0 -42
- package/normalize-webpack-path.cjs.mjs +0 -2
- package/normalize-webpack-path.esm.js +0 -33
- package/src/dom.d.ts +0 -10
- package/src/index.d.ts +0 -7
- /package/{index.cjs.d.ts → dist/index.cjs.d.ts} +0 -0
- /package/{normalize-webpack-path.cjs.d.ts → dist/normalize-webpack-path.cjs.d.ts} +0 -0
- /package/{src → dist/src}/env.d.ts +0 -0
- /package/{src → dist/src}/logger.d.ts +0 -0
- /package/{src → dist/src}/types/common.d.ts +0 -0
- /package/{src → dist/src}/types/manifest.d.ts +0 -0
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const FederationModuleManifest = 'federation-manifest.json';
|
|
6
|
+
const MANIFEST_EXT = '.json';
|
|
7
|
+
const BROWSER_LOG_KEY = 'FEDERATION_DEBUG';
|
|
8
|
+
const BROWSER_LOG_VALUE = '1';
|
|
9
|
+
const NameTransformSymbol = {
|
|
10
|
+
AT: '@',
|
|
11
|
+
HYPHEN: '-',
|
|
12
|
+
SLASH: '/'
|
|
13
|
+
};
|
|
14
|
+
const NameTransformMap = {
|
|
15
|
+
[NameTransformSymbol.AT]: 'scope_',
|
|
16
|
+
[NameTransformSymbol.HYPHEN]: '_',
|
|
17
|
+
[NameTransformSymbol.SLASH]: '__'
|
|
18
|
+
};
|
|
19
|
+
const EncodedNameTransformMap = {
|
|
20
|
+
[NameTransformMap[NameTransformSymbol.AT]]: NameTransformSymbol.AT,
|
|
21
|
+
[NameTransformMap[NameTransformSymbol.HYPHEN]]: NameTransformSymbol.HYPHEN,
|
|
22
|
+
[NameTransformMap[NameTransformSymbol.SLASH]]: NameTransformSymbol.SLASH
|
|
23
|
+
};
|
|
24
|
+
const SEPARATOR = ':';
|
|
25
|
+
const ManifestFileName = 'mf-manifest.json';
|
|
26
|
+
const StatsFileName = 'mf-stats.json';
|
|
27
|
+
const MFModuleType = {
|
|
28
|
+
NPM: 'npm',
|
|
29
|
+
APP: 'app'
|
|
30
|
+
};
|
|
31
|
+
const MODULE_DEVTOOL_IDENTIFIER = '__MF_DEVTOOLS_MODULE_INFO__';
|
|
32
|
+
const ENCODE_NAME_PREFIX = 'ENCODE_NAME_PREFIX';
|
|
33
|
+
const TEMP_DIR = '.federation';
|
|
34
|
+
|
|
35
|
+
var ContainerPlugin = /*#__PURE__*/Object.freeze({
|
|
36
|
+
__proto__: null
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
var ContainerReferencePlugin = /*#__PURE__*/Object.freeze({
|
|
40
|
+
__proto__: null
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
var ModuleFederationPlugin = /*#__PURE__*/Object.freeze({
|
|
44
|
+
__proto__: null
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
var SharePlugin = /*#__PURE__*/Object.freeze({
|
|
48
|
+
__proto__: null
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function isBrowserEnv() {
|
|
52
|
+
return typeof window !== 'undefined';
|
|
53
|
+
}
|
|
54
|
+
function isDebugMode() {
|
|
55
|
+
if (typeof process !== 'undefined' && process.env && process.env['FEDERATION_DEBUG']) {
|
|
56
|
+
return Boolean(process.env['FEDERATION_DEBUG']);
|
|
57
|
+
}
|
|
58
|
+
return typeof FEDERATION_DEBUG !== 'undefined' && Boolean(FEDERATION_DEBUG);
|
|
59
|
+
}
|
|
60
|
+
const getProcessEnv = function() {
|
|
61
|
+
return typeof process !== 'undefined' && process.env ? process.env : {};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
function safeToString(info) {
|
|
65
|
+
try {
|
|
66
|
+
return JSON.stringify(info, null, 2);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const DEBUG_LOG = '[ FEDERATION DEBUG ]';
|
|
72
|
+
function safeGetLocalStorageItem() {
|
|
73
|
+
try {
|
|
74
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
75
|
+
return localStorage.getItem(BROWSER_LOG_KEY) === BROWSER_LOG_VALUE;
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return typeof document !== 'undefined';
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
let Logger = class Logger {
|
|
83
|
+
info(msg, info) {
|
|
84
|
+
if (this.enable) {
|
|
85
|
+
const argsToString = safeToString(info) || '';
|
|
86
|
+
if (isBrowserEnv()) {
|
|
87
|
+
console.info(`%c ${this.identifier}: ${msg} ${argsToString}`, 'color:#3300CC');
|
|
88
|
+
} else {
|
|
89
|
+
console.info('\x1b[34m%s', `${this.identifier}: ${msg} ${argsToString ? `\n${argsToString}` : ''}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
logOriginalInfo(...args) {
|
|
94
|
+
if (this.enable) {
|
|
95
|
+
if (isBrowserEnv()) {
|
|
96
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
97
|
+
console.log(...args);
|
|
98
|
+
} else {
|
|
99
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
100
|
+
console.log(...args);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
constructor(identifier){
|
|
105
|
+
this.enable = false;
|
|
106
|
+
this.identifier = identifier || DEBUG_LOG;
|
|
107
|
+
if (isBrowserEnv() && safeGetLocalStorageItem()) {
|
|
108
|
+
this.enable = true;
|
|
109
|
+
} else if (isDebugMode()) {
|
|
110
|
+
this.enable = true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const LOG_CATEGORY = '[ Federation Runtime ]';
|
|
116
|
+
// entry: name:version version : 1.0.0 | ^1.2.3
|
|
117
|
+
// entry: name:entry entry: https://localhost:9000/federation-manifest.json
|
|
118
|
+
const parseEntry = (str, devVerOrUrl, separator = SEPARATOR)=>{
|
|
119
|
+
const strSplit = str.split(separator);
|
|
120
|
+
const devVersionOrUrl = getProcessEnv()['NODE_ENV'] === 'development' && devVerOrUrl;
|
|
121
|
+
const defaultVersion = '*';
|
|
122
|
+
const isEntry = (s)=>s.startsWith('http') || s.includes(MANIFEST_EXT);
|
|
123
|
+
// Check if the string starts with a type
|
|
124
|
+
if (strSplit.length >= 2) {
|
|
125
|
+
let [name, ...versionOrEntryArr] = strSplit;
|
|
126
|
+
if (str.startsWith(separator)) {
|
|
127
|
+
versionOrEntryArr = [
|
|
128
|
+
devVersionOrUrl || strSplit.slice(-1)[0]
|
|
129
|
+
];
|
|
130
|
+
name = strSplit.slice(0, -1).join(separator);
|
|
131
|
+
}
|
|
132
|
+
let versionOrEntry = devVersionOrUrl || versionOrEntryArr.join(separator);
|
|
133
|
+
if (isEntry(versionOrEntry)) {
|
|
134
|
+
return {
|
|
135
|
+
name,
|
|
136
|
+
entry: versionOrEntry
|
|
137
|
+
};
|
|
138
|
+
} else {
|
|
139
|
+
// Apply version rule
|
|
140
|
+
// devVersionOrUrl => inputVersion => defaultVersion
|
|
141
|
+
return {
|
|
142
|
+
name,
|
|
143
|
+
version: versionOrEntry || defaultVersion
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
} else if (strSplit.length === 1) {
|
|
147
|
+
const [name] = strSplit;
|
|
148
|
+
if (devVersionOrUrl && isEntry(devVersionOrUrl)) {
|
|
149
|
+
return {
|
|
150
|
+
name,
|
|
151
|
+
entry: devVersionOrUrl
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
name,
|
|
156
|
+
version: devVersionOrUrl || defaultVersion
|
|
157
|
+
};
|
|
158
|
+
} else {
|
|
159
|
+
throw `Invalid entry value: ${str}`;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const logger = new Logger();
|
|
163
|
+
const composeKeyWithSeparator = function(...args) {
|
|
164
|
+
if (!args.length) {
|
|
165
|
+
return '';
|
|
166
|
+
}
|
|
167
|
+
return args.reduce((sum, cur)=>{
|
|
168
|
+
if (!cur) {
|
|
169
|
+
return sum;
|
|
170
|
+
}
|
|
171
|
+
if (!sum) {
|
|
172
|
+
return cur;
|
|
173
|
+
}
|
|
174
|
+
return `${sum}${SEPARATOR}${cur}`;
|
|
175
|
+
}, '');
|
|
176
|
+
};
|
|
177
|
+
const encodeName = function(name, prefix = '', withExt = false) {
|
|
178
|
+
try {
|
|
179
|
+
const ext = withExt ? '.js' : '';
|
|
180
|
+
return `${prefix}${name.replace(new RegExp(`${NameTransformSymbol.AT}`, 'g'), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp(`${NameTransformSymbol.HYPHEN}`, 'g'), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp(`${NameTransformSymbol.SLASH}`, 'g'), NameTransformMap[NameTransformSymbol.SLASH])}${ext}`;
|
|
181
|
+
} catch (err) {
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const decodeName = function(name, prefix, withExt) {
|
|
186
|
+
try {
|
|
187
|
+
let decodedName = name;
|
|
188
|
+
if (prefix) {
|
|
189
|
+
if (!decodedName.startsWith(prefix)) {
|
|
190
|
+
return decodedName;
|
|
191
|
+
}
|
|
192
|
+
decodedName = decodedName.replace(new RegExp(prefix, 'g'), '');
|
|
193
|
+
}
|
|
194
|
+
decodedName = decodedName.replace(new RegExp(`${NameTransformMap[NameTransformSymbol.AT]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.AT]]).replace(new RegExp(`${NameTransformMap[NameTransformSymbol.SLASH]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.SLASH]]).replace(new RegExp(`${NameTransformMap[NameTransformSymbol.HYPHEN]}`, 'g'), EncodedNameTransformMap[NameTransformMap[NameTransformSymbol.HYPHEN]]);
|
|
195
|
+
if (withExt) {
|
|
196
|
+
decodedName = decodedName.replace('.js', '');
|
|
197
|
+
}
|
|
198
|
+
return decodedName;
|
|
199
|
+
} catch (err) {
|
|
200
|
+
throw err;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const generateExposeFilename = (exposeName, withExt)=>{
|
|
204
|
+
if (!exposeName) {
|
|
205
|
+
return '';
|
|
206
|
+
}
|
|
207
|
+
let expose = exposeName;
|
|
208
|
+
if (expose === '.') {
|
|
209
|
+
expose = 'default_export';
|
|
210
|
+
}
|
|
211
|
+
if (expose.startsWith('./')) {
|
|
212
|
+
expose = expose.replace('./', '');
|
|
213
|
+
}
|
|
214
|
+
return encodeName(expose, '__federation_expose_', withExt);
|
|
215
|
+
};
|
|
216
|
+
const generateShareFilename = (pkgName, withExt)=>{
|
|
217
|
+
if (!pkgName) {
|
|
218
|
+
return '';
|
|
219
|
+
}
|
|
220
|
+
return encodeName(pkgName, '__federation_shared_', withExt);
|
|
221
|
+
};
|
|
222
|
+
const getResourceUrl = (module, sourceUrl)=>{
|
|
223
|
+
if ('getPublicPath' in module) {
|
|
224
|
+
let publicPath;
|
|
225
|
+
if (!module.getPublicPath.startsWith('function')) {
|
|
226
|
+
publicPath = new Function(module.getPublicPath)();
|
|
227
|
+
} else {
|
|
228
|
+
publicPath = new Function('return ' + module.getPublicPath)()();
|
|
229
|
+
}
|
|
230
|
+
return `${publicPath}${sourceUrl}`;
|
|
231
|
+
} else if ('publicPath' in module) {
|
|
232
|
+
return `${module.publicPath}${sourceUrl}`;
|
|
233
|
+
} else {
|
|
234
|
+
console.warn('Cannot get resource URL. If in debug mode, please ignore.', module, sourceUrl);
|
|
235
|
+
return '';
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
239
|
+
const assert = (condition, msg)=>{
|
|
240
|
+
if (!condition) {
|
|
241
|
+
error(msg);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const error = (msg)=>{
|
|
245
|
+
throw new Error(`${LOG_CATEGORY}: ${msg}`);
|
|
246
|
+
};
|
|
247
|
+
const warn = (msg)=>{
|
|
248
|
+
console.warn(`${LOG_CATEGORY}: ${msg}`);
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
function _extends() {
|
|
252
|
+
_extends = Object.assign || function assign(target) {
|
|
253
|
+
for(var i = 1; i < arguments.length; i++){
|
|
254
|
+
var source = arguments[i];
|
|
255
|
+
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
256
|
+
}
|
|
257
|
+
return target;
|
|
258
|
+
};
|
|
259
|
+
return _extends.apply(this, arguments);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const simpleJoinRemoteEntry = (rPath, rName)=>{
|
|
263
|
+
if (!rPath) {
|
|
264
|
+
return rName;
|
|
265
|
+
}
|
|
266
|
+
const transformPath = (str)=>{
|
|
267
|
+
if (str === '.') {
|
|
268
|
+
return '';
|
|
269
|
+
}
|
|
270
|
+
if (str.startsWith('./')) {
|
|
271
|
+
return str.replace('./', '');
|
|
272
|
+
}
|
|
273
|
+
if (str.startsWith('/')) {
|
|
274
|
+
const strWithoutSlash = str.slice(1);
|
|
275
|
+
if (strWithoutSlash.endsWith('/')) {
|
|
276
|
+
return strWithoutSlash.slice(0, -1);
|
|
277
|
+
}
|
|
278
|
+
return strWithoutSlash;
|
|
279
|
+
}
|
|
280
|
+
return str;
|
|
281
|
+
};
|
|
282
|
+
const transformedPath = transformPath(rPath);
|
|
283
|
+
if (!transformedPath) {
|
|
284
|
+
return rName;
|
|
285
|
+
}
|
|
286
|
+
if (transformedPath.endsWith('/')) {
|
|
287
|
+
return `${transformedPath}${rName}`;
|
|
288
|
+
}
|
|
289
|
+
return `${transformedPath}/${rName}`;
|
|
290
|
+
};
|
|
291
|
+
function inferAutoPublicPath(url) {
|
|
292
|
+
return url.replace(/#.*$/, '').replace(/\?.*$/, '').replace(/\/[^\/]+$/, '/');
|
|
293
|
+
}
|
|
294
|
+
// Priority: overrides > remotes
|
|
295
|
+
// eslint-disable-next-line max-lines-per-function
|
|
296
|
+
function generateSnapshotFromManifest(manifest, options = {}) {
|
|
297
|
+
var _manifest_metaData, _manifest_metaData1;
|
|
298
|
+
const { remotes = {}, overrides = {}, version } = options;
|
|
299
|
+
let remoteSnapshot;
|
|
300
|
+
const getPublicPath = ()=>{
|
|
301
|
+
if ('publicPath' in manifest.metaData) {
|
|
302
|
+
if (manifest.metaData.publicPath === 'auto' && version) {
|
|
303
|
+
// use same implementation as publicPath auto runtime module implements
|
|
304
|
+
return inferAutoPublicPath(version);
|
|
305
|
+
}
|
|
306
|
+
return manifest.metaData.publicPath;
|
|
307
|
+
} else {
|
|
308
|
+
return manifest.metaData.getPublicPath;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const overridesKeys = Object.keys(overrides);
|
|
312
|
+
let remotesInfo = {};
|
|
313
|
+
// If remotes are not provided, only the remotes in the manifest will be read
|
|
314
|
+
if (!Object.keys(remotes).length) {
|
|
315
|
+
var _manifest_remotes;
|
|
316
|
+
remotesInfo = ((_manifest_remotes = manifest.remotes) == null ? void 0 : _manifest_remotes.reduce((res, next)=>{
|
|
317
|
+
let matchedVersion;
|
|
318
|
+
const name = next.federationContainerName;
|
|
319
|
+
// overrides have higher priority
|
|
320
|
+
if (overridesKeys.includes(name)) {
|
|
321
|
+
matchedVersion = overrides[name];
|
|
322
|
+
} else {
|
|
323
|
+
if ('version' in next) {
|
|
324
|
+
matchedVersion = next.version;
|
|
325
|
+
} else {
|
|
326
|
+
matchedVersion = next.entry;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
res[name] = {
|
|
330
|
+
matchedVersion
|
|
331
|
+
};
|
|
332
|
+
return res;
|
|
333
|
+
}, {})) || {};
|
|
334
|
+
}
|
|
335
|
+
// If remotes (deploy scenario) are specified, they need to be traversed again
|
|
336
|
+
Object.keys(remotes).forEach((key)=>remotesInfo[key] = {
|
|
337
|
+
// overrides will override dependencies
|
|
338
|
+
matchedVersion: overridesKeys.includes(key) ? overrides[key] : remotes[key]
|
|
339
|
+
});
|
|
340
|
+
const { remoteEntry: { path: remoteEntryPath, name: remoteEntryName, type: remoteEntryType }, types: remoteTypes, buildInfo: { buildVersion }, globalName, ssrRemoteEntry } = manifest.metaData;
|
|
341
|
+
const { exposes } = manifest;
|
|
342
|
+
let basicRemoteSnapshot = {
|
|
343
|
+
version: version ? version : '',
|
|
344
|
+
buildVersion,
|
|
345
|
+
globalName,
|
|
346
|
+
remoteEntry: simpleJoinRemoteEntry(remoteEntryPath, remoteEntryName),
|
|
347
|
+
remoteEntryType,
|
|
348
|
+
remoteTypes: simpleJoinRemoteEntry(remoteTypes.path, remoteTypes.name),
|
|
349
|
+
remoteTypesZip: remoteTypes.zip || '',
|
|
350
|
+
remoteTypesAPI: remoteTypes.api || '',
|
|
351
|
+
remotesInfo,
|
|
352
|
+
shared: manifest == null ? void 0 : manifest.shared.map((item)=>({
|
|
353
|
+
assets: item.assets,
|
|
354
|
+
sharedName: item.name,
|
|
355
|
+
version: item.version
|
|
356
|
+
})),
|
|
357
|
+
modules: exposes == null ? void 0 : exposes.map((expose)=>({
|
|
358
|
+
moduleName: expose.name,
|
|
359
|
+
modulePath: expose.path,
|
|
360
|
+
assets: expose.assets
|
|
361
|
+
}))
|
|
362
|
+
};
|
|
363
|
+
if ((_manifest_metaData = manifest.metaData) == null ? void 0 : _manifest_metaData.prefetchInterface) {
|
|
364
|
+
const prefetchInterface = manifest.metaData.prefetchInterface;
|
|
365
|
+
basicRemoteSnapshot = _extends({}, basicRemoteSnapshot, {
|
|
366
|
+
prefetchInterface
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
if ((_manifest_metaData1 = manifest.metaData) == null ? void 0 : _manifest_metaData1.prefetchEntry) {
|
|
370
|
+
const { path, name, type } = manifest.metaData.prefetchEntry;
|
|
371
|
+
basicRemoteSnapshot = _extends({}, basicRemoteSnapshot, {
|
|
372
|
+
prefetchEntry: simpleJoinRemoteEntry(path, name),
|
|
373
|
+
prefetchEntryType: type
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
if ('publicPath' in manifest.metaData) {
|
|
377
|
+
remoteSnapshot = _extends({}, basicRemoteSnapshot, {
|
|
378
|
+
publicPath: getPublicPath()
|
|
379
|
+
});
|
|
380
|
+
} else {
|
|
381
|
+
remoteSnapshot = _extends({}, basicRemoteSnapshot, {
|
|
382
|
+
getPublicPath: getPublicPath()
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
if (ssrRemoteEntry) {
|
|
386
|
+
const fullSSRRemoteEntry = simpleJoinRemoteEntry(ssrRemoteEntry.path, ssrRemoteEntry.name);
|
|
387
|
+
remoteSnapshot.ssrRemoteEntry = fullSSRRemoteEntry;
|
|
388
|
+
remoteSnapshot.ssrRemoteEntryType = 'commonjs-module';
|
|
389
|
+
}
|
|
390
|
+
return remoteSnapshot;
|
|
391
|
+
}
|
|
392
|
+
function isManifestProvider(moduleInfo) {
|
|
393
|
+
if ('remoteEntry' in moduleInfo && moduleInfo.remoteEntry.includes(MANIFEST_EXT)) {
|
|
394
|
+
return true;
|
|
395
|
+
} else {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
401
|
+
async function safeWrapper(callback, disableWarn) {
|
|
402
|
+
try {
|
|
403
|
+
const res = await callback();
|
|
404
|
+
return res;
|
|
405
|
+
} catch (e) {
|
|
406
|
+
!disableWarn && warn(e);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
function isStaticResourcesEqual(url1, url2) {
|
|
411
|
+
const REG_EXP = /^(https?:)?\/\//i;
|
|
412
|
+
// Transform url1 and url2 into relative paths
|
|
413
|
+
const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\/$/, '');
|
|
414
|
+
const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\/$/, '');
|
|
415
|
+
// Check if the relative paths are identical
|
|
416
|
+
return relativeUrl1 === relativeUrl2;
|
|
417
|
+
}
|
|
418
|
+
function createScript(info) {
|
|
419
|
+
// Retrieve the existing script element by its src attribute
|
|
420
|
+
let script = null;
|
|
421
|
+
let needAttach = true;
|
|
422
|
+
let timeout = 20000;
|
|
423
|
+
let timeoutId;
|
|
424
|
+
const scripts = document.getElementsByTagName('script');
|
|
425
|
+
for(let i = 0; i < scripts.length; i++){
|
|
426
|
+
const s = scripts[i];
|
|
427
|
+
const scriptSrc = s.getAttribute('src');
|
|
428
|
+
if (scriptSrc && isStaticResourcesEqual(scriptSrc, info.url)) {
|
|
429
|
+
script = s;
|
|
430
|
+
needAttach = false;
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (!script) {
|
|
435
|
+
script = document.createElement('script');
|
|
436
|
+
script.type = 'text/javascript';
|
|
437
|
+
script.src = info.url;
|
|
438
|
+
if (info.createScriptHook) {
|
|
439
|
+
const createScriptRes = info.createScriptHook(info.url, info.attrs);
|
|
440
|
+
if (createScriptRes instanceof HTMLScriptElement) {
|
|
441
|
+
script = createScriptRes;
|
|
442
|
+
} else if (typeof createScriptRes === 'object') {
|
|
443
|
+
if (createScriptRes.script) script = createScriptRes.script;
|
|
444
|
+
if (createScriptRes.timeout) timeout = createScriptRes.timeout;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const attrs = info.attrs;
|
|
448
|
+
if (attrs) {
|
|
449
|
+
Object.keys(attrs).forEach((name)=>{
|
|
450
|
+
if (script) {
|
|
451
|
+
if (name === 'async' || name === 'defer') {
|
|
452
|
+
script[name] = attrs[name];
|
|
453
|
+
// Attributes that do not exist are considered overridden
|
|
454
|
+
} else if (!script.getAttribute(name)) {
|
|
455
|
+
script.setAttribute(name, attrs[name]);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const onScriptComplete = (prev, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
462
|
+
event)=>{
|
|
463
|
+
var _info_cb;
|
|
464
|
+
clearTimeout(timeoutId);
|
|
465
|
+
// Prevent memory leaks in IE.
|
|
466
|
+
if (script) {
|
|
467
|
+
script.onerror = null;
|
|
468
|
+
script.onload = null;
|
|
469
|
+
safeWrapper(()=>{
|
|
470
|
+
const { needDeleteScript = true } = info;
|
|
471
|
+
if (needDeleteScript) {
|
|
472
|
+
(script == null ? void 0 : script.parentNode) && script.parentNode.removeChild(script);
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
if (prev) {
|
|
476
|
+
var _info_cb1;
|
|
477
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
478
|
+
const res = prev(event);
|
|
479
|
+
info == null ? void 0 : (_info_cb1 = info.cb) == null ? void 0 : _info_cb1.call(info);
|
|
480
|
+
return res;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
info == null ? void 0 : (_info_cb = info.cb) == null ? void 0 : _info_cb.call(info);
|
|
484
|
+
};
|
|
485
|
+
script.onerror = onScriptComplete.bind(null, script.onerror);
|
|
486
|
+
script.onload = onScriptComplete.bind(null, script.onload);
|
|
487
|
+
timeoutId = setTimeout(()=>{
|
|
488
|
+
onScriptComplete(null, new Error(`Remote script "${info.url}" time-outed.`));
|
|
489
|
+
}, timeout);
|
|
490
|
+
return {
|
|
491
|
+
script,
|
|
492
|
+
needAttach
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function createLink(info) {
|
|
496
|
+
// <link rel="preload" href="script.js" as="script">
|
|
497
|
+
// Retrieve the existing script element by its src attribute
|
|
498
|
+
let link = null;
|
|
499
|
+
let needAttach = true;
|
|
500
|
+
const links = document.getElementsByTagName('link');
|
|
501
|
+
for(let i = 0; i < links.length; i++){
|
|
502
|
+
const l = links[i];
|
|
503
|
+
const linkHref = l.getAttribute('href');
|
|
504
|
+
const linkRef = l.getAttribute('ref');
|
|
505
|
+
if (linkHref && isStaticResourcesEqual(linkHref, info.url) && linkRef === info.attrs['ref']) {
|
|
506
|
+
link = l;
|
|
507
|
+
needAttach = false;
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (!link) {
|
|
512
|
+
link = document.createElement('link');
|
|
513
|
+
link.setAttribute('href', info.url);
|
|
514
|
+
if (info.createLinkHook) {
|
|
515
|
+
const createLinkRes = info.createLinkHook(info.url);
|
|
516
|
+
if (createLinkRes instanceof HTMLLinkElement) {
|
|
517
|
+
link = createLinkRes;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
const attrs = info.attrs;
|
|
521
|
+
if (attrs) {
|
|
522
|
+
Object.keys(attrs).forEach((name)=>{
|
|
523
|
+
if (link && !link.getAttribute(name)) {
|
|
524
|
+
link.setAttribute(name, attrs[name]);
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const onLinkComplete = (prev, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
530
|
+
event)=>{
|
|
531
|
+
// Prevent memory leaks in IE.
|
|
532
|
+
if (link) {
|
|
533
|
+
link.onerror = null;
|
|
534
|
+
link.onload = null;
|
|
535
|
+
safeWrapper(()=>{
|
|
536
|
+
const { needDeleteLink = true } = info;
|
|
537
|
+
if (needDeleteLink) {
|
|
538
|
+
(link == null ? void 0 : link.parentNode) && link.parentNode.removeChild(link);
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
if (prev) {
|
|
542
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
543
|
+
const res = prev(event);
|
|
544
|
+
info.cb();
|
|
545
|
+
return res;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
info.cb();
|
|
549
|
+
};
|
|
550
|
+
link.onerror = onLinkComplete.bind(null, link.onerror);
|
|
551
|
+
link.onload = onLinkComplete.bind(null, link.onload);
|
|
552
|
+
return {
|
|
553
|
+
link,
|
|
554
|
+
needAttach
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function loadScript(url, info) {
|
|
558
|
+
const { attrs = {}, createScriptHook } = info;
|
|
559
|
+
return new Promise((resolve, _reject)=>{
|
|
560
|
+
const { script, needAttach } = createScript({
|
|
561
|
+
url,
|
|
562
|
+
cb: resolve,
|
|
563
|
+
attrs: _extends({
|
|
564
|
+
fetchpriority: 'high'
|
|
565
|
+
}, attrs),
|
|
566
|
+
createScriptHook,
|
|
567
|
+
needDeleteScript: true
|
|
568
|
+
});
|
|
569
|
+
needAttach && document.head.appendChild(script);
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function importNodeModule(name) {
|
|
574
|
+
if (!name) {
|
|
575
|
+
throw new Error('import specifier is required');
|
|
576
|
+
}
|
|
577
|
+
const importModule = new Function('name', `return import(name)`);
|
|
578
|
+
return importModule(name).then((res)=>res).catch((error)=>{
|
|
579
|
+
console.error(`Error importing module ${name}:`, error);
|
|
580
|
+
throw error;
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
const loadNodeFetch = async ()=>{
|
|
584
|
+
const fetchModule = await importNodeModule('node-fetch');
|
|
585
|
+
return fetchModule.default || fetchModule;
|
|
586
|
+
};
|
|
587
|
+
const lazyLoaderHookFetch = async (input, init)=>{
|
|
588
|
+
// @ts-ignore
|
|
589
|
+
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
|
|
590
|
+
const hook = (url, init)=>{
|
|
591
|
+
return loaderHooks.lifecycle.fetch.emit(url, init);
|
|
592
|
+
};
|
|
593
|
+
const res = await hook(input, init || {});
|
|
594
|
+
if (!res || !(res instanceof Response)) {
|
|
595
|
+
const fetchFunction = typeof fetch === 'undefined' ? await loadNodeFetch() : fetch;
|
|
596
|
+
return fetchFunction(input, init || {});
|
|
597
|
+
}
|
|
598
|
+
return res;
|
|
599
|
+
};
|
|
600
|
+
function createScriptNode(url, cb, attrs, createScriptHook) {
|
|
601
|
+
if (createScriptHook) {
|
|
602
|
+
const hookResult = createScriptHook(url);
|
|
603
|
+
if (hookResult && typeof hookResult === 'object' && 'url' in hookResult) {
|
|
604
|
+
url = hookResult.url;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
let urlObj;
|
|
608
|
+
try {
|
|
609
|
+
urlObj = new URL(url);
|
|
610
|
+
} catch (e) {
|
|
611
|
+
console.error('Error constructing URL:', e);
|
|
612
|
+
cb(new Error(`Invalid URL: ${e}`));
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const getFetch = async ()=>{
|
|
616
|
+
//@ts-ignore
|
|
617
|
+
if (typeof __webpack_require__ !== 'undefined') {
|
|
618
|
+
try {
|
|
619
|
+
//@ts-ignore
|
|
620
|
+
const loaderHooks = __webpack_require__.federation.instance.loaderHook;
|
|
621
|
+
if (loaderHooks.lifecycle.fetch) {
|
|
622
|
+
return lazyLoaderHookFetch;
|
|
623
|
+
}
|
|
624
|
+
} catch (e) {
|
|
625
|
+
console.warn('federation.instance.loaderHook.lifecycle.fetch failed:', e);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return typeof fetch === 'undefined' ? loadNodeFetch() : fetch;
|
|
629
|
+
};
|
|
630
|
+
const handleScriptFetch = async (f, urlObj)=>{
|
|
631
|
+
try {
|
|
632
|
+
var _vm_constants;
|
|
633
|
+
const res = await f(urlObj.href);
|
|
634
|
+
const data = await res.text();
|
|
635
|
+
const [path, vm] = await Promise.all([
|
|
636
|
+
importNodeModule('path'),
|
|
637
|
+
importNodeModule('vm')
|
|
638
|
+
]);
|
|
639
|
+
const scriptContext = {
|
|
640
|
+
exports: {},
|
|
641
|
+
module: {
|
|
642
|
+
exports: {}
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
const urlDirname = urlObj.pathname.split('/').slice(0, -1).join('/');
|
|
646
|
+
const filename = path.basename(urlObj.pathname);
|
|
647
|
+
var _vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER;
|
|
648
|
+
const script = new vm.Script(`(function(exports, module, require, __dirname, __filename) {${data}\n})`, {
|
|
649
|
+
filename,
|
|
650
|
+
importModuleDynamically: (_vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER = (_vm_constants = vm.constants) == null ? void 0 : _vm_constants.USE_MAIN_CONTEXT_DEFAULT_LOADER) != null ? _vm_constants_USE_MAIN_CONTEXT_DEFAULT_LOADER : importNodeModule
|
|
651
|
+
});
|
|
652
|
+
script.runInThisContext()(scriptContext.exports, scriptContext.module, eval('require'), urlDirname, filename);
|
|
653
|
+
const exportedInterface = scriptContext.module.exports || scriptContext.exports;
|
|
654
|
+
if (attrs && exportedInterface && attrs['globalName']) {
|
|
655
|
+
const container = exportedInterface[attrs['globalName']] || exportedInterface;
|
|
656
|
+
cb(undefined, container);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
cb(undefined, exportedInterface);
|
|
660
|
+
} catch (e) {
|
|
661
|
+
cb(e instanceof Error ? e : new Error(`Script execution error: ${e}`));
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
getFetch().then((f)=>handleScriptFetch(f, urlObj)).catch((err)=>{
|
|
665
|
+
cb(err);
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
function loadScriptNode(url, info) {
|
|
669
|
+
return new Promise((resolve, reject)=>{
|
|
670
|
+
createScriptNode(url, (error, scriptContext)=>{
|
|
671
|
+
if (error) {
|
|
672
|
+
reject(error);
|
|
673
|
+
} else {
|
|
674
|
+
var _info_attrs, _info_attrs1;
|
|
675
|
+
const remoteEntryKey = (info == null ? void 0 : (_info_attrs = info.attrs) == null ? void 0 : _info_attrs['globalName']) || `__FEDERATION_${info == null ? void 0 : (_info_attrs1 = info.attrs) == null ? void 0 : _info_attrs1['name']}:custom__`;
|
|
676
|
+
const entryExports = globalThis[remoteEntryKey] = scriptContext;
|
|
677
|
+
resolve(entryExports);
|
|
678
|
+
}
|
|
679
|
+
}, info.attrs, info.createScriptHook);
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function normalizeOptions(enableDefault, defaultOptions, key) {
|
|
684
|
+
return function(options) {
|
|
685
|
+
if (options === false) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
if (typeof options === 'undefined') {
|
|
689
|
+
if (enableDefault) {
|
|
690
|
+
return defaultOptions;
|
|
691
|
+
} else {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (options === true) {
|
|
696
|
+
return defaultOptions;
|
|
697
|
+
}
|
|
698
|
+
if (options && typeof options === 'object') {
|
|
699
|
+
return _extends({}, defaultOptions, options);
|
|
700
|
+
}
|
|
701
|
+
throw new Error(`Unexpected type for \`${key}\`, expect boolean/undefined/object, got: ${typeof options}`);
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
exports.BROWSER_LOG_KEY = BROWSER_LOG_KEY;
|
|
706
|
+
exports.BROWSER_LOG_VALUE = BROWSER_LOG_VALUE;
|
|
707
|
+
exports.ENCODE_NAME_PREFIX = ENCODE_NAME_PREFIX;
|
|
708
|
+
exports.EncodedNameTransformMap = EncodedNameTransformMap;
|
|
709
|
+
exports.FederationModuleManifest = FederationModuleManifest;
|
|
710
|
+
exports.Logger = Logger;
|
|
711
|
+
exports.MANIFEST_EXT = MANIFEST_EXT;
|
|
712
|
+
exports.MFModuleType = MFModuleType;
|
|
713
|
+
exports.MODULE_DEVTOOL_IDENTIFIER = MODULE_DEVTOOL_IDENTIFIER;
|
|
714
|
+
exports.ManifestFileName = ManifestFileName;
|
|
715
|
+
exports.NameTransformMap = NameTransformMap;
|
|
716
|
+
exports.NameTransformSymbol = NameTransformSymbol;
|
|
717
|
+
exports.SEPARATOR = SEPARATOR;
|
|
718
|
+
exports.StatsFileName = StatsFileName;
|
|
719
|
+
exports.TEMP_DIR = TEMP_DIR;
|
|
720
|
+
exports.assert = assert;
|
|
721
|
+
exports.composeKeyWithSeparator = composeKeyWithSeparator;
|
|
722
|
+
exports.containerPlugin = ContainerPlugin;
|
|
723
|
+
exports.containerReferencePlugin = ContainerReferencePlugin;
|
|
724
|
+
exports.createLink = createLink;
|
|
725
|
+
exports.createScript = createScript;
|
|
726
|
+
exports.createScriptNode = createScriptNode;
|
|
727
|
+
exports.decodeName = decodeName;
|
|
728
|
+
exports.encodeName = encodeName;
|
|
729
|
+
exports.error = error;
|
|
730
|
+
exports.generateExposeFilename = generateExposeFilename;
|
|
731
|
+
exports.generateShareFilename = generateShareFilename;
|
|
732
|
+
exports.generateSnapshotFromManifest = generateSnapshotFromManifest;
|
|
733
|
+
exports.getProcessEnv = getProcessEnv;
|
|
734
|
+
exports.getResourceUrl = getResourceUrl;
|
|
735
|
+
exports.inferAutoPublicPath = inferAutoPublicPath;
|
|
736
|
+
exports.isBrowserEnv = isBrowserEnv;
|
|
737
|
+
exports.isDebugMode = isDebugMode;
|
|
738
|
+
exports.isManifestProvider = isManifestProvider;
|
|
739
|
+
exports.isStaticResourcesEqual = isStaticResourcesEqual;
|
|
740
|
+
exports.loadScript = loadScript;
|
|
741
|
+
exports.loadScriptNode = loadScriptNode;
|
|
742
|
+
exports.logger = logger;
|
|
743
|
+
exports.moduleFederationPlugin = ModuleFederationPlugin;
|
|
744
|
+
exports.normalizeOptions = normalizeOptions;
|
|
745
|
+
exports.parseEntry = parseEntry;
|
|
746
|
+
exports.safeWrapper = safeWrapper;
|
|
747
|
+
exports.sharePlugin = SharePlugin;
|
|
748
|
+
exports.simpleJoinRemoteEntry = simpleJoinRemoteEntry;
|
|
749
|
+
exports.warn = warn;
|