@module-federation/data-prefetch 2.0.0 → 2.0.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/babel.cjs +95 -0
- package/dist/babel.d.ts +1 -2
- package/dist/babel.js +5 -0
- package/dist/cli/babel.cjs +115 -0
- package/dist/cli/babel.js +52 -0
- package/dist/cli/index.cjs +217 -0
- package/dist/{cli.esm.js → cli/index.js} +78 -73
- package/dist/cli.cjs +95 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +5 -0
- package/dist/common/constant.cjs +51 -0
- package/dist/common/constant.js +5 -0
- package/dist/common/index.cjs +95 -0
- package/dist/common/index.js +5 -0
- package/dist/common/node-utils.cjs +100 -0
- package/dist/common/node-utils.js +35 -0
- package/dist/common/runtime-utils.cjs +84 -0
- package/dist/common/runtime-utils.js +29 -0
- package/dist/constant.cjs +51 -0
- package/dist/constant.js +5 -0
- package/dist/index.cjs +106 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +7 -0
- package/dist/logger/index.cjs +60 -0
- package/dist/logger/index.js +10 -0
- package/dist/plugin.cjs +234 -0
- package/dist/plugin.d.ts +3 -2
- package/dist/plugin.js +173 -0
- package/dist/prefetch.cjs +197 -0
- package/dist/prefetch.js +149 -0
- package/dist/react/hooks.cjs +144 -0
- package/dist/{react.esm.js → react/hooks.js} +42 -30
- package/dist/react/index.cjs +95 -0
- package/dist/react/index.js +5 -0
- package/dist/react/utils.cjs +65 -0
- package/dist/react/utils.js +15 -0
- package/dist/react.cjs +95 -0
- package/dist/react.d.ts +1 -1
- package/dist/react.js +5 -0
- package/dist/universal/index.cjs +81 -0
- package/dist/{universal.esm.js → universal/index.js} +14 -10
- package/dist/universal.cjs +95 -0
- package/dist/universal.d.ts +1 -1
- package/dist/universal.js +5 -0
- package/package.json +47 -21
- package/dist/babel.cjs.cjs +0 -54
- package/dist/babel.cjs.cjs.map +0 -1
- package/dist/babel.cjs.d.ts +0 -2
- package/dist/babel.esm.js +0 -52
- package/dist/babel.esm.js.map +0 -1
- package/dist/cli.cjs.cjs +0 -156
- package/dist/cli.cjs.cjs.map +0 -1
- package/dist/cli.cjs.d.ts +0 -1
- package/dist/cli.esm.js.map +0 -1
- package/dist/constant.cjs.cjs +0 -6
- package/dist/constant.cjs.cjs.map +0 -1
- package/dist/constant.esm.js +0 -4
- package/dist/constant.esm.js.map +0 -1
- package/dist/index.cjs.cjs +0 -10
- package/dist/index.cjs.cjs.map +0 -1
- package/dist/index.cjs.d.ts +0 -1
- package/dist/index.cjs2.cjs +0 -8
- package/dist/index.cjs2.cjs.map +0 -1
- package/dist/index.esm.js +0 -3
- package/dist/index.esm.js.map +0 -1
- package/dist/index.esm2.js +0 -6
- package/dist/index.esm2.js.map +0 -1
- package/dist/plugin.cjs.cjs +0 -158
- package/dist/plugin.cjs.cjs.map +0 -1
- package/dist/plugin.cjs.d.ts +0 -2
- package/dist/plugin.esm.js +0 -153
- package/dist/plugin.esm.js.map +0 -1
- package/dist/prefetch.cjs.cjs +0 -182
- package/dist/prefetch.cjs.cjs.map +0 -1
- package/dist/prefetch.esm.js +0 -179
- package/dist/prefetch.esm.js.map +0 -1
- package/dist/react.cjs.cjs +0 -76
- package/dist/react.cjs.cjs.map +0 -1
- package/dist/react.cjs.d.ts +0 -1
- package/dist/react.esm.js.map +0 -1
- package/dist/runtime-utils.cjs.cjs +0 -29
- package/dist/runtime-utils.cjs.cjs.map +0 -1
- package/dist/runtime-utils.esm.js +0 -24
- package/dist/runtime-utils.esm.js.map +0 -1
- package/dist/src/index.d.ts +0 -2
- package/dist/src/plugin.d.ts +0 -3
- package/dist/universal.cjs.cjs +0 -30
- package/dist/universal.cjs.cjs.map +0 -1
- package/dist/universal.cjs.d.ts +0 -1
- package/dist/universal.esm.js.map +0 -1
- /package/dist/{src/cli → cli}/babel.d.ts +0 -0
- /package/dist/{src/cli → cli}/index.d.ts +0 -0
- /package/dist/{src/common → common}/constant.d.ts +0 -0
- /package/dist/{src/common → common}/index.d.ts +0 -0
- /package/dist/{src/common → common}/node-utils.d.ts +0 -0
- /package/dist/{src/common → common}/runtime-utils.d.ts +0 -0
- /package/dist/{src/constant.d.ts → constant.d.ts} +0 -0
- /package/dist/{src/logger → logger}/index.d.ts +0 -0
- /package/dist/{src/prefetch.d.ts → prefetch.d.ts} +0 -0
- /package/dist/{src/react → react}/hooks.d.ts +0 -0
- /package/dist/{src/react → react}/index.d.ts +0 -0
- /package/dist/{src/react → react}/utils.d.ts +0 -0
- /package/dist/{src/universal → universal}/index.d.ts +0 -0
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { getResourceUrl } from "@module-federation/sdk";
|
|
2
|
+
import { getSignalFromManifest } from "./common/runtime-utils.js";
|
|
3
|
+
import { MFDataPrefetch } from "./prefetch.js";
|
|
4
|
+
import logger from "./logger/index.js";
|
|
5
|
+
import { SHARED_STRATEGY } from "./constant.js";
|
|
6
|
+
|
|
7
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
8
|
+
|
|
9
|
+
;// CONCATENATED MODULE: external "./common/runtime-utils.js"
|
|
10
|
+
|
|
11
|
+
;// CONCATENATED MODULE: external "./prefetch.js"
|
|
12
|
+
|
|
13
|
+
;// CONCATENATED MODULE: external "./logger/index.js"
|
|
14
|
+
|
|
15
|
+
;// CONCATENATED MODULE: external "./constant.js"
|
|
16
|
+
|
|
17
|
+
;// CONCATENATED MODULE: ./src/plugin.ts
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const loadingArray = [];
|
|
24
|
+
let sharedFlag = SHARED_STRATEGY;
|
|
25
|
+
// eslint-disable-next-line max-lines-per-function
|
|
26
|
+
const prefetchPlugin = ()=>({
|
|
27
|
+
name: 'data-prefetch-runtime-plugin',
|
|
28
|
+
initContainer (options) {
|
|
29
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
30
|
+
if (!id) {
|
|
31
|
+
return options;
|
|
32
|
+
}
|
|
33
|
+
const snapshot = remoteSnapshot;
|
|
34
|
+
const { name } = remoteInfo;
|
|
35
|
+
const prefetchOptions = {
|
|
36
|
+
name,
|
|
37
|
+
remote: remoteInfo,
|
|
38
|
+
origin,
|
|
39
|
+
remoteSnapshot: snapshot
|
|
40
|
+
};
|
|
41
|
+
const signal = getSignalFromManifest(snapshot);
|
|
42
|
+
if (!signal) {
|
|
43
|
+
return options;
|
|
44
|
+
}
|
|
45
|
+
if (sharedFlag !== SHARED_STRATEGY) {
|
|
46
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
47
|
+
}
|
|
48
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
49
|
+
let prefetchUrl;
|
|
50
|
+
// @ts-expect-error
|
|
51
|
+
if (snapshot.prefetchEntry) {
|
|
52
|
+
// @ts-expect-error
|
|
53
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
54
|
+
}
|
|
55
|
+
const exist = loadingArray.find((loading)=>loading.id === id);
|
|
56
|
+
if (exist) {
|
|
57
|
+
return options;
|
|
58
|
+
}
|
|
59
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
60
|
+
const projectExports = instance.getProjectExports();
|
|
61
|
+
if (projectExports instanceof Promise) {
|
|
62
|
+
await projectExports;
|
|
63
|
+
}
|
|
64
|
+
return Promise.resolve().then(()=>{
|
|
65
|
+
const exports = instance.getExposeExports(id);
|
|
66
|
+
logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
|
|
67
|
+
const result = Object.keys(exports).map((k)=>{
|
|
68
|
+
const value = instance.prefetch({
|
|
69
|
+
id,
|
|
70
|
+
functionId: k
|
|
71
|
+
});
|
|
72
|
+
const functionId = k;
|
|
73
|
+
return {
|
|
74
|
+
value,
|
|
75
|
+
functionId
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
loadingArray.push({
|
|
82
|
+
id,
|
|
83
|
+
promise
|
|
84
|
+
});
|
|
85
|
+
return options;
|
|
86
|
+
},
|
|
87
|
+
afterResolve (options) {
|
|
88
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
89
|
+
const snapshot = remoteSnapshot;
|
|
90
|
+
const { name } = remoteInfo;
|
|
91
|
+
const prefetchOptions = {
|
|
92
|
+
name,
|
|
93
|
+
remote: remoteInfo,
|
|
94
|
+
origin,
|
|
95
|
+
remoteSnapshot: snapshot
|
|
96
|
+
};
|
|
97
|
+
const signal = getSignalFromManifest(snapshot);
|
|
98
|
+
if (!signal) {
|
|
99
|
+
return options;
|
|
100
|
+
}
|
|
101
|
+
const inited = loadingArray.some((info)=>info.id === id);
|
|
102
|
+
if (!inited) {
|
|
103
|
+
return options;
|
|
104
|
+
}
|
|
105
|
+
if (sharedFlag !== SHARED_STRATEGY) {
|
|
106
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
107
|
+
}
|
|
108
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
109
|
+
let prefetchUrl;
|
|
110
|
+
// @ts-expect-error
|
|
111
|
+
if (snapshot.prefetchEntry) {
|
|
112
|
+
// @ts-expect-error
|
|
113
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
114
|
+
}
|
|
115
|
+
const index = loadingArray.findIndex((loading)=>loading.id === id);
|
|
116
|
+
// clear cache
|
|
117
|
+
if (index !== -1) {
|
|
118
|
+
loadingArray.splice(index, 1);
|
|
119
|
+
}
|
|
120
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
121
|
+
const projectExports = instance.getProjectExports();
|
|
122
|
+
if (projectExports instanceof Promise) {
|
|
123
|
+
await projectExports;
|
|
124
|
+
}
|
|
125
|
+
return Promise.resolve().then(()=>{
|
|
126
|
+
const exports = instance.getExposeExports(id);
|
|
127
|
+
logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
|
|
128
|
+
const result = Object.keys(exports).map((k)=>{
|
|
129
|
+
const value = instance.prefetch({
|
|
130
|
+
id,
|
|
131
|
+
functionId: k
|
|
132
|
+
});
|
|
133
|
+
const functionId = k;
|
|
134
|
+
return {
|
|
135
|
+
value,
|
|
136
|
+
functionId
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
return result;
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
loadingArray.push({
|
|
143
|
+
id,
|
|
144
|
+
promise
|
|
145
|
+
});
|
|
146
|
+
return options;
|
|
147
|
+
},
|
|
148
|
+
async onLoad (options) {
|
|
149
|
+
var _loadingArray_find;
|
|
150
|
+
const { remote, id } = options;
|
|
151
|
+
const { name } = remote;
|
|
152
|
+
const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) === null || _loadingArray_find === void 0 ? void 0 : _loadingArray_find.promise;
|
|
153
|
+
if (promise) {
|
|
154
|
+
const prefetch = await promise;
|
|
155
|
+
const prefetchValue = prefetch.map((result)=>result.value);
|
|
156
|
+
await Promise.all(prefetchValue);
|
|
157
|
+
const instance = MFDataPrefetch.getInstance(name);
|
|
158
|
+
prefetch.forEach((result)=>{
|
|
159
|
+
const { value, functionId } = result;
|
|
160
|
+
instance.memorize(id + functionId, value);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return options;
|
|
164
|
+
},
|
|
165
|
+
beforeLoadShare (options) {
|
|
166
|
+
const shareInfo = options.shareInfo;
|
|
167
|
+
sharedFlag = (shareInfo === null || shareInfo === void 0 ? void 0 : shareInfo.strategy) || sharedFlag;
|
|
168
|
+
return options;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
/* ESM default export */ const src_plugin = (prefetchPlugin);
|
|
172
|
+
|
|
173
|
+
export { src_plugin as default, prefetchPlugin };
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
3
|
+
return typeof document === 'undefined'
|
|
4
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
5
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
6
|
+
new URL('main.js', document.baseURI).href;
|
|
7
|
+
})();
|
|
8
|
+
;
|
|
9
|
+
// The require scope
|
|
10
|
+
var __webpack_require__ = {};
|
|
11
|
+
|
|
12
|
+
/************************************************************************/
|
|
13
|
+
// webpack/runtime/define_property_getters
|
|
14
|
+
(() => {
|
|
15
|
+
__webpack_require__.d = (exports, definition) => {
|
|
16
|
+
for(var key in definition) {
|
|
17
|
+
if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
18
|
+
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
})();
|
|
23
|
+
// webpack/runtime/has_own_property
|
|
24
|
+
(() => {
|
|
25
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
26
|
+
})();
|
|
27
|
+
// webpack/runtime/make_namespace_object
|
|
28
|
+
(() => {
|
|
29
|
+
// define __esModule on exports
|
|
30
|
+
__webpack_require__.r = (exports) => {
|
|
31
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
32
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
/************************************************************************/
|
|
38
|
+
var __webpack_exports__ = {};
|
|
39
|
+
// ESM COMPAT FLAG
|
|
40
|
+
__webpack_require__.r(__webpack_exports__);
|
|
41
|
+
|
|
42
|
+
// EXPORTS
|
|
43
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
44
|
+
MFDataPrefetch: () => (/* binding */ MFDataPrefetch)
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
;// CONCATENATED MODULE: external "@module-federation/runtime"
|
|
48
|
+
const runtime_namespaceObject = require("@module-federation/runtime");
|
|
49
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
50
|
+
const sdk_namespaceObject = require("@module-federation/sdk");
|
|
51
|
+
;// CONCATENATED MODULE: external "./common/runtime-utils.cjs"
|
|
52
|
+
const runtime_utils_cjs_namespaceObject = require("./common/runtime-utils.cjs");
|
|
53
|
+
;// CONCATENATED MODULE: ./src/prefetch.ts
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// @ts-ignore init global variable for test
|
|
58
|
+
const federationGlobal = globalThis;
|
|
59
|
+
federationGlobal.__FEDERATION__ ??= {};
|
|
60
|
+
federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
|
|
61
|
+
entryLoading: {},
|
|
62
|
+
instance: new Map(),
|
|
63
|
+
__PREFETCH_EXPORTS__: {}
|
|
64
|
+
};
|
|
65
|
+
const getFederation = ()=>globalThis.__FEDERATION__;
|
|
66
|
+
class MFDataPrefetch {
|
|
67
|
+
get global() {
|
|
68
|
+
return getFederation().__PREFETCH__;
|
|
69
|
+
}
|
|
70
|
+
static getInstance(id) {
|
|
71
|
+
return getFederation().__PREFETCH__.instance.get(id);
|
|
72
|
+
}
|
|
73
|
+
async loadEntry(entry) {
|
|
74
|
+
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
75
|
+
if (entry) {
|
|
76
|
+
const { buildVersion, globalName } = remoteSnapshot;
|
|
77
|
+
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
78
|
+
if (!this.global.entryLoading[uniqueKey]) {
|
|
79
|
+
this.global.entryLoading[uniqueKey] = (0,sdk_namespaceObject.loadScript)(entry, {});
|
|
80
|
+
}
|
|
81
|
+
return this.global.entryLoading[uniqueKey];
|
|
82
|
+
} else {
|
|
83
|
+
const remoteInfo = (0,runtime_namespaceObject.getRemoteInfo)(remote);
|
|
84
|
+
const module = origin.moduleCache.get(remoteInfo.name);
|
|
85
|
+
return (0,runtime_namespaceObject.getRemoteEntry)({
|
|
86
|
+
origin: origin,
|
|
87
|
+
remoteInfo,
|
|
88
|
+
remoteEntryExports: module ? module.remoteEntryExports : undefined
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
getProjectExports() {
|
|
93
|
+
var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
|
|
94
|
+
if (Object.keys(this._exports).length > 0) {
|
|
95
|
+
return this._exports;
|
|
96
|
+
}
|
|
97
|
+
const { name } = this._options;
|
|
98
|
+
const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
|
|
99
|
+
const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
|
|
100
|
+
const resolve = exportsPromise.then((exports = {})=>{
|
|
101
|
+
// Match prefetch based on the function name suffix so that other capabilities can be expanded later.
|
|
102
|
+
// Not all functions should be directly identified as prefetch functions
|
|
103
|
+
const memory = {};
|
|
104
|
+
Object.keys(exports).forEach((key)=>{
|
|
105
|
+
memory[key] = {};
|
|
106
|
+
const exportVal = exports[key];
|
|
107
|
+
Object.keys(exportVal).reduce((memo, current)=>{
|
|
108
|
+
if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
|
|
109
|
+
memo[current] = exportVal[current];
|
|
110
|
+
}
|
|
111
|
+
return memo;
|
|
112
|
+
}, memory[key]);
|
|
113
|
+
});
|
|
114
|
+
this.memorizeExports(memory);
|
|
115
|
+
});
|
|
116
|
+
return resolve;
|
|
117
|
+
}
|
|
118
|
+
memorizeExports(exports) {
|
|
119
|
+
this._exports = exports;
|
|
120
|
+
}
|
|
121
|
+
getExposeExports(id) {
|
|
122
|
+
const prefetchId = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
|
|
123
|
+
const compatId = (0,runtime_utils_cjs_namespaceObject.compatGetPrefetchId)(id);
|
|
124
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
125
|
+
return prefetchExports || {};
|
|
126
|
+
}
|
|
127
|
+
prefetch(prefetchOptions) {
|
|
128
|
+
const { id, functionId = 'default', refetchParams } = prefetchOptions;
|
|
129
|
+
let prefetchResult;
|
|
130
|
+
const prefetchId = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
|
|
131
|
+
const compatId = (0,runtime_utils_cjs_namespaceObject.compatGetPrefetchId)(id);
|
|
132
|
+
const memorizeId = id + functionId;
|
|
133
|
+
const memory = this.prefetchMemory.get(memorizeId);
|
|
134
|
+
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
135
|
+
return memory;
|
|
136
|
+
}
|
|
137
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
138
|
+
if (!prefetchExports) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const executePrefetch = prefetchExports[functionId];
|
|
142
|
+
if (typeof executePrefetch === 'function') {
|
|
143
|
+
if (refetchParams) {
|
|
144
|
+
prefetchResult = executePrefetch(refetchParams);
|
|
145
|
+
} else {
|
|
146
|
+
prefetchResult = executePrefetch();
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
|
|
150
|
+
}
|
|
151
|
+
this.memorize(memorizeId, prefetchResult);
|
|
152
|
+
return prefetchResult;
|
|
153
|
+
}
|
|
154
|
+
memorize(id, value) {
|
|
155
|
+
this.prefetchMemory.set(id, value);
|
|
156
|
+
}
|
|
157
|
+
markOutdate(markOptions, isOutdate) {
|
|
158
|
+
const { id, functionId = 'default' } = markOptions;
|
|
159
|
+
if (!this.recordOutdate[id]) {
|
|
160
|
+
this.recordOutdate[id] = {};
|
|
161
|
+
}
|
|
162
|
+
this.recordOutdate[id][functionId] = isOutdate;
|
|
163
|
+
}
|
|
164
|
+
checkOutdate(outdateOptions) {
|
|
165
|
+
const { id, functionId = 'default', cacheStrategy } = outdateOptions;
|
|
166
|
+
if (typeof cacheStrategy === 'function') {
|
|
167
|
+
return cacheStrategy();
|
|
168
|
+
}
|
|
169
|
+
if (!this.recordOutdate[id]) {
|
|
170
|
+
this.recordOutdate[id] = {};
|
|
171
|
+
}
|
|
172
|
+
if (this.recordOutdate[id][functionId]) {
|
|
173
|
+
this.markOutdate({
|
|
174
|
+
id,
|
|
175
|
+
functionId
|
|
176
|
+
}, false);
|
|
177
|
+
return true;
|
|
178
|
+
} else {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
constructor(options){
|
|
183
|
+
this.prefetchMemory = new Map();
|
|
184
|
+
this.recordOutdate = {};
|
|
185
|
+
this._exports = {};
|
|
186
|
+
this._options = options;
|
|
187
|
+
this.global.instance.set(options.name, this);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
exports.MFDataPrefetch = __webpack_exports__.MFDataPrefetch;
|
|
192
|
+
for(var __webpack_i__ in __webpack_exports__) {
|
|
193
|
+
if(["MFDataPrefetch"].indexOf(__webpack_i__) === -1) {
|
|
194
|
+
exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
package/dist/prefetch.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { getRemoteEntry, getRemoteInfo } from "@module-federation/runtime";
|
|
2
|
+
import { loadScript } from "@module-federation/sdk";
|
|
3
|
+
import { compatGetPrefetchId, getPrefetchId } from "./common/runtime-utils.js";
|
|
4
|
+
|
|
5
|
+
;// CONCATENATED MODULE: external "@module-federation/runtime"
|
|
6
|
+
|
|
7
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
8
|
+
|
|
9
|
+
;// CONCATENATED MODULE: external "./common/runtime-utils.js"
|
|
10
|
+
|
|
11
|
+
;// CONCATENATED MODULE: ./src/prefetch.ts
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
// @ts-ignore init global variable for test
|
|
16
|
+
const federationGlobal = globalThis;
|
|
17
|
+
federationGlobal.__FEDERATION__ ??= {};
|
|
18
|
+
federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
|
|
19
|
+
entryLoading: {},
|
|
20
|
+
instance: new Map(),
|
|
21
|
+
__PREFETCH_EXPORTS__: {}
|
|
22
|
+
};
|
|
23
|
+
const getFederation = ()=>globalThis.__FEDERATION__;
|
|
24
|
+
class MFDataPrefetch {
|
|
25
|
+
get global() {
|
|
26
|
+
return getFederation().__PREFETCH__;
|
|
27
|
+
}
|
|
28
|
+
static getInstance(id) {
|
|
29
|
+
return getFederation().__PREFETCH__.instance.get(id);
|
|
30
|
+
}
|
|
31
|
+
async loadEntry(entry) {
|
|
32
|
+
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
33
|
+
if (entry) {
|
|
34
|
+
const { buildVersion, globalName } = remoteSnapshot;
|
|
35
|
+
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
36
|
+
if (!this.global.entryLoading[uniqueKey]) {
|
|
37
|
+
this.global.entryLoading[uniqueKey] = loadScript(entry, {});
|
|
38
|
+
}
|
|
39
|
+
return this.global.entryLoading[uniqueKey];
|
|
40
|
+
} else {
|
|
41
|
+
const remoteInfo = getRemoteInfo(remote);
|
|
42
|
+
const module = origin.moduleCache.get(remoteInfo.name);
|
|
43
|
+
return getRemoteEntry({
|
|
44
|
+
origin: origin,
|
|
45
|
+
remoteInfo,
|
|
46
|
+
remoteEntryExports: module ? module.remoteEntryExports : undefined
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
getProjectExports() {
|
|
51
|
+
var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
|
|
52
|
+
if (Object.keys(this._exports).length > 0) {
|
|
53
|
+
return this._exports;
|
|
54
|
+
}
|
|
55
|
+
const { name } = this._options;
|
|
56
|
+
const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
|
|
57
|
+
const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
|
|
58
|
+
const resolve = exportsPromise.then((exports = {})=>{
|
|
59
|
+
// Match prefetch based on the function name suffix so that other capabilities can be expanded later.
|
|
60
|
+
// Not all functions should be directly identified as prefetch functions
|
|
61
|
+
const memory = {};
|
|
62
|
+
Object.keys(exports).forEach((key)=>{
|
|
63
|
+
memory[key] = {};
|
|
64
|
+
const exportVal = exports[key];
|
|
65
|
+
Object.keys(exportVal).reduce((memo, current)=>{
|
|
66
|
+
if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
|
|
67
|
+
memo[current] = exportVal[current];
|
|
68
|
+
}
|
|
69
|
+
return memo;
|
|
70
|
+
}, memory[key]);
|
|
71
|
+
});
|
|
72
|
+
this.memorizeExports(memory);
|
|
73
|
+
});
|
|
74
|
+
return resolve;
|
|
75
|
+
}
|
|
76
|
+
memorizeExports(exports) {
|
|
77
|
+
this._exports = exports;
|
|
78
|
+
}
|
|
79
|
+
getExposeExports(id) {
|
|
80
|
+
const prefetchId = getPrefetchId(id);
|
|
81
|
+
const compatId = compatGetPrefetchId(id);
|
|
82
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
83
|
+
return prefetchExports || {};
|
|
84
|
+
}
|
|
85
|
+
prefetch(prefetchOptions) {
|
|
86
|
+
const { id, functionId = 'default', refetchParams } = prefetchOptions;
|
|
87
|
+
let prefetchResult;
|
|
88
|
+
const prefetchId = getPrefetchId(id);
|
|
89
|
+
const compatId = compatGetPrefetchId(id);
|
|
90
|
+
const memorizeId = id + functionId;
|
|
91
|
+
const memory = this.prefetchMemory.get(memorizeId);
|
|
92
|
+
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
93
|
+
return memory;
|
|
94
|
+
}
|
|
95
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
96
|
+
if (!prefetchExports) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const executePrefetch = prefetchExports[functionId];
|
|
100
|
+
if (typeof executePrefetch === 'function') {
|
|
101
|
+
if (refetchParams) {
|
|
102
|
+
prefetchResult = executePrefetch(refetchParams);
|
|
103
|
+
} else {
|
|
104
|
+
prefetchResult = executePrefetch();
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
|
|
108
|
+
}
|
|
109
|
+
this.memorize(memorizeId, prefetchResult);
|
|
110
|
+
return prefetchResult;
|
|
111
|
+
}
|
|
112
|
+
memorize(id, value) {
|
|
113
|
+
this.prefetchMemory.set(id, value);
|
|
114
|
+
}
|
|
115
|
+
markOutdate(markOptions, isOutdate) {
|
|
116
|
+
const { id, functionId = 'default' } = markOptions;
|
|
117
|
+
if (!this.recordOutdate[id]) {
|
|
118
|
+
this.recordOutdate[id] = {};
|
|
119
|
+
}
|
|
120
|
+
this.recordOutdate[id][functionId] = isOutdate;
|
|
121
|
+
}
|
|
122
|
+
checkOutdate(outdateOptions) {
|
|
123
|
+
const { id, functionId = 'default', cacheStrategy } = outdateOptions;
|
|
124
|
+
if (typeof cacheStrategy === 'function') {
|
|
125
|
+
return cacheStrategy();
|
|
126
|
+
}
|
|
127
|
+
if (!this.recordOutdate[id]) {
|
|
128
|
+
this.recordOutdate[id] = {};
|
|
129
|
+
}
|
|
130
|
+
if (this.recordOutdate[id][functionId]) {
|
|
131
|
+
this.markOutdate({
|
|
132
|
+
id,
|
|
133
|
+
functionId
|
|
134
|
+
}, false);
|
|
135
|
+
return true;
|
|
136
|
+
} else {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
constructor(options){
|
|
141
|
+
this.prefetchMemory = new Map();
|
|
142
|
+
this.recordOutdate = {};
|
|
143
|
+
this._exports = {};
|
|
144
|
+
this._options = options;
|
|
145
|
+
this.global.instance.set(options.name, this);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { MFDataPrefetch };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
3
|
+
return typeof document === 'undefined'
|
|
4
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
5
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
6
|
+
new URL('main.js', document.baseURI).href;
|
|
7
|
+
})();
|
|
8
|
+
;
|
|
9
|
+
// The require scope
|
|
10
|
+
var __webpack_require__ = {};
|
|
11
|
+
|
|
12
|
+
/************************************************************************/
|
|
13
|
+
// webpack/runtime/compat_get_default_export
|
|
14
|
+
(() => {
|
|
15
|
+
// getDefaultExport function for compatibility with non-ESM modules
|
|
16
|
+
__webpack_require__.n = (module) => {
|
|
17
|
+
var getter = module && module.__esModule ?
|
|
18
|
+
() => (module['default']) :
|
|
19
|
+
() => (module);
|
|
20
|
+
__webpack_require__.d(getter, { a: getter });
|
|
21
|
+
return getter;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
})();
|
|
25
|
+
// webpack/runtime/define_property_getters
|
|
26
|
+
(() => {
|
|
27
|
+
__webpack_require__.d = (exports, definition) => {
|
|
28
|
+
for(var key in definition) {
|
|
29
|
+
if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
30
|
+
Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
// webpack/runtime/has_own_property
|
|
36
|
+
(() => {
|
|
37
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
38
|
+
})();
|
|
39
|
+
// webpack/runtime/make_namespace_object
|
|
40
|
+
(() => {
|
|
41
|
+
// define __esModule on exports
|
|
42
|
+
__webpack_require__.r = (exports) => {
|
|
43
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
44
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
45
|
+
}
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
};
|
|
48
|
+
})();
|
|
49
|
+
/************************************************************************/
|
|
50
|
+
var __webpack_exports__ = {};
|
|
51
|
+
// ESM COMPAT FLAG
|
|
52
|
+
__webpack_require__.r(__webpack_exports__);
|
|
53
|
+
|
|
54
|
+
// EXPORTS
|
|
55
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
56
|
+
usePrefetch: () => (/* binding */ usePrefetch)
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
;// CONCATENATED MODULE: external "react"
|
|
60
|
+
const external_react_namespaceObject = require("react");
|
|
61
|
+
;// CONCATENATED MODULE: external "../logger/index.cjs"
|
|
62
|
+
const index_cjs_namespaceObject = require("../logger/index.cjs");
|
|
63
|
+
var index_cjs_default = /*#__PURE__*/__webpack_require__.n(index_cjs_namespaceObject);
|
|
64
|
+
;// CONCATENATED MODULE: external "../prefetch.cjs"
|
|
65
|
+
const external_prefetch_cjs_namespaceObject = require("../prefetch.cjs");
|
|
66
|
+
;// CONCATENATED MODULE: external "../universal.cjs"
|
|
67
|
+
const external_universal_cjs_namespaceObject = require("../universal.cjs");
|
|
68
|
+
;// CONCATENATED MODULE: external "../common/runtime-utils.cjs"
|
|
69
|
+
const runtime_utils_cjs_namespaceObject = require("../common/runtime-utils.cjs");
|
|
70
|
+
;// CONCATENATED MODULE: external "./utils.cjs"
|
|
71
|
+
const external_utils_cjs_namespaceObject = require("./utils.cjs");
|
|
72
|
+
;// CONCATENATED MODULE: ./src/react/hooks.ts
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
const usePrefetch = (options)=>{
|
|
80
|
+
const isFirstMounted = (0,external_utils_cjs_namespaceObject.useFirstMounted)();
|
|
81
|
+
if (isFirstMounted) {
|
|
82
|
+
const startTiming = performance.now();
|
|
83
|
+
index_cjs_default().info(`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || 'default'} - ${startTiming}`);
|
|
84
|
+
}
|
|
85
|
+
const { id, functionId, deferId } = options;
|
|
86
|
+
const prefetchInfo = {
|
|
87
|
+
id,
|
|
88
|
+
functionId
|
|
89
|
+
};
|
|
90
|
+
const mfScope = (0,runtime_utils_cjs_namespaceObject.getScope)();
|
|
91
|
+
let state;
|
|
92
|
+
const prefetchResult = (0,external_universal_cjs_namespaceObject.prefetch)(options);
|
|
93
|
+
if (deferId) {
|
|
94
|
+
if (prefetchResult instanceof Promise) {
|
|
95
|
+
state = prefetchResult.then((deferredData)=>deferredData.data[deferId]);
|
|
96
|
+
} else {
|
|
97
|
+
state = prefetchResult.data[deferId];
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
state = prefetchResult;
|
|
101
|
+
}
|
|
102
|
+
const [prefetchState, setPrefetchState] = (0,external_react_namespaceObject.useState)(state);
|
|
103
|
+
const prefetchInstance = external_prefetch_cjs_namespaceObject.MFDataPrefetch.getInstance(mfScope);
|
|
104
|
+
(0,external_react_namespaceObject.useEffect)(()=>{
|
|
105
|
+
const useEffectTiming = performance.now();
|
|
106
|
+
index_cjs_default().info(`3. Start Execute UseEffect: ${options.id} - ${options.functionId || 'default'} - ${useEffectTiming}`);
|
|
107
|
+
return ()=>{
|
|
108
|
+
prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
const refreshExecutor = (refetchParams)=>{
|
|
112
|
+
const refetchOptions = {
|
|
113
|
+
...options
|
|
114
|
+
};
|
|
115
|
+
if (refetchParams) {
|
|
116
|
+
refetchOptions.refetchParams = refetchParams;
|
|
117
|
+
}
|
|
118
|
+
prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
119
|
+
const newVal = (0,external_universal_cjs_namespaceObject.prefetch)(refetchOptions);
|
|
120
|
+
let newState;
|
|
121
|
+
if (deferId) {
|
|
122
|
+
if (newVal instanceof Promise) {
|
|
123
|
+
newState = newVal.then((deferredData)=>deferredData.data[deferId]);
|
|
124
|
+
} else {
|
|
125
|
+
newState = newVal.data[deferId];
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
newState = newVal;
|
|
129
|
+
}
|
|
130
|
+
setPrefetchState(newState);
|
|
131
|
+
};
|
|
132
|
+
return [
|
|
133
|
+
prefetchState,
|
|
134
|
+
refreshExecutor
|
|
135
|
+
];
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
exports.usePrefetch = __webpack_exports__.usePrefetch;
|
|
139
|
+
for(var __webpack_i__ in __webpack_exports__) {
|
|
140
|
+
if(["usePrefetch"].indexOf(__webpack_i__) === -1) {
|
|
141
|
+
exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|