remote-reload-utils 0.0.6 → 0.0.7
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/loadRemote.d.ts +3 -2
- package/dist/main.cjs +16 -4
- package/dist/main.js +16 -4
- package/dist/plugins.d.ts +2 -0
- package/package.json +1 -1
package/dist/loadRemote.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
|
|
2
|
+
import type { LoadRemoteOptions } from './types';
|
|
2
3
|
/**
|
|
3
4
|
* 多版本共存的 loadRemote
|
|
4
5
|
*/
|
|
5
|
-
export declare function loadRemoteMultiVersion(options: LoadRemoteOptions): Promise<{
|
|
6
|
+
export declare function loadRemoteMultiVersion(options: LoadRemoteOptions, plugins: ModuleFederationRuntimePlugin[]): Promise<{
|
|
6
7
|
scopeName: string;
|
|
7
8
|
mf: import("@module-federation/enhanced/runtime").ModuleFederation;
|
|
8
9
|
}>;
|
package/dist/main.cjs
CHANGED
|
@@ -27,6 +27,14 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
27
27
|
loadRemoteMultiVersion: ()=>loadRemoteMultiVersion
|
|
28
28
|
});
|
|
29
29
|
const runtime_namespaceObject = require("@module-federation/enhanced/runtime");
|
|
30
|
+
const fallbackPlugin = ()=>({
|
|
31
|
+
name: 'fallback-plugin',
|
|
32
|
+
errorLoadRemote (args) {
|
|
33
|
+
const fallback = 'fallback';
|
|
34
|
+
console.log(args, 'args');
|
|
35
|
+
return fallback;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
30
38
|
async function fetchLatestVersion(pkg) {
|
|
31
39
|
const res = await fetch(`https://registry.npmjs.org/${pkg}`);
|
|
32
40
|
if (!res.ok) throw new Error(`无法获取 ${pkg} 的版本信息`);
|
|
@@ -48,8 +56,8 @@ function setVersionCache(pkg, version) {
|
|
|
48
56
|
};
|
|
49
57
|
localStorage.setItem('mf-multi-version', JSON.stringify(cache));
|
|
50
58
|
}
|
|
51
|
-
async function loadRemoteMultiVersion(options) {
|
|
52
|
-
|
|
59
|
+
async function loadRemoteMultiVersion(options, plugins) {
|
|
60
|
+
const { name, pkg, version = 'latest', retries = 3, delay = 1000, localFallback, cacheTTL = 86400000, revalidate = true } = options;
|
|
53
61
|
let finalVersion = version;
|
|
54
62
|
if ('latest' === version) {
|
|
55
63
|
const cache = getVersionCache();
|
|
@@ -71,7 +79,7 @@ async function loadRemoteMultiVersion(options) {
|
|
|
71
79
|
`https://unpkg.com/${pkg}@${finalVersion}/dist/remoteEntry.js`
|
|
72
80
|
];
|
|
73
81
|
if (localFallback) urls.push(localFallback);
|
|
74
|
-
for (
|
|
82
|
+
for (const url of urls)for(let i = 0; i < retries; i++)try {
|
|
75
83
|
const mf = (0, runtime_namespaceObject.createInstance)({
|
|
76
84
|
name: 'host',
|
|
77
85
|
remotes: [
|
|
@@ -95,7 +103,11 @@ async function loadRemoteMultiVersion(options) {
|
|
|
95
103
|
requiredVersion: false
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
|
-
}
|
|
106
|
+
},
|
|
107
|
+
plugins: [
|
|
108
|
+
...plugins,
|
|
109
|
+
fallbackPlugin()
|
|
110
|
+
]
|
|
99
111
|
});
|
|
100
112
|
return {
|
|
101
113
|
scopeName,
|
package/dist/main.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { createInstance } from "@module-federation/enhanced/runtime";
|
|
2
|
+
const fallbackPlugin = ()=>({
|
|
3
|
+
name: 'fallback-plugin',
|
|
4
|
+
errorLoadRemote (args) {
|
|
5
|
+
const fallback = 'fallback';
|
|
6
|
+
console.log(args, 'args');
|
|
7
|
+
return fallback;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
2
10
|
async function fetchLatestVersion(pkg) {
|
|
3
11
|
const res = await fetch(`https://registry.npmjs.org/${pkg}`);
|
|
4
12
|
if (!res.ok) throw new Error(`无法获取 ${pkg} 的版本信息`);
|
|
@@ -20,8 +28,8 @@ function setVersionCache(pkg, version) {
|
|
|
20
28
|
};
|
|
21
29
|
localStorage.setItem('mf-multi-version', JSON.stringify(cache));
|
|
22
30
|
}
|
|
23
|
-
async function loadRemoteMultiVersion(options) {
|
|
24
|
-
|
|
31
|
+
async function loadRemoteMultiVersion(options, plugins) {
|
|
32
|
+
const { name, pkg, version = 'latest', retries = 3, delay = 1000, localFallback, cacheTTL = 86400000, revalidate = true } = options;
|
|
25
33
|
let finalVersion = version;
|
|
26
34
|
if ('latest' === version) {
|
|
27
35
|
const cache = getVersionCache();
|
|
@@ -43,7 +51,7 @@ async function loadRemoteMultiVersion(options) {
|
|
|
43
51
|
`https://unpkg.com/${pkg}@${finalVersion}/dist/remoteEntry.js`
|
|
44
52
|
];
|
|
45
53
|
if (localFallback) urls.push(localFallback);
|
|
46
|
-
for (
|
|
54
|
+
for (const url of urls)for(let i = 0; i < retries; i++)try {
|
|
47
55
|
const mf = createInstance({
|
|
48
56
|
name: 'host',
|
|
49
57
|
remotes: [
|
|
@@ -67,7 +75,11 @@ async function loadRemoteMultiVersion(options) {
|
|
|
67
75
|
requiredVersion: false
|
|
68
76
|
}
|
|
69
77
|
}
|
|
70
|
-
}
|
|
78
|
+
},
|
|
79
|
+
plugins: [
|
|
80
|
+
...plugins,
|
|
81
|
+
fallbackPlugin()
|
|
82
|
+
]
|
|
71
83
|
});
|
|
72
84
|
return {
|
|
73
85
|
scopeName,
|