@module-federation/data-prefetch 0.6.6 → 0.6.8
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/.swcrc +29 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +1 -1
- package/__tests__/prefetch.spec.ts +3 -2
- package/__tests__/react.spec.ts +1 -1
- package/dist/LICENSE +21 -0
- package/dist/babel.cjs.d.ts +2 -0
- package/dist/babel.cjs.js +50 -0
- package/dist/babel.esm.d.ts +2 -0
- package/dist/babel.esm.js +48 -0
- package/dist/cli.cjs.d.ts +1 -0
- package/dist/cli.cjs.js +155 -0
- package/dist/cli.esm.d.ts +1 -0
- package/dist/cli.esm.js +152 -0
- package/dist/index.cjs.d.ts +1 -0
- package/dist/index.cjs.js +13 -0
- package/dist/index.cjs2.js +7 -0
- package/dist/index.esm.d.ts +1 -0
- package/dist/index.esm.js +6 -0
- package/dist/index.esm2.js +5 -0
- package/dist/package.json +100 -0
- package/dist/plugin.cjs.d.ts +2 -0
- package/dist/plugin.cjs.js +159 -0
- package/dist/plugin.esm.d.ts +2 -0
- package/dist/plugin.esm.js +154 -0
- package/dist/polyfills.cjs.js +14 -0
- package/dist/polyfills.esm.js +12 -0
- package/dist/prefetch.cjs.js +142 -0
- package/dist/prefetch.esm.js +140 -0
- package/dist/react.cjs.d.ts +1 -0
- package/dist/react.cjs.js +77 -0
- package/dist/react.esm.d.ts +1 -0
- package/dist/react.esm.js +75 -0
- package/dist/runtime-utils.cjs.js +27 -0
- package/dist/runtime-utils.esm.js +22 -0
- package/dist/shared.cjs.d.ts +2 -0
- package/dist/shared.cjs.js +24 -0
- package/dist/shared.esm.d.ts +2 -0
- package/dist/shared.esm.js +22 -0
- package/dist/{cli → src/cli}/babel.d.ts +2 -4
- package/dist/{cli → src/cli}/index.d.ts +5 -8
- package/dist/src/common/constant.d.ts +1 -0
- package/dist/src/common/index.d.ts +1 -0
- package/dist/src/common/node-utils.d.ts +2 -0
- package/dist/src/common/runtime-utils.d.ts +5 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/logger/index.d.ts +3 -0
- package/dist/src/plugin.d.ts +3 -0
- package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +5 -7
- package/dist/src/react/hooks.d.ts +11 -0
- package/dist/src/react/index.d.ts +1 -0
- package/dist/src/react/utils.d.ts +1 -0
- package/dist/src/shared/index.d.ts +3 -0
- package/dist/src/universal/index.d.ts +2 -0
- package/dist/universal.cjs.d.ts +1 -0
- package/dist/universal.cjs.js +29 -0
- package/dist/universal.esm.d.ts +1 -0
- package/dist/universal.esm.js +27 -0
- package/jest.config.js +2 -2
- package/package.json +33 -30
- package/project.json +48 -6
- package/rollup.config.js +46 -0
- package/src/cli/index.ts +12 -10
- package/src/prefetch.ts +6 -2
- package/tsconfig.json +1 -1
- package/tsconfig.lib.json +10 -0
- package/dist/cli/babel.js +0 -84
- package/dist/cli/index.js +0 -205
- package/dist/esm/chunk-AJPO2B2T.js +0 -30
- package/dist/esm/chunk-EZUCZHGV.js +0 -11
- package/dist/esm/chunk-ISLXMAIA.js +0 -155
- package/dist/esm/chunk-KI4QGPIS.js +0 -32
- package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
- package/dist/esm/chunk-VVZ6XIM6.js +0 -166
- package/dist/esm/cli/babel.js +0 -57
- package/dist/esm/cli/index.js +0 -178
- package/dist/esm/index.js +0 -13
- package/dist/esm/plugin.js +0 -12
- package/dist/esm/react/index.js +0 -94
- package/dist/esm/shared/index.js +0 -27
- package/dist/esm/universal/index.js +0 -9
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -350
- package/dist/plugin.d.ts +0 -5
- package/dist/plugin.js +0 -346
- package/dist/react/index.d.ts +0 -16
- package/dist/react/index.js +0 -286
- package/dist/shared/index.d.ts +0 -5
- package/dist/shared/index.js +0 -48
- package/dist/universal/index.d.ts +0 -8
- package/dist/universal/index.js +0 -204
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { getRemoteInfo, getRemoteEntry } from '@module-federation/runtime';
|
|
2
|
+
import { loadScript } from '@module-federation/sdk';
|
|
3
|
+
import { b as getPrefetchId, c as compatGetPrefetchId } from './runtime-utils.esm.js';
|
|
4
|
+
|
|
5
|
+
var // @ts-ignore init global variable for test
|
|
6
|
+
_globalThis, _globalThis___FEDERATION__;
|
|
7
|
+
var ___FEDERATION__;
|
|
8
|
+
(___FEDERATION__ = (_globalThis = globalThis).__FEDERATION__) != null ? ___FEDERATION__ : _globalThis.__FEDERATION__ = {};
|
|
9
|
+
var ___PREFETCH__;
|
|
10
|
+
(___PREFETCH__ = (_globalThis___FEDERATION__ = globalThis.__FEDERATION__).__PREFETCH__) != null ? ___PREFETCH__ : _globalThis___FEDERATION__.__PREFETCH__ = {
|
|
11
|
+
entryLoading: {},
|
|
12
|
+
instance: new Map(),
|
|
13
|
+
__PREFETCH_EXPORTS__: {}
|
|
14
|
+
};
|
|
15
|
+
class MFDataPrefetch {
|
|
16
|
+
get global() {
|
|
17
|
+
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
18
|
+
}
|
|
19
|
+
static getInstance(id) {
|
|
20
|
+
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
21
|
+
}
|
|
22
|
+
async loadEntry(entry) {
|
|
23
|
+
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
24
|
+
if (entry) {
|
|
25
|
+
const { buildVersion, globalName } = remoteSnapshot;
|
|
26
|
+
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
27
|
+
if (!this.global.entryLoading[uniqueKey]) {
|
|
28
|
+
this.global.entryLoading[uniqueKey] = loadScript(entry, {});
|
|
29
|
+
}
|
|
30
|
+
return this.global.entryLoading[uniqueKey];
|
|
31
|
+
} else {
|
|
32
|
+
const remoteInfo = getRemoteInfo(remote);
|
|
33
|
+
const module = origin.moduleCache.get(remoteInfo.name);
|
|
34
|
+
return getRemoteEntry({
|
|
35
|
+
origin: origin,
|
|
36
|
+
remoteInfo,
|
|
37
|
+
remoteEntryExports: module ? module.remoteEntryExports : undefined
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
getProjectExports() {
|
|
42
|
+
var _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__;
|
|
43
|
+
if (Object.keys(this._exports).length > 0) {
|
|
44
|
+
return this._exports;
|
|
45
|
+
}
|
|
46
|
+
const { name } = this._options;
|
|
47
|
+
const exportsPromiseFn = (_globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__ = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__[name];
|
|
48
|
+
const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : Promise.resolve({});
|
|
49
|
+
const resolve = exportsPromise.then((exports = {})=>{
|
|
50
|
+
// Match prefetch based on the function name suffix so that other capabilities can be expanded later.
|
|
51
|
+
// Not all functions should be directly identified as prefetch functions
|
|
52
|
+
const memory = {};
|
|
53
|
+
Object.keys(exports).forEach((key)=>{
|
|
54
|
+
memory[key] = {};
|
|
55
|
+
const exportVal = exports[key];
|
|
56
|
+
Object.keys(exportVal).reduce((memo, current)=>{
|
|
57
|
+
if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
|
|
58
|
+
memo[current] = exportVal[current];
|
|
59
|
+
}
|
|
60
|
+
return memo;
|
|
61
|
+
}, memory[key]);
|
|
62
|
+
});
|
|
63
|
+
this.memorizeExports(memory);
|
|
64
|
+
});
|
|
65
|
+
return resolve;
|
|
66
|
+
}
|
|
67
|
+
memorizeExports(exports) {
|
|
68
|
+
this._exports = exports;
|
|
69
|
+
}
|
|
70
|
+
getExposeExports(id) {
|
|
71
|
+
const prefetchId = getPrefetchId(id);
|
|
72
|
+
const compatId = compatGetPrefetchId(id);
|
|
73
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
74
|
+
return prefetchExports || {};
|
|
75
|
+
}
|
|
76
|
+
prefetch(prefetchOptions) {
|
|
77
|
+
const { id, functionId = 'default', refetchParams } = prefetchOptions;
|
|
78
|
+
let prefetchResult;
|
|
79
|
+
const prefetchId = getPrefetchId(id);
|
|
80
|
+
const compatId = compatGetPrefetchId(id);
|
|
81
|
+
const memorizeId = id + functionId;
|
|
82
|
+
const memory = this.prefetchMemory.get(memorizeId);
|
|
83
|
+
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
84
|
+
return memory;
|
|
85
|
+
}
|
|
86
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
87
|
+
if (!prefetchExports) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const executePrefetch = prefetchExports[functionId];
|
|
91
|
+
if (typeof executePrefetch === 'function') {
|
|
92
|
+
if (refetchParams) {
|
|
93
|
+
prefetchResult = executePrefetch(refetchParams);
|
|
94
|
+
} else {
|
|
95
|
+
prefetchResult = executePrefetch();
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
|
|
99
|
+
}
|
|
100
|
+
this.memorize(memorizeId, prefetchResult);
|
|
101
|
+
return prefetchResult;
|
|
102
|
+
}
|
|
103
|
+
memorize(id, value) {
|
|
104
|
+
this.prefetchMemory.set(id, value);
|
|
105
|
+
}
|
|
106
|
+
markOutdate(markOptions, isOutdate) {
|
|
107
|
+
const { id, functionId = 'default' } = markOptions;
|
|
108
|
+
if (!this.recordOutdate[id]) {
|
|
109
|
+
this.recordOutdate[id] = {};
|
|
110
|
+
}
|
|
111
|
+
this.recordOutdate[id][functionId] = isOutdate;
|
|
112
|
+
}
|
|
113
|
+
checkOutdate(outdateOptions) {
|
|
114
|
+
const { id, functionId = 'default', cacheStrategy } = outdateOptions;
|
|
115
|
+
if (typeof cacheStrategy === 'function') {
|
|
116
|
+
return cacheStrategy();
|
|
117
|
+
}
|
|
118
|
+
if (!this.recordOutdate[id]) {
|
|
119
|
+
this.recordOutdate[id] = {};
|
|
120
|
+
}
|
|
121
|
+
if (this.recordOutdate[id][functionId]) {
|
|
122
|
+
this.markOutdate({
|
|
123
|
+
id,
|
|
124
|
+
functionId
|
|
125
|
+
}, false);
|
|
126
|
+
return true;
|
|
127
|
+
} else {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
constructor(options){
|
|
132
|
+
this.prefetchMemory = new Map();
|
|
133
|
+
this.recordOutdate = {};
|
|
134
|
+
this._exports = {};
|
|
135
|
+
this._options = options;
|
|
136
|
+
this.global.instance.set(options.name, this);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { MFDataPrefetch as M };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/react/index";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var polyfills = require('./polyfills.cjs.js');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var index = require('./index.cjs2.js');
|
|
6
|
+
var prefetch = require('./prefetch.cjs.js');
|
|
7
|
+
var universal = require('./universal.cjs.js');
|
|
8
|
+
var runtimeUtils = require('./runtime-utils.cjs.js');
|
|
9
|
+
require('@module-federation/sdk');
|
|
10
|
+
require('@module-federation/runtime');
|
|
11
|
+
|
|
12
|
+
const useFirstMounted = ()=>{
|
|
13
|
+
const ref = react.useRef(true);
|
|
14
|
+
react.useEffect(()=>{
|
|
15
|
+
ref.current = false;
|
|
16
|
+
}, []);
|
|
17
|
+
return ref.current;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const usePrefetch = (options)=>{
|
|
21
|
+
const isFirstMounted = useFirstMounted();
|
|
22
|
+
if (isFirstMounted) {
|
|
23
|
+
const startTiming = performance.now();
|
|
24
|
+
index.logger.info(`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || 'default'} - ${startTiming}`);
|
|
25
|
+
}
|
|
26
|
+
const { id, functionId, deferId } = options;
|
|
27
|
+
const prefetchInfo = {
|
|
28
|
+
id,
|
|
29
|
+
functionId
|
|
30
|
+
};
|
|
31
|
+
const mfScope = runtimeUtils.getScope();
|
|
32
|
+
let state;
|
|
33
|
+
const prefetchResult = universal.prefetch(options);
|
|
34
|
+
if (deferId) {
|
|
35
|
+
if (prefetchResult instanceof Promise) {
|
|
36
|
+
state = prefetchResult.then((deferredData)=>deferredData.data[deferId]);
|
|
37
|
+
} else {
|
|
38
|
+
state = prefetchResult.data[deferId];
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
state = prefetchResult;
|
|
42
|
+
}
|
|
43
|
+
const [prefetchState, setPrefetchState] = react.useState(state);
|
|
44
|
+
const prefetchInstance = prefetch.MFDataPrefetch.getInstance(mfScope);
|
|
45
|
+
react.useEffect(()=>{
|
|
46
|
+
const useEffectTiming = performance.now();
|
|
47
|
+
index.logger.info(`3. Start Execute UseEffect: ${options.id} - ${options.functionId || 'default'} - ${useEffectTiming}`);
|
|
48
|
+
return ()=>{
|
|
49
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
50
|
+
};
|
|
51
|
+
}, []);
|
|
52
|
+
const refreshExecutor = (refetchParams)=>{
|
|
53
|
+
const refetchOptions = polyfills._extends({}, options);
|
|
54
|
+
if (refetchParams) {
|
|
55
|
+
refetchOptions.refetchParams = refetchParams;
|
|
56
|
+
}
|
|
57
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
58
|
+
const newVal = universal.prefetch(refetchOptions);
|
|
59
|
+
let newState;
|
|
60
|
+
if (deferId) {
|
|
61
|
+
if (newVal instanceof Promise) {
|
|
62
|
+
newState = newVal.then((deferredData)=>deferredData.data[deferId]);
|
|
63
|
+
} else {
|
|
64
|
+
newState = newVal.data[deferId];
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
newState = newVal;
|
|
68
|
+
}
|
|
69
|
+
setPrefetchState(newState);
|
|
70
|
+
};
|
|
71
|
+
return [
|
|
72
|
+
prefetchState,
|
|
73
|
+
refreshExecutor
|
|
74
|
+
];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
exports.usePrefetch = usePrefetch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/react/index";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { _ as _extends } from './polyfills.esm.js';
|
|
2
|
+
import { useRef, useEffect, useState } from 'react';
|
|
3
|
+
import { l as logger } from './index.esm2.js';
|
|
4
|
+
import { M as MFDataPrefetch } from './prefetch.esm.js';
|
|
5
|
+
import { prefetch } from './universal.esm.js';
|
|
6
|
+
import { g as getScope } from './runtime-utils.esm.js';
|
|
7
|
+
import '@module-federation/sdk';
|
|
8
|
+
import '@module-federation/runtime';
|
|
9
|
+
|
|
10
|
+
const useFirstMounted = ()=>{
|
|
11
|
+
const ref = useRef(true);
|
|
12
|
+
useEffect(()=>{
|
|
13
|
+
ref.current = false;
|
|
14
|
+
}, []);
|
|
15
|
+
return ref.current;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const usePrefetch = (options)=>{
|
|
19
|
+
const isFirstMounted = useFirstMounted();
|
|
20
|
+
if (isFirstMounted) {
|
|
21
|
+
const startTiming = performance.now();
|
|
22
|
+
logger.info(`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || 'default'} - ${startTiming}`);
|
|
23
|
+
}
|
|
24
|
+
const { id, functionId, deferId } = options;
|
|
25
|
+
const prefetchInfo = {
|
|
26
|
+
id,
|
|
27
|
+
functionId
|
|
28
|
+
};
|
|
29
|
+
const mfScope = getScope();
|
|
30
|
+
let state;
|
|
31
|
+
const prefetchResult = prefetch(options);
|
|
32
|
+
if (deferId) {
|
|
33
|
+
if (prefetchResult instanceof Promise) {
|
|
34
|
+
state = prefetchResult.then((deferredData)=>deferredData.data[deferId]);
|
|
35
|
+
} else {
|
|
36
|
+
state = prefetchResult.data[deferId];
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
state = prefetchResult;
|
|
40
|
+
}
|
|
41
|
+
const [prefetchState, setPrefetchState] = useState(state);
|
|
42
|
+
const prefetchInstance = MFDataPrefetch.getInstance(mfScope);
|
|
43
|
+
useEffect(()=>{
|
|
44
|
+
const useEffectTiming = performance.now();
|
|
45
|
+
logger.info(`3. Start Execute UseEffect: ${options.id} - ${options.functionId || 'default'} - ${useEffectTiming}`);
|
|
46
|
+
return ()=>{
|
|
47
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
48
|
+
};
|
|
49
|
+
}, []);
|
|
50
|
+
const refreshExecutor = (refetchParams)=>{
|
|
51
|
+
const refetchOptions = _extends({}, options);
|
|
52
|
+
if (refetchParams) {
|
|
53
|
+
refetchOptions.refetchParams = refetchParams;
|
|
54
|
+
}
|
|
55
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
56
|
+
const newVal = prefetch(refetchOptions);
|
|
57
|
+
let newState;
|
|
58
|
+
if (deferId) {
|
|
59
|
+
if (newVal instanceof Promise) {
|
|
60
|
+
newState = newVal.then((deferredData)=>deferredData.data[deferId]);
|
|
61
|
+
} else {
|
|
62
|
+
newState = newVal.data[deferId];
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
newState = newVal;
|
|
66
|
+
}
|
|
67
|
+
setPrefetchState(newState);
|
|
68
|
+
};
|
|
69
|
+
return [
|
|
70
|
+
prefetchState,
|
|
71
|
+
refreshExecutor
|
|
72
|
+
];
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { usePrefetch };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var runtime = require('@module-federation/runtime');
|
|
4
|
+
var sdk = require('@module-federation/sdk');
|
|
5
|
+
|
|
6
|
+
const getScope = ()=>{
|
|
7
|
+
return runtime.getInstance().options.name;
|
|
8
|
+
};
|
|
9
|
+
const getPrefetchId = (id)=>sdk.encodeName(`${id}/${sdk.MFPrefetchCommon.identifier}`);
|
|
10
|
+
const compatGetPrefetchId = (id)=>sdk.encodeName(`${id}/VmokPrefetch`);
|
|
11
|
+
const getSignalFromManifest = (remoteSnapshot)=>{
|
|
12
|
+
if (!remoteSnapshot) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (!('prefetchEntry' in remoteSnapshot) && !('prefetchInterface' in remoteSnapshot)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
exports.compatGetPrefetchId = compatGetPrefetchId;
|
|
25
|
+
exports.getPrefetchId = getPrefetchId;
|
|
26
|
+
exports.getScope = getScope;
|
|
27
|
+
exports.getSignalFromManifest = getSignalFromManifest;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getInstance } from '@module-federation/runtime';
|
|
2
|
+
import { encodeName, MFPrefetchCommon } from '@module-federation/sdk';
|
|
3
|
+
|
|
4
|
+
const getScope = ()=>{
|
|
5
|
+
return getInstance().options.name;
|
|
6
|
+
};
|
|
7
|
+
const getPrefetchId = (id)=>encodeName(`${id}/${MFPrefetchCommon.identifier}`);
|
|
8
|
+
const compatGetPrefetchId = (id)=>encodeName(`${id}/VmokPrefetch`);
|
|
9
|
+
const getSignalFromManifest = (remoteSnapshot)=>{
|
|
10
|
+
if (!remoteSnapshot) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (!('prefetchEntry' in remoteSnapshot) && !('prefetchInterface' in remoteSnapshot)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { getSignalFromManifest as a, getPrefetchId as b, compatGetPrefetchId as c, getScope as g };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const sharedStrategy = ()=>({
|
|
4
|
+
name: 'shared-strategy',
|
|
5
|
+
beforeInit (args) {
|
|
6
|
+
const { userOptions } = args;
|
|
7
|
+
const shared = userOptions.shared;
|
|
8
|
+
if (shared) {
|
|
9
|
+
Object.keys(shared).forEach((sharedKey)=>{
|
|
10
|
+
const sharedConfigs = shared[sharedKey];
|
|
11
|
+
const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
|
|
12
|
+
sharedConfigs
|
|
13
|
+
];
|
|
14
|
+
arraySharedConfigs.forEach((s)=>{
|
|
15
|
+
s.strategy = 'loaded-first';
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
console.warn(`[Module Federation Data Prefetch]: Your shared strategy is set to 'loaded-first', this is a necessary condition for data prefetch`);
|
|
19
|
+
}
|
|
20
|
+
return args;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
module.exports = sharedStrategy;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const sharedStrategy = ()=>({
|
|
2
|
+
name: 'shared-strategy',
|
|
3
|
+
beforeInit (args) {
|
|
4
|
+
const { userOptions } = args;
|
|
5
|
+
const shared = userOptions.shared;
|
|
6
|
+
if (shared) {
|
|
7
|
+
Object.keys(shared).forEach((sharedKey)=>{
|
|
8
|
+
const sharedConfigs = shared[sharedKey];
|
|
9
|
+
const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
|
|
10
|
+
sharedConfigs
|
|
11
|
+
];
|
|
12
|
+
arraySharedConfigs.forEach((s)=>{
|
|
13
|
+
s.strategy = 'loaded-first';
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
console.warn(`[Module Federation Data Prefetch]: Your shared strategy is set to 'loaded-first', this is a necessary condition for data prefetch`);
|
|
17
|
+
}
|
|
18
|
+
return args;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export { sharedStrategy as default };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
2
|
-
|
|
1
|
+
import type { moduleFederationPlugin } from '@module-federation/sdk';
|
|
3
2
|
interface BabelPluginOptions {
|
|
4
3
|
hook_id: string;
|
|
5
4
|
import_pkg: string;
|
|
@@ -35,5 +34,4 @@ declare const _default: (babel: {
|
|
|
35
34
|
}): void;
|
|
36
35
|
};
|
|
37
36
|
};
|
|
38
|
-
|
|
39
|
-
export { _default as default };
|
|
37
|
+
export default _default;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import * as webpack from 'webpack';
|
|
2
|
-
import { WebpackPluginInstance, Compiler } from 'webpack';
|
|
3
1
|
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
4
|
-
|
|
5
|
-
declare const RuntimeGlobals: typeof webpack.RuntimeGlobals;
|
|
6
|
-
declare function getFederationGlobalScope(runtimeGlobals: typeof RuntimeGlobals): string;
|
|
7
|
-
declare class PrefetchPlugin implements WebpackPluginInstance {
|
|
2
|
+
import type { Compiler, WebpackPluginInstance } from 'webpack';
|
|
3
|
+
declare const RuntimeGlobals: typeof import("webpack").RuntimeGlobals;
|
|
4
|
+
export declare function getFederationGlobalScope(runtimeGlobals: typeof RuntimeGlobals): string;
|
|
5
|
+
export declare class PrefetchPlugin implements WebpackPluginInstance {
|
|
8
6
|
options: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
9
7
|
private _reWriteExports;
|
|
10
8
|
constructor(options: moduleFederationPlugin.ModuleFederationPluginOptions);
|
|
@@ -15,5 +13,4 @@ declare class PrefetchPlugin implements WebpackPluginInstance {
|
|
|
15
13
|
static setRemoteIdentifier(): string;
|
|
16
14
|
static removeRemoteIdentifier(): string;
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
export { PrefetchPlugin, getFederationGlobalScope };
|
|
16
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TEMP_DIR = ".mf";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './constant';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ModuleInfo } from '@module-federation/sdk';
|
|
2
|
+
export declare const getScope: () => string;
|
|
3
|
+
export declare const getPrefetchId: (id: string) => string;
|
|
4
|
+
export declare const compatGetPrefetchId: (id: string) => string;
|
|
5
|
+
export declare const getSignalFromManifest: (remoteSnapshot: ModuleInfo) => boolean;
|
|
@@ -1,30 +1,29 @@
|
|
|
1
1
|
import { FederationHost } from '@module-federation/runtime';
|
|
2
2
|
import { ModuleInfo } from '@module-federation/sdk';
|
|
3
3
|
import { Remote } from '@module-federation/runtime/types';
|
|
4
|
-
|
|
5
4
|
declare module '@module-federation/runtime' {
|
|
6
5
|
interface Federation {
|
|
7
6
|
__PREFETCH__: {
|
|
8
7
|
entryLoading: Record<string, undefined | Promise<void>>;
|
|
9
8
|
instance: Map<string, MFDataPrefetch>;
|
|
10
|
-
__PREFETCH_EXPORTS__: Record<string, Promise<Record<string, any>>>;
|
|
9
|
+
__PREFETCH_EXPORTS__: Record<string, () => Promise<Record<string, any>>>;
|
|
11
10
|
};
|
|
12
11
|
}
|
|
13
12
|
}
|
|
14
13
|
type PrefetchExports = Record<string, any>;
|
|
15
|
-
interface DataPrefetchOptions {
|
|
14
|
+
export interface DataPrefetchOptions {
|
|
16
15
|
name: string;
|
|
17
16
|
remote?: Remote;
|
|
18
17
|
origin?: FederationHost;
|
|
19
18
|
remoteSnapshot?: ModuleInfo;
|
|
20
19
|
}
|
|
21
|
-
interface prefetchOptions {
|
|
20
|
+
export interface prefetchOptions {
|
|
22
21
|
id: string;
|
|
23
22
|
functionId?: string;
|
|
24
23
|
cacheStrategy?: () => boolean;
|
|
25
24
|
refetchParams?: any;
|
|
26
25
|
}
|
|
27
|
-
declare class MFDataPrefetch {
|
|
26
|
+
export declare class MFDataPrefetch {
|
|
28
27
|
prefetchMemory: Map<string, Promise<any>>;
|
|
29
28
|
recordOutdate: Record<string, Record<string, boolean>>;
|
|
30
29
|
private _exports;
|
|
@@ -41,5 +40,4 @@ declare class MFDataPrefetch {
|
|
|
41
40
|
markOutdate(markOptions: Omit<prefetchOptions, 'cacheStrategy'>, isOutdate: boolean): void;
|
|
42
41
|
checkOutdate(outdateOptions: prefetchOptions): boolean;
|
|
43
42
|
}
|
|
44
|
-
|
|
45
|
-
export { DataPrefetchOptions as D, MFDataPrefetch as M, prefetchOptions as p };
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type prefetchOptions } from '../prefetch';
|
|
2
|
+
type refetchParams = any;
|
|
3
|
+
type prefetchReturnType<T> = [
|
|
4
|
+
Promise<T>,
|
|
5
|
+
(refetchParams?: refetchParams) => void
|
|
6
|
+
];
|
|
7
|
+
type UsePrefetchOptions = prefetchOptions & {
|
|
8
|
+
deferId?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const usePrefetch: <T>(options: UsePrefetchOptions) => prefetchReturnType<T>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './hooks';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useFirstMounted: () => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/universal/index";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var prefetch$1 = require('./prefetch.cjs.js');
|
|
4
|
+
var runtimeUtils = require('./runtime-utils.cjs.js');
|
|
5
|
+
require('@module-federation/runtime');
|
|
6
|
+
require('@module-federation/sdk');
|
|
7
|
+
|
|
8
|
+
function prefetch(options) {
|
|
9
|
+
const { id, functionId = 'default' } = options;
|
|
10
|
+
const mfScope = runtimeUtils.getScope();
|
|
11
|
+
const prefetchInstance = prefetch$1.MFDataPrefetch.getInstance(mfScope) || new prefetch$1.MFDataPrefetch({
|
|
12
|
+
name: mfScope
|
|
13
|
+
});
|
|
14
|
+
const res = prefetchInstance.getProjectExports();
|
|
15
|
+
if (res instanceof Promise) {
|
|
16
|
+
const promise = res.then(()=>{
|
|
17
|
+
const result = prefetchInstance.prefetch(options);
|
|
18
|
+
prefetchInstance.memorize(id + functionId, result);
|
|
19
|
+
return result;
|
|
20
|
+
});
|
|
21
|
+
return promise;
|
|
22
|
+
} else {
|
|
23
|
+
const result = prefetchInstance.prefetch(options);
|
|
24
|
+
prefetchInstance.memorize(id + functionId, result);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.prefetch = prefetch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/universal/index";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { M as MFDataPrefetch } from './prefetch.esm.js';
|
|
2
|
+
import { g as getScope } from './runtime-utils.esm.js';
|
|
3
|
+
import '@module-federation/runtime';
|
|
4
|
+
import '@module-federation/sdk';
|
|
5
|
+
|
|
6
|
+
function prefetch(options) {
|
|
7
|
+
const { id, functionId = 'default' } = options;
|
|
8
|
+
const mfScope = getScope();
|
|
9
|
+
const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
|
|
10
|
+
name: mfScope
|
|
11
|
+
});
|
|
12
|
+
const res = prefetchInstance.getProjectExports();
|
|
13
|
+
if (res instanceof Promise) {
|
|
14
|
+
const promise = res.then(()=>{
|
|
15
|
+
const result = prefetchInstance.prefetch(options);
|
|
16
|
+
prefetchInstance.memorize(id + functionId, result);
|
|
17
|
+
return result;
|
|
18
|
+
});
|
|
19
|
+
return promise;
|
|
20
|
+
} else {
|
|
21
|
+
const result = prefetchInstance.prefetch(options);
|
|
22
|
+
prefetchInstance.memorize(id + functionId, result);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { prefetch };
|
package/jest.config.js
CHANGED
|
@@ -24,6 +24,6 @@ module.exports = {
|
|
|
24
24
|
'^.+\\.(t|j)sx?$': ['@swc/jest', { swcrc: false }],
|
|
25
25
|
},
|
|
26
26
|
rootDir: __dirname,
|
|
27
|
-
testMatch: ['<rootDir
|
|
28
|
-
testPathIgnorePatterns: ['/node_modules/'
|
|
27
|
+
testMatch: ['<rootDir>/__tests__/**/*.spec.[jt]s?(x)'],
|
|
28
|
+
testPathIgnorePatterns: ['/node_modules/'],
|
|
29
29
|
};
|