@module-federation/data-prefetch 0.0.0-next-20240617071542
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/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/cli/babel.d.ts +39 -0
- package/dist/cli/babel.js +84 -0
- package/dist/cli/index.d.ts +11 -0
- package/dist/cli/index.js +165 -0
- package/dist/esm/chunk-BN3GGCE5.js +136 -0
- package/dist/esm/chunk-EWCGK4XA.js +32 -0
- package/dist/esm/chunk-JEPJP5O3.js +160 -0
- package/dist/esm/chunk-QHQN3BPZ.js +32 -0
- package/dist/esm/chunk-TTJJJ2WZ.js +7 -0
- package/dist/esm/cli/babel.js +55 -0
- package/dist/esm/cli/index.js +137 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/plugin.js +11 -0
- package/dist/esm/react/index.js +93 -0
- package/dist/esm/universal/index.js +8 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +324 -0
- package/dist/plugin.d.ts +5 -0
- package/dist/plugin.js +320 -0
- package/dist/prefetch-4e9646e4.d.ts +45 -0
- package/dist/react/index.d.ts +16 -0
- package/dist/react/index.js +295 -0
- package/dist/universal/index.d.ts +8 -0
- package/dist/universal/index.js +213 -0
- package/jest.config.js +29 -0
- package/package.json +88 -0
- package/project.json +21 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/cli/babel.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
var attribute = "id";
|
|
4
|
+
var hookId = "usePrefetch";
|
|
5
|
+
var importPackage = "@module-federation/data-prefetch/react";
|
|
6
|
+
var babel_default = (babel, options) => {
|
|
7
|
+
const t = babel.types;
|
|
8
|
+
let shouldHandle = false;
|
|
9
|
+
let scope = "";
|
|
10
|
+
const { mf_name: name, exposes } = options;
|
|
11
|
+
if (!exposes) {
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
const exposesKey = Object.keys(exposes);
|
|
15
|
+
const processedExposes = exposesKey.map((expose) => ({
|
|
16
|
+
key: expose.replace(".", ""),
|
|
17
|
+
value: path.resolve(
|
|
18
|
+
typeof exposes[expose] === "string" ? exposes[expose] : exposes[expose].import
|
|
19
|
+
)
|
|
20
|
+
}));
|
|
21
|
+
return {
|
|
22
|
+
visitor: {
|
|
23
|
+
ImportDeclaration(nodePath, state) {
|
|
24
|
+
const source = nodePath.node.source.value;
|
|
25
|
+
const { specifiers } = nodePath.node;
|
|
26
|
+
const { filename } = state.file.opts;
|
|
27
|
+
if (source === importPackage) {
|
|
28
|
+
shouldHandle = specifiers.some(
|
|
29
|
+
(specifier) => specifier.imported && specifier.imported.name === hookId && processedExposes.find(
|
|
30
|
+
(expose) => expose.value === filename && (scope = expose.key)
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
CallExpression(nodePath) {
|
|
36
|
+
if (shouldHandle && t.isIdentifier(nodePath.node.callee, { name: hookId }) && nodePath.node.arguments.length > 0) {
|
|
37
|
+
const objectExpression = nodePath.node.arguments[0];
|
|
38
|
+
if (objectExpression && t.isObjectExpression(objectExpression) && !objectExpression.properties.find(
|
|
39
|
+
(p) => p.key.name === attribute
|
|
40
|
+
)) {
|
|
41
|
+
objectExpression.properties.push(
|
|
42
|
+
t.objectProperty(
|
|
43
|
+
t.identifier(attribute),
|
|
44
|
+
t.stringLiteral(name + scope)
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export {
|
|
54
|
+
babel_default as default
|
|
55
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getPrefetchId
|
|
3
|
+
} from "../chunk-EWCGK4XA.js";
|
|
4
|
+
|
|
5
|
+
// src/cli/index.ts
|
|
6
|
+
import path2 from "path";
|
|
7
|
+
import fs2 from "fs-extra";
|
|
8
|
+
import {
|
|
9
|
+
encodeName,
|
|
10
|
+
MFPrefetchCommon
|
|
11
|
+
} from "@module-federation/sdk";
|
|
12
|
+
|
|
13
|
+
// src/common/constant.ts
|
|
14
|
+
var TEMP_DIR = ".mf";
|
|
15
|
+
|
|
16
|
+
// src/common/node-utils.ts
|
|
17
|
+
import path from "path";
|
|
18
|
+
import fs from "fs-extra";
|
|
19
|
+
var fileExistsWithCaseSync = (filepath) => {
|
|
20
|
+
const dir = path.dirname(filepath);
|
|
21
|
+
if (filepath === "/" || filepath === ".") {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
const filenames = fs.readdirSync(dir);
|
|
25
|
+
if (filenames.indexOf(path.basename(filepath)) === -1) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return fileExistsWithCaseSync(dir);
|
|
29
|
+
};
|
|
30
|
+
var fixPrefetchPath = (exposePath) => {
|
|
31
|
+
const pathExt = [".js", ".ts"];
|
|
32
|
+
const extReg = /\.(ts|js|tsx|jsx)$/;
|
|
33
|
+
if (extReg.test(exposePath)) {
|
|
34
|
+
return pathExt.map((ext) => exposePath.replace(extReg, `.prefetch${ext}`));
|
|
35
|
+
} else {
|
|
36
|
+
return pathExt.map((ext) => exposePath + `.prefetch${ext}`);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/cli/index.ts
|
|
41
|
+
var addTemplate = (name) => `
|
|
42
|
+
export default function () {
|
|
43
|
+
let hasInit = false;
|
|
44
|
+
return {
|
|
45
|
+
name: 'data-prefetch-init-plugin',
|
|
46
|
+
beforeInit(args) {
|
|
47
|
+
if (!hasInit) {
|
|
48
|
+
hasInit = true;
|
|
49
|
+
globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};
|
|
50
|
+
globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] || {
|
|
51
|
+
entryLoading: {},
|
|
52
|
+
instance: new Map(),
|
|
53
|
+
__PREFETCH_EXPORTS__: {},
|
|
54
|
+
};
|
|
55
|
+
globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] || {};
|
|
56
|
+
globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}']['${name}'] = import('./bootstrap');
|
|
57
|
+
}
|
|
58
|
+
return args;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
var PrefetchPlugin = class {
|
|
64
|
+
constructor(options) {
|
|
65
|
+
this.options = options;
|
|
66
|
+
this._reWriteExports = "";
|
|
67
|
+
}
|
|
68
|
+
apply(compiler) {
|
|
69
|
+
const { name, exposes } = this.options;
|
|
70
|
+
if (!exposes) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (!compiler.options.context) {
|
|
74
|
+
throw new Error("compiler.options.context is not defined");
|
|
75
|
+
}
|
|
76
|
+
const prefetchs = [];
|
|
77
|
+
const exposeAlias = Object.keys(exposes);
|
|
78
|
+
exposeAlias.forEach((alias) => {
|
|
79
|
+
let exposePath;
|
|
80
|
+
const exposeValue = exposes[alias];
|
|
81
|
+
if (typeof exposeValue === "string") {
|
|
82
|
+
exposePath = exposeValue;
|
|
83
|
+
} else {
|
|
84
|
+
exposePath = exposeValue.import;
|
|
85
|
+
}
|
|
86
|
+
const targetPaths = fixPrefetchPath(exposePath);
|
|
87
|
+
for (const pathItem of targetPaths) {
|
|
88
|
+
const absolutePath = path2.resolve(compiler.options.context, pathItem);
|
|
89
|
+
if (fileExistsWithCaseSync(absolutePath)) {
|
|
90
|
+
prefetchs.push(pathItem);
|
|
91
|
+
const absoluteAlias = alias.replace(".", "");
|
|
92
|
+
this._reWriteExports += `export * as ${getPrefetchId(
|
|
93
|
+
`${name}${absoluteAlias}`
|
|
94
|
+
)} from '${absolutePath}';
|
|
95
|
+
`;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const { runtimePlugins } = this.options;
|
|
101
|
+
if (!Array.isArray(runtimePlugins)) {
|
|
102
|
+
this.options.runtimePlugins = [];
|
|
103
|
+
}
|
|
104
|
+
this.options.runtimePlugins.push(
|
|
105
|
+
path2.resolve(__dirname, "../esm/plugin.js")
|
|
106
|
+
);
|
|
107
|
+
if (!this._reWriteExports) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const encodedName = encodeName(name);
|
|
111
|
+
const asyncEntryPath = path2.resolve(
|
|
112
|
+
compiler.options.context,
|
|
113
|
+
`node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`
|
|
114
|
+
);
|
|
115
|
+
const tempDirRealPath = path2.resolve(
|
|
116
|
+
compiler.options.context,
|
|
117
|
+
"node_modules",
|
|
118
|
+
TEMP_DIR
|
|
119
|
+
);
|
|
120
|
+
if (!fs2.existsSync(tempDirRealPath)) {
|
|
121
|
+
fs2.mkdirSync(tempDirRealPath);
|
|
122
|
+
}
|
|
123
|
+
if (!fs2.existsSync(`${tempDirRealPath}/${encodedName}`)) {
|
|
124
|
+
fs2.mkdirSync(`${tempDirRealPath}/${encodedName}`);
|
|
125
|
+
}
|
|
126
|
+
fs2.writeFileSync(asyncEntryPath, this._reWriteExports);
|
|
127
|
+
const prefetchEntry = path2.resolve(
|
|
128
|
+
compiler.options.context,
|
|
129
|
+
`node_modules/${TEMP_DIR}/${encodedName}/${MFPrefetchCommon.fileName}`
|
|
130
|
+
);
|
|
131
|
+
fs2.writeFileSync(prefetchEntry, addTemplate(name));
|
|
132
|
+
this.options.runtimePlugins.push(prefetchEntry);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
export {
|
|
136
|
+
PrefetchPlugin
|
|
137
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
prefetch
|
|
3
|
+
} from "../chunk-QHQN3BPZ.js";
|
|
4
|
+
import {
|
|
5
|
+
logger_default
|
|
6
|
+
} from "../chunk-TTJJJ2WZ.js";
|
|
7
|
+
import {
|
|
8
|
+
MFDataPrefetch
|
|
9
|
+
} from "../chunk-JEPJP5O3.js";
|
|
10
|
+
import {
|
|
11
|
+
getScope
|
|
12
|
+
} from "../chunk-EWCGK4XA.js";
|
|
13
|
+
|
|
14
|
+
// src/react/hooks.ts
|
|
15
|
+
import { useEffect as useEffect2, useState } from "react";
|
|
16
|
+
|
|
17
|
+
// src/react/utils.ts
|
|
18
|
+
import { useEffect, useRef } from "react";
|
|
19
|
+
var useFirstMounted = () => {
|
|
20
|
+
const ref = useRef(true);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
ref.current = false;
|
|
23
|
+
}, []);
|
|
24
|
+
return ref.current;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/react/hooks.ts
|
|
28
|
+
var usePrefetch = (options) => {
|
|
29
|
+
const isFirstMounted = useFirstMounted();
|
|
30
|
+
if (isFirstMounted) {
|
|
31
|
+
const startTiming = performance.now();
|
|
32
|
+
logger_default.info(
|
|
33
|
+
`2. Start Get Prefetch Data: ${options.id} - ${options.functionId} - ${startTiming}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const { id, functionId, deferId } = options;
|
|
37
|
+
const prefetchInfo = {
|
|
38
|
+
id,
|
|
39
|
+
functionId
|
|
40
|
+
};
|
|
41
|
+
const mfScope = getScope(id);
|
|
42
|
+
let state;
|
|
43
|
+
const prefetchResult = prefetch(options);
|
|
44
|
+
if (deferId) {
|
|
45
|
+
if (prefetchResult instanceof Promise) {
|
|
46
|
+
state = prefetchResult.then(
|
|
47
|
+
(deferredData) => deferredData.data[deferId]
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
state = prefetchResult.data[deferId];
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
state = prefetchResult;
|
|
54
|
+
}
|
|
55
|
+
const [prefetchState, setPrefetchState] = useState(
|
|
56
|
+
state
|
|
57
|
+
);
|
|
58
|
+
const prefetchInstance = MFDataPrefetch.getInstance(mfScope);
|
|
59
|
+
useEffect2(() => {
|
|
60
|
+
const useEffectTiming = performance.now();
|
|
61
|
+
logger_default.info(
|
|
62
|
+
`3. Start Execute UseEffect: ${options.id} - ${options.functionId} - ${useEffectTiming}`
|
|
63
|
+
);
|
|
64
|
+
return () => {
|
|
65
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
66
|
+
};
|
|
67
|
+
}, []);
|
|
68
|
+
const refreshExecutor = (refetchParams) => {
|
|
69
|
+
const refetchOptions = {
|
|
70
|
+
...options
|
|
71
|
+
};
|
|
72
|
+
if (refetchParams) {
|
|
73
|
+
refetchOptions.refetchParams = refetchParams;
|
|
74
|
+
}
|
|
75
|
+
prefetchInstance == null ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
|
|
76
|
+
const newVal = prefetch(refetchOptions);
|
|
77
|
+
let newState;
|
|
78
|
+
if (deferId) {
|
|
79
|
+
if (newVal instanceof Promise) {
|
|
80
|
+
newState = newVal.then((deferredData) => deferredData.data[deferId]);
|
|
81
|
+
} else {
|
|
82
|
+
newState = newVal.data[deferId];
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
newState = newVal;
|
|
86
|
+
}
|
|
87
|
+
setPrefetchState(newState);
|
|
88
|
+
};
|
|
89
|
+
return [prefetchState, refreshExecutor];
|
|
90
|
+
};
|
|
91
|
+
export {
|
|
92
|
+
usePrefetch
|
|
93
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { D as DataPrefetchOptions, M as MFDataPrefetch, p as prefetchOptions } from './prefetch-4e9646e4.js';
|
|
2
|
+
export { default as prefetchPlugin } from './plugin.js';
|
|
3
|
+
import '@module-federation/runtime';
|
|
4
|
+
import '@module-federation/sdk';
|
|
5
|
+
import '@module-federation/runtime/types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
MFDataPrefetch: () => MFDataPrefetch,
|
|
24
|
+
prefetchPlugin: () => prefetchPlugin
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/prefetch.ts
|
|
29
|
+
var import_runtime = require("@module-federation/runtime");
|
|
30
|
+
var import_sdk2 = require("@module-federation/sdk");
|
|
31
|
+
|
|
32
|
+
// src/common/runtime-utils.ts
|
|
33
|
+
var import_sdk = require("@module-federation/sdk");
|
|
34
|
+
var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
|
|
35
|
+
var getSignalFromManifest = (remoteSnapshot) => {
|
|
36
|
+
if (!remoteSnapshot) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (!("prefetchEntry" in remoteSnapshot) && !("prefetchInterface" in remoteSnapshot)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/prefetch.ts
|
|
49
|
+
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
50
|
+
var _a;
|
|
51
|
+
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
52
|
+
entryLoading: {},
|
|
53
|
+
instance: /* @__PURE__ */ new Map(),
|
|
54
|
+
__PREFETCH_EXPORTS__: {}
|
|
55
|
+
});
|
|
56
|
+
var MFDataPrefetch = class {
|
|
57
|
+
constructor(options) {
|
|
58
|
+
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
59
|
+
this.recordOutdate = {};
|
|
60
|
+
this._exports = {};
|
|
61
|
+
this._options = options;
|
|
62
|
+
this.global.instance.set(options.name, this);
|
|
63
|
+
}
|
|
64
|
+
get global() {
|
|
65
|
+
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
66
|
+
}
|
|
67
|
+
static getInstance(id) {
|
|
68
|
+
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
69
|
+
}
|
|
70
|
+
async loadEntry(entry) {
|
|
71
|
+
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
72
|
+
if (entry) {
|
|
73
|
+
const { buildVersion, globalName } = remoteSnapshot;
|
|
74
|
+
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
75
|
+
if (!this.global.entryLoading[uniqueKey]) {
|
|
76
|
+
this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
|
|
77
|
+
}
|
|
78
|
+
return this.global.entryLoading[uniqueKey];
|
|
79
|
+
} else {
|
|
80
|
+
const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
|
|
81
|
+
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
82
|
+
return (0, import_runtime.getRemoteEntry)({
|
|
83
|
+
remoteInfo,
|
|
84
|
+
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
|
|
85
|
+
createScriptHook: (url) => {
|
|
86
|
+
const res = origin.loaderHook.lifecycle.createScript.emit({
|
|
87
|
+
url
|
|
88
|
+
});
|
|
89
|
+
if (res instanceof HTMLScriptElement) {
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
getProjectExports() {
|
|
98
|
+
var _a2;
|
|
99
|
+
if (Object.keys(this._exports).length > 0) {
|
|
100
|
+
return this._exports;
|
|
101
|
+
}
|
|
102
|
+
const { name } = this._options;
|
|
103
|
+
const exportsPromise = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
104
|
+
const resolve = exportsPromise.then(
|
|
105
|
+
(exports = {}) => {
|
|
106
|
+
const memory = {};
|
|
107
|
+
Object.keys(exports).forEach((key) => {
|
|
108
|
+
memory[key] = {};
|
|
109
|
+
const exportVal = exports[key];
|
|
110
|
+
Object.keys(exportVal).reduce(
|
|
111
|
+
(memo, current) => {
|
|
112
|
+
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
113
|
+
memo[current] = exportVal[current];
|
|
114
|
+
}
|
|
115
|
+
return memo;
|
|
116
|
+
},
|
|
117
|
+
memory[key]
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
this.memorizeExports(memory);
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
return resolve;
|
|
124
|
+
}
|
|
125
|
+
memorizeExports(exports) {
|
|
126
|
+
this._exports = exports;
|
|
127
|
+
}
|
|
128
|
+
getExposeExports(id) {
|
|
129
|
+
const prefetchId = getPrefetchId(id);
|
|
130
|
+
const prefetchExports = this._exports[prefetchId];
|
|
131
|
+
return prefetchExports || {};
|
|
132
|
+
}
|
|
133
|
+
prefetch(prefetchOptions) {
|
|
134
|
+
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
135
|
+
let prefetchResult;
|
|
136
|
+
const prefetchId = getPrefetchId(id);
|
|
137
|
+
const memorizeId = id + functionId;
|
|
138
|
+
const memory = this.prefetchMemory.get(memorizeId);
|
|
139
|
+
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
140
|
+
return memory;
|
|
141
|
+
}
|
|
142
|
+
const prefetchExports = this._exports[prefetchId];
|
|
143
|
+
if (!prefetchExports) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const executePrefetch = prefetchExports[functionId];
|
|
147
|
+
if (typeof executePrefetch === "function") {
|
|
148
|
+
if (refetchParams) {
|
|
149
|
+
prefetchResult = executePrefetch(refetchParams);
|
|
150
|
+
} else {
|
|
151
|
+
prefetchResult = executePrefetch();
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
this.memorize(memorizeId, prefetchResult);
|
|
159
|
+
return prefetchResult;
|
|
160
|
+
}
|
|
161
|
+
memorize(id, value) {
|
|
162
|
+
this.prefetchMemory.set(id, value);
|
|
163
|
+
}
|
|
164
|
+
markOutdate(markOptions, isOutdate) {
|
|
165
|
+
const { id, functionId = "default" } = markOptions;
|
|
166
|
+
if (!this.recordOutdate[id]) {
|
|
167
|
+
this.recordOutdate[id] = {};
|
|
168
|
+
}
|
|
169
|
+
this.recordOutdate[id][functionId] = isOutdate;
|
|
170
|
+
}
|
|
171
|
+
checkOutdate(outdateOptions) {
|
|
172
|
+
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
173
|
+
if (typeof cacheStrategy === "function") {
|
|
174
|
+
return cacheStrategy();
|
|
175
|
+
}
|
|
176
|
+
if (!this.recordOutdate[id]) {
|
|
177
|
+
this.recordOutdate[id] = {};
|
|
178
|
+
}
|
|
179
|
+
if (this.recordOutdate[id][functionId]) {
|
|
180
|
+
this.markOutdate(
|
|
181
|
+
{
|
|
182
|
+
id,
|
|
183
|
+
functionId
|
|
184
|
+
},
|
|
185
|
+
false
|
|
186
|
+
);
|
|
187
|
+
return true;
|
|
188
|
+
} else {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// src/plugin.ts
|
|
195
|
+
var import_sdk4 = require("@module-federation/sdk");
|
|
196
|
+
|
|
197
|
+
// src/logger/index.ts
|
|
198
|
+
var import_sdk3 = require("@module-federation/sdk");
|
|
199
|
+
var logger_default = new import_sdk3.Logger("[Module Federation Data Prefetch]");
|
|
200
|
+
|
|
201
|
+
// src/plugin.ts
|
|
202
|
+
var loadingArray = [];
|
|
203
|
+
var prefetchPlugin = () => ({
|
|
204
|
+
name: "data-prefetch-runtime-plugin",
|
|
205
|
+
afterResolve(options) {
|
|
206
|
+
const { remoteSnapshot, remoteInfo, id, origin } = options;
|
|
207
|
+
const snapshot = remoteSnapshot;
|
|
208
|
+
const { name } = remoteInfo;
|
|
209
|
+
const prefetchOptions = {
|
|
210
|
+
name,
|
|
211
|
+
remote: remoteInfo,
|
|
212
|
+
origin,
|
|
213
|
+
remoteSnapshot: snapshot
|
|
214
|
+
};
|
|
215
|
+
const signal = getSignalFromManifest(snapshot);
|
|
216
|
+
if (!signal) {
|
|
217
|
+
return options;
|
|
218
|
+
}
|
|
219
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
220
|
+
let prefetchUrl;
|
|
221
|
+
if (snapshot.prefetchEntry) {
|
|
222
|
+
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
223
|
+
}
|
|
224
|
+
const exist = loadingArray.find((loading) => loading.id === id);
|
|
225
|
+
if (exist) {
|
|
226
|
+
return options;
|
|
227
|
+
}
|
|
228
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
229
|
+
const projectExports = instance.getProjectExports();
|
|
230
|
+
if (projectExports instanceof Promise) {
|
|
231
|
+
await projectExports;
|
|
232
|
+
}
|
|
233
|
+
const exports = instance.getExposeExports(id);
|
|
234
|
+
logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
|
|
235
|
+
const result = Object.keys(exports).map((k) => {
|
|
236
|
+
const value = instance.prefetch({
|
|
237
|
+
id,
|
|
238
|
+
functionId: k
|
|
239
|
+
});
|
|
240
|
+
const functionId = k;
|
|
241
|
+
return {
|
|
242
|
+
value,
|
|
243
|
+
functionId
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
return result;
|
|
247
|
+
});
|
|
248
|
+
loadingArray.push({
|
|
249
|
+
id,
|
|
250
|
+
promise
|
|
251
|
+
});
|
|
252
|
+
return options;
|
|
253
|
+
},
|
|
254
|
+
async onLoad(options) {
|
|
255
|
+
var _a2;
|
|
256
|
+
const { remote, id } = options;
|
|
257
|
+
const { name } = remote;
|
|
258
|
+
const promise = (_a2 = loadingArray.find((loading) => loading.id === id)) == null ? void 0 : _a2.promise;
|
|
259
|
+
if (promise) {
|
|
260
|
+
const prefetch = await promise;
|
|
261
|
+
const prefetchValue = prefetch.map((result) => result.value);
|
|
262
|
+
await Promise.all(prefetchValue);
|
|
263
|
+
const instance = MFDataPrefetch.getInstance(name);
|
|
264
|
+
prefetch.forEach((result) => {
|
|
265
|
+
const { value, functionId } = result;
|
|
266
|
+
instance.memorize(id + functionId, value);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return options;
|
|
270
|
+
},
|
|
271
|
+
handlePreloadModule(options) {
|
|
272
|
+
const { remoteSnapshot, name, id, preloadConfig, origin, remote } = options;
|
|
273
|
+
const snapshot = remoteSnapshot;
|
|
274
|
+
const signal = getSignalFromManifest(snapshot);
|
|
275
|
+
if (!signal) {
|
|
276
|
+
return options;
|
|
277
|
+
}
|
|
278
|
+
const prefetchOptions = {
|
|
279
|
+
name,
|
|
280
|
+
origin,
|
|
281
|
+
remote,
|
|
282
|
+
remoteSnapshot: snapshot
|
|
283
|
+
};
|
|
284
|
+
const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
|
|
285
|
+
let prefetchUrl;
|
|
286
|
+
if (snapshot.prefetchEntry) {
|
|
287
|
+
prefetchUrl = (0, import_sdk4.getResourceUrl)(snapshot, snapshot.prefetchEntry);
|
|
288
|
+
}
|
|
289
|
+
if (!preloadConfig.prefetchInterface) {
|
|
290
|
+
instance.loadEntry(prefetchUrl);
|
|
291
|
+
return options;
|
|
292
|
+
}
|
|
293
|
+
const promise = instance.loadEntry(prefetchUrl).then(async () => {
|
|
294
|
+
const projectExports = instance.getProjectExports();
|
|
295
|
+
if (projectExports instanceof Promise) {
|
|
296
|
+
await projectExports;
|
|
297
|
+
}
|
|
298
|
+
const exports = instance.getExposeExports(id);
|
|
299
|
+
logger_default.info(`1. Start Prefetch: ${id} - ${performance.now()}`);
|
|
300
|
+
const result = Object.keys(exports).map((k) => {
|
|
301
|
+
const value = instance.prefetch({
|
|
302
|
+
id,
|
|
303
|
+
functionId: k
|
|
304
|
+
});
|
|
305
|
+
const functionId = k;
|
|
306
|
+
return {
|
|
307
|
+
value,
|
|
308
|
+
functionId
|
|
309
|
+
};
|
|
310
|
+
});
|
|
311
|
+
return result;
|
|
312
|
+
});
|
|
313
|
+
loadingArray.push({
|
|
314
|
+
id,
|
|
315
|
+
promise
|
|
316
|
+
});
|
|
317
|
+
return options;
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
321
|
+
0 && (module.exports = {
|
|
322
|
+
MFDataPrefetch,
|
|
323
|
+
prefetchPlugin
|
|
324
|
+
});
|