remote-reload-utils 0.0.6 → 0.0.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.
@@ -1,8 +1,9 @@
1
- import { LoadRemoteOptions } from './types';
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
  }>;
@@ -0,0 +1,11 @@
1
+ import { createInstance, ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2
+ import type { LoadRemoteOptions } from './types';
3
+ interface LoadResult {
4
+ scopeName: string;
5
+ mf: ReturnType<typeof createInstance>;
6
+ }
7
+ /**
8
+ * 多版本共存的 loadRemote (优化版本)
9
+ */
10
+ export declare function loadRemoteMultiVersion(options: LoadRemoteOptions, plugins: ModuleFederationRuntimePlugin[]): Promise<LoadResult>;
11
+ export {};
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
- let { name, pkg, version = 'latest', retries = 3, delay = 1000, localFallback, cacheTTL = 86400000, revalidate = true } = options;
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 (let url of urls)for(let i = 0; i < retries; i++)try {
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
- let { name, pkg, version = 'latest', retries = 3, delay = 1000, localFallback, cacheTTL = 86400000, revalidate = true } = options;
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 (let url of urls)for(let i = 0; i < retries; i++)try {
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,
@@ -0,0 +1,2 @@
1
+ import type { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2
+ export declare const fallbackPlugin: () => ModuleFederationRuntimePlugin;
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ModuleFederationRuntimePlugin } from "@module-federation/enhanced/runtime";
1
2
  export interface LoadRemoteOptions {
2
3
  name: string;
3
4
  pkg: string;
@@ -7,6 +8,7 @@ export interface LoadRemoteOptions {
7
8
  localFallback?: string;
8
9
  cacheTTL?: number;
9
10
  revalidate?: boolean;
11
+ shared?: Record<string, ModuleFederationRuntimePlugin>;
10
12
  }
11
13
  export interface VersionCache {
12
14
  [pkg: string]: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remote-reload-utils",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "description": "Utilities for remote reload in Module Federation & React Component",
6
6
  "exports": {