@module-federation/runtime 0.0.0-next-20240829065118 → 0.0.0-next-20240830065016

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.
@@ -1,7 +1,19 @@
1
1
  'use strict';
2
2
 
3
- var _extends = require('@swc/helpers/_/_extends');
4
-
3
+ function _extends() {
4
+ _extends = Object.assign || function(target) {
5
+ for(var i = 1; i < arguments.length; i++){
6
+ var source = arguments[i];
7
+ for(var key in source){
8
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
9
+ target[key] = source[key];
10
+ }
11
+ }
12
+ }
13
+ return target;
14
+ };
15
+ return _extends.apply(this, arguments);
16
+ }
5
17
  const defaultRetries = 3;
6
18
  async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, fallbackUrl = '' }) {
7
19
  try {
@@ -46,7 +58,7 @@ const RetryPlugin = (params)=>({
46
58
  async fetch (url, options) {
47
59
  return fetchWithRetry({
48
60
  url,
49
- options: _extends._({}, options, params == null ? void 0 : params.options),
61
+ options: _extends({}, options, params == null ? void 0 : params.options),
50
62
  retryTimes: params == null ? void 0 : params.retryTimes,
51
63
  fallbackUrl: params == null ? void 0 : params.fallbackUrl
52
64
  });
@@ -1,5 +1,17 @@
1
- import { _ } from '@swc/helpers/_/_extends';
2
-
1
+ function _extends() {
2
+ _extends = Object.assign || function(target) {
3
+ for(var i = 1; i < arguments.length; i++){
4
+ var source = arguments[i];
5
+ for(var key in source){
6
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
7
+ target[key] = source[key];
8
+ }
9
+ }
10
+ }
11
+ return target;
12
+ };
13
+ return _extends.apply(this, arguments);
14
+ }
3
15
  const defaultRetries = 3;
4
16
  async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, fallbackUrl = '' }) {
5
17
  try {
@@ -44,7 +56,7 @@ const RetryPlugin = (params)=>({
44
56
  async fetch (url, options) {
45
57
  return fetchWithRetry({
46
58
  url,
47
- options: _({}, options, params == null ? void 0 : params.options),
59
+ options: _extends({}, options, params == null ? void 0 : params.options),
48
60
  retryTimes: params == null ? void 0 : params.retryTimes,
49
61
  fallbackUrl: params == null ? void 0 : params.fallbackUrl
50
62
  });
package/dist/share.cjs.js CHANGED
@@ -1,13 +1,17 @@
1
1
  'use strict';
2
2
 
3
- var _extends = require('@swc/helpers/_/_extends');
4
- var _object_without_properties_loose = require('@swc/helpers/_/_object_without_properties_loose');
5
- var sdk = require('@module-federation/sdk');
3
+ require('@module-federation/sdk');
6
4
 
7
5
  function getBuilderId() {
8
6
  //@ts-ignore
9
7
  return typeof FEDERATION_BUILD_IDENTIFIER !== 'undefined' ? FEDERATION_BUILD_IDENTIFIER : '';
10
8
  }
9
+ function isDebugMode() {
10
+ return Boolean("");
11
+ }
12
+ function isBrowserEnv() {
13
+ return typeof window !== 'undefined';
14
+ }
11
15
 
12
16
  const LOG_CATEGORY = '[ Federation Runtime ]';
13
17
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
@@ -53,6 +57,14 @@ function isRemoteInfoWithEntry(remote) {
53
57
  function isPureRemoteEntry(remote) {
54
58
  return !remote.entry.includes('.json') && remote.entry.includes('.js');
55
59
  }
60
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
61
+ function safeToString(info) {
62
+ try {
63
+ return JSON.stringify(info, null, 2);
64
+ } catch (e) {
65
+ return '';
66
+ }
67
+ }
56
68
  function isObject(val) {
57
69
  return val && typeof val === 'object';
58
70
  }
@@ -72,7 +84,7 @@ function getRemoteEntryInfoFromSnapshot(snapshot) {
72
84
  type: 'global',
73
85
  globalName: ''
74
86
  };
75
- if (sdk.isBrowserEnv()) {
87
+ if (isBrowserEnv()) {
76
88
  return 'remoteEntry' in snapshot ? {
77
89
  url: snapshot.remoteEntry,
78
90
  type: snapshot.remoteEntryType,
@@ -89,6 +101,32 @@ function getRemoteEntryInfoFromSnapshot(snapshot) {
89
101
  return defaultRemoteEntryInfo;
90
102
  }
91
103
 
104
+ function _extends$1() {
105
+ _extends$1 = Object.assign || function(target) {
106
+ for(var i = 1; i < arguments.length; i++){
107
+ var source = arguments[i];
108
+ for(var key in source){
109
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
110
+ target[key] = source[key];
111
+ }
112
+ }
113
+ }
114
+ return target;
115
+ };
116
+ return _extends$1.apply(this, arguments);
117
+ }
118
+ function _object_without_properties_loose(source, excluded) {
119
+ if (source == null) return {};
120
+ var target = {};
121
+ var sourceKeys = Object.keys(source);
122
+ var key, i;
123
+ for(i = 0; i < sourceKeys.length; i++){
124
+ key = sourceKeys[i];
125
+ if (excluded.indexOf(key) >= 0) continue;
126
+ target[key] = source[key];
127
+ }
128
+ return target;
129
+ }
92
130
  const nativeGlobal = (()=>{
93
131
  try {
94
132
  return new Function('return this')();
@@ -173,10 +211,10 @@ function setGlobalFederationInstance(FederationInstance) {
173
211
  function getGlobalFederationConstructor() {
174
212
  return globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__;
175
213
  }
176
- function setGlobalFederationConstructor(FederationConstructor, isDebug = sdk.isDebugMode()) {
214
+ function setGlobalFederationConstructor(FederationConstructor, isDebug = isDebugMode()) {
177
215
  if (isDebug) {
178
216
  globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = FederationConstructor;
179
- globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.6.0";
217
+ globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR_VERSION__ = "0.5.1";
180
218
  }
181
219
  }
182
220
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -224,7 +262,7 @@ const getTargetSnapshotInfoByModuleInfo = (moduleInfo, snapshot)=>{
224
262
  }
225
263
  // If the remote is not included in the hostSnapshot, deploy a micro app snapshot
226
264
  if ('version' in moduleInfo && moduleInfo['version']) {
227
- const { version } = moduleInfo, resModuleInfo = _object_without_properties_loose._(moduleInfo, [
265
+ const { version } = moduleInfo, resModuleInfo = _object_without_properties_loose(moduleInfo, [
228
266
  "version"
229
267
  ]);
230
268
  const moduleKeyWithoutVersion = getFMId(resModuleInfo);
@@ -242,7 +280,7 @@ const setGlobalSnapshotInfoByModuleInfo = (remoteInfo, moduleDetailInfo)=>{
242
280
  return nativeGlobal.__FEDERATION__.moduleInfo;
243
281
  };
244
282
  const addGlobalSnapshot = (moduleInfos)=>{
245
- nativeGlobal.__FEDERATION__.moduleInfo = _extends._({}, nativeGlobal.__FEDERATION__.moduleInfo, moduleInfos);
283
+ nativeGlobal.__FEDERATION__.moduleInfo = _extends$1({}, nativeGlobal.__FEDERATION__.moduleInfo, moduleInfos);
246
284
  return ()=>{
247
285
  const keys = Object.keys(moduleInfos);
248
286
  for (const key of keys){
@@ -276,9 +314,216 @@ const getGlobalHostPlugins = ()=>nativeGlobal.__FEDERATION__.__GLOBAL_PLUGIN__;
276
314
  const getPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.get(id);
277
315
  const setPreloaded = (id)=>globalThis.__FEDERATION__.__PRELOADED_MAP__.set(id, true);
278
316
 
317
+ function registerPlugins(plugins, hookInstances) {
318
+ const globalPlugins = getGlobalHostPlugins();
319
+ // Incorporate global plugins
320
+ if (globalPlugins.length > 0) {
321
+ globalPlugins.forEach((plugin)=>{
322
+ if (plugins == null ? void 0 : plugins.find((item)=>item.name !== plugin.name)) {
323
+ plugins.push(plugin);
324
+ }
325
+ });
326
+ }
327
+ if (plugins && plugins.length > 0) {
328
+ plugins.forEach((plugin)=>{
329
+ hookInstances.forEach((hookInstance)=>{
330
+ hookInstance.applyPlugin(plugin);
331
+ });
332
+ });
333
+ }
334
+ return plugins;
335
+ }
336
+
279
337
  const DEFAULT_SCOPE = 'default';
280
338
  const DEFAULT_REMOTE_TYPE = 'global';
281
339
 
340
+ class SyncHook {
341
+ on(fn) {
342
+ if (typeof fn === 'function') {
343
+ this.listeners.add(fn);
344
+ }
345
+ }
346
+ once(fn) {
347
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
348
+ const self = this;
349
+ this.on(function wrapper(...args) {
350
+ self.remove(wrapper);
351
+ // eslint-disable-next-line prefer-spread
352
+ return fn.apply(null, args);
353
+ });
354
+ }
355
+ emit(...data) {
356
+ let result;
357
+ if (this.listeners.size > 0) {
358
+ // eslint-disable-next-line prefer-spread
359
+ this.listeners.forEach((fn)=>{
360
+ result = fn(...data);
361
+ });
362
+ }
363
+ return result;
364
+ }
365
+ remove(fn) {
366
+ this.listeners.delete(fn);
367
+ }
368
+ removeAll() {
369
+ this.listeners.clear();
370
+ }
371
+ constructor(type){
372
+ this.type = '';
373
+ this.listeners = new Set();
374
+ if (type) {
375
+ this.type = type;
376
+ }
377
+ }
378
+ }
379
+
380
+ class AsyncHook extends SyncHook {
381
+ emit(...data) {
382
+ let result;
383
+ const ls = Array.from(this.listeners);
384
+ if (ls.length > 0) {
385
+ let i = 0;
386
+ const call = (prev)=>{
387
+ if (prev === false) {
388
+ return false; // Abort process
389
+ } else if (i < ls.length) {
390
+ return Promise.resolve(ls[i++].apply(null, data)).then(call);
391
+ } else {
392
+ return prev;
393
+ }
394
+ };
395
+ result = call();
396
+ }
397
+ return Promise.resolve(result);
398
+ }
399
+ }
400
+
401
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
402
+ function checkReturnData(originalData, returnedData) {
403
+ if (!isObject(returnedData)) {
404
+ return false;
405
+ }
406
+ if (originalData !== returnedData) {
407
+ // eslint-disable-next-line no-restricted-syntax
408
+ for(const key in originalData){
409
+ if (!(key in returnedData)) {
410
+ return false;
411
+ }
412
+ }
413
+ }
414
+ return true;
415
+ }
416
+ class SyncWaterfallHook extends SyncHook {
417
+ emit(data) {
418
+ if (!isObject(data)) {
419
+ error(`The data for the "${this.type}" hook should be an object.`);
420
+ }
421
+ for (const fn of this.listeners){
422
+ try {
423
+ const tempData = fn(data);
424
+ if (checkReturnData(data, tempData)) {
425
+ data = tempData;
426
+ } else {
427
+ this.onerror(`A plugin returned an unacceptable value for the "${this.type}" type.`);
428
+ break;
429
+ }
430
+ } catch (e) {
431
+ warn(e);
432
+ this.onerror(e);
433
+ }
434
+ }
435
+ return data;
436
+ }
437
+ constructor(type){
438
+ super();
439
+ this.onerror = error;
440
+ this.type = type;
441
+ }
442
+ }
443
+
444
+ class AsyncWaterfallHook extends SyncHook {
445
+ emit(data) {
446
+ if (!isObject(data)) {
447
+ error(`The response data for the "${this.type}" hook must be an object.`);
448
+ }
449
+ const ls = Array.from(this.listeners);
450
+ if (ls.length > 0) {
451
+ let i = 0;
452
+ const processError = (e)=>{
453
+ warn(e);
454
+ this.onerror(e);
455
+ return data;
456
+ };
457
+ const call = (prevData)=>{
458
+ if (checkReturnData(data, prevData)) {
459
+ data = prevData;
460
+ if (i < ls.length) {
461
+ try {
462
+ return Promise.resolve(ls[i++](data)).then(call, processError);
463
+ } catch (e) {
464
+ return processError(e);
465
+ }
466
+ }
467
+ } else {
468
+ this.onerror(`A plugin returned an incorrect value for the "${this.type}" type.`);
469
+ }
470
+ return data;
471
+ };
472
+ return Promise.resolve(call(data));
473
+ }
474
+ return Promise.resolve(data);
475
+ }
476
+ constructor(type){
477
+ super();
478
+ this.onerror = error;
479
+ this.type = type;
480
+ }
481
+ }
482
+
483
+ class PluginSystem {
484
+ applyPlugin(plugin) {
485
+ assert(isPlainObject(plugin), 'Plugin configuration is invalid.');
486
+ // The plugin's name is mandatory and must be unique
487
+ const pluginName = plugin.name;
488
+ assert(pluginName, 'A name must be provided by the plugin.');
489
+ if (!this.registerPlugins[pluginName]) {
490
+ this.registerPlugins[pluginName] = plugin;
491
+ Object.keys(this.lifecycle).forEach((key)=>{
492
+ const pluginLife = plugin[key];
493
+ if (pluginLife) {
494
+ this.lifecycle[key].on(pluginLife);
495
+ }
496
+ });
497
+ }
498
+ }
499
+ removePlugin(pluginName) {
500
+ assert(pluginName, 'A name is required.');
501
+ const plugin = this.registerPlugins[pluginName];
502
+ assert(plugin, `The plugin "${pluginName}" is not registered.`);
503
+ Object.keys(plugin).forEach((key)=>{
504
+ if (key !== 'name') {
505
+ this.lifecycle[key].remove(plugin[key]);
506
+ }
507
+ });
508
+ }
509
+ // eslint-disable-next-line @typescript-eslint/no-shadow
510
+ inherit({ lifecycle, registerPlugins }) {
511
+ Object.keys(lifecycle).forEach((hookName)=>{
512
+ assert(!this.lifecycle[hookName], `The hook "${hookName}" has a conflict and cannot be inherited.`);
513
+ this.lifecycle[hookName] = lifecycle[hookName];
514
+ });
515
+ Object.keys(registerPlugins).forEach((pluginName)=>{
516
+ assert(!this.registerPlugins[pluginName], `The plugin "${pluginName}" has a conflict and cannot be inherited.`);
517
+ this.applyPlugin(registerPlugins[pluginName]);
518
+ });
519
+ }
520
+ constructor(lifecycle){
521
+ this.registerPlugins = {};
522
+ this.lifecycle = lifecycle;
523
+ this.lifecycleKeys = Object.keys(lifecycle);
524
+ }
525
+ }
526
+
282
527
  // fork from https://github.com/originjs/vite-plugin-federation/blob/v1.1.12/packages/lib/src/utils/semver/index.ts
283
528
  // those constants are based on https://www.rubydoc.info/gems/semantic_range/3.0.0/SemanticRange#BUILDIDENTIFIER-constant
284
529
  // Copyright (c)
@@ -635,6 +880,20 @@ function satisfy(version, range) {
635
880
  return true;
636
881
  }
637
882
 
883
+ function _extends() {
884
+ _extends = Object.assign || function(target) {
885
+ for(var i = 1; i < arguments.length; i++){
886
+ var source = arguments[i];
887
+ for(var key in source){
888
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
889
+ target[key] = source[key];
890
+ }
891
+ }
892
+ }
893
+ return target;
894
+ };
895
+ return _extends.apply(this, arguments);
896
+ }
638
897
  function formatShare(shareArgs, from, name, shareStrategy) {
639
898
  let get;
640
899
  if ('get' in shareArgs) {
@@ -651,13 +910,13 @@ function formatShare(shareArgs, from, name, shareStrategy) {
651
910
  warn(`"shared.strategy is deprecated, please set in initOptions.shareStrategy instead!"`);
652
911
  }
653
912
  var _shareArgs_version, _shareArgs_scope, _shareArgs_strategy;
654
- return _extends._({
913
+ return _extends({
655
914
  deps: [],
656
915
  useIn: [],
657
916
  from,
658
917
  loading: null
659
918
  }, shareArgs, {
660
- shareConfig: _extends._({
919
+ shareConfig: _extends({
661
920
  requiredVersion: `^${shareArgs.version}`,
662
921
  singleton: false,
663
922
  eager: false,
@@ -683,7 +942,7 @@ function formatShareConfigs(globalOptions, userOptions) {
683
942
  });
684
943
  return res;
685
944
  }, {});
686
- const shared = _extends._({}, globalOptions.shared);
945
+ const shared = _extends({}, globalOptions.shared);
687
946
  Object.keys(shareInfos).forEach((shareKey)=>{
688
947
  if (!shared[shareKey]) {
689
948
  shared[shareKey] = shareInfos[shareKey];
@@ -847,9 +1106,14 @@ function getTargetSharedOptions(options) {
847
1106
  return Object.assign({}, resolver(shareInfos[pkgName]), extraOptions == null ? void 0 : extraOptions.customShareInfo);
848
1107
  }
849
1108
 
1109
+ exports.AsyncHook = AsyncHook;
1110
+ exports.AsyncWaterfallHook = AsyncWaterfallHook;
850
1111
  exports.DEFAULT_REMOTE_TYPE = DEFAULT_REMOTE_TYPE;
851
1112
  exports.DEFAULT_SCOPE = DEFAULT_SCOPE;
852
1113
  exports.Global = Global;
1114
+ exports.PluginSystem = PluginSystem;
1115
+ exports.SyncHook = SyncHook;
1116
+ exports.SyncWaterfallHook = SyncWaterfallHook;
853
1117
  exports.addGlobalSnapshot = addGlobalSnapshot;
854
1118
  exports.addUniqueItem = addUniqueItem;
855
1119
  exports.arrayOptions = arrayOptions;
@@ -872,15 +1136,15 @@ exports.getRemoteEntryInfoFromSnapshot = getRemoteEntryInfoFromSnapshot;
872
1136
  exports.getTargetSharedOptions = getTargetSharedOptions;
873
1137
  exports.getTargetSnapshotInfoByModuleInfo = getTargetSnapshotInfoByModuleInfo;
874
1138
  exports.globalLoading = globalLoading;
875
- exports.isObject = isObject;
876
- exports.isPlainObject = isPlainObject;
1139
+ exports.isBrowserEnv = isBrowserEnv;
877
1140
  exports.isPureRemoteEntry = isPureRemoteEntry;
878
1141
  exports.isRemoteInfoWithEntry = isRemoteInfoWithEntry;
879
1142
  exports.nativeGlobal = nativeGlobal;
880
1143
  exports.registerGlobalPlugins = registerGlobalPlugins;
1144
+ exports.registerPlugins = registerPlugins;
881
1145
  exports.resetFederationGlobalInfo = resetFederationGlobalInfo;
1146
+ exports.safeToString = safeToString;
882
1147
  exports.setGlobalFederationConstructor = setGlobalFederationConstructor;
883
1148
  exports.setGlobalFederationInstance = setGlobalFederationInstance;
884
1149
  exports.setGlobalSnapshotInfoByModuleInfo = setGlobalSnapshotInfoByModuleInfo;
885
1150
  exports.setPreloaded = setPreloaded;
886
- exports.warn = warn;