@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,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/data-prefetch",
|
|
3
|
+
"description": "Module Federation Data Prefetch",
|
|
4
|
+
"version": "0.6.8",
|
|
5
|
+
"author": "nieyan <nyqykk@foxmail.com>",
|
|
6
|
+
"homepage": "https://github.com/module-federation/core",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "cross-env WATCH=true tsup",
|
|
10
|
+
"build": "rm -rf dist && tsup",
|
|
11
|
+
"test": "jest"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.esm.js",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"types": "./dist/index.cjs.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./react": {
|
|
23
|
+
"import": "./dist/react.esm.js",
|
|
24
|
+
"require": "./dist/react.cjs.js",
|
|
25
|
+
"types": "./dist/react.cjs.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"./cli": {
|
|
28
|
+
"import": "./dist/cli.esm.js",
|
|
29
|
+
"require": "./dist/cli.cjs.js",
|
|
30
|
+
"types": "./dist/cli.cjs.d.ts"
|
|
31
|
+
},
|
|
32
|
+
"./babel-plugin": {
|
|
33
|
+
"import": "./dist/babel.esm.js",
|
|
34
|
+
"require": "./dist/babel.cjs.js",
|
|
35
|
+
"types": "./dist/babel.cjs.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./universal": {
|
|
38
|
+
"import": "./dist/universal.esm.js",
|
|
39
|
+
"require": "./dist/universal.cjs.js",
|
|
40
|
+
"types": "./dist/universal.cjs.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./shared": {
|
|
43
|
+
"import": "./dist/shared.esm.js",
|
|
44
|
+
"require": "./dist/shared.cjs.js",
|
|
45
|
+
"types": "./dist/shared/.cjs.d.ts"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
".": [
|
|
51
|
+
"./dist/index.cjs.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"react": [
|
|
54
|
+
"./dist/react.cjs.d.ts"
|
|
55
|
+
],
|
|
56
|
+
"cli": [
|
|
57
|
+
"./dist/cli.cjs.d.ts"
|
|
58
|
+
],
|
|
59
|
+
"universal": [
|
|
60
|
+
"./dist/universal.cjs.d.ts"
|
|
61
|
+
],
|
|
62
|
+
"shared": [
|
|
63
|
+
"./dist/shared.cjs.d.ts"
|
|
64
|
+
],
|
|
65
|
+
"babel-plugin": [
|
|
66
|
+
"./dist/babel.cjs.d.ts"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"main": "./index.cjs.js",
|
|
71
|
+
"module": "./index.esm.js",
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"react": ">=16.9.0",
|
|
74
|
+
"react-dom": ">=16.9.0"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
78
|
+
"@types/fs-extra": "9.0.6",
|
|
79
|
+
"@types/jest": "^29.5.11",
|
|
80
|
+
"@types/node": "^17.0.45",
|
|
81
|
+
"@types/react": "~18.0.38",
|
|
82
|
+
"esbuild-plugin-replace": "^1.4.0",
|
|
83
|
+
"jest": "^29.7.0",
|
|
84
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
85
|
+
"minimist": "^1.2.8",
|
|
86
|
+
"react": "^18.3.1",
|
|
87
|
+
"react-dom": "^18.3.1",
|
|
88
|
+
"react-router": "^6.21.3",
|
|
89
|
+
"react-test-renderer": "^18.2.0",
|
|
90
|
+
"ts-jest": "29.0.1",
|
|
91
|
+
"tsup": "6.2.0",
|
|
92
|
+
"webpack": "5.75.0"
|
|
93
|
+
},
|
|
94
|
+
"dependencies": {
|
|
95
|
+
"@module-federation/runtime": "workspace:*",
|
|
96
|
+
"@module-federation/sdk": "workspace:*",
|
|
97
|
+
"fs-extra": "9.1.0"
|
|
98
|
+
},
|
|
99
|
+
"types": "./index.esm.d.ts"
|
|
100
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var sdk = require('@module-federation/sdk');
|
|
6
|
+
var runtimeUtils = require('./runtime-utils.cjs.js');
|
|
7
|
+
var prefetch = require('./prefetch.cjs.js');
|
|
8
|
+
var index = require('./index.cjs2.js');
|
|
9
|
+
require('@module-federation/runtime');
|
|
10
|
+
|
|
11
|
+
const loadingArray = [];
|
|
12
|
+
const strategy = 'loaded-first';
|
|
13
|
+
let sharedFlag = strategy;
|
|
14
|
+
// eslint-disable-next-line max-lines-per-function
|
|
15
|
+
const prefetchPlugin = ()=>({
|
|
16
|
+
name: 'data-prefetch-runtime-plugin',
|
|
17
|
+
initContainer (options) {
|
|
18
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
19
|
+
const snapshot = remoteSnapshot;
|
|
20
|
+
const { name } = remoteInfo;
|
|
21
|
+
const prefetchOptions = {
|
|
22
|
+
name,
|
|
23
|
+
remote: remoteInfo,
|
|
24
|
+
origin,
|
|
25
|
+
remoteSnapshot: snapshot
|
|
26
|
+
};
|
|
27
|
+
const signal = runtimeUtils.getSignalFromManifest(snapshot);
|
|
28
|
+
if (!signal) {
|
|
29
|
+
return options;
|
|
30
|
+
}
|
|
31
|
+
if (sharedFlag !== strategy) {
|
|
32
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
33
|
+
}
|
|
34
|
+
const instance = prefetch.MFDataPrefetch.getInstance(name) || new prefetch.MFDataPrefetch(prefetchOptions);
|
|
35
|
+
let prefetchUrl;
|
|
36
|
+
// @ts-expect-error
|
|
37
|
+
if (snapshot.prefetchEntry) {
|
|
38
|
+
// @ts-expect-error
|
|
39
|
+
prefetchUrl = sdk.getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
40
|
+
}
|
|
41
|
+
const exist = loadingArray.find((loading)=>loading.id === id);
|
|
42
|
+
if (exist) {
|
|
43
|
+
return options;
|
|
44
|
+
}
|
|
45
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
46
|
+
const projectExports = instance.getProjectExports();
|
|
47
|
+
if (projectExports instanceof Promise) {
|
|
48
|
+
await projectExports;
|
|
49
|
+
}
|
|
50
|
+
return Promise.resolve().then(()=>{
|
|
51
|
+
const exports = instance.getExposeExports(id);
|
|
52
|
+
index.logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
|
|
53
|
+
const result = Object.keys(exports).map((k)=>{
|
|
54
|
+
const value = instance.prefetch({
|
|
55
|
+
id,
|
|
56
|
+
functionId: k
|
|
57
|
+
});
|
|
58
|
+
const functionId = k;
|
|
59
|
+
return {
|
|
60
|
+
value,
|
|
61
|
+
functionId
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
return result;
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
loadingArray.push({
|
|
68
|
+
id,
|
|
69
|
+
promise
|
|
70
|
+
});
|
|
71
|
+
return options;
|
|
72
|
+
},
|
|
73
|
+
afterResolve (options) {
|
|
74
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
75
|
+
const snapshot = remoteSnapshot;
|
|
76
|
+
const { name } = remoteInfo;
|
|
77
|
+
const prefetchOptions = {
|
|
78
|
+
name,
|
|
79
|
+
remote: remoteInfo,
|
|
80
|
+
origin,
|
|
81
|
+
remoteSnapshot: snapshot
|
|
82
|
+
};
|
|
83
|
+
const signal = runtimeUtils.getSignalFromManifest(snapshot);
|
|
84
|
+
if (!signal) {
|
|
85
|
+
return options;
|
|
86
|
+
}
|
|
87
|
+
const inited = loadingArray.some((info)=>info.id === id);
|
|
88
|
+
if (!inited) {
|
|
89
|
+
return options;
|
|
90
|
+
}
|
|
91
|
+
if (sharedFlag !== strategy) {
|
|
92
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
93
|
+
}
|
|
94
|
+
const instance = prefetch.MFDataPrefetch.getInstance(name) || new prefetch.MFDataPrefetch(prefetchOptions);
|
|
95
|
+
let prefetchUrl;
|
|
96
|
+
// @ts-expect-error
|
|
97
|
+
if (snapshot.prefetchEntry) {
|
|
98
|
+
// @ts-expect-error
|
|
99
|
+
prefetchUrl = sdk.getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
100
|
+
}
|
|
101
|
+
const index$1 = loadingArray.findIndex((loading)=>loading.id === id);
|
|
102
|
+
// clear cache
|
|
103
|
+
if (index$1 !== -1) {
|
|
104
|
+
loadingArray.splice(index$1, 1);
|
|
105
|
+
}
|
|
106
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
107
|
+
const projectExports = instance.getProjectExports();
|
|
108
|
+
if (projectExports instanceof Promise) {
|
|
109
|
+
await projectExports;
|
|
110
|
+
}
|
|
111
|
+
return Promise.resolve().then(()=>{
|
|
112
|
+
const exports = instance.getExposeExports(id);
|
|
113
|
+
index.logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
|
|
114
|
+
const result = Object.keys(exports).map((k)=>{
|
|
115
|
+
const value = instance.prefetch({
|
|
116
|
+
id,
|
|
117
|
+
functionId: k
|
|
118
|
+
});
|
|
119
|
+
const functionId = k;
|
|
120
|
+
return {
|
|
121
|
+
value,
|
|
122
|
+
functionId
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
return result;
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
loadingArray.push({
|
|
129
|
+
id,
|
|
130
|
+
promise
|
|
131
|
+
});
|
|
132
|
+
return options;
|
|
133
|
+
},
|
|
134
|
+
async onLoad (options) {
|
|
135
|
+
var _loadingArray_find;
|
|
136
|
+
const { remote, id } = options;
|
|
137
|
+
const { name } = remote;
|
|
138
|
+
const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) == null ? void 0 : _loadingArray_find.promise;
|
|
139
|
+
if (promise) {
|
|
140
|
+
const prefetch$1 = await promise;
|
|
141
|
+
const prefetchValue = prefetch$1.map((result)=>result.value);
|
|
142
|
+
await Promise.all(prefetchValue);
|
|
143
|
+
const instance = prefetch.MFDataPrefetch.getInstance(name);
|
|
144
|
+
prefetch$1.forEach((result)=>{
|
|
145
|
+
const { value, functionId } = result;
|
|
146
|
+
instance.memorize(id + functionId, value);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return options;
|
|
150
|
+
},
|
|
151
|
+
beforeLoadShare (options) {
|
|
152
|
+
const shareInfo = options.shareInfo;
|
|
153
|
+
sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
|
|
154
|
+
return options;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
exports.default = prefetchPlugin;
|
|
159
|
+
exports.prefetchPlugin = prefetchPlugin;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { getResourceUrl } from '@module-federation/sdk';
|
|
2
|
+
import { a as getSignalFromManifest } from './runtime-utils.esm.js';
|
|
3
|
+
import { M as MFDataPrefetch } from './prefetch.esm.js';
|
|
4
|
+
import { l as logger } from './index.esm2.js';
|
|
5
|
+
import '@module-federation/runtime';
|
|
6
|
+
|
|
7
|
+
const loadingArray = [];
|
|
8
|
+
const strategy = 'loaded-first';
|
|
9
|
+
let sharedFlag = strategy;
|
|
10
|
+
// eslint-disable-next-line max-lines-per-function
|
|
11
|
+
const prefetchPlugin = ()=>({
|
|
12
|
+
name: 'data-prefetch-runtime-plugin',
|
|
13
|
+
initContainer (options) {
|
|
14
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
15
|
+
const snapshot = remoteSnapshot;
|
|
16
|
+
const { name } = remoteInfo;
|
|
17
|
+
const prefetchOptions = {
|
|
18
|
+
name,
|
|
19
|
+
remote: remoteInfo,
|
|
20
|
+
origin,
|
|
21
|
+
remoteSnapshot: snapshot
|
|
22
|
+
};
|
|
23
|
+
const signal = getSignalFromManifest(snapshot);
|
|
24
|
+
if (!signal) {
|
|
25
|
+
return options;
|
|
26
|
+
}
|
|
27
|
+
if (sharedFlag !== strategy) {
|
|
28
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
29
|
+
}
|
|
30
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
31
|
+
let prefetchUrl;
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
if (snapshot.prefetchEntry) {
|
|
34
|
+
// @ts-expect-error
|
|
35
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
36
|
+
}
|
|
37
|
+
const exist = loadingArray.find((loading)=>loading.id === id);
|
|
38
|
+
if (exist) {
|
|
39
|
+
return options;
|
|
40
|
+
}
|
|
41
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
42
|
+
const projectExports = instance.getProjectExports();
|
|
43
|
+
if (projectExports instanceof Promise) {
|
|
44
|
+
await projectExports;
|
|
45
|
+
}
|
|
46
|
+
return Promise.resolve().then(()=>{
|
|
47
|
+
const exports = instance.getExposeExports(id);
|
|
48
|
+
logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
|
|
49
|
+
const result = Object.keys(exports).map((k)=>{
|
|
50
|
+
const value = instance.prefetch({
|
|
51
|
+
id,
|
|
52
|
+
functionId: k
|
|
53
|
+
});
|
|
54
|
+
const functionId = k;
|
|
55
|
+
return {
|
|
56
|
+
value,
|
|
57
|
+
functionId
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
return result;
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
loadingArray.push({
|
|
64
|
+
id,
|
|
65
|
+
promise
|
|
66
|
+
});
|
|
67
|
+
return options;
|
|
68
|
+
},
|
|
69
|
+
afterResolve (options) {
|
|
70
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
71
|
+
const snapshot = remoteSnapshot;
|
|
72
|
+
const { name } = remoteInfo;
|
|
73
|
+
const prefetchOptions = {
|
|
74
|
+
name,
|
|
75
|
+
remote: remoteInfo,
|
|
76
|
+
origin,
|
|
77
|
+
remoteSnapshot: snapshot
|
|
78
|
+
};
|
|
79
|
+
const signal = getSignalFromManifest(snapshot);
|
|
80
|
+
if (!signal) {
|
|
81
|
+
return options;
|
|
82
|
+
}
|
|
83
|
+
const inited = loadingArray.some((info)=>info.id === id);
|
|
84
|
+
if (!inited) {
|
|
85
|
+
return options;
|
|
86
|
+
}
|
|
87
|
+
if (sharedFlag !== strategy) {
|
|
88
|
+
throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
|
|
89
|
+
}
|
|
90
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
91
|
+
let prefetchUrl;
|
|
92
|
+
// @ts-expect-error
|
|
93
|
+
if (snapshot.prefetchEntry) {
|
|
94
|
+
// @ts-expect-error
|
|
95
|
+
prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
|
|
96
|
+
}
|
|
97
|
+
const index = loadingArray.findIndex((loading)=>loading.id === id);
|
|
98
|
+
// clear cache
|
|
99
|
+
if (index !== -1) {
|
|
100
|
+
loadingArray.splice(index, 1);
|
|
101
|
+
}
|
|
102
|
+
const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
|
|
103
|
+
const projectExports = instance.getProjectExports();
|
|
104
|
+
if (projectExports instanceof Promise) {
|
|
105
|
+
await projectExports;
|
|
106
|
+
}
|
|
107
|
+
return Promise.resolve().then(()=>{
|
|
108
|
+
const exports = instance.getExposeExports(id);
|
|
109
|
+
logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
|
|
110
|
+
const result = Object.keys(exports).map((k)=>{
|
|
111
|
+
const value = instance.prefetch({
|
|
112
|
+
id,
|
|
113
|
+
functionId: k
|
|
114
|
+
});
|
|
115
|
+
const functionId = k;
|
|
116
|
+
return {
|
|
117
|
+
value,
|
|
118
|
+
functionId
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
return result;
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
loadingArray.push({
|
|
125
|
+
id,
|
|
126
|
+
promise
|
|
127
|
+
});
|
|
128
|
+
return options;
|
|
129
|
+
},
|
|
130
|
+
async onLoad (options) {
|
|
131
|
+
var _loadingArray_find;
|
|
132
|
+
const { remote, id } = options;
|
|
133
|
+
const { name } = remote;
|
|
134
|
+
const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) == null ? void 0 : _loadingArray_find.promise;
|
|
135
|
+
if (promise) {
|
|
136
|
+
const prefetch = await promise;
|
|
137
|
+
const prefetchValue = prefetch.map((result)=>result.value);
|
|
138
|
+
await Promise.all(prefetchValue);
|
|
139
|
+
const instance = MFDataPrefetch.getInstance(name);
|
|
140
|
+
prefetch.forEach((result)=>{
|
|
141
|
+
const { value, functionId } = result;
|
|
142
|
+
instance.memorize(id + functionId, value);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return options;
|
|
146
|
+
},
|
|
147
|
+
beforeLoadShare (options) {
|
|
148
|
+
const shareInfo = options.shareInfo;
|
|
149
|
+
sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
|
|
150
|
+
return options;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
export { prefetchPlugin as default, prefetchPlugin };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _extends() {
|
|
4
|
+
_extends = Object.assign || function assign(target) {
|
|
5
|
+
for(var i = 1; i < arguments.length; i++){
|
|
6
|
+
var source = arguments[i];
|
|
7
|
+
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
8
|
+
}
|
|
9
|
+
return target;
|
|
10
|
+
};
|
|
11
|
+
return _extends.apply(this, arguments);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports._extends = _extends;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function _extends() {
|
|
2
|
+
_extends = Object.assign || function assign(target) {
|
|
3
|
+
for(var i = 1; i < arguments.length; i++){
|
|
4
|
+
var source = arguments[i];
|
|
5
|
+
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
6
|
+
}
|
|
7
|
+
return target;
|
|
8
|
+
};
|
|
9
|
+
return _extends.apply(this, arguments);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { _extends as _ };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var runtime = require('@module-federation/runtime');
|
|
4
|
+
var sdk = require('@module-federation/sdk');
|
|
5
|
+
var runtimeUtils = require('./runtime-utils.cjs.js');
|
|
6
|
+
|
|
7
|
+
var // @ts-ignore init global variable for test
|
|
8
|
+
_globalThis, _globalThis___FEDERATION__;
|
|
9
|
+
var ___FEDERATION__;
|
|
10
|
+
(___FEDERATION__ = (_globalThis = globalThis).__FEDERATION__) != null ? ___FEDERATION__ : _globalThis.__FEDERATION__ = {};
|
|
11
|
+
var ___PREFETCH__;
|
|
12
|
+
(___PREFETCH__ = (_globalThis___FEDERATION__ = globalThis.__FEDERATION__).__PREFETCH__) != null ? ___PREFETCH__ : _globalThis___FEDERATION__.__PREFETCH__ = {
|
|
13
|
+
entryLoading: {},
|
|
14
|
+
instance: new Map(),
|
|
15
|
+
__PREFETCH_EXPORTS__: {}
|
|
16
|
+
};
|
|
17
|
+
class MFDataPrefetch {
|
|
18
|
+
get global() {
|
|
19
|
+
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
20
|
+
}
|
|
21
|
+
static getInstance(id) {
|
|
22
|
+
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
23
|
+
}
|
|
24
|
+
async loadEntry(entry) {
|
|
25
|
+
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
26
|
+
if (entry) {
|
|
27
|
+
const { buildVersion, globalName } = remoteSnapshot;
|
|
28
|
+
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
29
|
+
if (!this.global.entryLoading[uniqueKey]) {
|
|
30
|
+
this.global.entryLoading[uniqueKey] = sdk.loadScript(entry, {});
|
|
31
|
+
}
|
|
32
|
+
return this.global.entryLoading[uniqueKey];
|
|
33
|
+
} else {
|
|
34
|
+
const remoteInfo = runtime.getRemoteInfo(remote);
|
|
35
|
+
const module = origin.moduleCache.get(remoteInfo.name);
|
|
36
|
+
return runtime.getRemoteEntry({
|
|
37
|
+
origin: origin,
|
|
38
|
+
remoteInfo,
|
|
39
|
+
remoteEntryExports: module ? module.remoteEntryExports : undefined
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
getProjectExports() {
|
|
44
|
+
var _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__;
|
|
45
|
+
if (Object.keys(this._exports).length > 0) {
|
|
46
|
+
return this._exports;
|
|
47
|
+
}
|
|
48
|
+
const { name } = this._options;
|
|
49
|
+
const exportsPromiseFn = (_globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__ = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _globalThis___FEDERATION_____PREFETCH_____PREFETCH_EXPORTS__[name];
|
|
50
|
+
const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : Promise.resolve({});
|
|
51
|
+
const resolve = exportsPromise.then((exports = {})=>{
|
|
52
|
+
// Match prefetch based on the function name suffix so that other capabilities can be expanded later.
|
|
53
|
+
// Not all functions should be directly identified as prefetch functions
|
|
54
|
+
const memory = {};
|
|
55
|
+
Object.keys(exports).forEach((key)=>{
|
|
56
|
+
memory[key] = {};
|
|
57
|
+
const exportVal = exports[key];
|
|
58
|
+
Object.keys(exportVal).reduce((memo, current)=>{
|
|
59
|
+
if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
|
|
60
|
+
memo[current] = exportVal[current];
|
|
61
|
+
}
|
|
62
|
+
return memo;
|
|
63
|
+
}, memory[key]);
|
|
64
|
+
});
|
|
65
|
+
this.memorizeExports(memory);
|
|
66
|
+
});
|
|
67
|
+
return resolve;
|
|
68
|
+
}
|
|
69
|
+
memorizeExports(exports) {
|
|
70
|
+
this._exports = exports;
|
|
71
|
+
}
|
|
72
|
+
getExposeExports(id) {
|
|
73
|
+
const prefetchId = runtimeUtils.getPrefetchId(id);
|
|
74
|
+
const compatId = runtimeUtils.compatGetPrefetchId(id);
|
|
75
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
76
|
+
return prefetchExports || {};
|
|
77
|
+
}
|
|
78
|
+
prefetch(prefetchOptions) {
|
|
79
|
+
const { id, functionId = 'default', refetchParams } = prefetchOptions;
|
|
80
|
+
let prefetchResult;
|
|
81
|
+
const prefetchId = runtimeUtils.getPrefetchId(id);
|
|
82
|
+
const compatId = runtimeUtils.compatGetPrefetchId(id);
|
|
83
|
+
const memorizeId = id + functionId;
|
|
84
|
+
const memory = this.prefetchMemory.get(memorizeId);
|
|
85
|
+
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
86
|
+
return memory;
|
|
87
|
+
}
|
|
88
|
+
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
89
|
+
if (!prefetchExports) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const executePrefetch = prefetchExports[functionId];
|
|
93
|
+
if (typeof executePrefetch === 'function') {
|
|
94
|
+
if (refetchParams) {
|
|
95
|
+
prefetchResult = executePrefetch(refetchParams);
|
|
96
|
+
} else {
|
|
97
|
+
prefetchResult = executePrefetch();
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
|
|
101
|
+
}
|
|
102
|
+
this.memorize(memorizeId, prefetchResult);
|
|
103
|
+
return prefetchResult;
|
|
104
|
+
}
|
|
105
|
+
memorize(id, value) {
|
|
106
|
+
this.prefetchMemory.set(id, value);
|
|
107
|
+
}
|
|
108
|
+
markOutdate(markOptions, isOutdate) {
|
|
109
|
+
const { id, functionId = 'default' } = markOptions;
|
|
110
|
+
if (!this.recordOutdate[id]) {
|
|
111
|
+
this.recordOutdate[id] = {};
|
|
112
|
+
}
|
|
113
|
+
this.recordOutdate[id][functionId] = isOutdate;
|
|
114
|
+
}
|
|
115
|
+
checkOutdate(outdateOptions) {
|
|
116
|
+
const { id, functionId = 'default', cacheStrategy } = outdateOptions;
|
|
117
|
+
if (typeof cacheStrategy === 'function') {
|
|
118
|
+
return cacheStrategy();
|
|
119
|
+
}
|
|
120
|
+
if (!this.recordOutdate[id]) {
|
|
121
|
+
this.recordOutdate[id] = {};
|
|
122
|
+
}
|
|
123
|
+
if (this.recordOutdate[id][functionId]) {
|
|
124
|
+
this.markOutdate({
|
|
125
|
+
id,
|
|
126
|
+
functionId
|
|
127
|
+
}, false);
|
|
128
|
+
return true;
|
|
129
|
+
} else {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
constructor(options){
|
|
134
|
+
this.prefetchMemory = new Map();
|
|
135
|
+
this.recordOutdate = {};
|
|
136
|
+
this._exports = {};
|
|
137
|
+
this._options = options;
|
|
138
|
+
this.global.instance.set(options.name, this);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
exports.MFDataPrefetch = MFDataPrefetch;
|